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/sdp/gstmikey.h>
95 #include <gst/rtp/gstrtppayloads.h>
97 #include "gst/gst-i18n-plugin.h"
99 #include "gstrtspsrc.h"
101 GST_DEBUG_CATEGORY_STATIC (rtspsrc_debug);
102 #define GST_CAT_DEFAULT (rtspsrc_debug)
104 static GstStaticPadTemplate rtptemplate = GST_STATIC_PAD_TEMPLATE ("stream_%u",
107 GST_STATIC_CAPS ("application/x-rtp; application/x-rdt"));
109 /* templates used internally */
110 static GstStaticPadTemplate anysrctemplate =
111 GST_STATIC_PAD_TEMPLATE ("internalsrc_%u",
114 GST_STATIC_CAPS_ANY);
116 static GstStaticPadTemplate anysinktemplate =
117 GST_STATIC_PAD_TEMPLATE ("internalsink_%u",
120 GST_STATIC_CAPS_ANY);
124 SIGNAL_HANDLE_REQUEST,
126 SIGNAL_SELECT_STREAM,
131 enum _GstRtspSrcRtcpSyncMode
138 enum _GstRtspSrcBufferMode
147 #define GST_TYPE_RTSP_SRC_BUFFER_MODE (gst_rtsp_src_buffer_mode_get_type())
149 gst_rtsp_src_buffer_mode_get_type (void)
151 static GType buffer_mode_type = 0;
152 static const GEnumValue buffer_modes[] = {
153 {BUFFER_MODE_NONE, "Only use RTP timestamps", "none"},
154 {BUFFER_MODE_SLAVE, "Slave receiver to sender clock", "slave"},
155 {BUFFER_MODE_BUFFER, "Do low/high watermark buffering", "buffer"},
156 {BUFFER_MODE_AUTO, "Choose mode depending on stream live", "auto"},
157 {BUFFER_MODE_SYNCED, "Synchronized sender and receiver clocks", "synced"},
161 if (!buffer_mode_type) {
163 g_enum_register_static ("GstRTSPSrcBufferMode", buffer_modes);
165 return buffer_mode_type;
168 #define DEFAULT_LOCATION NULL
169 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
170 #define DEFAULT_DEBUG FALSE
171 #define DEFAULT_RETRY 20
172 #define DEFAULT_TIMEOUT 5000000
173 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
174 #define DEFAULT_TCP_TIMEOUT 20000000
175 #define DEFAULT_LATENCY_MS 2000
176 #define DEFAULT_DROP_ON_LATENCY FALSE
177 #define DEFAULT_CONNECTION_SPEED 0
178 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
179 #define DEFAULT_DO_RTCP TRUE
180 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
181 #define DEFAULT_PROXY NULL
182 #define DEFAULT_RTP_BLOCKSIZE 0
183 #define DEFAULT_USER_ID NULL
184 #define DEFAULT_USER_PW NULL
185 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
186 #define DEFAULT_PORT_RANGE NULL
187 #define DEFAULT_SHORT_HEADER FALSE
188 #define DEFAULT_PROBATION 2
189 #define DEFAULT_UDP_RECONNECT TRUE
190 #define DEFAULT_MULTICAST_IFACE NULL
191 #define DEFAULT_NTP_SYNC FALSE
192 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
193 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
194 #define DEFAULT_TLS_DATABASE NULL
206 PROP_DROP_ON_LATENCY,
207 PROP_CONNECTION_SPEED,
210 PROP_DO_RTSP_KEEP_ALIVE,
219 PROP_UDP_BUFFER_SIZE,
223 PROP_MULTICAST_IFACE,
225 PROP_USE_PIPELINE_CLOCK,
227 PROP_TLS_VALIDATION_FLAGS,
232 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
234 gst_rtsp_nat_method_get_type (void)
236 static GType rtsp_nat_method_type = 0;
237 static const GEnumValue rtsp_nat_method[] = {
238 {GST_RTSP_NAT_NONE, "None", "none"},
239 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
243 if (!rtsp_nat_method_type) {
244 rtsp_nat_method_type =
245 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
247 return rtsp_nat_method_type;
250 static void gst_rtspsrc_finalize (GObject * object);
252 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
253 const GValue * value, GParamSpec * pspec);
254 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
255 GValue * value, GParamSpec * pspec);
257 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
259 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
260 gpointer iface_data);
262 static void gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes,
265 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
266 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
268 static GstCaps *gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media);
270 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
271 GstStateChange transition);
272 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
273 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
275 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
276 GstRTSPMessage * response);
278 static gboolean gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
280 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
281 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
283 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
284 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
286 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
287 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
288 gboolean only_close);
290 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
291 const gchar * uri, GError ** error);
292 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
294 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
295 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
296 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
297 GstRTSPStream * stream, GstEvent * event);
298 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
299 static void gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush);
307 /* commands we send to out loop to notify it of events */
308 #define CMD_OPEN (1 << 0)
309 #define CMD_PLAY (1 << 1)
310 #define CMD_PAUSE (1 << 2)
311 #define CMD_CLOSE (1 << 3)
312 #define CMD_WAIT (1 << 4)
313 #define CMD_RECONNECT (1 << 5)
314 #define CMD_LOOP (1 << 6)
316 /* mask for all commands */
317 #define CMD_ALL ((CMD_LOOP << 1) - 1)
319 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
321 gchar *__txt = _gst_element_error_printf text; \
322 gst_element_post_message (GST_ELEMENT_CAST (el), \
323 gst_message_new_progress (GST_OBJECT_CAST (el), \
324 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
328 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
330 #define gst_rtspsrc_parent_class parent_class
331 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
332 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
335 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
337 GST_DEBUG_OBJECT (src, "default handler");
342 select_stream_accum (GSignalInvocationHint * ihint,
343 GValue * return_accu, const GValue * handler_return, gpointer data)
347 myboolean = g_value_get_boolean (handler_return);
348 GST_DEBUG ("accum %d", myboolean);
349 g_value_set_boolean (return_accu, myboolean);
351 /* stop emission if FALSE */
356 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
358 GObjectClass *gobject_class;
359 GstElementClass *gstelement_class;
360 GstBinClass *gstbin_class;
362 gobject_class = (GObjectClass *) klass;
363 gstelement_class = (GstElementClass *) klass;
364 gstbin_class = (GstBinClass *) klass;
366 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
368 gobject_class->set_property = gst_rtspsrc_set_property;
369 gobject_class->get_property = gst_rtspsrc_get_property;
371 gobject_class->finalize = gst_rtspsrc_finalize;
373 g_object_class_install_property (gobject_class, PROP_LOCATION,
374 g_param_spec_string ("location", "RTSP Location",
375 "Location of the RTSP url to read",
376 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
378 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
379 g_param_spec_flags ("protocols", "Protocols",
380 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
381 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
383 g_object_class_install_property (gobject_class, PROP_DEBUG,
384 g_param_spec_boolean ("debug", "Debug",
385 "Dump request and response messages to stdout",
386 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
388 g_object_class_install_property (gobject_class, PROP_RETRY,
389 g_param_spec_uint ("retry", "Retry",
390 "Max number of retries when allocating RTP ports.",
391 0, G_MAXUINT16, DEFAULT_RETRY,
392 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
394 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
395 g_param_spec_uint64 ("timeout", "Timeout",
396 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
397 0, G_MAXUINT64, DEFAULT_TIMEOUT,
398 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
400 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
401 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
402 "Fail after timeout microseconds on TCP connections (0 = disabled)",
403 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
404 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
406 g_object_class_install_property (gobject_class, PROP_LATENCY,
407 g_param_spec_uint ("latency", "Buffer latency in ms",
408 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
409 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
411 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
412 g_param_spec_boolean ("drop-on-latency",
413 "Drop buffers when maximum latency is reached",
414 "Tells the jitterbuffer to never exceed the given latency in size",
415 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
417 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
418 g_param_spec_uint64 ("connection-speed", "Connection Speed",
419 "Network connection speed in kbps (0 = unknown)",
420 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
421 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
423 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
424 g_param_spec_enum ("nat-method", "NAT Method",
425 "Method to use for traversing firewalls and NAT",
426 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
427 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
430 * GstRTSPSrc:do-rtcp:
432 * Enable RTCP support. Some old server don't like RTCP and then this property
433 * needs to be set to FALSE.
435 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
436 g_param_spec_boolean ("do-rtcp", "Do RTCP",
437 "Send RTCP packets, disable for old incompatible server.",
438 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
441 * GstRTSPSrc:do-rtsp-keep-alive:
443 * Enable RTSP keep alive support. Some old server don't like RTSP
444 * keep alive and then this property needs to be set to FALSE.
446 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
447 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
448 "Send RTSP keep alive packets, disable for old incompatible server.",
449 DEFAULT_DO_RTSP_KEEP_ALIVE,
450 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
455 * Set the proxy parameters. This has to be a string of the format
456 * [http://][user:passwd@]host[:port].
458 g_object_class_install_property (gobject_class, PROP_PROXY,
459 g_param_spec_string ("proxy", "Proxy",
460 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
461 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
463 * GstRTSPSrc:proxy-id:
465 * Sets the proxy URI user id for authentication. If the URI set via the
466 * "proxy" property contains a user-id already, that will take precedence.
470 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
471 g_param_spec_string ("proxy-id", "proxy-id",
472 "HTTP proxy URI user id for authentication", "",
473 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
475 * GstRTSPSrc:proxy-pw:
477 * Sets the proxy URI password for authentication. If the URI set via the
478 * "proxy" property contains a password already, that will take precedence.
482 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
483 g_param_spec_string ("proxy-pw", "proxy-pw",
484 "HTTP proxy URI user password for authentication", "",
485 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
488 * GstRTSPSrc:rtp-blocksize:
490 * RTP package size to suggest to server.
492 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
493 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
494 "RTP package size to suggest to server (0 = disabled)",
495 0, 65536, DEFAULT_RTP_BLOCKSIZE,
496 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
498 g_object_class_install_property (gobject_class,
500 g_param_spec_string ("user-id", "user-id",
501 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
502 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
503 g_object_class_install_property (gobject_class, PROP_USER_PW,
504 g_param_spec_string ("user-pw", "user-pw",
505 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
506 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
509 * GstRTSPSrc:buffer-mode:
511 * Control the buffering and timestamping mode used by the jitterbuffer.
513 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
514 g_param_spec_enum ("buffer-mode", "Buffer Mode",
515 "Control the buffering algorithm in use",
516 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
517 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
520 * GstRTSPSrc:port-range:
522 * Configure the client port numbers that can be used to recieve RTP and
525 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
526 g_param_spec_string ("port-range", "Port range",
527 "Client port range that can be used to receive RTP and RTCP data, "
528 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
529 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
532 * GstRTSPSrc:udp-buffer-size:
534 * Size of the kernel UDP receive buffer in bytes.
536 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
537 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
538 "Size of the kernel UDP receive buffer in bytes, 0=default",
539 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
540 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
543 * GstRTSPSrc:short-header:
545 * Only send the basic RTSP headers for broken encoders.
547 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
548 g_param_spec_boolean ("short-header", "Short Header",
549 "Only send the basic RTSP headers for broken encoders",
550 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
552 g_object_class_install_property (gobject_class, PROP_PROBATION,
553 g_param_spec_uint ("probation", "Number of probations",
554 "Consecutive packet sequence numbers to accept the source",
555 0, G_MAXUINT, DEFAULT_PROBATION,
556 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
558 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
559 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
560 "Reconnect to the server if RTSP connection is closed when doing UDP",
561 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
563 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
564 g_param_spec_string ("multicast-iface", "Multicast Interface",
565 "The network interface on which to join the multicast group",
566 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
568 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
569 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
570 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
571 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
573 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
574 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
575 "Use the pipeline running-time to set the NTP time in the RTCP SR messages",
576 DEFAULT_USE_PIPELINE_CLOCK,
577 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
579 g_object_class_install_property (gobject_class, PROP_SDES,
580 g_param_spec_boxed ("sdes", "SDES",
581 "The SDES items of this session",
582 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
585 * GstRTSPSrc::tls-validation-flags:
587 * TLS certificate validation flags used to validate server
592 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
593 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
594 "TLS certificate validation flags used to validate the server certificate",
595 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
596 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
599 * GstRTSPSrc::tls-database:
601 * TLS database with anchor certificate authorities used to validate
602 * the server certificate.
606 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
607 g_param_spec_object ("tls-database", "TLS database",
608 "TLS database with anchor certificate authorities used to validate the server certificate",
609 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
612 * GstRTSPSrc::handle-request:
613 * @rtspsrc: a #GstRTSPSrc
614 * @request: a #GstRTSPMessage
615 * @response: a #GstRTSPMessage
617 * Handle a server request in @request and prepare @response.
619 * This signal is called from the streaming thread, you should therefore not
620 * do any state changes on @rtspsrc because this might deadlock. If you want
621 * to modify the state as a result of this signal, post a
622 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
627 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
628 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
629 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
630 G_TYPE_POINTER, G_TYPE_POINTER);
633 * GstRTSPSrc::on-sdp:
634 * @rtspsrc: a #GstRTSPSrc
635 * @sdp: a #GstSDPMessage
637 * Emited when the client has retrieved the SDP and before it configures the
638 * streams in the SDP. @sdp can be inspected and modified.
640 * This signal is called from the streaming thread, you should therefore not
641 * do any state changes on @rtspsrc because this might deadlock. If you want
642 * to modify the state as a result of this signal, post a
643 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
648 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
649 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
650 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
651 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
654 * GstRTSPSrc::select-stream:
655 * @rtspsrc: a #GstRTSPSrc
656 * @num: the stream number
657 * @caps: the stream caps
659 * Emited before the client decides to configure the stream @num with
662 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
667 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
668 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
669 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
670 (GCallback) default_select_stream, select_stream_accum, NULL,
671 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, G_TYPE_UINT,
674 * GstRTSPSrc::new-manager:
675 * @rtspsrc: a #GstRTSPSrc
676 * @manager: a #GstElement
678 * Emited after a new manager (like rtpbin) was created and the default
679 * properties were configured.
683 gst_rtspsrc_signals[SIGNAL_NEW_MANAGER] =
684 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
685 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
686 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
688 gstelement_class->send_event = gst_rtspsrc_send_event;
689 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
690 gstelement_class->change_state = gst_rtspsrc_change_state;
692 gst_element_class_add_pad_template (gstelement_class,
693 gst_static_pad_template_get (&rtptemplate));
695 gst_element_class_set_static_metadata (gstelement_class,
696 "RTSP packet receiver", "Source/Network",
697 "Receive data over the network via RTSP (RFC 2326)",
698 "Wim Taymans <wim@fluendo.com>, "
699 "Thijs Vermeir <thijs.vermeir@barco.com>, "
700 "Lutz Mueller <lutz@topfrose.de>");
702 gstbin_class->handle_message = gst_rtspsrc_handle_message;
704 gst_rtsp_ext_list_init ();
708 gst_rtspsrc_init (GstRTSPSrc * src)
710 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
711 src->protocols = DEFAULT_PROTOCOLS;
712 src->debug = DEFAULT_DEBUG;
713 src->retry = DEFAULT_RETRY;
714 src->udp_timeout = DEFAULT_TIMEOUT;
715 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
716 src->latency = DEFAULT_LATENCY_MS;
717 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
718 src->connection_speed = DEFAULT_CONNECTION_SPEED;
719 src->nat_method = DEFAULT_NAT_METHOD;
720 src->do_rtcp = DEFAULT_DO_RTCP;
721 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
722 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
723 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
724 src->user_id = g_strdup (DEFAULT_USER_ID);
725 src->user_pw = g_strdup (DEFAULT_USER_PW);
726 src->buffer_mode = DEFAULT_BUFFER_MODE;
727 src->client_port_range.min = 0;
728 src->client_port_range.max = 0;
729 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
730 src->short_header = DEFAULT_SHORT_HEADER;
731 src->probation = DEFAULT_PROBATION;
732 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
733 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
734 src->ntp_sync = DEFAULT_NTP_SYNC;
735 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
737 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
738 src->tls_database = DEFAULT_TLS_DATABASE;
740 /* get a list of all extensions */
741 src->extensions = gst_rtsp_ext_list_get ();
743 /* connect to send signal */
744 gst_rtsp_ext_list_connect (src->extensions, "send",
745 (GCallback) gst_rtspsrc_send_cb, src);
747 /* protects the streaming thread in interleaved mode or the polling
748 * thread in UDP mode. */
749 g_rec_mutex_init (&src->stream_rec_lock);
751 /* protects our state changes from multiple invocations */
752 g_rec_mutex_init (&src->state_rec_lock);
754 src->state = GST_RTSP_STATE_INVALID;
756 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
760 gst_rtspsrc_finalize (GObject * object)
764 rtspsrc = GST_RTSPSRC (object);
766 gst_rtsp_ext_list_free (rtspsrc->extensions);
767 g_free (rtspsrc->conninfo.location);
768 gst_rtsp_url_free (rtspsrc->conninfo.url);
769 g_free (rtspsrc->conninfo.url_str);
770 g_free (rtspsrc->user_id);
771 g_free (rtspsrc->user_pw);
772 g_free (rtspsrc->multi_iface);
775 gst_sdp_message_free (rtspsrc->sdp);
778 if (rtspsrc->provided_clock)
779 gst_object_unref (rtspsrc->provided_clock);
782 gst_structure_free (rtspsrc->sdes);
784 if (rtspsrc->tls_database)
785 g_object_unref (rtspsrc->tls_database);
788 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
789 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
791 G_OBJECT_CLASS (parent_class)->finalize (object);
795 gst_rtspsrc_provide_clock (GstElement * element)
797 GstRTSPSrc *src = GST_RTSPSRC (element);
800 if ((clock = src->provided_clock) != NULL)
801 gst_object_ref (clock);
806 /* a proxy string of the format [user:passwd@]host[:port] */
808 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
812 g_free (rtsp->proxy_user);
813 rtsp->proxy_user = NULL;
814 g_free (rtsp->proxy_passwd);
815 rtsp->proxy_passwd = NULL;
816 g_free (rtsp->proxy_host);
817 rtsp->proxy_host = NULL;
818 rtsp->proxy_port = 0;
825 /* we allow http:// in front but ignore it */
826 if (g_str_has_prefix (p, "http://"))
829 at = strchr (p, '@');
831 /* look for user:passwd */
832 col = strchr (proxy, ':');
833 if (col == NULL || col > at)
836 rtsp->proxy_user = g_strndup (p, col - p);
838 rtsp->proxy_passwd = g_strndup (col, at - col);
843 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
844 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
845 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
846 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
847 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
848 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
849 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
852 col = strchr (p, ':');
855 /* everything before the colon is the hostname */
856 rtsp->proxy_host = g_strndup (p, col - p);
858 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
860 rtsp->proxy_host = g_strdup (p);
861 rtsp->proxy_port = 8080;
867 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
869 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
870 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
873 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
875 rtspsrc->ptcp_timeout = NULL;
879 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
884 rtspsrc = GST_RTSPSRC (object);
888 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
889 g_value_get_string (value), NULL);
892 rtspsrc->protocols = g_value_get_flags (value);
895 rtspsrc->debug = g_value_get_boolean (value);
898 rtspsrc->retry = g_value_get_uint (value);
901 rtspsrc->udp_timeout = g_value_get_uint64 (value);
903 case PROP_TCP_TIMEOUT:
904 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
907 rtspsrc->latency = g_value_get_uint (value);
909 case PROP_DROP_ON_LATENCY:
910 rtspsrc->drop_on_latency = g_value_get_boolean (value);
912 case PROP_CONNECTION_SPEED:
913 rtspsrc->connection_speed = g_value_get_uint64 (value);
915 case PROP_NAT_METHOD:
916 rtspsrc->nat_method = g_value_get_enum (value);
919 rtspsrc->do_rtcp = g_value_get_boolean (value);
921 case PROP_DO_RTSP_KEEP_ALIVE:
922 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
925 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
928 if (rtspsrc->prop_proxy_id)
929 g_free (rtspsrc->prop_proxy_id);
930 rtspsrc->prop_proxy_id = g_value_dup_string (value);
933 if (rtspsrc->prop_proxy_pw)
934 g_free (rtspsrc->prop_proxy_pw);
935 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
937 case PROP_RTP_BLOCKSIZE:
938 rtspsrc->rtp_blocksize = g_value_get_uint (value);
941 if (rtspsrc->user_id)
942 g_free (rtspsrc->user_id);
943 rtspsrc->user_id = g_value_dup_string (value);
946 if (rtspsrc->user_pw)
947 g_free (rtspsrc->user_pw);
948 rtspsrc->user_pw = g_value_dup_string (value);
950 case PROP_BUFFER_MODE:
951 rtspsrc->buffer_mode = g_value_get_enum (value);
953 case PROP_PORT_RANGE:
957 str = g_value_get_string (value);
959 sscanf (str, "%u-%u",
960 &rtspsrc->client_port_range.min, &rtspsrc->client_port_range.max);
962 rtspsrc->client_port_range.min = 0;
963 rtspsrc->client_port_range.max = 0;
967 case PROP_UDP_BUFFER_SIZE:
968 rtspsrc->udp_buffer_size = g_value_get_int (value);
970 case PROP_SHORT_HEADER:
971 rtspsrc->short_header = g_value_get_boolean (value);
974 rtspsrc->probation = g_value_get_uint (value);
976 case PROP_UDP_RECONNECT:
977 rtspsrc->udp_reconnect = g_value_get_boolean (value);
979 case PROP_MULTICAST_IFACE:
980 g_free (rtspsrc->multi_iface);
982 if (g_value_get_string (value) == NULL)
983 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
985 rtspsrc->multi_iface = g_value_dup_string (value);
988 rtspsrc->ntp_sync = g_value_get_boolean (value);
990 case PROP_USE_PIPELINE_CLOCK:
991 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
994 rtspsrc->sdes = g_value_dup_boxed (value);
996 case PROP_TLS_VALIDATION_FLAGS:
997 rtspsrc->tls_validation_flags = g_value_get_flags (value);
999 case PROP_TLS_DATABASE:
1000 g_clear_object (&rtspsrc->tls_database);
1001 rtspsrc->tls_database = g_value_dup_object (value);
1004 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1010 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
1013 GstRTSPSrc *rtspsrc;
1015 rtspsrc = GST_RTSPSRC (object);
1019 g_value_set_string (value, rtspsrc->conninfo.location);
1021 case PROP_PROTOCOLS:
1022 g_value_set_flags (value, rtspsrc->protocols);
1025 g_value_set_boolean (value, rtspsrc->debug);
1028 g_value_set_uint (value, rtspsrc->retry);
1031 g_value_set_uint64 (value, rtspsrc->udp_timeout);
1033 case PROP_TCP_TIMEOUT:
1037 timeout = rtspsrc->tcp_timeout.tv_sec * G_USEC_PER_SEC +
1038 rtspsrc->tcp_timeout.tv_usec;
1039 g_value_set_uint64 (value, timeout);
1043 g_value_set_uint (value, rtspsrc->latency);
1045 case PROP_DROP_ON_LATENCY:
1046 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1048 case PROP_CONNECTION_SPEED:
1049 g_value_set_uint64 (value, rtspsrc->connection_speed);
1051 case PROP_NAT_METHOD:
1052 g_value_set_enum (value, rtspsrc->nat_method);
1055 g_value_set_boolean (value, rtspsrc->do_rtcp);
1057 case PROP_DO_RTSP_KEEP_ALIVE:
1058 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1064 if (rtspsrc->proxy_host) {
1066 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1070 g_value_take_string (value, str);
1074 g_value_set_string (value, rtspsrc->prop_proxy_id);
1077 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1079 case PROP_RTP_BLOCKSIZE:
1080 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1083 g_value_set_string (value, rtspsrc->user_id);
1086 g_value_set_string (value, rtspsrc->user_pw);
1088 case PROP_BUFFER_MODE:
1089 g_value_set_enum (value, rtspsrc->buffer_mode);
1091 case PROP_PORT_RANGE:
1095 if (rtspsrc->client_port_range.min != 0) {
1096 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1097 rtspsrc->client_port_range.max);
1101 g_value_take_string (value, str);
1104 case PROP_UDP_BUFFER_SIZE:
1105 g_value_set_int (value, rtspsrc->udp_buffer_size);
1107 case PROP_SHORT_HEADER:
1108 g_value_set_boolean (value, rtspsrc->short_header);
1110 case PROP_PROBATION:
1111 g_value_set_uint (value, rtspsrc->probation);
1113 case PROP_UDP_RECONNECT:
1114 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1116 case PROP_MULTICAST_IFACE:
1117 g_value_set_string (value, rtspsrc->multi_iface);
1120 g_value_set_boolean (value, rtspsrc->ntp_sync);
1122 case PROP_USE_PIPELINE_CLOCK:
1123 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1126 g_value_set_boxed (value, rtspsrc->sdes);
1128 case PROP_TLS_VALIDATION_FLAGS:
1129 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1131 case PROP_TLS_DATABASE:
1132 g_value_set_object (value, rtspsrc->tls_database);
1135 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1141 find_stream_by_id (GstRTSPStream * stream, gint * id)
1143 if (stream->id == *id)
1150 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1152 if (stream->channel[0] == *channel || stream->channel[1] == *channel)
1159 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1161 GstElement *src = (GstElement *) a;
1163 if (stream->udpsrc[0] == src)
1165 if (stream->udpsrc[1] == src)
1172 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1174 if (stream->conninfo.location) {
1175 /* check qualified setup_url */
1176 if (!strcmp (stream->conninfo.location, (gchar *) a))
1179 if (stream->control_url) {
1180 /* check original control_url */
1181 if (!strcmp (stream->control_url, (gchar *) a))
1184 /* check if qualified setup_url ends with string */
1185 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1192 static GstRTSPStream *
1193 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1197 /* find and get stream */
1198 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1199 return (GstRTSPStream *) lstream->data;
1204 static const GstSDPBandwidth *
1205 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1206 const GstSDPMedia * media, const gchar * type)
1210 /* first look in the media specific section */
1211 len = gst_sdp_media_bandwidths_len (media);
1212 for (i = 0; i < len; i++) {
1213 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1215 if (strcmp (bw->bwtype, type) == 0)
1218 /* then look in the message specific section */
1219 len = gst_sdp_message_bandwidths_len (sdp);
1220 for (i = 0; i < len; i++) {
1221 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1223 if (strcmp (bw->bwtype, type) == 0)
1230 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1231 const GstSDPMedia * media, GstRTSPStream * stream)
1233 const GstSDPBandwidth *bw;
1235 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1236 stream->as_bandwidth = bw->bandwidth;
1238 stream->as_bandwidth = -1;
1240 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1241 stream->rr_bandwidth = bw->bandwidth;
1243 stream->rr_bandwidth = -1;
1245 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1246 stream->rs_bandwidth = bw->bandwidth;
1248 stream->rs_bandwidth = -1;
1252 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1253 const GstSDPConnection * conn)
1255 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1258 if (conn->addrtype == NULL)
1261 /* check for IPV6 */
1262 if (strcmp (conn->addrtype, "IP4") == 0)
1263 stream->is_ipv6 = FALSE;
1264 else if (strcmp (conn->addrtype, "IP6") == 0)
1265 stream->is_ipv6 = TRUE;
1270 g_free (stream->destination);
1271 stream->destination = g_strdup (conn->address);
1273 /* check for multicast */
1274 stream->is_multicast =
1275 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1277 stream->ttl = conn->ttl;
1280 /* Go over the connections for a stream.
1281 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1283 * - If we are dealing with a localhost address, we disable multicast
1286 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1287 const GstSDPMedia * media, GstRTSPStream * stream)
1289 const GstSDPConnection *conn;
1292 /* first look in the media specific section */
1293 len = gst_sdp_media_connections_len (media);
1294 for (i = 0; i < len; i++) {
1295 conn = gst_sdp_media_get_connection (media, i);
1297 gst_rtspsrc_do_stream_connection (src, stream, conn);
1299 /* then look in the message specific section */
1300 if ((conn = gst_sdp_message_get_connection (sdp))) {
1301 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 parse_keymgmt (const gchar * keymgmt, GstCaps * caps)
1690 gboolean res = FALSE;
1694 GstMIKEYMessage *msg;
1695 const GstMIKEYPayload *payload;
1696 const gchar *srtp_cipher;
1697 const gchar *srtp_auth;
1699 p = (gchar *) keymgmt;
1705 PARSE_STRING (p, " ", kmpid);
1706 if (!g_str_equal (kmpid, "mikey"))
1709 data = g_base64_decode (p, &size);
1713 msg = gst_mikey_message_new_from_data (data, size);
1717 srtp_cipher = "aes-128-icm";
1718 srtp_auth = "hmac-sha1-80";
1720 /* check the Security policy if any */
1721 if ((payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, 0))) {
1722 GstMIKEYPayloadSP *p = (GstMIKEYPayloadSP *) payload;
1725 if (p->proto != GST_MIKEY_SEC_PROTO_SRTP)
1728 len = gst_mikey_payload_sp_get_n_params (payload);
1729 for (i = 0; i < len; i++) {
1730 const GstMIKEYPayloadSPParam *param =
1731 gst_mikey_payload_sp_get_param (payload, i);
1733 switch (param->type) {
1734 case GST_MIKEY_SP_SRTP_ENC_ALG:
1735 switch (param->val[0]) {
1737 srtp_cipher = "null";
1741 srtp_cipher = "aes-128-icm";
1747 case GST_MIKEY_SP_SRTP_AUTH_ALG:
1748 switch (param->val[0]) {
1754 srtp_auth = "hmac-sha1-80";
1760 case GST_MIKEY_SP_SRTP_SRTP_ENC:
1762 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
1770 if (!(payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_KEMAC, 0)))
1773 GstMIKEYPayloadKEMAC *p = (GstMIKEYPayloadKEMAC *) payload;
1776 if (p->enc_alg != GST_MIKEY_ENC_NULL || p->mac_alg != GST_MIKEY_MAC_NULL)
1780 gst_buffer_new_wrapped (g_memdup (p->enc_data, p->enc_len), p->enc_len);
1781 gst_caps_set_simple (caps, "srtp-key", GST_TYPE_BUFFER, buf, NULL);
1784 gst_caps_set_simple (caps,
1785 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
1786 "srtp-auth", G_TYPE_STRING, srtp_auth,
1787 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
1788 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
1792 gst_mikey_message_free (msg);
1798 * Mapping SDP attributes to caps
1800 * prepend 'a-' to IANA registered sdp attributes names
1801 * (ie: not prefixed with 'x-') in order to avoid
1802 * collision with gstreamer standard caps properties names
1805 gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
1807 if (attributes->len > 0) {
1811 s = gst_caps_get_structure (caps, 0);
1813 for (i = 0; i < attributes->len; i++) {
1814 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
1815 gchar *tofree, *key;
1819 /* skip some of the attribute we already handle */
1820 if (!strcmp (key, "fmtp"))
1822 if (!strcmp (key, "rtpmap"))
1824 if (!strcmp (key, "control"))
1826 if (!strcmp (key, "range"))
1828 if (g_str_equal (key, "key-mgmt")) {
1829 parse_keymgmt (attr->value, caps);
1833 /* string must be valid UTF8 */
1834 if (!g_utf8_validate (attr->value, -1, NULL))
1837 if (!g_str_has_prefix (key, "x-"))
1838 tofree = key = g_strdup_printf ("a-%s", key);
1842 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
1843 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
1849 static const gchar *
1850 rtsp_get_attribute_for_pt (const GstSDPMedia * media, const gchar * name,
1859 if ((attr = gst_sdp_media_get_attribute_val_n (media, name, i)) == NULL)
1862 if (sscanf (attr, "%d ", &val) != 1)
1872 * Mapping of caps to and from SDP fields:
1874 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1875 * a=fmtp:<payload> <param>[=<value>];...
1878 gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
1881 const gchar *rtpmap;
1885 gchar *params = NULL;
1891 /* get and parse rtpmap */
1892 rtpmap = rtsp_get_attribute_for_pt (media, "rtpmap", pt);
1895 ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
1897 g_warning ("error parsing rtpmap, ignoring");
1901 /* dynamic payloads need rtpmap or we fail */
1902 if (rtpmap == NULL && pt >= 96)
1905 /* check if we have a rate, if not, we need to look up the rate from the
1906 * default rates based on the payload types. */
1908 const GstRTPPayloadInfo *info;
1910 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
1911 /* dynamic types, use media and encoding_name */
1912 tmp = g_ascii_strdown (media->media, -1);
1913 info = gst_rtp_payload_info_for_name (tmp, name);
1916 /* static types, use payload type */
1917 info = gst_rtp_payload_info_for_pt (pt);
1921 if ((rate = info->clock_rate) == 0)
1924 /* we fail if we cannot find one */
1929 tmp = g_ascii_strdown (media->media, -1);
1930 caps = gst_caps_new_simple ("application/x-unknown",
1931 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
1933 s = gst_caps_get_structure (caps, 0);
1935 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
1937 /* encoding name must be upper case */
1939 tmp = g_ascii_strup (name, -1);
1940 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
1944 /* params must be lower case */
1945 if (params != NULL) {
1946 tmp = g_ascii_strdown (params, -1);
1947 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
1951 /* parse optional fmtp: field */
1952 if ((fmtp = rtsp_get_attribute_for_pt (media, "fmtp", pt))) {
1958 /* p is now of the format <payload> <param>[=<value>];... */
1959 PARSE_INT (p, " ", payload);
1960 if (payload != -1 && payload == pt) {
1964 /* <param>[=<value>] are separated with ';' */
1965 pairs = g_strsplit (p, ";", 0);
1966 for (i = 0; pairs[i]; i++) {
1968 const gchar *val, *key;
1970 /* the key may not have a '=', the value can have other '='s */
1971 valpos = strstr (pairs[i], "=");
1973 /* we have a '=' and thus a value, remove the '=' with \0 */
1975 /* value is everything between '=' and ';'. We split the pairs at ;
1976 * boundaries so we can take the remainder of the value. Some servers
1977 * put spaces around the value which we strip off here. Alternatively
1978 * we could strip those spaces in the depayloaders should these spaces
1979 * actually carry any meaning in the future. */
1980 val = g_strstrip (valpos + 1);
1982 /* simple <param>;.. is translated into <param>=1;... */
1985 /* strip the key of spaces, convert key to lowercase but not the value. */
1986 key = g_strstrip (pairs[i]);
1987 if (strlen (key) > 1) {
1988 tmp = g_ascii_strdown (key, -1);
1989 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
2001 g_warning ("rtpmap type not given for dynamic payload %d", pt);
2006 g_warning ("rate unknown for payload type %d", pt);
2012 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
2013 gint * rtpport, gint * rtcpport)
2016 GstStateChangeReturn ret;
2017 GstElement *udpsrc0, *udpsrc1;
2018 gint tmp_rtp, tmp_rtcp;
2022 src = stream->parent;
2028 /* Start at next port */
2029 tmp_rtp = src->next_port_num;
2031 if (stream->is_ipv6)
2032 host = "udp://[::0]";
2034 host = "udp://0.0.0.0";
2036 /* try to allocate 2 UDP ports, the RTP port should be an even
2037 * number and the RTCP port should be the next (uneven) port */
2040 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
2041 tmp_rtp >= src->client_port_range.max)
2044 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2045 if (udpsrc0 == NULL)
2046 goto no_udp_protocol;
2047 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
2049 if (src->udp_buffer_size != 0)
2050 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
2053 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
2054 if (ret == GST_STATE_CHANGE_FAILURE) {
2056 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
2059 if (++count > src->retry)
2062 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2063 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2064 gst_object_unref (udpsrc0);
2067 GST_DEBUG_OBJECT (src, "retry %d", count);
2070 goto no_udp_protocol;
2073 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
2074 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
2076 /* check if port is even */
2077 if ((tmp_rtp & 0x01) != 0) {
2078 /* port not even, close and allocate another */
2079 if (++count > src->retry)
2082 GST_DEBUG_OBJECT (src, "RTP port not even");
2084 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2085 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2086 gst_object_unref (udpsrc0);
2089 GST_DEBUG_OBJECT (src, "retry %d", count);
2094 /* allocate port+1 for RTCP now */
2095 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2096 if (udpsrc1 == NULL)
2097 goto no_udp_rtcp_protocol;
2100 tmp_rtcp = tmp_rtp + 1;
2101 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
2104 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
2106 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
2107 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
2108 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
2109 if (ret == GST_STATE_CHANGE_FAILURE) {
2110 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
2112 if (++count > src->retry)
2115 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2116 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2117 gst_object_unref (udpsrc0);
2120 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
2121 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2122 gst_object_unref (udpsrc1);
2126 GST_DEBUG_OBJECT (src, "retry %d", count);
2130 /* all fine, do port check */
2131 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
2132 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
2134 /* this should not happen... */
2135 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
2138 /* we keep these elements, we configure all in configure_transport when the
2139 * server told us to really use the UDP ports. */
2140 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
2141 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
2142 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
2143 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
2145 /* keep track of next available port number when we have a range
2147 if (src->next_port_num != 0)
2148 src->next_port_num = tmp_rtcp + 1;
2155 GST_DEBUG_OBJECT (src, "could not get UDP source");
2160 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
2164 no_udp_rtcp_protocol:
2166 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
2171 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
2172 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
2178 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2179 gst_object_unref (udpsrc0);
2182 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2183 gst_object_unref (udpsrc1);
2190 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
2195 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
2197 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2198 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2201 for (i = 0; i < 2; i++) {
2202 if (stream->udpsrc[i])
2203 gst_element_set_state (stream->udpsrc[i], state);
2209 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
2216 event = gst_event_new_flush_start ();
2217 GST_DEBUG_OBJECT (src, "start flush");
2219 state = GST_STATE_PAUSED;
2221 event = gst_event_new_flush_stop (FALSE);
2222 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2225 state = GST_STATE_PLAYING;
2227 state = GST_STATE_PAUSED;
2229 gst_rtspsrc_push_event (src, event);
2230 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2231 gst_rtspsrc_set_state (src, state);
2234 static GstRTSPResult
2235 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
2236 GstRTSPMessage * message, GTimeVal * timeout)
2241 ret = gst_rtsp_connection_send (conn, message, timeout);
2243 ret = GST_RTSP_ERROR;
2248 static GstRTSPResult
2249 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
2250 GstRTSPMessage * message, GTimeVal * timeout)
2255 ret = gst_rtsp_connection_receive (conn, message, timeout);
2257 ret = GST_RTSP_ERROR;
2263 gst_rtspsrc_get_position (GstRTSPSrc * src)
2268 query = gst_query_new_position (GST_FORMAT_TIME);
2269 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2270 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2271 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2275 if (stream->srcpad) {
2276 if (gst_pad_query (stream->srcpad, query)) {
2277 gst_query_parse_position (query, &fmt, &pos);
2278 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2279 GST_TIME_ARGS (pos));
2280 src->last_pos = pos;
2290 gst_rtspsrc_do_seek (GstRTSPSrc * src, GstSegment * segment)
2292 src->state = GST_RTSP_STATE_SEEKING;
2293 /* PLAY will add the range header now. */
2294 src->need_range = TRUE;
2300 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2305 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2307 gboolean flush, skip;
2310 GstSegment seeksegment = { 0, };
2314 GST_DEBUG_OBJECT (src, "doing seek with event");
2316 gst_event_parse_seek (event, &rate, &format, &flags,
2317 &cur_type, &cur, &stop_type, &stop);
2319 /* no negative rates yet */
2323 /* we need TIME format */
2324 if (format != src->segment.format)
2327 GST_DEBUG_OBJECT (src, "doing seek without event");
2329 cur_type = GST_SEEK_TYPE_SET;
2330 stop_type = GST_SEEK_TYPE_SET;
2333 /* get flush flag */
2334 flush = flags & GST_SEEK_FLAG_FLUSH;
2335 skip = flags & GST_SEEK_FLAG_SKIP;
2337 /* now we need to make sure the streaming thread is stopped. We do this by
2338 * either sending a FLUSH_START event downstream which will cause the
2339 * streaming thread to stop with a WRONG_STATE.
2340 * For a non-flushing seek we simply pause the task, which will happen as soon
2341 * as it completes one iteration (and thus might block when the sink is
2342 * blocking in preroll). */
2344 GST_DEBUG_OBJECT (src, "starting flush");
2345 gst_rtspsrc_flush (src, TRUE, FALSE);
2348 gst_task_pause (src->task);
2352 /* we should now be able to grab the streaming thread because we stopped it
2353 * with the above flush/pause code */
2354 GST_RTSP_STREAM_LOCK (src);
2356 GST_DEBUG_OBJECT (src, "stopped streaming");
2358 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2359 gst_rtspsrc_connection_flush (src, FALSE);
2361 /* copy segment, we need this because we still need the old
2362 * segment when we close the current segment. */
2363 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2365 /* configure the seek parameters in the seeksegment. We will then have the
2366 * right values in the segment to perform the seek */
2368 GST_DEBUG_OBJECT (src, "configuring seek");
2369 gst_segment_do_seek (&seeksegment, rate, format, flags,
2370 cur_type, cur, stop_type, stop, &update);
2373 /* figure out the last position we need to play. If it's configured (stop !=
2374 * -1), use that, else we play until the total duration of the file */
2375 if ((stop = seeksegment.stop) == -1)
2376 stop = seeksegment.duration;
2378 playing = (src->state == GST_RTSP_STATE_PLAYING);
2380 /* if we were playing, pause first */
2382 /* obtain current position in case seek fails */
2383 gst_rtspsrc_get_position (src);
2384 gst_rtspsrc_pause (src, FALSE);
2388 gst_rtspsrc_do_seek (src, &seeksegment);
2390 /* and continue playing */
2392 gst_rtspsrc_play (src, &seeksegment, FALSE);
2394 /* prepare for streaming again */
2396 /* if we started flush, we stop now */
2397 GST_DEBUG_OBJECT (src, "stopping flush");
2398 gst_rtspsrc_flush (src, FALSE, playing);
2401 /* now we did the seek and can activate the new segment values */
2402 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2404 /* if we're doing a segment seek, post a SEGMENT_START message */
2405 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2406 gst_element_post_message (GST_ELEMENT_CAST (src),
2407 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2408 src->segment.format, src->segment.position));
2411 /* now create the newsegment */
2412 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2413 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2416 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2417 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2418 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2419 stream->discont = TRUE;
2422 GST_RTSP_STREAM_UNLOCK (src);
2429 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2434 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2440 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2444 gboolean res = TRUE;
2447 src = GST_RTSPSRC_CAST (parent);
2449 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2450 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2452 switch (GST_EVENT_TYPE (event)) {
2453 case GST_EVENT_SEEK:
2454 res = gst_rtspsrc_perform_seek (src, event);
2458 case GST_EVENT_NAVIGATION:
2459 case GST_EVENT_LATENCY:
2467 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2468 res = gst_pad_send_event (target, event);
2469 gst_object_unref (target);
2471 gst_event_unref (event);
2474 gst_event_unref (event);
2480 /* this is the final event function we receive on the internal source pad when
2481 * we deal with TCP connections */
2483 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2488 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2490 switch (GST_EVENT_TYPE (event)) {
2491 case GST_EVENT_SEEK:
2493 case GST_EVENT_NAVIGATION:
2494 case GST_EVENT_LATENCY:
2496 gst_event_unref (event);
2503 /* this is the final query function we receive on the internal source pad when
2504 * we deal with TCP connections */
2506 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2510 gboolean res = TRUE;
2512 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2514 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2515 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2517 switch (GST_QUERY_TYPE (query)) {
2518 case GST_QUERY_POSITION:
2523 case GST_QUERY_DURATION:
2527 gst_query_parse_duration (query, &format, NULL);
2530 case GST_FORMAT_TIME:
2531 gst_query_set_duration (query, format, src->segment.duration);
2539 case GST_QUERY_LATENCY:
2541 /* we are live with a min latency of 0 and unlimited max latency, this
2542 * result will be updated by the session manager if there is any. */
2543 gst_query_set_latency (query, TRUE, 0, -1);
2553 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2555 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2559 gboolean res = FALSE;
2561 src = GST_RTSPSRC_CAST (parent);
2563 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2564 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2566 switch (GST_QUERY_TYPE (query)) {
2567 case GST_QUERY_DURATION:
2571 gst_query_parse_duration (query, &format, NULL);
2574 case GST_FORMAT_TIME:
2575 gst_query_set_duration (query, format, src->segment.duration);
2583 case GST_QUERY_SEEKING:
2587 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2588 if (format == GST_FORMAT_TIME) {
2590 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2592 /* seeking without duration is unlikely */
2593 seekable = seekable && src->seekable && src->segment.duration &&
2594 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2596 /* FIXME ?? should we have 0 and segment.duration here; see demuxers */
2597 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
2598 src->segment.start, src->segment.stop);
2607 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2609 gst_query_set_uri (query, uri);
2617 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2619 /* forward the query to the proxy target pad */
2621 res = gst_pad_query (target, query);
2622 gst_object_unref (target);
2631 /* callback for RTCP messages to be sent to the server when operating in TCP
2633 static GstFlowReturn
2634 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2637 GstRTSPStream *stream;
2638 GstFlowReturn res = GST_FLOW_OK;
2643 GstRTSPMessage message = { 0 };
2644 GstRTSPConnection *conn;
2646 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2647 src = stream->parent;
2649 gst_buffer_map (buffer, &map, GST_MAP_READ);
2653 gst_rtsp_message_init_data (&message, stream->channel[1]);
2655 /* lend the body data to the message */
2656 gst_rtsp_message_take_body (&message, data, size);
2658 if (stream->conninfo.connection)
2659 conn = stream->conninfo.connection;
2661 conn = src->conninfo.connection;
2663 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2664 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2665 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2667 /* and steal it away again because we will free it when unreffing the
2669 gst_rtsp_message_steal_body (&message, &data, &size);
2670 gst_rtsp_message_unset (&message);
2672 gst_buffer_unmap (buffer, &map);
2673 gst_buffer_unref (buffer);
2678 static GstPadProbeReturn
2679 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2681 GstRTSPSrc *src = user_data;
2683 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2684 GST_DEBUG_PAD_NAME (pad));
2686 /* activate the streams */
2687 GST_OBJECT_LOCK (src);
2688 if (!src->need_activate)
2691 src->need_activate = FALSE;
2692 GST_OBJECT_UNLOCK (src);
2694 gst_rtspsrc_activate_streams (src);
2696 return GST_PAD_PROBE_OK;
2700 GST_OBJECT_UNLOCK (src);
2701 return GST_PAD_PROBE_OK;
2705 /* this callback is called when the session manager generated a new src pad with
2706 * payloaded RTP packets. We simply ghost the pad here. */
2708 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2711 GstPadTemplate *template;
2714 GstRTSPStream *stream;
2717 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2719 GST_RTSP_STATE_LOCK (src);
2721 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2722 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2723 goto unknown_stream;
2725 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2727 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2729 goto unknown_stream;
2732 stream->ssrc = ssrc;
2734 /* we'll add it later see below */
2735 stream->added = TRUE;
2737 /* check if we added all streams */
2739 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
2740 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
2742 GST_DEBUG_OBJECT (src, "stream %p, container %d, added %d, setup %d",
2743 ostream, ostream->container, ostream->added, ostream->setup);
2745 /* if we find a stream for which we did a setup that is not added, we
2746 * need to wait some more */
2747 if (ostream->setup && !ostream->added) {
2752 GST_RTSP_STATE_UNLOCK (src);
2754 /* create a new pad we will use to stream to */
2755 template = gst_static_pad_template_get (&rtptemplate);
2756 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2757 gst_object_unref (template);
2760 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2761 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2762 gst_pad_set_active (stream->srcpad, TRUE);
2763 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2766 GST_DEBUG_OBJECT (src, "We added all streams");
2767 /* when we get here, all stream are added and we can fire the no-more-pads
2769 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2777 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2778 GST_RTSP_STATE_UNLOCK (src);
2785 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
2789 len = stream->ptmap->len;
2790 for (i = 0; i < len; i++) {
2791 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
2799 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2801 GstRTSPStream *stream;
2804 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2806 GST_RTSP_STATE_LOCK (src);
2807 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2809 goto unknown_stream;
2811 if ((caps = stream_get_caps_for_pt (stream, pt)))
2812 gst_caps_ref (caps);
2813 GST_RTSP_STATE_UNLOCK (src);
2819 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2820 GST_RTSP_STATE_UNLOCK (src);
2826 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2828 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2834 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2840 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2846 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2848 GstRTSPSrc *src = stream->parent;
2851 g_object_get (source, "ssrc", &ssrc, NULL);
2853 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
2854 ssrc, stream->ssrc, stream->id);
2856 if (ssrc == stream->ssrc)
2857 gst_rtspsrc_do_stream_eos (src, stream);
2861 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2863 GstRTSPSrc *src = stream->parent;
2866 g_object_get (source, "ssrc", &ssrc, NULL);
2868 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
2869 ssrc, stream->ssrc, stream->id);
2871 if (ssrc == stream->ssrc)
2872 gst_rtspsrc_do_stream_eos (src, stream);
2876 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2878 GstRTSPStream *stream;
2880 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2882 /* get stream for session */
2883 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2885 gst_rtspsrc_do_stream_eos (src, stream);
2890 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2892 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2897 set_manager_buffer_mode (GstRTSPSrc * src)
2899 GObjectClass *klass;
2901 if (src->manager == NULL)
2904 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2906 if (!g_object_class_find_property (klass, "buffer-mode"))
2909 if (src->buffer_mode != BUFFER_MODE_AUTO) {
2910 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
2915 GST_DEBUG_OBJECT (src,
2916 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
2918 if (src->provided_clock) {
2919 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
2921 if (clock == src->provided_clock) {
2922 GST_DEBUG_OBJECT (src, "selected synced");
2923 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
2926 gst_object_unref (clock);
2931 /* Otherwise fall-through and use another buffer mode */
2933 gst_object_unref (clock);
2936 GST_DEBUG_OBJECT (src, "auto buffering mode");
2937 if (src->use_buffering) {
2938 GST_DEBUG_OBJECT (src, "selected buffer");
2939 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
2941 GST_DEBUG_OBJECT (src, "selected slave");
2942 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
2947 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
2949 GST_DEBUG ("request key %u", ssrc);
2950 return gst_caps_ref (stream_get_caps_for_pt (stream, stream->default_pt));
2954 request_rtp_decoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
2956 if (stream->id != session)
2959 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
2960 stream->profile != GST_RTSP_PROFILE_SAVPF)
2963 if (stream->srtpdec == NULL) {
2966 name = g_strdup_printf ("srtpdec_%u", session);
2967 stream->srtpdec = gst_element_factory_make ("srtpdec", name);
2970 g_signal_connect (stream->srtpdec, "request-key",
2971 (GCallback) request_key, stream);
2973 return gst_object_ref (stream->srtpdec);
2976 /* try to get and configure a manager */
2978 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
2979 GstRTSPTransport * transport)
2981 const gchar *manager;
2983 GstStateChangeReturn ret;
2985 /* find a manager */
2986 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
2990 GST_DEBUG_OBJECT (src, "using manager %s", manager);
2992 /* configure the manager */
2993 if (src->manager == NULL) {
2994 GObjectClass *klass;
2996 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
2998 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
3002 goto use_no_manager;
3004 if (!(src->manager = gst_element_factory_make (manager, "manager")))
3005 goto manager_failed;
3008 /* we manage this element */
3009 gst_element_set_locked_state (src->manager, TRUE);
3010 gst_bin_add (GST_BIN_CAST (src), src->manager);
3012 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
3013 if (ret == GST_STATE_CHANGE_FAILURE)
3014 goto start_manager_failure;
3016 g_object_set (src->manager, "latency", src->latency, NULL);
3018 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3020 if (g_object_class_find_property (klass, "ntp-sync")) {
3021 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
3024 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
3025 g_object_set (src->manager, "use-pipeline-clock",
3026 src->use_pipeline_clock, NULL);
3029 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
3030 g_object_set (src->manager, "sdes", src->sdes, NULL);
3033 if (g_object_class_find_property (klass, "drop-on-latency")) {
3034 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
3038 /* buffer mode pauses are handled by adding offsets to buffer times,
3039 * but some depayloaders may have a hard time syncing output times
3040 * with such input times, e.g. container ones, most notably ASF */
3041 /* TODO alternatives are having an event that indicates these shifts,
3042 * or having rtsp extensions provide suggestion on buffer mode */
3043 /* valid duration implies not likely live pipeline,
3044 * so slaving in jitterbuffer does not make much sense
3045 * (and might mess things up due to bursts) */
3046 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
3047 src->segment.duration && !stream->container) {
3048 src->use_buffering = TRUE;
3050 src->use_buffering = FALSE;
3053 set_manager_buffer_mode (src);
3055 /* connect to signals */
3056 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
3058 src->manager_sig_id =
3059 g_signal_connect (src->manager, "pad-added",
3060 (GCallback) new_manager_pad, src);
3061 src->manager_ptmap_id =
3062 g_signal_connect (src->manager, "request-pt-map",
3063 (GCallback) request_pt_map, src);
3065 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
3068 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
3071 g_signal_connect (src->manager, "request-rtp-decoder",
3072 (GCallback) request_rtp_decoder, stream);
3073 g_signal_connect (src->manager, "request-rtcp-decoder",
3074 (GCallback) request_rtp_decoder, stream);
3076 /* we stream directly to the manager, get some pads. Each RTSP stream goes
3077 * into a separate RTP session. */
3078 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
3079 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
3081 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
3082 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
3085 /* now configure the bandwidth in the manager */
3086 if (g_signal_lookup ("get-internal-session",
3087 G_OBJECT_TYPE (src->manager)) != 0) {
3088 GObject *rtpsession;
3090 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
3093 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
3095 stream->session = rtpsession;
3097 if (stream->as_bandwidth != -1) {
3098 GST_INFO_OBJECT (src, "setting AS: %f",
3099 (gdouble) (stream->as_bandwidth * 1000));
3100 g_object_set (rtpsession, "bandwidth",
3101 (gdouble) (stream->as_bandwidth * 1000), NULL);
3103 if (stream->rr_bandwidth != -1) {
3104 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
3105 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
3108 if (stream->rs_bandwidth != -1) {
3109 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
3110 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
3114 g_object_set (rtpsession, "probation", src->probation, NULL);
3116 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
3118 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
3120 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
3122 g_signal_connect (rtpsession, "on-ssrc-active",
3123 (GCallback) on_ssrc_active, stream);
3134 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3139 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
3142 start_manager_failure:
3144 GST_DEBUG_OBJECT (src, "could not start session manager");
3149 /* free the UDP sources allocated when negotiating a transport.
3150 * This function is called when the server negotiated to a transport where the
3151 * UDP sources are not needed anymore, such as TCP or multicast. */
3153 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
3157 for (i = 0; i < 2; i++) {
3158 if (stream->udpsrc[i]) {
3159 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
3160 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
3161 gst_object_unref (stream->udpsrc[i]);
3162 stream->udpsrc[i] = NULL;
3167 /* for TCP, create pads to send and receive data to and from the manager and to
3168 * intercept various events and queries
3171 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
3172 GstRTSPTransport * transport, GstPad ** outpad)
3175 GstPadTemplate *template;
3176 GstPad *pad0, *pad1;
3178 /* configure for interleaved delivery, nothing needs to be done
3179 * here, the loop function will call the chain functions of the
3180 * session manager. */
3181 stream->channel[0] = transport->interleaved.min;
3182 stream->channel[1] = transport->interleaved.max;
3183 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
3184 stream->channel[0], stream->channel[1]);
3186 /* we can remove the allocated UDP ports now */
3187 gst_rtspsrc_stream_free_udp (stream);
3189 /* no session manager, send data to srcpad directly */
3190 if (!stream->channelpad[0]) {
3191 GST_DEBUG_OBJECT (src, "no manager, creating pad");
3193 /* create a new pad we will use to stream to */
3194 name = g_strdup_printf ("stream_%u", stream->id);
3195 template = gst_static_pad_template_get (&rtptemplate);
3196 stream->channelpad[0] = gst_pad_new_from_template (template, name);
3197 gst_object_unref (template);
3200 /* set caps and activate */
3201 gst_pad_use_fixed_caps (stream->channelpad[0]);
3202 gst_pad_set_active (stream->channelpad[0], TRUE);
3204 *outpad = gst_object_ref (stream->channelpad[0]);
3206 GST_DEBUG_OBJECT (src, "using manager source pad");
3208 template = gst_static_pad_template_get (&anysrctemplate);
3210 /* allocate pads for sending the channel data into the manager */
3211 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
3212 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
3213 gst_object_unref (stream->channelpad[0]);
3214 stream->channelpad[0] = pad0;
3215 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
3216 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
3217 gst_pad_set_element_private (pad0, src);
3218 gst_pad_set_active (pad0, TRUE);
3220 if (stream->channelpad[1]) {
3221 /* if we have a sinkpad for the other channel, create a pad and link to the
3223 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
3224 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
3225 gst_pad_link_full (pad1, stream->channelpad[1],
3226 GST_PAD_LINK_CHECK_NOTHING);
3227 gst_object_unref (stream->channelpad[1]);
3228 stream->channelpad[1] = pad1;
3229 gst_pad_set_active (pad1, TRUE);
3231 gst_object_unref (template);
3233 /* setup RTCP transport back to the server if we have to. */
3234 if (src->manager && src->do_rtcp) {
3237 template = gst_static_pad_template_get (&anysinktemplate);
3239 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
3240 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
3241 gst_pad_set_element_private (stream->rtcppad, stream);
3242 gst_pad_set_active (stream->rtcppad, TRUE);
3244 /* get session RTCP pad */
3245 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3246 pad = gst_element_get_request_pad (src->manager, name);
3251 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3252 gst_object_unref (pad);
3255 gst_object_unref (template);
3261 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
3262 GstRTSPTransport * transport, const gchar ** destination, gint * min,
3263 gint * max, guint * ttl)
3265 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3267 if (!(*destination = transport->destination))
3268 *destination = stream->destination;
3271 /* transport first */
3272 *min = transport->port.min;
3273 *max = transport->port.max;
3274 if (*min == -1 && *max == -1) {
3275 /* then try from SDP */
3276 if (stream->port != 0) {
3277 *min = stream->port;
3278 *max = stream->port + 1;
3284 if (!(*ttl = transport->ttl))
3289 /* first take the source, then the endpoint to figure out where to send
3291 if (!(*destination = transport->source)) {
3292 if (src->conninfo.connection)
3293 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3294 else if (stream->conninfo.connection)
3296 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3300 /* for unicast we only expect the ports here */
3301 *min = transport->server_port.min;
3302 *max = transport->server_port.max;
3307 /* For multicast create UDP sources and join the multicast group. */
3309 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3310 GstRTSPTransport * transport, GstPad ** outpad)
3313 const gchar *destination;
3316 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3318 /* we can remove the allocated UDP ports now */
3319 gst_rtspsrc_stream_free_udp (stream);
3321 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3324 /* we need a destination now */
3325 if (destination == NULL)
3326 goto no_destination;
3328 /* we really need ports now or we won't be able to receive anything at all */
3329 if (min == -1 && max == -1)
3332 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3333 destination, min, max);
3335 /* creating UDP source for RTP */
3337 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3339 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3341 if (stream->udpsrc[0] == NULL)
3344 /* take ownership */
3345 gst_object_ref_sink (stream->udpsrc[0]);
3347 if (src->udp_buffer_size != 0)
3348 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3349 src->udp_buffer_size, NULL);
3351 if (src->multi_iface != NULL)
3352 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3353 src->multi_iface, NULL);
3356 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3357 gst_element_set_state (stream->udpsrc[0], GST_STATE_READY);
3360 /* creating another UDP source for RTCP */
3364 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3366 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3368 if (stream->udpsrc[1] == NULL)
3371 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3372 stream->profile == GST_RTSP_PROFILE_SAVPF)
3373 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3375 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3376 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3377 gst_caps_unref (caps);
3379 /* take ownership */
3380 gst_object_ref_sink (stream->udpsrc[1]);
3382 if (src->multi_iface != NULL)
3383 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3384 src->multi_iface, NULL);
3386 gst_element_set_state (stream->udpsrc[1], GST_STATE_READY);
3393 GST_DEBUG_OBJECT (src, "no UDP source element found");
3398 GST_DEBUG_OBJECT (src, "no destination found");
3403 GST_DEBUG_OBJECT (src, "no ports found");
3408 /* configure the remainder of the UDP ports */
3410 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3411 GstRTSPTransport * transport, GstPad ** outpad)
3413 /* we manage the UDP elements now. For unicast, the UDP sources where
3414 * allocated in the stream when we suggested a transport. */
3415 if (stream->udpsrc[0]) {
3418 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3419 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3421 GST_DEBUG_OBJECT (src, "setting up UDP source");
3423 /* configure a timeout on the UDP port. When the timeout message is
3424 * posted, we assume UDP transport is not possible. We reconnect using TCP
3426 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3427 src->udp_timeout * 1000, NULL);
3429 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt)))
3430 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
3432 /* get output pad of the UDP source. */
3433 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3435 /* save it so we can unblock */
3436 stream->blockedpad = *outpad;
3438 /* configure pad block on the pad. As soon as there is dataflow on the
3439 * UDP source, we know that UDP is not blocked by a firewall and we can
3440 * configure all the streams to let the application autoplug decoders. */
3442 gst_pad_add_probe (stream->blockedpad,
3443 GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM, pad_blocked, src, NULL);
3445 if (stream->channelpad[0]) {
3446 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3447 /* configure for UDP delivery, we need to connect the UDP pads to
3448 * the session plugin. */
3449 gst_pad_link_full (*outpad, stream->channelpad[0],
3450 GST_PAD_LINK_CHECK_NOTHING);
3451 gst_object_unref (*outpad);
3453 /* we connected to pad-added signal to get pads from the manager */
3455 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3460 if (stream->udpsrc[1]) {
3463 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3464 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3466 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3467 stream->profile == GST_RTSP_PROFILE_SAVPF)
3468 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3470 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3471 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3472 gst_caps_unref (caps);
3474 if (stream->channelpad[1]) {
3477 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3479 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3480 gst_pad_link_full (pad, stream->channelpad[1],
3481 GST_PAD_LINK_CHECK_NOTHING);
3482 gst_object_unref (pad);
3484 /* leave unlinked */
3490 /* configure the UDP sink back to the server for status reports */
3492 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3493 GstRTSPStream * stream, GstRTSPTransport * transport)
3496 gint rtp_port, rtcp_port;
3497 gboolean do_rtp, do_rtcp;
3498 const gchar *destination;
3503 /* get transport info */
3504 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
3505 &rtp_port, &rtcp_port, &ttl);
3507 /* see what we need to do */
3508 do_rtp = (rtp_port != -1);
3509 /* it's possible that the server does not want us to send RTCP in which case
3511 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
3513 /* we need a destination when we have RTP or RTCP ports */
3514 if (destination == NULL && (do_rtp || do_rtcp))
3515 goto no_destination;
3517 /* try to construct the fakesrc to the RTP port of the server to open up any
3520 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
3523 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
3524 stream->udpsink[0] =
3525 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3527 if (stream->udpsink[0] == NULL)
3528 goto no_sink_element;
3530 /* don't join multicast group, we will have the source socket do that */
3531 /* no sync or async state changes needed */
3532 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
3533 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3535 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3537 if (stream->udpsrc[0]) {
3538 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
3539 * so that NAT firewalls will open a hole for us */
3540 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
3541 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
3542 /* configure socket and make sure udpsink does not close it when shutting
3543 * down, it belongs to udpsrc after all. */
3544 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
3545 "close-socket", FALSE, NULL);
3546 g_object_unref (socket);
3549 /* the source for the dummy packets to open up NAT */
3550 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
3551 if (stream->fakesrc == NULL)
3552 goto no_fakesrc_element;
3554 /* random data in 5 buffers, a size of 200 bytes should be fine */
3555 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
3556 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
3558 /* we don't want to consider this a sink */
3559 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
3561 /* keep everything locked */
3562 gst_element_set_locked_state (stream->udpsink[0], TRUE);
3563 gst_element_set_locked_state (stream->fakesrc, TRUE);
3565 gst_object_ref (stream->udpsink[0]);
3566 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
3567 gst_object_ref (stream->fakesrc);
3568 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
3570 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
3571 "sink", GST_PAD_LINK_CHECK_NOTHING);
3574 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
3577 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
3578 stream->udpsink[1] =
3579 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3581 if (stream->udpsink[1] == NULL)
3582 goto no_sink_element;
3584 /* don't join multicast group, we will have the source socket do that */
3585 /* no sync or async state changes needed */
3586 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
3587 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3589 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3591 if (stream->udpsrc[1]) {
3592 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
3593 * because some servers check the port number of where it sends RTCP to identify
3594 * the RTCP packets it receives */
3595 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
3596 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
3597 /* configure socket and make sure udpsink does not close it when shutting
3598 * down, it belongs to udpsrc after all. */
3599 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
3600 "close-socket", FALSE, NULL);
3601 g_object_unref (socket);
3604 /* we don't want to consider this a sink */
3605 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
3607 /* we keep this playing always */
3608 gst_element_set_locked_state (stream->udpsink[1], TRUE);
3609 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
3611 gst_object_ref (stream->udpsink[1]);
3612 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
3614 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
3616 /* get session RTCP pad */
3617 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3618 pad = gst_element_get_request_pad (src->manager, name);
3623 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3624 gst_object_unref (pad);
3633 GST_DEBUG_OBJECT (src, "no destination address specified");
3638 GST_DEBUG_OBJECT (src, "no UDP sink element found");
3643 GST_DEBUG_OBJECT (src, "no fakesrc element found");
3648 /* sets up all elements needed for streaming over the specified transport.
3649 * Does not yet expose the element pads, this will be done when there is actuall
3650 * dataflow detected, which might never happen when UDP is blocked in a
3651 * firewall, for example.
3654 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
3655 GstRTSPTransport * transport)
3658 GstPad *outpad = NULL;
3659 GstPadTemplate *template;
3661 const gchar *media_type;
3664 src = stream->parent;
3666 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
3668 /* get the proper media type for this stream now */
3669 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
3670 goto unknown_transport;
3672 goto unknown_transport;
3674 /* configure the final media type */
3675 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
3677 len = stream->ptmap->len;
3678 for (i = 0; i < len; i++) {
3680 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3682 if (item->caps == NULL)
3685 s = gst_caps_get_structure (item->caps, 0);
3686 gst_structure_set_name (s, media_type);
3687 /* set ssrc if known */
3688 if (transport->ssrc)
3689 gst_structure_set (s, "ssrc", G_TYPE_UINT, transport->ssrc, NULL);
3692 /* try to get and configure a manager, channelpad[0-1] will be configured with
3693 * the pads for the manager, or NULL when no manager is needed. */
3694 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
3697 switch (transport->lower_transport) {
3698 case GST_RTSP_LOWER_TRANS_TCP:
3699 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
3700 goto transport_failed;
3702 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3703 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
3704 goto transport_failed;
3705 /* fallthrough, the rest is the same for UDP and MCAST */
3706 case GST_RTSP_LOWER_TRANS_UDP:
3707 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
3708 goto transport_failed;
3709 /* configure udpsinks back to the server for RTCP messages and for the
3710 * dummy RTP messages to open NAT. */
3711 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
3712 goto transport_failed;
3715 goto unknown_transport;
3719 GST_DEBUG_OBJECT (src, "creating ghostpad");
3721 gst_pad_use_fixed_caps (outpad);
3723 /* create ghostpad, don't add just yet, this will be done when we activate
3725 name = g_strdup_printf ("stream_%u", stream->id);
3726 template = gst_static_pad_template_get (&rtptemplate);
3727 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
3728 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3729 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3730 gst_object_unref (template);
3733 gst_object_unref (outpad);
3735 /* mark pad as ok */
3736 stream->last_ret = GST_FLOW_OK;
3743 GST_DEBUG_OBJECT (src, "failed to configure transport");
3748 GST_DEBUG_OBJECT (src, "unknown transport");
3753 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3758 /* send a couple of dummy random packets on the receiver RTP port to the server,
3759 * this should make a firewall think we initiated the data transfer and
3760 * hopefully allow packets to go from the sender port to our RTP receiver port */
3762 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
3766 if (src->nat_method != GST_RTSP_NAT_DUMMY)
3769 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3770 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3772 if (stream->fakesrc && stream->udpsink[0]) {
3773 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
3774 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
3775 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
3776 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
3777 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
3783 /* Adds the source pads of all configured streams to the element.
3784 * This code is performed when we detected dataflow.
3786 * We detect dataflow from either the _loop function or with pad probes on the
3790 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
3794 GST_DEBUG_OBJECT (src, "activating streams");
3796 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3797 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3799 if (stream->udpsrc[0]) {
3800 /* remove timeout, we are streaming now and timeouts will be handled by
3801 * the session manager and jitter buffer */
3802 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
3804 if (stream->srcpad) {
3805 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
3806 gst_pad_set_active (stream->srcpad, TRUE);
3808 /* if we don't have a session manager, set the caps now. If we have a
3809 * session, we will get a notification of the pad and the caps. */
3810 if (!src->manager) {
3813 caps = stream_get_caps_for_pt (stream, stream->default_pt);
3814 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
3815 gst_pad_set_caps (stream->srcpad, caps);
3818 if (!stream->added) {
3819 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
3820 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3821 stream->added = TRUE;
3826 /* unblock all pads */
3827 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3828 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3830 if (stream->blockid) {
3831 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
3832 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
3833 stream->blockid = 0;
3841 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
3842 gboolean reset_manager)
3845 guint64 start, stop;
3846 gdouble play_speed, play_scale;
3848 GST_DEBUG_OBJECT (src, "configuring stream caps");
3850 start = segment->position;
3851 stop = segment->duration;
3852 play_speed = segment->rate;
3853 play_scale = segment->applied_rate;
3855 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3856 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3862 len = stream->ptmap->len;
3863 for (j = 0; j < len; j++) {
3865 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
3867 if (item->caps == NULL)
3870 caps = gst_caps_make_writable (item->caps);
3872 if (stream->timebase != -1)
3873 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
3874 (guint) stream->timebase, NULL);
3875 if (stream->seqbase != -1)
3876 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
3877 (guint) stream->seqbase, NULL);
3878 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
3880 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
3881 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
3882 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
3885 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
3888 if (item->pt == stream->default_pt && stream->udpsrc[0]) {
3889 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
3893 if (reset_manager && src->manager) {
3894 GST_DEBUG_OBJECT (src, "clear session");
3895 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
3899 static GstFlowReturn
3900 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
3905 /* store the value */
3906 stream->last_ret = ret;
3908 /* if it's success we can return the value right away */
3909 if (ret == GST_FLOW_OK)
3912 /* any other error that is not-linked can be returned right
3914 if (ret != GST_FLOW_NOT_LINKED)
3917 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3918 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3919 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3921 ret = ostream->last_ret;
3922 /* some other return value (must be SUCCESS but we can return
3923 * other values as well) */
3924 if (ret != GST_FLOW_NOT_LINKED)
3927 /* if we get here, all other pads were unlinked and we return
3928 * NOT_LINKED then */
3934 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
3937 gboolean res = TRUE;
3939 /* only streams that have a connection to the outside world */
3943 if (stream->udpsrc[0]) {
3944 gst_event_ref (event);
3945 res = gst_element_send_event (stream->udpsrc[0], event);
3946 } else if (stream->channelpad[0]) {
3947 gst_event_ref (event);
3948 if (GST_PAD_IS_SRC (stream->channelpad[0]))
3949 res = gst_pad_push_event (stream->channelpad[0], event);
3951 res = gst_pad_send_event (stream->channelpad[0], event);
3954 if (stream->udpsrc[1]) {
3955 gst_event_ref (event);
3956 res &= gst_element_send_event (stream->udpsrc[1], event);
3957 } else if (stream->channelpad[1]) {
3958 gst_event_ref (event);
3959 if (GST_PAD_IS_SRC (stream->channelpad[1]))
3960 res &= gst_pad_push_event (stream->channelpad[1], event);
3962 res &= gst_pad_send_event (stream->channelpad[1], event);
3966 gst_event_unref (event);
3972 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
3975 gboolean res = TRUE;
3977 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3978 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3980 gst_event_ref (event);
3981 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
3983 gst_event_unref (event);
3988 static GstRTSPResult
3989 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3994 if (info->connection == NULL) {
3995 if (info->url == NULL) {
3996 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
3997 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
4001 /* create connection */
4002 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
4003 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
4004 goto could_not_create;
4007 g_free (info->url_str);
4008 info->url_str = gst_rtsp_url_get_request_uri (info->url);
4010 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
4012 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
4013 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
4014 src->tls_validation_flags))
4015 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
4017 if (src->tls_database)
4018 gst_rtsp_connection_set_tls_database (info->connection,
4022 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
4023 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
4025 if (src->proxy_host) {
4026 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
4028 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
4033 if (!info->connected) {
4036 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
4037 ("Connecting to %s", info->location));
4038 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
4040 gst_rtsp_connection_connect (info->connection,
4041 src->ptcp_timeout)) < 0)
4042 goto could_not_connect;
4044 info->connected = TRUE;
4051 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
4056 gchar *str = gst_rtsp_strresult (res);
4057 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
4063 gchar *str = gst_rtsp_strresult (res);
4064 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
4070 static GstRTSPResult
4071 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
4074 GST_RTSP_STATE_LOCK (src);
4075 if (info->connected) {
4076 GST_DEBUG_OBJECT (src, "closing connection...");
4077 gst_rtsp_connection_close (info->connection);
4078 info->connected = FALSE;
4080 if (free && info->connection) {
4081 /* free connection */
4082 GST_DEBUG_OBJECT (src, "freeing connection...");
4083 gst_rtsp_connection_free (info->connection);
4084 info->connection = NULL;
4086 GST_RTSP_STATE_UNLOCK (src);
4090 static GstRTSPResult
4091 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4096 GST_DEBUG_OBJECT (src, "reconnecting connection...");
4097 gst_rtsp_conninfo_close (src, info, FALSE);
4098 res = gst_rtsp_conninfo_connect (src, info, async);
4104 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
4108 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
4109 GST_RTSP_STATE_LOCK (src);
4110 if (src->conninfo.connection && src->conninfo.flushing != flush) {
4111 GST_DEBUG_OBJECT (src, "connection flush");
4112 gst_rtsp_connection_flush (src->conninfo.connection, flush);
4113 src->conninfo.flushing = flush;
4115 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4116 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4117 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
4118 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
4119 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
4120 stream->conninfo.flushing = flush;
4123 GST_RTSP_STATE_UNLOCK (src);
4126 /* FIXME, handle server request, reply with OK, for now */
4127 static GstRTSPResult
4128 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
4129 GstRTSPMessage * request)
4131 GstRTSPMessage response = { 0 };
4134 GST_DEBUG_OBJECT (src, "got server request message");
4137 gst_rtsp_message_dump (request);
4139 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
4141 if (res == GST_RTSP_ENOTIMPL) {
4142 /* default implementation, send OK */
4143 GST_DEBUG_OBJECT (src, "prepare OK reply");
4145 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
4150 /* let app parse and reply */
4151 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
4152 0, request, &response);
4155 gst_rtsp_message_dump (&response);
4157 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
4161 gst_rtsp_message_unset (&response);
4162 } else if (res == GST_RTSP_EEOF)
4170 gst_rtsp_message_unset (&response);
4175 /* send server keep-alive */
4176 static GstRTSPResult
4177 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
4179 GstRTSPMessage request = { 0 };
4181 GstRTSPMethod method;
4182 const gchar *control;
4184 if (src->do_rtsp_keep_alive == FALSE) {
4185 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
4186 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4190 GST_DEBUG_OBJECT (src, "creating server keep-alive");
4192 /* find a method to use for keep-alive */
4193 if (src->methods & GST_RTSP_GET_PARAMETER)
4194 method = GST_RTSP_GET_PARAMETER;
4196 method = GST_RTSP_OPTIONS;
4198 control = get_aggregate_control (src);
4199 if (control == NULL)
4202 res = gst_rtsp_message_init_request (&request, method, control);
4207 gst_rtsp_message_dump (&request);
4210 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
4215 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4216 gst_rtsp_message_unset (&request);
4223 GST_WARNING_OBJECT (src, "no control url to send keepalive");
4228 gchar *str = gst_rtsp_strresult (res);
4230 gst_rtsp_message_unset (&request);
4231 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
4232 ("Could not send keep-alive. (%s)", str));
4238 static GstFlowReturn
4239 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
4241 GstFlowReturn ret = GST_FLOW_OK;
4243 GstRTSPStream *stream;
4244 GstPad *outpad = NULL;
4251 channel = message->type_data.data.channel;
4253 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
4255 goto unknown_stream;
4257 if (channel == stream->channel[0]) {
4258 outpad = stream->channelpad[0];
4260 } else if (channel == stream->channel[1]) {
4261 outpad = stream->channelpad[1];
4267 /* take a look at the body to figure out what we have */
4268 gst_rtsp_message_get_body (message, &data, &size);
4270 goto invalid_length;
4272 /* channels are not correct on some servers, do extra check */
4273 if (data[1] >= 200 && data[1] <= 204) {
4274 /* hmm RTCP message switch to the RTCP pad of the same stream. */
4275 outpad = stream->channelpad[1];
4279 /* we have no clue what this is, just ignore then. */
4281 goto unknown_stream;
4283 /* take the message body for further processing */
4284 gst_rtsp_message_steal_body (message, &data, &size);
4286 /* strip the trailing \0 */
4289 buf = gst_buffer_new ();
4290 gst_buffer_append_memory (buf,
4291 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
4293 /* don't need message anymore */
4294 gst_rtsp_message_unset (message);
4296 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
4299 if (src->need_activate) {
4305 guint group_id = gst_util_group_id_next ();
4307 /* generate an SHA256 sum of the URI */
4308 cs = g_checksum_new (G_CHECKSUM_SHA256);
4309 uri = src->conninfo.location;
4310 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
4312 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4313 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4316 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
4317 event = gst_event_new_stream_start (stream_id);
4318 gst_event_set_group_id (event, group_id);
4321 gst_rtspsrc_stream_push_event (src, ostream, event);
4323 g_checksum_free (cs);
4325 gst_rtspsrc_activate_streams (src);
4326 src->need_activate = FALSE;
4328 if ((event = src->start_segment) != NULL) {
4329 src->start_segment = NULL;
4330 gst_rtspsrc_push_event (src, event);
4333 if (src->base_time == -1) {
4334 /* Take current running_time. This timestamp will be put on
4335 * the first buffer of each stream because we are a live source and so we
4336 * timestamp with the running_time. When we are dealing with TCP, we also
4337 * only timestamp the first buffer (using the DISCONT flag) because a server
4338 * typically bursts data, for which we don't want to compensate by speeding
4339 * up the media. The other timestamps will be interpollated from this one
4340 * using the RTP timestamps. */
4341 GST_OBJECT_LOCK (src);
4342 if (GST_ELEMENT_CLOCK (src)) {
4344 GstClockTime base_time;
4346 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
4347 base_time = GST_ELEMENT_CAST (src)->base_time;
4349 src->base_time = now - base_time;
4351 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
4352 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
4354 GST_OBJECT_UNLOCK (src);
4357 if (stream->discont && !is_rtcp) {
4358 /* mark first RTP buffer as discont */
4359 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4360 stream->discont = FALSE;
4361 /* first buffer gets the timestamp, other buffers are not timestamped and
4362 * their presentation time will be interpollated from the rtp timestamps. */
4363 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
4364 GST_TIME_ARGS (src->base_time));
4366 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
4369 /* chain to the peer pad */
4370 if (GST_PAD_IS_SINK (outpad))
4371 ret = gst_pad_chain (outpad, buf);
4373 ret = gst_pad_push (outpad, buf);
4376 /* combine all stream flows for the data transport */
4377 ret = gst_rtspsrc_combine_flows (src, stream, ret);
4384 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
4385 gst_rtsp_message_unset (message);
4390 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4391 ("Short message received, ignoring."));
4392 gst_rtsp_message_unset (message);
4397 static GstFlowReturn
4398 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
4400 GstRTSPMessage message = { 0 };
4402 GstFlowReturn ret = GST_FLOW_OK;
4403 GTimeVal tv_timeout;
4406 /* get the next timeout interval */
4407 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4409 /* see if the timeout period expired */
4410 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
4411 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
4412 /* send keep-alive, only act on interrupt, a warning will be posted for
4414 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4416 /* get new timeout */
4417 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4420 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
4421 tv_timeout.tv_sec, tv_timeout.tv_usec);
4423 /* protect the connection with the connection lock so that we can see when
4424 * we are finished doing server communication */
4426 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4427 &message, src->ptcp_timeout);
4431 GST_DEBUG_OBJECT (src, "we received a server message");
4433 case GST_RTSP_EINTR:
4434 /* we got interrupted this means we need to stop */
4436 case GST_RTSP_ETIMEOUT:
4437 /* no reply, send keep alive */
4438 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4439 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4443 /* go EOS when the server closed the connection */
4449 switch (message.type) {
4450 case GST_RTSP_MESSAGE_REQUEST:
4451 /* server sends us a request message, handle it */
4453 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4455 if (res == GST_RTSP_EEOF)
4458 goto handle_request_failed;
4460 case GST_RTSP_MESSAGE_RESPONSE:
4461 /* we ignore response messages */
4462 GST_DEBUG_OBJECT (src, "ignoring response message");
4464 gst_rtsp_message_dump (&message);
4466 case GST_RTSP_MESSAGE_DATA:
4467 GST_DEBUG_OBJECT (src, "got data message");
4468 ret = gst_rtspsrc_handle_data (src, &message);
4469 if (ret != GST_FLOW_OK)
4470 goto handle_data_failed;
4473 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4478 g_assert_not_reached ();
4483 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4484 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4485 ("The server closed the connection."));
4486 src->conninfo.connected = FALSE;
4487 gst_rtsp_message_unset (&message);
4488 return GST_FLOW_EOS;
4492 gst_rtsp_message_unset (&message);
4493 GST_DEBUG_OBJECT (src, "got interrupted");
4494 return GST_FLOW_FLUSHING;
4498 gchar *str = gst_rtsp_strresult (res);
4500 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4501 ("Could not receive message. (%s)", str));
4504 gst_rtsp_message_unset (&message);
4505 return GST_FLOW_ERROR;
4507 handle_request_failed:
4509 gchar *str = gst_rtsp_strresult (res);
4511 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4512 ("Could not handle server message. (%s)", str));
4514 gst_rtsp_message_unset (&message);
4515 return GST_FLOW_ERROR;
4519 GST_DEBUG_OBJECT (src, "could no handle data message");
4524 static GstFlowReturn
4525 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
4528 GstRTSPMessage message = { 0 };
4532 GTimeVal tv_timeout;
4534 /* get the next timeout interval */
4535 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4537 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
4538 (gint) tv_timeout.tv_sec);
4540 gst_rtsp_message_unset (&message);
4542 /* we should continue reading the TCP socket because the server might
4543 * send us requests. When the session timeout expires, we need to send a
4544 * keep-alive request to keep the session open. */
4545 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4546 &message, &tv_timeout);
4550 GST_DEBUG_OBJECT (src, "we received a server message");
4552 case GST_RTSP_EINTR:
4553 /* we got interrupted, see what we have to do */
4555 case GST_RTSP_ETIMEOUT:
4556 /* send keep-alive, ignore the result, a warning will be posted. */
4557 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4558 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4562 /* server closed the connection. not very fatal for UDP, reconnect and
4563 * see what happens. */
4564 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4565 ("The server closed the connection."));
4566 if (src->udp_reconnect) {
4568 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
4575 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
4577 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4578 ("Unhandled return value %d.", res));
4582 switch (message.type) {
4583 case GST_RTSP_MESSAGE_REQUEST:
4584 /* server sends us a request message, handle it */
4586 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4588 if (res == GST_RTSP_EEOF)
4591 goto handle_request_failed;
4593 case GST_RTSP_MESSAGE_RESPONSE:
4594 /* we ignore response and data messages */
4595 GST_DEBUG_OBJECT (src, "ignoring response message");
4597 gst_rtsp_message_dump (&message);
4598 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4599 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
4600 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
4601 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
4602 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4609 case GST_RTSP_MESSAGE_DATA:
4610 /* we ignore response and data messages */
4611 GST_DEBUG_OBJECT (src, "ignoring data message");
4614 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4619 g_assert_not_reached ();
4621 /* we get here when the connection got interrupted */
4624 gst_rtsp_message_unset (&message);
4625 GST_DEBUG_OBJECT (src, "got interrupted");
4626 return GST_FLOW_FLUSHING;
4630 gchar *str = gst_rtsp_strresult (res);
4633 src->conninfo.connected = FALSE;
4634 if (res != GST_RTSP_EINTR) {
4635 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
4636 ("Could not connect to server. (%s)", str));
4638 ret = GST_FLOW_ERROR;
4640 ret = GST_FLOW_FLUSHING;
4646 gchar *str = gst_rtsp_strresult (res);
4648 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4649 ("Could not receive message. (%s)", str));
4651 return GST_FLOW_ERROR;
4653 handle_request_failed:
4655 gchar *str = gst_rtsp_strresult (res);
4658 gst_rtsp_message_unset (&message);
4659 if (res != GST_RTSP_EINTR) {
4660 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4661 ("Could not handle server message. (%s)", str));
4663 ret = GST_FLOW_ERROR;
4665 ret = GST_FLOW_FLUSHING;
4671 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4672 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4673 ("The server closed the connection."));
4674 src->conninfo.connected = FALSE;
4675 gst_rtsp_message_unset (&message);
4676 return GST_FLOW_EOS;
4680 static GstRTSPResult
4681 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
4683 GstRTSPResult res = GST_RTSP_OK;
4686 GST_DEBUG_OBJECT (src, "doing reconnect");
4688 GST_OBJECT_LOCK (src);
4689 /* only restart when the pads were not yet activated, else we were
4690 * streaming over UDP */
4691 restart = src->need_activate;
4692 GST_OBJECT_UNLOCK (src);
4694 /* no need to restart, we're done */
4698 /* we can try only TCP now */
4699 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
4701 /* close and cleanup our state */
4702 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
4705 /* see if we have TCP left to try. Also don't try TCP when we were configured
4707 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
4710 /* We post a warning message now to inform the user
4711 * that nothing happened. It's most likely a firewall thing. */
4712 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4713 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4714 "firewall is blocking it. Retrying using a TCP connection.",
4715 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4717 /* open new connection using tcp */
4718 if (gst_rtspsrc_open (src, async) < 0)
4721 /* start playback */
4722 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
4731 src->cur_protocols = 0;
4732 /* no transport possible, post an error and stop */
4733 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4734 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4735 "firewall is blocking it. No other protocols to try.",
4736 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4737 return GST_RTSP_ERROR;
4741 GST_DEBUG_OBJECT (src, "open failed");
4746 GST_DEBUG_OBJECT (src, "play failed");
4752 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
4756 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
4759 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
4762 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
4765 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
4773 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
4777 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
4780 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
4783 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
4786 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
4794 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
4798 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
4801 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
4804 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
4807 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
4815 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
4819 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
4822 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
4825 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
4828 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
4836 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
4838 if (ret == GST_RTSP_OK)
4839 gst_rtspsrc_loop_complete_cmd (src, cmd);
4840 else if (ret == GST_RTSP_EINTR)
4841 gst_rtspsrc_loop_cancel_cmd (src, cmd);
4843 gst_rtspsrc_loop_error_cmd (src, cmd);
4847 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
4850 gboolean flushed = FALSE;
4852 /* start new request */
4853 gst_rtspsrc_loop_start_cmd (src, cmd);
4855 GST_DEBUG_OBJECT (src, "sending cmd %d", cmd);
4857 GST_OBJECT_LOCK (src);
4858 old = src->pending_cmd;
4859 if (old == CMD_RECONNECT) {
4860 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
4861 cmd = CMD_RECONNECT;
4863 if (old != CMD_WAIT) {
4864 src->pending_cmd = CMD_WAIT;
4865 GST_OBJECT_UNLOCK (src);
4866 /* cancel previous request */
4867 GST_DEBUG_OBJECT (src, "cancel previous request %d", old);
4868 gst_rtspsrc_loop_cancel_cmd (src, old);
4869 GST_OBJECT_LOCK (src);
4871 src->pending_cmd = cmd;
4872 /* interrupt if allowed */
4873 if (src->busy_cmd & mask) {
4874 GST_DEBUG_OBJECT (src, "connection flush busy %d", src->busy_cmd);
4875 gst_rtspsrc_connection_flush (src, TRUE);
4878 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %d", src->busy_cmd);
4881 gst_task_start (src->task);
4882 GST_OBJECT_UNLOCK (src);
4888 gst_rtspsrc_loop (GstRTSPSrc * src)
4892 if (!src->conninfo.connection || !src->conninfo.connected)
4895 if (src->interleaved)
4896 ret = gst_rtspsrc_loop_interleaved (src);
4898 ret = gst_rtspsrc_loop_udp (src);
4900 if (ret != GST_FLOW_OK)
4908 GST_WARNING_OBJECT (src, "we are not connected");
4909 ret = GST_FLOW_FLUSHING;
4914 const gchar *reason = gst_flow_get_name (ret);
4916 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
4917 src->running = FALSE;
4918 if (ret == GST_FLOW_EOS) {
4919 /* perform EOS logic */
4920 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4921 gst_element_post_message (GST_ELEMENT_CAST (src),
4922 gst_message_new_segment_done (GST_OBJECT_CAST (src),
4923 src->segment.format, src->segment.position));
4924 gst_rtspsrc_push_event (src,
4925 gst_event_new_segment_done (src->segment.format,
4926 src->segment.position));
4928 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4930 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
4931 /* for fatal errors we post an error message, post the error before the
4932 * EOS so the app knows about the error first. */
4933 GST_ELEMENT_ERROR (src, STREAM, FAILED,
4934 ("Internal data flow error."),
4935 ("streaming task paused, reason %s (%d)", reason, ret));
4936 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4938 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
4943 #ifndef GST_DISABLE_GST_DEBUG
4944 static const gchar *
4945 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
4949 while (method != 0) {
4966 static const gchar *
4967 gst_rtspsrc_skip_lws (const gchar * s)
4969 while (g_ascii_isspace (*s))
4974 static const gchar *
4975 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
4977 while (s > start && g_ascii_isspace (*(s - 1)))
4982 static const gchar *
4983 gst_rtspsrc_skip_commas (const gchar * s)
4985 /* The grammar allows for multiple commas */
4986 while (g_ascii_isspace (*s) || *s == ',')
4991 static const gchar *
4992 gst_rtspsrc_skip_item (const gchar * s)
4994 gboolean quoted = FALSE;
4995 const gchar *start = s;
4997 /* A list item ends at the last non-whitespace character
4998 * before a comma which is not inside a quoted-string. Or at
4999 * the end of the string.
5005 if (*s == '\\' && *(s + 1))
5014 return gst_rtspsrc_unskip_lws (s, start);
5018 gst_rtsp_decode_quoted_string (gchar * quoted_string)
5022 src = quoted_string + 1;
5023 dst = quoted_string;
5024 while (*src && *src != '"') {
5025 if (*src == '\\' && *(src + 1))
5032 /* Extract the authentication tokens that the server provided for each method
5033 * into an array of structures and give those to the connection object.
5036 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
5037 const gchar * header, gboolean * stale)
5039 GSList *list = NULL, *iter;
5041 gchar *item, *eq, *name_end, *value;
5043 g_return_if_fail (stale != NULL);
5045 gst_rtsp_connection_clear_auth_params (conn);
5048 /* Parse a header whose content is described by RFC2616 as
5049 * "#something", where "something" does not itself contain commas,
5050 * except as part of quoted-strings, into a list of allocated strings.
5052 header = gst_rtspsrc_skip_commas (header);
5054 end = gst_rtspsrc_skip_item (header);
5055 list = g_slist_prepend (list, g_strndup (header, end - header));
5056 header = gst_rtspsrc_skip_commas (end);
5061 list = g_slist_reverse (list);
5062 for (iter = list; iter; iter = iter->next) {
5065 eq = strchr (item, '=');
5067 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
5068 if (name_end == item) {
5069 /* That's no good... */
5076 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
5078 gst_rtsp_decode_quoted_string (value);
5082 if (item && (strcmp (item, "stale") == 0) &&
5083 value && (strcmp (value, "TRUE") == 0))
5085 gst_rtsp_connection_set_auth_param (conn, item, value);
5089 g_slist_free (list);
5092 /* Parse a WWW-Authenticate Response header and determine the
5093 * available authentication methods
5095 * This code should also cope with the fact that each WWW-Authenticate
5096 * header can contain multiple challenge methods + tokens
5098 * At the moment, for Basic auth, we just do a minimal check and don't
5099 * even parse out the realm */
5101 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
5102 GstRTSPConnection * conn, gboolean * stale)
5106 g_return_if_fail (hdr != NULL);
5107 g_return_if_fail (methods != NULL);
5108 g_return_if_fail (stale != NULL);
5110 /* Skip whitespace at the start of the string */
5111 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
5113 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
5114 *methods |= GST_RTSP_AUTH_BASIC;
5115 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
5116 *methods |= GST_RTSP_AUTH_DIGEST;
5117 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
5122 * gst_rtspsrc_setup_auth:
5123 * @src: the rtsp source
5125 * Configure a username and password and auth method on the
5126 * connection object based on a response we received from the
5129 * Currently, this requires that a username and password were supplied
5130 * in the uri. In the future, they may be requested on demand by sending
5131 * a message up the bus.
5133 * Returns: TRUE if authentication information could be set up correctly.
5136 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
5140 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
5141 GstRTSPAuthMethod method;
5142 GstRTSPResult auth_result;
5144 GstRTSPConnection *conn;
5146 gboolean stale = FALSE;
5148 conn = src->conninfo.connection;
5150 /* Identify the available auth methods and see if any are supported */
5151 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
5152 &hdr, 0) == GST_RTSP_OK) {
5153 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
5156 if (avail_methods == GST_RTSP_AUTH_NONE)
5157 goto no_auth_available;
5159 /* For digest auth, if the response indicates that the session
5160 * data are stale, we just update them in the connection object and
5161 * return TRUE to retry the request */
5163 src->tried_url_auth = FALSE;
5165 url = gst_rtsp_connection_get_url (conn);
5167 /* Do we have username and password available? */
5168 if (url != NULL && !src->tried_url_auth && url->user != NULL
5169 && url->passwd != NULL) {
5172 src->tried_url_auth = TRUE;
5173 GST_DEBUG_OBJECT (src,
5174 "Attempting authentication using credentials from the URL");
5176 user = src->user_id;
5177 pass = src->user_pw;
5178 GST_DEBUG_OBJECT (src,
5179 "Attempting authentication using credentials from the properties");
5182 /* FIXME: If the url didn't contain username and password or we tried them
5183 * already, request a username and passwd from the application via some kind
5184 * of credentials request message */
5186 /* If we don't have a username and passwd at this point, bail out. */
5187 if (user == NULL || pass == NULL)
5190 /* Try to configure for each available authentication method, strongest to
5192 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
5193 /* Check if this method is available on the server */
5194 if ((method & avail_methods) == 0)
5197 /* Pass the credentials to the connection to try on the next request */
5198 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
5199 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
5200 * ignore it and end up retrying later */
5201 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
5202 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
5203 gst_rtsp_auth_method_to_string (method));
5208 if (method == GST_RTSP_AUTH_NONE)
5209 goto no_auth_available;
5215 /* Output an error indicating that we couldn't connect because there were
5216 * no supported authentication protocols */
5217 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5218 ("No supported authentication protocol was found"));
5223 /* We don't fire an error message, we just return FALSE and let the
5224 * normal NOT_AUTHORIZED error be propagated */
5229 static GstRTSPResult
5230 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5231 GstRTSPMessage * request, GstRTSPMessage * response,
5232 GstRTSPStatusCode * code)
5235 GstRTSPStatusCode thecode;
5236 gchar *content_base = NULL;
5240 if (!src->short_header)
5241 gst_rtsp_ext_list_before_send (src->extensions, request);
5243 GST_DEBUG_OBJECT (src, "sending message");
5246 gst_rtsp_message_dump (request);
5248 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
5252 gst_rtsp_connection_reset_timeout (conn);
5255 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
5260 gst_rtsp_message_dump (response);
5262 switch (response->type) {
5263 case GST_RTSP_MESSAGE_REQUEST:
5264 res = gst_rtspsrc_handle_request (src, conn, response);
5265 if (res == GST_RTSP_EEOF)
5268 goto handle_request_failed;
5270 case GST_RTSP_MESSAGE_RESPONSE:
5271 /* ok, a response is good */
5272 GST_DEBUG_OBJECT (src, "received response message");
5274 case GST_RTSP_MESSAGE_DATA:
5275 /* get next response */
5276 GST_DEBUG_OBJECT (src, "handle data response message");
5277 gst_rtspsrc_handle_data (src, response);
5280 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5285 thecode = response->type_data.response.code;
5287 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
5289 /* if the caller wanted the result code, we store it. */
5293 /* If the request didn't succeed, bail out before doing any more */
5294 if (thecode != GST_RTSP_STS_OK)
5297 /* store new content base if any */
5298 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
5301 g_free (src->content_base);
5302 src->content_base = g_strdup (content_base);
5304 gst_rtsp_ext_list_after_send (src->extensions, request, response);
5311 gchar *str = gst_rtsp_strresult (res);
5313 if (res != GST_RTSP_EINTR) {
5314 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5315 ("Could not send message. (%s)", str));
5317 GST_WARNING_OBJECT (src, "send interrupted");
5326 GST_WARNING_OBJECT (src, "server closed connection");
5327 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
5329 /* if reconnect succeeds, try again */
5331 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
5335 /* only try once after reconnect, then fallthrough and error out */
5338 gchar *str = gst_rtsp_strresult (res);
5340 if (res != GST_RTSP_EINTR) {
5341 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5342 ("Could not receive message. (%s)", str));
5344 GST_WARNING_OBJECT (src, "receive interrupted");
5352 handle_request_failed:
5354 /* ERROR was posted */
5355 gst_rtsp_message_unset (response);
5360 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5361 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5362 ("The server closed the connection."));
5363 gst_rtsp_message_unset (response);
5370 * @src: the rtsp source
5371 * @conn: the connection to send on
5372 * @request: must point to a valid request
5373 * @response: must point to an empty #GstRTSPMessage
5374 * @code: an optional code result
5376 * send @request and retrieve the response in @response. optionally @code can be
5377 * non-NULL in which case it will contain the status code of the response.
5379 * If This function returns #GST_RTSP_OK, @response will contain a valid response
5380 * message that should be cleaned with gst_rtsp_message_unset() after usage.
5382 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
5383 * @response message) if the response code was not 200 (OK).
5385 * If the attempt results in an authentication failure, then this will attempt
5386 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
5389 * Returns: #GST_RTSP_OK if the processing was successful.
5391 static GstRTSPResult
5392 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5393 GstRTSPMessage * request, GstRTSPMessage * response,
5394 GstRTSPStatusCode * code)
5396 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
5397 GstRTSPResult res = GST_RTSP_ERROR;
5400 GstRTSPMethod method = GST_RTSP_INVALID;
5406 /* make sure we don't loop forever */
5410 /* save method so we can disable it when the server complains */
5411 method = request->type_data.request.method;
5414 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
5418 case GST_RTSP_STS_UNAUTHORIZED:
5419 if (gst_rtspsrc_setup_auth (src, response)) {
5420 /* Try the request/response again after configuring the auth info
5428 } while (retry == TRUE);
5430 /* If the user requested the code, let them handle errors, otherwise
5431 * post an error below */
5434 else if (int_code != GST_RTSP_STS_OK)
5435 goto error_response;
5442 GST_DEBUG_OBJECT (src, "got error %d", res);
5447 res = GST_RTSP_ERROR;
5449 switch (response->type_data.response.code) {
5450 case GST_RTSP_STS_NOT_FOUND:
5451 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
5452 response->type_data.response.reason));
5454 case GST_RTSP_STS_MOVED_PERMANENTLY:
5455 case GST_RTSP_STS_MOVE_TEMPORARILY:
5457 gchar *new_location;
5458 GstRTSPLowerTrans transports;
5460 GST_DEBUG_OBJECT (src, "got redirection");
5461 /* if we don't have a Location Header, we must error */
5462 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
5463 &new_location, 0) < 0)
5466 /* When we receive a redirect result, we go back to the INIT state after
5467 * parsing the new URI. The caller should do the needed steps to issue
5468 * a new setup when it detects this state change. */
5469 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
5471 /* save current transports */
5472 if (src->conninfo.url)
5473 transports = src->conninfo.url->transports;
5475 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
5477 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
5479 /* set old transports */
5480 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
5481 src->conninfo.url->transports = transports;
5483 src->need_redirect = TRUE;
5484 src->state = GST_RTSP_STATE_INIT;
5488 case GST_RTSP_STS_NOT_ACCEPTABLE:
5489 case GST_RTSP_STS_NOT_IMPLEMENTED:
5490 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
5491 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
5492 gst_rtsp_method_as_text (method));
5493 src->methods &= ~method;
5497 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5498 ("Got error response: %d (%s).", response->type_data.response.code,
5499 response->type_data.response.reason));
5502 /* if we return ERROR we should unset the response ourselves */
5503 if (res == GST_RTSP_ERROR)
5504 gst_rtsp_message_unset (response);
5510 static GstRTSPResult
5511 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
5512 GstRTSPMessage * response, GstRTSPSrc * src)
5514 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
5519 /* parse the response and collect all the supported methods. We need this
5520 * information so that we don't try to send an unsupported request to the
5524 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
5526 GstRTSPHeaderField field;
5530 /* reset supported methods */
5533 /* Try Allow Header first */
5534 field = GST_RTSP_HDR_ALLOW;
5537 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5538 if (indx == 0 && !respoptions) {
5539 /* if no Allow header was found then try the Public header... */
5540 field = GST_RTSP_HDR_PUBLIC;
5541 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5546 src->methods |= gst_rtsp_options_from_text (respoptions);
5551 if (src->methods == 0) {
5552 /* neither Allow nor Public are required, assume the server supports
5553 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
5555 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
5556 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
5558 /* always assume PLAY, FIXME, extensions should be able to override
5560 src->methods |= GST_RTSP_PLAY;
5561 /* also assume it will support Range */
5562 src->seekable = TRUE;
5564 /* we need describe and setup */
5565 if (!(src->methods & GST_RTSP_DESCRIBE))
5567 if (!(src->methods & GST_RTSP_SETUP))
5575 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5576 ("Server does not support DESCRIBE."));
5581 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5582 ("Server does not support SETUP."));
5587 /* masks to be kept in sync with the hardcoded protocol order of preference
5589 static guint protocol_masks[] = {
5590 GST_RTSP_LOWER_TRANS_UDP,
5591 GST_RTSP_LOWER_TRANS_UDP_MCAST,
5592 GST_RTSP_LOWER_TRANS_TCP,
5596 static GstRTSPResult
5597 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
5598 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
5602 gboolean add_udp_str;
5607 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
5612 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
5614 /* extension listed transports, use those */
5615 if (*transports != NULL)
5618 /* it's the default */
5619 add_udp_str = FALSE;
5621 /* the default RTSP transports */
5622 result = g_string_new ("RTP");
5625 case GST_RTSP_PROFILE_AVP:
5626 g_string_append (result, "/AVP");
5628 case GST_RTSP_PROFILE_SAVP:
5629 g_string_append (result, "/SAVP");
5631 case GST_RTSP_PROFILE_AVPF:
5632 g_string_append (result, "/AVPF");
5634 case GST_RTSP_PROFILE_SAVPF:
5635 g_string_append (result, "/SAVPF");
5641 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
5642 GST_DEBUG_OBJECT (src, "adding UDP unicast");
5644 g_string_append (result, "/UDP");
5645 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
5646 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
5647 GST_DEBUG_OBJECT (src, "adding UDP multicast");
5648 /* we don't have to allocate any UDP ports yet, if the selected transport
5649 * turns out to be multicast we can create them and join the multicast
5650 * group indicated in the transport reply */
5652 g_string_append (result, "/UDP");
5653 g_string_append (result, ";multicast");
5654 if (src->next_port_num != 0) {
5655 if (src->client_port_range.max > 0 &&
5656 src->next_port_num >= src->client_port_range.max)
5659 g_string_append_printf (result, ";client_port=%d-%d",
5660 src->next_port_num, src->next_port_num + 1);
5662 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
5663 GST_DEBUG_OBJECT (src, "adding TCP");
5665 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
5667 *transports = g_string_free (result, FALSE);
5669 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
5676 GST_ERROR ("extension gave error %d", res);
5681 GST_ERROR ("no more ports available");
5682 return GST_RTSP_ERROR;
5686 static GstRTSPResult
5687 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
5688 gint orig_rtpport, gint orig_rtcpport)
5691 gint nr_udp, nr_int;
5693 gint rtpport = 0, rtcpport = 0;
5696 src = stream->parent;
5698 /* find number of placeholders first */
5699 if (strstr (*transports, "%%i2"))
5701 else if (strstr (*transports, "%%i1"))
5706 if (strstr (*transports, "%%u2"))
5708 else if (strstr (*transports, "%%u1"))
5713 if (nr_udp == 0 && nr_int == 0)
5717 if (!orig_rtpport || !orig_rtcpport) {
5718 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
5721 rtpport = orig_rtpport;
5722 rtcpport = orig_rtcpport;
5726 str = g_string_new ("");
5728 while ((next = strstr (p, "%%"))) {
5729 g_string_append_len (str, p, next - p);
5730 if (next[2] == 'u') {
5732 g_string_append_printf (str, "%d", rtpport);
5733 else if (next[3] == '2')
5734 g_string_append_printf (str, "%d", rtcpport);
5736 if (next[2] == 'i') {
5738 g_string_append_printf (str, "%d", src->free_channel);
5739 else if (next[3] == '2')
5740 g_string_append_printf (str, "%d", src->free_channel + 1);
5745 /* append final part */
5746 g_string_append (str, p);
5748 g_free (*transports);
5749 *transports = g_string_free (str, FALSE);
5757 GST_ERROR ("failed to allocate udp ports");
5758 return GST_RTSP_ERROR;
5762 /* Perform the SETUP request for all the streams.
5764 * We ask the server for a specific transport, which initially includes all the
5765 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
5766 * two local UDP ports that we send to the server.
5768 * Once the server replied with a transport, we configure the other streams
5769 * with the same transport.
5771 * This function will also configure the stream for the selected transport,
5772 * which basically means creating the pipeline.
5774 static GstRTSPResult
5775 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
5778 GstRTSPResult res = GST_RTSP_ERROR;
5779 GstRTSPMessage request = { 0 };
5780 GstRTSPMessage response = { 0 };
5781 GstRTSPStream *stream = NULL;
5782 GstRTSPLowerTrans protocols;
5783 GstRTSPStatusCode code;
5784 gboolean unsupported_real = FALSE;
5785 gint rtpport, rtcpport;
5789 if (src->conninfo.connection) {
5790 url = gst_rtsp_connection_get_url (src->conninfo.connection);
5791 /* we initially allow all configured lower transports. based on the URL
5792 * transports and the replies from the server we narrow them down. */
5793 protocols = url->transports & src->cur_protocols;
5796 protocols = src->cur_protocols;
5802 /* reset some state */
5803 src->free_channel = 0;
5804 src->interleaved = FALSE;
5805 src->need_activate = FALSE;
5806 /* keep track of next port number, 0 is random */
5807 src->next_port_num = src->client_port_range.min;
5808 rtpport = rtcpport = 0;
5810 if (G_UNLIKELY (src->streams == NULL))
5813 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5814 GstRTSPConnection *conn;
5821 stream = (GstRTSPStream *) walk->data;
5823 caps = stream_get_caps_for_pt (stream, stream->default_pt);
5825 GST_DEBUG_OBJECT (src, "skipping stream %p, no caps", stream);
5829 if (stream->skipped) {
5830 GST_DEBUG_OBJECT (src, "skipping stream %p", stream);
5834 /* see if we need to configure this stream */
5835 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
5836 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
5841 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
5842 stream->id, caps, &selected);
5844 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
5848 /* merge/overwrite global caps */
5853 s = gst_caps_get_structure (caps, 0);
5855 num = gst_structure_n_fields (src->props);
5856 for (j = 0; j < num; j++) {
5860 name = gst_structure_nth_field_name (src->props, j);
5861 val = gst_structure_get_value (src->props, name);
5862 gst_structure_set_value (s, name, val);
5864 GST_DEBUG_OBJECT (src, "copied %s", name);
5868 /* skip setup if we have no URL for it */
5869 if (stream->conninfo.location == NULL) {
5870 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
5874 if (src->conninfo.connection == NULL) {
5875 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
5876 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
5879 conn = stream->conninfo.connection;
5881 conn = src->conninfo.connection;
5883 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
5884 stream->conninfo.location);
5886 /* if we have a multicast connection, only suggest multicast from now on */
5887 if (stream->is_multicast)
5888 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
5891 /* first selectable protocol */
5892 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5894 if (!protocol_masks[mask])
5898 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
5899 protocol_masks[mask]);
5900 /* create a string with first transport in line */
5902 res = gst_rtspsrc_create_transports_string (src,
5903 protocols & protocol_masks[mask], stream->profile, &transports);
5904 if (res < 0 || transports == NULL)
5905 goto setup_transport_failed;
5907 if (strlen (transports) == 0) {
5908 g_free (transports);
5909 GST_DEBUG_OBJECT (src, "no transports found");
5914 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
5916 /* replace placeholders with real values, this function will optionally
5917 * allocate UDP ports and other info needed to execute the setup request */
5918 res = gst_rtspsrc_prepare_transports (stream, &transports,
5919 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
5921 g_free (transports);
5922 goto setup_transport_failed;
5925 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
5927 /* create SETUP request */
5929 gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
5930 stream->conninfo.location);
5932 g_free (transports);
5933 goto create_request_failed;
5936 /* select transport */
5937 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
5939 /* if the user wants a non default RTP packet size we add the blocksize
5941 if (src->rtp_blocksize > 0) {
5942 hval = g_strdup_printf ("%d", src->rtp_blocksize);
5943 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
5947 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
5950 /* handle the code ourselves */
5951 if ((res = gst_rtspsrc_send (src, conn, &request, &response, &code) < 0))
5955 case GST_RTSP_STS_OK:
5957 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
5958 gst_rtsp_message_unset (&request);
5959 gst_rtsp_message_unset (&response);
5960 /* cleanup of leftover transport */
5961 gst_rtspsrc_stream_free_udp (stream);
5962 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
5963 * we might be in this case */
5964 if (stream->container && rtpport && rtcpport && !retry) {
5965 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
5970 /* this transport did not go down well, but we may have others to try
5971 * that we did not send yet, try those and only give up then
5972 * but not without checking for lost cause/extension so we can
5973 * post a nicer/more useful error message later */
5974 if (!unsupported_real)
5975 unsupported_real = stream->is_real;
5976 /* select next available protocol, give up on this stream if none */
5978 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5980 if (!protocol_masks[mask] || unsupported_real)
5985 /* cleanup of leftover transport and move to the next stream */
5986 gst_rtspsrc_stream_free_udp (stream);
5987 goto response_error;
5990 /* parse response transport */
5992 gchar *resptrans = NULL;
5993 GstRTSPTransport transport = { 0 };
5995 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
5998 gst_rtspsrc_stream_free_udp (stream);
6002 /* parse transport, go to next stream on parse error */
6003 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
6004 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
6008 /* update allowed transports for other streams. once the transport of
6009 * one stream has been determined, we make sure that all other streams
6010 * are configured in the same way */
6011 switch (transport.lower_transport) {
6012 case GST_RTSP_LOWER_TRANS_TCP:
6013 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
6014 protocols = GST_RTSP_LOWER_TRANS_TCP;
6015 src->interleaved = TRUE;
6016 /* update free channels */
6018 MAX (transport.interleaved.min, src->free_channel);
6020 MAX (transport.interleaved.max, src->free_channel);
6021 src->free_channel++;
6023 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
6024 /* only allow multicast for other streams */
6025 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
6026 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
6027 /* if the server selected our ports, increment our counters so that
6028 * we select a new port later */
6029 if (src->next_port_num == transport.port.min &&
6030 src->next_port_num + 1 == transport.port.max) {
6031 src->next_port_num += 2;
6034 case GST_RTSP_LOWER_TRANS_UDP:
6035 /* only allow unicast for other streams */
6036 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
6037 protocols = GST_RTSP_LOWER_TRANS_UDP;
6040 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
6041 transport.lower_transport);
6045 if (!stream->container || (!src->interleaved && !retry)) {
6046 /* now configure the stream with the selected transport */
6047 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
6048 GST_DEBUG_OBJECT (src,
6049 "could not configure stream %p transport, skipping stream",
6052 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
6053 /* retain the first allocated UDP port pair */
6054 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
6055 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
6058 /* we need to activate at least one streams when we detect activity */
6059 src->need_activate = TRUE;
6061 /* stream is setup now */
6062 stream->setup = TRUE;
6067 GstRTSPStream *sskip;
6069 skip = g_list_next (skip);
6073 sskip = (GstRTSPStream *) skip->data;
6075 /* skip all streams with the same control url */
6076 if (g_str_equal (stream->conninfo.location, sskip->conninfo.location)) {
6077 GST_DEBUG_OBJECT (src, "found stream %p with same control %s",
6078 sskip, sskip->conninfo.location);
6079 sskip->skipped = TRUE;
6084 /* clean up our transport struct */
6085 gst_rtsp_transport_init (&transport);
6086 /* clean up used RTSP messages */
6087 gst_rtsp_message_unset (&request);
6088 gst_rtsp_message_unset (&response);
6092 /* store the transport protocol that was configured */
6093 src->cur_protocols = protocols;
6095 gst_rtsp_ext_list_stream_select (src->extensions, url);
6097 /* if there is nothing to activate, error out */
6098 if (!src->need_activate)
6099 goto nothing_to_activate;
6106 /* no transport possible, post an error and stop */
6107 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6108 ("Could not connect to server, no protocols left"));
6109 return GST_RTSP_ERROR;
6113 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6114 ("SDP contains no streams"));
6115 return GST_RTSP_ERROR;
6117 create_request_failed:
6119 gchar *str = gst_rtsp_strresult (res);
6121 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6122 ("Could not create request. (%s)", str));
6126 setup_transport_failed:
6128 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6129 ("Could not setup transport."));
6130 res = GST_RTSP_ERROR;
6135 const gchar *str = gst_rtsp_status_as_text (code);
6137 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6138 ("Error (%d): %s", code, GST_STR_NULL (str)));
6139 res = GST_RTSP_ERROR;
6144 gchar *str = gst_rtsp_strresult (res);
6146 if (res != GST_RTSP_EINTR) {
6147 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6148 ("Could not send message. (%s)", str));
6150 GST_WARNING_OBJECT (src, "send interrupted");
6157 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6158 ("Server did not select transport."));
6159 res = GST_RTSP_ERROR;
6162 nothing_to_activate:
6164 /* none of the available error codes is really right .. */
6165 if (unsupported_real) {
6166 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6167 (_("No supported stream was found. You might need to install a "
6168 "GStreamer RTSP extension plugin for Real media streams.")),
6171 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6172 (_("No supported stream was found. You might need to allow "
6173 "more transport protocols or may otherwise be missing "
6174 "the right GStreamer RTSP extension plugin.")), (NULL));
6176 return GST_RTSP_ERROR;
6180 gst_rtsp_message_unset (&request);
6181 gst_rtsp_message_unset (&response);
6187 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
6188 GstSegment * segment)
6191 GstRTSPTimeRange *therange;
6194 gst_rtsp_range_free (src->range);
6196 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
6197 GST_DEBUG_OBJECT (src, "parsed range %s", range);
6198 src->range = therange;
6200 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
6202 gst_segment_init (segment, GST_FORMAT_TIME);
6206 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
6207 therange->min.type, therange->min.seconds, therange->max.type,
6208 therange->max.seconds);
6210 if (therange->min.type == GST_RTSP_TIME_NOW)
6212 else if (therange->min.type == GST_RTSP_TIME_END)
6215 seconds = therange->min.seconds * GST_SECOND;
6217 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
6218 GST_TIME_ARGS (seconds));
6220 /* we need to start playback without clipping from the position reported by
6222 segment->start = seconds;
6223 segment->position = seconds;
6225 if (therange->max.type == GST_RTSP_TIME_NOW)
6227 else if (therange->max.type == GST_RTSP_TIME_END)
6230 seconds = therange->max.seconds * GST_SECOND;
6232 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
6233 GST_TIME_ARGS (seconds));
6235 /* live (WMS) server might send overflowed large max as its idea of infinity,
6236 * compensate to prevent problems later on */
6237 if (seconds != -1 && seconds < 0) {
6239 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
6242 /* live (WMS) might send min == max, which is not worth recording */
6243 if (segment->duration == -1 && seconds == segment->start)
6246 /* don't change duration with unknown value, we might have a valid value
6247 * there that we want to keep. */
6249 segment->duration = seconds;
6254 /* Parse clock profived by the server with following syntax:
6256 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
6259 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
6261 gboolean res = FALSE;
6263 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
6264 gchar **fields = NULL, **parts = NULL;
6265 gchar *remote_ip, *str;
6267 GstClockTime base_time;
6270 fields = g_strsplit (gstclock, " ", 0);
6272 /* wrapped clock, not very interesting for now */
6273 if (fields[1] == NULL)
6276 /* remote IP address and port */
6277 if ((str = fields[2]) == NULL)
6280 parts = g_strsplit (str, ":", 0);
6282 if ((remote_ip = parts[0]) == NULL)
6285 if ((str = parts[1]) == NULL)
6293 if ((str = fields[3]) == NULL)
6296 base_time = g_ascii_strtoull (str, NULL, 10);
6299 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
6302 if (src->provided_clock)
6303 gst_object_unref (src->provided_clock);
6304 src->provided_clock = netclock;
6306 gst_element_post_message (GST_ELEMENT_CAST (src),
6307 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
6308 src->provided_clock, TRUE));
6312 g_strfreev (fields);
6318 /* must be called with the RTSP state lock */
6319 static GstRTSPResult
6320 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
6326 /* prepare global stream caps properties */
6328 gst_structure_remove_all_fields (src->props);
6330 src->props = gst_structure_new_empty ("RTSPProperties");
6333 gst_sdp_message_dump (sdp);
6335 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
6337 /* let the app inspect and change the SDP */
6338 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
6340 gst_segment_init (&src->segment, GST_FORMAT_TIME);
6342 /* parse range for duration reporting. */
6347 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
6351 /* keep track of the range and configure it in the segment */
6352 if (gst_rtspsrc_parse_range (src, range, &src->segment))
6356 /* parse clock information. This is GStreamer specific, a server can tell the
6357 * client what clock it is using and wrap that in a network clock. The
6358 * advantage of that is that we can slave to it. */
6360 const gchar *gstclock;
6363 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
6364 if (gstclock == NULL)
6367 /* parse the clock and expose it in the provide_clock method */
6368 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
6372 /* try to find a global control attribute. Note that a '*' means that we should
6373 * do aggregate control with the current url (so we don't do anything and
6374 * leave the current connection as is) */
6376 const gchar *control;
6379 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
6380 if (control == NULL)
6383 /* only take fully qualified urls */
6384 if (g_str_has_prefix (control, "rtsp://"))
6388 g_free (src->conninfo.location);
6389 src->conninfo.location = g_strdup (control);
6390 /* make a connection for this, if there was a connection already, nothing
6392 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
6393 GST_ERROR_OBJECT (src, "could not connect");
6396 /* we need to keep the control url separate from the connection url because
6397 * the rules for constructing the media control url need it */
6398 g_free (src->control);
6399 src->control = g_strdup (control);
6402 /* create streams */
6403 n_streams = gst_sdp_message_medias_len (sdp);
6404 for (i = 0; i < n_streams; i++) {
6405 gst_rtspsrc_create_stream (src, sdp, i);
6408 src->state = GST_RTSP_STATE_INIT;
6411 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
6414 /* reset our state */
6415 src->need_range = TRUE;
6418 src->state = GST_RTSP_STATE_READY;
6425 GST_ERROR_OBJECT (src, "setup failed");
6426 gst_rtspsrc_cleanup (src);
6431 static GstRTSPResult
6432 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
6436 GstRTSPMessage request = { 0 };
6437 GstRTSPMessage response = { 0 };
6440 gchar *respcont = NULL;
6443 src->need_redirect = FALSE;
6445 /* can't continue without a valid url */
6446 if (G_UNLIKELY (src->conninfo.url == NULL)) {
6447 res = GST_RTSP_EINVAL;
6450 src->tried_url_auth = FALSE;
6452 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
6453 goto connect_failed;
6455 /* create OPTIONS */
6456 GST_DEBUG_OBJECT (src, "create options...");
6458 gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
6459 src->conninfo.url_str);
6461 goto create_request_failed;
6464 GST_DEBUG_OBJECT (src, "send options...");
6467 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
6470 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6475 if (!gst_rtspsrc_parse_methods (src, &response))
6478 /* create DESCRIBE */
6479 GST_DEBUG_OBJECT (src, "create describe...");
6481 gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
6482 src->conninfo.url_str);
6484 goto create_request_failed;
6486 /* we only accept SDP for now */
6487 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
6491 GST_DEBUG_OBJECT (src, "send describe...");
6494 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
6497 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6501 /* we only perform redirect for the describe, currently */
6502 if (src->need_redirect) {
6503 /* close connection, we don't have to send a TEARDOWN yet, ignore the
6505 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6507 gst_rtsp_message_unset (&request);
6508 gst_rtsp_message_unset (&response);
6514 /* it could be that the DESCRIBE method was not implemented */
6515 if (!src->methods & GST_RTSP_DESCRIBE)
6518 /* check if reply is SDP */
6519 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
6521 /* could not be set but since the request returned OK, we assume it
6522 * was SDP, else check it. */
6524 if (!g_ascii_strcasecmp (respcont, "application/sdp") == 0)
6525 goto wrong_content_type;
6528 /* get message body and parse as SDP */
6529 gst_rtsp_message_get_body (&response, &data, &size);
6530 if (data == NULL || size == 0)
6533 GST_DEBUG_OBJECT (src, "parse SDP...");
6534 gst_sdp_message_new (sdp);
6535 gst_sdp_message_parse_buffer (data, size, *sdp);
6537 /* clean up any messages */
6538 gst_rtsp_message_unset (&request);
6539 gst_rtsp_message_unset (&response);
6546 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
6547 ("No valid RTSP URL was provided"));
6552 gchar *str = gst_rtsp_strresult (res);
6554 if (res != GST_RTSP_EINTR) {
6555 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
6556 ("Failed to connect. (%s)", str));
6558 GST_WARNING_OBJECT (src, "connect interrupted");
6563 create_request_failed:
6565 gchar *str = gst_rtsp_strresult (res);
6567 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6568 ("Could not create request. (%s)", str));
6574 /* Don't post a message - the rtsp_send method will have
6575 * taken care of it because we passed NULL for the response code */
6580 /* error was posted */
6581 res = GST_RTSP_ERROR;
6586 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6587 ("Server does not support SDP, got %s.", respcont));
6588 res = GST_RTSP_ERROR;
6593 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6594 ("Server can not provide an SDP."));
6595 res = GST_RTSP_ERROR;
6600 if (src->conninfo.connection) {
6601 GST_DEBUG_OBJECT (src, "free connection");
6602 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6604 gst_rtsp_message_unset (&request);
6605 gst_rtsp_message_unset (&response);
6610 static GstRTSPResult
6611 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
6616 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
6618 if (src->sdp == NULL) {
6619 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
6623 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
6628 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
6635 GST_WARNING_OBJECT (src, "can't get sdp");
6636 src->open_error = TRUE;
6641 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
6642 src->open_error = TRUE;
6647 static GstRTSPResult
6648 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
6650 GstRTSPMessage request = { 0 };
6651 GstRTSPMessage response = { 0 };
6652 GstRTSPResult res = GST_RTSP_OK;
6654 const gchar *control;
6656 GST_DEBUG_OBJECT (src, "TEARDOWN...");
6658 gst_rtspsrc_set_state (src, GST_STATE_READY);
6660 if (src->state < GST_RTSP_STATE_READY) {
6661 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
6668 /* construct a control url */
6669 control = get_aggregate_control (src);
6671 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
6674 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6675 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6676 const gchar *setup_url;
6677 GstRTSPConnInfo *info;
6679 /* try aggregate control first but do non-aggregate control otherwise */
6681 setup_url = control;
6682 else if ((setup_url = stream->conninfo.location) == NULL)
6685 if (src->conninfo.connection) {
6686 info = &src->conninfo;
6687 } else if (stream->conninfo.connection) {
6688 info = &stream->conninfo;
6692 if (!info->connected)
6697 gst_rtsp_message_init_request (&request, GST_RTSP_TEARDOWN, setup_url);
6699 goto create_request_failed;
6702 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
6705 gst_rtspsrc_send (src, info->connection, &request, &response,
6709 /* FIXME, parse result? */
6710 gst_rtsp_message_unset (&request);
6711 gst_rtsp_message_unset (&response);
6714 /* early exit when we did aggregate control */
6720 /* close connections */
6721 GST_DEBUG_OBJECT (src, "closing connection...");
6722 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6723 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6724 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6725 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
6729 gst_rtspsrc_cleanup (src);
6731 src->state = GST_RTSP_STATE_INVALID;
6734 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
6739 create_request_failed:
6741 gchar *str = gst_rtsp_strresult (res);
6743 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6744 ("Could not create request. (%s)", str));
6750 gchar *str = gst_rtsp_strresult (res);
6752 gst_rtsp_message_unset (&request);
6753 if (res != GST_RTSP_EINTR) {
6754 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6755 ("Could not send message. (%s)", str));
6757 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
6764 GST_DEBUG_OBJECT (src,
6765 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
6770 /* RTP-Info is of the format:
6772 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
6774 * rtptime corresponds to the timestamp for the NPT time given in the header
6775 * seqbase corresponds to the next sequence number we received. This number
6776 * indicates the first seqnum after the seek and should be used to discard
6777 * packets that are from before the seek.
6780 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
6785 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
6787 infos = g_strsplit (rtpinfo, ",", 0);
6788 for (i = 0; infos[i]; i++) {
6790 GstRTSPStream *stream;
6794 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
6796 /* init values, types of seqbase and timebase are bigger than needed so we
6797 * can store -1 as uninitialized values */
6802 /* parse url, find stream for url.
6803 * parse seq and rtptime. The seq number should be configured in the rtp
6804 * depayloader or session manager to detect gaps. Same for the rtptime, it
6805 * should be used to create an initial time newsegment. */
6806 fields = g_strsplit (infos[i], ";", 0);
6807 for (j = 0; fields[j]; j++) {
6808 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
6809 /* remove leading whitespace */
6810 fields[j] = g_strchug (fields[j]);
6811 if (g_str_has_prefix (fields[j], "url=")) {
6812 /* get the url and the stream */
6814 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
6815 } else if (g_str_has_prefix (fields[j], "seq=")) {
6816 seqbase = atoi (fields[j] + 4);
6817 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
6818 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
6821 g_strfreev (fields);
6822 /* now we need to store the values for the caps of the stream */
6823 if (stream != NULL) {
6824 GST_DEBUG_OBJECT (src,
6825 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
6826 stream, seqbase, timebase);
6828 /* we have a stream, configure detected params */
6829 stream->seqbase = seqbase;
6830 stream->timebase = timebase;
6839 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
6844 interval = strtoul (rtcp, NULL, 10);
6845 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
6850 interval *= GST_MSECOND;
6852 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6853 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6855 /* already (optionally) retrieved this when configuring manager */
6856 if (stream->session) {
6857 GObject *rtpsession = stream->session;
6859 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
6861 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
6865 /* now it happens that (Xenon) server sending this may also provide bogus
6866 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
6867 * and just use RTP-Info to sync */
6869 GObjectClass *klass;
6871 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
6872 if (g_object_class_find_property (klass, "rtcp-sync")) {
6873 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
6874 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
6880 gst_rtspsrc_get_float (const gchar * dstr)
6882 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
6884 /* canonicalise floating point string so we can handle float strings
6885 * in the form "24.930" or "24,930" irrespective of the current locale */
6886 g_strlcpy (s, dstr, sizeof (s));
6887 g_strdelimit (s, ",", '.');
6888 return g_ascii_strtod (s, NULL);
6892 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
6894 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
6896 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
6897 g_strlcpy (val_str, "now", sizeof (val_str));
6899 if (segment->position == 0) {
6900 g_strlcpy (val_str, "0", sizeof (val_str));
6902 g_ascii_dtostr (val_str, sizeof (val_str),
6903 ((gdouble) segment->position) / GST_SECOND);
6906 return g_strdup_printf ("npt=%s-", val_str);
6910 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
6914 stream->timebase = -1;
6915 stream->seqbase = -1;
6917 len = stream->ptmap->len;
6918 for (i = 0; i < len; i++) {
6919 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
6922 if (item->caps == NULL)
6925 item->caps = gst_caps_make_writable (item->caps);
6926 s = gst_caps_get_structure (item->caps, 0);
6927 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
6931 static GstRTSPResult
6932 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
6934 GstRTSPResult res = GST_RTSP_OK;
6936 if (src->state < GST_RTSP_STATE_READY) {
6937 res = GST_RTSP_ERROR;
6938 if (src->open_error) {
6939 GST_DEBUG_OBJECT (src, "the stream was in error");
6943 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
6945 if ((res = gst_rtspsrc_open (src, async)) < 0) {
6946 GST_DEBUG_OBJECT (src, "failed to open stream");
6955 static GstRTSPResult
6956 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
6958 GstRTSPMessage request = { 0 };
6959 GstRTSPMessage response = { 0 };
6960 GstRTSPResult res = GST_RTSP_OK;
6964 const gchar *control;
6966 GST_DEBUG_OBJECT (src, "PLAY...");
6968 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6971 if (!(src->methods & GST_RTSP_PLAY))
6974 if (src->state == GST_RTSP_STATE_PLAYING)
6977 if (!src->conninfo.connection || !src->conninfo.connected)
6980 /* send some dummy packets before we activate the receive in the
6982 gst_rtspsrc_send_dummy_packets (src);
6984 /* require new SR packets */
6986 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
6988 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
6990 /* construct a control url */
6991 control = get_aggregate_control (src);
6993 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6994 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6995 const gchar *setup_url;
6996 GstRTSPConnection *conn;
6998 /* try aggregate control first but do non-aggregate control otherwise */
7000 setup_url = control;
7001 else if ((setup_url = stream->conninfo.location) == NULL)
7004 if (src->conninfo.connection) {
7005 conn = src->conninfo.connection;
7006 } else if (stream->conninfo.connection) {
7007 conn = stream->conninfo.connection;
7013 res = gst_rtsp_message_init_request (&request, GST_RTSP_PLAY, setup_url);
7015 goto create_request_failed;
7017 if (src->need_range) {
7018 hval = gen_range_header (src, segment);
7020 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
7022 /* store the newsegment event so it can be sent from the streaming thread. */
7023 if (src->start_segment)
7024 gst_event_unref (src->start_segment);
7025 src->start_segment = gst_event_new_segment (&src->segment);
7028 if (segment->rate != 1.0) {
7029 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
7031 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
7033 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
7035 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
7039 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
7041 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7044 /* seek may have silently failed as it is not supported */
7045 if (!(src->methods & GST_RTSP_PLAY)) {
7046 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
7047 /* obviously it is supported as we made it here */
7048 src->methods |= GST_RTSP_PLAY;
7049 src->seekable = FALSE;
7050 /* but there is nothing to parse in the response,
7051 * so convey we have no idea and not to expect anything particular */
7052 clear_rtp_base (src, stream);
7056 /* need to do for all streams */
7057 for (run = src->streams; run; run = g_list_next (run))
7058 clear_rtp_base (src, (GstRTSPStream *) run->data);
7060 /* NOTE the above also disables npt based eos detection */
7061 /* and below forces position to 0,
7062 * which is visible feedback we lost the plot */
7063 segment->start = segment->position = src->last_pos;
7066 gst_rtsp_message_unset (&request);
7068 /* parse RTP npt field. This is the current position in the stream (Normal
7069 * Play Time) and should be put in the NEWSEGMENT position field. */
7070 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
7072 gst_rtspsrc_parse_range (src, hval, segment);
7074 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
7075 segment->rate = 1.0;
7077 /* parse Speed header. This is the intended playback rate of the stream
7078 * and should be put in the NEWSEGMENT rate field. */
7079 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
7080 0) == GST_RTSP_OK) {
7081 segment->rate = gst_rtspsrc_get_float (hval);
7082 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
7083 &hval, 0) == GST_RTSP_OK) {
7084 segment->rate = gst_rtspsrc_get_float (hval);
7087 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
7088 * for the RTP packets. If this is not present, we assume all starts from 0...
7089 * This is info for the RTP session manager that we pass to it in caps. */
7091 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
7092 &hval, hval_idx++) == GST_RTSP_OK)
7093 gst_rtspsrc_parse_rtpinfo (src, hval);
7095 /* some servers indicate RTCP parameters in PLAY response,
7096 * rather than properly in SDP */
7097 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
7098 &hval, 0) == GST_RTSP_OK)
7099 gst_rtspsrc_handle_rtcp_interval (src, hval);
7101 gst_rtsp_message_unset (&response);
7103 /* early exit when we did aggregate control */
7107 /* configure the caps of the streams after we parsed all headers. Only reset
7108 * the manager object when we set a new Range header (we did a seek) */
7109 gst_rtspsrc_configure_caps (src, segment, src->need_range);
7111 /* set again when needed */
7112 src->need_range = FALSE;
7114 src->running = TRUE;
7115 src->base_time = -1;
7116 src->state = GST_RTSP_STATE_PLAYING;
7119 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
7120 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7121 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7122 stream->discont = TRUE;
7127 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
7134 GST_DEBUG_OBJECT (src, "failed to open stream");
7139 GST_DEBUG_OBJECT (src, "PLAY is not supported");
7144 GST_DEBUG_OBJECT (src, "we were already PLAYING");
7147 create_request_failed:
7149 gchar *str = gst_rtsp_strresult (res);
7151 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7152 ("Could not create request. (%s)", str));
7158 gchar *str = gst_rtsp_strresult (res);
7160 gst_rtsp_message_unset (&request);
7161 if (res != GST_RTSP_EINTR) {
7162 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7163 ("Could not send message. (%s)", str));
7165 GST_WARNING_OBJECT (src, "PLAY interrupted");
7172 static GstRTSPResult
7173 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
7175 GstRTSPResult res = GST_RTSP_OK;
7176 GstRTSPMessage request = { 0 };
7177 GstRTSPMessage response = { 0 };
7179 const gchar *control;
7181 GST_DEBUG_OBJECT (src, "PAUSE...");
7183 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7186 if (!(src->methods & GST_RTSP_PAUSE))
7189 if (src->state == GST_RTSP_STATE_READY)
7192 if (!src->conninfo.connection || !src->conninfo.connected)
7195 /* construct a control url */
7196 control = get_aggregate_control (src);
7198 /* loop over the streams. We might exit the loop early when we could do an
7199 * aggregate control */
7200 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7201 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7202 GstRTSPConnection *conn;
7203 const gchar *setup_url;
7205 /* try aggregate control first but do non-aggregate control otherwise */
7207 setup_url = control;
7208 else if ((setup_url = stream->conninfo.location) == NULL)
7211 if (src->conninfo.connection) {
7212 conn = src->conninfo.connection;
7213 } else if (stream->conninfo.connection) {
7214 conn = stream->conninfo.connection;
7220 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
7221 ("Sending PAUSE request"));
7224 gst_rtsp_message_init_request (&request, GST_RTSP_PAUSE,
7226 goto create_request_failed;
7228 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7231 gst_rtsp_message_unset (&request);
7232 gst_rtsp_message_unset (&response);
7234 /* exit early when we did agregate control */
7239 /* change element states now */
7240 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
7243 src->state = GST_RTSP_STATE_READY;
7247 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
7254 GST_DEBUG_OBJECT (src, "failed to open stream");
7259 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
7264 GST_DEBUG_OBJECT (src, "we were already PAUSED");
7267 create_request_failed:
7269 gchar *str = gst_rtsp_strresult (res);
7271 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7272 ("Could not create request. (%s)", str));
7278 gchar *str = gst_rtsp_strresult (res);
7280 gst_rtsp_message_unset (&request);
7281 if (res != GST_RTSP_EINTR) {
7282 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7283 ("Could not send message. (%s)", str));
7285 GST_WARNING_OBJECT (src, "PAUSE interrupted");
7293 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
7295 GstRTSPSrc *rtspsrc;
7297 rtspsrc = GST_RTSPSRC (bin);
7299 switch (GST_MESSAGE_TYPE (message)) {
7300 case GST_MESSAGE_EOS:
7301 gst_message_unref (message);
7303 case GST_MESSAGE_ELEMENT:
7305 const GstStructure *s = gst_message_get_structure (message);
7307 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
7308 gboolean ignore_timeout;
7310 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
7312 GST_OBJECT_LOCK (rtspsrc);
7313 ignore_timeout = rtspsrc->ignore_timeout;
7314 rtspsrc->ignore_timeout = TRUE;
7315 GST_OBJECT_UNLOCK (rtspsrc);
7317 /* we only act on the first udp timeout message, others are irrelevant
7318 * and can be ignored. */
7319 if (!ignore_timeout)
7320 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
7322 gst_message_unref (message);
7325 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7328 case GST_MESSAGE_ERROR:
7331 GstRTSPStream *stream;
7334 udpsrc = GST_MESSAGE_SRC (message);
7336 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
7337 GST_ELEMENT_NAME (udpsrc));
7339 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
7343 /* we ignore the RTCP udpsrc */
7344 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
7347 /* if we get error messages from the udp sources, that's not a problem as
7348 * long as not all of them error out. We also don't really know what the
7349 * problem is, the message does not give enough detail... */
7350 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
7351 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
7352 if (ret != GST_FLOW_OK)
7356 gst_message_unref (message);
7360 /* fatal but not our message, forward */
7361 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7366 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7372 /* the thread where everything happens */
7374 gst_rtspsrc_thread (GstRTSPSrc * src)
7378 GST_OBJECT_LOCK (src);
7379 cmd = src->pending_cmd;
7380 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
7381 || cmd == CMD_LOOP || cmd == CMD_OPEN)
7382 src->pending_cmd = CMD_LOOP;
7384 src->pending_cmd = CMD_WAIT;
7385 GST_DEBUG_OBJECT (src, "got command %d", cmd);
7387 /* we got the message command, so ensure communication is possible again */
7388 gst_rtspsrc_connection_flush (src, FALSE);
7390 src->busy_cmd = cmd;
7391 GST_OBJECT_UNLOCK (src);
7395 gst_rtspsrc_open (src, TRUE);
7398 gst_rtspsrc_play (src, &src->segment, TRUE);
7401 gst_rtspsrc_pause (src, TRUE);
7404 gst_rtspsrc_close (src, TRUE, FALSE);
7407 gst_rtspsrc_loop (src);
7410 gst_rtspsrc_reconnect (src, FALSE);
7416 GST_OBJECT_LOCK (src);
7417 /* and go back to sleep */
7418 if (src->pending_cmd == CMD_WAIT) {
7420 gst_task_pause (src->task);
7423 src->busy_cmd = CMD_WAIT;
7424 GST_OBJECT_UNLOCK (src);
7428 gst_rtspsrc_start (GstRTSPSrc * src)
7430 GST_DEBUG_OBJECT (src, "starting");
7432 GST_OBJECT_LOCK (src);
7434 src->pending_cmd = CMD_WAIT;
7436 if (src->task == NULL) {
7437 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
7438 if (src->task == NULL)
7441 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
7443 GST_OBJECT_UNLOCK (src);
7450 GST_OBJECT_UNLOCK (src);
7451 GST_ERROR_OBJECT (src, "failed to create task");
7457 gst_rtspsrc_stop (GstRTSPSrc * src)
7461 GST_DEBUG_OBJECT (src, "stopping");
7463 /* also cancels pending task */
7464 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
7466 GST_OBJECT_LOCK (src);
7467 if ((task = src->task)) {
7469 GST_OBJECT_UNLOCK (src);
7471 gst_task_stop (task);
7473 /* make sure it is not running */
7474 GST_RTSP_STREAM_LOCK (src);
7475 GST_RTSP_STREAM_UNLOCK (src);
7477 /* now wait for the task to finish */
7478 gst_task_join (task);
7480 /* and free the task */
7481 gst_object_unref (GST_OBJECT (task));
7483 GST_OBJECT_LOCK (src);
7485 GST_OBJECT_UNLOCK (src);
7487 /* ensure synchronously all is closed and clean */
7488 gst_rtspsrc_close (src, FALSE, TRUE);
7493 static GstStateChangeReturn
7494 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
7496 GstRTSPSrc *rtspsrc;
7497 GstStateChangeReturn ret;
7499 rtspsrc = GST_RTSPSRC (element);
7501 switch (transition) {
7502 case GST_STATE_CHANGE_NULL_TO_READY:
7503 if (!gst_rtspsrc_start (rtspsrc))
7506 case GST_STATE_CHANGE_READY_TO_PAUSED:
7507 /* init some state */
7508 rtspsrc->cur_protocols = rtspsrc->protocols;
7509 /* first attempt, don't ignore timeouts */
7510 rtspsrc->ignore_timeout = FALSE;
7511 rtspsrc->open_error = FALSE;
7512 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
7514 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7515 set_manager_buffer_mode (rtspsrc);
7517 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7518 /* unblock the tcp tasks and make the loop waiting */
7519 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
7520 /* make sure it is waiting before we send PAUSE or PLAY below */
7521 GST_RTSP_STREAM_LOCK (rtspsrc);
7522 GST_RTSP_STREAM_UNLOCK (rtspsrc);
7525 case GST_STATE_CHANGE_PAUSED_TO_READY:
7531 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
7532 if (ret == GST_STATE_CHANGE_FAILURE)
7535 switch (transition) {
7536 case GST_STATE_CHANGE_NULL_TO_READY:
7537 ret = GST_STATE_CHANGE_SUCCESS;
7539 case GST_STATE_CHANGE_READY_TO_PAUSED:
7540 ret = GST_STATE_CHANGE_NO_PREROLL;
7542 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7543 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
7544 ret = GST_STATE_CHANGE_SUCCESS;
7546 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7547 /* send pause request and keep the idle task around */
7548 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
7549 ret = GST_STATE_CHANGE_NO_PREROLL;
7551 case GST_STATE_CHANGE_PAUSED_TO_READY:
7552 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_PAUSE);
7553 ret = GST_STATE_CHANGE_SUCCESS;
7555 case GST_STATE_CHANGE_READY_TO_NULL:
7556 gst_rtspsrc_stop (rtspsrc);
7557 ret = GST_STATE_CHANGE_SUCCESS;
7568 GST_DEBUG_OBJECT (rtspsrc, "start failed");
7569 return GST_STATE_CHANGE_FAILURE;
7574 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
7577 GstRTSPSrc *rtspsrc;
7579 rtspsrc = GST_RTSPSRC (element);
7581 if (GST_EVENT_IS_DOWNSTREAM (event)) {
7582 res = gst_rtspsrc_push_event (rtspsrc, event);
7584 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
7591 /*** GSTURIHANDLER INTERFACE *************************************************/
7594 gst_rtspsrc_uri_get_type (GType type)
7599 static const gchar *const *
7600 gst_rtspsrc_uri_get_protocols (GType type)
7602 static const gchar *protocols[] =
7603 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
7604 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
7611 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
7613 GstRTSPSrc *src = GST_RTSPSRC (handler);
7615 /* FIXME: make thread-safe */
7616 return g_strdup (src->conninfo.location);
7620 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
7625 GstRTSPUrl *newurl = NULL;
7626 GstSDPMessage *sdp = NULL;
7628 src = GST_RTSPSRC (handler);
7630 /* same URI, we're fine */
7631 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
7634 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
7635 if ((res = gst_sdp_message_new (&sdp) < 0))
7638 GST_DEBUG_OBJECT (src, "parsing SDP message");
7639 if ((res = gst_sdp_message_parse_uri (uri, sdp) < 0))
7643 GST_DEBUG_OBJECT (src, "parsing URI");
7644 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
7648 /* if worked, free previous and store new url object along with the original
7650 GST_DEBUG_OBJECT (src, "configuring URI");
7651 g_free (src->conninfo.location);
7652 src->conninfo.location = g_strdup (uri);
7653 gst_rtsp_url_free (src->conninfo.url);
7654 src->conninfo.url = newurl;
7655 g_free (src->conninfo.url_str);
7657 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
7659 src->conninfo.url_str = NULL;
7662 gst_sdp_message_free (src->sdp);
7664 src->from_sdp = sdp != NULL;
7666 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
7667 GST_DEBUG_OBJECT (src, "request uri is: %s",
7668 GST_STR_NULL (src->conninfo.url_str));
7675 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
7680 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", res);
7681 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7682 "Could not create SDP");
7687 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", res,
7688 GST_STR_NULL (uri));
7689 gst_sdp_message_free (sdp);
7690 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7696 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
7697 GST_STR_NULL (uri), res);
7698 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7699 "Invalid RTSP URI");
7705 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
7707 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
7709 iface->get_type = gst_rtspsrc_uri_get_type;
7710 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
7711 iface->get_uri = gst_rtspsrc_uri_get_uri;
7712 iface->set_uri = gst_rtspsrc_uri_set_uri;