2 * Copyright (C) <2005,2006> Wim Taymans <wim at fluendo dot com>
3 * <2006> Lutz Mueller <lutz at topfrose dot de>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 * Unless otherwise indicated, Source Code is licensed under MIT license.
22 * See further explanation attached in License Statement (distributed in the file
25 * Permission is hereby granted, free of charge, to any person obtaining a copy of
26 * this software and associated documentation files (the "Software"), to deal in
27 * the Software without restriction, including without limitation the rights to
28 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
29 * of the Software, and to permit persons to whom the Software is furnished to do
30 * so, subject to the following conditions:
32 * The above copyright notice and this permission notice shall be included in all
33 * copies or substantial portions of the Software.
35 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
44 * SECTION:element-rtspsrc
46 * Makes a connection to an RTSP server and read the data.
47 * rtspsrc strictly follows RFC 2326 and therefore does not (yet) support
48 * RealMedia/Quicktime/Microsoft extensions.
50 * RTSP supports transport over TCP or UDP in unicast or multicast mode. By
51 * default rtspsrc will negotiate a connection in the following order:
52 * UDP unicast/UDP multicast/TCP. The order cannot be changed but the allowed
53 * protocols can be controlled with the #GstRTSPSrc:protocols property.
55 * rtspsrc currently understands SDP as the format of the session description.
56 * For each stream listed in the SDP a new rtp_stream%d pad will be created
57 * with caps derived from the SDP media description. This is a caps of mime type
58 * "application/x-rtp" that can be connected to any available RTP depayloader
61 * rtspsrc will internally instantiate an RTP session manager element
62 * that will handle the RTCP messages to and from the server, jitter removal,
63 * packet reordering along with providing a clock for the pipeline.
64 * This feature is implemented using the gstrtpbin element.
66 * rtspsrc acts like a live source and will therefore only generate data in the
70 * <title>Example launch line</title>
72 * gst-launch-1.0 rtspsrc location=rtsp://some.server/url ! fakesink
73 * ]| Establish a connection to an RTSP server and send the raw RTP packets to a
77 * Last reviewed on 2006-08-18 (0.10.5)
86 #endif /* HAVE_UNISTD_H */
92 #include <gst/net/gstnet.h>
93 #include <gst/sdp/gstsdpmessage.h>
94 #include <gst/rtp/gstrtppayloads.h>
96 #include "gst/gst-i18n-plugin.h"
98 #include "gstrtspsrc.h"
100 GST_DEBUG_CATEGORY_STATIC (rtspsrc_debug);
101 #define GST_CAT_DEFAULT (rtspsrc_debug)
103 static GstStaticPadTemplate rtptemplate = GST_STATIC_PAD_TEMPLATE ("stream_%u",
106 GST_STATIC_CAPS ("application/x-rtp; application/x-rdt"));
108 /* templates used internally */
109 static GstStaticPadTemplate anysrctemplate =
110 GST_STATIC_PAD_TEMPLATE ("internalsrc_%u",
113 GST_STATIC_CAPS_ANY);
115 static GstStaticPadTemplate anysinktemplate =
116 GST_STATIC_PAD_TEMPLATE ("internalsink_%u",
119 GST_STATIC_CAPS_ANY);
123 SIGNAL_HANDLE_REQUEST,
125 SIGNAL_SELECT_STREAM,
130 enum _GstRtspSrcRtcpSyncMode
137 enum _GstRtspSrcBufferMode
146 #define GST_TYPE_RTSP_SRC_BUFFER_MODE (gst_rtsp_src_buffer_mode_get_type())
148 gst_rtsp_src_buffer_mode_get_type (void)
150 static GType buffer_mode_type = 0;
151 static const GEnumValue buffer_modes[] = {
152 {BUFFER_MODE_NONE, "Only use RTP timestamps", "none"},
153 {BUFFER_MODE_SLAVE, "Slave receiver to sender clock", "slave"},
154 {BUFFER_MODE_BUFFER, "Do low/high watermark buffering", "buffer"},
155 {BUFFER_MODE_AUTO, "Choose mode depending on stream live", "auto"},
156 {BUFFER_MODE_SYNCED, "Synchronized sender and receiver clocks", "synced"},
160 if (!buffer_mode_type) {
162 g_enum_register_static ("GstRTSPSrcBufferMode", buffer_modes);
164 return buffer_mode_type;
167 #define DEFAULT_LOCATION NULL
168 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
169 #define DEFAULT_DEBUG FALSE
170 #define DEFAULT_RETRY 20
171 #define DEFAULT_TIMEOUT 5000000
172 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
173 #define DEFAULT_TCP_TIMEOUT 20000000
174 #define DEFAULT_LATENCY_MS 2000
175 #define DEFAULT_DROP_ON_LATENCY FALSE
176 #define DEFAULT_CONNECTION_SPEED 0
177 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
178 #define DEFAULT_DO_RTCP TRUE
179 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
180 #define DEFAULT_PROXY NULL
181 #define DEFAULT_RTP_BLOCKSIZE 0
182 #define DEFAULT_USER_ID NULL
183 #define DEFAULT_USER_PW NULL
184 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
185 #define DEFAULT_PORT_RANGE NULL
186 #define DEFAULT_SHORT_HEADER FALSE
187 #define DEFAULT_PROBATION 2
188 #define DEFAULT_UDP_RECONNECT TRUE
189 #define DEFAULT_MULTICAST_IFACE NULL
190 #define DEFAULT_NTP_SYNC FALSE
191 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
192 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
193 #define DEFAULT_TLS_DATABASE NULL
205 PROP_DROP_ON_LATENCY,
206 PROP_CONNECTION_SPEED,
209 PROP_DO_RTSP_KEEP_ALIVE,
218 PROP_UDP_BUFFER_SIZE,
222 PROP_MULTICAST_IFACE,
224 PROP_USE_PIPELINE_CLOCK,
226 PROP_TLS_VALIDATION_FLAGS,
231 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
233 gst_rtsp_nat_method_get_type (void)
235 static GType rtsp_nat_method_type = 0;
236 static const GEnumValue rtsp_nat_method[] = {
237 {GST_RTSP_NAT_NONE, "None", "none"},
238 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
242 if (!rtsp_nat_method_type) {
243 rtsp_nat_method_type =
244 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
246 return rtsp_nat_method_type;
249 static void gst_rtspsrc_finalize (GObject * object);
251 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
252 const GValue * value, GParamSpec * pspec);
253 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
254 GValue * value, GParamSpec * pspec);
256 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
258 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
259 gpointer iface_data);
261 static void gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes,
264 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
265 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
267 static GstCaps *gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media);
269 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
270 GstStateChange transition);
271 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
272 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
274 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
275 GstRTSPMessage * response);
277 static gboolean gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
279 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
280 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
282 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
283 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
285 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
286 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
287 gboolean only_close);
289 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
290 const gchar * uri, GError ** error);
291 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
293 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
294 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
295 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
296 GstRTSPStream * stream, GstEvent * event);
297 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
298 static void gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush);
306 /* commands we send to out loop to notify it of events */
307 #define CMD_OPEN (1 << 0)
308 #define CMD_PLAY (1 << 1)
309 #define CMD_PAUSE (1 << 2)
310 #define CMD_CLOSE (1 << 3)
311 #define CMD_WAIT (1 << 4)
312 #define CMD_RECONNECT (1 << 5)
313 #define CMD_LOOP (1 << 6)
315 /* mask for all commands */
316 #define CMD_ALL ((CMD_LOOP << 1) - 1)
318 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
320 gchar *__txt = _gst_element_error_printf text; \
321 gst_element_post_message (GST_ELEMENT_CAST (el), \
322 gst_message_new_progress (GST_OBJECT_CAST (el), \
323 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
327 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
329 #define gst_rtspsrc_parent_class parent_class
330 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
331 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
334 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
336 GST_DEBUG_OBJECT (src, "default handler");
341 select_stream_accum (GSignalInvocationHint * ihint,
342 GValue * return_accu, const GValue * handler_return, gpointer data)
346 myboolean = g_value_get_boolean (handler_return);
347 GST_DEBUG ("accum %d", myboolean);
348 g_value_set_boolean (return_accu, myboolean);
350 /* stop emission if FALSE */
355 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
357 GObjectClass *gobject_class;
358 GstElementClass *gstelement_class;
359 GstBinClass *gstbin_class;
361 gobject_class = (GObjectClass *) klass;
362 gstelement_class = (GstElementClass *) klass;
363 gstbin_class = (GstBinClass *) klass;
365 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
367 gobject_class->set_property = gst_rtspsrc_set_property;
368 gobject_class->get_property = gst_rtspsrc_get_property;
370 gobject_class->finalize = gst_rtspsrc_finalize;
372 g_object_class_install_property (gobject_class, PROP_LOCATION,
373 g_param_spec_string ("location", "RTSP Location",
374 "Location of the RTSP url to read",
375 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
377 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
378 g_param_spec_flags ("protocols", "Protocols",
379 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
380 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
382 g_object_class_install_property (gobject_class, PROP_DEBUG,
383 g_param_spec_boolean ("debug", "Debug",
384 "Dump request and response messages to stdout",
385 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
387 g_object_class_install_property (gobject_class, PROP_RETRY,
388 g_param_spec_uint ("retry", "Retry",
389 "Max number of retries when allocating RTP ports.",
390 0, G_MAXUINT16, DEFAULT_RETRY,
391 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
393 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
394 g_param_spec_uint64 ("timeout", "Timeout",
395 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
396 0, G_MAXUINT64, DEFAULT_TIMEOUT,
397 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
399 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
400 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
401 "Fail after timeout microseconds on TCP connections (0 = disabled)",
402 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
403 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
405 g_object_class_install_property (gobject_class, PROP_LATENCY,
406 g_param_spec_uint ("latency", "Buffer latency in ms",
407 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
408 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
410 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
411 g_param_spec_boolean ("drop-on-latency",
412 "Drop buffers when maximum latency is reached",
413 "Tells the jitterbuffer to never exceed the given latency in size",
414 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
416 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
417 g_param_spec_uint64 ("connection-speed", "Connection Speed",
418 "Network connection speed in kbps (0 = unknown)",
419 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
420 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
422 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
423 g_param_spec_enum ("nat-method", "NAT Method",
424 "Method to use for traversing firewalls and NAT",
425 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
426 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
429 * GstRTSPSrc:do-rtcp:
431 * Enable RTCP support. Some old server don't like RTCP and then this property
432 * needs to be set to FALSE.
434 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
435 g_param_spec_boolean ("do-rtcp", "Do RTCP",
436 "Send RTCP packets, disable for old incompatible server.",
437 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
440 * GstRTSPSrc:do-rtsp-keep-alive:
442 * Enable RTSP keep alive support. Some old server don't like RTSP
443 * keep alive and then this property needs to be set to FALSE.
445 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
446 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
447 "Send RTSP keep alive packets, disable for old incompatible server.",
448 DEFAULT_DO_RTSP_KEEP_ALIVE,
449 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
454 * Set the proxy parameters. This has to be a string of the format
455 * [http://][user:passwd@]host[:port].
457 g_object_class_install_property (gobject_class, PROP_PROXY,
458 g_param_spec_string ("proxy", "Proxy",
459 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
460 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
462 * GstRTSPSrc:proxy-id:
464 * Sets the proxy URI user id for authentication. If the URI set via the
465 * "proxy" property contains a user-id already, that will take precedence.
469 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
470 g_param_spec_string ("proxy-id", "proxy-id",
471 "HTTP proxy URI user id for authentication", "",
472 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
474 * GstRTSPSrc:proxy-pw:
476 * Sets the proxy URI password for authentication. If the URI set via the
477 * "proxy" property contains a password already, that will take precedence.
481 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
482 g_param_spec_string ("proxy-pw", "proxy-pw",
483 "HTTP proxy URI user password for authentication", "",
484 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
487 * GstRTSPSrc:rtp-blocksize:
489 * RTP package size to suggest to server.
491 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
492 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
493 "RTP package size to suggest to server (0 = disabled)",
494 0, 65536, DEFAULT_RTP_BLOCKSIZE,
495 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
497 g_object_class_install_property (gobject_class,
499 g_param_spec_string ("user-id", "user-id",
500 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
501 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
502 g_object_class_install_property (gobject_class, PROP_USER_PW,
503 g_param_spec_string ("user-pw", "user-pw",
504 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
505 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
508 * GstRTSPSrc:buffer-mode:
510 * 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
524 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
525 g_param_spec_string ("port-range", "Port range",
526 "Client port range that can be used to receive RTP and RTCP data, "
527 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
528 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
531 * GstRTSPSrc:udp-buffer-size:
533 * Size of the kernel UDP receive buffer in bytes.
535 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
536 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
537 "Size of the kernel UDP receive buffer in bytes, 0=default",
538 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
539 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
542 * GstRTSPSrc:short-header:
544 * Only send the basic RTSP headers for broken encoders.
546 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
547 g_param_spec_boolean ("short-header", "Short Header",
548 "Only send the basic RTSP headers for broken encoders",
549 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
551 g_object_class_install_property (gobject_class, PROP_PROBATION,
552 g_param_spec_uint ("probation", "Number of probations",
553 "Consecutive packet sequence numbers to accept the source",
554 0, G_MAXUINT, DEFAULT_PROBATION,
555 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
557 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
558 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
559 "Reconnect to the server if RTSP connection is closed when doing UDP",
560 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
562 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
563 g_param_spec_string ("multicast-iface", "Multicast Interface",
564 "The network interface on which to join the multicast group",
565 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
567 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
568 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
569 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
570 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
572 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
573 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
574 "Use the pipeline running-time to set the NTP time in the RTCP SR messages",
575 DEFAULT_USE_PIPELINE_CLOCK,
576 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
578 g_object_class_install_property (gobject_class, PROP_SDES,
579 g_param_spec_boxed ("sdes", "SDES",
580 "The SDES items of this session",
581 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
584 * GstRTSPSrc::tls-validation-flags:
586 * TLS certificate validation flags used to validate server
591 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
592 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
593 "TLS certificate validation flags used to validate the server certificate",
594 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
595 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
598 * GstRTSPSrc::tls-database:
600 * TLS database with anchor certificate authorities used to validate
601 * the server certificate.
605 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
606 g_param_spec_object ("tls-database", "TLS database",
607 "TLS database with anchor certificate authorities used to validate the server certificate",
608 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
611 * GstRTSPSrc::handle-request:
612 * @rtspsrc: a #GstRTSPSrc
613 * @request: a #GstRTSPMessage
614 * @response: a #GstRTSPMessage
616 * Handle a server request in @request and prepare @response.
618 * This signal is called from the streaming thread, you should therefore not
619 * do any state changes on @rtspsrc because this might deadlock. If you want
620 * to modify the state as a result of this signal, post a
621 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
626 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
627 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
628 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
629 G_TYPE_POINTER, G_TYPE_POINTER);
632 * GstRTSPSrc::on-sdp:
633 * @rtspsrc: a #GstRTSPSrc
634 * @sdp: a #GstSDPMessage
636 * Emited when the client has retrieved the SDP and before it configures the
637 * streams in the SDP. @sdp can be inspected and modified.
639 * This signal is called from the streaming thread, you should therefore not
640 * do any state changes on @rtspsrc because this might deadlock. If you want
641 * to modify the state as a result of this signal, post a
642 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
647 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
648 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
649 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
650 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
653 * GstRTSPSrc::select-stream:
654 * @rtspsrc: a #GstRTSPSrc
655 * @num: the stream number
656 * @caps: the stream caps
658 * Emited before the client decides to configure the stream @num with
661 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
666 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
667 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
668 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
669 (GCallback) default_select_stream, select_stream_accum, NULL,
670 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, G_TYPE_UINT,
673 * GstRTSPSrc::new-manager:
674 * @rtspsrc: a #GstRTSPSrc
675 * @manager: a #GstElement
677 * Emited after a new manager (like rtpbin) was created and the default
678 * properties were configured.
682 gst_rtspsrc_signals[SIGNAL_NEW_MANAGER] =
683 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
684 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
685 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
687 gstelement_class->send_event = gst_rtspsrc_send_event;
688 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
689 gstelement_class->change_state = gst_rtspsrc_change_state;
691 gst_element_class_add_pad_template (gstelement_class,
692 gst_static_pad_template_get (&rtptemplate));
694 gst_element_class_set_static_metadata (gstelement_class,
695 "RTSP packet receiver", "Source/Network",
696 "Receive data over the network via RTSP (RFC 2326)",
697 "Wim Taymans <wim@fluendo.com>, "
698 "Thijs Vermeir <thijs.vermeir@barco.com>, "
699 "Lutz Mueller <lutz@topfrose.de>");
701 gstbin_class->handle_message = gst_rtspsrc_handle_message;
703 gst_rtsp_ext_list_init ();
707 gst_rtspsrc_init (GstRTSPSrc * src)
709 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
710 src->protocols = DEFAULT_PROTOCOLS;
711 src->debug = DEFAULT_DEBUG;
712 src->retry = DEFAULT_RETRY;
713 src->udp_timeout = DEFAULT_TIMEOUT;
714 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
715 src->latency = DEFAULT_LATENCY_MS;
716 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
717 src->connection_speed = DEFAULT_CONNECTION_SPEED;
718 src->nat_method = DEFAULT_NAT_METHOD;
719 src->do_rtcp = DEFAULT_DO_RTCP;
720 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
721 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
722 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
723 src->user_id = g_strdup (DEFAULT_USER_ID);
724 src->user_pw = g_strdup (DEFAULT_USER_PW);
725 src->buffer_mode = DEFAULT_BUFFER_MODE;
726 src->client_port_range.min = 0;
727 src->client_port_range.max = 0;
728 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
729 src->short_header = DEFAULT_SHORT_HEADER;
730 src->probation = DEFAULT_PROBATION;
731 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
732 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
733 src->ntp_sync = DEFAULT_NTP_SYNC;
734 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
736 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
737 src->tls_database = DEFAULT_TLS_DATABASE;
739 /* get a list of all extensions */
740 src->extensions = gst_rtsp_ext_list_get ();
742 /* connect to send signal */
743 gst_rtsp_ext_list_connect (src->extensions, "send",
744 (GCallback) gst_rtspsrc_send_cb, src);
746 /* protects the streaming thread in interleaved mode or the polling
747 * thread in UDP mode. */
748 g_rec_mutex_init (&src->stream_rec_lock);
750 /* protects our state changes from multiple invocations */
751 g_rec_mutex_init (&src->state_rec_lock);
753 src->state = GST_RTSP_STATE_INVALID;
755 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
759 gst_rtspsrc_finalize (GObject * object)
763 rtspsrc = GST_RTSPSRC (object);
765 gst_rtsp_ext_list_free (rtspsrc->extensions);
766 g_free (rtspsrc->conninfo.location);
767 gst_rtsp_url_free (rtspsrc->conninfo.url);
768 g_free (rtspsrc->conninfo.url_str);
769 g_free (rtspsrc->user_id);
770 g_free (rtspsrc->user_pw);
771 g_free (rtspsrc->multi_iface);
774 gst_sdp_message_free (rtspsrc->sdp);
777 if (rtspsrc->provided_clock)
778 gst_object_unref (rtspsrc->provided_clock);
781 gst_structure_free (rtspsrc->sdes);
783 if (rtspsrc->tls_database)
784 g_object_unref (rtspsrc->tls_database);
787 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
788 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
790 G_OBJECT_CLASS (parent_class)->finalize (object);
794 gst_rtspsrc_provide_clock (GstElement * element)
796 GstRTSPSrc *src = GST_RTSPSRC (element);
799 if ((clock = src->provided_clock) != NULL)
800 gst_object_ref (clock);
805 /* a proxy string of the format [user:passwd@]host[:port] */
807 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
811 g_free (rtsp->proxy_user);
812 rtsp->proxy_user = NULL;
813 g_free (rtsp->proxy_passwd);
814 rtsp->proxy_passwd = NULL;
815 g_free (rtsp->proxy_host);
816 rtsp->proxy_host = NULL;
817 rtsp->proxy_port = 0;
824 /* we allow http:// in front but ignore it */
825 if (g_str_has_prefix (p, "http://"))
828 at = strchr (p, '@');
830 /* look for user:passwd */
831 col = strchr (proxy, ':');
832 if (col == NULL || col > at)
835 rtsp->proxy_user = g_strndup (p, col - p);
837 rtsp->proxy_passwd = g_strndup (col, at - col);
842 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
843 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
844 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
845 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
846 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
847 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
848 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
851 col = strchr (p, ':');
854 /* everything before the colon is the hostname */
855 rtsp->proxy_host = g_strndup (p, col - p);
857 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
859 rtsp->proxy_host = g_strdup (p);
860 rtsp->proxy_port = 8080;
866 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
868 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
869 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
872 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
874 rtspsrc->ptcp_timeout = NULL;
878 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
883 rtspsrc = GST_RTSPSRC (object);
887 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
888 g_value_get_string (value), NULL);
891 rtspsrc->protocols = g_value_get_flags (value);
894 rtspsrc->debug = g_value_get_boolean (value);
897 rtspsrc->retry = g_value_get_uint (value);
900 rtspsrc->udp_timeout = g_value_get_uint64 (value);
902 case PROP_TCP_TIMEOUT:
903 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
906 rtspsrc->latency = g_value_get_uint (value);
908 case PROP_DROP_ON_LATENCY:
909 rtspsrc->drop_on_latency = g_value_get_boolean (value);
911 case PROP_CONNECTION_SPEED:
912 rtspsrc->connection_speed = g_value_get_uint64 (value);
914 case PROP_NAT_METHOD:
915 rtspsrc->nat_method = g_value_get_enum (value);
918 rtspsrc->do_rtcp = g_value_get_boolean (value);
920 case PROP_DO_RTSP_KEEP_ALIVE:
921 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
924 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
927 if (rtspsrc->prop_proxy_id)
928 g_free (rtspsrc->prop_proxy_id);
929 rtspsrc->prop_proxy_id = g_value_dup_string (value);
932 if (rtspsrc->prop_proxy_pw)
933 g_free (rtspsrc->prop_proxy_pw);
934 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
936 case PROP_RTP_BLOCKSIZE:
937 rtspsrc->rtp_blocksize = g_value_get_uint (value);
940 if (rtspsrc->user_id)
941 g_free (rtspsrc->user_id);
942 rtspsrc->user_id = g_value_dup_string (value);
945 if (rtspsrc->user_pw)
946 g_free (rtspsrc->user_pw);
947 rtspsrc->user_pw = g_value_dup_string (value);
949 case PROP_BUFFER_MODE:
950 rtspsrc->buffer_mode = g_value_get_enum (value);
952 case PROP_PORT_RANGE:
956 str = g_value_get_string (value);
958 sscanf (str, "%u-%u",
959 &rtspsrc->client_port_range.min, &rtspsrc->client_port_range.max);
961 rtspsrc->client_port_range.min = 0;
962 rtspsrc->client_port_range.max = 0;
966 case PROP_UDP_BUFFER_SIZE:
967 rtspsrc->udp_buffer_size = g_value_get_int (value);
969 case PROP_SHORT_HEADER:
970 rtspsrc->short_header = g_value_get_boolean (value);
973 rtspsrc->probation = g_value_get_uint (value);
975 case PROP_UDP_RECONNECT:
976 rtspsrc->udp_reconnect = g_value_get_boolean (value);
978 case PROP_MULTICAST_IFACE:
979 g_free (rtspsrc->multi_iface);
981 if (g_value_get_string (value) == NULL)
982 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
984 rtspsrc->multi_iface = g_value_dup_string (value);
987 rtspsrc->ntp_sync = g_value_get_boolean (value);
989 case PROP_USE_PIPELINE_CLOCK:
990 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
993 rtspsrc->sdes = g_value_dup_boxed (value);
995 case PROP_TLS_VALIDATION_FLAGS:
996 rtspsrc->tls_validation_flags = g_value_get_flags (value);
998 case PROP_TLS_DATABASE:
999 g_clear_object (&rtspsrc->tls_database);
1000 rtspsrc->tls_database = g_value_dup_object (value);
1003 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1009 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
1012 GstRTSPSrc *rtspsrc;
1014 rtspsrc = GST_RTSPSRC (object);
1018 g_value_set_string (value, rtspsrc->conninfo.location);
1020 case PROP_PROTOCOLS:
1021 g_value_set_flags (value, rtspsrc->protocols);
1024 g_value_set_boolean (value, rtspsrc->debug);
1027 g_value_set_uint (value, rtspsrc->retry);
1030 g_value_set_uint64 (value, rtspsrc->udp_timeout);
1032 case PROP_TCP_TIMEOUT:
1036 timeout = rtspsrc->tcp_timeout.tv_sec * G_USEC_PER_SEC +
1037 rtspsrc->tcp_timeout.tv_usec;
1038 g_value_set_uint64 (value, timeout);
1042 g_value_set_uint (value, rtspsrc->latency);
1044 case PROP_DROP_ON_LATENCY:
1045 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1047 case PROP_CONNECTION_SPEED:
1048 g_value_set_uint64 (value, rtspsrc->connection_speed);
1050 case PROP_NAT_METHOD:
1051 g_value_set_enum (value, rtspsrc->nat_method);
1054 g_value_set_boolean (value, rtspsrc->do_rtcp);
1056 case PROP_DO_RTSP_KEEP_ALIVE:
1057 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1063 if (rtspsrc->proxy_host) {
1065 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1069 g_value_take_string (value, str);
1073 g_value_set_string (value, rtspsrc->prop_proxy_id);
1076 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1078 case PROP_RTP_BLOCKSIZE:
1079 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1082 g_value_set_string (value, rtspsrc->user_id);
1085 g_value_set_string (value, rtspsrc->user_pw);
1087 case PROP_BUFFER_MODE:
1088 g_value_set_enum (value, rtspsrc->buffer_mode);
1090 case PROP_PORT_RANGE:
1094 if (rtspsrc->client_port_range.min != 0) {
1095 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1096 rtspsrc->client_port_range.max);
1100 g_value_take_string (value, str);
1103 case PROP_UDP_BUFFER_SIZE:
1104 g_value_set_int (value, rtspsrc->udp_buffer_size);
1106 case PROP_SHORT_HEADER:
1107 g_value_set_boolean (value, rtspsrc->short_header);
1109 case PROP_PROBATION:
1110 g_value_set_uint (value, rtspsrc->probation);
1112 case PROP_UDP_RECONNECT:
1113 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1115 case PROP_MULTICAST_IFACE:
1116 g_value_set_string (value, rtspsrc->multi_iface);
1119 g_value_set_boolean (value, rtspsrc->ntp_sync);
1121 case PROP_USE_PIPELINE_CLOCK:
1122 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1125 g_value_set_boxed (value, rtspsrc->sdes);
1127 case PROP_TLS_VALIDATION_FLAGS:
1128 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1130 case PROP_TLS_DATABASE:
1131 g_value_set_object (value, rtspsrc->tls_database);
1134 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1140 find_stream_by_id (GstRTSPStream * stream, gint * id)
1142 if (stream->id == *id)
1149 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1151 if (stream->channel[0] == *channel || stream->channel[1] == *channel)
1158 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1160 GstElement *src = (GstElement *) a;
1162 if (stream->udpsrc[0] == src)
1164 if (stream->udpsrc[1] == src)
1171 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1173 if (stream->conninfo.location) {
1174 /* check qualified setup_url */
1175 if (!strcmp (stream->conninfo.location, (gchar *) a))
1178 if (stream->control_url) {
1179 /* check original control_url */
1180 if (!strcmp (stream->control_url, (gchar *) a))
1183 /* check if qualified setup_url ends with string */
1184 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1191 static GstRTSPStream *
1192 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1196 /* find and get stream */
1197 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1198 return (GstRTSPStream *) lstream->data;
1203 static const GstSDPBandwidth *
1204 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1205 const GstSDPMedia * media, const gchar * type)
1209 /* first look in the media specific section */
1210 len = gst_sdp_media_bandwidths_len (media);
1211 for (i = 0; i < len; i++) {
1212 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1214 if (strcmp (bw->bwtype, type) == 0)
1217 /* then look in the message specific section */
1218 len = gst_sdp_message_bandwidths_len (sdp);
1219 for (i = 0; i < len; i++) {
1220 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1222 if (strcmp (bw->bwtype, type) == 0)
1229 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1230 const GstSDPMedia * media, GstRTSPStream * stream)
1232 const GstSDPBandwidth *bw;
1234 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1235 stream->as_bandwidth = bw->bandwidth;
1237 stream->as_bandwidth = -1;
1239 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1240 stream->rr_bandwidth = bw->bandwidth;
1242 stream->rr_bandwidth = -1;
1244 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1245 stream->rs_bandwidth = bw->bandwidth;
1247 stream->rs_bandwidth = -1;
1251 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1252 const GstSDPConnection * conn)
1254 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1257 if (conn->addrtype == NULL)
1260 /* check for IPV6 */
1261 if (strcmp (conn->addrtype, "IP4") == 0)
1262 stream->is_ipv6 = FALSE;
1263 else if (strcmp (conn->addrtype, "IP6") == 0)
1264 stream->is_ipv6 = TRUE;
1269 g_free (stream->destination);
1270 stream->destination = g_strdup (conn->address);
1272 /* check for multicast */
1273 stream->is_multicast =
1274 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1276 stream->ttl = conn->ttl;
1279 /* Go over the connections for a stream.
1280 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1282 * - If we are dealing with a localhost address, we disable multicast
1285 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1286 const GstSDPMedia * media, GstRTSPStream * stream)
1288 const GstSDPConnection *conn;
1291 /* first look in the media specific section */
1292 len = gst_sdp_media_connections_len (media);
1293 for (i = 0; i < len; i++) {
1294 conn = gst_sdp_media_get_connection (media, i);
1296 gst_rtspsrc_do_stream_connection (src, stream, conn);
1298 /* then look in the message specific section */
1299 if ((conn = gst_sdp_message_get_connection (sdp))) {
1300 gst_rtspsrc_do_stream_connection (src, stream, conn);
1305 /* m=<media> <UDP port> RTP/AVP <payload>
1308 gst_rtspsrc_collect_payloads (GstRTSPSrc * src, const GstSDPMessage * sdp,
1309 const GstSDPMedia * media, GstRTSPStream * stream)
1315 proto = gst_sdp_media_get_proto (media);
1319 if (g_str_equal (proto, "RTP/AVP"))
1320 stream->profile = GST_RTSP_PROFILE_AVP;
1321 else if (g_str_equal (proto, "RTP/SAVP"))
1322 stream->profile = GST_RTSP_PROFILE_SAVP;
1323 else if (g_str_equal (proto, "RTP/AVPF"))
1324 stream->profile = GST_RTSP_PROFILE_AVPF;
1325 else if (g_str_equal (proto, "RTP/SAVPF"))
1326 stream->profile = GST_RTSP_PROFILE_SAVPF;
1330 len = gst_sdp_media_formats_len (media);
1331 for (i = 0; i < len; i++) {
1338 pt = atoi (gst_sdp_media_get_format (media, i));
1340 GST_DEBUG_OBJECT (src, " looking at %d pt: %d", i, pt);
1343 caps = gst_rtspsrc_media_to_caps (pt, media);
1345 GST_WARNING_OBJECT (src, " skipping pt %d without caps", pt);
1349 /* do some tweaks */
1350 s = gst_caps_get_structure (caps, 0);
1351 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
1352 stream->is_real = (strstr (enc, "-REAL") != NULL);
1353 if (strcmp (enc, "X-ASF-PF") == 0)
1354 stream->container = TRUE;
1356 GST_DEBUG ("mapping sdp session level attributes to caps");
1357 gst_rtspsrc_sdp_attributes_to_caps (sdp->attributes, caps);
1358 GST_DEBUG ("mapping sdp media level attributes to caps");
1359 gst_rtspsrc_sdp_attributes_to_caps (media->attributes, caps);
1361 /* the first pt will be the default */
1362 if (stream->ptmap->len == 0)
1363 stream->default_pt = pt;
1367 g_array_append_val (stream->ptmap, item);
1373 GST_ERROR_OBJECT (src, "can't find proto in media");
1378 GST_ERROR_OBJECT (src, "unknown proto in media %s", proto);
1383 static const gchar *
1384 get_aggregate_control (GstRTSPSrc * src)
1389 base = src->control;
1390 else if (src->content_base)
1391 base = src->content_base;
1392 else if (src->conninfo.url_str)
1393 base = src->conninfo.url_str;
1400 static GstRTSPStream *
1401 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
1403 GstRTSPStream *stream;
1404 const gchar *control_url;
1405 const GstSDPMedia *media;
1407 /* get media, should not return NULL */
1408 media = gst_sdp_message_get_media (sdp, idx);
1412 stream = g_new0 (GstRTSPStream, 1);
1413 stream->parent = src;
1414 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1416 stream->last_ret = GST_FLOW_NOT_LINKED;
1417 stream->added = FALSE;
1418 stream->disabled = FALSE;
1420 stream->eos = FALSE;
1421 stream->discont = TRUE;
1422 stream->seqbase = -1;
1423 stream->timebase = -1;
1424 stream->profile = GST_RTSP_PROFILE_AVP;
1425 stream->ptmap = g_array_new (FALSE, FALSE, sizeof (PtMapItem));
1427 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1428 * session manager to scale RTCP. */
1429 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1431 /* collect connection info */
1432 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1434 /* make the payload type map */
1435 gst_rtspsrc_collect_payloads (src, sdp, media, stream);
1437 /* collect port number */
1438 stream->port = gst_sdp_media_get_port (media);
1440 /* get control url to construct the setup url. The setup url is used to
1441 * configure the transport of the stream and is used to identity the stream in
1442 * the RTP-Info header field returned from PLAY. */
1443 control_url = gst_sdp_media_get_attribute_val (media, "control");
1444 if (control_url == NULL)
1445 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1447 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1448 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1449 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1450 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1452 if (control_url != NULL) {
1453 stream->control_url = g_strdup (control_url);
1454 /* Build a fully qualified url using the content_base if any or by prefixing
1455 * the original request.
1456 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1457 * likely build a URL that the server will fail to understand, this is ok,
1458 * we will fail then. */
1459 if (g_str_has_prefix (control_url, "rtsp://"))
1460 stream->conninfo.location = g_strdup (control_url);
1465 if (g_strcmp0 (control_url, "*") == 0)
1468 base = get_aggregate_control (src);
1470 /* check if the base ends or control starts with / */
1471 has_slash = g_str_has_prefix (control_url, "/");
1472 has_slash = has_slash || g_str_has_suffix (base, "/");
1474 /* concatenate the two strings, insert / when not present */
1475 stream->conninfo.location =
1476 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1479 GST_DEBUG_OBJECT (src, " setup: %s",
1480 GST_STR_NULL (stream->conninfo.location));
1482 /* we keep track of all streams */
1483 src->streams = g_list_append (src->streams, stream);
1491 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1495 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1497 g_array_free (stream->ptmap, TRUE);
1499 g_free (stream->destination);
1500 g_free (stream->control_url);
1501 g_free (stream->conninfo.location);
1503 for (i = 0; i < 2; i++) {
1504 if (stream->udpsrc[i]) {
1505 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1506 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1507 gst_object_unref (stream->udpsrc[i]);
1508 stream->udpsrc[i] = NULL;
1510 if (stream->channelpad[i]) {
1511 gst_object_unref (stream->channelpad[i]);
1512 stream->channelpad[i] = NULL;
1514 if (stream->udpsink[i]) {
1515 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1516 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1517 gst_object_unref (stream->udpsink[i]);
1518 stream->udpsink[i] = NULL;
1521 if (stream->fakesrc) {
1522 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1523 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1524 gst_object_unref (stream->fakesrc);
1525 stream->fakesrc = NULL;
1527 if (stream->srcpad) {
1528 gst_pad_set_active (stream->srcpad, FALSE);
1529 if (stream->added) {
1530 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1531 stream->added = FALSE;
1533 stream->srcpad = NULL;
1535 if (stream->rtcppad) {
1536 gst_object_unref (stream->rtcppad);
1537 stream->rtcppad = NULL;
1539 if (stream->session) {
1540 g_object_unref (stream->session);
1541 stream->session = NULL;
1547 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1551 GST_DEBUG_OBJECT (src, "cleanup");
1553 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1554 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1556 gst_rtspsrc_stream_free (src, stream);
1558 g_list_free (src->streams);
1559 src->streams = NULL;
1561 if (src->manager_sig_id) {
1562 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1563 src->manager_sig_id = 0;
1565 gst_element_set_state (src->manager, GST_STATE_NULL);
1566 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1567 src->manager = NULL;
1570 gst_structure_free (src->props);
1573 g_free (src->content_base);
1574 src->content_base = NULL;
1576 g_free (src->control);
1577 src->control = NULL;
1580 gst_rtsp_range_free (src->range);
1583 /* don't clear the SDP when it was used in the url */
1584 if (src->sdp && !src->from_sdp) {
1585 gst_sdp_message_free (src->sdp);
1588 if (src->start_segment) {
1589 gst_event_unref (src->start_segment);
1590 src->start_segment = NULL;
1592 if (src->provided_clock) {
1593 gst_object_unref (src->provided_clock);
1594 src->provided_clock = NULL;
1598 #define PARSE_INT(p, del, res) \
1601 p = strstr (p, del); \
1611 #define PARSE_STRING(p, del, res) \
1614 p = strstr (p, del); \
1626 #define SKIP_SPACES(p) \
1627 while (*p && g_ascii_isspace (*p)) \
1632 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1635 gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
1636 gint * rate, gchar ** params)
1640 p = (gchar *) rtpmap;
1642 PARSE_INT (p, " ", *payload);
1650 PARSE_STRING (p, "/", *name);
1651 if (*name == NULL) {
1652 GST_DEBUG ("no rate, name %s", p);
1653 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
1654 * streams seem to omit the rate. */
1661 p = strstr (p, "/");
1679 * Mapping SDP attributes to caps
1681 * prepend 'a-' to IANA registered sdp attributes names
1682 * (ie: not prefixed with 'x-') in order to avoid
1683 * collision with gstreamer standard caps properties names
1686 gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
1688 if (attributes->len > 0) {
1692 s = gst_caps_get_structure (caps, 0);
1694 for (i = 0; i < attributes->len; i++) {
1695 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
1696 gchar *tofree, *key;
1700 /* skip some of the attribute we already handle */
1701 if (!strcmp (key, "fmtp"))
1703 if (!strcmp (key, "rtpmap"))
1705 if (!strcmp (key, "control"))
1707 if (!strcmp (key, "range"))
1710 /* string must be valid UTF8 */
1711 if (!g_utf8_validate (attr->value, -1, NULL))
1714 if (!g_str_has_prefix (key, "x-"))
1715 tofree = key = g_strdup_printf ("a-%s", key);
1719 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
1720 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
1726 static const gchar *
1727 rtsp_get_attribute_for_pt (const GstSDPMedia * media, const gchar * name,
1736 if ((attr = gst_sdp_media_get_attribute_val_n (media, name, i)) == NULL)
1739 if (sscanf (attr, "%d ", &val) != 1)
1749 * Mapping of caps to and from SDP fields:
1751 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1752 * a=fmtp:<payload> <param>[=<value>];...
1755 gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
1758 const gchar *rtpmap;
1762 gchar *params = NULL;
1768 /* get and parse rtpmap */
1769 rtpmap = rtsp_get_attribute_for_pt (media, "rtpmap", pt);
1771 /* dynamic payloads need rtpmap or we fail */
1772 if (rtpmap == NULL && pt >= 96)
1775 ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
1777 g_warning ("error parsing rtpmap, ignoring");
1780 /* check if we have a rate, if not, we need to look up the rate from the
1781 * default rates based on the payload types. */
1783 const GstRTPPayloadInfo *info;
1785 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
1786 /* dynamic types, use media and encoding_name */
1787 tmp = g_ascii_strdown (media->media, -1);
1788 info = gst_rtp_payload_info_for_name (tmp, name);
1791 /* static types, use payload type */
1792 info = gst_rtp_payload_info_for_pt (pt);
1796 if ((rate = info->clock_rate) == 0)
1799 /* we fail if we cannot find one */
1804 tmp = g_ascii_strdown (media->media, -1);
1805 caps = gst_caps_new_simple ("application/x-unknown",
1806 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
1808 s = gst_caps_get_structure (caps, 0);
1810 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
1812 /* encoding name must be upper case */
1814 tmp = g_ascii_strup (name, -1);
1815 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
1819 /* params must be lower case */
1820 if (params != NULL) {
1821 tmp = g_ascii_strdown (params, -1);
1822 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
1826 /* parse optional fmtp: field */
1827 if ((fmtp = rtsp_get_attribute_for_pt (media, "fmtp", pt))) {
1833 /* p is now of the format <payload> <param>[=<value>];... */
1834 PARSE_INT (p, " ", payload);
1835 if (payload != -1 && payload == pt) {
1839 /* <param>[=<value>] are separated with ';' */
1840 pairs = g_strsplit (p, ";", 0);
1841 for (i = 0; pairs[i]; i++) {
1843 const gchar *val, *key;
1845 /* the key may not have a '=', the value can have other '='s */
1846 valpos = strstr (pairs[i], "=");
1848 /* we have a '=' and thus a value, remove the '=' with \0 */
1850 /* value is everything between '=' and ';'. We split the pairs at ;
1851 * boundaries so we can take the remainder of the value. Some servers
1852 * put spaces around the value which we strip off here. Alternatively
1853 * we could strip those spaces in the depayloaders should these spaces
1854 * actually carry any meaning in the future. */
1855 val = g_strstrip (valpos + 1);
1857 /* simple <param>;.. is translated into <param>=1;... */
1860 /* strip the key of spaces, convert key to lowercase but not the value. */
1861 key = g_strstrip (pairs[i]);
1862 if (strlen (key) > 1) {
1863 tmp = g_ascii_strdown (key, -1);
1864 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
1876 g_warning ("rtpmap type not given for dynamic payload %d", pt);
1881 g_warning ("rate unknown for payload type %d", pt);
1887 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
1888 gint * rtpport, gint * rtcpport)
1891 GstStateChangeReturn ret;
1892 GstElement *udpsrc0, *udpsrc1;
1893 gint tmp_rtp, tmp_rtcp;
1897 src = stream->parent;
1903 /* Start at next port */
1904 tmp_rtp = src->next_port_num;
1906 if (stream->is_ipv6)
1907 host = "udp://[::0]";
1909 host = "udp://0.0.0.0";
1911 /* try to allocate 2 UDP ports, the RTP port should be an even
1912 * number and the RTCP port should be the next (uneven) port */
1915 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
1916 tmp_rtp >= src->client_port_range.max)
1919 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1920 if (udpsrc0 == NULL)
1921 goto no_udp_protocol;
1922 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
1924 if (src->udp_buffer_size != 0)
1925 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
1928 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
1929 if (ret == GST_STATE_CHANGE_FAILURE) {
1931 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
1934 if (++count > src->retry)
1937 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1938 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1939 gst_object_unref (udpsrc0);
1942 GST_DEBUG_OBJECT (src, "retry %d", count);
1945 goto no_udp_protocol;
1948 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
1949 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
1951 /* check if port is even */
1952 if ((tmp_rtp & 0x01) != 0) {
1953 /* port not even, close and allocate another */
1954 if (++count > src->retry)
1957 GST_DEBUG_OBJECT (src, "RTP port not even");
1959 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1960 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1961 gst_object_unref (udpsrc0);
1964 GST_DEBUG_OBJECT (src, "retry %d", count);
1969 /* allocate port+1 for RTCP now */
1970 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1971 if (udpsrc1 == NULL)
1972 goto no_udp_rtcp_protocol;
1975 tmp_rtcp = tmp_rtp + 1;
1976 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
1979 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
1981 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
1982 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
1983 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
1984 if (ret == GST_STATE_CHANGE_FAILURE) {
1985 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
1987 if (++count > src->retry)
1990 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1991 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1992 gst_object_unref (udpsrc0);
1995 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
1996 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1997 gst_object_unref (udpsrc1);
2001 GST_DEBUG_OBJECT (src, "retry %d", count);
2005 /* all fine, do port check */
2006 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
2007 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
2009 /* this should not happen... */
2010 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
2013 /* we keep these elements, we configure all in configure_transport when the
2014 * server told us to really use the UDP ports. */
2015 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
2016 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
2017 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
2018 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
2020 /* keep track of next available port number when we have a range
2022 if (src->next_port_num != 0)
2023 src->next_port_num = tmp_rtcp + 1;
2030 GST_DEBUG_OBJECT (src, "could not get UDP source");
2035 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
2039 no_udp_rtcp_protocol:
2041 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
2046 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
2047 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
2053 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2054 gst_object_unref (udpsrc0);
2057 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2058 gst_object_unref (udpsrc1);
2065 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
2070 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
2072 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2073 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2076 for (i = 0; i < 2; i++) {
2077 if (stream->udpsrc[i])
2078 gst_element_set_state (stream->udpsrc[i], state);
2084 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
2091 event = gst_event_new_flush_start ();
2092 GST_DEBUG_OBJECT (src, "start flush");
2094 state = GST_STATE_PAUSED;
2096 event = gst_event_new_flush_stop (FALSE);
2097 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2100 state = GST_STATE_PLAYING;
2102 state = GST_STATE_PAUSED;
2104 gst_rtspsrc_push_event (src, event);
2105 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2106 gst_rtspsrc_set_state (src, state);
2109 static GstRTSPResult
2110 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
2111 GstRTSPMessage * message, GTimeVal * timeout)
2116 ret = gst_rtsp_connection_send (conn, message, timeout);
2118 ret = GST_RTSP_ERROR;
2123 static GstRTSPResult
2124 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
2125 GstRTSPMessage * message, GTimeVal * timeout)
2130 ret = gst_rtsp_connection_receive (conn, message, timeout);
2132 ret = GST_RTSP_ERROR;
2138 gst_rtspsrc_get_position (GstRTSPSrc * src)
2143 query = gst_query_new_position (GST_FORMAT_TIME);
2144 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2145 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2146 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2150 if (stream->srcpad) {
2151 if (gst_pad_query (stream->srcpad, query)) {
2152 gst_query_parse_position (query, &fmt, &pos);
2153 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2154 GST_TIME_ARGS (pos));
2155 src->last_pos = pos;
2165 gst_rtspsrc_do_seek (GstRTSPSrc * src, GstSegment * segment)
2167 src->state = GST_RTSP_STATE_SEEKING;
2168 /* PLAY will add the range header now. */
2169 src->need_range = TRUE;
2175 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2180 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2182 gboolean flush, skip;
2185 GstSegment seeksegment = { 0, };
2189 GST_DEBUG_OBJECT (src, "doing seek with event");
2191 gst_event_parse_seek (event, &rate, &format, &flags,
2192 &cur_type, &cur, &stop_type, &stop);
2194 /* no negative rates yet */
2198 /* we need TIME format */
2199 if (format != src->segment.format)
2202 GST_DEBUG_OBJECT (src, "doing seek without event");
2204 cur_type = GST_SEEK_TYPE_SET;
2205 stop_type = GST_SEEK_TYPE_SET;
2208 /* get flush flag */
2209 flush = flags & GST_SEEK_FLAG_FLUSH;
2210 skip = flags & GST_SEEK_FLAG_SKIP;
2212 /* now we need to make sure the streaming thread is stopped. We do this by
2213 * either sending a FLUSH_START event downstream which will cause the
2214 * streaming thread to stop with a WRONG_STATE.
2215 * For a non-flushing seek we simply pause the task, which will happen as soon
2216 * as it completes one iteration (and thus might block when the sink is
2217 * blocking in preroll). */
2219 GST_DEBUG_OBJECT (src, "starting flush");
2220 gst_rtspsrc_flush (src, TRUE, FALSE);
2223 gst_task_pause (src->task);
2227 /* we should now be able to grab the streaming thread because we stopped it
2228 * with the above flush/pause code */
2229 GST_RTSP_STREAM_LOCK (src);
2231 GST_DEBUG_OBJECT (src, "stopped streaming");
2233 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2234 gst_rtspsrc_connection_flush (src, FALSE);
2236 /* copy segment, we need this because we still need the old
2237 * segment when we close the current segment. */
2238 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2240 /* configure the seek parameters in the seeksegment. We will then have the
2241 * right values in the segment to perform the seek */
2243 GST_DEBUG_OBJECT (src, "configuring seek");
2244 gst_segment_do_seek (&seeksegment, rate, format, flags,
2245 cur_type, cur, stop_type, stop, &update);
2248 /* figure out the last position we need to play. If it's configured (stop !=
2249 * -1), use that, else we play until the total duration of the file */
2250 if ((stop = seeksegment.stop) == -1)
2251 stop = seeksegment.duration;
2253 playing = (src->state == GST_RTSP_STATE_PLAYING);
2255 /* if we were playing, pause first */
2257 /* obtain current position in case seek fails */
2258 gst_rtspsrc_get_position (src);
2259 gst_rtspsrc_pause (src, FALSE);
2263 gst_rtspsrc_do_seek (src, &seeksegment);
2265 /* and continue playing */
2267 gst_rtspsrc_play (src, &seeksegment, FALSE);
2269 /* prepare for streaming again */
2271 /* if we started flush, we stop now */
2272 GST_DEBUG_OBJECT (src, "stopping flush");
2273 gst_rtspsrc_flush (src, FALSE, playing);
2276 /* now we did the seek and can activate the new segment values */
2277 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2279 /* if we're doing a segment seek, post a SEGMENT_START message */
2280 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2281 gst_element_post_message (GST_ELEMENT_CAST (src),
2282 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2283 src->segment.format, src->segment.position));
2286 /* now create the newsegment */
2287 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2288 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2291 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2292 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2293 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2294 stream->discont = TRUE;
2297 GST_RTSP_STREAM_UNLOCK (src);
2304 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2309 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2315 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2319 gboolean res = TRUE;
2322 src = GST_RTSPSRC_CAST (parent);
2324 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2325 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2327 switch (GST_EVENT_TYPE (event)) {
2328 case GST_EVENT_SEEK:
2329 res = gst_rtspsrc_perform_seek (src, event);
2333 case GST_EVENT_NAVIGATION:
2334 case GST_EVENT_LATENCY:
2342 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2343 res = gst_pad_send_event (target, event);
2344 gst_object_unref (target);
2346 gst_event_unref (event);
2349 gst_event_unref (event);
2355 /* this is the final event function we receive on the internal source pad when
2356 * we deal with TCP connections */
2358 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2363 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2365 switch (GST_EVENT_TYPE (event)) {
2366 case GST_EVENT_SEEK:
2368 case GST_EVENT_NAVIGATION:
2369 case GST_EVENT_LATENCY:
2371 gst_event_unref (event);
2378 /* this is the final query function we receive on the internal source pad when
2379 * we deal with TCP connections */
2381 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2385 gboolean res = TRUE;
2387 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2389 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2390 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2392 switch (GST_QUERY_TYPE (query)) {
2393 case GST_QUERY_POSITION:
2398 case GST_QUERY_DURATION:
2402 gst_query_parse_duration (query, &format, NULL);
2405 case GST_FORMAT_TIME:
2406 gst_query_set_duration (query, format, src->segment.duration);
2414 case GST_QUERY_LATENCY:
2416 /* we are live with a min latency of 0 and unlimited max latency, this
2417 * result will be updated by the session manager if there is any. */
2418 gst_query_set_latency (query, TRUE, 0, -1);
2428 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2430 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2434 gboolean res = FALSE;
2436 src = GST_RTSPSRC_CAST (parent);
2438 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2439 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2441 switch (GST_QUERY_TYPE (query)) {
2442 case GST_QUERY_DURATION:
2446 gst_query_parse_duration (query, &format, NULL);
2449 case GST_FORMAT_TIME:
2450 gst_query_set_duration (query, format, src->segment.duration);
2458 case GST_QUERY_SEEKING:
2462 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2463 if (format == GST_FORMAT_TIME) {
2465 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2467 /* seeking without duration is unlikely */
2468 seekable = seekable && src->seekable && src->segment.duration &&
2469 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2471 /* FIXME ?? should we have 0 and segment.duration here; see demuxers */
2472 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
2473 src->segment.start, src->segment.stop);
2482 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2484 gst_query_set_uri (query, uri);
2492 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2494 /* forward the query to the proxy target pad */
2496 res = gst_pad_query (target, query);
2497 gst_object_unref (target);
2506 /* callback for RTCP messages to be sent to the server when operating in TCP
2508 static GstFlowReturn
2509 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2512 GstRTSPStream *stream;
2513 GstFlowReturn res = GST_FLOW_OK;
2518 GstRTSPMessage message = { 0 };
2519 GstRTSPConnection *conn;
2521 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2522 src = stream->parent;
2524 gst_buffer_map (buffer, &map, GST_MAP_READ);
2528 gst_rtsp_message_init_data (&message, stream->channel[1]);
2530 /* lend the body data to the message */
2531 gst_rtsp_message_take_body (&message, data, size);
2533 if (stream->conninfo.connection)
2534 conn = stream->conninfo.connection;
2536 conn = src->conninfo.connection;
2538 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2539 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2540 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2542 /* and steal it away again because we will free it when unreffing the
2544 gst_rtsp_message_steal_body (&message, &data, &size);
2545 gst_rtsp_message_unset (&message);
2547 gst_buffer_unmap (buffer, &map);
2548 gst_buffer_unref (buffer);
2553 static GstPadProbeReturn
2554 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2556 GstRTSPSrc *src = user_data;
2558 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2559 GST_DEBUG_PAD_NAME (pad));
2561 /* activate the streams */
2562 GST_OBJECT_LOCK (src);
2563 if (!src->need_activate)
2566 src->need_activate = FALSE;
2567 GST_OBJECT_UNLOCK (src);
2569 gst_rtspsrc_activate_streams (src);
2571 return GST_PAD_PROBE_OK;
2575 GST_OBJECT_UNLOCK (src);
2576 return GST_PAD_PROBE_OK;
2580 /* this callback is called when the session manager generated a new src pad with
2581 * payloaded RTP packets. We simply ghost the pad here. */
2583 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2586 GstPadTemplate *template;
2589 GstRTSPStream *stream;
2592 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2594 GST_RTSP_STATE_LOCK (src);
2596 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2597 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2598 goto unknown_stream;
2600 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2602 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2604 goto unknown_stream;
2607 stream->ssrc = ssrc;
2609 /* we'll add it later see below */
2610 stream->added = TRUE;
2612 /* check if we added all streams */
2614 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
2615 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
2617 GST_DEBUG_OBJECT (src, "stream %p, container %d, disabled %d, added %d",
2618 ostream, ostream->container, ostream->disabled, ostream->added);
2620 /* a container stream only needs one pad added. Also disabled streams don't
2622 if (!ostream->container && !ostream->disabled && !ostream->added) {
2627 GST_RTSP_STATE_UNLOCK (src);
2629 /* create a new pad we will use to stream to */
2630 template = gst_static_pad_template_get (&rtptemplate);
2631 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2632 gst_object_unref (template);
2635 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2636 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2637 gst_pad_set_active (stream->srcpad, TRUE);
2638 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2641 GST_DEBUG_OBJECT (src, "We added all streams");
2642 /* when we get here, all stream are added and we can fire the no-more-pads
2644 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2652 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2653 GST_RTSP_STATE_UNLOCK (src);
2660 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
2664 len = stream->ptmap->len;
2665 for (i = 0; i < len; i++) {
2666 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
2674 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2676 GstRTSPStream *stream;
2679 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2681 GST_RTSP_STATE_LOCK (src);
2682 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2684 goto unknown_stream;
2686 if ((caps = stream_get_caps_for_pt (stream, pt)))
2687 gst_caps_ref (caps);
2688 GST_RTSP_STATE_UNLOCK (src);
2694 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2695 GST_RTSP_STATE_UNLOCK (src);
2701 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2703 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2709 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2715 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2721 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2723 GstRTSPSrc *src = stream->parent;
2726 g_object_get (source, "ssrc", &ssrc, NULL);
2728 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
2729 ssrc, stream->ssrc, stream->id);
2731 if (ssrc == stream->ssrc)
2732 gst_rtspsrc_do_stream_eos (src, stream);
2736 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2738 GstRTSPSrc *src = stream->parent;
2741 g_object_get (source, "ssrc", &ssrc, NULL);
2743 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
2744 ssrc, stream->ssrc, stream->id);
2746 if (ssrc == stream->ssrc)
2747 gst_rtspsrc_do_stream_eos (src, stream);
2751 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2753 GstRTSPStream *stream;
2755 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2757 /* get stream for session */
2758 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2760 gst_rtspsrc_do_stream_eos (src, stream);
2765 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2767 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2772 set_manager_buffer_mode (GstRTSPSrc * src)
2774 GObjectClass *klass;
2776 if (src->manager == NULL)
2779 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2781 if (!g_object_class_find_property (klass, "buffer-mode"))
2784 if (src->buffer_mode != BUFFER_MODE_AUTO) {
2785 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
2790 GST_DEBUG_OBJECT (src,
2791 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
2793 if (src->provided_clock) {
2794 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
2796 if (clock == src->provided_clock) {
2797 GST_DEBUG_OBJECT (src, "selected synced");
2798 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
2801 gst_object_unref (clock);
2806 /* Otherwise fall-through and use another buffer mode */
2808 gst_object_unref (clock);
2811 GST_DEBUG_OBJECT (src, "auto buffering mode");
2812 if (src->use_buffering) {
2813 GST_DEBUG_OBJECT (src, "selected buffer");
2814 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
2816 GST_DEBUG_OBJECT (src, "selected slave");
2817 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
2821 /* try to get and configure a manager */
2823 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
2824 GstRTSPTransport * transport)
2826 const gchar *manager;
2828 GstStateChangeReturn ret;
2830 /* find a manager */
2831 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
2835 GST_DEBUG_OBJECT (src, "using manager %s", manager);
2837 /* configure the manager */
2838 if (src->manager == NULL) {
2839 GObjectClass *klass;
2841 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
2843 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
2847 goto use_no_manager;
2849 if (!(src->manager = gst_element_factory_make (manager, "manager")))
2850 goto manager_failed;
2853 /* we manage this element */
2854 gst_element_set_locked_state (src->manager, TRUE);
2855 gst_bin_add (GST_BIN_CAST (src), src->manager);
2857 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
2858 if (ret == GST_STATE_CHANGE_FAILURE)
2859 goto start_manager_failure;
2861 g_object_set (src->manager, "latency", src->latency, NULL);
2863 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2865 if (g_object_class_find_property (klass, "ntp-sync")) {
2866 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
2869 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
2870 g_object_set (src->manager, "use-pipeline-clock",
2871 src->use_pipeline_clock, NULL);
2874 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
2875 g_object_set (src->manager, "sdes", src->sdes, NULL);
2878 if (g_object_class_find_property (klass, "drop-on-latency")) {
2879 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
2883 /* buffer mode pauses are handled by adding offsets to buffer times,
2884 * but some depayloaders may have a hard time syncing output times
2885 * with such input times, e.g. container ones, most notably ASF */
2886 /* TODO alternatives are having an event that indicates these shifts,
2887 * or having rtsp extensions provide suggestion on buffer mode */
2888 /* valid duration implies not likely live pipeline,
2889 * so slaving in jitterbuffer does not make much sense
2890 * (and might mess things up due to bursts) */
2891 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
2892 src->segment.duration && !stream->container) {
2893 src->use_buffering = TRUE;
2895 src->use_buffering = FALSE;
2898 set_manager_buffer_mode (src);
2900 /* connect to signals */
2901 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
2903 src->manager_sig_id =
2904 g_signal_connect (src->manager, "pad-added",
2905 (GCallback) new_manager_pad, src);
2906 src->manager_ptmap_id =
2907 g_signal_connect (src->manager, "request-pt-map",
2908 (GCallback) request_pt_map, src);
2910 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
2913 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
2917 /* we stream directly to the manager, get some pads. Each RTSP stream goes
2918 * into a separate RTP session. */
2919 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
2920 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
2922 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
2923 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
2926 /* now configure the bandwidth in the manager */
2927 if (g_signal_lookup ("get-internal-session",
2928 G_OBJECT_TYPE (src->manager)) != 0) {
2929 GObject *rtpsession;
2931 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
2934 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
2936 stream->session = rtpsession;
2938 if (stream->as_bandwidth != -1) {
2939 GST_INFO_OBJECT (src, "setting AS: %f",
2940 (gdouble) (stream->as_bandwidth * 1000));
2941 g_object_set (rtpsession, "bandwidth",
2942 (gdouble) (stream->as_bandwidth * 1000), NULL);
2944 if (stream->rr_bandwidth != -1) {
2945 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
2946 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
2949 if (stream->rs_bandwidth != -1) {
2950 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
2951 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
2955 g_object_set (rtpsession, "probation", src->probation, NULL);
2957 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2959 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
2961 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
2963 g_signal_connect (rtpsession, "on-ssrc-active",
2964 (GCallback) on_ssrc_active, stream);
2975 GST_DEBUG_OBJECT (src, "cannot get a session manager");
2980 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
2983 start_manager_failure:
2985 GST_DEBUG_OBJECT (src, "could not start session manager");
2990 /* free the UDP sources allocated when negotiating a transport.
2991 * This function is called when the server negotiated to a transport where the
2992 * UDP sources are not needed anymore, such as TCP or multicast. */
2994 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
2998 for (i = 0; i < 2; i++) {
2999 if (stream->udpsrc[i]) {
3000 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
3001 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
3002 gst_object_unref (stream->udpsrc[i]);
3003 stream->udpsrc[i] = NULL;
3008 /* for TCP, create pads to send and receive data to and from the manager and to
3009 * intercept various events and queries
3012 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
3013 GstRTSPTransport * transport, GstPad ** outpad)
3016 GstPadTemplate *template;
3017 GstPad *pad0, *pad1;
3019 /* configure for interleaved delivery, nothing needs to be done
3020 * here, the loop function will call the chain functions of the
3021 * session manager. */
3022 stream->channel[0] = transport->interleaved.min;
3023 stream->channel[1] = transport->interleaved.max;
3024 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
3025 stream->channel[0], stream->channel[1]);
3027 /* we can remove the allocated UDP ports now */
3028 gst_rtspsrc_stream_free_udp (stream);
3030 /* no session manager, send data to srcpad directly */
3031 if (!stream->channelpad[0]) {
3032 GST_DEBUG_OBJECT (src, "no manager, creating pad");
3034 /* create a new pad we will use to stream to */
3035 name = g_strdup_printf ("stream_%u", stream->id);
3036 template = gst_static_pad_template_get (&rtptemplate);
3037 stream->channelpad[0] = gst_pad_new_from_template (template, name);
3038 gst_object_unref (template);
3041 /* set caps and activate */
3042 gst_pad_use_fixed_caps (stream->channelpad[0]);
3043 gst_pad_set_active (stream->channelpad[0], TRUE);
3045 *outpad = gst_object_ref (stream->channelpad[0]);
3047 GST_DEBUG_OBJECT (src, "using manager source pad");
3049 template = gst_static_pad_template_get (&anysrctemplate);
3051 /* allocate pads for sending the channel data into the manager */
3052 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
3053 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
3054 gst_object_unref (stream->channelpad[0]);
3055 stream->channelpad[0] = pad0;
3056 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
3057 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
3058 gst_pad_set_element_private (pad0, src);
3059 gst_pad_set_active (pad0, TRUE);
3061 if (stream->channelpad[1]) {
3062 /* if we have a sinkpad for the other channel, create a pad and link to the
3064 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
3065 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
3066 gst_pad_link_full (pad1, stream->channelpad[1],
3067 GST_PAD_LINK_CHECK_NOTHING);
3068 gst_object_unref (stream->channelpad[1]);
3069 stream->channelpad[1] = pad1;
3070 gst_pad_set_active (pad1, TRUE);
3072 gst_object_unref (template);
3074 /* setup RTCP transport back to the server if we have to. */
3075 if (src->manager && src->do_rtcp) {
3078 template = gst_static_pad_template_get (&anysinktemplate);
3080 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
3081 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
3082 gst_pad_set_element_private (stream->rtcppad, stream);
3083 gst_pad_set_active (stream->rtcppad, TRUE);
3085 /* get session RTCP pad */
3086 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3087 pad = gst_element_get_request_pad (src->manager, name);
3092 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3093 gst_object_unref (pad);
3096 gst_object_unref (template);
3102 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
3103 GstRTSPTransport * transport, const gchar ** destination, gint * min,
3104 gint * max, guint * ttl)
3106 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3108 if (!(*destination = transport->destination))
3109 *destination = stream->destination;
3112 /* transport first */
3113 *min = transport->port.min;
3114 *max = transport->port.max;
3115 if (*min == -1 && *max == -1) {
3116 /* then try from SDP */
3117 if (stream->port != 0) {
3118 *min = stream->port;
3119 *max = stream->port + 1;
3125 if (!(*ttl = transport->ttl))
3130 /* first take the source, then the endpoint to figure out where to send
3132 if (!(*destination = transport->source)) {
3133 if (src->conninfo.connection)
3134 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3135 else if (stream->conninfo.connection)
3137 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3141 /* for unicast we only expect the ports here */
3142 *min = transport->server_port.min;
3143 *max = transport->server_port.max;
3148 /* For multicast create UDP sources and join the multicast group. */
3150 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3151 GstRTSPTransport * transport, GstPad ** outpad)
3154 const gchar *destination;
3157 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3159 /* we can remove the allocated UDP ports now */
3160 gst_rtspsrc_stream_free_udp (stream);
3162 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3165 /* we need a destination now */
3166 if (destination == NULL)
3167 goto no_destination;
3169 /* we really need ports now or we won't be able to receive anything at all */
3170 if (min == -1 && max == -1)
3173 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3174 destination, min, max);
3176 /* creating UDP source for RTP */
3178 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3180 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3182 if (stream->udpsrc[0] == NULL)
3185 /* take ownership */
3186 gst_object_ref_sink (stream->udpsrc[0]);
3188 if (src->udp_buffer_size != 0)
3189 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3190 src->udp_buffer_size, NULL);
3192 if (src->multi_iface != NULL)
3193 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3194 src->multi_iface, NULL);
3197 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3198 gst_element_set_state (stream->udpsrc[0], GST_STATE_PAUSED);
3201 /* creating another UDP source for RTCP */
3205 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3207 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3209 if (stream->udpsrc[1] == NULL)
3212 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3213 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3214 gst_caps_unref (caps);
3216 /* take ownership */
3217 gst_object_ref_sink (stream->udpsrc[1]);
3219 if (src->multi_iface != NULL)
3220 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3221 src->multi_iface, NULL);
3223 gst_element_set_state (stream->udpsrc[1], GST_STATE_PAUSED);
3230 GST_DEBUG_OBJECT (src, "no UDP source element found");
3235 GST_DEBUG_OBJECT (src, "no destination found");
3240 GST_DEBUG_OBJECT (src, "no ports found");
3245 /* configure the remainder of the UDP ports */
3247 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3248 GstRTSPTransport * transport, GstPad ** outpad)
3250 /* we manage the UDP elements now. For unicast, the UDP sources where
3251 * allocated in the stream when we suggested a transport. */
3252 if (stream->udpsrc[0]) {
3253 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3254 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3256 GST_DEBUG_OBJECT (src, "setting up UDP source");
3258 /* configure a timeout on the UDP port. When the timeout message is
3259 * posted, we assume UDP transport is not possible. We reconnect using TCP
3261 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3262 src->udp_timeout * 1000, NULL);
3264 /* get output pad of the UDP source. */
3265 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3267 /* save it so we can unblock */
3268 stream->blockedpad = *outpad;
3270 /* configure pad block on the pad. As soon as there is dataflow on the
3271 * UDP source, we know that UDP is not blocked by a firewall and we can
3272 * configure all the streams to let the application autoplug decoders. */
3274 gst_pad_add_probe (stream->blockedpad,
3275 GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM, pad_blocked, src, NULL);
3277 if (stream->channelpad[0]) {
3278 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3279 /* configure for UDP delivery, we need to connect the UDP pads to
3280 * the session plugin. */
3281 gst_pad_link_full (*outpad, stream->channelpad[0],
3282 GST_PAD_LINK_CHECK_NOTHING);
3283 gst_object_unref (*outpad);
3285 /* we connected to pad-added signal to get pads from the manager */
3287 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3292 if (stream->udpsrc[1]) {
3295 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3296 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3298 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3299 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3300 gst_caps_unref (caps);
3302 if (stream->channelpad[1]) {
3305 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3307 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3308 gst_pad_link_full (pad, stream->channelpad[1],
3309 GST_PAD_LINK_CHECK_NOTHING);
3310 gst_object_unref (pad);
3312 /* leave unlinked */
3318 /* configure the UDP sink back to the server for status reports */
3320 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3321 GstRTSPStream * stream, GstRTSPTransport * transport)
3324 gint rtp_port, rtcp_port;
3325 gboolean do_rtp, do_rtcp;
3326 const gchar *destination;
3331 /* get transport info */
3332 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
3333 &rtp_port, &rtcp_port, &ttl);
3335 /* see what we need to do */
3336 do_rtp = (rtp_port != -1);
3337 /* it's possible that the server does not want us to send RTCP in which case
3339 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
3341 /* we need a destination when we have RTP or RTCP ports */
3342 if (destination == NULL && (do_rtp || do_rtcp))
3343 goto no_destination;
3345 /* try to construct the fakesrc to the RTP port of the server to open up any
3348 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
3351 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
3352 stream->udpsink[0] =
3353 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3355 if (stream->udpsink[0] == NULL)
3356 goto no_sink_element;
3358 /* don't join multicast group, we will have the source socket do that */
3359 /* no sync or async state changes needed */
3360 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
3361 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3363 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3365 if (stream->udpsrc[0]) {
3366 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
3367 * so that NAT firewalls will open a hole for us */
3368 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
3369 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
3370 /* configure socket and make sure udpsink does not close it when shutting
3371 * down, it belongs to udpsrc after all. */
3372 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
3373 "close-socket", FALSE, NULL);
3374 g_object_unref (socket);
3377 /* the source for the dummy packets to open up NAT */
3378 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
3379 if (stream->fakesrc == NULL)
3380 goto no_fakesrc_element;
3382 /* random data in 5 buffers, a size of 200 bytes should be fine */
3383 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
3384 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
3386 /* we don't want to consider this a sink */
3387 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
3389 /* keep everything locked */
3390 gst_element_set_locked_state (stream->udpsink[0], TRUE);
3391 gst_element_set_locked_state (stream->fakesrc, TRUE);
3393 gst_object_ref (stream->udpsink[0]);
3394 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
3395 gst_object_ref (stream->fakesrc);
3396 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
3398 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
3399 "sink", GST_PAD_LINK_CHECK_NOTHING);
3402 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
3405 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
3406 stream->udpsink[1] =
3407 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3409 if (stream->udpsink[1] == NULL)
3410 goto no_sink_element;
3412 /* don't join multicast group, we will have the source socket do that */
3413 /* no sync or async state changes needed */
3414 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
3415 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3417 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3419 if (stream->udpsrc[1]) {
3420 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
3421 * because some servers check the port number of where it sends RTCP to identify
3422 * the RTCP packets it receives */
3423 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
3424 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
3425 /* configure socket and make sure udpsink does not close it when shutting
3426 * down, it belongs to udpsrc after all. */
3427 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
3428 "close-socket", FALSE, NULL);
3429 g_object_unref (socket);
3432 /* we don't want to consider this a sink */
3433 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
3435 /* we keep this playing always */
3436 gst_element_set_locked_state (stream->udpsink[1], TRUE);
3437 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
3439 gst_object_ref (stream->udpsink[1]);
3440 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
3442 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
3444 /* get session RTCP pad */
3445 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3446 pad = gst_element_get_request_pad (src->manager, name);
3451 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3452 gst_object_unref (pad);
3461 GST_DEBUG_OBJECT (src, "no destination address specified");
3466 GST_DEBUG_OBJECT (src, "no UDP sink element found");
3471 GST_DEBUG_OBJECT (src, "no fakesrc element found");
3476 /* sets up all elements needed for streaming over the specified transport.
3477 * Does not yet expose the element pads, this will be done when there is actuall
3478 * dataflow detected, which might never happen when UDP is blocked in a
3479 * firewall, for example.
3482 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
3483 GstRTSPTransport * transport)
3486 GstPad *outpad = NULL;
3487 GstPadTemplate *template;
3489 const gchar *media_type;
3492 src = stream->parent;
3494 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
3496 /* get the proper media type for this stream now */
3497 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
3498 goto unknown_transport;
3500 goto unknown_transport;
3502 /* configure the final media type */
3503 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
3505 len = stream->ptmap->len;
3506 for (i = 0; i < len; i++) {
3508 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3510 if (item->caps == NULL)
3513 s = gst_caps_get_structure (item->caps, 0);
3514 gst_structure_set_name (s, media_type);
3517 /* try to get and configure a manager, channelpad[0-1] will be configured with
3518 * the pads for the manager, or NULL when no manager is needed. */
3519 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
3522 switch (transport->lower_transport) {
3523 case GST_RTSP_LOWER_TRANS_TCP:
3524 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
3525 goto transport_failed;
3527 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3528 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
3529 goto transport_failed;
3530 /* fallthrough, the rest is the same for UDP and MCAST */
3531 case GST_RTSP_LOWER_TRANS_UDP:
3532 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
3533 goto transport_failed;
3534 /* configure udpsinks back to the server for RTCP messages and for the
3535 * dummy RTP messages to open NAT. */
3536 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
3537 goto transport_failed;
3540 goto unknown_transport;
3544 GST_DEBUG_OBJECT (src, "creating ghostpad");
3546 gst_pad_use_fixed_caps (outpad);
3548 /* create ghostpad, don't add just yet, this will be done when we activate
3550 name = g_strdup_printf ("stream_%u", stream->id);
3551 template = gst_static_pad_template_get (&rtptemplate);
3552 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
3553 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3554 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3555 gst_object_unref (template);
3558 gst_object_unref (outpad);
3560 /* mark pad as ok */
3561 stream->last_ret = GST_FLOW_OK;
3568 GST_DEBUG_OBJECT (src, "failed to configure transport");
3573 GST_DEBUG_OBJECT (src, "unknown transport");
3578 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3583 /* send a couple of dummy random packets on the receiver RTP port to the server,
3584 * this should make a firewall think we initiated the data transfer and
3585 * hopefully allow packets to go from the sender port to our RTP receiver port */
3587 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
3591 if (src->nat_method != GST_RTSP_NAT_DUMMY)
3594 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3595 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3597 if (stream->fakesrc && stream->udpsink[0]) {
3598 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
3599 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
3600 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
3601 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
3602 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
3608 /* Adds the source pads of all configured streams to the element.
3609 * This code is performed when we detected dataflow.
3611 * We detect dataflow from either the _loop function or with pad probes on the
3615 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
3619 GST_DEBUG_OBJECT (src, "activating streams");
3621 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3622 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3624 if (stream->udpsrc[0]) {
3625 /* remove timeout, we are streaming now and timeouts will be handled by
3626 * the session manager and jitter buffer */
3627 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
3629 if (stream->srcpad) {
3630 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
3631 gst_pad_set_active (stream->srcpad, TRUE);
3633 /* if we don't have a session manager, set the caps now. If we have a
3634 * session, we will get a notification of the pad and the caps. */
3635 if (!src->manager) {
3638 caps = stream_get_caps_for_pt (stream, stream->default_pt);
3639 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
3640 gst_pad_set_caps (stream->srcpad, caps);
3643 if (!stream->added) {
3644 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
3645 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3646 stream->added = TRUE;
3651 /* unblock all pads */
3652 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3653 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3655 if (stream->blockid) {
3656 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
3657 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
3658 stream->blockid = 0;
3666 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
3667 gboolean reset_manager)
3670 guint64 start, stop;
3671 gdouble play_speed, play_scale;
3673 GST_DEBUG_OBJECT (src, "configuring stream caps");
3675 start = segment->position;
3676 stop = segment->duration;
3677 play_speed = segment->rate;
3678 play_scale = segment->applied_rate;
3680 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3681 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3684 len = stream->ptmap->len;
3685 for (j = 0; j < len; j++) {
3687 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
3689 if (item->caps == NULL)
3692 caps = gst_caps_make_writable (item->caps);
3694 if (stream->timebase != -1)
3695 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
3696 (guint) stream->timebase, NULL);
3697 if (stream->seqbase != -1)
3698 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
3699 (guint) stream->seqbase, NULL);
3700 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
3702 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
3703 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
3704 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
3707 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
3711 if (reset_manager && src->manager) {
3712 GST_DEBUG_OBJECT (src, "clear session");
3713 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
3717 static GstFlowReturn
3718 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
3723 /* store the value */
3724 stream->last_ret = ret;
3726 /* if it's success we can return the value right away */
3727 if (ret == GST_FLOW_OK)
3730 /* any other error that is not-linked can be returned right
3732 if (ret != GST_FLOW_NOT_LINKED)
3735 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3736 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3737 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3739 ret = ostream->last_ret;
3740 /* some other return value (must be SUCCESS but we can return
3741 * other values as well) */
3742 if (ret != GST_FLOW_NOT_LINKED)
3745 /* if we get here, all other pads were unlinked and we return
3746 * NOT_LINKED then */
3752 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
3755 gboolean res = TRUE;
3757 /* only streams that have a connection to the outside world */
3758 if (stream->container || stream->disabled)
3761 if (stream->udpsrc[0]) {
3762 gst_event_ref (event);
3763 res = gst_element_send_event (stream->udpsrc[0], event);
3764 } else if (stream->channelpad[0]) {
3765 gst_event_ref (event);
3766 if (GST_PAD_IS_SRC (stream->channelpad[0]))
3767 res = gst_pad_push_event (stream->channelpad[0], event);
3769 res = gst_pad_send_event (stream->channelpad[0], event);
3772 if (stream->udpsrc[1]) {
3773 gst_event_ref (event);
3774 res &= gst_element_send_event (stream->udpsrc[1], event);
3775 } else if (stream->channelpad[1]) {
3776 gst_event_ref (event);
3777 if (GST_PAD_IS_SRC (stream->channelpad[1]))
3778 res &= gst_pad_push_event (stream->channelpad[1], event);
3780 res &= gst_pad_send_event (stream->channelpad[1], event);
3784 gst_event_unref (event);
3790 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
3793 gboolean res = TRUE;
3795 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3796 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3798 gst_event_ref (event);
3799 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
3801 gst_event_unref (event);
3806 static GstRTSPResult
3807 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3812 if (info->connection == NULL) {
3813 if (info->url == NULL) {
3814 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
3815 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
3819 /* create connection */
3820 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
3821 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
3822 goto could_not_create;
3825 g_free (info->url_str);
3826 info->url_str = gst_rtsp_url_get_request_uri (info->url);
3828 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
3830 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
3831 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
3832 src->tls_validation_flags))
3833 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
3835 if (src->tls_database)
3836 gst_rtsp_connection_set_tls_database (info->connection,
3840 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
3841 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
3843 if (src->proxy_host) {
3844 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
3846 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
3851 if (!info->connected) {
3854 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
3855 ("Connecting to %s", info->location));
3856 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
3858 gst_rtsp_connection_connect (info->connection,
3859 src->ptcp_timeout)) < 0)
3860 goto could_not_connect;
3862 info->connected = TRUE;
3869 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
3874 gchar *str = gst_rtsp_strresult (res);
3875 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
3881 gchar *str = gst_rtsp_strresult (res);
3882 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
3888 static GstRTSPResult
3889 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
3892 GST_RTSP_STATE_LOCK (src);
3893 if (info->connected) {
3894 GST_DEBUG_OBJECT (src, "closing connection...");
3895 gst_rtsp_connection_close (info->connection);
3896 info->connected = FALSE;
3898 if (free && info->connection) {
3899 /* free connection */
3900 GST_DEBUG_OBJECT (src, "freeing connection...");
3901 gst_rtsp_connection_free (info->connection);
3902 info->connection = NULL;
3904 GST_RTSP_STATE_UNLOCK (src);
3908 static GstRTSPResult
3909 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3914 GST_DEBUG_OBJECT (src, "reconnecting connection...");
3915 gst_rtsp_conninfo_close (src, info, FALSE);
3916 res = gst_rtsp_conninfo_connect (src, info, async);
3922 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
3926 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
3927 GST_RTSP_STATE_LOCK (src);
3928 if (src->conninfo.connection && src->conninfo.flushing != flush) {
3929 GST_DEBUG_OBJECT (src, "connection flush");
3930 gst_rtsp_connection_flush (src->conninfo.connection, flush);
3931 src->conninfo.flushing = flush;
3933 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3934 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3935 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
3936 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
3937 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
3938 stream->conninfo.flushing = flush;
3941 GST_RTSP_STATE_UNLOCK (src);
3944 /* FIXME, handle server request, reply with OK, for now */
3945 static GstRTSPResult
3946 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
3947 GstRTSPMessage * request)
3949 GstRTSPMessage response = { 0 };
3952 GST_DEBUG_OBJECT (src, "got server request message");
3955 gst_rtsp_message_dump (request);
3957 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
3959 if (res == GST_RTSP_ENOTIMPL) {
3960 /* default implementation, send OK */
3961 GST_DEBUG_OBJECT (src, "prepare OK reply");
3963 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
3968 /* let app parse and reply */
3969 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
3970 0, request, &response);
3973 gst_rtsp_message_dump (&response);
3975 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
3979 gst_rtsp_message_unset (&response);
3980 } else if (res == GST_RTSP_EEOF)
3988 gst_rtsp_message_unset (&response);
3993 /* send server keep-alive */
3994 static GstRTSPResult
3995 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
3997 GstRTSPMessage request = { 0 };
3999 GstRTSPMethod method;
4000 const gchar *control;
4002 if (src->do_rtsp_keep_alive == FALSE) {
4003 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
4004 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4008 GST_DEBUG_OBJECT (src, "creating server keep-alive");
4010 /* find a method to use for keep-alive */
4011 if (src->methods & GST_RTSP_GET_PARAMETER)
4012 method = GST_RTSP_GET_PARAMETER;
4014 method = GST_RTSP_OPTIONS;
4016 control = get_aggregate_control (src);
4017 if (control == NULL)
4020 res = gst_rtsp_message_init_request (&request, method, control);
4025 gst_rtsp_message_dump (&request);
4028 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
4033 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4034 gst_rtsp_message_unset (&request);
4041 GST_WARNING_OBJECT (src, "no control url to send keepalive");
4046 gchar *str = gst_rtsp_strresult (res);
4048 gst_rtsp_message_unset (&request);
4049 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
4050 ("Could not send keep-alive. (%s)", str));
4056 static GstFlowReturn
4057 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
4059 GstFlowReturn ret = GST_FLOW_OK;
4061 GstRTSPStream *stream;
4062 GstPad *outpad = NULL;
4069 channel = message->type_data.data.channel;
4071 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
4073 goto unknown_stream;
4075 if (channel == stream->channel[0]) {
4076 outpad = stream->channelpad[0];
4078 } else if (channel == stream->channel[1]) {
4079 outpad = stream->channelpad[1];
4085 /* take a look at the body to figure out what we have */
4086 gst_rtsp_message_get_body (message, &data, &size);
4088 goto invalid_length;
4090 /* channels are not correct on some servers, do extra check */
4091 if (data[1] >= 200 && data[1] <= 204) {
4092 /* hmm RTCP message switch to the RTCP pad of the same stream. */
4093 outpad = stream->channelpad[1];
4097 /* we have no clue what this is, just ignore then. */
4099 goto unknown_stream;
4101 /* take the message body for further processing */
4102 gst_rtsp_message_steal_body (message, &data, &size);
4104 /* strip the trailing \0 */
4107 buf = gst_buffer_new ();
4108 gst_buffer_append_memory (buf,
4109 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
4111 /* don't need message anymore */
4112 gst_rtsp_message_unset (message);
4114 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
4117 if (src->need_activate) {
4123 guint group_id = gst_util_group_id_next ();
4125 /* generate an SHA256 sum of the URI */
4126 cs = g_checksum_new (G_CHECKSUM_SHA256);
4127 uri = src->conninfo.location;
4128 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
4130 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4131 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4134 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
4135 event = gst_event_new_stream_start (stream_id);
4136 gst_event_set_group_id (event, group_id);
4139 gst_rtspsrc_stream_push_event (src, ostream, event);
4141 g_checksum_free (cs);
4143 gst_rtspsrc_activate_streams (src);
4144 src->need_activate = FALSE;
4146 if ((event = src->start_segment) != NULL) {
4147 src->start_segment = NULL;
4148 gst_rtspsrc_push_event (src, event);
4151 if (src->base_time == -1) {
4152 /* Take current running_time. This timestamp will be put on
4153 * the first buffer of each stream because we are a live source and so we
4154 * timestamp with the running_time. When we are dealing with TCP, we also
4155 * only timestamp the first buffer (using the DISCONT flag) because a server
4156 * typically bursts data, for which we don't want to compensate by speeding
4157 * up the media. The other timestamps will be interpollated from this one
4158 * using the RTP timestamps. */
4159 GST_OBJECT_LOCK (src);
4160 if (GST_ELEMENT_CLOCK (src)) {
4162 GstClockTime base_time;
4164 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
4165 base_time = GST_ELEMENT_CAST (src)->base_time;
4167 src->base_time = now - base_time;
4169 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
4170 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
4172 GST_OBJECT_UNLOCK (src);
4175 if (stream->discont && !is_rtcp) {
4176 /* mark first RTP buffer as discont */
4177 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4178 stream->discont = FALSE;
4179 /* first buffer gets the timestamp, other buffers are not timestamped and
4180 * their presentation time will be interpollated from the rtp timestamps. */
4181 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
4182 GST_TIME_ARGS (src->base_time));
4184 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
4187 /* chain to the peer pad */
4188 if (GST_PAD_IS_SINK (outpad))
4189 ret = gst_pad_chain (outpad, buf);
4191 ret = gst_pad_push (outpad, buf);
4194 /* combine all stream flows for the data transport */
4195 ret = gst_rtspsrc_combine_flows (src, stream, ret);
4202 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
4203 gst_rtsp_message_unset (message);
4208 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4209 ("Short message received, ignoring."));
4210 gst_rtsp_message_unset (message);
4215 static GstFlowReturn
4216 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
4218 GstRTSPMessage message = { 0 };
4220 GstFlowReturn ret = GST_FLOW_OK;
4221 GTimeVal tv_timeout;
4224 /* get the next timeout interval */
4225 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4227 /* see if the timeout period expired */
4228 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
4229 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
4230 /* send keep-alive, only act on interrupt, a warning will be posted for
4232 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4234 /* get new timeout */
4235 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4238 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
4239 tv_timeout.tv_sec, tv_timeout.tv_usec);
4241 /* protect the connection with the connection lock so that we can see when
4242 * we are finished doing server communication */
4244 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4245 &message, src->ptcp_timeout);
4249 GST_DEBUG_OBJECT (src, "we received a server message");
4251 case GST_RTSP_EINTR:
4252 /* we got interrupted this means we need to stop */
4254 case GST_RTSP_ETIMEOUT:
4255 /* no reply, send keep alive */
4256 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4257 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4261 /* go EOS when the server closed the connection */
4267 switch (message.type) {
4268 case GST_RTSP_MESSAGE_REQUEST:
4269 /* server sends us a request message, handle it */
4271 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4273 if (res == GST_RTSP_EEOF)
4276 goto handle_request_failed;
4278 case GST_RTSP_MESSAGE_RESPONSE:
4279 /* we ignore response messages */
4280 GST_DEBUG_OBJECT (src, "ignoring response message");
4282 gst_rtsp_message_dump (&message);
4284 case GST_RTSP_MESSAGE_DATA:
4285 GST_DEBUG_OBJECT (src, "got data message");
4286 ret = gst_rtspsrc_handle_data (src, &message);
4287 if (ret != GST_FLOW_OK)
4288 goto handle_data_failed;
4291 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4296 g_assert_not_reached ();
4301 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4302 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4303 ("The server closed the connection."));
4304 src->conninfo.connected = FALSE;
4305 gst_rtsp_message_unset (&message);
4306 return GST_FLOW_EOS;
4310 gst_rtsp_message_unset (&message);
4311 GST_DEBUG_OBJECT (src, "got interrupted");
4312 return GST_FLOW_FLUSHING;
4316 gchar *str = gst_rtsp_strresult (res);
4318 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4319 ("Could not receive message. (%s)", str));
4322 gst_rtsp_message_unset (&message);
4323 return GST_FLOW_ERROR;
4325 handle_request_failed:
4327 gchar *str = gst_rtsp_strresult (res);
4329 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4330 ("Could not handle server message. (%s)", str));
4332 gst_rtsp_message_unset (&message);
4333 return GST_FLOW_ERROR;
4337 GST_DEBUG_OBJECT (src, "could no handle data message");
4342 static GstFlowReturn
4343 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
4346 GstRTSPMessage message = { 0 };
4350 GTimeVal tv_timeout;
4352 /* get the next timeout interval */
4353 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4355 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
4356 (gint) tv_timeout.tv_sec);
4358 gst_rtsp_message_unset (&message);
4360 /* we should continue reading the TCP socket because the server might
4361 * send us requests. When the session timeout expires, we need to send a
4362 * keep-alive request to keep the session open. */
4363 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4364 &message, &tv_timeout);
4368 GST_DEBUG_OBJECT (src, "we received a server message");
4370 case GST_RTSP_EINTR:
4371 /* we got interrupted, see what we have to do */
4373 case GST_RTSP_ETIMEOUT:
4374 /* send keep-alive, ignore the result, a warning will be posted. */
4375 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4376 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4380 /* server closed the connection. not very fatal for UDP, reconnect and
4381 * see what happens. */
4382 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4383 ("The server closed the connection."));
4384 if (src->udp_reconnect) {
4386 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
4393 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
4395 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4396 ("Unhandled return value %d.", res));
4400 switch (message.type) {
4401 case GST_RTSP_MESSAGE_REQUEST:
4402 /* server sends us a request message, handle it */
4404 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4406 if (res == GST_RTSP_EEOF)
4409 goto handle_request_failed;
4411 case GST_RTSP_MESSAGE_RESPONSE:
4412 /* we ignore response and data messages */
4413 GST_DEBUG_OBJECT (src, "ignoring response message");
4415 gst_rtsp_message_dump (&message);
4416 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4417 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
4418 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
4419 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
4420 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4427 case GST_RTSP_MESSAGE_DATA:
4428 /* we ignore response and data messages */
4429 GST_DEBUG_OBJECT (src, "ignoring data message");
4432 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4437 g_assert_not_reached ();
4439 /* we get here when the connection got interrupted */
4442 gst_rtsp_message_unset (&message);
4443 GST_DEBUG_OBJECT (src, "got interrupted");
4444 return GST_FLOW_FLUSHING;
4448 gchar *str = gst_rtsp_strresult (res);
4451 src->conninfo.connected = FALSE;
4452 if (res != GST_RTSP_EINTR) {
4453 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
4454 ("Could not connect to server. (%s)", str));
4456 ret = GST_FLOW_ERROR;
4458 ret = GST_FLOW_FLUSHING;
4464 gchar *str = gst_rtsp_strresult (res);
4466 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4467 ("Could not receive message. (%s)", str));
4469 return GST_FLOW_ERROR;
4471 handle_request_failed:
4473 gchar *str = gst_rtsp_strresult (res);
4476 gst_rtsp_message_unset (&message);
4477 if (res != GST_RTSP_EINTR) {
4478 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4479 ("Could not handle server message. (%s)", str));
4481 ret = GST_FLOW_ERROR;
4483 ret = GST_FLOW_FLUSHING;
4489 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4490 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4491 ("The server closed the connection."));
4492 src->conninfo.connected = FALSE;
4493 gst_rtsp_message_unset (&message);
4494 return GST_FLOW_EOS;
4498 static GstRTSPResult
4499 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
4501 GstRTSPResult res = GST_RTSP_OK;
4504 GST_DEBUG_OBJECT (src, "doing reconnect");
4506 GST_OBJECT_LOCK (src);
4507 /* only restart when the pads were not yet activated, else we were
4508 * streaming over UDP */
4509 restart = src->need_activate;
4510 GST_OBJECT_UNLOCK (src);
4512 /* no need to restart, we're done */
4516 /* we can try only TCP now */
4517 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
4519 /* close and cleanup our state */
4520 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
4523 /* see if we have TCP left to try. Also don't try TCP when we were configured
4525 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
4528 /* We post a warning message now to inform the user
4529 * that nothing happened. It's most likely a firewall thing. */
4530 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4531 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4532 "firewall is blocking it. Retrying using a TCP connection.",
4533 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4535 /* open new connection using tcp */
4536 if (gst_rtspsrc_open (src, async) < 0)
4539 /* start playback */
4540 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
4549 src->cur_protocols = 0;
4550 /* no transport possible, post an error and stop */
4551 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4552 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4553 "firewall is blocking it. No other protocols to try.",
4554 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4555 return GST_RTSP_ERROR;
4559 GST_DEBUG_OBJECT (src, "open failed");
4564 GST_DEBUG_OBJECT (src, "play failed");
4570 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
4574 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
4577 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
4580 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
4583 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
4591 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
4595 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
4598 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
4601 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
4604 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
4612 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
4616 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
4619 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
4622 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
4625 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
4633 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
4637 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
4640 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
4643 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
4646 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
4654 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
4656 if (ret == GST_RTSP_OK)
4657 gst_rtspsrc_loop_complete_cmd (src, cmd);
4658 else if (ret == GST_RTSP_EINTR)
4659 gst_rtspsrc_loop_cancel_cmd (src, cmd);
4661 gst_rtspsrc_loop_error_cmd (src, cmd);
4665 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
4668 gboolean flushed = FALSE;
4670 /* start new request */
4671 gst_rtspsrc_loop_start_cmd (src, cmd);
4673 GST_DEBUG_OBJECT (src, "sending cmd %d", cmd);
4675 GST_OBJECT_LOCK (src);
4676 old = src->pending_cmd;
4677 if (old == CMD_RECONNECT) {
4678 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
4679 cmd = CMD_RECONNECT;
4681 if (old != CMD_WAIT) {
4682 src->pending_cmd = CMD_WAIT;
4683 GST_OBJECT_UNLOCK (src);
4684 /* cancel previous request */
4685 GST_DEBUG_OBJECT (src, "cancel previous request %d", old);
4686 gst_rtspsrc_loop_cancel_cmd (src, old);
4687 GST_OBJECT_LOCK (src);
4689 src->pending_cmd = cmd;
4690 /* interrupt if allowed */
4691 if (src->busy_cmd & mask) {
4692 GST_DEBUG_OBJECT (src, "connection flush busy %d", src->busy_cmd);
4693 gst_rtspsrc_connection_flush (src, TRUE);
4696 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %d", src->busy_cmd);
4699 gst_task_start (src->task);
4700 GST_OBJECT_UNLOCK (src);
4706 gst_rtspsrc_loop (GstRTSPSrc * src)
4710 if (!src->conninfo.connection || !src->conninfo.connected)
4713 if (src->interleaved)
4714 ret = gst_rtspsrc_loop_interleaved (src);
4716 ret = gst_rtspsrc_loop_udp (src);
4718 if (ret != GST_FLOW_OK)
4726 GST_WARNING_OBJECT (src, "we are not connected");
4727 ret = GST_FLOW_FLUSHING;
4732 const gchar *reason = gst_flow_get_name (ret);
4734 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
4735 src->running = FALSE;
4736 if (ret == GST_FLOW_EOS) {
4737 /* perform EOS logic */
4738 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4739 gst_element_post_message (GST_ELEMENT_CAST (src),
4740 gst_message_new_segment_done (GST_OBJECT_CAST (src),
4741 src->segment.format, src->segment.position));
4742 gst_rtspsrc_push_event (src,
4743 gst_event_new_segment_done (src->segment.format,
4744 src->segment.position));
4746 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4748 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
4749 /* for fatal errors we post an error message, post the error before the
4750 * EOS so the app knows about the error first. */
4751 GST_ELEMENT_ERROR (src, STREAM, FAILED,
4752 ("Internal data flow error."),
4753 ("streaming task paused, reason %s (%d)", reason, ret));
4754 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4756 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
4761 #ifndef GST_DISABLE_GST_DEBUG
4762 static const gchar *
4763 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
4767 while (method != 0) {
4784 static const gchar *
4785 gst_rtspsrc_skip_lws (const gchar * s)
4787 while (g_ascii_isspace (*s))
4792 static const gchar *
4793 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
4795 while (s > start && g_ascii_isspace (*(s - 1)))
4800 static const gchar *
4801 gst_rtspsrc_skip_commas (const gchar * s)
4803 /* The grammar allows for multiple commas */
4804 while (g_ascii_isspace (*s) || *s == ',')
4809 static const gchar *
4810 gst_rtspsrc_skip_item (const gchar * s)
4812 gboolean quoted = FALSE;
4813 const gchar *start = s;
4815 /* A list item ends at the last non-whitespace character
4816 * before a comma which is not inside a quoted-string. Or at
4817 * the end of the string.
4823 if (*s == '\\' && *(s + 1))
4832 return gst_rtspsrc_unskip_lws (s, start);
4836 gst_rtsp_decode_quoted_string (gchar * quoted_string)
4840 src = quoted_string + 1;
4841 dst = quoted_string;
4842 while (*src && *src != '"') {
4843 if (*src == '\\' && *(src + 1))
4850 /* Extract the authentication tokens that the server provided for each method
4851 * into an array of structures and give those to the connection object.
4854 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
4855 const gchar * header, gboolean * stale)
4857 GSList *list = NULL, *iter;
4859 gchar *item, *eq, *name_end, *value;
4861 g_return_if_fail (stale != NULL);
4863 gst_rtsp_connection_clear_auth_params (conn);
4866 /* Parse a header whose content is described by RFC2616 as
4867 * "#something", where "something" does not itself contain commas,
4868 * except as part of quoted-strings, into a list of allocated strings.
4870 header = gst_rtspsrc_skip_commas (header);
4872 end = gst_rtspsrc_skip_item (header);
4873 list = g_slist_prepend (list, g_strndup (header, end - header));
4874 header = gst_rtspsrc_skip_commas (end);
4879 list = g_slist_reverse (list);
4880 for (iter = list; iter; iter = iter->next) {
4883 eq = strchr (item, '=');
4885 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
4886 if (name_end == item) {
4887 /* That's no good... */
4894 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
4896 gst_rtsp_decode_quoted_string (value);
4900 if (item && (strcmp (item, "stale") == 0) &&
4901 value && (strcmp (value, "TRUE") == 0))
4903 gst_rtsp_connection_set_auth_param (conn, item, value);
4907 g_slist_free (list);
4910 /* Parse a WWW-Authenticate Response header and determine the
4911 * available authentication methods
4913 * This code should also cope with the fact that each WWW-Authenticate
4914 * header can contain multiple challenge methods + tokens
4916 * At the moment, for Basic auth, we just do a minimal check and don't
4917 * even parse out the realm */
4919 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
4920 GstRTSPConnection * conn, gboolean * stale)
4924 g_return_if_fail (hdr != NULL);
4925 g_return_if_fail (methods != NULL);
4926 g_return_if_fail (stale != NULL);
4928 /* Skip whitespace at the start of the string */
4929 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
4931 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
4932 *methods |= GST_RTSP_AUTH_BASIC;
4933 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
4934 *methods |= GST_RTSP_AUTH_DIGEST;
4935 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
4940 * gst_rtspsrc_setup_auth:
4941 * @src: the rtsp source
4943 * Configure a username and password and auth method on the
4944 * connection object based on a response we received from the
4947 * Currently, this requires that a username and password were supplied
4948 * in the uri. In the future, they may be requested on demand by sending
4949 * a message up the bus.
4951 * Returns: TRUE if authentication information could be set up correctly.
4954 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
4958 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
4959 GstRTSPAuthMethod method;
4960 GstRTSPResult auth_result;
4962 GstRTSPConnection *conn;
4964 gboolean stale = FALSE;
4966 conn = src->conninfo.connection;
4968 /* Identify the available auth methods and see if any are supported */
4969 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
4970 &hdr, 0) == GST_RTSP_OK) {
4971 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
4974 if (avail_methods == GST_RTSP_AUTH_NONE)
4975 goto no_auth_available;
4977 /* For digest auth, if the response indicates that the session
4978 * data are stale, we just update them in the connection object and
4979 * return TRUE to retry the request */
4981 src->tried_url_auth = FALSE;
4983 url = gst_rtsp_connection_get_url (conn);
4985 /* Do we have username and password available? */
4986 if (url != NULL && !src->tried_url_auth && url->user != NULL
4987 && url->passwd != NULL) {
4990 src->tried_url_auth = TRUE;
4991 GST_DEBUG_OBJECT (src,
4992 "Attempting authentication using credentials from the URL");
4994 user = src->user_id;
4995 pass = src->user_pw;
4996 GST_DEBUG_OBJECT (src,
4997 "Attempting authentication using credentials from the properties");
5000 /* FIXME: If the url didn't contain username and password or we tried them
5001 * already, request a username and passwd from the application via some kind
5002 * of credentials request message */
5004 /* If we don't have a username and passwd at this point, bail out. */
5005 if (user == NULL || pass == NULL)
5008 /* Try to configure for each available authentication method, strongest to
5010 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
5011 /* Check if this method is available on the server */
5012 if ((method & avail_methods) == 0)
5015 /* Pass the credentials to the connection to try on the next request */
5016 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
5017 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
5018 * ignore it and end up retrying later */
5019 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
5020 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
5021 gst_rtsp_auth_method_to_string (method));
5026 if (method == GST_RTSP_AUTH_NONE)
5027 goto no_auth_available;
5033 /* Output an error indicating that we couldn't connect because there were
5034 * no supported authentication protocols */
5035 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5036 ("No supported authentication protocol was found"));
5041 /* We don't fire an error message, we just return FALSE and let the
5042 * normal NOT_AUTHORIZED error be propagated */
5047 static GstRTSPResult
5048 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5049 GstRTSPMessage * request, GstRTSPMessage * response,
5050 GstRTSPStatusCode * code)
5053 GstRTSPStatusCode thecode;
5054 gchar *content_base = NULL;
5058 if (!src->short_header)
5059 gst_rtsp_ext_list_before_send (src->extensions, request);
5061 GST_DEBUG_OBJECT (src, "sending message");
5064 gst_rtsp_message_dump (request);
5066 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
5070 gst_rtsp_connection_reset_timeout (conn);
5073 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
5078 gst_rtsp_message_dump (response);
5080 switch (response->type) {
5081 case GST_RTSP_MESSAGE_REQUEST:
5082 res = gst_rtspsrc_handle_request (src, conn, response);
5083 if (res == GST_RTSP_EEOF)
5086 goto handle_request_failed;
5088 case GST_RTSP_MESSAGE_RESPONSE:
5089 /* ok, a response is good */
5090 GST_DEBUG_OBJECT (src, "received response message");
5092 case GST_RTSP_MESSAGE_DATA:
5093 /* get next response */
5094 GST_DEBUG_OBJECT (src, "handle data response message");
5095 gst_rtspsrc_handle_data (src, response);
5098 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5103 thecode = response->type_data.response.code;
5105 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
5107 /* if the caller wanted the result code, we store it. */
5111 /* If the request didn't succeed, bail out before doing any more */
5112 if (thecode != GST_RTSP_STS_OK)
5115 /* store new content base if any */
5116 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
5119 g_free (src->content_base);
5120 src->content_base = g_strdup (content_base);
5122 gst_rtsp_ext_list_after_send (src->extensions, request, response);
5129 gchar *str = gst_rtsp_strresult (res);
5131 if (res != GST_RTSP_EINTR) {
5132 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5133 ("Could not send message. (%s)", str));
5135 GST_WARNING_OBJECT (src, "send interrupted");
5144 GST_WARNING_OBJECT (src, "server closed connection");
5145 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
5147 /* if reconnect succeeds, try again */
5149 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
5153 /* only try once after reconnect, then fallthrough and error out */
5156 gchar *str = gst_rtsp_strresult (res);
5158 if (res != GST_RTSP_EINTR) {
5159 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5160 ("Could not receive message. (%s)", str));
5162 GST_WARNING_OBJECT (src, "receive interrupted");
5170 handle_request_failed:
5172 /* ERROR was posted */
5173 gst_rtsp_message_unset (response);
5178 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5179 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5180 ("The server closed the connection."));
5181 gst_rtsp_message_unset (response);
5188 * @src: the rtsp source
5189 * @conn: the connection to send on
5190 * @request: must point to a valid request
5191 * @response: must point to an empty #GstRTSPMessage
5192 * @code: an optional code result
5194 * send @request and retrieve the response in @response. optionally @code can be
5195 * non-NULL in which case it will contain the status code of the response.
5197 * If This function returns #GST_RTSP_OK, @response will contain a valid response
5198 * message that should be cleaned with gst_rtsp_message_unset() after usage.
5200 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
5201 * @response message) if the response code was not 200 (OK).
5203 * If the attempt results in an authentication failure, then this will attempt
5204 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
5207 * Returns: #GST_RTSP_OK if the processing was successful.
5209 static GstRTSPResult
5210 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5211 GstRTSPMessage * request, GstRTSPMessage * response,
5212 GstRTSPStatusCode * code)
5214 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
5215 GstRTSPResult res = GST_RTSP_ERROR;
5218 GstRTSPMethod method = GST_RTSP_INVALID;
5224 /* make sure we don't loop forever */
5228 /* save method so we can disable it when the server complains */
5229 method = request->type_data.request.method;
5232 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
5236 case GST_RTSP_STS_UNAUTHORIZED:
5237 if (gst_rtspsrc_setup_auth (src, response)) {
5238 /* Try the request/response again after configuring the auth info
5246 } while (retry == TRUE);
5248 /* If the user requested the code, let them handle errors, otherwise
5249 * post an error below */
5252 else if (int_code != GST_RTSP_STS_OK)
5253 goto error_response;
5260 GST_DEBUG_OBJECT (src, "got error %d", res);
5265 res = GST_RTSP_ERROR;
5267 switch (response->type_data.response.code) {
5268 case GST_RTSP_STS_NOT_FOUND:
5269 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
5270 response->type_data.response.reason));
5272 case GST_RTSP_STS_MOVED_PERMANENTLY:
5273 case GST_RTSP_STS_MOVE_TEMPORARILY:
5275 gchar *new_location;
5276 GstRTSPLowerTrans transports;
5278 GST_DEBUG_OBJECT (src, "got redirection");
5279 /* if we don't have a Location Header, we must error */
5280 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
5281 &new_location, 0) < 0)
5284 /* When we receive a redirect result, we go back to the INIT state after
5285 * parsing the new URI. The caller should do the needed steps to issue
5286 * a new setup when it detects this state change. */
5287 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
5289 /* save current transports */
5290 if (src->conninfo.url)
5291 transports = src->conninfo.url->transports;
5293 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
5295 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
5297 /* set old transports */
5298 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
5299 src->conninfo.url->transports = transports;
5301 src->need_redirect = TRUE;
5302 src->state = GST_RTSP_STATE_INIT;
5306 case GST_RTSP_STS_NOT_ACCEPTABLE:
5307 case GST_RTSP_STS_NOT_IMPLEMENTED:
5308 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
5309 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
5310 gst_rtsp_method_as_text (method));
5311 src->methods &= ~method;
5315 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5316 ("Got error response: %d (%s).", response->type_data.response.code,
5317 response->type_data.response.reason));
5320 /* if we return ERROR we should unset the response ourselves */
5321 if (res == GST_RTSP_ERROR)
5322 gst_rtsp_message_unset (response);
5328 static GstRTSPResult
5329 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
5330 GstRTSPMessage * response, GstRTSPSrc * src)
5332 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
5337 /* parse the response and collect all the supported methods. We need this
5338 * information so that we don't try to send an unsupported request to the
5342 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
5344 GstRTSPHeaderField field;
5348 /* reset supported methods */
5351 /* Try Allow Header first */
5352 field = GST_RTSP_HDR_ALLOW;
5355 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5356 if (indx == 0 && !respoptions) {
5357 /* if no Allow header was found then try the Public header... */
5358 field = GST_RTSP_HDR_PUBLIC;
5359 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5364 src->methods |= gst_rtsp_options_from_text (respoptions);
5369 if (src->methods == 0) {
5370 /* neither Allow nor Public are required, assume the server supports
5371 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
5373 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
5374 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
5376 /* always assume PLAY, FIXME, extensions should be able to override
5378 src->methods |= GST_RTSP_PLAY;
5379 /* also assume it will support Range */
5380 src->seekable = TRUE;
5382 /* we need describe and setup */
5383 if (!(src->methods & GST_RTSP_DESCRIBE))
5385 if (!(src->methods & GST_RTSP_SETUP))
5393 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5394 ("Server does not support DESCRIBE."));
5399 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5400 ("Server does not support SETUP."));
5405 /* masks to be kept in sync with the hardcoded protocol order of preference
5407 static guint protocol_masks[] = {
5408 GST_RTSP_LOWER_TRANS_UDP,
5409 GST_RTSP_LOWER_TRANS_UDP_MCAST,
5410 GST_RTSP_LOWER_TRANS_TCP,
5414 static GstRTSPResult
5415 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
5416 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
5420 gboolean add_udp_str;
5425 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
5430 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
5432 /* extension listed transports, use those */
5433 if (*transports != NULL)
5436 /* it's the default */
5437 add_udp_str = FALSE;
5439 /* the default RTSP transports */
5440 result = g_string_new ("RTP");
5443 case GST_RTSP_PROFILE_AVP:
5444 g_string_append (result, "/AVP");
5446 case GST_RTSP_PROFILE_SAVP:
5447 g_string_append (result, "/SAVP");
5449 case GST_RTSP_PROFILE_AVPF:
5450 g_string_append (result, "/AVPF");
5452 case GST_RTSP_PROFILE_SAVPF:
5453 g_string_append (result, "/SAVPF");
5459 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
5460 GST_DEBUG_OBJECT (src, "adding UDP unicast");
5462 g_string_append (result, "/UDP");
5463 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
5464 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
5465 GST_DEBUG_OBJECT (src, "adding UDP multicast");
5466 /* we don't have to allocate any UDP ports yet, if the selected transport
5467 * turns out to be multicast we can create them and join the multicast
5468 * group indicated in the transport reply */
5470 g_string_append (result, "/UDP");
5471 g_string_append (result, ";multicast");
5472 if (src->next_port_num != 0) {
5473 if (src->client_port_range.max > 0 &&
5474 src->next_port_num >= src->client_port_range.max)
5477 g_string_append_printf (result, ";client_port=%d-%d",
5478 src->next_port_num, src->next_port_num + 1);
5480 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
5481 GST_DEBUG_OBJECT (src, "adding TCP");
5483 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
5485 *transports = g_string_free (result, FALSE);
5487 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
5494 GST_ERROR ("extension gave error %d", res);
5499 GST_ERROR ("no more ports available");
5500 return GST_RTSP_ERROR;
5504 static GstRTSPResult
5505 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
5506 gint orig_rtpport, gint orig_rtcpport)
5509 gint nr_udp, nr_int;
5511 gint rtpport = 0, rtcpport = 0;
5514 src = stream->parent;
5516 /* find number of placeholders first */
5517 if (strstr (*transports, "%%i2"))
5519 else if (strstr (*transports, "%%i1"))
5524 if (strstr (*transports, "%%u2"))
5526 else if (strstr (*transports, "%%u1"))
5531 if (nr_udp == 0 && nr_int == 0)
5535 if (!orig_rtpport || !orig_rtcpport) {
5536 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
5539 rtpport = orig_rtpport;
5540 rtcpport = orig_rtcpport;
5544 str = g_string_new ("");
5546 while ((next = strstr (p, "%%"))) {
5547 g_string_append_len (str, p, next - p);
5548 if (next[2] == 'u') {
5550 g_string_append_printf (str, "%d", rtpport);
5551 else if (next[3] == '2')
5552 g_string_append_printf (str, "%d", rtcpport);
5554 if (next[2] == 'i') {
5556 g_string_append_printf (str, "%d", src->free_channel);
5557 else if (next[3] == '2')
5558 g_string_append_printf (str, "%d", src->free_channel + 1);
5563 /* append final part */
5564 g_string_append (str, p);
5566 g_free (*transports);
5567 *transports = g_string_free (str, FALSE);
5575 GST_ERROR ("failed to allocate udp ports");
5576 return GST_RTSP_ERROR;
5580 /* Perform the SETUP request for all the streams.
5582 * We ask the server for a specific transport, which initially includes all the
5583 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
5584 * two local UDP ports that we send to the server.
5586 * Once the server replied with a transport, we configure the other streams
5587 * with the same transport.
5589 * This function will also configure the stream for the selected transport,
5590 * which basically means creating the pipeline.
5592 static GstRTSPResult
5593 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
5596 GstRTSPResult res = GST_RTSP_ERROR;
5597 GstRTSPMessage request = { 0 };
5598 GstRTSPMessage response = { 0 };
5599 GstRTSPStream *stream = NULL;
5600 GstRTSPLowerTrans protocols;
5601 GstRTSPStatusCode code;
5602 gboolean unsupported_real = FALSE;
5603 gint rtpport, rtcpport;
5607 if (src->conninfo.connection) {
5608 url = gst_rtsp_connection_get_url (src->conninfo.connection);
5609 /* we initially allow all configured lower transports. based on the URL
5610 * transports and the replies from the server we narrow them down. */
5611 protocols = url->transports & src->cur_protocols;
5614 protocols = src->cur_protocols;
5620 /* reset some state */
5621 src->free_channel = 0;
5622 src->interleaved = FALSE;
5623 src->need_activate = FALSE;
5624 /* keep track of next port number, 0 is random */
5625 src->next_port_num = src->client_port_range.min;
5626 rtpport = rtcpport = 0;
5628 if (G_UNLIKELY (src->streams == NULL))
5631 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5632 GstRTSPConnection *conn;
5639 stream = (GstRTSPStream *) walk->data;
5640 caps = stream_get_caps_for_pt (stream, stream->default_pt);
5642 GST_DEBUG_OBJECT (src, "skipping stream %p, no caps", stream);
5646 /* see if we need to configure this stream */
5647 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
5648 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
5650 stream->disabled = TRUE;
5654 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
5655 stream->id, caps, &selected);
5657 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
5658 stream->disabled = TRUE;
5661 stream->disabled = FALSE;
5663 /* merge/overwrite global caps */
5668 s = gst_caps_get_structure (caps, 0);
5670 num = gst_structure_n_fields (src->props);
5671 for (j = 0; j < num; j++) {
5675 name = gst_structure_nth_field_name (src->props, j);
5676 val = gst_structure_get_value (src->props, name);
5677 gst_structure_set_value (s, name, val);
5679 GST_DEBUG_OBJECT (src, "copied %s", name);
5683 /* skip setup if we have no URL for it */
5684 if (stream->conninfo.location == NULL) {
5685 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
5689 if (src->conninfo.connection == NULL) {
5690 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
5691 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
5694 conn = stream->conninfo.connection;
5696 conn = src->conninfo.connection;
5698 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
5699 stream->conninfo.location);
5701 /* if we have a multicast connection, only suggest multicast from now on */
5702 if (stream->is_multicast)
5703 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
5706 /* first selectable protocol */
5707 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5709 if (!protocol_masks[mask])
5713 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
5714 protocol_masks[mask]);
5715 /* create a string with first transport in line */
5717 res = gst_rtspsrc_create_transports_string (src,
5718 protocols & protocol_masks[mask], stream->profile, &transports);
5719 if (res < 0 || transports == NULL)
5720 goto setup_transport_failed;
5722 if (strlen (transports) == 0) {
5723 g_free (transports);
5724 GST_DEBUG_OBJECT (src, "no transports found");
5729 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
5731 /* replace placeholders with real values, this function will optionally
5732 * allocate UDP ports and other info needed to execute the setup request */
5733 res = gst_rtspsrc_prepare_transports (stream, &transports,
5734 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
5736 g_free (transports);
5737 goto setup_transport_failed;
5740 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
5742 /* create SETUP request */
5744 gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
5745 stream->conninfo.location);
5747 g_free (transports);
5748 goto create_request_failed;
5751 /* select transport */
5752 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
5754 /* if the user wants a non default RTP packet size we add the blocksize
5756 if (src->rtp_blocksize > 0) {
5757 hval = g_strdup_printf ("%d", src->rtp_blocksize);
5758 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
5762 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
5765 /* handle the code ourselves */
5766 if ((res = gst_rtspsrc_send (src, conn, &request, &response, &code) < 0))
5770 case GST_RTSP_STS_OK:
5772 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
5773 gst_rtsp_message_unset (&request);
5774 gst_rtsp_message_unset (&response);
5775 /* cleanup of leftover transport */
5776 gst_rtspsrc_stream_free_udp (stream);
5777 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
5778 * we might be in this case */
5779 if (stream->container && rtpport && rtcpport && !retry) {
5780 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
5785 /* this transport did not go down well, but we may have others to try
5786 * that we did not send yet, try those and only give up then
5787 * but not without checking for lost cause/extension so we can
5788 * post a nicer/more useful error message later */
5789 if (!unsupported_real)
5790 unsupported_real = stream->is_real;
5791 /* select next available protocol, give up on this stream if none */
5793 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5795 if (!protocol_masks[mask] || unsupported_real)
5800 /* cleanup of leftover transport and move to the next stream */
5801 gst_rtspsrc_stream_free_udp (stream);
5802 goto response_error;
5805 /* parse response transport */
5807 gchar *resptrans = NULL;
5808 GstRTSPTransport transport = { 0 };
5810 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
5813 gst_rtspsrc_stream_free_udp (stream);
5817 /* parse transport, go to next stream on parse error */
5818 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
5819 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
5823 /* update allowed transports for other streams. once the transport of
5824 * one stream has been determined, we make sure that all other streams
5825 * are configured in the same way */
5826 switch (transport.lower_transport) {
5827 case GST_RTSP_LOWER_TRANS_TCP:
5828 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
5829 protocols = GST_RTSP_LOWER_TRANS_TCP;
5830 src->interleaved = TRUE;
5831 /* update free channels */
5833 MAX (transport.interleaved.min, src->free_channel);
5835 MAX (transport.interleaved.max, src->free_channel);
5836 src->free_channel++;
5838 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
5839 /* only allow multicast for other streams */
5840 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
5841 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
5842 /* if the server selected our ports, increment our counters so that
5843 * we select a new port later */
5844 if (src->next_port_num == transport.port.min &&
5845 src->next_port_num + 1 == transport.port.max) {
5846 src->next_port_num += 2;
5849 case GST_RTSP_LOWER_TRANS_UDP:
5850 /* only allow unicast for other streams */
5851 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
5852 protocols = GST_RTSP_LOWER_TRANS_UDP;
5855 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
5856 transport.lower_transport);
5860 if (!stream->container || (!src->interleaved && !retry)) {
5861 /* now configure the stream with the selected transport */
5862 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
5863 GST_DEBUG_OBJECT (src,
5864 "could not configure stream %p transport, skipping stream",
5867 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
5868 /* retain the first allocated UDP port pair */
5869 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
5870 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
5873 /* we need to activate at least one streams when we detect activity */
5874 src->need_activate = TRUE;
5876 /* clean up our transport struct */
5877 gst_rtsp_transport_init (&transport);
5878 /* clean up used RTSP messages */
5879 gst_rtsp_message_unset (&request);
5880 gst_rtsp_message_unset (&response);
5884 /* store the transport protocol that was configured */
5885 src->cur_protocols = protocols;
5887 gst_rtsp_ext_list_stream_select (src->extensions, url);
5889 /* if there is nothing to activate, error out */
5890 if (!src->need_activate)
5891 goto nothing_to_activate;
5898 /* no transport possible, post an error and stop */
5899 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5900 ("Could not connect to server, no protocols left"));
5901 return GST_RTSP_ERROR;
5905 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5906 ("SDP contains no streams"));
5907 return GST_RTSP_ERROR;
5909 create_request_failed:
5911 gchar *str = gst_rtsp_strresult (res);
5913 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5914 ("Could not create request. (%s)", str));
5918 setup_transport_failed:
5920 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5921 ("Could not setup transport."));
5922 res = GST_RTSP_ERROR;
5927 const gchar *str = gst_rtsp_status_as_text (code);
5929 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5930 ("Error (%d): %s", code, GST_STR_NULL (str)));
5931 res = GST_RTSP_ERROR;
5936 gchar *str = gst_rtsp_strresult (res);
5938 if (res != GST_RTSP_EINTR) {
5939 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5940 ("Could not send message. (%s)", str));
5942 GST_WARNING_OBJECT (src, "send interrupted");
5949 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5950 ("Server did not select transport."));
5951 res = GST_RTSP_ERROR;
5954 nothing_to_activate:
5956 /* none of the available error codes is really right .. */
5957 if (unsupported_real) {
5958 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5959 (_("No supported stream was found. You might need to install a "
5960 "GStreamer RTSP extension plugin for Real media streams.")),
5963 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5964 (_("No supported stream was found. You might need to allow "
5965 "more transport protocols or may otherwise be missing "
5966 "the right GStreamer RTSP extension plugin.")), (NULL));
5968 return GST_RTSP_ERROR;
5972 gst_rtsp_message_unset (&request);
5973 gst_rtsp_message_unset (&response);
5979 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
5980 GstSegment * segment)
5983 GstRTSPTimeRange *therange;
5986 gst_rtsp_range_free (src->range);
5988 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
5989 GST_DEBUG_OBJECT (src, "parsed range %s", range);
5990 src->range = therange;
5992 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
5994 gst_segment_init (segment, GST_FORMAT_TIME);
5998 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
5999 therange->min.type, therange->min.seconds, therange->max.type,
6000 therange->max.seconds);
6002 if (therange->min.type == GST_RTSP_TIME_NOW)
6004 else if (therange->min.type == GST_RTSP_TIME_END)
6007 seconds = therange->min.seconds * GST_SECOND;
6009 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
6010 GST_TIME_ARGS (seconds));
6012 /* we need to start playback without clipping from the position reported by
6014 segment->start = seconds;
6015 segment->position = seconds;
6017 if (therange->max.type == GST_RTSP_TIME_NOW)
6019 else if (therange->max.type == GST_RTSP_TIME_END)
6022 seconds = therange->max.seconds * GST_SECOND;
6024 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
6025 GST_TIME_ARGS (seconds));
6027 /* live (WMS) server might send overflowed large max as its idea of infinity,
6028 * compensate to prevent problems later on */
6029 if (seconds != -1 && seconds < 0) {
6031 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
6034 /* live (WMS) might send min == max, which is not worth recording */
6035 if (segment->duration == -1 && seconds == segment->start)
6038 /* don't change duration with unknown value, we might have a valid value
6039 * there that we want to keep. */
6041 segment->duration = seconds;
6046 /* Parse clock profived by the server with following syntax:
6048 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
6051 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
6053 gboolean res = FALSE;
6055 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
6056 gchar **fields = NULL, **parts = NULL;
6057 gchar *remote_ip, *str;
6059 GstClockTime base_time;
6062 fields = g_strsplit (gstclock, " ", 0);
6064 /* wrapped clock, not very interesting for now */
6065 if (fields[1] == NULL)
6068 /* remote IP address and port */
6069 if ((str = fields[2]) == NULL)
6072 parts = g_strsplit (str, ":", 0);
6074 if ((remote_ip = parts[0]) == NULL)
6077 if ((str = parts[1]) == NULL)
6085 if ((str = fields[3]) == NULL)
6088 base_time = g_ascii_strtoull (str, NULL, 10);
6091 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
6094 if (src->provided_clock)
6095 gst_object_unref (src->provided_clock);
6096 src->provided_clock = netclock;
6098 gst_element_post_message (GST_ELEMENT_CAST (src),
6099 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
6100 src->provided_clock, TRUE));
6104 g_strfreev (fields);
6110 /* must be called with the RTSP state lock */
6111 static GstRTSPResult
6112 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
6118 /* prepare global stream caps properties */
6120 gst_structure_remove_all_fields (src->props);
6122 src->props = gst_structure_new_empty ("RTSPProperties");
6125 gst_sdp_message_dump (sdp);
6127 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
6129 /* let the app inspect and change the SDP */
6130 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
6132 gst_segment_init (&src->segment, GST_FORMAT_TIME);
6134 /* parse range for duration reporting. */
6139 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
6143 /* keep track of the range and configure it in the segment */
6144 if (gst_rtspsrc_parse_range (src, range, &src->segment))
6148 /* parse clock information. This is GStreamer specific, a server can tell the
6149 * client what clock it is using and wrap that in a network clock. The
6150 * advantage of that is that we can slave to it. */
6152 const gchar *gstclock;
6155 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
6156 if (gstclock == NULL)
6159 /* parse the clock and expose it in the provide_clock method */
6160 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
6164 /* try to find a global control attribute. Note that a '*' means that we should
6165 * do aggregate control with the current url (so we don't do anything and
6166 * leave the current connection as is) */
6168 const gchar *control;
6171 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
6172 if (control == NULL)
6175 /* only take fully qualified urls */
6176 if (g_str_has_prefix (control, "rtsp://"))
6180 g_free (src->conninfo.location);
6181 src->conninfo.location = g_strdup (control);
6182 /* make a connection for this, if there was a connection already, nothing
6184 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
6185 GST_ERROR_OBJECT (src, "could not connect");
6188 /* we need to keep the control url separate from the connection url because
6189 * the rules for constructing the media control url need it */
6190 g_free (src->control);
6191 src->control = g_strdup (control);
6194 /* create streams */
6195 n_streams = gst_sdp_message_medias_len (sdp);
6196 for (i = 0; i < n_streams; i++) {
6197 gst_rtspsrc_create_stream (src, sdp, i);
6200 src->state = GST_RTSP_STATE_INIT;
6203 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
6206 /* reset our state */
6207 src->need_range = TRUE;
6210 src->state = GST_RTSP_STATE_READY;
6217 GST_ERROR_OBJECT (src, "setup failed");
6218 gst_rtspsrc_cleanup (src);
6223 static GstRTSPResult
6224 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
6228 GstRTSPMessage request = { 0 };
6229 GstRTSPMessage response = { 0 };
6232 gchar *respcont = NULL;
6235 src->need_redirect = FALSE;
6237 /* can't continue without a valid url */
6238 if (G_UNLIKELY (src->conninfo.url == NULL)) {
6239 res = GST_RTSP_EINVAL;
6242 src->tried_url_auth = FALSE;
6244 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
6245 goto connect_failed;
6247 /* create OPTIONS */
6248 GST_DEBUG_OBJECT (src, "create options...");
6250 gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
6251 src->conninfo.url_str);
6253 goto create_request_failed;
6256 GST_DEBUG_OBJECT (src, "send options...");
6259 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
6262 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6267 if (!gst_rtspsrc_parse_methods (src, &response))
6270 /* create DESCRIBE */
6271 GST_DEBUG_OBJECT (src, "create describe...");
6273 gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
6274 src->conninfo.url_str);
6276 goto create_request_failed;
6278 /* we only accept SDP for now */
6279 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
6283 GST_DEBUG_OBJECT (src, "send describe...");
6286 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
6289 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6293 /* we only perform redirect for the describe, currently */
6294 if (src->need_redirect) {
6295 /* close connection, we don't have to send a TEARDOWN yet, ignore the
6297 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6299 gst_rtsp_message_unset (&request);
6300 gst_rtsp_message_unset (&response);
6306 /* it could be that the DESCRIBE method was not implemented */
6307 if (!src->methods & GST_RTSP_DESCRIBE)
6310 /* check if reply is SDP */
6311 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
6313 /* could not be set but since the request returned OK, we assume it
6314 * was SDP, else check it. */
6316 if (!g_ascii_strcasecmp (respcont, "application/sdp") == 0)
6317 goto wrong_content_type;
6320 /* get message body and parse as SDP */
6321 gst_rtsp_message_get_body (&response, &data, &size);
6322 if (data == NULL || size == 0)
6325 GST_DEBUG_OBJECT (src, "parse SDP...");
6326 gst_sdp_message_new (sdp);
6327 gst_sdp_message_parse_buffer (data, size, *sdp);
6329 /* clean up any messages */
6330 gst_rtsp_message_unset (&request);
6331 gst_rtsp_message_unset (&response);
6338 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
6339 ("No valid RTSP URL was provided"));
6344 gchar *str = gst_rtsp_strresult (res);
6346 if (res != GST_RTSP_EINTR) {
6347 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
6348 ("Failed to connect. (%s)", str));
6350 GST_WARNING_OBJECT (src, "connect interrupted");
6355 create_request_failed:
6357 gchar *str = gst_rtsp_strresult (res);
6359 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6360 ("Could not create request. (%s)", str));
6366 /* Don't post a message - the rtsp_send method will have
6367 * taken care of it because we passed NULL for the response code */
6372 /* error was posted */
6373 res = GST_RTSP_ERROR;
6378 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6379 ("Server does not support SDP, got %s.", respcont));
6380 res = GST_RTSP_ERROR;
6385 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6386 ("Server can not provide an SDP."));
6387 res = GST_RTSP_ERROR;
6392 if (src->conninfo.connection) {
6393 GST_DEBUG_OBJECT (src, "free connection");
6394 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6396 gst_rtsp_message_unset (&request);
6397 gst_rtsp_message_unset (&response);
6402 static GstRTSPResult
6403 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
6408 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
6410 if (src->sdp == NULL) {
6411 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
6415 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
6420 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
6427 GST_WARNING_OBJECT (src, "can't get sdp");
6428 src->open_error = TRUE;
6433 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
6434 src->open_error = TRUE;
6439 static GstRTSPResult
6440 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
6442 GstRTSPMessage request = { 0 };
6443 GstRTSPMessage response = { 0 };
6444 GstRTSPResult res = GST_RTSP_OK;
6446 const gchar *control;
6448 GST_DEBUG_OBJECT (src, "TEARDOWN...");
6450 gst_rtspsrc_set_state (src, GST_STATE_READY);
6452 if (src->state < GST_RTSP_STATE_READY) {
6453 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
6460 /* construct a control url */
6461 control = get_aggregate_control (src);
6463 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
6466 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6467 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6468 const gchar *setup_url;
6469 GstRTSPConnInfo *info;
6471 /* try aggregate control first but do non-aggregate control otherwise */
6473 setup_url = control;
6474 else if ((setup_url = stream->conninfo.location) == NULL)
6477 if (src->conninfo.connection) {
6478 info = &src->conninfo;
6479 } else if (stream->conninfo.connection) {
6480 info = &stream->conninfo;
6484 if (!info->connected)
6489 gst_rtsp_message_init_request (&request, GST_RTSP_TEARDOWN, setup_url);
6491 goto create_request_failed;
6494 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
6497 gst_rtspsrc_send (src, info->connection, &request, &response,
6501 /* FIXME, parse result? */
6502 gst_rtsp_message_unset (&request);
6503 gst_rtsp_message_unset (&response);
6506 /* early exit when we did aggregate control */
6512 /* close connections */
6513 GST_DEBUG_OBJECT (src, "closing connection...");
6514 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6515 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6516 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6517 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
6521 gst_rtspsrc_cleanup (src);
6523 src->state = GST_RTSP_STATE_INVALID;
6526 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
6531 create_request_failed:
6533 gchar *str = gst_rtsp_strresult (res);
6535 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6536 ("Could not create request. (%s)", str));
6542 gchar *str = gst_rtsp_strresult (res);
6544 gst_rtsp_message_unset (&request);
6545 if (res != GST_RTSP_EINTR) {
6546 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6547 ("Could not send message. (%s)", str));
6549 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
6556 GST_DEBUG_OBJECT (src,
6557 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
6562 /* RTP-Info is of the format:
6564 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
6566 * rtptime corresponds to the timestamp for the NPT time given in the header
6567 * seqbase corresponds to the next sequence number we received. This number
6568 * indicates the first seqnum after the seek and should be used to discard
6569 * packets that are from before the seek.
6572 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
6577 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
6579 infos = g_strsplit (rtpinfo, ",", 0);
6580 for (i = 0; infos[i]; i++) {
6582 GstRTSPStream *stream;
6586 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
6588 /* init values, types of seqbase and timebase are bigger than needed so we
6589 * can store -1 as uninitialized values */
6594 /* parse url, find stream for url.
6595 * parse seq and rtptime. The seq number should be configured in the rtp
6596 * depayloader or session manager to detect gaps. Same for the rtptime, it
6597 * should be used to create an initial time newsegment. */
6598 fields = g_strsplit (infos[i], ";", 0);
6599 for (j = 0; fields[j]; j++) {
6600 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
6601 /* remove leading whitespace */
6602 fields[j] = g_strchug (fields[j]);
6603 if (g_str_has_prefix (fields[j], "url=")) {
6604 /* get the url and the stream */
6606 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
6607 } else if (g_str_has_prefix (fields[j], "seq=")) {
6608 seqbase = atoi (fields[j] + 4);
6609 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
6610 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
6613 g_strfreev (fields);
6614 /* now we need to store the values for the caps of the stream */
6615 if (stream != NULL) {
6616 GST_DEBUG_OBJECT (src,
6617 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
6618 stream, seqbase, timebase);
6620 /* we have a stream, configure detected params */
6621 stream->seqbase = seqbase;
6622 stream->timebase = timebase;
6631 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
6636 interval = strtoul (rtcp, NULL, 10);
6637 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
6642 interval *= GST_MSECOND;
6644 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6645 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6647 /* already (optionally) retrieved this when configuring manager */
6648 if (stream->session) {
6649 GObject *rtpsession = stream->session;
6651 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
6653 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
6657 /* now it happens that (Xenon) server sending this may also provide bogus
6658 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
6659 * and just use RTP-Info to sync */
6661 GObjectClass *klass;
6663 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
6664 if (g_object_class_find_property (klass, "rtcp-sync")) {
6665 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
6666 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
6672 gst_rtspsrc_get_float (const gchar * dstr)
6674 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
6676 /* canonicalise floating point string so we can handle float strings
6677 * in the form "24.930" or "24,930" irrespective of the current locale */
6678 g_strlcpy (s, dstr, sizeof (s));
6679 g_strdelimit (s, ",", '.');
6680 return g_ascii_strtod (s, NULL);
6684 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
6686 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
6688 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
6689 g_strlcpy (val_str, "now", sizeof (val_str));
6691 if (segment->position == 0) {
6692 g_strlcpy (val_str, "0", sizeof (val_str));
6694 g_ascii_dtostr (val_str, sizeof (val_str),
6695 ((gdouble) segment->position) / GST_SECOND);
6698 return g_strdup_printf ("npt=%s-", val_str);
6702 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
6706 stream->timebase = -1;
6707 stream->seqbase = -1;
6709 len = stream->ptmap->len;
6710 for (i = 0; i < len; i++) {
6711 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
6714 if (item->caps == NULL)
6717 item->caps = gst_caps_make_writable (item->caps);
6718 s = gst_caps_get_structure (item->caps, 0);
6719 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
6723 static GstRTSPResult
6724 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
6726 GstRTSPResult res = GST_RTSP_OK;
6728 if (src->state < GST_RTSP_STATE_READY) {
6729 res = GST_RTSP_ERROR;
6730 if (src->open_error) {
6731 GST_DEBUG_OBJECT (src, "the stream was in error");
6735 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
6737 if ((res = gst_rtspsrc_open (src, async)) < 0) {
6738 GST_DEBUG_OBJECT (src, "failed to open stream");
6747 static GstRTSPResult
6748 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
6750 GstRTSPMessage request = { 0 };
6751 GstRTSPMessage response = { 0 };
6752 GstRTSPResult res = GST_RTSP_OK;
6756 const gchar *control;
6758 GST_DEBUG_OBJECT (src, "PLAY...");
6760 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6763 if (!(src->methods & GST_RTSP_PLAY))
6766 if (src->state == GST_RTSP_STATE_PLAYING)
6769 if (!src->conninfo.connection || !src->conninfo.connected)
6772 /* send some dummy packets before we activate the receive in the
6774 gst_rtspsrc_send_dummy_packets (src);
6776 /* require new SR packets */
6778 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
6780 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
6782 /* construct a control url */
6783 control = get_aggregate_control (src);
6785 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6786 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6787 const gchar *setup_url;
6788 GstRTSPConnection *conn;
6790 /* try aggregate control first but do non-aggregate control otherwise */
6792 setup_url = control;
6793 else if ((setup_url = stream->conninfo.location) == NULL)
6796 if (src->conninfo.connection) {
6797 conn = src->conninfo.connection;
6798 } else if (stream->conninfo.connection) {
6799 conn = stream->conninfo.connection;
6805 res = gst_rtsp_message_init_request (&request, GST_RTSP_PLAY, setup_url);
6807 goto create_request_failed;
6809 if (src->need_range) {
6810 hval = gen_range_header (src, segment);
6812 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
6814 /* store the newsegment event so it can be sent from the streaming thread. */
6815 if (src->start_segment)
6816 gst_event_unref (src->start_segment);
6817 src->start_segment = gst_event_new_segment (&src->segment);
6820 if (segment->rate != 1.0) {
6821 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
6823 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
6825 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
6827 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
6831 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
6833 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6836 /* seek may have silently failed as it is not supported */
6837 if (!(src->methods & GST_RTSP_PLAY)) {
6838 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
6839 /* obviously it is supported as we made it here */
6840 src->methods |= GST_RTSP_PLAY;
6841 src->seekable = FALSE;
6842 /* but there is nothing to parse in the response,
6843 * so convey we have no idea and not to expect anything particular */
6844 clear_rtp_base (src, stream);
6848 /* need to do for all streams */
6849 for (run = src->streams; run; run = g_list_next (run))
6850 clear_rtp_base (src, (GstRTSPStream *) run->data);
6852 /* NOTE the above also disables npt based eos detection */
6853 /* and below forces position to 0,
6854 * which is visible feedback we lost the plot */
6855 segment->start = segment->position = src->last_pos;
6858 gst_rtsp_message_unset (&request);
6860 /* parse RTP npt field. This is the current position in the stream (Normal
6861 * Play Time) and should be put in the NEWSEGMENT position field. */
6862 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
6864 gst_rtspsrc_parse_range (src, hval, segment);
6866 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
6867 segment->rate = 1.0;
6869 /* parse Speed header. This is the intended playback rate of the stream
6870 * and should be put in the NEWSEGMENT rate field. */
6871 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
6872 0) == GST_RTSP_OK) {
6873 segment->rate = gst_rtspsrc_get_float (hval);
6874 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
6875 &hval, 0) == GST_RTSP_OK) {
6876 segment->rate = gst_rtspsrc_get_float (hval);
6879 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
6880 * for the RTP packets. If this is not present, we assume all starts from 0...
6881 * This is info for the RTP session manager that we pass to it in caps. */
6883 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
6884 &hval, hval_idx++) == GST_RTSP_OK)
6885 gst_rtspsrc_parse_rtpinfo (src, hval);
6887 /* some servers indicate RTCP parameters in PLAY response,
6888 * rather than properly in SDP */
6889 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
6890 &hval, 0) == GST_RTSP_OK)
6891 gst_rtspsrc_handle_rtcp_interval (src, hval);
6893 gst_rtsp_message_unset (&response);
6895 /* early exit when we did aggregate control */
6899 /* configure the caps of the streams after we parsed all headers. Only reset
6900 * the manager object when we set a new Range header (we did a seek) */
6901 gst_rtspsrc_configure_caps (src, segment, src->need_range);
6903 /* set again when needed */
6904 src->need_range = FALSE;
6906 src->running = TRUE;
6907 src->base_time = -1;
6908 src->state = GST_RTSP_STATE_PLAYING;
6911 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
6912 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6913 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6914 stream->discont = TRUE;
6919 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
6926 GST_DEBUG_OBJECT (src, "failed to open stream");
6931 GST_DEBUG_OBJECT (src, "PLAY is not supported");
6936 GST_DEBUG_OBJECT (src, "we were already PLAYING");
6939 create_request_failed:
6941 gchar *str = gst_rtsp_strresult (res);
6943 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6944 ("Could not create request. (%s)", str));
6950 gchar *str = gst_rtsp_strresult (res);
6952 gst_rtsp_message_unset (&request);
6953 if (res != GST_RTSP_EINTR) {
6954 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6955 ("Could not send message. (%s)", str));
6957 GST_WARNING_OBJECT (src, "PLAY interrupted");
6964 static GstRTSPResult
6965 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
6967 GstRTSPResult res = GST_RTSP_OK;
6968 GstRTSPMessage request = { 0 };
6969 GstRTSPMessage response = { 0 };
6971 const gchar *control;
6973 GST_DEBUG_OBJECT (src, "PAUSE...");
6975 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6978 if (!(src->methods & GST_RTSP_PAUSE))
6981 if (src->state == GST_RTSP_STATE_READY)
6984 if (!src->conninfo.connection || !src->conninfo.connected)
6987 /* construct a control url */
6988 control = get_aggregate_control (src);
6990 /* loop over the streams. We might exit the loop early when we could do an
6991 * aggregate control */
6992 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6993 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6994 GstRTSPConnection *conn;
6995 const gchar *setup_url;
6997 /* try aggregate control first but do non-aggregate control otherwise */
6999 setup_url = control;
7000 else if ((setup_url = stream->conninfo.location) == NULL)
7003 if (src->conninfo.connection) {
7004 conn = src->conninfo.connection;
7005 } else if (stream->conninfo.connection) {
7006 conn = stream->conninfo.connection;
7012 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
7013 ("Sending PAUSE request"));
7016 gst_rtsp_message_init_request (&request, GST_RTSP_PAUSE,
7018 goto create_request_failed;
7020 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7023 gst_rtsp_message_unset (&request);
7024 gst_rtsp_message_unset (&response);
7026 /* exit early when we did agregate control */
7031 /* change element states now */
7032 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
7035 src->state = GST_RTSP_STATE_READY;
7039 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
7046 GST_DEBUG_OBJECT (src, "failed to open stream");
7051 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
7056 GST_DEBUG_OBJECT (src, "we were already PAUSED");
7059 create_request_failed:
7061 gchar *str = gst_rtsp_strresult (res);
7063 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7064 ("Could not create request. (%s)", str));
7070 gchar *str = gst_rtsp_strresult (res);
7072 gst_rtsp_message_unset (&request);
7073 if (res != GST_RTSP_EINTR) {
7074 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7075 ("Could not send message. (%s)", str));
7077 GST_WARNING_OBJECT (src, "PAUSE interrupted");
7085 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
7087 GstRTSPSrc *rtspsrc;
7089 rtspsrc = GST_RTSPSRC (bin);
7091 switch (GST_MESSAGE_TYPE (message)) {
7092 case GST_MESSAGE_EOS:
7093 gst_message_unref (message);
7095 case GST_MESSAGE_ELEMENT:
7097 const GstStructure *s = gst_message_get_structure (message);
7099 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
7100 gboolean ignore_timeout;
7102 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
7104 GST_OBJECT_LOCK (rtspsrc);
7105 ignore_timeout = rtspsrc->ignore_timeout;
7106 rtspsrc->ignore_timeout = TRUE;
7107 GST_OBJECT_UNLOCK (rtspsrc);
7109 /* we only act on the first udp timeout message, others are irrelevant
7110 * and can be ignored. */
7111 if (!ignore_timeout)
7112 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
7114 gst_message_unref (message);
7117 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7120 case GST_MESSAGE_ERROR:
7123 GstRTSPStream *stream;
7126 udpsrc = GST_MESSAGE_SRC (message);
7128 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
7129 GST_ELEMENT_NAME (udpsrc));
7131 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
7135 /* we ignore the RTCP udpsrc */
7136 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
7139 /* if we get error messages from the udp sources, that's not a problem as
7140 * long as not all of them error out. We also don't really know what the
7141 * problem is, the message does not give enough detail... */
7142 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
7143 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
7144 if (ret != GST_FLOW_OK)
7148 gst_message_unref (message);
7152 /* fatal but not our message, forward */
7153 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7158 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7164 /* the thread where everything happens */
7166 gst_rtspsrc_thread (GstRTSPSrc * src)
7170 GST_OBJECT_LOCK (src);
7171 cmd = src->pending_cmd;
7172 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
7173 || cmd == CMD_LOOP || cmd == CMD_OPEN)
7174 src->pending_cmd = CMD_LOOP;
7176 src->pending_cmd = CMD_WAIT;
7177 GST_DEBUG_OBJECT (src, "got command %d", cmd);
7179 /* we got the message command, so ensure communication is possible again */
7180 gst_rtspsrc_connection_flush (src, FALSE);
7182 src->busy_cmd = cmd;
7183 GST_OBJECT_UNLOCK (src);
7187 gst_rtspsrc_open (src, TRUE);
7190 gst_rtspsrc_play (src, &src->segment, TRUE);
7193 gst_rtspsrc_pause (src, TRUE);
7196 gst_rtspsrc_close (src, TRUE, FALSE);
7199 gst_rtspsrc_loop (src);
7202 gst_rtspsrc_reconnect (src, FALSE);
7208 GST_OBJECT_LOCK (src);
7209 /* and go back to sleep */
7210 if (src->pending_cmd == CMD_WAIT) {
7212 gst_task_pause (src->task);
7215 src->busy_cmd = CMD_WAIT;
7216 GST_OBJECT_UNLOCK (src);
7220 gst_rtspsrc_start (GstRTSPSrc * src)
7222 GST_DEBUG_OBJECT (src, "starting");
7224 GST_OBJECT_LOCK (src);
7226 src->pending_cmd = CMD_WAIT;
7228 if (src->task == NULL) {
7229 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
7230 if (src->task == NULL)
7233 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
7235 GST_OBJECT_UNLOCK (src);
7242 GST_OBJECT_UNLOCK (src);
7243 GST_ERROR_OBJECT (src, "failed to create task");
7249 gst_rtspsrc_stop (GstRTSPSrc * src)
7253 GST_DEBUG_OBJECT (src, "stopping");
7255 /* also cancels pending task */
7256 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
7258 GST_OBJECT_LOCK (src);
7259 if ((task = src->task)) {
7261 GST_OBJECT_UNLOCK (src);
7263 gst_task_stop (task);
7265 /* make sure it is not running */
7266 GST_RTSP_STREAM_LOCK (src);
7267 GST_RTSP_STREAM_UNLOCK (src);
7269 /* now wait for the task to finish */
7270 gst_task_join (task);
7272 /* and free the task */
7273 gst_object_unref (GST_OBJECT (task));
7275 GST_OBJECT_LOCK (src);
7277 GST_OBJECT_UNLOCK (src);
7279 /* ensure synchronously all is closed and clean */
7280 gst_rtspsrc_close (src, FALSE, TRUE);
7285 static GstStateChangeReturn
7286 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
7288 GstRTSPSrc *rtspsrc;
7289 GstStateChangeReturn ret;
7291 rtspsrc = GST_RTSPSRC (element);
7293 switch (transition) {
7294 case GST_STATE_CHANGE_NULL_TO_READY:
7295 if (!gst_rtspsrc_start (rtspsrc))
7298 case GST_STATE_CHANGE_READY_TO_PAUSED:
7299 /* init some state */
7300 rtspsrc->cur_protocols = rtspsrc->protocols;
7301 /* first attempt, don't ignore timeouts */
7302 rtspsrc->ignore_timeout = FALSE;
7303 rtspsrc->open_error = FALSE;
7304 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
7306 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7307 set_manager_buffer_mode (rtspsrc);
7309 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7310 /* unblock the tcp tasks and make the loop waiting */
7311 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
7312 /* make sure it is waiting before we send PAUSE or PLAY below */
7313 GST_RTSP_STREAM_LOCK (rtspsrc);
7314 GST_RTSP_STREAM_UNLOCK (rtspsrc);
7317 case GST_STATE_CHANGE_PAUSED_TO_READY:
7323 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
7324 if (ret == GST_STATE_CHANGE_FAILURE)
7327 switch (transition) {
7328 case GST_STATE_CHANGE_NULL_TO_READY:
7329 ret = GST_STATE_CHANGE_SUCCESS;
7331 case GST_STATE_CHANGE_READY_TO_PAUSED:
7332 ret = GST_STATE_CHANGE_NO_PREROLL;
7334 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7335 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
7336 ret = GST_STATE_CHANGE_SUCCESS;
7338 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7339 /* send pause request and keep the idle task around */
7340 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
7341 ret = GST_STATE_CHANGE_NO_PREROLL;
7343 case GST_STATE_CHANGE_PAUSED_TO_READY:
7344 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_PAUSE);
7345 ret = GST_STATE_CHANGE_SUCCESS;
7347 case GST_STATE_CHANGE_READY_TO_NULL:
7348 gst_rtspsrc_stop (rtspsrc);
7349 ret = GST_STATE_CHANGE_SUCCESS;
7360 GST_DEBUG_OBJECT (rtspsrc, "start failed");
7361 return GST_STATE_CHANGE_FAILURE;
7366 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
7369 GstRTSPSrc *rtspsrc;
7371 rtspsrc = GST_RTSPSRC (element);
7373 if (GST_EVENT_IS_DOWNSTREAM (event)) {
7374 res = gst_rtspsrc_push_event (rtspsrc, event);
7376 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
7383 /*** GSTURIHANDLER INTERFACE *************************************************/
7386 gst_rtspsrc_uri_get_type (GType type)
7391 static const gchar *const *
7392 gst_rtspsrc_uri_get_protocols (GType type)
7394 static const gchar *protocols[] =
7395 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
7396 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
7403 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
7405 GstRTSPSrc *src = GST_RTSPSRC (handler);
7407 /* FIXME: make thread-safe */
7408 return g_strdup (src->conninfo.location);
7412 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
7417 GstRTSPUrl *newurl = NULL;
7418 GstSDPMessage *sdp = NULL;
7420 src = GST_RTSPSRC (handler);
7422 /* same URI, we're fine */
7423 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
7426 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
7427 if ((res = gst_sdp_message_new (&sdp) < 0))
7430 GST_DEBUG_OBJECT (src, "parsing SDP message");
7431 if ((res = gst_sdp_message_parse_uri (uri, sdp) < 0))
7435 GST_DEBUG_OBJECT (src, "parsing URI");
7436 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
7440 /* if worked, free previous and store new url object along with the original
7442 GST_DEBUG_OBJECT (src, "configuring URI");
7443 g_free (src->conninfo.location);
7444 src->conninfo.location = g_strdup (uri);
7445 gst_rtsp_url_free (src->conninfo.url);
7446 src->conninfo.url = newurl;
7447 g_free (src->conninfo.url_str);
7449 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
7451 src->conninfo.url_str = NULL;
7454 gst_sdp_message_free (src->sdp);
7456 src->from_sdp = sdp != NULL;
7458 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
7459 GST_DEBUG_OBJECT (src, "request uri is: %s",
7460 GST_STR_NULL (src->conninfo.url_str));
7467 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
7472 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", res);
7473 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7474 "Could not create SDP");
7479 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", res,
7480 GST_STR_NULL (uri));
7481 gst_sdp_message_free (sdp);
7482 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7488 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
7489 GST_STR_NULL (uri), res);
7490 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7491 "Invalid RTSP URI");
7497 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
7499 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
7501 iface->get_type = gst_rtspsrc_uri_get_type;
7502 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
7503 iface->get_uri = gst_rtspsrc_uri_get_uri;
7504 iface->set_uri = gst_rtspsrc_uri_set_uri;