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]);
1518 if (stream->channelpad[i])
1519 gst_object_unref (stream->channelpad[i]);
1521 if (stream->udpsink[i]) {
1522 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1523 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1524 gst_object_unref (stream->udpsink[i]);
1527 if (stream->fakesrc) {
1528 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1529 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1530 gst_object_unref (stream->fakesrc);
1532 if (stream->srcpad) {
1533 gst_pad_set_active (stream->srcpad, FALSE);
1535 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1537 if (stream->srtpenc)
1538 gst_object_unref (stream->srtpenc);
1539 if (stream->srtpdec)
1540 gst_object_unref (stream->srtpdec);
1542 gst_buffer_unref (stream->key);
1543 if (stream->rtcppad)
1544 gst_object_unref (stream->rtcppad);
1545 if (stream->session)
1546 g_object_unref (stream->session);
1551 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1555 GST_DEBUG_OBJECT (src, "cleanup");
1557 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1558 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1560 gst_rtspsrc_stream_free (src, stream);
1562 g_list_free (src->streams);
1563 src->streams = NULL;
1565 if (src->manager_sig_id) {
1566 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1567 src->manager_sig_id = 0;
1569 gst_element_set_state (src->manager, GST_STATE_NULL);
1570 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1571 src->manager = NULL;
1574 gst_structure_free (src->props);
1577 g_free (src->content_base);
1578 src->content_base = NULL;
1580 g_free (src->control);
1581 src->control = NULL;
1584 gst_rtsp_range_free (src->range);
1587 /* don't clear the SDP when it was used in the url */
1588 if (src->sdp && !src->from_sdp) {
1589 gst_sdp_message_free (src->sdp);
1592 if (src->start_segment) {
1593 gst_event_unref (src->start_segment);
1594 src->start_segment = NULL;
1596 if (src->provided_clock) {
1597 gst_object_unref (src->provided_clock);
1598 src->provided_clock = NULL;
1602 #define PARSE_INT(p, del, res) \
1605 p = strstr (p, del); \
1615 #define PARSE_STRING(p, del, res) \
1618 p = strstr (p, del); \
1630 #define SKIP_SPACES(p) \
1631 while (*p && g_ascii_isspace (*p)) \
1636 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1639 gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
1640 gint * rate, gchar ** params)
1644 p = (gchar *) rtpmap;
1646 PARSE_INT (p, " ", *payload);
1654 PARSE_STRING (p, "/", *name);
1655 if (*name == NULL) {
1656 GST_DEBUG ("no rate, name %s", p);
1657 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
1658 * streams seem to omit the rate. */
1665 p = strstr (p, "/");
1683 parse_keymgmt (const gchar * keymgmt, GstCaps * caps)
1685 gboolean res = FALSE;
1689 GstMIKEYMessage *msg;
1690 const GstMIKEYPayload *payload;
1691 const gchar *srtp_cipher;
1692 const gchar *srtp_auth;
1694 p = (gchar *) keymgmt;
1700 PARSE_STRING (p, " ", kmpid);
1701 if (!g_str_equal (kmpid, "mikey"))
1704 data = g_base64_decode (p, &size);
1708 msg = gst_mikey_message_new_from_data (data, size, NULL, NULL);
1712 srtp_cipher = "aes-128-icm";
1713 srtp_auth = "hmac-sha1-80";
1715 /* check the Security policy if any */
1716 if ((payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, 0))) {
1717 GstMIKEYPayloadSP *p = (GstMIKEYPayloadSP *) payload;
1720 if (p->proto != GST_MIKEY_SEC_PROTO_SRTP)
1723 len = gst_mikey_payload_sp_get_n_params (payload);
1724 for (i = 0; i < len; i++) {
1725 const GstMIKEYPayloadSPParam *param =
1726 gst_mikey_payload_sp_get_param (payload, i);
1728 switch (param->type) {
1729 case GST_MIKEY_SP_SRTP_ENC_ALG:
1730 switch (param->val[0]) {
1732 srtp_cipher = "null";
1736 srtp_cipher = "aes-128-icm";
1742 case GST_MIKEY_SP_SRTP_AUTH_ALG:
1743 switch (param->val[0]) {
1749 srtp_auth = "hmac-sha1-80";
1755 case GST_MIKEY_SP_SRTP_SRTP_ENC:
1757 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
1765 if (!(payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_KEMAC, 0)))
1768 GstMIKEYPayloadKEMAC *p = (GstMIKEYPayloadKEMAC *) payload;
1769 const GstMIKEYPayload *sub;
1770 GstMIKEYPayloadKeyData *pkd;
1773 if (p->enc_alg != GST_MIKEY_ENC_NULL || p->mac_alg != GST_MIKEY_MAC_NULL)
1776 if (!(sub = gst_mikey_payload_kemac_get_sub (payload, 0)))
1779 if (sub->type != GST_MIKEY_PT_KEY_DATA)
1782 pkd = (GstMIKEYPayloadKeyData *) sub;
1784 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
1786 gst_caps_set_simple (caps, "srtp-key", GST_TYPE_BUFFER, buf, NULL);
1789 gst_caps_set_simple (caps,
1790 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
1791 "srtp-auth", G_TYPE_STRING, srtp_auth,
1792 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
1793 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
1797 gst_mikey_message_free (msg);
1803 * Mapping SDP attributes to caps
1805 * prepend 'a-' to IANA registered sdp attributes names
1806 * (ie: not prefixed with 'x-') in order to avoid
1807 * collision with gstreamer standard caps properties names
1810 gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
1812 if (attributes->len > 0) {
1816 s = gst_caps_get_structure (caps, 0);
1818 for (i = 0; i < attributes->len; i++) {
1819 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
1820 gchar *tofree, *key;
1824 /* skip some of the attribute we already handle */
1825 if (!strcmp (key, "fmtp"))
1827 if (!strcmp (key, "rtpmap"))
1829 if (!strcmp (key, "control"))
1831 if (!strcmp (key, "range"))
1833 if (g_str_equal (key, "key-mgmt")) {
1834 parse_keymgmt (attr->value, caps);
1838 /* string must be valid UTF8 */
1839 if (!g_utf8_validate (attr->value, -1, NULL))
1842 if (!g_str_has_prefix (key, "x-"))
1843 tofree = key = g_strdup_printf ("a-%s", key);
1847 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
1848 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
1854 static const gchar *
1855 rtsp_get_attribute_for_pt (const GstSDPMedia * media, const gchar * name,
1864 if ((attr = gst_sdp_media_get_attribute_val_n (media, name, i)) == NULL)
1867 if (sscanf (attr, "%d ", &val) != 1)
1877 * Mapping of caps to and from SDP fields:
1879 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1880 * a=fmtp:<payload> <param>[=<value>];...
1883 gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
1886 const gchar *rtpmap;
1890 gchar *params = NULL;
1896 /* get and parse rtpmap */
1897 rtpmap = rtsp_get_attribute_for_pt (media, "rtpmap", pt);
1900 ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
1902 g_warning ("error parsing rtpmap, ignoring");
1906 /* dynamic payloads need rtpmap or we fail */
1907 if (rtpmap == NULL && pt >= 96)
1910 /* check if we have a rate, if not, we need to look up the rate from the
1911 * default rates based on the payload types. */
1913 const GstRTPPayloadInfo *info;
1915 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
1916 /* dynamic types, use media and encoding_name */
1917 tmp = g_ascii_strdown (media->media, -1);
1918 info = gst_rtp_payload_info_for_name (tmp, name);
1921 /* static types, use payload type */
1922 info = gst_rtp_payload_info_for_pt (pt);
1926 if ((rate = info->clock_rate) == 0)
1929 /* we fail if we cannot find one */
1934 tmp = g_ascii_strdown (media->media, -1);
1935 caps = gst_caps_new_simple ("application/x-unknown",
1936 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
1938 s = gst_caps_get_structure (caps, 0);
1940 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
1942 /* encoding name must be upper case */
1944 tmp = g_ascii_strup (name, -1);
1945 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
1949 /* params must be lower case */
1950 if (params != NULL) {
1951 tmp = g_ascii_strdown (params, -1);
1952 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
1956 /* parse optional fmtp: field */
1957 if ((fmtp = rtsp_get_attribute_for_pt (media, "fmtp", pt))) {
1963 /* p is now of the format <payload> <param>[=<value>];... */
1964 PARSE_INT (p, " ", payload);
1965 if (payload != -1 && payload == pt) {
1969 /* <param>[=<value>] are separated with ';' */
1970 pairs = g_strsplit (p, ";", 0);
1971 for (i = 0; pairs[i]; i++) {
1973 const gchar *val, *key;
1975 /* the key may not have a '=', the value can have other '='s */
1976 valpos = strstr (pairs[i], "=");
1978 /* we have a '=' and thus a value, remove the '=' with \0 */
1980 /* value is everything between '=' and ';'. We split the pairs at ;
1981 * boundaries so we can take the remainder of the value. Some servers
1982 * put spaces around the value which we strip off here. Alternatively
1983 * we could strip those spaces in the depayloaders should these spaces
1984 * actually carry any meaning in the future. */
1985 val = g_strstrip (valpos + 1);
1987 /* simple <param>;.. is translated into <param>=1;... */
1990 /* strip the key of spaces, convert key to lowercase but not the value. */
1991 key = g_strstrip (pairs[i]);
1992 if (strlen (key) > 1) {
1993 tmp = g_ascii_strdown (key, -1);
1994 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
2006 g_warning ("rtpmap type not given for dynamic payload %d", pt);
2011 g_warning ("rate unknown for payload type %d", pt);
2017 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
2018 gint * rtpport, gint * rtcpport)
2021 GstStateChangeReturn ret;
2022 GstElement *udpsrc0, *udpsrc1;
2023 gint tmp_rtp, tmp_rtcp;
2027 src = stream->parent;
2033 /* Start at next port */
2034 tmp_rtp = src->next_port_num;
2036 if (stream->is_ipv6)
2037 host = "udp://[::0]";
2039 host = "udp://0.0.0.0";
2041 /* try to allocate 2 UDP ports, the RTP port should be an even
2042 * number and the RTCP port should be the next (uneven) port */
2045 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
2046 tmp_rtp >= src->client_port_range.max)
2049 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2050 if (udpsrc0 == NULL)
2051 goto no_udp_protocol;
2052 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
2054 if (src->udp_buffer_size != 0)
2055 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
2058 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
2059 if (ret == GST_STATE_CHANGE_FAILURE) {
2061 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
2064 if (++count > src->retry)
2067 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2068 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2069 gst_object_unref (udpsrc0);
2072 GST_DEBUG_OBJECT (src, "retry %d", count);
2075 goto no_udp_protocol;
2078 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
2079 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
2081 /* check if port is even */
2082 if ((tmp_rtp & 0x01) != 0) {
2083 /* port not even, close and allocate another */
2084 if (++count > src->retry)
2087 GST_DEBUG_OBJECT (src, "RTP port not even");
2089 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2090 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2091 gst_object_unref (udpsrc0);
2094 GST_DEBUG_OBJECT (src, "retry %d", count);
2099 /* allocate port+1 for RTCP now */
2100 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2101 if (udpsrc1 == NULL)
2102 goto no_udp_rtcp_protocol;
2105 tmp_rtcp = tmp_rtp + 1;
2106 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
2109 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
2111 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
2112 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
2113 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
2114 if (ret == GST_STATE_CHANGE_FAILURE) {
2115 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
2117 if (++count > src->retry)
2120 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2121 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2122 gst_object_unref (udpsrc0);
2125 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
2126 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2127 gst_object_unref (udpsrc1);
2131 GST_DEBUG_OBJECT (src, "retry %d", count);
2135 /* all fine, do port check */
2136 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
2137 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
2139 /* this should not happen... */
2140 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
2143 /* we keep these elements, we configure all in configure_transport when the
2144 * server told us to really use the UDP ports. */
2145 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
2146 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
2147 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
2148 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
2150 /* keep track of next available port number when we have a range
2152 if (src->next_port_num != 0)
2153 src->next_port_num = tmp_rtcp + 1;
2160 GST_DEBUG_OBJECT (src, "could not get UDP source");
2165 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
2169 no_udp_rtcp_protocol:
2171 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
2176 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
2177 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
2183 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2184 gst_object_unref (udpsrc0);
2187 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2188 gst_object_unref (udpsrc1);
2195 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
2200 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
2202 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2203 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2206 for (i = 0; i < 2; i++) {
2207 if (stream->udpsrc[i])
2208 gst_element_set_state (stream->udpsrc[i], state);
2214 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
2221 event = gst_event_new_flush_start ();
2222 GST_DEBUG_OBJECT (src, "start flush");
2224 state = GST_STATE_PAUSED;
2226 event = gst_event_new_flush_stop (FALSE);
2227 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2230 state = GST_STATE_PLAYING;
2232 state = GST_STATE_PAUSED;
2234 gst_rtspsrc_push_event (src, event);
2235 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2236 gst_rtspsrc_set_state (src, state);
2239 static GstRTSPResult
2240 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
2241 GstRTSPMessage * message, GTimeVal * timeout)
2246 ret = gst_rtsp_connection_send (conn, message, timeout);
2248 ret = GST_RTSP_ERROR;
2253 static GstRTSPResult
2254 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
2255 GstRTSPMessage * message, GTimeVal * timeout)
2260 ret = gst_rtsp_connection_receive (conn, message, timeout);
2262 ret = GST_RTSP_ERROR;
2268 gst_rtspsrc_get_position (GstRTSPSrc * src)
2273 query = gst_query_new_position (GST_FORMAT_TIME);
2274 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2275 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2276 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2280 if (stream->srcpad) {
2281 if (gst_pad_query (stream->srcpad, query)) {
2282 gst_query_parse_position (query, &fmt, &pos);
2283 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2284 GST_TIME_ARGS (pos));
2285 src->last_pos = pos;
2295 gst_rtspsrc_do_seek (GstRTSPSrc * src, GstSegment * segment)
2297 src->state = GST_RTSP_STATE_SEEKING;
2298 /* PLAY will add the range header now. */
2299 src->need_range = TRUE;
2305 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2310 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2312 gboolean flush, skip;
2315 GstSegment seeksegment = { 0, };
2319 GST_DEBUG_OBJECT (src, "doing seek with event");
2321 gst_event_parse_seek (event, &rate, &format, &flags,
2322 &cur_type, &cur, &stop_type, &stop);
2324 /* no negative rates yet */
2328 /* we need TIME format */
2329 if (format != src->segment.format)
2332 GST_DEBUG_OBJECT (src, "doing seek without event");
2334 cur_type = GST_SEEK_TYPE_SET;
2335 stop_type = GST_SEEK_TYPE_SET;
2338 /* get flush flag */
2339 flush = flags & GST_SEEK_FLAG_FLUSH;
2340 skip = flags & GST_SEEK_FLAG_SKIP;
2342 /* now we need to make sure the streaming thread is stopped. We do this by
2343 * either sending a FLUSH_START event downstream which will cause the
2344 * streaming thread to stop with a WRONG_STATE.
2345 * For a non-flushing seek we simply pause the task, which will happen as soon
2346 * as it completes one iteration (and thus might block when the sink is
2347 * blocking in preroll). */
2349 GST_DEBUG_OBJECT (src, "starting flush");
2350 gst_rtspsrc_flush (src, TRUE, FALSE);
2353 gst_task_pause (src->task);
2357 /* we should now be able to grab the streaming thread because we stopped it
2358 * with the above flush/pause code */
2359 GST_RTSP_STREAM_LOCK (src);
2361 GST_DEBUG_OBJECT (src, "stopped streaming");
2363 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2364 gst_rtspsrc_connection_flush (src, FALSE);
2366 /* copy segment, we need this because we still need the old
2367 * segment when we close the current segment. */
2368 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2370 /* configure the seek parameters in the seeksegment. We will then have the
2371 * right values in the segment to perform the seek */
2373 GST_DEBUG_OBJECT (src, "configuring seek");
2374 gst_segment_do_seek (&seeksegment, rate, format, flags,
2375 cur_type, cur, stop_type, stop, &update);
2378 /* figure out the last position we need to play. If it's configured (stop !=
2379 * -1), use that, else we play until the total duration of the file */
2380 if ((stop = seeksegment.stop) == -1)
2381 stop = seeksegment.duration;
2383 playing = (src->state == GST_RTSP_STATE_PLAYING);
2385 /* if we were playing, pause first */
2387 /* obtain current position in case seek fails */
2388 gst_rtspsrc_get_position (src);
2389 gst_rtspsrc_pause (src, FALSE);
2393 gst_rtspsrc_do_seek (src, &seeksegment);
2395 /* and continue playing */
2397 gst_rtspsrc_play (src, &seeksegment, FALSE);
2399 /* prepare for streaming again */
2401 /* if we started flush, we stop now */
2402 GST_DEBUG_OBJECT (src, "stopping flush");
2403 gst_rtspsrc_flush (src, FALSE, playing);
2406 /* now we did the seek and can activate the new segment values */
2407 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2409 /* if we're doing a segment seek, post a SEGMENT_START message */
2410 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2411 gst_element_post_message (GST_ELEMENT_CAST (src),
2412 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2413 src->segment.format, src->segment.position));
2416 /* now create the newsegment */
2417 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2418 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2421 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2422 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2423 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2424 stream->discont = TRUE;
2427 GST_RTSP_STREAM_UNLOCK (src);
2434 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2439 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2445 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2449 gboolean res = TRUE;
2452 src = GST_RTSPSRC_CAST (parent);
2454 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2455 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2457 switch (GST_EVENT_TYPE (event)) {
2458 case GST_EVENT_SEEK:
2459 res = gst_rtspsrc_perform_seek (src, event);
2463 case GST_EVENT_NAVIGATION:
2464 case GST_EVENT_LATENCY:
2472 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2473 res = gst_pad_send_event (target, event);
2474 gst_object_unref (target);
2476 gst_event_unref (event);
2479 gst_event_unref (event);
2485 /* this is the final event function we receive on the internal source pad when
2486 * we deal with TCP connections */
2488 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2493 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2495 switch (GST_EVENT_TYPE (event)) {
2496 case GST_EVENT_SEEK:
2498 case GST_EVENT_NAVIGATION:
2499 case GST_EVENT_LATENCY:
2501 gst_event_unref (event);
2508 /* this is the final query function we receive on the internal source pad when
2509 * we deal with TCP connections */
2511 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2515 gboolean res = TRUE;
2517 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2519 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2520 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2522 switch (GST_QUERY_TYPE (query)) {
2523 case GST_QUERY_POSITION:
2528 case GST_QUERY_DURATION:
2532 gst_query_parse_duration (query, &format, NULL);
2535 case GST_FORMAT_TIME:
2536 gst_query_set_duration (query, format, src->segment.duration);
2544 case GST_QUERY_LATENCY:
2546 /* we are live with a min latency of 0 and unlimited max latency, this
2547 * result will be updated by the session manager if there is any. */
2548 gst_query_set_latency (query, TRUE, 0, -1);
2558 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2560 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2564 gboolean res = FALSE;
2566 src = GST_RTSPSRC_CAST (parent);
2568 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2569 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2571 switch (GST_QUERY_TYPE (query)) {
2572 case GST_QUERY_DURATION:
2576 gst_query_parse_duration (query, &format, NULL);
2579 case GST_FORMAT_TIME:
2580 gst_query_set_duration (query, format, src->segment.duration);
2588 case GST_QUERY_SEEKING:
2592 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2593 if (format == GST_FORMAT_TIME) {
2595 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2597 /* seeking without duration is unlikely */
2598 seekable = seekable && src->seekable && src->segment.duration &&
2599 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2601 /* FIXME ?? should we have 0 and segment.duration here; see demuxers */
2602 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
2603 src->segment.start, src->segment.stop);
2612 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2614 gst_query_set_uri (query, uri);
2622 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2624 /* forward the query to the proxy target pad */
2626 res = gst_pad_query (target, query);
2627 gst_object_unref (target);
2636 /* callback for RTCP messages to be sent to the server when operating in TCP
2638 static GstFlowReturn
2639 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2642 GstRTSPStream *stream;
2643 GstFlowReturn res = GST_FLOW_OK;
2648 GstRTSPMessage message = { 0 };
2649 GstRTSPConnection *conn;
2651 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2652 src = stream->parent;
2654 gst_buffer_map (buffer, &map, GST_MAP_READ);
2658 gst_rtsp_message_init_data (&message, stream->channel[1]);
2660 /* lend the body data to the message */
2661 gst_rtsp_message_take_body (&message, data, size);
2663 if (stream->conninfo.connection)
2664 conn = stream->conninfo.connection;
2666 conn = src->conninfo.connection;
2668 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2669 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2670 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2672 /* and steal it away again because we will free it when unreffing the
2674 gst_rtsp_message_steal_body (&message, &data, &size);
2675 gst_rtsp_message_unset (&message);
2677 gst_buffer_unmap (buffer, &map);
2678 gst_buffer_unref (buffer);
2683 static GstPadProbeReturn
2684 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2686 GstRTSPSrc *src = user_data;
2688 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2689 GST_DEBUG_PAD_NAME (pad));
2691 /* activate the streams */
2692 GST_OBJECT_LOCK (src);
2693 if (!src->need_activate)
2696 src->need_activate = FALSE;
2697 GST_OBJECT_UNLOCK (src);
2699 gst_rtspsrc_activate_streams (src);
2701 return GST_PAD_PROBE_OK;
2705 GST_OBJECT_UNLOCK (src);
2706 return GST_PAD_PROBE_OK;
2711 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
2713 GstPad *gpad = GST_PAD_CAST (user_data);
2715 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
2716 gst_pad_store_sticky_event (gpad, *event);
2721 /* this callback is called when the session manager generated a new src pad with
2722 * payloaded RTP packets. We simply ghost the pad here. */
2724 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2727 GstPadTemplate *template;
2730 GstRTSPStream *stream;
2733 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2735 GST_RTSP_STATE_LOCK (src);
2737 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2738 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2739 goto unknown_stream;
2741 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2743 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2745 goto unknown_stream;
2748 stream->ssrc = ssrc;
2750 /* we'll add it later see below */
2751 stream->added = TRUE;
2753 /* check if we added all streams */
2755 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
2756 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
2758 GST_DEBUG_OBJECT (src, "stream %p, container %d, added %d, setup %d",
2759 ostream, ostream->container, ostream->added, ostream->setup);
2761 /* if we find a stream for which we did a setup that is not added, we
2762 * need to wait some more */
2763 if (ostream->setup && !ostream->added) {
2768 GST_RTSP_STATE_UNLOCK (src);
2770 /* create a new pad we will use to stream to */
2771 template = gst_static_pad_template_get (&rtptemplate);
2772 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2773 gst_object_unref (template);
2776 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2777 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2778 gst_pad_set_active (stream->srcpad, TRUE);
2779 gst_pad_sticky_events_foreach (pad, copy_sticky_events, stream->srcpad);
2780 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2783 GST_DEBUG_OBJECT (src, "We added all streams");
2784 /* when we get here, all stream are added and we can fire the no-more-pads
2786 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2794 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2795 GST_RTSP_STATE_UNLOCK (src);
2802 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
2806 len = stream->ptmap->len;
2807 for (i = 0; i < len; i++) {
2808 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
2816 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2818 GstRTSPStream *stream;
2821 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2823 GST_RTSP_STATE_LOCK (src);
2824 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2826 goto unknown_stream;
2828 if ((caps = stream_get_caps_for_pt (stream, pt)))
2829 gst_caps_ref (caps);
2830 GST_RTSP_STATE_UNLOCK (src);
2836 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2837 GST_RTSP_STATE_UNLOCK (src);
2843 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2845 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2851 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2857 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2863 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2865 GstRTSPSrc *src = stream->parent;
2868 g_object_get (source, "ssrc", &ssrc, NULL);
2870 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
2871 ssrc, stream->ssrc, stream->id);
2873 if (ssrc == stream->ssrc)
2874 gst_rtspsrc_do_stream_eos (src, stream);
2878 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2880 GstRTSPSrc *src = stream->parent;
2883 g_object_get (source, "ssrc", &ssrc, NULL);
2885 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
2886 ssrc, stream->ssrc, stream->id);
2888 if (ssrc == stream->ssrc)
2889 gst_rtspsrc_do_stream_eos (src, stream);
2893 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2895 GstRTSPStream *stream;
2897 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2899 /* get stream for session */
2900 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2902 gst_rtspsrc_do_stream_eos (src, stream);
2907 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2909 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2914 set_manager_buffer_mode (GstRTSPSrc * src)
2916 GObjectClass *klass;
2918 if (src->manager == NULL)
2921 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2923 if (!g_object_class_find_property (klass, "buffer-mode"))
2926 if (src->buffer_mode != BUFFER_MODE_AUTO) {
2927 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
2932 GST_DEBUG_OBJECT (src,
2933 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
2935 if (src->provided_clock) {
2936 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
2938 if (clock == src->provided_clock) {
2939 GST_DEBUG_OBJECT (src, "selected synced");
2940 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
2943 gst_object_unref (clock);
2948 /* Otherwise fall-through and use another buffer mode */
2950 gst_object_unref (clock);
2953 GST_DEBUG_OBJECT (src, "auto buffering mode");
2954 if (src->use_buffering) {
2955 GST_DEBUG_OBJECT (src, "selected buffer");
2956 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
2958 GST_DEBUG_OBJECT (src, "selected slave");
2959 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
2964 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
2966 GST_DEBUG ("request key %u", ssrc);
2967 return gst_caps_ref (stream_get_caps_for_pt (stream, stream->default_pt));
2971 request_rtp_decoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
2973 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
2974 if (stream->id != session)
2977 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
2978 stream->profile != GST_RTSP_PROFILE_SAVPF)
2981 if (stream->srtpdec == NULL) {
2984 name = g_strdup_printf ("srtpdec_%u", session);
2985 stream->srtpdec = gst_element_factory_make ("srtpdec", name);
2988 g_signal_connect (stream->srtpdec, "request-key",
2989 (GCallback) request_key, stream);
2991 return gst_object_ref (stream->srtpdec);
2995 request_rtcp_encoder (GstElement * rtpbin, guint session,
2996 GstRTSPStream * stream)
3001 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3002 if (stream->id != session)
3005 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3006 stream->profile != GST_RTSP_PROFILE_SAVPF)
3009 if (stream->srtpenc == NULL) {
3010 name = g_strdup_printf ("srtpenc_%u", session);
3011 stream->srtpenc = gst_element_factory_make ("srtpenc", name);
3014 /* key has been made before */
3015 g_object_set (stream->srtpenc, "key", stream->key, NULL);
3017 name = g_strdup_printf ("rtcp_sink_%d", session);
3018 pad = gst_element_get_request_pad (stream->srtpenc, name);
3020 gst_object_unref (pad);
3022 return gst_object_ref (stream->srtpenc);
3026 /* try to get and configure a manager */
3028 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
3029 GstRTSPTransport * transport)
3031 const gchar *manager;
3033 GstStateChangeReturn ret;
3035 /* find a manager */
3036 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
3040 GST_DEBUG_OBJECT (src, "using manager %s", manager);
3042 /* configure the manager */
3043 if (src->manager == NULL) {
3044 GObjectClass *klass;
3046 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
3048 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
3052 goto use_no_manager;
3054 if (!(src->manager = gst_element_factory_make (manager, "manager")))
3055 goto manager_failed;
3058 /* we manage this element */
3059 gst_element_set_locked_state (src->manager, TRUE);
3060 gst_bin_add (GST_BIN_CAST (src), src->manager);
3062 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
3063 if (ret == GST_STATE_CHANGE_FAILURE)
3064 goto start_manager_failure;
3066 g_object_set (src->manager, "latency", src->latency, NULL);
3068 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3070 if (g_object_class_find_property (klass, "ntp-sync")) {
3071 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
3074 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
3075 g_object_set (src->manager, "use-pipeline-clock",
3076 src->use_pipeline_clock, NULL);
3079 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
3080 g_object_set (src->manager, "sdes", src->sdes, NULL);
3083 if (g_object_class_find_property (klass, "drop-on-latency")) {
3084 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
3088 /* buffer mode pauses are handled by adding offsets to buffer times,
3089 * but some depayloaders may have a hard time syncing output times
3090 * with such input times, e.g. container ones, most notably ASF */
3091 /* TODO alternatives are having an event that indicates these shifts,
3092 * or having rtsp extensions provide suggestion on buffer mode */
3093 /* valid duration implies not likely live pipeline,
3094 * so slaving in jitterbuffer does not make much sense
3095 * (and might mess things up due to bursts) */
3096 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
3097 src->segment.duration && !stream->container) {
3098 src->use_buffering = TRUE;
3100 src->use_buffering = FALSE;
3103 set_manager_buffer_mode (src);
3105 /* connect to signals */
3106 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
3108 src->manager_sig_id =
3109 g_signal_connect (src->manager, "pad-added",
3110 (GCallback) new_manager_pad, src);
3111 src->manager_ptmap_id =
3112 g_signal_connect (src->manager, "request-pt-map",
3113 (GCallback) request_pt_map, src);
3115 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
3118 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
3121 g_signal_connect (src->manager, "request-rtp-decoder",
3122 (GCallback) request_rtp_decoder, stream);
3123 g_signal_connect (src->manager, "request-rtcp-decoder",
3124 (GCallback) request_rtp_decoder, stream);
3125 g_signal_connect (src->manager, "request-rtcp-encoder",
3126 (GCallback) request_rtcp_encoder, stream);
3128 /* we stream directly to the manager, get some pads. Each RTSP stream goes
3129 * into a separate RTP session. */
3130 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
3131 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
3133 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
3134 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
3137 /* now configure the bandwidth in the manager */
3138 if (g_signal_lookup ("get-internal-session",
3139 G_OBJECT_TYPE (src->manager)) != 0) {
3140 GObject *rtpsession;
3142 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
3145 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
3147 stream->session = rtpsession;
3149 if (stream->as_bandwidth != -1) {
3150 GST_INFO_OBJECT (src, "setting AS: %f",
3151 (gdouble) (stream->as_bandwidth * 1000));
3152 g_object_set (rtpsession, "bandwidth",
3153 (gdouble) (stream->as_bandwidth * 1000), NULL);
3155 if (stream->rr_bandwidth != -1) {
3156 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
3157 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
3160 if (stream->rs_bandwidth != -1) {
3161 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
3162 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
3166 g_object_set (rtpsession, "probation", src->probation, NULL);
3168 g_object_set (rtpsession, "internal-ssrc", stream->send_ssrc, NULL);
3170 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
3172 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
3174 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
3176 g_signal_connect (rtpsession, "on-ssrc-active",
3177 (GCallback) on_ssrc_active, stream);
3188 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3193 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
3196 start_manager_failure:
3198 GST_DEBUG_OBJECT (src, "could not start session manager");
3203 /* free the UDP sources allocated when negotiating a transport.
3204 * This function is called when the server negotiated to a transport where the
3205 * UDP sources are not needed anymore, such as TCP or multicast. */
3207 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
3211 for (i = 0; i < 2; i++) {
3212 if (stream->udpsrc[i]) {
3213 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
3214 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
3215 gst_object_unref (stream->udpsrc[i]);
3216 stream->udpsrc[i] = NULL;
3221 /* for TCP, create pads to send and receive data to and from the manager and to
3222 * intercept various events and queries
3225 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
3226 GstRTSPTransport * transport, GstPad ** outpad)
3229 GstPadTemplate *template;
3230 GstPad *pad0, *pad1;
3232 /* configure for interleaved delivery, nothing needs to be done
3233 * here, the loop function will call the chain functions of the
3234 * session manager. */
3235 stream->channel[0] = transport->interleaved.min;
3236 stream->channel[1] = transport->interleaved.max;
3237 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
3238 stream->channel[0], stream->channel[1]);
3240 /* we can remove the allocated UDP ports now */
3241 gst_rtspsrc_stream_free_udp (stream);
3243 /* no session manager, send data to srcpad directly */
3244 if (!stream->channelpad[0]) {
3245 GST_DEBUG_OBJECT (src, "no manager, creating pad");
3247 /* create a new pad we will use to stream to */
3248 name = g_strdup_printf ("stream_%u", stream->id);
3249 template = gst_static_pad_template_get (&rtptemplate);
3250 stream->channelpad[0] = gst_pad_new_from_template (template, name);
3251 gst_object_unref (template);
3254 /* set caps and activate */
3255 gst_pad_use_fixed_caps (stream->channelpad[0]);
3256 gst_pad_set_active (stream->channelpad[0], TRUE);
3258 *outpad = gst_object_ref (stream->channelpad[0]);
3260 GST_DEBUG_OBJECT (src, "using manager source pad");
3262 template = gst_static_pad_template_get (&anysrctemplate);
3264 /* allocate pads for sending the channel data into the manager */
3265 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
3266 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
3267 gst_object_unref (stream->channelpad[0]);
3268 stream->channelpad[0] = pad0;
3269 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
3270 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
3271 gst_pad_set_element_private (pad0, src);
3272 gst_pad_set_active (pad0, TRUE);
3274 if (stream->channelpad[1]) {
3275 /* if we have a sinkpad for the other channel, create a pad and link to the
3277 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
3278 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
3279 gst_pad_link_full (pad1, stream->channelpad[1],
3280 GST_PAD_LINK_CHECK_NOTHING);
3281 gst_object_unref (stream->channelpad[1]);
3282 stream->channelpad[1] = pad1;
3283 gst_pad_set_active (pad1, TRUE);
3285 gst_object_unref (template);
3287 /* setup RTCP transport back to the server if we have to. */
3288 if (src->manager && src->do_rtcp) {
3291 template = gst_static_pad_template_get (&anysinktemplate);
3293 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
3294 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
3295 gst_pad_set_element_private (stream->rtcppad, stream);
3296 gst_pad_set_active (stream->rtcppad, TRUE);
3298 /* get session RTCP pad */
3299 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3300 pad = gst_element_get_request_pad (src->manager, name);
3305 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3306 gst_object_unref (pad);
3309 gst_object_unref (template);
3315 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
3316 GstRTSPTransport * transport, const gchar ** destination, gint * min,
3317 gint * max, guint * ttl)
3319 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3321 if (!(*destination = transport->destination))
3322 *destination = stream->destination;
3325 /* transport first */
3326 *min = transport->port.min;
3327 *max = transport->port.max;
3328 if (*min == -1 && *max == -1) {
3329 /* then try from SDP */
3330 if (stream->port != 0) {
3331 *min = stream->port;
3332 *max = stream->port + 1;
3338 if (!(*ttl = transport->ttl))
3343 /* first take the source, then the endpoint to figure out where to send
3345 if (!(*destination = transport->source)) {
3346 if (src->conninfo.connection)
3347 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3348 else if (stream->conninfo.connection)
3350 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3354 /* for unicast we only expect the ports here */
3355 *min = transport->server_port.min;
3356 *max = transport->server_port.max;
3361 /* For multicast create UDP sources and join the multicast group. */
3363 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3364 GstRTSPTransport * transport, GstPad ** outpad)
3367 const gchar *destination;
3370 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3372 /* we can remove the allocated UDP ports now */
3373 gst_rtspsrc_stream_free_udp (stream);
3375 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3378 /* we need a destination now */
3379 if (destination == NULL)
3380 goto no_destination;
3382 /* we really need ports now or we won't be able to receive anything at all */
3383 if (min == -1 && max == -1)
3386 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3387 destination, min, max);
3389 /* creating UDP source for RTP */
3391 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3393 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3395 if (stream->udpsrc[0] == NULL)
3398 /* take ownership */
3399 gst_object_ref_sink (stream->udpsrc[0]);
3401 if (src->udp_buffer_size != 0)
3402 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3403 src->udp_buffer_size, NULL);
3405 if (src->multi_iface != NULL)
3406 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3407 src->multi_iface, NULL);
3410 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3411 gst_element_set_state (stream->udpsrc[0], GST_STATE_READY);
3414 /* creating another UDP source for RTCP */
3418 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3420 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3422 if (stream->udpsrc[1] == NULL)
3425 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3426 stream->profile == GST_RTSP_PROFILE_SAVPF)
3427 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3429 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3430 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3431 gst_caps_unref (caps);
3433 /* take ownership */
3434 gst_object_ref_sink (stream->udpsrc[1]);
3436 if (src->multi_iface != NULL)
3437 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3438 src->multi_iface, NULL);
3440 gst_element_set_state (stream->udpsrc[1], GST_STATE_READY);
3447 GST_DEBUG_OBJECT (src, "no UDP source element found");
3452 GST_DEBUG_OBJECT (src, "no destination found");
3457 GST_DEBUG_OBJECT (src, "no ports found");
3462 /* configure the remainder of the UDP ports */
3464 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3465 GstRTSPTransport * transport, GstPad ** outpad)
3467 /* we manage the UDP elements now. For unicast, the UDP sources where
3468 * allocated in the stream when we suggested a transport. */
3469 if (stream->udpsrc[0]) {
3472 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3473 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3475 GST_DEBUG_OBJECT (src, "setting up UDP source");
3477 /* configure a timeout on the UDP port. When the timeout message is
3478 * posted, we assume UDP transport is not possible. We reconnect using TCP
3480 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3481 src->udp_timeout * 1000, NULL);
3483 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt)))
3484 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
3486 /* get output pad of the UDP source. */
3487 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3489 /* save it so we can unblock */
3490 stream->blockedpad = *outpad;
3492 /* configure pad block on the pad. As soon as there is dataflow on the
3493 * UDP source, we know that UDP is not blocked by a firewall and we can
3494 * configure all the streams to let the application autoplug decoders. */
3496 gst_pad_add_probe (stream->blockedpad,
3497 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3498 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocked, src, NULL);
3500 if (stream->channelpad[0]) {
3501 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3502 /* configure for UDP delivery, we need to connect the UDP pads to
3503 * the session plugin. */
3504 gst_pad_link_full (*outpad, stream->channelpad[0],
3505 GST_PAD_LINK_CHECK_NOTHING);
3506 gst_object_unref (*outpad);
3508 /* we connected to pad-added signal to get pads from the manager */
3510 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3515 if (stream->udpsrc[1]) {
3518 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3519 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3521 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3522 stream->profile == GST_RTSP_PROFILE_SAVPF)
3523 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3525 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3526 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3527 gst_caps_unref (caps);
3529 if (stream->channelpad[1]) {
3532 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3534 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3535 gst_pad_link_full (pad, stream->channelpad[1],
3536 GST_PAD_LINK_CHECK_NOTHING);
3537 gst_object_unref (pad);
3539 /* leave unlinked */
3545 /* configure the UDP sink back to the server for status reports */
3547 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3548 GstRTSPStream * stream, GstRTSPTransport * transport)
3551 gint rtp_port, rtcp_port;
3552 gboolean do_rtp, do_rtcp;
3553 const gchar *destination;
3558 /* get transport info */
3559 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
3560 &rtp_port, &rtcp_port, &ttl);
3562 /* see what we need to do */
3563 do_rtp = (rtp_port != -1);
3564 /* it's possible that the server does not want us to send RTCP in which case
3566 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
3568 /* we need a destination when we have RTP or RTCP ports */
3569 if (destination == NULL && (do_rtp || do_rtcp))
3570 goto no_destination;
3572 /* try to construct the fakesrc to the RTP port of the server to open up any
3575 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
3578 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
3579 stream->udpsink[0] =
3580 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3582 if (stream->udpsink[0] == NULL)
3583 goto no_sink_element;
3585 /* don't join multicast group, we will have the source socket do that */
3586 /* no sync or async state changes needed */
3587 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
3588 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3590 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3592 if (stream->udpsrc[0]) {
3593 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
3594 * so that NAT firewalls will open a hole for us */
3595 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
3596 GST_DEBUG_OBJECT (src, "RTP 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[0]), "socket", socket,
3600 "close-socket", FALSE, NULL);
3601 g_object_unref (socket);
3604 /* the source for the dummy packets to open up NAT */
3605 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
3606 if (stream->fakesrc == NULL)
3607 goto no_fakesrc_element;
3609 /* random data in 5 buffers, a size of 200 bytes should be fine */
3610 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
3611 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
3613 /* we don't want to consider this a sink */
3614 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
3616 /* keep everything locked */
3617 gst_element_set_locked_state (stream->udpsink[0], TRUE);
3618 gst_element_set_locked_state (stream->fakesrc, TRUE);
3620 gst_object_ref (stream->udpsink[0]);
3621 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
3622 gst_object_ref (stream->fakesrc);
3623 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
3625 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
3626 "sink", GST_PAD_LINK_CHECK_NOTHING);
3629 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
3632 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
3633 stream->udpsink[1] =
3634 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3636 if (stream->udpsink[1] == NULL)
3637 goto no_sink_element;
3639 /* don't join multicast group, we will have the source socket do that */
3640 /* no sync or async state changes needed */
3641 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
3642 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3644 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3646 if (stream->udpsrc[1]) {
3647 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
3648 * because some servers check the port number of where it sends RTCP to identify
3649 * the RTCP packets it receives */
3650 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
3651 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
3652 /* configure socket and make sure udpsink does not close it when shutting
3653 * down, it belongs to udpsrc after all. */
3654 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
3655 "close-socket", FALSE, NULL);
3656 g_object_unref (socket);
3659 /* we don't want to consider this a sink */
3660 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
3662 /* we keep this playing always */
3663 gst_element_set_locked_state (stream->udpsink[1], TRUE);
3664 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
3666 gst_object_ref (stream->udpsink[1]);
3667 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
3669 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
3671 /* get session RTCP pad */
3672 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3673 pad = gst_element_get_request_pad (src->manager, name);
3678 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3679 gst_object_unref (pad);
3688 GST_DEBUG_OBJECT (src, "no destination address specified");
3693 GST_DEBUG_OBJECT (src, "no UDP sink element found");
3698 GST_DEBUG_OBJECT (src, "no fakesrc element found");
3703 /* sets up all elements needed for streaming over the specified transport.
3704 * Does not yet expose the element pads, this will be done when there is actuall
3705 * dataflow detected, which might never happen when UDP is blocked in a
3706 * firewall, for example.
3709 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
3710 GstRTSPTransport * transport)
3713 GstPad *outpad = NULL;
3714 GstPadTemplate *template;
3716 const gchar *media_type;
3719 src = stream->parent;
3721 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
3723 /* get the proper media type for this stream now */
3724 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
3725 goto unknown_transport;
3727 goto unknown_transport;
3729 /* configure the final media type */
3730 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
3732 len = stream->ptmap->len;
3733 for (i = 0; i < len; i++) {
3735 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3737 if (item->caps == NULL)
3740 s = gst_caps_get_structure (item->caps, 0);
3741 gst_structure_set_name (s, media_type);
3742 /* set ssrc if known */
3743 if (transport->ssrc)
3744 gst_structure_set (s, "ssrc", G_TYPE_UINT, transport->ssrc, NULL);
3747 /* try to get and configure a manager, channelpad[0-1] will be configured with
3748 * the pads for the manager, or NULL when no manager is needed. */
3749 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
3752 switch (transport->lower_transport) {
3753 case GST_RTSP_LOWER_TRANS_TCP:
3754 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
3755 goto transport_failed;
3757 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3758 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
3759 goto transport_failed;
3760 /* fallthrough, the rest is the same for UDP and MCAST */
3761 case GST_RTSP_LOWER_TRANS_UDP:
3762 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
3763 goto transport_failed;
3764 /* configure udpsinks back to the server for RTCP messages and for the
3765 * dummy RTP messages to open NAT. */
3766 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
3767 goto transport_failed;
3770 goto unknown_transport;
3774 GST_DEBUG_OBJECT (src, "creating ghostpad");
3776 gst_pad_use_fixed_caps (outpad);
3778 /* create ghostpad, don't add just yet, this will be done when we activate
3780 name = g_strdup_printf ("stream_%u", stream->id);
3781 template = gst_static_pad_template_get (&rtptemplate);
3782 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
3783 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3784 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3785 gst_object_unref (template);
3788 gst_object_unref (outpad);
3790 /* mark pad as ok */
3791 stream->last_ret = GST_FLOW_OK;
3798 GST_DEBUG_OBJECT (src, "failed to configure transport");
3803 GST_DEBUG_OBJECT (src, "unknown transport");
3808 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3813 /* send a couple of dummy random packets on the receiver RTP port to the server,
3814 * this should make a firewall think we initiated the data transfer and
3815 * hopefully allow packets to go from the sender port to our RTP receiver port */
3817 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
3821 if (src->nat_method != GST_RTSP_NAT_DUMMY)
3824 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3825 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3827 if (stream->fakesrc && stream->udpsink[0]) {
3828 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
3829 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
3830 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
3831 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
3832 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
3838 /* Adds the source pads of all configured streams to the element.
3839 * This code is performed when we detected dataflow.
3841 * We detect dataflow from either the _loop function or with pad probes on the
3845 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
3849 GST_DEBUG_OBJECT (src, "activating streams");
3851 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3852 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3854 if (stream->udpsrc[0]) {
3855 /* remove timeout, we are streaming now and timeouts will be handled by
3856 * the session manager and jitter buffer */
3857 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
3859 if (stream->srcpad) {
3860 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
3861 gst_pad_set_active (stream->srcpad, TRUE);
3863 /* if we don't have a session manager, set the caps now. If we have a
3864 * session, we will get a notification of the pad and the caps. */
3865 if (!src->manager) {
3868 caps = stream_get_caps_for_pt (stream, stream->default_pt);
3869 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
3870 gst_pad_set_caps (stream->srcpad, caps);
3873 if (!stream->added) {
3874 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
3875 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3876 stream->added = TRUE;
3881 /* unblock all pads */
3882 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3883 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3885 if (stream->blockid) {
3886 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
3887 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
3888 stream->blockid = 0;
3896 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
3897 gboolean reset_manager)
3900 guint64 start, stop;
3901 gdouble play_speed, play_scale;
3903 GST_DEBUG_OBJECT (src, "configuring stream caps");
3905 start = segment->position;
3906 stop = segment->duration;
3907 play_speed = segment->rate;
3908 play_scale = segment->applied_rate;
3910 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3911 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3917 len = stream->ptmap->len;
3918 for (j = 0; j < len; j++) {
3920 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
3922 if (item->caps == NULL)
3925 caps = gst_caps_make_writable (item->caps);
3927 if (stream->timebase != -1)
3928 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
3929 (guint) stream->timebase, NULL);
3930 if (stream->seqbase != -1)
3931 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
3932 (guint) stream->seqbase, NULL);
3933 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
3935 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
3936 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
3937 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
3940 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
3943 if (item->pt == stream->default_pt && stream->udpsrc[0]) {
3944 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
3948 if (reset_manager && src->manager) {
3949 GST_DEBUG_OBJECT (src, "clear session");
3950 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
3954 static GstFlowReturn
3955 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
3960 /* store the value */
3961 stream->last_ret = ret;
3963 /* if it's success we can return the value right away */
3964 if (ret == GST_FLOW_OK)
3967 /* any other error that is not-linked can be returned right
3969 if (ret != GST_FLOW_NOT_LINKED)
3972 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3973 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3974 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3976 ret = ostream->last_ret;
3977 /* some other return value (must be SUCCESS but we can return
3978 * other values as well) */
3979 if (ret != GST_FLOW_NOT_LINKED)
3982 /* if we get here, all other pads were unlinked and we return
3983 * NOT_LINKED then */
3989 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
3992 gboolean res = TRUE;
3994 /* only streams that have a connection to the outside world */
3998 if (stream->udpsrc[0]) {
3999 gst_event_ref (event);
4000 res = gst_element_send_event (stream->udpsrc[0], event);
4001 } else if (stream->channelpad[0]) {
4002 gst_event_ref (event);
4003 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4004 res = gst_pad_push_event (stream->channelpad[0], event);
4006 res = gst_pad_send_event (stream->channelpad[0], event);
4009 if (stream->udpsrc[1]) {
4010 gst_event_ref (event);
4011 res &= gst_element_send_event (stream->udpsrc[1], event);
4012 } else if (stream->channelpad[1]) {
4013 gst_event_ref (event);
4014 if (GST_PAD_IS_SRC (stream->channelpad[1]))
4015 res &= gst_pad_push_event (stream->channelpad[1], event);
4017 res &= gst_pad_send_event (stream->channelpad[1], event);
4021 gst_event_unref (event);
4027 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
4030 gboolean res = TRUE;
4032 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4033 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4035 gst_event_ref (event);
4036 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
4038 gst_event_unref (event);
4043 static GstRTSPResult
4044 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4049 if (info->connection == NULL) {
4050 if (info->url == NULL) {
4051 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
4052 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
4056 /* create connection */
4057 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
4058 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
4059 goto could_not_create;
4062 g_free (info->url_str);
4063 info->url_str = gst_rtsp_url_get_request_uri (info->url);
4065 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
4067 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
4068 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
4069 src->tls_validation_flags))
4070 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
4072 if (src->tls_database)
4073 gst_rtsp_connection_set_tls_database (info->connection,
4077 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
4078 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
4080 if (src->proxy_host) {
4081 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
4083 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
4088 if (!info->connected) {
4091 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
4092 ("Connecting to %s", info->location));
4093 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
4095 gst_rtsp_connection_connect (info->connection,
4096 src->ptcp_timeout)) < 0)
4097 goto could_not_connect;
4099 info->connected = TRUE;
4106 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
4111 gchar *str = gst_rtsp_strresult (res);
4112 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
4118 gchar *str = gst_rtsp_strresult (res);
4119 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
4125 static GstRTSPResult
4126 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
4129 GST_RTSP_STATE_LOCK (src);
4130 if (info->connected) {
4131 GST_DEBUG_OBJECT (src, "closing connection...");
4132 gst_rtsp_connection_close (info->connection);
4133 info->connected = FALSE;
4135 if (free && info->connection) {
4136 /* free connection */
4137 GST_DEBUG_OBJECT (src, "freeing connection...");
4138 gst_rtsp_connection_free (info->connection);
4139 info->connection = NULL;
4141 GST_RTSP_STATE_UNLOCK (src);
4145 static GstRTSPResult
4146 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4151 GST_DEBUG_OBJECT (src, "reconnecting connection...");
4152 gst_rtsp_conninfo_close (src, info, FALSE);
4153 res = gst_rtsp_conninfo_connect (src, info, async);
4159 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
4163 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
4164 GST_RTSP_STATE_LOCK (src);
4165 if (src->conninfo.connection && src->conninfo.flushing != flush) {
4166 GST_DEBUG_OBJECT (src, "connection flush");
4167 gst_rtsp_connection_flush (src->conninfo.connection, flush);
4168 src->conninfo.flushing = flush;
4170 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4171 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4172 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
4173 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
4174 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
4175 stream->conninfo.flushing = flush;
4178 GST_RTSP_STATE_UNLOCK (src);
4181 /* FIXME, handle server request, reply with OK, for now */
4182 static GstRTSPResult
4183 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
4184 GstRTSPMessage * request)
4186 GstRTSPMessage response = { 0 };
4189 GST_DEBUG_OBJECT (src, "got server request message");
4192 gst_rtsp_message_dump (request);
4194 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
4196 if (res == GST_RTSP_ENOTIMPL) {
4197 /* default implementation, send OK */
4198 GST_DEBUG_OBJECT (src, "prepare OK reply");
4200 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
4205 /* let app parse and reply */
4206 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
4207 0, request, &response);
4210 gst_rtsp_message_dump (&response);
4212 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
4216 gst_rtsp_message_unset (&response);
4217 } else if (res == GST_RTSP_EEOF)
4225 gst_rtsp_message_unset (&response);
4230 /* send server keep-alive */
4231 static GstRTSPResult
4232 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
4234 GstRTSPMessage request = { 0 };
4236 GstRTSPMethod method;
4237 const gchar *control;
4239 if (src->do_rtsp_keep_alive == FALSE) {
4240 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
4241 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4245 GST_DEBUG_OBJECT (src, "creating server keep-alive");
4247 /* find a method to use for keep-alive */
4248 if (src->methods & GST_RTSP_GET_PARAMETER)
4249 method = GST_RTSP_GET_PARAMETER;
4251 method = GST_RTSP_OPTIONS;
4253 control = get_aggregate_control (src);
4254 if (control == NULL)
4257 res = gst_rtsp_message_init_request (&request, method, control);
4262 gst_rtsp_message_dump (&request);
4265 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
4270 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4271 gst_rtsp_message_unset (&request);
4278 GST_WARNING_OBJECT (src, "no control url to send keepalive");
4283 gchar *str = gst_rtsp_strresult (res);
4285 gst_rtsp_message_unset (&request);
4286 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
4287 ("Could not send keep-alive. (%s)", str));
4293 static GstFlowReturn
4294 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
4296 GstFlowReturn ret = GST_FLOW_OK;
4298 GstRTSPStream *stream;
4299 GstPad *outpad = NULL;
4306 channel = message->type_data.data.channel;
4308 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
4310 goto unknown_stream;
4312 if (channel == stream->channel[0]) {
4313 outpad = stream->channelpad[0];
4315 } else if (channel == stream->channel[1]) {
4316 outpad = stream->channelpad[1];
4322 /* take a look at the body to figure out what we have */
4323 gst_rtsp_message_get_body (message, &data, &size);
4325 goto invalid_length;
4327 /* channels are not correct on some servers, do extra check */
4328 if (data[1] >= 200 && data[1] <= 204) {
4329 /* hmm RTCP message switch to the RTCP pad of the same stream. */
4330 outpad = stream->channelpad[1];
4334 /* we have no clue what this is, just ignore then. */
4336 goto unknown_stream;
4338 /* take the message body for further processing */
4339 gst_rtsp_message_steal_body (message, &data, &size);
4341 /* strip the trailing \0 */
4344 buf = gst_buffer_new ();
4345 gst_buffer_append_memory (buf,
4346 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
4348 /* don't need message anymore */
4349 gst_rtsp_message_unset (message);
4351 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
4354 if (src->need_activate) {
4360 guint group_id = gst_util_group_id_next ();
4362 /* generate an SHA256 sum of the URI */
4363 cs = g_checksum_new (G_CHECKSUM_SHA256);
4364 uri = src->conninfo.location;
4365 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
4367 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4368 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4371 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
4372 event = gst_event_new_stream_start (stream_id);
4373 gst_event_set_group_id (event, group_id);
4376 gst_rtspsrc_stream_push_event (src, ostream, event);
4378 g_checksum_free (cs);
4380 gst_rtspsrc_activate_streams (src);
4381 src->need_activate = FALSE;
4383 if ((event = src->start_segment) != NULL) {
4384 src->start_segment = NULL;
4385 gst_rtspsrc_push_event (src, event);
4388 if (src->base_time == -1) {
4389 /* Take current running_time. This timestamp will be put on
4390 * the first buffer of each stream because we are a live source and so we
4391 * timestamp with the running_time. When we are dealing with TCP, we also
4392 * only timestamp the first buffer (using the DISCONT flag) because a server
4393 * typically bursts data, for which we don't want to compensate by speeding
4394 * up the media. The other timestamps will be interpollated from this one
4395 * using the RTP timestamps. */
4396 GST_OBJECT_LOCK (src);
4397 if (GST_ELEMENT_CLOCK (src)) {
4399 GstClockTime base_time;
4401 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
4402 base_time = GST_ELEMENT_CAST (src)->base_time;
4404 src->base_time = now - base_time;
4406 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
4407 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
4409 GST_OBJECT_UNLOCK (src);
4412 if (stream->discont && !is_rtcp) {
4413 /* mark first RTP buffer as discont */
4414 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4415 stream->discont = FALSE;
4416 /* first buffer gets the timestamp, other buffers are not timestamped and
4417 * their presentation time will be interpollated from the rtp timestamps. */
4418 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
4419 GST_TIME_ARGS (src->base_time));
4421 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
4424 /* chain to the peer pad */
4425 if (GST_PAD_IS_SINK (outpad))
4426 ret = gst_pad_chain (outpad, buf);
4428 ret = gst_pad_push (outpad, buf);
4431 /* combine all stream flows for the data transport */
4432 ret = gst_rtspsrc_combine_flows (src, stream, ret);
4439 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
4440 gst_rtsp_message_unset (message);
4445 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4446 ("Short message received, ignoring."));
4447 gst_rtsp_message_unset (message);
4452 static GstFlowReturn
4453 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
4455 GstRTSPMessage message = { 0 };
4457 GstFlowReturn ret = GST_FLOW_OK;
4458 GTimeVal tv_timeout;
4461 /* get the next timeout interval */
4462 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4464 /* see if the timeout period expired */
4465 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
4466 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
4467 /* send keep-alive, only act on interrupt, a warning will be posted for
4469 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4471 /* get new timeout */
4472 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4475 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
4476 tv_timeout.tv_sec, tv_timeout.tv_usec);
4478 /* protect the connection with the connection lock so that we can see when
4479 * we are finished doing server communication */
4481 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4482 &message, src->ptcp_timeout);
4486 GST_DEBUG_OBJECT (src, "we received a server message");
4488 case GST_RTSP_EINTR:
4489 /* we got interrupted this means we need to stop */
4491 case GST_RTSP_ETIMEOUT:
4492 /* no reply, send keep alive */
4493 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4494 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4498 /* go EOS when the server closed the connection */
4504 switch (message.type) {
4505 case GST_RTSP_MESSAGE_REQUEST:
4506 /* server sends us a request message, handle it */
4508 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4510 if (res == GST_RTSP_EEOF)
4513 goto handle_request_failed;
4515 case GST_RTSP_MESSAGE_RESPONSE:
4516 /* we ignore response messages */
4517 GST_DEBUG_OBJECT (src, "ignoring response message");
4519 gst_rtsp_message_dump (&message);
4521 case GST_RTSP_MESSAGE_DATA:
4522 GST_DEBUG_OBJECT (src, "got data message");
4523 ret = gst_rtspsrc_handle_data (src, &message);
4524 if (ret != GST_FLOW_OK)
4525 goto handle_data_failed;
4528 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4533 g_assert_not_reached ();
4538 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4539 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4540 ("The server closed the connection."));
4541 src->conninfo.connected = FALSE;
4542 gst_rtsp_message_unset (&message);
4543 return GST_FLOW_EOS;
4547 gst_rtsp_message_unset (&message);
4548 GST_DEBUG_OBJECT (src, "got interrupted");
4549 return GST_FLOW_FLUSHING;
4553 gchar *str = gst_rtsp_strresult (res);
4555 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4556 ("Could not receive message. (%s)", str));
4559 gst_rtsp_message_unset (&message);
4560 return GST_FLOW_ERROR;
4562 handle_request_failed:
4564 gchar *str = gst_rtsp_strresult (res);
4566 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4567 ("Could not handle server message. (%s)", str));
4569 gst_rtsp_message_unset (&message);
4570 return GST_FLOW_ERROR;
4574 GST_DEBUG_OBJECT (src, "could no handle data message");
4579 static GstFlowReturn
4580 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
4583 GstRTSPMessage message = { 0 };
4587 GTimeVal tv_timeout;
4589 /* get the next timeout interval */
4590 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4592 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
4593 (gint) tv_timeout.tv_sec);
4595 gst_rtsp_message_unset (&message);
4597 /* we should continue reading the TCP socket because the server might
4598 * send us requests. When the session timeout expires, we need to send a
4599 * keep-alive request to keep the session open. */
4600 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4601 &message, &tv_timeout);
4605 GST_DEBUG_OBJECT (src, "we received a server message");
4607 case GST_RTSP_EINTR:
4608 /* we got interrupted, see what we have to do */
4610 case GST_RTSP_ETIMEOUT:
4611 /* send keep-alive, ignore the result, a warning will be posted. */
4612 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4613 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4617 /* server closed the connection. not very fatal for UDP, reconnect and
4618 * see what happens. */
4619 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4620 ("The server closed the connection."));
4621 if (src->udp_reconnect) {
4623 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
4630 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
4632 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4633 ("Unhandled return value %d.", res));
4637 switch (message.type) {
4638 case GST_RTSP_MESSAGE_REQUEST:
4639 /* server sends us a request message, handle it */
4641 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4643 if (res == GST_RTSP_EEOF)
4646 goto handle_request_failed;
4648 case GST_RTSP_MESSAGE_RESPONSE:
4649 /* we ignore response and data messages */
4650 GST_DEBUG_OBJECT (src, "ignoring response message");
4652 gst_rtsp_message_dump (&message);
4653 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4654 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
4655 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
4656 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
4657 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4664 case GST_RTSP_MESSAGE_DATA:
4665 /* we ignore response and data messages */
4666 GST_DEBUG_OBJECT (src, "ignoring data message");
4669 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4674 g_assert_not_reached ();
4676 /* we get here when the connection got interrupted */
4679 gst_rtsp_message_unset (&message);
4680 GST_DEBUG_OBJECT (src, "got interrupted");
4681 return GST_FLOW_FLUSHING;
4685 gchar *str = gst_rtsp_strresult (res);
4688 src->conninfo.connected = FALSE;
4689 if (res != GST_RTSP_EINTR) {
4690 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
4691 ("Could not connect to server. (%s)", str));
4693 ret = GST_FLOW_ERROR;
4695 ret = GST_FLOW_FLUSHING;
4701 gchar *str = gst_rtsp_strresult (res);
4703 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4704 ("Could not receive message. (%s)", str));
4706 return GST_FLOW_ERROR;
4708 handle_request_failed:
4710 gchar *str = gst_rtsp_strresult (res);
4713 gst_rtsp_message_unset (&message);
4714 if (res != GST_RTSP_EINTR) {
4715 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4716 ("Could not handle server message. (%s)", str));
4718 ret = GST_FLOW_ERROR;
4720 ret = GST_FLOW_FLUSHING;
4726 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4727 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4728 ("The server closed the connection."));
4729 src->conninfo.connected = FALSE;
4730 gst_rtsp_message_unset (&message);
4731 return GST_FLOW_EOS;
4735 static GstRTSPResult
4736 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
4738 GstRTSPResult res = GST_RTSP_OK;
4741 GST_DEBUG_OBJECT (src, "doing reconnect");
4743 GST_OBJECT_LOCK (src);
4744 /* only restart when the pads were not yet activated, else we were
4745 * streaming over UDP */
4746 restart = src->need_activate;
4747 GST_OBJECT_UNLOCK (src);
4749 /* no need to restart, we're done */
4753 /* we can try only TCP now */
4754 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
4756 /* close and cleanup our state */
4757 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
4760 /* see if we have TCP left to try. Also don't try TCP when we were configured
4762 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
4765 /* We post a warning message now to inform the user
4766 * that nothing happened. It's most likely a firewall thing. */
4767 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4768 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4769 "firewall is blocking it. Retrying using a TCP connection.",
4770 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4772 /* open new connection using tcp */
4773 if (gst_rtspsrc_open (src, async) < 0)
4776 /* start playback */
4777 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
4786 src->cur_protocols = 0;
4787 /* no transport possible, post an error and stop */
4788 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4789 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4790 "firewall is blocking it. No other protocols to try.",
4791 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4792 return GST_RTSP_ERROR;
4796 GST_DEBUG_OBJECT (src, "open failed");
4801 GST_DEBUG_OBJECT (src, "play failed");
4807 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
4811 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
4814 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
4817 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
4820 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
4828 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
4832 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
4835 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
4838 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
4841 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
4849 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
4853 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
4856 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
4859 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
4862 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
4870 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
4874 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
4877 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
4880 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
4883 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
4891 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
4893 if (ret == GST_RTSP_OK)
4894 gst_rtspsrc_loop_complete_cmd (src, cmd);
4895 else if (ret == GST_RTSP_EINTR)
4896 gst_rtspsrc_loop_cancel_cmd (src, cmd);
4898 gst_rtspsrc_loop_error_cmd (src, cmd);
4902 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
4905 gboolean flushed = FALSE;
4907 /* start new request */
4908 gst_rtspsrc_loop_start_cmd (src, cmd);
4910 GST_DEBUG_OBJECT (src, "sending cmd %d", cmd);
4912 GST_OBJECT_LOCK (src);
4913 old = src->pending_cmd;
4914 if (old == CMD_RECONNECT) {
4915 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
4916 cmd = CMD_RECONNECT;
4918 if (old != CMD_WAIT) {
4919 src->pending_cmd = CMD_WAIT;
4920 GST_OBJECT_UNLOCK (src);
4921 /* cancel previous request */
4922 GST_DEBUG_OBJECT (src, "cancel previous request %d", old);
4923 gst_rtspsrc_loop_cancel_cmd (src, old);
4924 GST_OBJECT_LOCK (src);
4926 src->pending_cmd = cmd;
4927 /* interrupt if allowed */
4928 if (src->busy_cmd & mask) {
4929 GST_DEBUG_OBJECT (src, "connection flush busy %d", src->busy_cmd);
4930 gst_rtspsrc_connection_flush (src, TRUE);
4933 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %d", src->busy_cmd);
4936 gst_task_start (src->task);
4937 GST_OBJECT_UNLOCK (src);
4943 gst_rtspsrc_loop (GstRTSPSrc * src)
4947 if (!src->conninfo.connection || !src->conninfo.connected)
4950 if (src->interleaved)
4951 ret = gst_rtspsrc_loop_interleaved (src);
4953 ret = gst_rtspsrc_loop_udp (src);
4955 if (ret != GST_FLOW_OK)
4963 GST_WARNING_OBJECT (src, "we are not connected");
4964 ret = GST_FLOW_FLUSHING;
4969 const gchar *reason = gst_flow_get_name (ret);
4971 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
4972 src->running = FALSE;
4973 if (ret == GST_FLOW_EOS) {
4974 /* perform EOS logic */
4975 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4976 gst_element_post_message (GST_ELEMENT_CAST (src),
4977 gst_message_new_segment_done (GST_OBJECT_CAST (src),
4978 src->segment.format, src->segment.position));
4979 gst_rtspsrc_push_event (src,
4980 gst_event_new_segment_done (src->segment.format,
4981 src->segment.position));
4983 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4985 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
4986 /* for fatal errors we post an error message, post the error before the
4987 * EOS so the app knows about the error first. */
4988 GST_ELEMENT_ERROR (src, STREAM, FAILED,
4989 ("Internal data flow error."),
4990 ("streaming task paused, reason %s (%d)", reason, ret));
4991 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4993 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
4998 #ifndef GST_DISABLE_GST_DEBUG
4999 static const gchar *
5000 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
5004 while (method != 0) {
5021 static const gchar *
5022 gst_rtspsrc_skip_lws (const gchar * s)
5024 while (g_ascii_isspace (*s))
5029 static const gchar *
5030 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
5032 while (s > start && g_ascii_isspace (*(s - 1)))
5037 static const gchar *
5038 gst_rtspsrc_skip_commas (const gchar * s)
5040 /* The grammar allows for multiple commas */
5041 while (g_ascii_isspace (*s) || *s == ',')
5046 static const gchar *
5047 gst_rtspsrc_skip_item (const gchar * s)
5049 gboolean quoted = FALSE;
5050 const gchar *start = s;
5052 /* A list item ends at the last non-whitespace character
5053 * before a comma which is not inside a quoted-string. Or at
5054 * the end of the string.
5060 if (*s == '\\' && *(s + 1))
5069 return gst_rtspsrc_unskip_lws (s, start);
5073 gst_rtsp_decode_quoted_string (gchar * quoted_string)
5077 src = quoted_string + 1;
5078 dst = quoted_string;
5079 while (*src && *src != '"') {
5080 if (*src == '\\' && *(src + 1))
5087 /* Extract the authentication tokens that the server provided for each method
5088 * into an array of structures and give those to the connection object.
5091 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
5092 const gchar * header, gboolean * stale)
5094 GSList *list = NULL, *iter;
5096 gchar *item, *eq, *name_end, *value;
5098 g_return_if_fail (stale != NULL);
5100 gst_rtsp_connection_clear_auth_params (conn);
5103 /* Parse a header whose content is described by RFC2616 as
5104 * "#something", where "something" does not itself contain commas,
5105 * except as part of quoted-strings, into a list of allocated strings.
5107 header = gst_rtspsrc_skip_commas (header);
5109 end = gst_rtspsrc_skip_item (header);
5110 list = g_slist_prepend (list, g_strndup (header, end - header));
5111 header = gst_rtspsrc_skip_commas (end);
5116 list = g_slist_reverse (list);
5117 for (iter = list; iter; iter = iter->next) {
5120 eq = strchr (item, '=');
5122 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
5123 if (name_end == item) {
5124 /* That's no good... */
5131 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
5133 gst_rtsp_decode_quoted_string (value);
5137 if (item && (strcmp (item, "stale") == 0) &&
5138 value && (strcmp (value, "TRUE") == 0))
5140 gst_rtsp_connection_set_auth_param (conn, item, value);
5144 g_slist_free (list);
5147 /* Parse a WWW-Authenticate Response header and determine the
5148 * available authentication methods
5150 * This code should also cope with the fact that each WWW-Authenticate
5151 * header can contain multiple challenge methods + tokens
5153 * At the moment, for Basic auth, we just do a minimal check and don't
5154 * even parse out the realm */
5156 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
5157 GstRTSPConnection * conn, gboolean * stale)
5161 g_return_if_fail (hdr != NULL);
5162 g_return_if_fail (methods != NULL);
5163 g_return_if_fail (stale != NULL);
5165 /* Skip whitespace at the start of the string */
5166 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
5168 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
5169 *methods |= GST_RTSP_AUTH_BASIC;
5170 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
5171 *methods |= GST_RTSP_AUTH_DIGEST;
5172 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
5177 * gst_rtspsrc_setup_auth:
5178 * @src: the rtsp source
5180 * Configure a username and password and auth method on the
5181 * connection object based on a response we received from the
5184 * Currently, this requires that a username and password were supplied
5185 * in the uri. In the future, they may be requested on demand by sending
5186 * a message up the bus.
5188 * Returns: TRUE if authentication information could be set up correctly.
5191 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
5195 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
5196 GstRTSPAuthMethod method;
5197 GstRTSPResult auth_result;
5199 GstRTSPConnection *conn;
5201 gboolean stale = FALSE;
5203 conn = src->conninfo.connection;
5205 /* Identify the available auth methods and see if any are supported */
5206 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
5207 &hdr, 0) == GST_RTSP_OK) {
5208 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
5211 if (avail_methods == GST_RTSP_AUTH_NONE)
5212 goto no_auth_available;
5214 /* For digest auth, if the response indicates that the session
5215 * data are stale, we just update them in the connection object and
5216 * return TRUE to retry the request */
5218 src->tried_url_auth = FALSE;
5220 url = gst_rtsp_connection_get_url (conn);
5222 /* Do we have username and password available? */
5223 if (url != NULL && !src->tried_url_auth && url->user != NULL
5224 && url->passwd != NULL) {
5227 src->tried_url_auth = TRUE;
5228 GST_DEBUG_OBJECT (src,
5229 "Attempting authentication using credentials from the URL");
5231 user = src->user_id;
5232 pass = src->user_pw;
5233 GST_DEBUG_OBJECT (src,
5234 "Attempting authentication using credentials from the properties");
5237 /* FIXME: If the url didn't contain username and password or we tried them
5238 * already, request a username and passwd from the application via some kind
5239 * of credentials request message */
5241 /* If we don't have a username and passwd at this point, bail out. */
5242 if (user == NULL || pass == NULL)
5245 /* Try to configure for each available authentication method, strongest to
5247 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
5248 /* Check if this method is available on the server */
5249 if ((method & avail_methods) == 0)
5252 /* Pass the credentials to the connection to try on the next request */
5253 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
5254 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
5255 * ignore it and end up retrying later */
5256 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
5257 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
5258 gst_rtsp_auth_method_to_string (method));
5263 if (method == GST_RTSP_AUTH_NONE)
5264 goto no_auth_available;
5270 /* Output an error indicating that we couldn't connect because there were
5271 * no supported authentication protocols */
5272 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5273 ("No supported authentication protocol was found"));
5278 /* We don't fire an error message, we just return FALSE and let the
5279 * normal NOT_AUTHORIZED error be propagated */
5284 static GstRTSPResult
5285 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5286 GstRTSPMessage * request, GstRTSPMessage * response,
5287 GstRTSPStatusCode * code)
5290 GstRTSPStatusCode thecode;
5291 gchar *content_base = NULL;
5295 if (!src->short_header)
5296 gst_rtsp_ext_list_before_send (src->extensions, request);
5298 GST_DEBUG_OBJECT (src, "sending message");
5301 gst_rtsp_message_dump (request);
5303 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
5307 gst_rtsp_connection_reset_timeout (conn);
5310 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
5315 gst_rtsp_message_dump (response);
5317 switch (response->type) {
5318 case GST_RTSP_MESSAGE_REQUEST:
5319 res = gst_rtspsrc_handle_request (src, conn, response);
5320 if (res == GST_RTSP_EEOF)
5323 goto handle_request_failed;
5325 case GST_RTSP_MESSAGE_RESPONSE:
5326 /* ok, a response is good */
5327 GST_DEBUG_OBJECT (src, "received response message");
5329 case GST_RTSP_MESSAGE_DATA:
5330 /* get next response */
5331 GST_DEBUG_OBJECT (src, "handle data response message");
5332 gst_rtspsrc_handle_data (src, response);
5335 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5340 thecode = response->type_data.response.code;
5342 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
5344 /* if the caller wanted the result code, we store it. */
5348 /* If the request didn't succeed, bail out before doing any more */
5349 if (thecode != GST_RTSP_STS_OK)
5352 /* store new content base if any */
5353 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
5356 g_free (src->content_base);
5357 src->content_base = g_strdup (content_base);
5359 gst_rtsp_ext_list_after_send (src->extensions, request, response);
5366 gchar *str = gst_rtsp_strresult (res);
5368 if (res != GST_RTSP_EINTR) {
5369 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5370 ("Could not send message. (%s)", str));
5372 GST_WARNING_OBJECT (src, "send interrupted");
5381 GST_WARNING_OBJECT (src, "server closed connection");
5382 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
5384 /* if reconnect succeeds, try again */
5386 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
5390 /* only try once after reconnect, then fallthrough and error out */
5393 gchar *str = gst_rtsp_strresult (res);
5395 if (res != GST_RTSP_EINTR) {
5396 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5397 ("Could not receive message. (%s)", str));
5399 GST_WARNING_OBJECT (src, "receive interrupted");
5407 handle_request_failed:
5409 /* ERROR was posted */
5410 gst_rtsp_message_unset (response);
5415 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5416 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5417 ("The server closed the connection."));
5418 gst_rtsp_message_unset (response);
5425 * @src: the rtsp source
5426 * @conn: the connection to send on
5427 * @request: must point to a valid request
5428 * @response: must point to an empty #GstRTSPMessage
5429 * @code: an optional code result
5431 * send @request and retrieve the response in @response. optionally @code can be
5432 * non-NULL in which case it will contain the status code of the response.
5434 * If This function returns #GST_RTSP_OK, @response will contain a valid response
5435 * message that should be cleaned with gst_rtsp_message_unset() after usage.
5437 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
5438 * @response message) if the response code was not 200 (OK).
5440 * If the attempt results in an authentication failure, then this will attempt
5441 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
5444 * Returns: #GST_RTSP_OK if the processing was successful.
5446 static GstRTSPResult
5447 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5448 GstRTSPMessage * request, GstRTSPMessage * response,
5449 GstRTSPStatusCode * code)
5451 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
5452 GstRTSPResult res = GST_RTSP_ERROR;
5455 GstRTSPMethod method = GST_RTSP_INVALID;
5461 /* make sure we don't loop forever */
5465 /* save method so we can disable it when the server complains */
5466 method = request->type_data.request.method;
5469 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
5473 case GST_RTSP_STS_UNAUTHORIZED:
5474 if (gst_rtspsrc_setup_auth (src, response)) {
5475 /* Try the request/response again after configuring the auth info
5483 } while (retry == TRUE);
5485 /* If the user requested the code, let them handle errors, otherwise
5486 * post an error below */
5489 else if (int_code != GST_RTSP_STS_OK)
5490 goto error_response;
5497 GST_DEBUG_OBJECT (src, "got error %d", res);
5502 res = GST_RTSP_ERROR;
5504 switch (response->type_data.response.code) {
5505 case GST_RTSP_STS_NOT_FOUND:
5506 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
5507 response->type_data.response.reason));
5509 case GST_RTSP_STS_MOVED_PERMANENTLY:
5510 case GST_RTSP_STS_MOVE_TEMPORARILY:
5512 gchar *new_location;
5513 GstRTSPLowerTrans transports;
5515 GST_DEBUG_OBJECT (src, "got redirection");
5516 /* if we don't have a Location Header, we must error */
5517 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
5518 &new_location, 0) < 0)
5521 /* When we receive a redirect result, we go back to the INIT state after
5522 * parsing the new URI. The caller should do the needed steps to issue
5523 * a new setup when it detects this state change. */
5524 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
5526 /* save current transports */
5527 if (src->conninfo.url)
5528 transports = src->conninfo.url->transports;
5530 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
5532 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
5534 /* set old transports */
5535 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
5536 src->conninfo.url->transports = transports;
5538 src->need_redirect = TRUE;
5539 src->state = GST_RTSP_STATE_INIT;
5543 case GST_RTSP_STS_NOT_ACCEPTABLE:
5544 case GST_RTSP_STS_NOT_IMPLEMENTED:
5545 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
5546 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
5547 gst_rtsp_method_as_text (method));
5548 src->methods &= ~method;
5552 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5553 ("Got error response: %d (%s).", response->type_data.response.code,
5554 response->type_data.response.reason));
5557 /* if we return ERROR we should unset the response ourselves */
5558 if (res == GST_RTSP_ERROR)
5559 gst_rtsp_message_unset (response);
5565 static GstRTSPResult
5566 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
5567 GstRTSPMessage * response, GstRTSPSrc * src)
5569 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
5574 /* parse the response and collect all the supported methods. We need this
5575 * information so that we don't try to send an unsupported request to the
5579 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
5581 GstRTSPHeaderField field;
5585 /* reset supported methods */
5588 /* Try Allow Header first */
5589 field = GST_RTSP_HDR_ALLOW;
5592 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5593 if (indx == 0 && !respoptions) {
5594 /* if no Allow header was found then try the Public header... */
5595 field = GST_RTSP_HDR_PUBLIC;
5596 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5601 src->methods |= gst_rtsp_options_from_text (respoptions);
5606 if (src->methods == 0) {
5607 /* neither Allow nor Public are required, assume the server supports
5608 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
5610 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
5611 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
5613 /* always assume PLAY, FIXME, extensions should be able to override
5615 src->methods |= GST_RTSP_PLAY;
5616 /* also assume it will support Range */
5617 src->seekable = TRUE;
5619 /* we need describe and setup */
5620 if (!(src->methods & GST_RTSP_DESCRIBE))
5622 if (!(src->methods & GST_RTSP_SETUP))
5630 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5631 ("Server does not support DESCRIBE."));
5636 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5637 ("Server does not support SETUP."));
5642 /* masks to be kept in sync with the hardcoded protocol order of preference
5644 static guint protocol_masks[] = {
5645 GST_RTSP_LOWER_TRANS_UDP,
5646 GST_RTSP_LOWER_TRANS_UDP_MCAST,
5647 GST_RTSP_LOWER_TRANS_TCP,
5651 static GstRTSPResult
5652 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
5653 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
5657 gboolean add_udp_str;
5662 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
5667 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
5669 /* extension listed transports, use those */
5670 if (*transports != NULL)
5673 /* it's the default */
5674 add_udp_str = FALSE;
5676 /* the default RTSP transports */
5677 result = g_string_new ("RTP");
5680 case GST_RTSP_PROFILE_AVP:
5681 g_string_append (result, "/AVP");
5683 case GST_RTSP_PROFILE_SAVP:
5684 g_string_append (result, "/SAVP");
5686 case GST_RTSP_PROFILE_AVPF:
5687 g_string_append (result, "/AVPF");
5689 case GST_RTSP_PROFILE_SAVPF:
5690 g_string_append (result, "/SAVPF");
5696 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
5697 GST_DEBUG_OBJECT (src, "adding UDP unicast");
5699 g_string_append (result, "/UDP");
5700 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
5701 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
5702 GST_DEBUG_OBJECT (src, "adding UDP multicast");
5703 /* we don't have to allocate any UDP ports yet, if the selected transport
5704 * turns out to be multicast we can create them and join the multicast
5705 * group indicated in the transport reply */
5707 g_string_append (result, "/UDP");
5708 g_string_append (result, ";multicast");
5709 if (src->next_port_num != 0) {
5710 if (src->client_port_range.max > 0 &&
5711 src->next_port_num >= src->client_port_range.max)
5714 g_string_append_printf (result, ";client_port=%d-%d",
5715 src->next_port_num, src->next_port_num + 1);
5717 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
5718 GST_DEBUG_OBJECT (src, "adding TCP");
5720 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
5722 *transports = g_string_free (result, FALSE);
5724 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
5731 GST_ERROR ("extension gave error %d", res);
5736 GST_ERROR ("no more ports available");
5737 return GST_RTSP_ERROR;
5741 static GstRTSPResult
5742 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
5743 gint orig_rtpport, gint orig_rtcpport)
5746 gint nr_udp, nr_int;
5748 gint rtpport = 0, rtcpport = 0;
5751 src = stream->parent;
5753 /* find number of placeholders first */
5754 if (strstr (*transports, "%%i2"))
5756 else if (strstr (*transports, "%%i1"))
5761 if (strstr (*transports, "%%u2"))
5763 else if (strstr (*transports, "%%u1"))
5768 if (nr_udp == 0 && nr_int == 0)
5772 if (!orig_rtpport || !orig_rtcpport) {
5773 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
5776 rtpport = orig_rtpport;
5777 rtcpport = orig_rtcpport;
5781 str = g_string_new ("");
5783 while ((next = strstr (p, "%%"))) {
5784 g_string_append_len (str, p, next - p);
5785 if (next[2] == 'u') {
5787 g_string_append_printf (str, "%d", rtpport);
5788 else if (next[3] == '2')
5789 g_string_append_printf (str, "%d", rtcpport);
5791 if (next[2] == 'i') {
5793 g_string_append_printf (str, "%d", src->free_channel);
5794 else if (next[3] == '2')
5795 g_string_append_printf (str, "%d", src->free_channel + 1);
5800 /* append final part */
5801 g_string_append (str, p);
5803 g_free (*transports);
5804 *transports = g_string_free (str, FALSE);
5812 GST_ERROR ("failed to allocate udp ports");
5813 return GST_RTSP_ERROR;
5818 gst_rtspsrc_stream_make_keymgmt (GstRTSPSrc * src, GstRTSPStream * stream)
5821 gchar *result, *base64;
5826 GstMIKEYMessage *msg;
5827 GstMIKEYPayload *payload, *pkd;
5831 key_data = g_malloc (KEY_SIZE);
5832 for (i = 0; i < KEY_SIZE; i += 4)
5833 GST_WRITE_UINT32_BE (key_data + i, g_random_int ());
5836 gst_buffer_unref (stream->key);
5837 stream->key = gst_buffer_new_wrapped (key_data, KEY_SIZE);
5839 msg = gst_mikey_message_new ();
5840 /* unencrypted MIKEY message, we send this over TLS so this is allowed */
5841 gst_mikey_message_set_info (msg, GST_MIKEY_VERSION, GST_MIKEY_TYPE_PSK_INIT,
5842 FALSE, GST_MIKEY_PRF_MIKEY_1, g_random_int (), GST_MIKEY_MAP_TYPE_SRTP);
5843 /* add policy '0' for our SSRC */
5844 stream->send_ssrc = g_random_int ();
5845 gst_mikey_message_add_cs_srtp (msg, 0, stream->send_ssrc, 0);
5846 /* timestamp is now */
5847 gst_mikey_message_add_t_now_ntp_utc (msg);
5848 /* add some random data */
5849 gst_mikey_message_add_rand_len (msg, 16);
5851 /* the policy '0' is SRTP */
5852 payload = gst_mikey_payload_new (GST_MIKEY_PT_SP);
5853 gst_mikey_payload_sp_set (payload, 0, GST_MIKEY_SEC_PROTO_SRTP);
5855 /* only AES-CM is supported */
5857 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_ALG, 1, &byte);
5858 /* only HMAC-SHA1 */
5859 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_ALG, 1,
5861 /* we enable encryption on RTP and RTCP */
5862 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_ENC, 1,
5864 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTCP_ENC, 1,
5866 /* we enable authentication on RTP and RTCP */
5867 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_AUTH, 1,
5869 gst_mikey_message_add_payload (msg, payload);
5871 /* make unencrypted KEMAC */
5872 payload = gst_mikey_payload_new (GST_MIKEY_PT_KEMAC);
5873 gst_mikey_payload_kemac_set (payload, GST_MIKEY_ENC_NULL, GST_MIKEY_MAC_NULL);
5874 /* add the key in KEMAC */
5875 pkd = gst_mikey_payload_new (GST_MIKEY_PT_KEY_DATA);
5876 gst_mikey_payload_key_data_set_key (pkd, GST_MIKEY_KD_TEK, KEY_SIZE,
5878 gst_mikey_payload_kemac_add_sub (payload, pkd);
5879 gst_mikey_message_add_payload (msg, payload);
5881 /* now serialize this to bytes */
5882 bytes = gst_mikey_message_to_bytes (msg, NULL, NULL);
5883 gst_mikey_message_free (msg);
5884 /* and make it into base64 */
5885 data = g_bytes_get_data (bytes, &size);
5886 base64 = g_base64_encode (data, size);
5887 g_bytes_unref (bytes);
5889 result = g_strdup_printf ("prot=mikey;uri=\"%s\";data=\"%s\"",
5890 stream->conninfo.location, base64);
5897 /* Perform the SETUP request for all the streams.
5899 * We ask the server for a specific transport, which initially includes all the
5900 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
5901 * two local UDP ports that we send to the server.
5903 * Once the server replied with a transport, we configure the other streams
5904 * with the same transport.
5906 * This function will also configure the stream for the selected transport,
5907 * which basically means creating the pipeline.
5909 static GstRTSPResult
5910 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
5913 GstRTSPResult res = GST_RTSP_ERROR;
5914 GstRTSPMessage request = { 0 };
5915 GstRTSPMessage response = { 0 };
5916 GstRTSPStream *stream = NULL;
5917 GstRTSPLowerTrans protocols;
5918 GstRTSPStatusCode code;
5919 gboolean unsupported_real = FALSE;
5920 gint rtpport, rtcpport;
5924 if (src->conninfo.connection) {
5925 url = gst_rtsp_connection_get_url (src->conninfo.connection);
5926 /* we initially allow all configured lower transports. based on the URL
5927 * transports and the replies from the server we narrow them down. */
5928 protocols = url->transports & src->cur_protocols;
5931 protocols = src->cur_protocols;
5937 /* reset some state */
5938 src->free_channel = 0;
5939 src->interleaved = FALSE;
5940 src->need_activate = FALSE;
5941 /* keep track of next port number, 0 is random */
5942 src->next_port_num = src->client_port_range.min;
5943 rtpport = rtcpport = 0;
5945 if (G_UNLIKELY (src->streams == NULL))
5948 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5949 GstRTSPConnection *conn;
5956 stream = (GstRTSPStream *) walk->data;
5958 caps = stream_get_caps_for_pt (stream, stream->default_pt);
5960 GST_DEBUG_OBJECT (src, "skipping stream %p, no caps", stream);
5964 if (stream->skipped) {
5965 GST_DEBUG_OBJECT (src, "skipping stream %p", stream);
5969 /* see if we need to configure this stream */
5970 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
5971 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
5976 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
5977 stream->id, caps, &selected);
5979 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
5983 /* merge/overwrite global caps */
5988 s = gst_caps_get_structure (caps, 0);
5990 num = gst_structure_n_fields (src->props);
5991 for (j = 0; j < num; j++) {
5995 name = gst_structure_nth_field_name (src->props, j);
5996 val = gst_structure_get_value (src->props, name);
5997 gst_structure_set_value (s, name, val);
5999 GST_DEBUG_OBJECT (src, "copied %s", name);
6003 /* skip setup if we have no URL for it */
6004 if (stream->conninfo.location == NULL) {
6005 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
6009 if (src->conninfo.connection == NULL) {
6010 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
6011 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
6014 conn = stream->conninfo.connection;
6016 conn = src->conninfo.connection;
6018 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
6019 stream->conninfo.location);
6021 /* if we have a multicast connection, only suggest multicast from now on */
6022 if (stream->is_multicast)
6023 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
6026 /* first selectable protocol */
6027 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6029 if (!protocol_masks[mask])
6033 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
6034 protocol_masks[mask]);
6035 /* create a string with first transport in line */
6037 res = gst_rtspsrc_create_transports_string (src,
6038 protocols & protocol_masks[mask], stream->profile, &transports);
6039 if (res < 0 || transports == NULL)
6040 goto setup_transport_failed;
6042 if (strlen (transports) == 0) {
6043 g_free (transports);
6044 GST_DEBUG_OBJECT (src, "no transports found");
6049 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
6051 /* replace placeholders with real values, this function will optionally
6052 * allocate UDP ports and other info needed to execute the setup request */
6053 res = gst_rtspsrc_prepare_transports (stream, &transports,
6054 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
6056 g_free (transports);
6057 goto setup_transport_failed;
6060 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
6062 /* create SETUP request */
6064 gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
6065 stream->conninfo.location);
6067 g_free (transports);
6068 goto create_request_failed;
6071 /* select transport */
6072 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
6075 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
6076 stream->profile == GST_RTSP_PROFILE_SAVPF) {
6077 hval = gst_rtspsrc_stream_make_keymgmt (src, stream);
6078 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
6081 /* if the user wants a non default RTP packet size we add the blocksize
6083 if (src->rtp_blocksize > 0) {
6084 hval = g_strdup_printf ("%d", src->rtp_blocksize);
6085 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
6089 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
6092 /* handle the code ourselves */
6093 if ((res = gst_rtspsrc_send (src, conn, &request, &response, &code) < 0))
6097 case GST_RTSP_STS_OK:
6099 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
6100 gst_rtsp_message_unset (&request);
6101 gst_rtsp_message_unset (&response);
6102 /* cleanup of leftover transport */
6103 gst_rtspsrc_stream_free_udp (stream);
6104 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
6105 * we might be in this case */
6106 if (stream->container && rtpport && rtcpport && !retry) {
6107 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
6112 /* this transport did not go down well, but we may have others to try
6113 * that we did not send yet, try those and only give up then
6114 * but not without checking for lost cause/extension so we can
6115 * post a nicer/more useful error message later */
6116 if (!unsupported_real)
6117 unsupported_real = stream->is_real;
6118 /* select next available protocol, give up on this stream if none */
6120 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6122 if (!protocol_masks[mask] || unsupported_real)
6127 /* cleanup of leftover transport and move to the next stream */
6128 gst_rtspsrc_stream_free_udp (stream);
6129 goto response_error;
6132 /* parse response transport */
6134 gchar *resptrans = NULL;
6135 GstRTSPTransport transport = { 0 };
6137 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
6140 gst_rtspsrc_stream_free_udp (stream);
6144 /* parse transport, go to next stream on parse error */
6145 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
6146 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
6150 /* update allowed transports for other streams. once the transport of
6151 * one stream has been determined, we make sure that all other streams
6152 * are configured in the same way */
6153 switch (transport.lower_transport) {
6154 case GST_RTSP_LOWER_TRANS_TCP:
6155 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
6156 protocols = GST_RTSP_LOWER_TRANS_TCP;
6157 src->interleaved = TRUE;
6158 /* update free channels */
6160 MAX (transport.interleaved.min, src->free_channel);
6162 MAX (transport.interleaved.max, src->free_channel);
6163 src->free_channel++;
6165 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
6166 /* only allow multicast for other streams */
6167 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
6168 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
6169 /* if the server selected our ports, increment our counters so that
6170 * we select a new port later */
6171 if (src->next_port_num == transport.port.min &&
6172 src->next_port_num + 1 == transport.port.max) {
6173 src->next_port_num += 2;
6176 case GST_RTSP_LOWER_TRANS_UDP:
6177 /* only allow unicast for other streams */
6178 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
6179 protocols = GST_RTSP_LOWER_TRANS_UDP;
6182 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
6183 transport.lower_transport);
6187 if (!stream->container || (!src->interleaved && !retry)) {
6188 /* now configure the stream with the selected transport */
6189 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
6190 GST_DEBUG_OBJECT (src,
6191 "could not configure stream %p transport, skipping stream",
6194 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
6195 /* retain the first allocated UDP port pair */
6196 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
6197 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
6200 /* we need to activate at least one streams when we detect activity */
6201 src->need_activate = TRUE;
6203 /* stream is setup now */
6204 stream->setup = TRUE;
6209 GstRTSPStream *sskip;
6211 skip = g_list_next (skip);
6215 sskip = (GstRTSPStream *) skip->data;
6217 /* skip all streams with the same control url */
6218 if (g_str_equal (stream->conninfo.location, sskip->conninfo.location)) {
6219 GST_DEBUG_OBJECT (src, "found stream %p with same control %s",
6220 sskip, sskip->conninfo.location);
6221 sskip->skipped = TRUE;
6226 /* clean up our transport struct */
6227 gst_rtsp_transport_init (&transport);
6228 /* clean up used RTSP messages */
6229 gst_rtsp_message_unset (&request);
6230 gst_rtsp_message_unset (&response);
6234 /* store the transport protocol that was configured */
6235 src->cur_protocols = protocols;
6237 gst_rtsp_ext_list_stream_select (src->extensions, url);
6239 /* if there is nothing to activate, error out */
6240 if (!src->need_activate)
6241 goto nothing_to_activate;
6248 /* no transport possible, post an error and stop */
6249 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6250 ("Could not connect to server, no protocols left"));
6251 return GST_RTSP_ERROR;
6255 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6256 ("SDP contains no streams"));
6257 return GST_RTSP_ERROR;
6259 create_request_failed:
6261 gchar *str = gst_rtsp_strresult (res);
6263 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6264 ("Could not create request. (%s)", str));
6268 setup_transport_failed:
6270 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6271 ("Could not setup transport."));
6272 res = GST_RTSP_ERROR;
6277 const gchar *str = gst_rtsp_status_as_text (code);
6279 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6280 ("Error (%d): %s", code, GST_STR_NULL (str)));
6281 res = GST_RTSP_ERROR;
6286 gchar *str = gst_rtsp_strresult (res);
6288 if (res != GST_RTSP_EINTR) {
6289 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6290 ("Could not send message. (%s)", str));
6292 GST_WARNING_OBJECT (src, "send interrupted");
6299 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6300 ("Server did not select transport."));
6301 res = GST_RTSP_ERROR;
6304 nothing_to_activate:
6306 /* none of the available error codes is really right .. */
6307 if (unsupported_real) {
6308 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6309 (_("No supported stream was found. You might need to install a "
6310 "GStreamer RTSP extension plugin for Real media streams.")),
6313 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6314 (_("No supported stream was found. You might need to allow "
6315 "more transport protocols or may otherwise be missing "
6316 "the right GStreamer RTSP extension plugin.")), (NULL));
6318 return GST_RTSP_ERROR;
6322 gst_rtsp_message_unset (&request);
6323 gst_rtsp_message_unset (&response);
6329 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
6330 GstSegment * segment)
6333 GstRTSPTimeRange *therange;
6336 gst_rtsp_range_free (src->range);
6338 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
6339 GST_DEBUG_OBJECT (src, "parsed range %s", range);
6340 src->range = therange;
6342 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
6344 gst_segment_init (segment, GST_FORMAT_TIME);
6348 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
6349 therange->min.type, therange->min.seconds, therange->max.type,
6350 therange->max.seconds);
6352 if (therange->min.type == GST_RTSP_TIME_NOW)
6354 else if (therange->min.type == GST_RTSP_TIME_END)
6357 seconds = therange->min.seconds * GST_SECOND;
6359 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
6360 GST_TIME_ARGS (seconds));
6362 /* we need to start playback without clipping from the position reported by
6364 segment->start = seconds;
6365 segment->position = seconds;
6367 if (therange->max.type == GST_RTSP_TIME_NOW)
6369 else if (therange->max.type == GST_RTSP_TIME_END)
6372 seconds = therange->max.seconds * GST_SECOND;
6374 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
6375 GST_TIME_ARGS (seconds));
6377 /* live (WMS) server might send overflowed large max as its idea of infinity,
6378 * compensate to prevent problems later on */
6379 if (seconds != -1 && seconds < 0) {
6381 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
6384 /* live (WMS) might send min == max, which is not worth recording */
6385 if (segment->duration == -1 && seconds == segment->start)
6388 /* don't change duration with unknown value, we might have a valid value
6389 * there that we want to keep. */
6391 segment->duration = seconds;
6396 /* Parse clock profived by the server with following syntax:
6398 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
6401 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
6403 gboolean res = FALSE;
6405 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
6406 gchar **fields = NULL, **parts = NULL;
6407 gchar *remote_ip, *str;
6409 GstClockTime base_time;
6412 fields = g_strsplit (gstclock, " ", 0);
6414 /* wrapped clock, not very interesting for now */
6415 if (fields[1] == NULL)
6418 /* remote IP address and port */
6419 if ((str = fields[2]) == NULL)
6422 parts = g_strsplit (str, ":", 0);
6424 if ((remote_ip = parts[0]) == NULL)
6427 if ((str = parts[1]) == NULL)
6435 if ((str = fields[3]) == NULL)
6438 base_time = g_ascii_strtoull (str, NULL, 10);
6441 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
6444 if (src->provided_clock)
6445 gst_object_unref (src->provided_clock);
6446 src->provided_clock = netclock;
6448 gst_element_post_message (GST_ELEMENT_CAST (src),
6449 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
6450 src->provided_clock, TRUE));
6454 g_strfreev (fields);
6460 /* must be called with the RTSP state lock */
6461 static GstRTSPResult
6462 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
6468 /* prepare global stream caps properties */
6470 gst_structure_remove_all_fields (src->props);
6472 src->props = gst_structure_new_empty ("RTSPProperties");
6475 gst_sdp_message_dump (sdp);
6477 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
6479 /* let the app inspect and change the SDP */
6480 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
6482 gst_segment_init (&src->segment, GST_FORMAT_TIME);
6484 /* parse range for duration reporting. */
6489 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
6493 /* keep track of the range and configure it in the segment */
6494 if (gst_rtspsrc_parse_range (src, range, &src->segment))
6498 /* parse clock information. This is GStreamer specific, a server can tell the
6499 * client what clock it is using and wrap that in a network clock. The
6500 * advantage of that is that we can slave to it. */
6502 const gchar *gstclock;
6505 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
6506 if (gstclock == NULL)
6509 /* parse the clock and expose it in the provide_clock method */
6510 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
6514 /* try to find a global control attribute. Note that a '*' means that we should
6515 * do aggregate control with the current url (so we don't do anything and
6516 * leave the current connection as is) */
6518 const gchar *control;
6521 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
6522 if (control == NULL)
6525 /* only take fully qualified urls */
6526 if (g_str_has_prefix (control, "rtsp://"))
6530 g_free (src->conninfo.location);
6531 src->conninfo.location = g_strdup (control);
6532 /* make a connection for this, if there was a connection already, nothing
6534 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
6535 GST_ERROR_OBJECT (src, "could not connect");
6538 /* we need to keep the control url separate from the connection url because
6539 * the rules for constructing the media control url need it */
6540 g_free (src->control);
6541 src->control = g_strdup (control);
6544 /* create streams */
6545 n_streams = gst_sdp_message_medias_len (sdp);
6546 for (i = 0; i < n_streams; i++) {
6547 gst_rtspsrc_create_stream (src, sdp, i);
6550 src->state = GST_RTSP_STATE_INIT;
6553 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
6556 /* reset our state */
6557 src->need_range = TRUE;
6560 src->state = GST_RTSP_STATE_READY;
6567 GST_ERROR_OBJECT (src, "setup failed");
6568 gst_rtspsrc_cleanup (src);
6573 static GstRTSPResult
6574 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
6578 GstRTSPMessage request = { 0 };
6579 GstRTSPMessage response = { 0 };
6582 gchar *respcont = NULL;
6585 src->need_redirect = FALSE;
6587 /* can't continue without a valid url */
6588 if (G_UNLIKELY (src->conninfo.url == NULL)) {
6589 res = GST_RTSP_EINVAL;
6592 src->tried_url_auth = FALSE;
6594 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
6595 goto connect_failed;
6597 /* create OPTIONS */
6598 GST_DEBUG_OBJECT (src, "create options...");
6600 gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
6601 src->conninfo.url_str);
6603 goto create_request_failed;
6606 GST_DEBUG_OBJECT (src, "send options...");
6609 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
6612 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6617 if (!gst_rtspsrc_parse_methods (src, &response))
6620 /* create DESCRIBE */
6621 GST_DEBUG_OBJECT (src, "create describe...");
6623 gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
6624 src->conninfo.url_str);
6626 goto create_request_failed;
6628 /* we only accept SDP for now */
6629 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
6633 GST_DEBUG_OBJECT (src, "send describe...");
6636 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
6639 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6643 /* we only perform redirect for the describe, currently */
6644 if (src->need_redirect) {
6645 /* close connection, we don't have to send a TEARDOWN yet, ignore the
6647 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6649 gst_rtsp_message_unset (&request);
6650 gst_rtsp_message_unset (&response);
6656 /* it could be that the DESCRIBE method was not implemented */
6657 if (!src->methods & GST_RTSP_DESCRIBE)
6660 /* check if reply is SDP */
6661 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
6663 /* could not be set but since the request returned OK, we assume it
6664 * was SDP, else check it. */
6666 if (!g_ascii_strcasecmp (respcont, "application/sdp") == 0)
6667 goto wrong_content_type;
6670 /* get message body and parse as SDP */
6671 gst_rtsp_message_get_body (&response, &data, &size);
6672 if (data == NULL || size == 0)
6675 GST_DEBUG_OBJECT (src, "parse SDP...");
6676 gst_sdp_message_new (sdp);
6677 gst_sdp_message_parse_buffer (data, size, *sdp);
6679 /* clean up any messages */
6680 gst_rtsp_message_unset (&request);
6681 gst_rtsp_message_unset (&response);
6688 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
6689 ("No valid RTSP URL was provided"));
6694 gchar *str = gst_rtsp_strresult (res);
6696 if (res != GST_RTSP_EINTR) {
6697 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
6698 ("Failed to connect. (%s)", str));
6700 GST_WARNING_OBJECT (src, "connect interrupted");
6705 create_request_failed:
6707 gchar *str = gst_rtsp_strresult (res);
6709 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6710 ("Could not create request. (%s)", str));
6716 /* Don't post a message - the rtsp_send method will have
6717 * taken care of it because we passed NULL for the response code */
6722 /* error was posted */
6723 res = GST_RTSP_ERROR;
6728 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6729 ("Server does not support SDP, got %s.", respcont));
6730 res = GST_RTSP_ERROR;
6735 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6736 ("Server can not provide an SDP."));
6737 res = GST_RTSP_ERROR;
6742 if (src->conninfo.connection) {
6743 GST_DEBUG_OBJECT (src, "free connection");
6744 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6746 gst_rtsp_message_unset (&request);
6747 gst_rtsp_message_unset (&response);
6752 static GstRTSPResult
6753 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
6758 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
6760 if (src->sdp == NULL) {
6761 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
6765 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
6770 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
6777 GST_WARNING_OBJECT (src, "can't get sdp");
6778 src->open_error = TRUE;
6783 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
6784 src->open_error = TRUE;
6789 static GstRTSPResult
6790 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
6792 GstRTSPMessage request = { 0 };
6793 GstRTSPMessage response = { 0 };
6794 GstRTSPResult res = GST_RTSP_OK;
6796 const gchar *control;
6798 GST_DEBUG_OBJECT (src, "TEARDOWN...");
6800 gst_rtspsrc_set_state (src, GST_STATE_READY);
6802 if (src->state < GST_RTSP_STATE_READY) {
6803 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
6810 /* construct a control url */
6811 control = get_aggregate_control (src);
6813 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
6816 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6817 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6818 const gchar *setup_url;
6819 GstRTSPConnInfo *info;
6821 /* try aggregate control first but do non-aggregate control otherwise */
6823 setup_url = control;
6824 else if ((setup_url = stream->conninfo.location) == NULL)
6827 if (src->conninfo.connection) {
6828 info = &src->conninfo;
6829 } else if (stream->conninfo.connection) {
6830 info = &stream->conninfo;
6834 if (!info->connected)
6839 gst_rtsp_message_init_request (&request, GST_RTSP_TEARDOWN, setup_url);
6841 goto create_request_failed;
6844 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
6847 gst_rtspsrc_send (src, info->connection, &request, &response,
6851 /* FIXME, parse result? */
6852 gst_rtsp_message_unset (&request);
6853 gst_rtsp_message_unset (&response);
6856 /* early exit when we did aggregate control */
6862 /* close connections */
6863 GST_DEBUG_OBJECT (src, "closing connection...");
6864 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6865 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6866 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6867 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
6871 gst_rtspsrc_cleanup (src);
6873 src->state = GST_RTSP_STATE_INVALID;
6876 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
6881 create_request_failed:
6883 gchar *str = gst_rtsp_strresult (res);
6885 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6886 ("Could not create request. (%s)", str));
6892 gchar *str = gst_rtsp_strresult (res);
6894 gst_rtsp_message_unset (&request);
6895 if (res != GST_RTSP_EINTR) {
6896 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6897 ("Could not send message. (%s)", str));
6899 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
6906 GST_DEBUG_OBJECT (src,
6907 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
6912 /* RTP-Info is of the format:
6914 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
6916 * rtptime corresponds to the timestamp for the NPT time given in the header
6917 * seqbase corresponds to the next sequence number we received. This number
6918 * indicates the first seqnum after the seek and should be used to discard
6919 * packets that are from before the seek.
6922 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
6927 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
6929 infos = g_strsplit (rtpinfo, ",", 0);
6930 for (i = 0; infos[i]; i++) {
6932 GstRTSPStream *stream;
6936 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
6938 /* init values, types of seqbase and timebase are bigger than needed so we
6939 * can store -1 as uninitialized values */
6944 /* parse url, find stream for url.
6945 * parse seq and rtptime. The seq number should be configured in the rtp
6946 * depayloader or session manager to detect gaps. Same for the rtptime, it
6947 * should be used to create an initial time newsegment. */
6948 fields = g_strsplit (infos[i], ";", 0);
6949 for (j = 0; fields[j]; j++) {
6950 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
6951 /* remove leading whitespace */
6952 fields[j] = g_strchug (fields[j]);
6953 if (g_str_has_prefix (fields[j], "url=")) {
6954 /* get the url and the stream */
6956 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
6957 } else if (g_str_has_prefix (fields[j], "seq=")) {
6958 seqbase = atoi (fields[j] + 4);
6959 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
6960 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
6963 g_strfreev (fields);
6964 /* now we need to store the values for the caps of the stream */
6965 if (stream != NULL) {
6966 GST_DEBUG_OBJECT (src,
6967 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
6968 stream, seqbase, timebase);
6970 /* we have a stream, configure detected params */
6971 stream->seqbase = seqbase;
6972 stream->timebase = timebase;
6981 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
6986 interval = strtoul (rtcp, NULL, 10);
6987 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
6992 interval *= GST_MSECOND;
6994 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6995 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6997 /* already (optionally) retrieved this when configuring manager */
6998 if (stream->session) {
6999 GObject *rtpsession = stream->session;
7001 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
7003 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
7007 /* now it happens that (Xenon) server sending this may also provide bogus
7008 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
7009 * and just use RTP-Info to sync */
7011 GObjectClass *klass;
7013 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
7014 if (g_object_class_find_property (klass, "rtcp-sync")) {
7015 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
7016 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
7022 gst_rtspsrc_get_float (const gchar * dstr)
7024 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7026 /* canonicalise floating point string so we can handle float strings
7027 * in the form "24.930" or "24,930" irrespective of the current locale */
7028 g_strlcpy (s, dstr, sizeof (s));
7029 g_strdelimit (s, ",", '.');
7030 return g_ascii_strtod (s, NULL);
7034 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
7036 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7038 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
7039 g_strlcpy (val_str, "now", sizeof (val_str));
7041 if (segment->position == 0) {
7042 g_strlcpy (val_str, "0", sizeof (val_str));
7044 g_ascii_dtostr (val_str, sizeof (val_str),
7045 ((gdouble) segment->position) / GST_SECOND);
7048 return g_strdup_printf ("npt=%s-", val_str);
7052 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
7056 stream->timebase = -1;
7057 stream->seqbase = -1;
7059 len = stream->ptmap->len;
7060 for (i = 0; i < len; i++) {
7061 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
7064 if (item->caps == NULL)
7067 item->caps = gst_caps_make_writable (item->caps);
7068 s = gst_caps_get_structure (item->caps, 0);
7069 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
7073 static GstRTSPResult
7074 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
7076 GstRTSPResult res = GST_RTSP_OK;
7078 if (src->state < GST_RTSP_STATE_READY) {
7079 res = GST_RTSP_ERROR;
7080 if (src->open_error) {
7081 GST_DEBUG_OBJECT (src, "the stream was in error");
7085 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
7087 if ((res = gst_rtspsrc_open (src, async)) < 0) {
7088 GST_DEBUG_OBJECT (src, "failed to open stream");
7097 static GstRTSPResult
7098 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
7100 GstRTSPMessage request = { 0 };
7101 GstRTSPMessage response = { 0 };
7102 GstRTSPResult res = GST_RTSP_OK;
7106 const gchar *control;
7108 GST_DEBUG_OBJECT (src, "PLAY...");
7110 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7113 if (!(src->methods & GST_RTSP_PLAY))
7116 if (src->state == GST_RTSP_STATE_PLAYING)
7119 if (!src->conninfo.connection || !src->conninfo.connected)
7122 /* send some dummy packets before we activate the receive in the
7124 gst_rtspsrc_send_dummy_packets (src);
7126 /* require new SR packets */
7128 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
7130 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
7132 /* construct a control url */
7133 control = get_aggregate_control (src);
7135 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7136 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7137 const gchar *setup_url;
7138 GstRTSPConnection *conn;
7140 /* try aggregate control first but do non-aggregate control otherwise */
7142 setup_url = control;
7143 else if ((setup_url = stream->conninfo.location) == NULL)
7146 if (src->conninfo.connection) {
7147 conn = src->conninfo.connection;
7148 } else if (stream->conninfo.connection) {
7149 conn = stream->conninfo.connection;
7155 res = gst_rtsp_message_init_request (&request, GST_RTSP_PLAY, setup_url);
7157 goto create_request_failed;
7159 if (src->need_range) {
7160 hval = gen_range_header (src, segment);
7162 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
7164 /* store the newsegment event so it can be sent from the streaming thread. */
7165 if (src->start_segment)
7166 gst_event_unref (src->start_segment);
7167 src->start_segment = gst_event_new_segment (&src->segment);
7170 if (segment->rate != 1.0) {
7171 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
7173 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
7175 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
7177 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
7181 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
7183 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7186 /* seek may have silently failed as it is not supported */
7187 if (!(src->methods & GST_RTSP_PLAY)) {
7188 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
7189 /* obviously it is supported as we made it here */
7190 src->methods |= GST_RTSP_PLAY;
7191 src->seekable = FALSE;
7192 /* but there is nothing to parse in the response,
7193 * so convey we have no idea and not to expect anything particular */
7194 clear_rtp_base (src, stream);
7198 /* need to do for all streams */
7199 for (run = src->streams; run; run = g_list_next (run))
7200 clear_rtp_base (src, (GstRTSPStream *) run->data);
7202 /* NOTE the above also disables npt based eos detection */
7203 /* and below forces position to 0,
7204 * which is visible feedback we lost the plot */
7205 segment->start = segment->position = src->last_pos;
7208 gst_rtsp_message_unset (&request);
7210 /* parse RTP npt field. This is the current position in the stream (Normal
7211 * Play Time) and should be put in the NEWSEGMENT position field. */
7212 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
7214 gst_rtspsrc_parse_range (src, hval, segment);
7216 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
7217 segment->rate = 1.0;
7219 /* parse Speed header. This is the intended playback rate of the stream
7220 * and should be put in the NEWSEGMENT rate field. */
7221 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
7222 0) == GST_RTSP_OK) {
7223 segment->rate = gst_rtspsrc_get_float (hval);
7224 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
7225 &hval, 0) == GST_RTSP_OK) {
7226 segment->rate = gst_rtspsrc_get_float (hval);
7229 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
7230 * for the RTP packets. If this is not present, we assume all starts from 0...
7231 * This is info for the RTP session manager that we pass to it in caps. */
7233 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
7234 &hval, hval_idx++) == GST_RTSP_OK)
7235 gst_rtspsrc_parse_rtpinfo (src, hval);
7237 /* some servers indicate RTCP parameters in PLAY response,
7238 * rather than properly in SDP */
7239 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
7240 &hval, 0) == GST_RTSP_OK)
7241 gst_rtspsrc_handle_rtcp_interval (src, hval);
7243 gst_rtsp_message_unset (&response);
7245 /* early exit when we did aggregate control */
7249 /* configure the caps of the streams after we parsed all headers. Only reset
7250 * the manager object when we set a new Range header (we did a seek) */
7251 gst_rtspsrc_configure_caps (src, segment, src->need_range);
7253 /* set again when needed */
7254 src->need_range = FALSE;
7256 src->running = TRUE;
7257 src->base_time = -1;
7258 src->state = GST_RTSP_STATE_PLAYING;
7261 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
7262 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7263 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7264 stream->discont = TRUE;
7269 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
7276 GST_DEBUG_OBJECT (src, "failed to open stream");
7281 GST_DEBUG_OBJECT (src, "PLAY is not supported");
7286 GST_DEBUG_OBJECT (src, "we were already PLAYING");
7289 create_request_failed:
7291 gchar *str = gst_rtsp_strresult (res);
7293 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7294 ("Could not create request. (%s)", str));
7300 gchar *str = gst_rtsp_strresult (res);
7302 gst_rtsp_message_unset (&request);
7303 if (res != GST_RTSP_EINTR) {
7304 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7305 ("Could not send message. (%s)", str));
7307 GST_WARNING_OBJECT (src, "PLAY interrupted");
7314 static GstRTSPResult
7315 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
7317 GstRTSPResult res = GST_RTSP_OK;
7318 GstRTSPMessage request = { 0 };
7319 GstRTSPMessage response = { 0 };
7321 const gchar *control;
7323 GST_DEBUG_OBJECT (src, "PAUSE...");
7325 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7328 if (!(src->methods & GST_RTSP_PAUSE))
7331 if (src->state == GST_RTSP_STATE_READY)
7334 if (!src->conninfo.connection || !src->conninfo.connected)
7337 /* construct a control url */
7338 control = get_aggregate_control (src);
7340 /* loop over the streams. We might exit the loop early when we could do an
7341 * aggregate control */
7342 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7343 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7344 GstRTSPConnection *conn;
7345 const gchar *setup_url;
7347 /* try aggregate control first but do non-aggregate control otherwise */
7349 setup_url = control;
7350 else if ((setup_url = stream->conninfo.location) == NULL)
7353 if (src->conninfo.connection) {
7354 conn = src->conninfo.connection;
7355 } else if (stream->conninfo.connection) {
7356 conn = stream->conninfo.connection;
7362 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
7363 ("Sending PAUSE request"));
7366 gst_rtsp_message_init_request (&request, GST_RTSP_PAUSE,
7368 goto create_request_failed;
7370 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7373 gst_rtsp_message_unset (&request);
7374 gst_rtsp_message_unset (&response);
7376 /* exit early when we did agregate control */
7381 /* change element states now */
7382 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
7385 src->state = GST_RTSP_STATE_READY;
7389 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
7396 GST_DEBUG_OBJECT (src, "failed to open stream");
7401 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
7406 GST_DEBUG_OBJECT (src, "we were already PAUSED");
7409 create_request_failed:
7411 gchar *str = gst_rtsp_strresult (res);
7413 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7414 ("Could not create request. (%s)", str));
7420 gchar *str = gst_rtsp_strresult (res);
7422 gst_rtsp_message_unset (&request);
7423 if (res != GST_RTSP_EINTR) {
7424 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7425 ("Could not send message. (%s)", str));
7427 GST_WARNING_OBJECT (src, "PAUSE interrupted");
7435 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
7437 GstRTSPSrc *rtspsrc;
7439 rtspsrc = GST_RTSPSRC (bin);
7441 switch (GST_MESSAGE_TYPE (message)) {
7442 case GST_MESSAGE_EOS:
7443 gst_message_unref (message);
7445 case GST_MESSAGE_ELEMENT:
7447 const GstStructure *s = gst_message_get_structure (message);
7449 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
7450 gboolean ignore_timeout;
7452 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
7454 GST_OBJECT_LOCK (rtspsrc);
7455 ignore_timeout = rtspsrc->ignore_timeout;
7456 rtspsrc->ignore_timeout = TRUE;
7457 GST_OBJECT_UNLOCK (rtspsrc);
7459 /* we only act on the first udp timeout message, others are irrelevant
7460 * and can be ignored. */
7461 if (!ignore_timeout)
7462 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
7464 gst_message_unref (message);
7467 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7470 case GST_MESSAGE_ERROR:
7473 GstRTSPStream *stream;
7476 udpsrc = GST_MESSAGE_SRC (message);
7478 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
7479 GST_ELEMENT_NAME (udpsrc));
7481 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
7485 /* we ignore the RTCP udpsrc */
7486 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
7489 /* if we get error messages from the udp sources, that's not a problem as
7490 * long as not all of them error out. We also don't really know what the
7491 * problem is, the message does not give enough detail... */
7492 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
7493 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
7494 if (ret != GST_FLOW_OK)
7498 gst_message_unref (message);
7502 /* fatal but not our message, forward */
7503 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7508 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7514 /* the thread where everything happens */
7516 gst_rtspsrc_thread (GstRTSPSrc * src)
7520 GST_OBJECT_LOCK (src);
7521 cmd = src->pending_cmd;
7522 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
7523 || cmd == CMD_LOOP || cmd == CMD_OPEN)
7524 src->pending_cmd = CMD_LOOP;
7526 src->pending_cmd = CMD_WAIT;
7527 GST_DEBUG_OBJECT (src, "got command %d", cmd);
7529 /* we got the message command, so ensure communication is possible again */
7530 gst_rtspsrc_connection_flush (src, FALSE);
7532 src->busy_cmd = cmd;
7533 GST_OBJECT_UNLOCK (src);
7537 gst_rtspsrc_open (src, TRUE);
7540 gst_rtspsrc_play (src, &src->segment, TRUE);
7543 gst_rtspsrc_pause (src, TRUE);
7546 gst_rtspsrc_close (src, TRUE, FALSE);
7549 gst_rtspsrc_loop (src);
7552 gst_rtspsrc_reconnect (src, FALSE);
7558 GST_OBJECT_LOCK (src);
7559 /* and go back to sleep */
7560 if (src->pending_cmd == CMD_WAIT) {
7562 gst_task_pause (src->task);
7565 src->busy_cmd = CMD_WAIT;
7566 GST_OBJECT_UNLOCK (src);
7570 gst_rtspsrc_start (GstRTSPSrc * src)
7572 GST_DEBUG_OBJECT (src, "starting");
7574 GST_OBJECT_LOCK (src);
7576 src->pending_cmd = CMD_WAIT;
7578 if (src->task == NULL) {
7579 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
7580 if (src->task == NULL)
7583 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
7585 GST_OBJECT_UNLOCK (src);
7592 GST_OBJECT_UNLOCK (src);
7593 GST_ERROR_OBJECT (src, "failed to create task");
7599 gst_rtspsrc_stop (GstRTSPSrc * src)
7603 GST_DEBUG_OBJECT (src, "stopping");
7605 /* also cancels pending task */
7606 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
7608 GST_OBJECT_LOCK (src);
7609 if ((task = src->task)) {
7611 GST_OBJECT_UNLOCK (src);
7613 gst_task_stop (task);
7615 /* make sure it is not running */
7616 GST_RTSP_STREAM_LOCK (src);
7617 GST_RTSP_STREAM_UNLOCK (src);
7619 /* now wait for the task to finish */
7620 gst_task_join (task);
7622 /* and free the task */
7623 gst_object_unref (GST_OBJECT (task));
7625 GST_OBJECT_LOCK (src);
7627 GST_OBJECT_UNLOCK (src);
7629 /* ensure synchronously all is closed and clean */
7630 gst_rtspsrc_close (src, FALSE, TRUE);
7635 static GstStateChangeReturn
7636 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
7638 GstRTSPSrc *rtspsrc;
7639 GstStateChangeReturn ret;
7641 rtspsrc = GST_RTSPSRC (element);
7643 switch (transition) {
7644 case GST_STATE_CHANGE_NULL_TO_READY:
7645 if (!gst_rtspsrc_start (rtspsrc))
7648 case GST_STATE_CHANGE_READY_TO_PAUSED:
7649 /* init some state */
7650 rtspsrc->cur_protocols = rtspsrc->protocols;
7651 /* first attempt, don't ignore timeouts */
7652 rtspsrc->ignore_timeout = FALSE;
7653 rtspsrc->open_error = FALSE;
7654 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
7656 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7657 set_manager_buffer_mode (rtspsrc);
7659 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7660 /* unblock the tcp tasks and make the loop waiting */
7661 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
7662 /* make sure it is waiting before we send PAUSE or PLAY below */
7663 GST_RTSP_STREAM_LOCK (rtspsrc);
7664 GST_RTSP_STREAM_UNLOCK (rtspsrc);
7667 case GST_STATE_CHANGE_PAUSED_TO_READY:
7673 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
7674 if (ret == GST_STATE_CHANGE_FAILURE)
7677 switch (transition) {
7678 case GST_STATE_CHANGE_NULL_TO_READY:
7679 ret = GST_STATE_CHANGE_SUCCESS;
7681 case GST_STATE_CHANGE_READY_TO_PAUSED:
7682 ret = GST_STATE_CHANGE_NO_PREROLL;
7684 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7685 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
7686 ret = GST_STATE_CHANGE_SUCCESS;
7688 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7689 /* send pause request and keep the idle task around */
7690 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
7691 ret = GST_STATE_CHANGE_NO_PREROLL;
7693 case GST_STATE_CHANGE_PAUSED_TO_READY:
7694 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_PAUSE);
7695 ret = GST_STATE_CHANGE_SUCCESS;
7697 case GST_STATE_CHANGE_READY_TO_NULL:
7698 gst_rtspsrc_stop (rtspsrc);
7699 ret = GST_STATE_CHANGE_SUCCESS;
7710 GST_DEBUG_OBJECT (rtspsrc, "start failed");
7711 return GST_STATE_CHANGE_FAILURE;
7716 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
7719 GstRTSPSrc *rtspsrc;
7721 rtspsrc = GST_RTSPSRC (element);
7723 if (GST_EVENT_IS_DOWNSTREAM (event)) {
7724 res = gst_rtspsrc_push_event (rtspsrc, event);
7726 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
7733 /*** GSTURIHANDLER INTERFACE *************************************************/
7736 gst_rtspsrc_uri_get_type (GType type)
7741 static const gchar *const *
7742 gst_rtspsrc_uri_get_protocols (GType type)
7744 static const gchar *protocols[] =
7745 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
7746 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
7753 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
7755 GstRTSPSrc *src = GST_RTSPSRC (handler);
7757 /* FIXME: make thread-safe */
7758 return g_strdup (src->conninfo.location);
7762 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
7767 GstRTSPUrl *newurl = NULL;
7768 GstSDPMessage *sdp = NULL;
7770 src = GST_RTSPSRC (handler);
7772 /* same URI, we're fine */
7773 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
7776 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
7777 if ((res = gst_sdp_message_new (&sdp) < 0))
7780 GST_DEBUG_OBJECT (src, "parsing SDP message");
7781 if ((res = gst_sdp_message_parse_uri (uri, sdp) < 0))
7785 GST_DEBUG_OBJECT (src, "parsing URI");
7786 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
7790 /* if worked, free previous and store new url object along with the original
7792 GST_DEBUG_OBJECT (src, "configuring URI");
7793 g_free (src->conninfo.location);
7794 src->conninfo.location = g_strdup (uri);
7795 gst_rtsp_url_free (src->conninfo.url);
7796 src->conninfo.url = newurl;
7797 g_free (src->conninfo.url_str);
7799 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
7801 src->conninfo.url_str = NULL;
7804 gst_sdp_message_free (src->sdp);
7806 src->from_sdp = sdp != NULL;
7808 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
7809 GST_DEBUG_OBJECT (src, "request uri is: %s",
7810 GST_STR_NULL (src->conninfo.url_str));
7817 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
7822 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", res);
7823 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7824 "Could not create SDP");
7829 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", res,
7830 GST_STR_NULL (uri));
7831 gst_sdp_message_free (sdp);
7832 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7838 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
7839 GST_STR_NULL (uri), res);
7840 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7841 "Invalid RTSP URI");
7847 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
7849 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
7851 iface->get_type = gst_rtspsrc_uri_get_type;
7852 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
7853 iface->get_uri = gst_rtspsrc_uri_get_uri;
7854 iface->set_uri = gst_rtspsrc_uri_set_uri;