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;
1401 clear_ptmap_item (PtMapItem * item)
1404 gst_caps_unref (item->caps);
1407 static GstRTSPStream *
1408 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
1410 GstRTSPStream *stream;
1411 const gchar *control_url;
1412 const GstSDPMedia *media;
1414 /* get media, should not return NULL */
1415 media = gst_sdp_message_get_media (sdp, idx);
1419 stream = g_new0 (GstRTSPStream, 1);
1420 stream->parent = src;
1421 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1423 stream->last_ret = GST_FLOW_NOT_LINKED;
1424 stream->added = FALSE;
1425 stream->setup = FALSE;
1426 stream->skipped = FALSE;
1428 stream->eos = FALSE;
1429 stream->discont = TRUE;
1430 stream->seqbase = -1;
1431 stream->timebase = -1;
1432 stream->profile = GST_RTSP_PROFILE_AVP;
1433 stream->ptmap = g_array_new (FALSE, FALSE, sizeof (PtMapItem));
1434 g_array_set_clear_func (stream->ptmap, (GDestroyNotify) clear_ptmap_item);
1436 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1437 * session manager to scale RTCP. */
1438 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1440 /* collect connection info */
1441 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1443 /* make the payload type map */
1444 gst_rtspsrc_collect_payloads (src, sdp, media, stream);
1446 /* collect port number */
1447 stream->port = gst_sdp_media_get_port (media);
1449 /* get control url to construct the setup url. The setup url is used to
1450 * configure the transport of the stream and is used to identity the stream in
1451 * the RTP-Info header field returned from PLAY. */
1452 control_url = gst_sdp_media_get_attribute_val (media, "control");
1453 if (control_url == NULL)
1454 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1456 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1457 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1458 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1459 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1461 if (control_url != NULL) {
1462 stream->control_url = g_strdup (control_url);
1463 /* Build a fully qualified url using the content_base if any or by prefixing
1464 * the original request.
1465 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1466 * likely build a URL that the server will fail to understand, this is ok,
1467 * we will fail then. */
1468 if (g_str_has_prefix (control_url, "rtsp://"))
1469 stream->conninfo.location = g_strdup (control_url);
1474 if (g_strcmp0 (control_url, "*") == 0)
1477 base = get_aggregate_control (src);
1479 /* check if the base ends or control starts with / */
1480 has_slash = g_str_has_prefix (control_url, "/");
1481 has_slash = has_slash || g_str_has_suffix (base, "/");
1483 /* concatenate the two strings, insert / when not present */
1484 stream->conninfo.location =
1485 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1488 GST_DEBUG_OBJECT (src, " setup: %s",
1489 GST_STR_NULL (stream->conninfo.location));
1491 /* we keep track of all streams */
1492 src->streams = g_list_append (src->streams, stream);
1500 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1504 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1506 g_array_free (stream->ptmap, TRUE);
1508 g_free (stream->destination);
1509 g_free (stream->control_url);
1510 g_free (stream->conninfo.location);
1512 for (i = 0; i < 2; i++) {
1513 if (stream->udpsrc[i]) {
1514 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1515 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1516 gst_object_unref (stream->udpsrc[i]);
1517 stream->udpsrc[i] = NULL;
1519 if (stream->channelpad[i]) {
1520 gst_object_unref (stream->channelpad[i]);
1521 stream->channelpad[i] = NULL;
1523 if (stream->udpsink[i]) {
1524 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1525 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1526 gst_object_unref (stream->udpsink[i]);
1527 stream->udpsink[i] = NULL;
1530 if (stream->fakesrc) {
1531 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1532 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1533 gst_object_unref (stream->fakesrc);
1534 stream->fakesrc = NULL;
1536 if (stream->srcpad) {
1537 gst_pad_set_active (stream->srcpad, FALSE);
1538 if (stream->added) {
1539 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1540 stream->added = FALSE;
1542 stream->srcpad = NULL;
1544 if (stream->rtcppad) {
1545 gst_object_unref (stream->rtcppad);
1546 stream->rtcppad = NULL;
1548 if (stream->session) {
1549 g_object_unref (stream->session);
1550 stream->session = NULL;
1556 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1560 GST_DEBUG_OBJECT (src, "cleanup");
1562 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1563 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1565 gst_rtspsrc_stream_free (src, stream);
1567 g_list_free (src->streams);
1568 src->streams = NULL;
1570 if (src->manager_sig_id) {
1571 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1572 src->manager_sig_id = 0;
1574 gst_element_set_state (src->manager, GST_STATE_NULL);
1575 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1576 src->manager = NULL;
1579 gst_structure_free (src->props);
1582 g_free (src->content_base);
1583 src->content_base = NULL;
1585 g_free (src->control);
1586 src->control = NULL;
1589 gst_rtsp_range_free (src->range);
1592 /* don't clear the SDP when it was used in the url */
1593 if (src->sdp && !src->from_sdp) {
1594 gst_sdp_message_free (src->sdp);
1597 if (src->start_segment) {
1598 gst_event_unref (src->start_segment);
1599 src->start_segment = NULL;
1601 if (src->provided_clock) {
1602 gst_object_unref (src->provided_clock);
1603 src->provided_clock = NULL;
1607 #define PARSE_INT(p, del, res) \
1610 p = strstr (p, del); \
1620 #define PARSE_STRING(p, del, res) \
1623 p = strstr (p, del); \
1635 #define SKIP_SPACES(p) \
1636 while (*p && g_ascii_isspace (*p)) \
1641 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1644 gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
1645 gint * rate, gchar ** params)
1649 p = (gchar *) rtpmap;
1651 PARSE_INT (p, " ", *payload);
1659 PARSE_STRING (p, "/", *name);
1660 if (*name == NULL) {
1661 GST_DEBUG ("no rate, name %s", p);
1662 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
1663 * streams seem to omit the rate. */
1670 p = strstr (p, "/");
1688 * Mapping SDP attributes to caps
1690 * prepend 'a-' to IANA registered sdp attributes names
1691 * (ie: not prefixed with 'x-') in order to avoid
1692 * collision with gstreamer standard caps properties names
1695 gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
1697 if (attributes->len > 0) {
1701 s = gst_caps_get_structure (caps, 0);
1703 for (i = 0; i < attributes->len; i++) {
1704 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
1705 gchar *tofree, *key;
1709 /* skip some of the attribute we already handle */
1710 if (!strcmp (key, "fmtp"))
1712 if (!strcmp (key, "rtpmap"))
1714 if (!strcmp (key, "control"))
1716 if (!strcmp (key, "range"))
1719 /* string must be valid UTF8 */
1720 if (!g_utf8_validate (attr->value, -1, NULL))
1723 if (!g_str_has_prefix (key, "x-"))
1724 tofree = key = g_strdup_printf ("a-%s", key);
1728 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
1729 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
1735 static const gchar *
1736 rtsp_get_attribute_for_pt (const GstSDPMedia * media, const gchar * name,
1745 if ((attr = gst_sdp_media_get_attribute_val_n (media, name, i)) == NULL)
1748 if (sscanf (attr, "%d ", &val) != 1)
1758 * Mapping of caps to and from SDP fields:
1760 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1761 * a=fmtp:<payload> <param>[=<value>];...
1764 gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
1767 const gchar *rtpmap;
1771 gchar *params = NULL;
1777 /* get and parse rtpmap */
1778 rtpmap = rtsp_get_attribute_for_pt (media, "rtpmap", pt);
1781 ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
1783 g_warning ("error parsing rtpmap, ignoring");
1787 /* dynamic payloads need rtpmap or we fail */
1788 if (rtpmap == NULL && pt >= 96)
1791 /* check if we have a rate, if not, we need to look up the rate from the
1792 * default rates based on the payload types. */
1794 const GstRTPPayloadInfo *info;
1796 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
1797 /* dynamic types, use media and encoding_name */
1798 tmp = g_ascii_strdown (media->media, -1);
1799 info = gst_rtp_payload_info_for_name (tmp, name);
1802 /* static types, use payload type */
1803 info = gst_rtp_payload_info_for_pt (pt);
1807 if ((rate = info->clock_rate) == 0)
1810 /* we fail if we cannot find one */
1815 tmp = g_ascii_strdown (media->media, -1);
1816 caps = gst_caps_new_simple ("application/x-unknown",
1817 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
1819 s = gst_caps_get_structure (caps, 0);
1821 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
1823 /* encoding name must be upper case */
1825 tmp = g_ascii_strup (name, -1);
1826 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
1830 /* params must be lower case */
1831 if (params != NULL) {
1832 tmp = g_ascii_strdown (params, -1);
1833 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
1837 /* parse optional fmtp: field */
1838 if ((fmtp = rtsp_get_attribute_for_pt (media, "fmtp", pt))) {
1844 /* p is now of the format <payload> <param>[=<value>];... */
1845 PARSE_INT (p, " ", payload);
1846 if (payload != -1 && payload == pt) {
1850 /* <param>[=<value>] are separated with ';' */
1851 pairs = g_strsplit (p, ";", 0);
1852 for (i = 0; pairs[i]; i++) {
1854 const gchar *val, *key;
1856 /* the key may not have a '=', the value can have other '='s */
1857 valpos = strstr (pairs[i], "=");
1859 /* we have a '=' and thus a value, remove the '=' with \0 */
1861 /* value is everything between '=' and ';'. We split the pairs at ;
1862 * boundaries so we can take the remainder of the value. Some servers
1863 * put spaces around the value which we strip off here. Alternatively
1864 * we could strip those spaces in the depayloaders should these spaces
1865 * actually carry any meaning in the future. */
1866 val = g_strstrip (valpos + 1);
1868 /* simple <param>;.. is translated into <param>=1;... */
1871 /* strip the key of spaces, convert key to lowercase but not the value. */
1872 key = g_strstrip (pairs[i]);
1873 if (strlen (key) > 1) {
1874 tmp = g_ascii_strdown (key, -1);
1875 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
1887 g_warning ("rtpmap type not given for dynamic payload %d", pt);
1892 g_warning ("rate unknown for payload type %d", pt);
1898 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
1899 gint * rtpport, gint * rtcpport)
1902 GstStateChangeReturn ret;
1903 GstElement *udpsrc0, *udpsrc1;
1904 gint tmp_rtp, tmp_rtcp;
1908 src = stream->parent;
1914 /* Start at next port */
1915 tmp_rtp = src->next_port_num;
1917 if (stream->is_ipv6)
1918 host = "udp://[::0]";
1920 host = "udp://0.0.0.0";
1922 /* try to allocate 2 UDP ports, the RTP port should be an even
1923 * number and the RTCP port should be the next (uneven) port */
1926 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
1927 tmp_rtp >= src->client_port_range.max)
1930 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1931 if (udpsrc0 == NULL)
1932 goto no_udp_protocol;
1933 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
1935 if (src->udp_buffer_size != 0)
1936 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
1939 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
1940 if (ret == GST_STATE_CHANGE_FAILURE) {
1942 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
1945 if (++count > src->retry)
1948 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1949 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1950 gst_object_unref (udpsrc0);
1953 GST_DEBUG_OBJECT (src, "retry %d", count);
1956 goto no_udp_protocol;
1959 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
1960 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
1962 /* check if port is even */
1963 if ((tmp_rtp & 0x01) != 0) {
1964 /* port not even, close and allocate another */
1965 if (++count > src->retry)
1968 GST_DEBUG_OBJECT (src, "RTP port not even");
1970 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1971 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1972 gst_object_unref (udpsrc0);
1975 GST_DEBUG_OBJECT (src, "retry %d", count);
1980 /* allocate port+1 for RTCP now */
1981 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1982 if (udpsrc1 == NULL)
1983 goto no_udp_rtcp_protocol;
1986 tmp_rtcp = tmp_rtp + 1;
1987 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
1990 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
1992 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
1993 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
1994 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
1995 if (ret == GST_STATE_CHANGE_FAILURE) {
1996 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
1998 if (++count > src->retry)
2001 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2002 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2003 gst_object_unref (udpsrc0);
2006 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
2007 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2008 gst_object_unref (udpsrc1);
2012 GST_DEBUG_OBJECT (src, "retry %d", count);
2016 /* all fine, do port check */
2017 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
2018 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
2020 /* this should not happen... */
2021 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
2024 /* we keep these elements, we configure all in configure_transport when the
2025 * server told us to really use the UDP ports. */
2026 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
2027 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
2028 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
2029 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
2031 /* keep track of next available port number when we have a range
2033 if (src->next_port_num != 0)
2034 src->next_port_num = tmp_rtcp + 1;
2041 GST_DEBUG_OBJECT (src, "could not get UDP source");
2046 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
2050 no_udp_rtcp_protocol:
2052 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
2057 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
2058 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
2064 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2065 gst_object_unref (udpsrc0);
2068 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2069 gst_object_unref (udpsrc1);
2076 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
2081 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
2083 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2084 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2087 for (i = 0; i < 2; i++) {
2088 if (stream->udpsrc[i])
2089 gst_element_set_state (stream->udpsrc[i], state);
2095 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
2102 event = gst_event_new_flush_start ();
2103 GST_DEBUG_OBJECT (src, "start flush");
2105 state = GST_STATE_PAUSED;
2107 event = gst_event_new_flush_stop (FALSE);
2108 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2111 state = GST_STATE_PLAYING;
2113 state = GST_STATE_PAUSED;
2115 gst_rtspsrc_push_event (src, event);
2116 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2117 gst_rtspsrc_set_state (src, state);
2120 static GstRTSPResult
2121 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
2122 GstRTSPMessage * message, GTimeVal * timeout)
2127 ret = gst_rtsp_connection_send (conn, message, timeout);
2129 ret = GST_RTSP_ERROR;
2134 static GstRTSPResult
2135 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
2136 GstRTSPMessage * message, GTimeVal * timeout)
2141 ret = gst_rtsp_connection_receive (conn, message, timeout);
2143 ret = GST_RTSP_ERROR;
2149 gst_rtspsrc_get_position (GstRTSPSrc * src)
2154 query = gst_query_new_position (GST_FORMAT_TIME);
2155 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2156 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2157 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2161 if (stream->srcpad) {
2162 if (gst_pad_query (stream->srcpad, query)) {
2163 gst_query_parse_position (query, &fmt, &pos);
2164 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2165 GST_TIME_ARGS (pos));
2166 src->last_pos = pos;
2176 gst_rtspsrc_do_seek (GstRTSPSrc * src, GstSegment * segment)
2178 src->state = GST_RTSP_STATE_SEEKING;
2179 /* PLAY will add the range header now. */
2180 src->need_range = TRUE;
2186 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2191 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2193 gboolean flush, skip;
2196 GstSegment seeksegment = { 0, };
2200 GST_DEBUG_OBJECT (src, "doing seek with event");
2202 gst_event_parse_seek (event, &rate, &format, &flags,
2203 &cur_type, &cur, &stop_type, &stop);
2205 /* no negative rates yet */
2209 /* we need TIME format */
2210 if (format != src->segment.format)
2213 GST_DEBUG_OBJECT (src, "doing seek without event");
2215 cur_type = GST_SEEK_TYPE_SET;
2216 stop_type = GST_SEEK_TYPE_SET;
2219 /* get flush flag */
2220 flush = flags & GST_SEEK_FLAG_FLUSH;
2221 skip = flags & GST_SEEK_FLAG_SKIP;
2223 /* now we need to make sure the streaming thread is stopped. We do this by
2224 * either sending a FLUSH_START event downstream which will cause the
2225 * streaming thread to stop with a WRONG_STATE.
2226 * For a non-flushing seek we simply pause the task, which will happen as soon
2227 * as it completes one iteration (and thus might block when the sink is
2228 * blocking in preroll). */
2230 GST_DEBUG_OBJECT (src, "starting flush");
2231 gst_rtspsrc_flush (src, TRUE, FALSE);
2234 gst_task_pause (src->task);
2238 /* we should now be able to grab the streaming thread because we stopped it
2239 * with the above flush/pause code */
2240 GST_RTSP_STREAM_LOCK (src);
2242 GST_DEBUG_OBJECT (src, "stopped streaming");
2244 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2245 gst_rtspsrc_connection_flush (src, FALSE);
2247 /* copy segment, we need this because we still need the old
2248 * segment when we close the current segment. */
2249 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2251 /* configure the seek parameters in the seeksegment. We will then have the
2252 * right values in the segment to perform the seek */
2254 GST_DEBUG_OBJECT (src, "configuring seek");
2255 gst_segment_do_seek (&seeksegment, rate, format, flags,
2256 cur_type, cur, stop_type, stop, &update);
2259 /* figure out the last position we need to play. If it's configured (stop !=
2260 * -1), use that, else we play until the total duration of the file */
2261 if ((stop = seeksegment.stop) == -1)
2262 stop = seeksegment.duration;
2264 playing = (src->state == GST_RTSP_STATE_PLAYING);
2266 /* if we were playing, pause first */
2268 /* obtain current position in case seek fails */
2269 gst_rtspsrc_get_position (src);
2270 gst_rtspsrc_pause (src, FALSE);
2274 gst_rtspsrc_do_seek (src, &seeksegment);
2276 /* and continue playing */
2278 gst_rtspsrc_play (src, &seeksegment, FALSE);
2280 /* prepare for streaming again */
2282 /* if we started flush, we stop now */
2283 GST_DEBUG_OBJECT (src, "stopping flush");
2284 gst_rtspsrc_flush (src, FALSE, playing);
2287 /* now we did the seek and can activate the new segment values */
2288 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2290 /* if we're doing a segment seek, post a SEGMENT_START message */
2291 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2292 gst_element_post_message (GST_ELEMENT_CAST (src),
2293 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2294 src->segment.format, src->segment.position));
2297 /* now create the newsegment */
2298 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2299 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2302 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2303 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2304 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2305 stream->discont = TRUE;
2308 GST_RTSP_STREAM_UNLOCK (src);
2315 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2320 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2326 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2330 gboolean res = TRUE;
2333 src = GST_RTSPSRC_CAST (parent);
2335 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2336 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2338 switch (GST_EVENT_TYPE (event)) {
2339 case GST_EVENT_SEEK:
2340 res = gst_rtspsrc_perform_seek (src, event);
2344 case GST_EVENT_NAVIGATION:
2345 case GST_EVENT_LATENCY:
2353 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2354 res = gst_pad_send_event (target, event);
2355 gst_object_unref (target);
2357 gst_event_unref (event);
2360 gst_event_unref (event);
2366 /* this is the final event function we receive on the internal source pad when
2367 * we deal with TCP connections */
2369 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2374 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2376 switch (GST_EVENT_TYPE (event)) {
2377 case GST_EVENT_SEEK:
2379 case GST_EVENT_NAVIGATION:
2380 case GST_EVENT_LATENCY:
2382 gst_event_unref (event);
2389 /* this is the final query function we receive on the internal source pad when
2390 * we deal with TCP connections */
2392 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2396 gboolean res = TRUE;
2398 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2400 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2401 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2403 switch (GST_QUERY_TYPE (query)) {
2404 case GST_QUERY_POSITION:
2409 case GST_QUERY_DURATION:
2413 gst_query_parse_duration (query, &format, NULL);
2416 case GST_FORMAT_TIME:
2417 gst_query_set_duration (query, format, src->segment.duration);
2425 case GST_QUERY_LATENCY:
2427 /* we are live with a min latency of 0 and unlimited max latency, this
2428 * result will be updated by the session manager if there is any. */
2429 gst_query_set_latency (query, TRUE, 0, -1);
2439 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2441 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2445 gboolean res = FALSE;
2447 src = GST_RTSPSRC_CAST (parent);
2449 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2450 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2452 switch (GST_QUERY_TYPE (query)) {
2453 case GST_QUERY_DURATION:
2457 gst_query_parse_duration (query, &format, NULL);
2460 case GST_FORMAT_TIME:
2461 gst_query_set_duration (query, format, src->segment.duration);
2469 case GST_QUERY_SEEKING:
2473 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2474 if (format == GST_FORMAT_TIME) {
2476 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2478 /* seeking without duration is unlikely */
2479 seekable = seekable && src->seekable && src->segment.duration &&
2480 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2482 /* FIXME ?? should we have 0 and segment.duration here; see demuxers */
2483 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
2484 src->segment.start, src->segment.stop);
2493 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2495 gst_query_set_uri (query, uri);
2503 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2505 /* forward the query to the proxy target pad */
2507 res = gst_pad_query (target, query);
2508 gst_object_unref (target);
2517 /* callback for RTCP messages to be sent to the server when operating in TCP
2519 static GstFlowReturn
2520 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2523 GstRTSPStream *stream;
2524 GstFlowReturn res = GST_FLOW_OK;
2529 GstRTSPMessage message = { 0 };
2530 GstRTSPConnection *conn;
2532 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2533 src = stream->parent;
2535 gst_buffer_map (buffer, &map, GST_MAP_READ);
2539 gst_rtsp_message_init_data (&message, stream->channel[1]);
2541 /* lend the body data to the message */
2542 gst_rtsp_message_take_body (&message, data, size);
2544 if (stream->conninfo.connection)
2545 conn = stream->conninfo.connection;
2547 conn = src->conninfo.connection;
2549 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2550 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2551 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2553 /* and steal it away again because we will free it when unreffing the
2555 gst_rtsp_message_steal_body (&message, &data, &size);
2556 gst_rtsp_message_unset (&message);
2558 gst_buffer_unmap (buffer, &map);
2559 gst_buffer_unref (buffer);
2564 static GstPadProbeReturn
2565 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2567 GstRTSPSrc *src = user_data;
2569 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2570 GST_DEBUG_PAD_NAME (pad));
2572 /* activate the streams */
2573 GST_OBJECT_LOCK (src);
2574 if (!src->need_activate)
2577 src->need_activate = FALSE;
2578 GST_OBJECT_UNLOCK (src);
2580 gst_rtspsrc_activate_streams (src);
2582 return GST_PAD_PROBE_OK;
2586 GST_OBJECT_UNLOCK (src);
2587 return GST_PAD_PROBE_OK;
2591 /* this callback is called when the session manager generated a new src pad with
2592 * payloaded RTP packets. We simply ghost the pad here. */
2594 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2597 GstPadTemplate *template;
2600 GstRTSPStream *stream;
2603 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2605 GST_RTSP_STATE_LOCK (src);
2607 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2608 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2609 goto unknown_stream;
2611 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2613 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2615 goto unknown_stream;
2618 stream->ssrc = ssrc;
2620 /* we'll add it later see below */
2621 stream->added = TRUE;
2623 /* check if we added all streams */
2625 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
2626 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
2628 GST_DEBUG_OBJECT (src, "stream %p, container %d, added %d, setup %d",
2629 ostream, ostream->container, ostream->added, ostream->setup);
2631 /* if we find a stream for which we did a setup that is not added, we
2632 * need to wait some more */
2633 if (ostream->setup && !ostream->added) {
2638 GST_RTSP_STATE_UNLOCK (src);
2640 /* create a new pad we will use to stream to */
2641 template = gst_static_pad_template_get (&rtptemplate);
2642 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2643 gst_object_unref (template);
2646 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2647 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2648 gst_pad_set_active (stream->srcpad, TRUE);
2649 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2652 GST_DEBUG_OBJECT (src, "We added all streams");
2653 /* when we get here, all stream are added and we can fire the no-more-pads
2655 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2663 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2664 GST_RTSP_STATE_UNLOCK (src);
2671 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
2675 len = stream->ptmap->len;
2676 for (i = 0; i < len; i++) {
2677 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
2685 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2687 GstRTSPStream *stream;
2690 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2692 GST_RTSP_STATE_LOCK (src);
2693 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2695 goto unknown_stream;
2697 if ((caps = stream_get_caps_for_pt (stream, pt)))
2698 gst_caps_ref (caps);
2699 GST_RTSP_STATE_UNLOCK (src);
2705 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2706 GST_RTSP_STATE_UNLOCK (src);
2712 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2714 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2720 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2726 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2732 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2734 GstRTSPSrc *src = stream->parent;
2737 g_object_get (source, "ssrc", &ssrc, NULL);
2739 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
2740 ssrc, stream->ssrc, stream->id);
2742 if (ssrc == stream->ssrc)
2743 gst_rtspsrc_do_stream_eos (src, stream);
2747 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2749 GstRTSPSrc *src = stream->parent;
2752 g_object_get (source, "ssrc", &ssrc, NULL);
2754 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
2755 ssrc, stream->ssrc, stream->id);
2757 if (ssrc == stream->ssrc)
2758 gst_rtspsrc_do_stream_eos (src, stream);
2762 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2764 GstRTSPStream *stream;
2766 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2768 /* get stream for session */
2769 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2771 gst_rtspsrc_do_stream_eos (src, stream);
2776 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2778 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2783 set_manager_buffer_mode (GstRTSPSrc * src)
2785 GObjectClass *klass;
2787 if (src->manager == NULL)
2790 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2792 if (!g_object_class_find_property (klass, "buffer-mode"))
2795 if (src->buffer_mode != BUFFER_MODE_AUTO) {
2796 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
2801 GST_DEBUG_OBJECT (src,
2802 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
2804 if (src->provided_clock) {
2805 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
2807 if (clock == src->provided_clock) {
2808 GST_DEBUG_OBJECT (src, "selected synced");
2809 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
2812 gst_object_unref (clock);
2817 /* Otherwise fall-through and use another buffer mode */
2819 gst_object_unref (clock);
2822 GST_DEBUG_OBJECT (src, "auto buffering mode");
2823 if (src->use_buffering) {
2824 GST_DEBUG_OBJECT (src, "selected buffer");
2825 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
2827 GST_DEBUG_OBJECT (src, "selected slave");
2828 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
2832 /* try to get and configure a manager */
2834 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
2835 GstRTSPTransport * transport)
2837 const gchar *manager;
2839 GstStateChangeReturn ret;
2841 /* find a manager */
2842 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
2846 GST_DEBUG_OBJECT (src, "using manager %s", manager);
2848 /* configure the manager */
2849 if (src->manager == NULL) {
2850 GObjectClass *klass;
2852 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
2854 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
2858 goto use_no_manager;
2860 if (!(src->manager = gst_element_factory_make (manager, "manager")))
2861 goto manager_failed;
2864 /* we manage this element */
2865 gst_element_set_locked_state (src->manager, TRUE);
2866 gst_bin_add (GST_BIN_CAST (src), src->manager);
2868 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
2869 if (ret == GST_STATE_CHANGE_FAILURE)
2870 goto start_manager_failure;
2872 g_object_set (src->manager, "latency", src->latency, NULL);
2874 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2876 if (g_object_class_find_property (klass, "ntp-sync")) {
2877 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
2880 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
2881 g_object_set (src->manager, "use-pipeline-clock",
2882 src->use_pipeline_clock, NULL);
2885 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
2886 g_object_set (src->manager, "sdes", src->sdes, NULL);
2889 if (g_object_class_find_property (klass, "drop-on-latency")) {
2890 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
2894 /* buffer mode pauses are handled by adding offsets to buffer times,
2895 * but some depayloaders may have a hard time syncing output times
2896 * with such input times, e.g. container ones, most notably ASF */
2897 /* TODO alternatives are having an event that indicates these shifts,
2898 * or having rtsp extensions provide suggestion on buffer mode */
2899 /* valid duration implies not likely live pipeline,
2900 * so slaving in jitterbuffer does not make much sense
2901 * (and might mess things up due to bursts) */
2902 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
2903 src->segment.duration && !stream->container) {
2904 src->use_buffering = TRUE;
2906 src->use_buffering = FALSE;
2909 set_manager_buffer_mode (src);
2911 /* connect to signals */
2912 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
2914 src->manager_sig_id =
2915 g_signal_connect (src->manager, "pad-added",
2916 (GCallback) new_manager_pad, src);
2917 src->manager_ptmap_id =
2918 g_signal_connect (src->manager, "request-pt-map",
2919 (GCallback) request_pt_map, src);
2921 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
2924 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
2928 /* we stream directly to the manager, get some pads. Each RTSP stream goes
2929 * into a separate RTP session. */
2930 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
2931 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
2933 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
2934 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
2937 /* now configure the bandwidth in the manager */
2938 if (g_signal_lookup ("get-internal-session",
2939 G_OBJECT_TYPE (src->manager)) != 0) {
2940 GObject *rtpsession;
2942 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
2945 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
2947 stream->session = rtpsession;
2949 if (stream->as_bandwidth != -1) {
2950 GST_INFO_OBJECT (src, "setting AS: %f",
2951 (gdouble) (stream->as_bandwidth * 1000));
2952 g_object_set (rtpsession, "bandwidth",
2953 (gdouble) (stream->as_bandwidth * 1000), NULL);
2955 if (stream->rr_bandwidth != -1) {
2956 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
2957 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
2960 if (stream->rs_bandwidth != -1) {
2961 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
2962 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
2966 g_object_set (rtpsession, "probation", src->probation, NULL);
2968 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2970 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
2972 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
2974 g_signal_connect (rtpsession, "on-ssrc-active",
2975 (GCallback) on_ssrc_active, stream);
2986 GST_DEBUG_OBJECT (src, "cannot get a session manager");
2991 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
2994 start_manager_failure:
2996 GST_DEBUG_OBJECT (src, "could not start session manager");
3001 /* free the UDP sources allocated when negotiating a transport.
3002 * This function is called when the server negotiated to a transport where the
3003 * UDP sources are not needed anymore, such as TCP or multicast. */
3005 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
3009 for (i = 0; i < 2; i++) {
3010 if (stream->udpsrc[i]) {
3011 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
3012 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
3013 gst_object_unref (stream->udpsrc[i]);
3014 stream->udpsrc[i] = NULL;
3019 /* for TCP, create pads to send and receive data to and from the manager and to
3020 * intercept various events and queries
3023 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
3024 GstRTSPTransport * transport, GstPad ** outpad)
3027 GstPadTemplate *template;
3028 GstPad *pad0, *pad1;
3030 /* configure for interleaved delivery, nothing needs to be done
3031 * here, the loop function will call the chain functions of the
3032 * session manager. */
3033 stream->channel[0] = transport->interleaved.min;
3034 stream->channel[1] = transport->interleaved.max;
3035 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
3036 stream->channel[0], stream->channel[1]);
3038 /* we can remove the allocated UDP ports now */
3039 gst_rtspsrc_stream_free_udp (stream);
3041 /* no session manager, send data to srcpad directly */
3042 if (!stream->channelpad[0]) {
3043 GST_DEBUG_OBJECT (src, "no manager, creating pad");
3045 /* create a new pad we will use to stream to */
3046 name = g_strdup_printf ("stream_%u", stream->id);
3047 template = gst_static_pad_template_get (&rtptemplate);
3048 stream->channelpad[0] = gst_pad_new_from_template (template, name);
3049 gst_object_unref (template);
3052 /* set caps and activate */
3053 gst_pad_use_fixed_caps (stream->channelpad[0]);
3054 gst_pad_set_active (stream->channelpad[0], TRUE);
3056 *outpad = gst_object_ref (stream->channelpad[0]);
3058 GST_DEBUG_OBJECT (src, "using manager source pad");
3060 template = gst_static_pad_template_get (&anysrctemplate);
3062 /* allocate pads for sending the channel data into the manager */
3063 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
3064 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
3065 gst_object_unref (stream->channelpad[0]);
3066 stream->channelpad[0] = pad0;
3067 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
3068 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
3069 gst_pad_set_element_private (pad0, src);
3070 gst_pad_set_active (pad0, TRUE);
3072 if (stream->channelpad[1]) {
3073 /* if we have a sinkpad for the other channel, create a pad and link to the
3075 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
3076 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
3077 gst_pad_link_full (pad1, stream->channelpad[1],
3078 GST_PAD_LINK_CHECK_NOTHING);
3079 gst_object_unref (stream->channelpad[1]);
3080 stream->channelpad[1] = pad1;
3081 gst_pad_set_active (pad1, TRUE);
3083 gst_object_unref (template);
3085 /* setup RTCP transport back to the server if we have to. */
3086 if (src->manager && src->do_rtcp) {
3089 template = gst_static_pad_template_get (&anysinktemplate);
3091 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
3092 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
3093 gst_pad_set_element_private (stream->rtcppad, stream);
3094 gst_pad_set_active (stream->rtcppad, TRUE);
3096 /* get session RTCP pad */
3097 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3098 pad = gst_element_get_request_pad (src->manager, name);
3103 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3104 gst_object_unref (pad);
3107 gst_object_unref (template);
3113 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
3114 GstRTSPTransport * transport, const gchar ** destination, gint * min,
3115 gint * max, guint * ttl)
3117 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3119 if (!(*destination = transport->destination))
3120 *destination = stream->destination;
3123 /* transport first */
3124 *min = transport->port.min;
3125 *max = transport->port.max;
3126 if (*min == -1 && *max == -1) {
3127 /* then try from SDP */
3128 if (stream->port != 0) {
3129 *min = stream->port;
3130 *max = stream->port + 1;
3136 if (!(*ttl = transport->ttl))
3141 /* first take the source, then the endpoint to figure out where to send
3143 if (!(*destination = transport->source)) {
3144 if (src->conninfo.connection)
3145 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3146 else if (stream->conninfo.connection)
3148 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3152 /* for unicast we only expect the ports here */
3153 *min = transport->server_port.min;
3154 *max = transport->server_port.max;
3159 /* For multicast create UDP sources and join the multicast group. */
3161 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3162 GstRTSPTransport * transport, GstPad ** outpad)
3165 const gchar *destination;
3168 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3170 /* we can remove the allocated UDP ports now */
3171 gst_rtspsrc_stream_free_udp (stream);
3173 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3176 /* we need a destination now */
3177 if (destination == NULL)
3178 goto no_destination;
3180 /* we really need ports now or we won't be able to receive anything at all */
3181 if (min == -1 && max == -1)
3184 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3185 destination, min, max);
3187 /* creating UDP source for RTP */
3189 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3191 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3193 if (stream->udpsrc[0] == NULL)
3196 /* take ownership */
3197 gst_object_ref_sink (stream->udpsrc[0]);
3199 if (src->udp_buffer_size != 0)
3200 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3201 src->udp_buffer_size, NULL);
3203 if (src->multi_iface != NULL)
3204 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3205 src->multi_iface, NULL);
3208 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3209 gst_element_set_state (stream->udpsrc[0], GST_STATE_PAUSED);
3212 /* creating another UDP source for RTCP */
3216 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3218 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3220 if (stream->udpsrc[1] == NULL)
3223 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3224 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3225 gst_caps_unref (caps);
3227 /* take ownership */
3228 gst_object_ref_sink (stream->udpsrc[1]);
3230 if (src->multi_iface != NULL)
3231 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3232 src->multi_iface, NULL);
3234 gst_element_set_state (stream->udpsrc[1], GST_STATE_PAUSED);
3241 GST_DEBUG_OBJECT (src, "no UDP source element found");
3246 GST_DEBUG_OBJECT (src, "no destination found");
3251 GST_DEBUG_OBJECT (src, "no ports found");
3256 /* configure the remainder of the UDP ports */
3258 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3259 GstRTSPTransport * transport, GstPad ** outpad)
3261 /* we manage the UDP elements now. For unicast, the UDP sources where
3262 * allocated in the stream when we suggested a transport. */
3263 if (stream->udpsrc[0]) {
3264 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3265 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3267 GST_DEBUG_OBJECT (src, "setting up UDP source");
3269 /* configure a timeout on the UDP port. When the timeout message is
3270 * posted, we assume UDP transport is not possible. We reconnect using TCP
3272 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3273 src->udp_timeout * 1000, NULL);
3275 /* get output pad of the UDP source. */
3276 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3278 /* save it so we can unblock */
3279 stream->blockedpad = *outpad;
3281 /* configure pad block on the pad. As soon as there is dataflow on the
3282 * UDP source, we know that UDP is not blocked by a firewall and we can
3283 * configure all the streams to let the application autoplug decoders. */
3285 gst_pad_add_probe (stream->blockedpad,
3286 GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM, pad_blocked, src, NULL);
3288 if (stream->channelpad[0]) {
3289 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3290 /* configure for UDP delivery, we need to connect the UDP pads to
3291 * the session plugin. */
3292 gst_pad_link_full (*outpad, stream->channelpad[0],
3293 GST_PAD_LINK_CHECK_NOTHING);
3294 gst_object_unref (*outpad);
3296 /* we connected to pad-added signal to get pads from the manager */
3298 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3303 if (stream->udpsrc[1]) {
3306 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3307 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3309 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3310 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3311 gst_caps_unref (caps);
3313 if (stream->channelpad[1]) {
3316 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3318 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3319 gst_pad_link_full (pad, stream->channelpad[1],
3320 GST_PAD_LINK_CHECK_NOTHING);
3321 gst_object_unref (pad);
3323 /* leave unlinked */
3329 /* configure the UDP sink back to the server for status reports */
3331 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3332 GstRTSPStream * stream, GstRTSPTransport * transport)
3335 gint rtp_port, rtcp_port;
3336 gboolean do_rtp, do_rtcp;
3337 const gchar *destination;
3342 /* get transport info */
3343 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
3344 &rtp_port, &rtcp_port, &ttl);
3346 /* see what we need to do */
3347 do_rtp = (rtp_port != -1);
3348 /* it's possible that the server does not want us to send RTCP in which case
3350 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
3352 /* we need a destination when we have RTP or RTCP ports */
3353 if (destination == NULL && (do_rtp || do_rtcp))
3354 goto no_destination;
3356 /* try to construct the fakesrc to the RTP port of the server to open up any
3359 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
3362 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
3363 stream->udpsink[0] =
3364 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3366 if (stream->udpsink[0] == NULL)
3367 goto no_sink_element;
3369 /* don't join multicast group, we will have the source socket do that */
3370 /* no sync or async state changes needed */
3371 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
3372 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3374 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3376 if (stream->udpsrc[0]) {
3377 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
3378 * so that NAT firewalls will open a hole for us */
3379 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
3380 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
3381 /* configure socket and make sure udpsink does not close it when shutting
3382 * down, it belongs to udpsrc after all. */
3383 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
3384 "close-socket", FALSE, NULL);
3385 g_object_unref (socket);
3388 /* the source for the dummy packets to open up NAT */
3389 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
3390 if (stream->fakesrc == NULL)
3391 goto no_fakesrc_element;
3393 /* random data in 5 buffers, a size of 200 bytes should be fine */
3394 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
3395 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
3397 /* we don't want to consider this a sink */
3398 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
3400 /* keep everything locked */
3401 gst_element_set_locked_state (stream->udpsink[0], TRUE);
3402 gst_element_set_locked_state (stream->fakesrc, TRUE);
3404 gst_object_ref (stream->udpsink[0]);
3405 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
3406 gst_object_ref (stream->fakesrc);
3407 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
3409 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
3410 "sink", GST_PAD_LINK_CHECK_NOTHING);
3413 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
3416 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
3417 stream->udpsink[1] =
3418 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3420 if (stream->udpsink[1] == NULL)
3421 goto no_sink_element;
3423 /* don't join multicast group, we will have the source socket do that */
3424 /* no sync or async state changes needed */
3425 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
3426 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3428 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3430 if (stream->udpsrc[1]) {
3431 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
3432 * because some servers check the port number of where it sends RTCP to identify
3433 * the RTCP packets it receives */
3434 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
3435 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
3436 /* configure socket and make sure udpsink does not close it when shutting
3437 * down, it belongs to udpsrc after all. */
3438 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
3439 "close-socket", FALSE, NULL);
3440 g_object_unref (socket);
3443 /* we don't want to consider this a sink */
3444 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
3446 /* we keep this playing always */
3447 gst_element_set_locked_state (stream->udpsink[1], TRUE);
3448 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
3450 gst_object_ref (stream->udpsink[1]);
3451 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
3453 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
3455 /* get session RTCP pad */
3456 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3457 pad = gst_element_get_request_pad (src->manager, name);
3462 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3463 gst_object_unref (pad);
3472 GST_DEBUG_OBJECT (src, "no destination address specified");
3477 GST_DEBUG_OBJECT (src, "no UDP sink element found");
3482 GST_DEBUG_OBJECT (src, "no fakesrc element found");
3487 /* sets up all elements needed for streaming over the specified transport.
3488 * Does not yet expose the element pads, this will be done when there is actuall
3489 * dataflow detected, which might never happen when UDP is blocked in a
3490 * firewall, for example.
3493 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
3494 GstRTSPTransport * transport)
3497 GstPad *outpad = NULL;
3498 GstPadTemplate *template;
3500 const gchar *media_type;
3503 src = stream->parent;
3505 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
3507 /* get the proper media type for this stream now */
3508 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
3509 goto unknown_transport;
3511 goto unknown_transport;
3513 /* configure the final media type */
3514 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
3516 len = stream->ptmap->len;
3517 for (i = 0; i < len; i++) {
3519 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3521 if (item->caps == NULL)
3524 s = gst_caps_get_structure (item->caps, 0);
3525 gst_structure_set_name (s, media_type);
3528 /* try to get and configure a manager, channelpad[0-1] will be configured with
3529 * the pads for the manager, or NULL when no manager is needed. */
3530 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
3533 switch (transport->lower_transport) {
3534 case GST_RTSP_LOWER_TRANS_TCP:
3535 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
3536 goto transport_failed;
3538 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3539 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
3540 goto transport_failed;
3541 /* fallthrough, the rest is the same for UDP and MCAST */
3542 case GST_RTSP_LOWER_TRANS_UDP:
3543 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
3544 goto transport_failed;
3545 /* configure udpsinks back to the server for RTCP messages and for the
3546 * dummy RTP messages to open NAT. */
3547 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
3548 goto transport_failed;
3551 goto unknown_transport;
3555 GST_DEBUG_OBJECT (src, "creating ghostpad");
3557 gst_pad_use_fixed_caps (outpad);
3559 /* create ghostpad, don't add just yet, this will be done when we activate
3561 name = g_strdup_printf ("stream_%u", stream->id);
3562 template = gst_static_pad_template_get (&rtptemplate);
3563 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
3564 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3565 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3566 gst_object_unref (template);
3569 gst_object_unref (outpad);
3571 /* mark pad as ok */
3572 stream->last_ret = GST_FLOW_OK;
3579 GST_DEBUG_OBJECT (src, "failed to configure transport");
3584 GST_DEBUG_OBJECT (src, "unknown transport");
3589 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3594 /* send a couple of dummy random packets on the receiver RTP port to the server,
3595 * this should make a firewall think we initiated the data transfer and
3596 * hopefully allow packets to go from the sender port to our RTP receiver port */
3598 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
3602 if (src->nat_method != GST_RTSP_NAT_DUMMY)
3605 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3606 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3608 if (stream->fakesrc && stream->udpsink[0]) {
3609 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
3610 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
3611 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
3612 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
3613 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
3619 /* Adds the source pads of all configured streams to the element.
3620 * This code is performed when we detected dataflow.
3622 * We detect dataflow from either the _loop function or with pad probes on the
3626 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
3630 GST_DEBUG_OBJECT (src, "activating streams");
3632 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3633 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3635 if (stream->udpsrc[0]) {
3636 /* remove timeout, we are streaming now and timeouts will be handled by
3637 * the session manager and jitter buffer */
3638 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
3640 if (stream->srcpad) {
3641 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
3642 gst_pad_set_active (stream->srcpad, TRUE);
3644 /* if we don't have a session manager, set the caps now. If we have a
3645 * session, we will get a notification of the pad and the caps. */
3646 if (!src->manager) {
3649 caps = stream_get_caps_for_pt (stream, stream->default_pt);
3650 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
3651 gst_pad_set_caps (stream->srcpad, caps);
3654 if (!stream->added) {
3655 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
3656 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3657 stream->added = TRUE;
3662 /* unblock all pads */
3663 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3664 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3666 if (stream->blockid) {
3667 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
3668 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
3669 stream->blockid = 0;
3677 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
3678 gboolean reset_manager)
3681 guint64 start, stop;
3682 gdouble play_speed, play_scale;
3684 GST_DEBUG_OBJECT (src, "configuring stream caps");
3686 start = segment->position;
3687 stop = segment->duration;
3688 play_speed = segment->rate;
3689 play_scale = segment->applied_rate;
3691 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3692 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3698 len = stream->ptmap->len;
3699 for (j = 0; j < len; j++) {
3701 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
3703 if (item->caps == NULL)
3706 caps = gst_caps_make_writable (item->caps);
3708 if (stream->timebase != -1)
3709 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
3710 (guint) stream->timebase, NULL);
3711 if (stream->seqbase != -1)
3712 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
3713 (guint) stream->seqbase, NULL);
3714 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
3716 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
3717 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
3718 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
3721 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
3725 if (reset_manager && src->manager) {
3726 GST_DEBUG_OBJECT (src, "clear session");
3727 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
3731 static GstFlowReturn
3732 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
3737 /* store the value */
3738 stream->last_ret = ret;
3740 /* if it's success we can return the value right away */
3741 if (ret == GST_FLOW_OK)
3744 /* any other error that is not-linked can be returned right
3746 if (ret != GST_FLOW_NOT_LINKED)
3749 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3750 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3751 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3753 ret = ostream->last_ret;
3754 /* some other return value (must be SUCCESS but we can return
3755 * other values as well) */
3756 if (ret != GST_FLOW_NOT_LINKED)
3759 /* if we get here, all other pads were unlinked and we return
3760 * NOT_LINKED then */
3766 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
3769 gboolean res = TRUE;
3771 /* only streams that have a connection to the outside world */
3775 if (stream->udpsrc[0]) {
3776 gst_event_ref (event);
3777 res = gst_element_send_event (stream->udpsrc[0], event);
3778 } else if (stream->channelpad[0]) {
3779 gst_event_ref (event);
3780 if (GST_PAD_IS_SRC (stream->channelpad[0]))
3781 res = gst_pad_push_event (stream->channelpad[0], event);
3783 res = gst_pad_send_event (stream->channelpad[0], event);
3786 if (stream->udpsrc[1]) {
3787 gst_event_ref (event);
3788 res &= gst_element_send_event (stream->udpsrc[1], event);
3789 } else if (stream->channelpad[1]) {
3790 gst_event_ref (event);
3791 if (GST_PAD_IS_SRC (stream->channelpad[1]))
3792 res &= gst_pad_push_event (stream->channelpad[1], event);
3794 res &= gst_pad_send_event (stream->channelpad[1], event);
3798 gst_event_unref (event);
3804 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
3807 gboolean res = TRUE;
3809 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3810 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3812 gst_event_ref (event);
3813 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
3815 gst_event_unref (event);
3820 static GstRTSPResult
3821 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3826 if (info->connection == NULL) {
3827 if (info->url == NULL) {
3828 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
3829 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
3833 /* create connection */
3834 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
3835 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
3836 goto could_not_create;
3839 g_free (info->url_str);
3840 info->url_str = gst_rtsp_url_get_request_uri (info->url);
3842 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
3844 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
3845 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
3846 src->tls_validation_flags))
3847 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
3849 if (src->tls_database)
3850 gst_rtsp_connection_set_tls_database (info->connection,
3854 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
3855 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
3857 if (src->proxy_host) {
3858 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
3860 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
3865 if (!info->connected) {
3868 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
3869 ("Connecting to %s", info->location));
3870 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
3872 gst_rtsp_connection_connect (info->connection,
3873 src->ptcp_timeout)) < 0)
3874 goto could_not_connect;
3876 info->connected = TRUE;
3883 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
3888 gchar *str = gst_rtsp_strresult (res);
3889 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
3895 gchar *str = gst_rtsp_strresult (res);
3896 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
3902 static GstRTSPResult
3903 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
3906 GST_RTSP_STATE_LOCK (src);
3907 if (info->connected) {
3908 GST_DEBUG_OBJECT (src, "closing connection...");
3909 gst_rtsp_connection_close (info->connection);
3910 info->connected = FALSE;
3912 if (free && info->connection) {
3913 /* free connection */
3914 GST_DEBUG_OBJECT (src, "freeing connection...");
3915 gst_rtsp_connection_free (info->connection);
3916 info->connection = NULL;
3918 GST_RTSP_STATE_UNLOCK (src);
3922 static GstRTSPResult
3923 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3928 GST_DEBUG_OBJECT (src, "reconnecting connection...");
3929 gst_rtsp_conninfo_close (src, info, FALSE);
3930 res = gst_rtsp_conninfo_connect (src, info, async);
3936 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
3940 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
3941 GST_RTSP_STATE_LOCK (src);
3942 if (src->conninfo.connection && src->conninfo.flushing != flush) {
3943 GST_DEBUG_OBJECT (src, "connection flush");
3944 gst_rtsp_connection_flush (src->conninfo.connection, flush);
3945 src->conninfo.flushing = flush;
3947 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3948 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3949 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
3950 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
3951 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
3952 stream->conninfo.flushing = flush;
3955 GST_RTSP_STATE_UNLOCK (src);
3958 /* FIXME, handle server request, reply with OK, for now */
3959 static GstRTSPResult
3960 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
3961 GstRTSPMessage * request)
3963 GstRTSPMessage response = { 0 };
3966 GST_DEBUG_OBJECT (src, "got server request message");
3969 gst_rtsp_message_dump (request);
3971 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
3973 if (res == GST_RTSP_ENOTIMPL) {
3974 /* default implementation, send OK */
3975 GST_DEBUG_OBJECT (src, "prepare OK reply");
3977 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
3982 /* let app parse and reply */
3983 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
3984 0, request, &response);
3987 gst_rtsp_message_dump (&response);
3989 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
3993 gst_rtsp_message_unset (&response);
3994 } else if (res == GST_RTSP_EEOF)
4002 gst_rtsp_message_unset (&response);
4007 /* send server keep-alive */
4008 static GstRTSPResult
4009 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
4011 GstRTSPMessage request = { 0 };
4013 GstRTSPMethod method;
4014 const gchar *control;
4016 if (src->do_rtsp_keep_alive == FALSE) {
4017 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
4018 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4022 GST_DEBUG_OBJECT (src, "creating server keep-alive");
4024 /* find a method to use for keep-alive */
4025 if (src->methods & GST_RTSP_GET_PARAMETER)
4026 method = GST_RTSP_GET_PARAMETER;
4028 method = GST_RTSP_OPTIONS;
4030 control = get_aggregate_control (src);
4031 if (control == NULL)
4034 res = gst_rtsp_message_init_request (&request, method, control);
4039 gst_rtsp_message_dump (&request);
4042 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
4047 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4048 gst_rtsp_message_unset (&request);
4055 GST_WARNING_OBJECT (src, "no control url to send keepalive");
4060 gchar *str = gst_rtsp_strresult (res);
4062 gst_rtsp_message_unset (&request);
4063 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
4064 ("Could not send keep-alive. (%s)", str));
4070 static GstFlowReturn
4071 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
4073 GstFlowReturn ret = GST_FLOW_OK;
4075 GstRTSPStream *stream;
4076 GstPad *outpad = NULL;
4083 channel = message->type_data.data.channel;
4085 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
4087 goto unknown_stream;
4089 if (channel == stream->channel[0]) {
4090 outpad = stream->channelpad[0];
4092 } else if (channel == stream->channel[1]) {
4093 outpad = stream->channelpad[1];
4099 /* take a look at the body to figure out what we have */
4100 gst_rtsp_message_get_body (message, &data, &size);
4102 goto invalid_length;
4104 /* channels are not correct on some servers, do extra check */
4105 if (data[1] >= 200 && data[1] <= 204) {
4106 /* hmm RTCP message switch to the RTCP pad of the same stream. */
4107 outpad = stream->channelpad[1];
4111 /* we have no clue what this is, just ignore then. */
4113 goto unknown_stream;
4115 /* take the message body for further processing */
4116 gst_rtsp_message_steal_body (message, &data, &size);
4118 /* strip the trailing \0 */
4121 buf = gst_buffer_new ();
4122 gst_buffer_append_memory (buf,
4123 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
4125 /* don't need message anymore */
4126 gst_rtsp_message_unset (message);
4128 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
4131 if (src->need_activate) {
4137 guint group_id = gst_util_group_id_next ();
4139 /* generate an SHA256 sum of the URI */
4140 cs = g_checksum_new (G_CHECKSUM_SHA256);
4141 uri = src->conninfo.location;
4142 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
4144 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4145 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4148 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
4149 event = gst_event_new_stream_start (stream_id);
4150 gst_event_set_group_id (event, group_id);
4153 gst_rtspsrc_stream_push_event (src, ostream, event);
4155 g_checksum_free (cs);
4157 gst_rtspsrc_activate_streams (src);
4158 src->need_activate = FALSE;
4160 if ((event = src->start_segment) != NULL) {
4161 src->start_segment = NULL;
4162 gst_rtspsrc_push_event (src, event);
4165 if (src->base_time == -1) {
4166 /* Take current running_time. This timestamp will be put on
4167 * the first buffer of each stream because we are a live source and so we
4168 * timestamp with the running_time. When we are dealing with TCP, we also
4169 * only timestamp the first buffer (using the DISCONT flag) because a server
4170 * typically bursts data, for which we don't want to compensate by speeding
4171 * up the media. The other timestamps will be interpollated from this one
4172 * using the RTP timestamps. */
4173 GST_OBJECT_LOCK (src);
4174 if (GST_ELEMENT_CLOCK (src)) {
4176 GstClockTime base_time;
4178 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
4179 base_time = GST_ELEMENT_CAST (src)->base_time;
4181 src->base_time = now - base_time;
4183 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
4184 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
4186 GST_OBJECT_UNLOCK (src);
4189 if (stream->discont && !is_rtcp) {
4190 /* mark first RTP buffer as discont */
4191 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4192 stream->discont = FALSE;
4193 /* first buffer gets the timestamp, other buffers are not timestamped and
4194 * their presentation time will be interpollated from the rtp timestamps. */
4195 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
4196 GST_TIME_ARGS (src->base_time));
4198 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
4201 /* chain to the peer pad */
4202 if (GST_PAD_IS_SINK (outpad))
4203 ret = gst_pad_chain (outpad, buf);
4205 ret = gst_pad_push (outpad, buf);
4208 /* combine all stream flows for the data transport */
4209 ret = gst_rtspsrc_combine_flows (src, stream, ret);
4216 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
4217 gst_rtsp_message_unset (message);
4222 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4223 ("Short message received, ignoring."));
4224 gst_rtsp_message_unset (message);
4229 static GstFlowReturn
4230 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
4232 GstRTSPMessage message = { 0 };
4234 GstFlowReturn ret = GST_FLOW_OK;
4235 GTimeVal tv_timeout;
4238 /* get the next timeout interval */
4239 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4241 /* see if the timeout period expired */
4242 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
4243 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
4244 /* send keep-alive, only act on interrupt, a warning will be posted for
4246 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4248 /* get new timeout */
4249 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4252 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
4253 tv_timeout.tv_sec, tv_timeout.tv_usec);
4255 /* protect the connection with the connection lock so that we can see when
4256 * we are finished doing server communication */
4258 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4259 &message, src->ptcp_timeout);
4263 GST_DEBUG_OBJECT (src, "we received a server message");
4265 case GST_RTSP_EINTR:
4266 /* we got interrupted this means we need to stop */
4268 case GST_RTSP_ETIMEOUT:
4269 /* no reply, send keep alive */
4270 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4271 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4275 /* go EOS when the server closed the connection */
4281 switch (message.type) {
4282 case GST_RTSP_MESSAGE_REQUEST:
4283 /* server sends us a request message, handle it */
4285 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4287 if (res == GST_RTSP_EEOF)
4290 goto handle_request_failed;
4292 case GST_RTSP_MESSAGE_RESPONSE:
4293 /* we ignore response messages */
4294 GST_DEBUG_OBJECT (src, "ignoring response message");
4296 gst_rtsp_message_dump (&message);
4298 case GST_RTSP_MESSAGE_DATA:
4299 GST_DEBUG_OBJECT (src, "got data message");
4300 ret = gst_rtspsrc_handle_data (src, &message);
4301 if (ret != GST_FLOW_OK)
4302 goto handle_data_failed;
4305 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4310 g_assert_not_reached ();
4315 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4316 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4317 ("The server closed the connection."));
4318 src->conninfo.connected = FALSE;
4319 gst_rtsp_message_unset (&message);
4320 return GST_FLOW_EOS;
4324 gst_rtsp_message_unset (&message);
4325 GST_DEBUG_OBJECT (src, "got interrupted");
4326 return GST_FLOW_FLUSHING;
4330 gchar *str = gst_rtsp_strresult (res);
4332 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4333 ("Could not receive message. (%s)", str));
4336 gst_rtsp_message_unset (&message);
4337 return GST_FLOW_ERROR;
4339 handle_request_failed:
4341 gchar *str = gst_rtsp_strresult (res);
4343 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4344 ("Could not handle server message. (%s)", str));
4346 gst_rtsp_message_unset (&message);
4347 return GST_FLOW_ERROR;
4351 GST_DEBUG_OBJECT (src, "could no handle data message");
4356 static GstFlowReturn
4357 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
4360 GstRTSPMessage message = { 0 };
4364 GTimeVal tv_timeout;
4366 /* get the next timeout interval */
4367 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4369 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
4370 (gint) tv_timeout.tv_sec);
4372 gst_rtsp_message_unset (&message);
4374 /* we should continue reading the TCP socket because the server might
4375 * send us requests. When the session timeout expires, we need to send a
4376 * keep-alive request to keep the session open. */
4377 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4378 &message, &tv_timeout);
4382 GST_DEBUG_OBJECT (src, "we received a server message");
4384 case GST_RTSP_EINTR:
4385 /* we got interrupted, see what we have to do */
4387 case GST_RTSP_ETIMEOUT:
4388 /* send keep-alive, ignore the result, a warning will be posted. */
4389 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4390 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4394 /* server closed the connection. not very fatal for UDP, reconnect and
4395 * see what happens. */
4396 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4397 ("The server closed the connection."));
4398 if (src->udp_reconnect) {
4400 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
4407 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
4409 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4410 ("Unhandled return value %d.", res));
4414 switch (message.type) {
4415 case GST_RTSP_MESSAGE_REQUEST:
4416 /* server sends us a request message, handle it */
4418 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4420 if (res == GST_RTSP_EEOF)
4423 goto handle_request_failed;
4425 case GST_RTSP_MESSAGE_RESPONSE:
4426 /* we ignore response and data messages */
4427 GST_DEBUG_OBJECT (src, "ignoring response message");
4429 gst_rtsp_message_dump (&message);
4430 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4431 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
4432 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
4433 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
4434 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4441 case GST_RTSP_MESSAGE_DATA:
4442 /* we ignore response and data messages */
4443 GST_DEBUG_OBJECT (src, "ignoring data message");
4446 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4451 g_assert_not_reached ();
4453 /* we get here when the connection got interrupted */
4456 gst_rtsp_message_unset (&message);
4457 GST_DEBUG_OBJECT (src, "got interrupted");
4458 return GST_FLOW_FLUSHING;
4462 gchar *str = gst_rtsp_strresult (res);
4465 src->conninfo.connected = FALSE;
4466 if (res != GST_RTSP_EINTR) {
4467 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
4468 ("Could not connect to server. (%s)", str));
4470 ret = GST_FLOW_ERROR;
4472 ret = GST_FLOW_FLUSHING;
4478 gchar *str = gst_rtsp_strresult (res);
4480 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4481 ("Could not receive message. (%s)", str));
4483 return GST_FLOW_ERROR;
4485 handle_request_failed:
4487 gchar *str = gst_rtsp_strresult (res);
4490 gst_rtsp_message_unset (&message);
4491 if (res != GST_RTSP_EINTR) {
4492 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4493 ("Could not handle server message. (%s)", str));
4495 ret = GST_FLOW_ERROR;
4497 ret = GST_FLOW_FLUSHING;
4503 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4504 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4505 ("The server closed the connection."));
4506 src->conninfo.connected = FALSE;
4507 gst_rtsp_message_unset (&message);
4508 return GST_FLOW_EOS;
4512 static GstRTSPResult
4513 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
4515 GstRTSPResult res = GST_RTSP_OK;
4518 GST_DEBUG_OBJECT (src, "doing reconnect");
4520 GST_OBJECT_LOCK (src);
4521 /* only restart when the pads were not yet activated, else we were
4522 * streaming over UDP */
4523 restart = src->need_activate;
4524 GST_OBJECT_UNLOCK (src);
4526 /* no need to restart, we're done */
4530 /* we can try only TCP now */
4531 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
4533 /* close and cleanup our state */
4534 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
4537 /* see if we have TCP left to try. Also don't try TCP when we were configured
4539 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
4542 /* We post a warning message now to inform the user
4543 * that nothing happened. It's most likely a firewall thing. */
4544 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4545 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4546 "firewall is blocking it. Retrying using a TCP connection.",
4547 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4549 /* open new connection using tcp */
4550 if (gst_rtspsrc_open (src, async) < 0)
4553 /* start playback */
4554 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
4563 src->cur_protocols = 0;
4564 /* no transport possible, post an error and stop */
4565 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4566 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4567 "firewall is blocking it. No other protocols to try.",
4568 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4569 return GST_RTSP_ERROR;
4573 GST_DEBUG_OBJECT (src, "open failed");
4578 GST_DEBUG_OBJECT (src, "play failed");
4584 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
4588 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
4591 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
4594 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
4597 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
4605 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
4609 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
4612 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
4615 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
4618 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
4626 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
4630 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
4633 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
4636 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
4639 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
4647 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
4651 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
4654 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
4657 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
4660 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
4668 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
4670 if (ret == GST_RTSP_OK)
4671 gst_rtspsrc_loop_complete_cmd (src, cmd);
4672 else if (ret == GST_RTSP_EINTR)
4673 gst_rtspsrc_loop_cancel_cmd (src, cmd);
4675 gst_rtspsrc_loop_error_cmd (src, cmd);
4679 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
4682 gboolean flushed = FALSE;
4684 /* start new request */
4685 gst_rtspsrc_loop_start_cmd (src, cmd);
4687 GST_DEBUG_OBJECT (src, "sending cmd %d", cmd);
4689 GST_OBJECT_LOCK (src);
4690 old = src->pending_cmd;
4691 if (old == CMD_RECONNECT) {
4692 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
4693 cmd = CMD_RECONNECT;
4695 if (old != CMD_WAIT) {
4696 src->pending_cmd = CMD_WAIT;
4697 GST_OBJECT_UNLOCK (src);
4698 /* cancel previous request */
4699 GST_DEBUG_OBJECT (src, "cancel previous request %d", old);
4700 gst_rtspsrc_loop_cancel_cmd (src, old);
4701 GST_OBJECT_LOCK (src);
4703 src->pending_cmd = cmd;
4704 /* interrupt if allowed */
4705 if (src->busy_cmd & mask) {
4706 GST_DEBUG_OBJECT (src, "connection flush busy %d", src->busy_cmd);
4707 gst_rtspsrc_connection_flush (src, TRUE);
4710 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %d", src->busy_cmd);
4713 gst_task_start (src->task);
4714 GST_OBJECT_UNLOCK (src);
4720 gst_rtspsrc_loop (GstRTSPSrc * src)
4724 if (!src->conninfo.connection || !src->conninfo.connected)
4727 if (src->interleaved)
4728 ret = gst_rtspsrc_loop_interleaved (src);
4730 ret = gst_rtspsrc_loop_udp (src);
4732 if (ret != GST_FLOW_OK)
4740 GST_WARNING_OBJECT (src, "we are not connected");
4741 ret = GST_FLOW_FLUSHING;
4746 const gchar *reason = gst_flow_get_name (ret);
4748 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
4749 src->running = FALSE;
4750 if (ret == GST_FLOW_EOS) {
4751 /* perform EOS logic */
4752 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4753 gst_element_post_message (GST_ELEMENT_CAST (src),
4754 gst_message_new_segment_done (GST_OBJECT_CAST (src),
4755 src->segment.format, src->segment.position));
4756 gst_rtspsrc_push_event (src,
4757 gst_event_new_segment_done (src->segment.format,
4758 src->segment.position));
4760 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4762 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
4763 /* for fatal errors we post an error message, post the error before the
4764 * EOS so the app knows about the error first. */
4765 GST_ELEMENT_ERROR (src, STREAM, FAILED,
4766 ("Internal data flow error."),
4767 ("streaming task paused, reason %s (%d)", reason, ret));
4768 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4770 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
4775 #ifndef GST_DISABLE_GST_DEBUG
4776 static const gchar *
4777 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
4781 while (method != 0) {
4798 static const gchar *
4799 gst_rtspsrc_skip_lws (const gchar * s)
4801 while (g_ascii_isspace (*s))
4806 static const gchar *
4807 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
4809 while (s > start && g_ascii_isspace (*(s - 1)))
4814 static const gchar *
4815 gst_rtspsrc_skip_commas (const gchar * s)
4817 /* The grammar allows for multiple commas */
4818 while (g_ascii_isspace (*s) || *s == ',')
4823 static const gchar *
4824 gst_rtspsrc_skip_item (const gchar * s)
4826 gboolean quoted = FALSE;
4827 const gchar *start = s;
4829 /* A list item ends at the last non-whitespace character
4830 * before a comma which is not inside a quoted-string. Or at
4831 * the end of the string.
4837 if (*s == '\\' && *(s + 1))
4846 return gst_rtspsrc_unskip_lws (s, start);
4850 gst_rtsp_decode_quoted_string (gchar * quoted_string)
4854 src = quoted_string + 1;
4855 dst = quoted_string;
4856 while (*src && *src != '"') {
4857 if (*src == '\\' && *(src + 1))
4864 /* Extract the authentication tokens that the server provided for each method
4865 * into an array of structures and give those to the connection object.
4868 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
4869 const gchar * header, gboolean * stale)
4871 GSList *list = NULL, *iter;
4873 gchar *item, *eq, *name_end, *value;
4875 g_return_if_fail (stale != NULL);
4877 gst_rtsp_connection_clear_auth_params (conn);
4880 /* Parse a header whose content is described by RFC2616 as
4881 * "#something", where "something" does not itself contain commas,
4882 * except as part of quoted-strings, into a list of allocated strings.
4884 header = gst_rtspsrc_skip_commas (header);
4886 end = gst_rtspsrc_skip_item (header);
4887 list = g_slist_prepend (list, g_strndup (header, end - header));
4888 header = gst_rtspsrc_skip_commas (end);
4893 list = g_slist_reverse (list);
4894 for (iter = list; iter; iter = iter->next) {
4897 eq = strchr (item, '=');
4899 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
4900 if (name_end == item) {
4901 /* That's no good... */
4908 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
4910 gst_rtsp_decode_quoted_string (value);
4914 if (item && (strcmp (item, "stale") == 0) &&
4915 value && (strcmp (value, "TRUE") == 0))
4917 gst_rtsp_connection_set_auth_param (conn, item, value);
4921 g_slist_free (list);
4924 /* Parse a WWW-Authenticate Response header and determine the
4925 * available authentication methods
4927 * This code should also cope with the fact that each WWW-Authenticate
4928 * header can contain multiple challenge methods + tokens
4930 * At the moment, for Basic auth, we just do a minimal check and don't
4931 * even parse out the realm */
4933 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
4934 GstRTSPConnection * conn, gboolean * stale)
4938 g_return_if_fail (hdr != NULL);
4939 g_return_if_fail (methods != NULL);
4940 g_return_if_fail (stale != NULL);
4942 /* Skip whitespace at the start of the string */
4943 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
4945 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
4946 *methods |= GST_RTSP_AUTH_BASIC;
4947 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
4948 *methods |= GST_RTSP_AUTH_DIGEST;
4949 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
4954 * gst_rtspsrc_setup_auth:
4955 * @src: the rtsp source
4957 * Configure a username and password and auth method on the
4958 * connection object based on a response we received from the
4961 * Currently, this requires that a username and password were supplied
4962 * in the uri. In the future, they may be requested on demand by sending
4963 * a message up the bus.
4965 * Returns: TRUE if authentication information could be set up correctly.
4968 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
4972 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
4973 GstRTSPAuthMethod method;
4974 GstRTSPResult auth_result;
4976 GstRTSPConnection *conn;
4978 gboolean stale = FALSE;
4980 conn = src->conninfo.connection;
4982 /* Identify the available auth methods and see if any are supported */
4983 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
4984 &hdr, 0) == GST_RTSP_OK) {
4985 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
4988 if (avail_methods == GST_RTSP_AUTH_NONE)
4989 goto no_auth_available;
4991 /* For digest auth, if the response indicates that the session
4992 * data are stale, we just update them in the connection object and
4993 * return TRUE to retry the request */
4995 src->tried_url_auth = FALSE;
4997 url = gst_rtsp_connection_get_url (conn);
4999 /* Do we have username and password available? */
5000 if (url != NULL && !src->tried_url_auth && url->user != NULL
5001 && url->passwd != NULL) {
5004 src->tried_url_auth = TRUE;
5005 GST_DEBUG_OBJECT (src,
5006 "Attempting authentication using credentials from the URL");
5008 user = src->user_id;
5009 pass = src->user_pw;
5010 GST_DEBUG_OBJECT (src,
5011 "Attempting authentication using credentials from the properties");
5014 /* FIXME: If the url didn't contain username and password or we tried them
5015 * already, request a username and passwd from the application via some kind
5016 * of credentials request message */
5018 /* If we don't have a username and passwd at this point, bail out. */
5019 if (user == NULL || pass == NULL)
5022 /* Try to configure for each available authentication method, strongest to
5024 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
5025 /* Check if this method is available on the server */
5026 if ((method & avail_methods) == 0)
5029 /* Pass the credentials to the connection to try on the next request */
5030 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
5031 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
5032 * ignore it and end up retrying later */
5033 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
5034 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
5035 gst_rtsp_auth_method_to_string (method));
5040 if (method == GST_RTSP_AUTH_NONE)
5041 goto no_auth_available;
5047 /* Output an error indicating that we couldn't connect because there were
5048 * no supported authentication protocols */
5049 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5050 ("No supported authentication protocol was found"));
5055 /* We don't fire an error message, we just return FALSE and let the
5056 * normal NOT_AUTHORIZED error be propagated */
5061 static GstRTSPResult
5062 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5063 GstRTSPMessage * request, GstRTSPMessage * response,
5064 GstRTSPStatusCode * code)
5067 GstRTSPStatusCode thecode;
5068 gchar *content_base = NULL;
5072 if (!src->short_header)
5073 gst_rtsp_ext_list_before_send (src->extensions, request);
5075 GST_DEBUG_OBJECT (src, "sending message");
5078 gst_rtsp_message_dump (request);
5080 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
5084 gst_rtsp_connection_reset_timeout (conn);
5087 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
5092 gst_rtsp_message_dump (response);
5094 switch (response->type) {
5095 case GST_RTSP_MESSAGE_REQUEST:
5096 res = gst_rtspsrc_handle_request (src, conn, response);
5097 if (res == GST_RTSP_EEOF)
5100 goto handle_request_failed;
5102 case GST_RTSP_MESSAGE_RESPONSE:
5103 /* ok, a response is good */
5104 GST_DEBUG_OBJECT (src, "received response message");
5106 case GST_RTSP_MESSAGE_DATA:
5107 /* get next response */
5108 GST_DEBUG_OBJECT (src, "handle data response message");
5109 gst_rtspsrc_handle_data (src, response);
5112 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5117 thecode = response->type_data.response.code;
5119 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
5121 /* if the caller wanted the result code, we store it. */
5125 /* If the request didn't succeed, bail out before doing any more */
5126 if (thecode != GST_RTSP_STS_OK)
5129 /* store new content base if any */
5130 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
5133 g_free (src->content_base);
5134 src->content_base = g_strdup (content_base);
5136 gst_rtsp_ext_list_after_send (src->extensions, request, response);
5143 gchar *str = gst_rtsp_strresult (res);
5145 if (res != GST_RTSP_EINTR) {
5146 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5147 ("Could not send message. (%s)", str));
5149 GST_WARNING_OBJECT (src, "send interrupted");
5158 GST_WARNING_OBJECT (src, "server closed connection");
5159 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
5161 /* if reconnect succeeds, try again */
5163 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
5167 /* only try once after reconnect, then fallthrough and error out */
5170 gchar *str = gst_rtsp_strresult (res);
5172 if (res != GST_RTSP_EINTR) {
5173 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5174 ("Could not receive message. (%s)", str));
5176 GST_WARNING_OBJECT (src, "receive interrupted");
5184 handle_request_failed:
5186 /* ERROR was posted */
5187 gst_rtsp_message_unset (response);
5192 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5193 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5194 ("The server closed the connection."));
5195 gst_rtsp_message_unset (response);
5202 * @src: the rtsp source
5203 * @conn: the connection to send on
5204 * @request: must point to a valid request
5205 * @response: must point to an empty #GstRTSPMessage
5206 * @code: an optional code result
5208 * send @request and retrieve the response in @response. optionally @code can be
5209 * non-NULL in which case it will contain the status code of the response.
5211 * If This function returns #GST_RTSP_OK, @response will contain a valid response
5212 * message that should be cleaned with gst_rtsp_message_unset() after usage.
5214 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
5215 * @response message) if the response code was not 200 (OK).
5217 * If the attempt results in an authentication failure, then this will attempt
5218 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
5221 * Returns: #GST_RTSP_OK if the processing was successful.
5223 static GstRTSPResult
5224 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5225 GstRTSPMessage * request, GstRTSPMessage * response,
5226 GstRTSPStatusCode * code)
5228 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
5229 GstRTSPResult res = GST_RTSP_ERROR;
5232 GstRTSPMethod method = GST_RTSP_INVALID;
5238 /* make sure we don't loop forever */
5242 /* save method so we can disable it when the server complains */
5243 method = request->type_data.request.method;
5246 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
5250 case GST_RTSP_STS_UNAUTHORIZED:
5251 if (gst_rtspsrc_setup_auth (src, response)) {
5252 /* Try the request/response again after configuring the auth info
5260 } while (retry == TRUE);
5262 /* If the user requested the code, let them handle errors, otherwise
5263 * post an error below */
5266 else if (int_code != GST_RTSP_STS_OK)
5267 goto error_response;
5274 GST_DEBUG_OBJECT (src, "got error %d", res);
5279 res = GST_RTSP_ERROR;
5281 switch (response->type_data.response.code) {
5282 case GST_RTSP_STS_NOT_FOUND:
5283 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
5284 response->type_data.response.reason));
5286 case GST_RTSP_STS_MOVED_PERMANENTLY:
5287 case GST_RTSP_STS_MOVE_TEMPORARILY:
5289 gchar *new_location;
5290 GstRTSPLowerTrans transports;
5292 GST_DEBUG_OBJECT (src, "got redirection");
5293 /* if we don't have a Location Header, we must error */
5294 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
5295 &new_location, 0) < 0)
5298 /* When we receive a redirect result, we go back to the INIT state after
5299 * parsing the new URI. The caller should do the needed steps to issue
5300 * a new setup when it detects this state change. */
5301 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
5303 /* save current transports */
5304 if (src->conninfo.url)
5305 transports = src->conninfo.url->transports;
5307 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
5309 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
5311 /* set old transports */
5312 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
5313 src->conninfo.url->transports = transports;
5315 src->need_redirect = TRUE;
5316 src->state = GST_RTSP_STATE_INIT;
5320 case GST_RTSP_STS_NOT_ACCEPTABLE:
5321 case GST_RTSP_STS_NOT_IMPLEMENTED:
5322 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
5323 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
5324 gst_rtsp_method_as_text (method));
5325 src->methods &= ~method;
5329 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5330 ("Got error response: %d (%s).", response->type_data.response.code,
5331 response->type_data.response.reason));
5334 /* if we return ERROR we should unset the response ourselves */
5335 if (res == GST_RTSP_ERROR)
5336 gst_rtsp_message_unset (response);
5342 static GstRTSPResult
5343 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
5344 GstRTSPMessage * response, GstRTSPSrc * src)
5346 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
5351 /* parse the response and collect all the supported methods. We need this
5352 * information so that we don't try to send an unsupported request to the
5356 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
5358 GstRTSPHeaderField field;
5362 /* reset supported methods */
5365 /* Try Allow Header first */
5366 field = GST_RTSP_HDR_ALLOW;
5369 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5370 if (indx == 0 && !respoptions) {
5371 /* if no Allow header was found then try the Public header... */
5372 field = GST_RTSP_HDR_PUBLIC;
5373 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5378 src->methods |= gst_rtsp_options_from_text (respoptions);
5383 if (src->methods == 0) {
5384 /* neither Allow nor Public are required, assume the server supports
5385 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
5387 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
5388 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
5390 /* always assume PLAY, FIXME, extensions should be able to override
5392 src->methods |= GST_RTSP_PLAY;
5393 /* also assume it will support Range */
5394 src->seekable = TRUE;
5396 /* we need describe and setup */
5397 if (!(src->methods & GST_RTSP_DESCRIBE))
5399 if (!(src->methods & GST_RTSP_SETUP))
5407 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5408 ("Server does not support DESCRIBE."));
5413 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5414 ("Server does not support SETUP."));
5419 /* masks to be kept in sync with the hardcoded protocol order of preference
5421 static guint protocol_masks[] = {
5422 GST_RTSP_LOWER_TRANS_UDP,
5423 GST_RTSP_LOWER_TRANS_UDP_MCAST,
5424 GST_RTSP_LOWER_TRANS_TCP,
5428 static GstRTSPResult
5429 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
5430 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
5434 gboolean add_udp_str;
5439 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
5444 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
5446 /* extension listed transports, use those */
5447 if (*transports != NULL)
5450 /* it's the default */
5451 add_udp_str = FALSE;
5453 /* the default RTSP transports */
5454 result = g_string_new ("RTP");
5457 case GST_RTSP_PROFILE_AVP:
5458 g_string_append (result, "/AVP");
5460 case GST_RTSP_PROFILE_SAVP:
5461 g_string_append (result, "/SAVP");
5463 case GST_RTSP_PROFILE_AVPF:
5464 g_string_append (result, "/AVPF");
5466 case GST_RTSP_PROFILE_SAVPF:
5467 g_string_append (result, "/SAVPF");
5473 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
5474 GST_DEBUG_OBJECT (src, "adding UDP unicast");
5476 g_string_append (result, "/UDP");
5477 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
5478 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
5479 GST_DEBUG_OBJECT (src, "adding UDP multicast");
5480 /* we don't have to allocate any UDP ports yet, if the selected transport
5481 * turns out to be multicast we can create them and join the multicast
5482 * group indicated in the transport reply */
5484 g_string_append (result, "/UDP");
5485 g_string_append (result, ";multicast");
5486 if (src->next_port_num != 0) {
5487 if (src->client_port_range.max > 0 &&
5488 src->next_port_num >= src->client_port_range.max)
5491 g_string_append_printf (result, ";client_port=%d-%d",
5492 src->next_port_num, src->next_port_num + 1);
5494 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
5495 GST_DEBUG_OBJECT (src, "adding TCP");
5497 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
5499 *transports = g_string_free (result, FALSE);
5501 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
5508 GST_ERROR ("extension gave error %d", res);
5513 GST_ERROR ("no more ports available");
5514 return GST_RTSP_ERROR;
5518 static GstRTSPResult
5519 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
5520 gint orig_rtpport, gint orig_rtcpport)
5523 gint nr_udp, nr_int;
5525 gint rtpport = 0, rtcpport = 0;
5528 src = stream->parent;
5530 /* find number of placeholders first */
5531 if (strstr (*transports, "%%i2"))
5533 else if (strstr (*transports, "%%i1"))
5538 if (strstr (*transports, "%%u2"))
5540 else if (strstr (*transports, "%%u1"))
5545 if (nr_udp == 0 && nr_int == 0)
5549 if (!orig_rtpport || !orig_rtcpport) {
5550 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
5553 rtpport = orig_rtpport;
5554 rtcpport = orig_rtcpport;
5558 str = g_string_new ("");
5560 while ((next = strstr (p, "%%"))) {
5561 g_string_append_len (str, p, next - p);
5562 if (next[2] == 'u') {
5564 g_string_append_printf (str, "%d", rtpport);
5565 else if (next[3] == '2')
5566 g_string_append_printf (str, "%d", rtcpport);
5568 if (next[2] == 'i') {
5570 g_string_append_printf (str, "%d", src->free_channel);
5571 else if (next[3] == '2')
5572 g_string_append_printf (str, "%d", src->free_channel + 1);
5577 /* append final part */
5578 g_string_append (str, p);
5580 g_free (*transports);
5581 *transports = g_string_free (str, FALSE);
5589 GST_ERROR ("failed to allocate udp ports");
5590 return GST_RTSP_ERROR;
5594 /* Perform the SETUP request for all the streams.
5596 * We ask the server for a specific transport, which initially includes all the
5597 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
5598 * two local UDP ports that we send to the server.
5600 * Once the server replied with a transport, we configure the other streams
5601 * with the same transport.
5603 * This function will also configure the stream for the selected transport,
5604 * which basically means creating the pipeline.
5606 static GstRTSPResult
5607 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
5610 GstRTSPResult res = GST_RTSP_ERROR;
5611 GstRTSPMessage request = { 0 };
5612 GstRTSPMessage response = { 0 };
5613 GstRTSPStream *stream = NULL;
5614 GstRTSPLowerTrans protocols;
5615 GstRTSPStatusCode code;
5616 gboolean unsupported_real = FALSE;
5617 gint rtpport, rtcpport;
5621 if (src->conninfo.connection) {
5622 url = gst_rtsp_connection_get_url (src->conninfo.connection);
5623 /* we initially allow all configured lower transports. based on the URL
5624 * transports and the replies from the server we narrow them down. */
5625 protocols = url->transports & src->cur_protocols;
5628 protocols = src->cur_protocols;
5634 /* reset some state */
5635 src->free_channel = 0;
5636 src->interleaved = FALSE;
5637 src->need_activate = FALSE;
5638 /* keep track of next port number, 0 is random */
5639 src->next_port_num = src->client_port_range.min;
5640 rtpport = rtcpport = 0;
5642 if (G_UNLIKELY (src->streams == NULL))
5645 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5646 GstRTSPConnection *conn;
5653 stream = (GstRTSPStream *) walk->data;
5655 caps = stream_get_caps_for_pt (stream, stream->default_pt);
5657 GST_DEBUG_OBJECT (src, "skipping stream %p, no caps", stream);
5661 if (stream->skipped) {
5662 GST_DEBUG_OBJECT (src, "skipping stream %p", stream);
5666 /* see if we need to configure this stream */
5667 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
5668 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
5673 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
5674 stream->id, caps, &selected);
5676 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
5680 /* merge/overwrite global caps */
5685 s = gst_caps_get_structure (caps, 0);
5687 num = gst_structure_n_fields (src->props);
5688 for (j = 0; j < num; j++) {
5692 name = gst_structure_nth_field_name (src->props, j);
5693 val = gst_structure_get_value (src->props, name);
5694 gst_structure_set_value (s, name, val);
5696 GST_DEBUG_OBJECT (src, "copied %s", name);
5700 /* skip setup if we have no URL for it */
5701 if (stream->conninfo.location == NULL) {
5702 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
5706 if (src->conninfo.connection == NULL) {
5707 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
5708 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
5711 conn = stream->conninfo.connection;
5713 conn = src->conninfo.connection;
5715 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
5716 stream->conninfo.location);
5718 /* if we have a multicast connection, only suggest multicast from now on */
5719 if (stream->is_multicast)
5720 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
5723 /* first selectable protocol */
5724 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5726 if (!protocol_masks[mask])
5730 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
5731 protocol_masks[mask]);
5732 /* create a string with first transport in line */
5734 res = gst_rtspsrc_create_transports_string (src,
5735 protocols & protocol_masks[mask], stream->profile, &transports);
5736 if (res < 0 || transports == NULL)
5737 goto setup_transport_failed;
5739 if (strlen (transports) == 0) {
5740 g_free (transports);
5741 GST_DEBUG_OBJECT (src, "no transports found");
5746 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
5748 /* replace placeholders with real values, this function will optionally
5749 * allocate UDP ports and other info needed to execute the setup request */
5750 res = gst_rtspsrc_prepare_transports (stream, &transports,
5751 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
5753 g_free (transports);
5754 goto setup_transport_failed;
5757 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
5759 /* create SETUP request */
5761 gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
5762 stream->conninfo.location);
5764 g_free (transports);
5765 goto create_request_failed;
5768 /* select transport */
5769 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
5771 /* if the user wants a non default RTP packet size we add the blocksize
5773 if (src->rtp_blocksize > 0) {
5774 hval = g_strdup_printf ("%d", src->rtp_blocksize);
5775 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
5779 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
5782 /* handle the code ourselves */
5783 if ((res = gst_rtspsrc_send (src, conn, &request, &response, &code) < 0))
5787 case GST_RTSP_STS_OK:
5789 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
5790 gst_rtsp_message_unset (&request);
5791 gst_rtsp_message_unset (&response);
5792 /* cleanup of leftover transport */
5793 gst_rtspsrc_stream_free_udp (stream);
5794 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
5795 * we might be in this case */
5796 if (stream->container && rtpport && rtcpport && !retry) {
5797 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
5802 /* this transport did not go down well, but we may have others to try
5803 * that we did not send yet, try those and only give up then
5804 * but not without checking for lost cause/extension so we can
5805 * post a nicer/more useful error message later */
5806 if (!unsupported_real)
5807 unsupported_real = stream->is_real;
5808 /* select next available protocol, give up on this stream if none */
5810 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5812 if (!protocol_masks[mask] || unsupported_real)
5817 /* cleanup of leftover transport and move to the next stream */
5818 gst_rtspsrc_stream_free_udp (stream);
5819 goto response_error;
5822 /* parse response transport */
5824 gchar *resptrans = NULL;
5825 GstRTSPTransport transport = { 0 };
5827 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
5830 gst_rtspsrc_stream_free_udp (stream);
5834 /* parse transport, go to next stream on parse error */
5835 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
5836 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
5840 /* update allowed transports for other streams. once the transport of
5841 * one stream has been determined, we make sure that all other streams
5842 * are configured in the same way */
5843 switch (transport.lower_transport) {
5844 case GST_RTSP_LOWER_TRANS_TCP:
5845 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
5846 protocols = GST_RTSP_LOWER_TRANS_TCP;
5847 src->interleaved = TRUE;
5848 /* update free channels */
5850 MAX (transport.interleaved.min, src->free_channel);
5852 MAX (transport.interleaved.max, src->free_channel);
5853 src->free_channel++;
5855 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
5856 /* only allow multicast for other streams */
5857 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
5858 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
5859 /* if the server selected our ports, increment our counters so that
5860 * we select a new port later */
5861 if (src->next_port_num == transport.port.min &&
5862 src->next_port_num + 1 == transport.port.max) {
5863 src->next_port_num += 2;
5866 case GST_RTSP_LOWER_TRANS_UDP:
5867 /* only allow unicast for other streams */
5868 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
5869 protocols = GST_RTSP_LOWER_TRANS_UDP;
5872 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
5873 transport.lower_transport);
5877 if (!stream->container || (!src->interleaved && !retry)) {
5878 /* now configure the stream with the selected transport */
5879 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
5880 GST_DEBUG_OBJECT (src,
5881 "could not configure stream %p transport, skipping stream",
5884 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
5885 /* retain the first allocated UDP port pair */
5886 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
5887 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
5890 /* we need to activate at least one streams when we detect activity */
5891 src->need_activate = TRUE;
5893 /* stream is setup now */
5894 stream->setup = TRUE;
5899 GstRTSPStream *sskip;
5901 skip = g_list_next (skip);
5905 sskip = (GstRTSPStream *) skip->data;
5907 /* skip all streams with the same control url */
5908 if (g_str_equal (stream->conninfo.location, sskip->conninfo.location)) {
5909 GST_DEBUG_OBJECT (src, "found stream %p with same control %s",
5910 sskip, sskip->conninfo.location);
5911 sskip->skipped = TRUE;
5916 /* clean up our transport struct */
5917 gst_rtsp_transport_init (&transport);
5918 /* clean up used RTSP messages */
5919 gst_rtsp_message_unset (&request);
5920 gst_rtsp_message_unset (&response);
5924 /* store the transport protocol that was configured */
5925 src->cur_protocols = protocols;
5927 gst_rtsp_ext_list_stream_select (src->extensions, url);
5929 /* if there is nothing to activate, error out */
5930 if (!src->need_activate)
5931 goto nothing_to_activate;
5938 /* no transport possible, post an error and stop */
5939 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5940 ("Could not connect to server, no protocols left"));
5941 return GST_RTSP_ERROR;
5945 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5946 ("SDP contains no streams"));
5947 return GST_RTSP_ERROR;
5949 create_request_failed:
5951 gchar *str = gst_rtsp_strresult (res);
5953 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5954 ("Could not create request. (%s)", str));
5958 setup_transport_failed:
5960 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5961 ("Could not setup transport."));
5962 res = GST_RTSP_ERROR;
5967 const gchar *str = gst_rtsp_status_as_text (code);
5969 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5970 ("Error (%d): %s", code, GST_STR_NULL (str)));
5971 res = GST_RTSP_ERROR;
5976 gchar *str = gst_rtsp_strresult (res);
5978 if (res != GST_RTSP_EINTR) {
5979 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5980 ("Could not send message. (%s)", str));
5982 GST_WARNING_OBJECT (src, "send interrupted");
5989 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5990 ("Server did not select transport."));
5991 res = GST_RTSP_ERROR;
5994 nothing_to_activate:
5996 /* none of the available error codes is really right .. */
5997 if (unsupported_real) {
5998 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5999 (_("No supported stream was found. You might need to install a "
6000 "GStreamer RTSP extension plugin for Real media streams.")),
6003 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6004 (_("No supported stream was found. You might need to allow "
6005 "more transport protocols or may otherwise be missing "
6006 "the right GStreamer RTSP extension plugin.")), (NULL));
6008 return GST_RTSP_ERROR;
6012 gst_rtsp_message_unset (&request);
6013 gst_rtsp_message_unset (&response);
6019 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
6020 GstSegment * segment)
6023 GstRTSPTimeRange *therange;
6026 gst_rtsp_range_free (src->range);
6028 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
6029 GST_DEBUG_OBJECT (src, "parsed range %s", range);
6030 src->range = therange;
6032 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
6034 gst_segment_init (segment, GST_FORMAT_TIME);
6038 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
6039 therange->min.type, therange->min.seconds, therange->max.type,
6040 therange->max.seconds);
6042 if (therange->min.type == GST_RTSP_TIME_NOW)
6044 else if (therange->min.type == GST_RTSP_TIME_END)
6047 seconds = therange->min.seconds * GST_SECOND;
6049 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
6050 GST_TIME_ARGS (seconds));
6052 /* we need to start playback without clipping from the position reported by
6054 segment->start = seconds;
6055 segment->position = seconds;
6057 if (therange->max.type == GST_RTSP_TIME_NOW)
6059 else if (therange->max.type == GST_RTSP_TIME_END)
6062 seconds = therange->max.seconds * GST_SECOND;
6064 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
6065 GST_TIME_ARGS (seconds));
6067 /* live (WMS) server might send overflowed large max as its idea of infinity,
6068 * compensate to prevent problems later on */
6069 if (seconds != -1 && seconds < 0) {
6071 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
6074 /* live (WMS) might send min == max, which is not worth recording */
6075 if (segment->duration == -1 && seconds == segment->start)
6078 /* don't change duration with unknown value, we might have a valid value
6079 * there that we want to keep. */
6081 segment->duration = seconds;
6086 /* Parse clock profived by the server with following syntax:
6088 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
6091 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
6093 gboolean res = FALSE;
6095 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
6096 gchar **fields = NULL, **parts = NULL;
6097 gchar *remote_ip, *str;
6099 GstClockTime base_time;
6102 fields = g_strsplit (gstclock, " ", 0);
6104 /* wrapped clock, not very interesting for now */
6105 if (fields[1] == NULL)
6108 /* remote IP address and port */
6109 if ((str = fields[2]) == NULL)
6112 parts = g_strsplit (str, ":", 0);
6114 if ((remote_ip = parts[0]) == NULL)
6117 if ((str = parts[1]) == NULL)
6125 if ((str = fields[3]) == NULL)
6128 base_time = g_ascii_strtoull (str, NULL, 10);
6131 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
6134 if (src->provided_clock)
6135 gst_object_unref (src->provided_clock);
6136 src->provided_clock = netclock;
6138 gst_element_post_message (GST_ELEMENT_CAST (src),
6139 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
6140 src->provided_clock, TRUE));
6144 g_strfreev (fields);
6150 /* must be called with the RTSP state lock */
6151 static GstRTSPResult
6152 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
6158 /* prepare global stream caps properties */
6160 gst_structure_remove_all_fields (src->props);
6162 src->props = gst_structure_new_empty ("RTSPProperties");
6165 gst_sdp_message_dump (sdp);
6167 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
6169 /* let the app inspect and change the SDP */
6170 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
6172 gst_segment_init (&src->segment, GST_FORMAT_TIME);
6174 /* parse range for duration reporting. */
6179 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
6183 /* keep track of the range and configure it in the segment */
6184 if (gst_rtspsrc_parse_range (src, range, &src->segment))
6188 /* parse clock information. This is GStreamer specific, a server can tell the
6189 * client what clock it is using and wrap that in a network clock. The
6190 * advantage of that is that we can slave to it. */
6192 const gchar *gstclock;
6195 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
6196 if (gstclock == NULL)
6199 /* parse the clock and expose it in the provide_clock method */
6200 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
6204 /* try to find a global control attribute. Note that a '*' means that we should
6205 * do aggregate control with the current url (so we don't do anything and
6206 * leave the current connection as is) */
6208 const gchar *control;
6211 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
6212 if (control == NULL)
6215 /* only take fully qualified urls */
6216 if (g_str_has_prefix (control, "rtsp://"))
6220 g_free (src->conninfo.location);
6221 src->conninfo.location = g_strdup (control);
6222 /* make a connection for this, if there was a connection already, nothing
6224 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
6225 GST_ERROR_OBJECT (src, "could not connect");
6228 /* we need to keep the control url separate from the connection url because
6229 * the rules for constructing the media control url need it */
6230 g_free (src->control);
6231 src->control = g_strdup (control);
6234 /* create streams */
6235 n_streams = gst_sdp_message_medias_len (sdp);
6236 for (i = 0; i < n_streams; i++) {
6237 gst_rtspsrc_create_stream (src, sdp, i);
6240 src->state = GST_RTSP_STATE_INIT;
6243 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
6246 /* reset our state */
6247 src->need_range = TRUE;
6250 src->state = GST_RTSP_STATE_READY;
6257 GST_ERROR_OBJECT (src, "setup failed");
6258 gst_rtspsrc_cleanup (src);
6263 static GstRTSPResult
6264 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
6268 GstRTSPMessage request = { 0 };
6269 GstRTSPMessage response = { 0 };
6272 gchar *respcont = NULL;
6275 src->need_redirect = FALSE;
6277 /* can't continue without a valid url */
6278 if (G_UNLIKELY (src->conninfo.url == NULL)) {
6279 res = GST_RTSP_EINVAL;
6282 src->tried_url_auth = FALSE;
6284 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
6285 goto connect_failed;
6287 /* create OPTIONS */
6288 GST_DEBUG_OBJECT (src, "create options...");
6290 gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
6291 src->conninfo.url_str);
6293 goto create_request_failed;
6296 GST_DEBUG_OBJECT (src, "send options...");
6299 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
6302 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6307 if (!gst_rtspsrc_parse_methods (src, &response))
6310 /* create DESCRIBE */
6311 GST_DEBUG_OBJECT (src, "create describe...");
6313 gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
6314 src->conninfo.url_str);
6316 goto create_request_failed;
6318 /* we only accept SDP for now */
6319 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
6323 GST_DEBUG_OBJECT (src, "send describe...");
6326 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
6329 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6333 /* we only perform redirect for the describe, currently */
6334 if (src->need_redirect) {
6335 /* close connection, we don't have to send a TEARDOWN yet, ignore the
6337 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6339 gst_rtsp_message_unset (&request);
6340 gst_rtsp_message_unset (&response);
6346 /* it could be that the DESCRIBE method was not implemented */
6347 if (!src->methods & GST_RTSP_DESCRIBE)
6350 /* check if reply is SDP */
6351 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
6353 /* could not be set but since the request returned OK, we assume it
6354 * was SDP, else check it. */
6356 if (!g_ascii_strcasecmp (respcont, "application/sdp") == 0)
6357 goto wrong_content_type;
6360 /* get message body and parse as SDP */
6361 gst_rtsp_message_get_body (&response, &data, &size);
6362 if (data == NULL || size == 0)
6365 GST_DEBUG_OBJECT (src, "parse SDP...");
6366 gst_sdp_message_new (sdp);
6367 gst_sdp_message_parse_buffer (data, size, *sdp);
6369 /* clean up any messages */
6370 gst_rtsp_message_unset (&request);
6371 gst_rtsp_message_unset (&response);
6378 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
6379 ("No valid RTSP URL was provided"));
6384 gchar *str = gst_rtsp_strresult (res);
6386 if (res != GST_RTSP_EINTR) {
6387 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
6388 ("Failed to connect. (%s)", str));
6390 GST_WARNING_OBJECT (src, "connect interrupted");
6395 create_request_failed:
6397 gchar *str = gst_rtsp_strresult (res);
6399 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6400 ("Could not create request. (%s)", str));
6406 /* Don't post a message - the rtsp_send method will have
6407 * taken care of it because we passed NULL for the response code */
6412 /* error was posted */
6413 res = GST_RTSP_ERROR;
6418 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6419 ("Server does not support SDP, got %s.", respcont));
6420 res = GST_RTSP_ERROR;
6425 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6426 ("Server can not provide an SDP."));
6427 res = GST_RTSP_ERROR;
6432 if (src->conninfo.connection) {
6433 GST_DEBUG_OBJECT (src, "free connection");
6434 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6436 gst_rtsp_message_unset (&request);
6437 gst_rtsp_message_unset (&response);
6442 static GstRTSPResult
6443 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
6448 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
6450 if (src->sdp == NULL) {
6451 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
6455 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
6460 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
6467 GST_WARNING_OBJECT (src, "can't get sdp");
6468 src->open_error = TRUE;
6473 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
6474 src->open_error = TRUE;
6479 static GstRTSPResult
6480 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
6482 GstRTSPMessage request = { 0 };
6483 GstRTSPMessage response = { 0 };
6484 GstRTSPResult res = GST_RTSP_OK;
6486 const gchar *control;
6488 GST_DEBUG_OBJECT (src, "TEARDOWN...");
6490 gst_rtspsrc_set_state (src, GST_STATE_READY);
6492 if (src->state < GST_RTSP_STATE_READY) {
6493 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
6500 /* construct a control url */
6501 control = get_aggregate_control (src);
6503 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
6506 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6507 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6508 const gchar *setup_url;
6509 GstRTSPConnInfo *info;
6511 /* try aggregate control first but do non-aggregate control otherwise */
6513 setup_url = control;
6514 else if ((setup_url = stream->conninfo.location) == NULL)
6517 if (src->conninfo.connection) {
6518 info = &src->conninfo;
6519 } else if (stream->conninfo.connection) {
6520 info = &stream->conninfo;
6524 if (!info->connected)
6529 gst_rtsp_message_init_request (&request, GST_RTSP_TEARDOWN, setup_url);
6531 goto create_request_failed;
6534 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
6537 gst_rtspsrc_send (src, info->connection, &request, &response,
6541 /* FIXME, parse result? */
6542 gst_rtsp_message_unset (&request);
6543 gst_rtsp_message_unset (&response);
6546 /* early exit when we did aggregate control */
6552 /* close connections */
6553 GST_DEBUG_OBJECT (src, "closing connection...");
6554 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6555 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6556 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6557 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
6561 gst_rtspsrc_cleanup (src);
6563 src->state = GST_RTSP_STATE_INVALID;
6566 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
6571 create_request_failed:
6573 gchar *str = gst_rtsp_strresult (res);
6575 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6576 ("Could not create request. (%s)", str));
6582 gchar *str = gst_rtsp_strresult (res);
6584 gst_rtsp_message_unset (&request);
6585 if (res != GST_RTSP_EINTR) {
6586 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6587 ("Could not send message. (%s)", str));
6589 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
6596 GST_DEBUG_OBJECT (src,
6597 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
6602 /* RTP-Info is of the format:
6604 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
6606 * rtptime corresponds to the timestamp for the NPT time given in the header
6607 * seqbase corresponds to the next sequence number we received. This number
6608 * indicates the first seqnum after the seek and should be used to discard
6609 * packets that are from before the seek.
6612 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
6617 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
6619 infos = g_strsplit (rtpinfo, ",", 0);
6620 for (i = 0; infos[i]; i++) {
6622 GstRTSPStream *stream;
6626 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
6628 /* init values, types of seqbase and timebase are bigger than needed so we
6629 * can store -1 as uninitialized values */
6634 /* parse url, find stream for url.
6635 * parse seq and rtptime. The seq number should be configured in the rtp
6636 * depayloader or session manager to detect gaps. Same for the rtptime, it
6637 * should be used to create an initial time newsegment. */
6638 fields = g_strsplit (infos[i], ";", 0);
6639 for (j = 0; fields[j]; j++) {
6640 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
6641 /* remove leading whitespace */
6642 fields[j] = g_strchug (fields[j]);
6643 if (g_str_has_prefix (fields[j], "url=")) {
6644 /* get the url and the stream */
6646 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
6647 } else if (g_str_has_prefix (fields[j], "seq=")) {
6648 seqbase = atoi (fields[j] + 4);
6649 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
6650 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
6653 g_strfreev (fields);
6654 /* now we need to store the values for the caps of the stream */
6655 if (stream != NULL) {
6656 GST_DEBUG_OBJECT (src,
6657 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
6658 stream, seqbase, timebase);
6660 /* we have a stream, configure detected params */
6661 stream->seqbase = seqbase;
6662 stream->timebase = timebase;
6671 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
6676 interval = strtoul (rtcp, NULL, 10);
6677 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
6682 interval *= GST_MSECOND;
6684 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6685 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6687 /* already (optionally) retrieved this when configuring manager */
6688 if (stream->session) {
6689 GObject *rtpsession = stream->session;
6691 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
6693 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
6697 /* now it happens that (Xenon) server sending this may also provide bogus
6698 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
6699 * and just use RTP-Info to sync */
6701 GObjectClass *klass;
6703 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
6704 if (g_object_class_find_property (klass, "rtcp-sync")) {
6705 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
6706 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
6712 gst_rtspsrc_get_float (const gchar * dstr)
6714 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
6716 /* canonicalise floating point string so we can handle float strings
6717 * in the form "24.930" or "24,930" irrespective of the current locale */
6718 g_strlcpy (s, dstr, sizeof (s));
6719 g_strdelimit (s, ",", '.');
6720 return g_ascii_strtod (s, NULL);
6724 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
6726 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
6728 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
6729 g_strlcpy (val_str, "now", sizeof (val_str));
6731 if (segment->position == 0) {
6732 g_strlcpy (val_str, "0", sizeof (val_str));
6734 g_ascii_dtostr (val_str, sizeof (val_str),
6735 ((gdouble) segment->position) / GST_SECOND);
6738 return g_strdup_printf ("npt=%s-", val_str);
6742 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
6746 stream->timebase = -1;
6747 stream->seqbase = -1;
6749 len = stream->ptmap->len;
6750 for (i = 0; i < len; i++) {
6751 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
6754 if (item->caps == NULL)
6757 item->caps = gst_caps_make_writable (item->caps);
6758 s = gst_caps_get_structure (item->caps, 0);
6759 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
6763 static GstRTSPResult
6764 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
6766 GstRTSPResult res = GST_RTSP_OK;
6768 if (src->state < GST_RTSP_STATE_READY) {
6769 res = GST_RTSP_ERROR;
6770 if (src->open_error) {
6771 GST_DEBUG_OBJECT (src, "the stream was in error");
6775 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
6777 if ((res = gst_rtspsrc_open (src, async)) < 0) {
6778 GST_DEBUG_OBJECT (src, "failed to open stream");
6787 static GstRTSPResult
6788 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
6790 GstRTSPMessage request = { 0 };
6791 GstRTSPMessage response = { 0 };
6792 GstRTSPResult res = GST_RTSP_OK;
6796 const gchar *control;
6798 GST_DEBUG_OBJECT (src, "PLAY...");
6800 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6803 if (!(src->methods & GST_RTSP_PLAY))
6806 if (src->state == GST_RTSP_STATE_PLAYING)
6809 if (!src->conninfo.connection || !src->conninfo.connected)
6812 /* send some dummy packets before we activate the receive in the
6814 gst_rtspsrc_send_dummy_packets (src);
6816 /* require new SR packets */
6818 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
6820 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
6822 /* construct a control url */
6823 control = get_aggregate_control (src);
6825 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6826 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6827 const gchar *setup_url;
6828 GstRTSPConnection *conn;
6830 /* try aggregate control first but do non-aggregate control otherwise */
6832 setup_url = control;
6833 else if ((setup_url = stream->conninfo.location) == NULL)
6836 if (src->conninfo.connection) {
6837 conn = src->conninfo.connection;
6838 } else if (stream->conninfo.connection) {
6839 conn = stream->conninfo.connection;
6845 res = gst_rtsp_message_init_request (&request, GST_RTSP_PLAY, setup_url);
6847 goto create_request_failed;
6849 if (src->need_range) {
6850 hval = gen_range_header (src, segment);
6852 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
6854 /* store the newsegment event so it can be sent from the streaming thread. */
6855 if (src->start_segment)
6856 gst_event_unref (src->start_segment);
6857 src->start_segment = gst_event_new_segment (&src->segment);
6860 if (segment->rate != 1.0) {
6861 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
6863 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
6865 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
6867 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
6871 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
6873 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6876 /* seek may have silently failed as it is not supported */
6877 if (!(src->methods & GST_RTSP_PLAY)) {
6878 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
6879 /* obviously it is supported as we made it here */
6880 src->methods |= GST_RTSP_PLAY;
6881 src->seekable = FALSE;
6882 /* but there is nothing to parse in the response,
6883 * so convey we have no idea and not to expect anything particular */
6884 clear_rtp_base (src, stream);
6888 /* need to do for all streams */
6889 for (run = src->streams; run; run = g_list_next (run))
6890 clear_rtp_base (src, (GstRTSPStream *) run->data);
6892 /* NOTE the above also disables npt based eos detection */
6893 /* and below forces position to 0,
6894 * which is visible feedback we lost the plot */
6895 segment->start = segment->position = src->last_pos;
6898 gst_rtsp_message_unset (&request);
6900 /* parse RTP npt field. This is the current position in the stream (Normal
6901 * Play Time) and should be put in the NEWSEGMENT position field. */
6902 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
6904 gst_rtspsrc_parse_range (src, hval, segment);
6906 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
6907 segment->rate = 1.0;
6909 /* parse Speed header. This is the intended playback rate of the stream
6910 * and should be put in the NEWSEGMENT rate field. */
6911 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
6912 0) == GST_RTSP_OK) {
6913 segment->rate = gst_rtspsrc_get_float (hval);
6914 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
6915 &hval, 0) == GST_RTSP_OK) {
6916 segment->rate = gst_rtspsrc_get_float (hval);
6919 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
6920 * for the RTP packets. If this is not present, we assume all starts from 0...
6921 * This is info for the RTP session manager that we pass to it in caps. */
6923 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
6924 &hval, hval_idx++) == GST_RTSP_OK)
6925 gst_rtspsrc_parse_rtpinfo (src, hval);
6927 /* some servers indicate RTCP parameters in PLAY response,
6928 * rather than properly in SDP */
6929 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
6930 &hval, 0) == GST_RTSP_OK)
6931 gst_rtspsrc_handle_rtcp_interval (src, hval);
6933 gst_rtsp_message_unset (&response);
6935 /* early exit when we did aggregate control */
6939 /* configure the caps of the streams after we parsed all headers. Only reset
6940 * the manager object when we set a new Range header (we did a seek) */
6941 gst_rtspsrc_configure_caps (src, segment, src->need_range);
6943 /* set again when needed */
6944 src->need_range = FALSE;
6946 src->running = TRUE;
6947 src->base_time = -1;
6948 src->state = GST_RTSP_STATE_PLAYING;
6951 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
6952 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6953 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6954 stream->discont = TRUE;
6959 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
6966 GST_DEBUG_OBJECT (src, "failed to open stream");
6971 GST_DEBUG_OBJECT (src, "PLAY is not supported");
6976 GST_DEBUG_OBJECT (src, "we were already PLAYING");
6979 create_request_failed:
6981 gchar *str = gst_rtsp_strresult (res);
6983 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6984 ("Could not create request. (%s)", str));
6990 gchar *str = gst_rtsp_strresult (res);
6992 gst_rtsp_message_unset (&request);
6993 if (res != GST_RTSP_EINTR) {
6994 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6995 ("Could not send message. (%s)", str));
6997 GST_WARNING_OBJECT (src, "PLAY interrupted");
7004 static GstRTSPResult
7005 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
7007 GstRTSPResult res = GST_RTSP_OK;
7008 GstRTSPMessage request = { 0 };
7009 GstRTSPMessage response = { 0 };
7011 const gchar *control;
7013 GST_DEBUG_OBJECT (src, "PAUSE...");
7015 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7018 if (!(src->methods & GST_RTSP_PAUSE))
7021 if (src->state == GST_RTSP_STATE_READY)
7024 if (!src->conninfo.connection || !src->conninfo.connected)
7027 /* construct a control url */
7028 control = get_aggregate_control (src);
7030 /* loop over the streams. We might exit the loop early when we could do an
7031 * aggregate control */
7032 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7033 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7034 GstRTSPConnection *conn;
7035 const gchar *setup_url;
7037 /* try aggregate control first but do non-aggregate control otherwise */
7039 setup_url = control;
7040 else if ((setup_url = stream->conninfo.location) == NULL)
7043 if (src->conninfo.connection) {
7044 conn = src->conninfo.connection;
7045 } else if (stream->conninfo.connection) {
7046 conn = stream->conninfo.connection;
7052 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
7053 ("Sending PAUSE request"));
7056 gst_rtsp_message_init_request (&request, GST_RTSP_PAUSE,
7058 goto create_request_failed;
7060 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7063 gst_rtsp_message_unset (&request);
7064 gst_rtsp_message_unset (&response);
7066 /* exit early when we did agregate control */
7071 /* change element states now */
7072 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
7075 src->state = GST_RTSP_STATE_READY;
7079 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
7086 GST_DEBUG_OBJECT (src, "failed to open stream");
7091 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
7096 GST_DEBUG_OBJECT (src, "we were already PAUSED");
7099 create_request_failed:
7101 gchar *str = gst_rtsp_strresult (res);
7103 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7104 ("Could not create request. (%s)", str));
7110 gchar *str = gst_rtsp_strresult (res);
7112 gst_rtsp_message_unset (&request);
7113 if (res != GST_RTSP_EINTR) {
7114 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7115 ("Could not send message. (%s)", str));
7117 GST_WARNING_OBJECT (src, "PAUSE interrupted");
7125 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
7127 GstRTSPSrc *rtspsrc;
7129 rtspsrc = GST_RTSPSRC (bin);
7131 switch (GST_MESSAGE_TYPE (message)) {
7132 case GST_MESSAGE_EOS:
7133 gst_message_unref (message);
7135 case GST_MESSAGE_ELEMENT:
7137 const GstStructure *s = gst_message_get_structure (message);
7139 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
7140 gboolean ignore_timeout;
7142 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
7144 GST_OBJECT_LOCK (rtspsrc);
7145 ignore_timeout = rtspsrc->ignore_timeout;
7146 rtspsrc->ignore_timeout = TRUE;
7147 GST_OBJECT_UNLOCK (rtspsrc);
7149 /* we only act on the first udp timeout message, others are irrelevant
7150 * and can be ignored. */
7151 if (!ignore_timeout)
7152 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
7154 gst_message_unref (message);
7157 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7160 case GST_MESSAGE_ERROR:
7163 GstRTSPStream *stream;
7166 udpsrc = GST_MESSAGE_SRC (message);
7168 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
7169 GST_ELEMENT_NAME (udpsrc));
7171 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
7175 /* we ignore the RTCP udpsrc */
7176 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
7179 /* if we get error messages from the udp sources, that's not a problem as
7180 * long as not all of them error out. We also don't really know what the
7181 * problem is, the message does not give enough detail... */
7182 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
7183 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
7184 if (ret != GST_FLOW_OK)
7188 gst_message_unref (message);
7192 /* fatal but not our message, forward */
7193 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7198 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7204 /* the thread where everything happens */
7206 gst_rtspsrc_thread (GstRTSPSrc * src)
7210 GST_OBJECT_LOCK (src);
7211 cmd = src->pending_cmd;
7212 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
7213 || cmd == CMD_LOOP || cmd == CMD_OPEN)
7214 src->pending_cmd = CMD_LOOP;
7216 src->pending_cmd = CMD_WAIT;
7217 GST_DEBUG_OBJECT (src, "got command %d", cmd);
7219 /* we got the message command, so ensure communication is possible again */
7220 gst_rtspsrc_connection_flush (src, FALSE);
7222 src->busy_cmd = cmd;
7223 GST_OBJECT_UNLOCK (src);
7227 gst_rtspsrc_open (src, TRUE);
7230 gst_rtspsrc_play (src, &src->segment, TRUE);
7233 gst_rtspsrc_pause (src, TRUE);
7236 gst_rtspsrc_close (src, TRUE, FALSE);
7239 gst_rtspsrc_loop (src);
7242 gst_rtspsrc_reconnect (src, FALSE);
7248 GST_OBJECT_LOCK (src);
7249 /* and go back to sleep */
7250 if (src->pending_cmd == CMD_WAIT) {
7252 gst_task_pause (src->task);
7255 src->busy_cmd = CMD_WAIT;
7256 GST_OBJECT_UNLOCK (src);
7260 gst_rtspsrc_start (GstRTSPSrc * src)
7262 GST_DEBUG_OBJECT (src, "starting");
7264 GST_OBJECT_LOCK (src);
7266 src->pending_cmd = CMD_WAIT;
7268 if (src->task == NULL) {
7269 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
7270 if (src->task == NULL)
7273 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
7275 GST_OBJECT_UNLOCK (src);
7282 GST_OBJECT_UNLOCK (src);
7283 GST_ERROR_OBJECT (src, "failed to create task");
7289 gst_rtspsrc_stop (GstRTSPSrc * src)
7293 GST_DEBUG_OBJECT (src, "stopping");
7295 /* also cancels pending task */
7296 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
7298 GST_OBJECT_LOCK (src);
7299 if ((task = src->task)) {
7301 GST_OBJECT_UNLOCK (src);
7303 gst_task_stop (task);
7305 /* make sure it is not running */
7306 GST_RTSP_STREAM_LOCK (src);
7307 GST_RTSP_STREAM_UNLOCK (src);
7309 /* now wait for the task to finish */
7310 gst_task_join (task);
7312 /* and free the task */
7313 gst_object_unref (GST_OBJECT (task));
7315 GST_OBJECT_LOCK (src);
7317 GST_OBJECT_UNLOCK (src);
7319 /* ensure synchronously all is closed and clean */
7320 gst_rtspsrc_close (src, FALSE, TRUE);
7325 static GstStateChangeReturn
7326 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
7328 GstRTSPSrc *rtspsrc;
7329 GstStateChangeReturn ret;
7331 rtspsrc = GST_RTSPSRC (element);
7333 switch (transition) {
7334 case GST_STATE_CHANGE_NULL_TO_READY:
7335 if (!gst_rtspsrc_start (rtspsrc))
7338 case GST_STATE_CHANGE_READY_TO_PAUSED:
7339 /* init some state */
7340 rtspsrc->cur_protocols = rtspsrc->protocols;
7341 /* first attempt, don't ignore timeouts */
7342 rtspsrc->ignore_timeout = FALSE;
7343 rtspsrc->open_error = FALSE;
7344 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
7346 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7347 set_manager_buffer_mode (rtspsrc);
7349 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7350 /* unblock the tcp tasks and make the loop waiting */
7351 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
7352 /* make sure it is waiting before we send PAUSE or PLAY below */
7353 GST_RTSP_STREAM_LOCK (rtspsrc);
7354 GST_RTSP_STREAM_UNLOCK (rtspsrc);
7357 case GST_STATE_CHANGE_PAUSED_TO_READY:
7363 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
7364 if (ret == GST_STATE_CHANGE_FAILURE)
7367 switch (transition) {
7368 case GST_STATE_CHANGE_NULL_TO_READY:
7369 ret = GST_STATE_CHANGE_SUCCESS;
7371 case GST_STATE_CHANGE_READY_TO_PAUSED:
7372 ret = GST_STATE_CHANGE_NO_PREROLL;
7374 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7375 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
7376 ret = GST_STATE_CHANGE_SUCCESS;
7378 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7379 /* send pause request and keep the idle task around */
7380 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
7381 ret = GST_STATE_CHANGE_NO_PREROLL;
7383 case GST_STATE_CHANGE_PAUSED_TO_READY:
7384 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_PAUSE);
7385 ret = GST_STATE_CHANGE_SUCCESS;
7387 case GST_STATE_CHANGE_READY_TO_NULL:
7388 gst_rtspsrc_stop (rtspsrc);
7389 ret = GST_STATE_CHANGE_SUCCESS;
7400 GST_DEBUG_OBJECT (rtspsrc, "start failed");
7401 return GST_STATE_CHANGE_FAILURE;
7406 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
7409 GstRTSPSrc *rtspsrc;
7411 rtspsrc = GST_RTSPSRC (element);
7413 if (GST_EVENT_IS_DOWNSTREAM (event)) {
7414 res = gst_rtspsrc_push_event (rtspsrc, event);
7416 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
7423 /*** GSTURIHANDLER INTERFACE *************************************************/
7426 gst_rtspsrc_uri_get_type (GType type)
7431 static const gchar *const *
7432 gst_rtspsrc_uri_get_protocols (GType type)
7434 static const gchar *protocols[] =
7435 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
7436 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
7443 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
7445 GstRTSPSrc *src = GST_RTSPSRC (handler);
7447 /* FIXME: make thread-safe */
7448 return g_strdup (src->conninfo.location);
7452 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
7457 GstRTSPUrl *newurl = NULL;
7458 GstSDPMessage *sdp = NULL;
7460 src = GST_RTSPSRC (handler);
7462 /* same URI, we're fine */
7463 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
7466 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
7467 if ((res = gst_sdp_message_new (&sdp) < 0))
7470 GST_DEBUG_OBJECT (src, "parsing SDP message");
7471 if ((res = gst_sdp_message_parse_uri (uri, sdp) < 0))
7475 GST_DEBUG_OBJECT (src, "parsing URI");
7476 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
7480 /* if worked, free previous and store new url object along with the original
7482 GST_DEBUG_OBJECT (src, "configuring URI");
7483 g_free (src->conninfo.location);
7484 src->conninfo.location = g_strdup (uri);
7485 gst_rtsp_url_free (src->conninfo.url);
7486 src->conninfo.url = newurl;
7487 g_free (src->conninfo.url_str);
7489 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
7491 src->conninfo.url_str = NULL;
7494 gst_sdp_message_free (src->sdp);
7496 src->from_sdp = sdp != NULL;
7498 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
7499 GST_DEBUG_OBJECT (src, "request uri is: %s",
7500 GST_STR_NULL (src->conninfo.url_str));
7507 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
7512 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", res);
7513 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7514 "Could not create SDP");
7519 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", res,
7520 GST_STR_NULL (uri));
7521 gst_sdp_message_free (sdp);
7522 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7528 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
7529 GST_STR_NULL (uri), res);
7530 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7531 "Invalid RTSP URI");
7537 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
7539 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
7541 iface->get_type = gst_rtspsrc_uri_get_type;
7542 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
7543 iface->get_uri = gst_rtspsrc_uri_get_uri;
7544 iface->set_uri = gst_rtspsrc_uri_set_uri;