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
84 #endif /* HAVE_UNISTD_H */
90 #include <gst/net/gstnet.h>
91 #include <gst/sdp/gstsdpmessage.h>
92 #include <gst/sdp/gstmikey.h>
93 #include <gst/rtp/gstrtppayloads.h>
95 #include "gst/gst-i18n-plugin.h"
97 #include "gstrtspsrc.h"
99 GST_DEBUG_CATEGORY_STATIC (rtspsrc_debug);
100 #define GST_CAT_DEFAULT (rtspsrc_debug)
102 static GstStaticPadTemplate rtptemplate = GST_STATIC_PAD_TEMPLATE ("stream_%u",
105 GST_STATIC_CAPS ("application/x-rtp; application/x-rdt"));
107 /* templates used internally */
108 static GstStaticPadTemplate anysrctemplate =
109 GST_STATIC_PAD_TEMPLATE ("internalsrc_%u",
112 GST_STATIC_CAPS_ANY);
114 static GstStaticPadTemplate anysinktemplate =
115 GST_STATIC_PAD_TEMPLATE ("internalsink_%u",
118 GST_STATIC_CAPS_ANY);
122 SIGNAL_HANDLE_REQUEST,
124 SIGNAL_SELECT_STREAM,
129 enum _GstRtspSrcRtcpSyncMode
136 enum _GstRtspSrcBufferMode
145 #define GST_TYPE_RTSP_SRC_BUFFER_MODE (gst_rtsp_src_buffer_mode_get_type())
147 gst_rtsp_src_buffer_mode_get_type (void)
149 static GType buffer_mode_type = 0;
150 static const GEnumValue buffer_modes[] = {
151 {BUFFER_MODE_NONE, "Only use RTP timestamps", "none"},
152 {BUFFER_MODE_SLAVE, "Slave receiver to sender clock", "slave"},
153 {BUFFER_MODE_BUFFER, "Do low/high watermark buffering", "buffer"},
154 {BUFFER_MODE_AUTO, "Choose mode depending on stream live", "auto"},
155 {BUFFER_MODE_SYNCED, "Synchronized sender and receiver clocks", "synced"},
159 if (!buffer_mode_type) {
161 g_enum_register_static ("GstRTSPSrcBufferMode", buffer_modes);
163 return buffer_mode_type;
166 #define DEFAULT_LOCATION NULL
167 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
168 #define DEFAULT_DEBUG FALSE
169 #define DEFAULT_RETRY 20
170 #define DEFAULT_TIMEOUT 5000000
171 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
172 #define DEFAULT_TCP_TIMEOUT 20000000
173 #define DEFAULT_LATENCY_MS 2000
174 #define DEFAULT_DROP_ON_LATENCY FALSE
175 #define DEFAULT_CONNECTION_SPEED 0
176 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
177 #define DEFAULT_DO_RTCP TRUE
178 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
179 #define DEFAULT_PROXY NULL
180 #define DEFAULT_RTP_BLOCKSIZE 0
181 #define DEFAULT_USER_ID NULL
182 #define DEFAULT_USER_PW NULL
183 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
184 #define DEFAULT_PORT_RANGE NULL
185 #define DEFAULT_SHORT_HEADER FALSE
186 #define DEFAULT_PROBATION 2
187 #define DEFAULT_UDP_RECONNECT TRUE
188 #define DEFAULT_MULTICAST_IFACE NULL
189 #define DEFAULT_NTP_SYNC FALSE
190 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
191 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
192 #define DEFAULT_TLS_DATABASE NULL
204 PROP_DROP_ON_LATENCY,
205 PROP_CONNECTION_SPEED,
208 PROP_DO_RTSP_KEEP_ALIVE,
217 PROP_UDP_BUFFER_SIZE,
221 PROP_MULTICAST_IFACE,
223 PROP_USE_PIPELINE_CLOCK,
225 PROP_TLS_VALIDATION_FLAGS,
230 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
232 gst_rtsp_nat_method_get_type (void)
234 static GType rtsp_nat_method_type = 0;
235 static const GEnumValue rtsp_nat_method[] = {
236 {GST_RTSP_NAT_NONE, "None", "none"},
237 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
241 if (!rtsp_nat_method_type) {
242 rtsp_nat_method_type =
243 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
245 return rtsp_nat_method_type;
248 static void gst_rtspsrc_finalize (GObject * object);
250 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
251 const GValue * value, GParamSpec * pspec);
252 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
253 GValue * value, GParamSpec * pspec);
255 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
257 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
258 gpointer iface_data);
260 static void gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes,
263 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
264 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
266 static GstCaps *gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media);
268 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
269 GstStateChange transition);
270 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
271 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
273 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
274 GstRTSPMessage * response);
276 static gboolean gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
278 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
279 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
281 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
282 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
284 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
285 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
286 gboolean only_close);
288 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
289 const gchar * uri, GError ** error);
290 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
292 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
293 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
294 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
295 GstRTSPStream * stream, GstEvent * event);
296 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
297 static void gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush);
305 /* commands we send to out loop to notify it of events */
306 #define CMD_OPEN (1 << 0)
307 #define CMD_PLAY (1 << 1)
308 #define CMD_PAUSE (1 << 2)
309 #define CMD_CLOSE (1 << 3)
310 #define CMD_WAIT (1 << 4)
311 #define CMD_RECONNECT (1 << 5)
312 #define CMD_LOOP (1 << 6)
314 /* mask for all commands */
315 #define CMD_ALL ((CMD_LOOP << 1) - 1)
317 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
319 gchar *__txt = _gst_element_error_printf text; \
320 gst_element_post_message (GST_ELEMENT_CAST (el), \
321 gst_message_new_progress (GST_OBJECT_CAST (el), \
322 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
326 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
328 #define gst_rtspsrc_parent_class parent_class
329 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
330 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
333 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
335 GST_DEBUG_OBJECT (src, "default handler");
340 select_stream_accum (GSignalInvocationHint * ihint,
341 GValue * return_accu, const GValue * handler_return, gpointer data)
345 myboolean = g_value_get_boolean (handler_return);
346 GST_DEBUG ("accum %d", myboolean);
347 g_value_set_boolean (return_accu, myboolean);
349 /* stop emission if FALSE */
354 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
356 GObjectClass *gobject_class;
357 GstElementClass *gstelement_class;
358 GstBinClass *gstbin_class;
360 gobject_class = (GObjectClass *) klass;
361 gstelement_class = (GstElementClass *) klass;
362 gstbin_class = (GstBinClass *) klass;
364 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
366 gobject_class->set_property = gst_rtspsrc_set_property;
367 gobject_class->get_property = gst_rtspsrc_get_property;
369 gobject_class->finalize = gst_rtspsrc_finalize;
371 g_object_class_install_property (gobject_class, PROP_LOCATION,
372 g_param_spec_string ("location", "RTSP Location",
373 "Location of the RTSP url to read",
374 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
376 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
377 g_param_spec_flags ("protocols", "Protocols",
378 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
379 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
381 g_object_class_install_property (gobject_class, PROP_DEBUG,
382 g_param_spec_boolean ("debug", "Debug",
383 "Dump request and response messages to stdout",
384 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
386 g_object_class_install_property (gobject_class, PROP_RETRY,
387 g_param_spec_uint ("retry", "Retry",
388 "Max number of retries when allocating RTP ports.",
389 0, G_MAXUINT16, DEFAULT_RETRY,
390 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
392 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
393 g_param_spec_uint64 ("timeout", "Timeout",
394 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
395 0, G_MAXUINT64, DEFAULT_TIMEOUT,
396 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
398 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
399 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
400 "Fail after timeout microseconds on TCP connections (0 = disabled)",
401 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
402 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
404 g_object_class_install_property (gobject_class, PROP_LATENCY,
405 g_param_spec_uint ("latency", "Buffer latency in ms",
406 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
407 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
409 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
410 g_param_spec_boolean ("drop-on-latency",
411 "Drop buffers when maximum latency is reached",
412 "Tells the jitterbuffer to never exceed the given latency in size",
413 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
415 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
416 g_param_spec_uint64 ("connection-speed", "Connection Speed",
417 "Network connection speed in kbps (0 = unknown)",
418 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
419 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
421 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
422 g_param_spec_enum ("nat-method", "NAT Method",
423 "Method to use for traversing firewalls and NAT",
424 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
425 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
428 * GstRTSPSrc:do-rtcp:
430 * Enable RTCP support. Some old server don't like RTCP and then this property
431 * needs to be set to FALSE.
433 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
434 g_param_spec_boolean ("do-rtcp", "Do RTCP",
435 "Send RTCP packets, disable for old incompatible server.",
436 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
439 * GstRTSPSrc:do-rtsp-keep-alive:
441 * Enable RTSP keep alive support. Some old server don't like RTSP
442 * keep alive and then this property needs to be set to FALSE.
444 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
445 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
446 "Send RTSP keep alive packets, disable for old incompatible server.",
447 DEFAULT_DO_RTSP_KEEP_ALIVE,
448 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
453 * Set the proxy parameters. This has to be a string of the format
454 * [http://][user:passwd@]host[:port].
456 g_object_class_install_property (gobject_class, PROP_PROXY,
457 g_param_spec_string ("proxy", "Proxy",
458 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
459 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
461 * GstRTSPSrc:proxy-id:
463 * Sets the proxy URI user id for authentication. If the URI set via the
464 * "proxy" property contains a user-id already, that will take precedence.
468 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
469 g_param_spec_string ("proxy-id", "proxy-id",
470 "HTTP proxy URI user id for authentication", "",
471 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
473 * GstRTSPSrc:proxy-pw:
475 * Sets the proxy URI password for authentication. If the URI set via the
476 * "proxy" property contains a password already, that will take precedence.
480 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
481 g_param_spec_string ("proxy-pw", "proxy-pw",
482 "HTTP proxy URI user password for authentication", "",
483 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
486 * GstRTSPSrc:rtp-blocksize:
488 * RTP package size to suggest to server.
490 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
491 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
492 "RTP package size to suggest to server (0 = disabled)",
493 0, 65536, DEFAULT_RTP_BLOCKSIZE,
494 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
496 g_object_class_install_property (gobject_class,
498 g_param_spec_string ("user-id", "user-id",
499 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
500 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
501 g_object_class_install_property (gobject_class, PROP_USER_PW,
502 g_param_spec_string ("user-pw", "user-pw",
503 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
504 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
507 * GstRTSPSrc:buffer-mode:
509 * Control the buffering and timestamping mode used by the jitterbuffer.
511 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
512 g_param_spec_enum ("buffer-mode", "Buffer Mode",
513 "Control the buffering algorithm in use",
514 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
515 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
518 * GstRTSPSrc:port-range:
520 * Configure the client port numbers that can be used to recieve RTP and
523 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
524 g_param_spec_string ("port-range", "Port range",
525 "Client port range that can be used to receive RTP and RTCP data, "
526 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
527 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
530 * GstRTSPSrc:udp-buffer-size:
532 * Size of the kernel UDP receive buffer in bytes.
534 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
535 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
536 "Size of the kernel UDP receive buffer in bytes, 0=default",
537 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
538 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
541 * GstRTSPSrc:short-header:
543 * Only send the basic RTSP headers for broken encoders.
545 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
546 g_param_spec_boolean ("short-header", "Short Header",
547 "Only send the basic RTSP headers for broken encoders",
548 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
550 g_object_class_install_property (gobject_class, PROP_PROBATION,
551 g_param_spec_uint ("probation", "Number of probations",
552 "Consecutive packet sequence numbers to accept the source",
553 0, G_MAXUINT, DEFAULT_PROBATION,
554 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
556 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
557 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
558 "Reconnect to the server if RTSP connection is closed when doing UDP",
559 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
561 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
562 g_param_spec_string ("multicast-iface", "Multicast Interface",
563 "The network interface on which to join the multicast group",
564 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
566 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
567 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
568 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
569 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
571 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
572 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
573 "Use the pipeline running-time to set the NTP time in the RTCP SR messages",
574 DEFAULT_USE_PIPELINE_CLOCK,
575 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
577 g_object_class_install_property (gobject_class, PROP_SDES,
578 g_param_spec_boxed ("sdes", "SDES",
579 "The SDES items of this session",
580 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
583 * GstRTSPSrc::tls-validation-flags:
585 * TLS certificate validation flags used to validate server
590 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
591 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
592 "TLS certificate validation flags used to validate the server certificate",
593 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
594 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
597 * GstRTSPSrc::tls-database:
599 * TLS database with anchor certificate authorities used to validate
600 * the server certificate.
604 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
605 g_param_spec_object ("tls-database", "TLS database",
606 "TLS database with anchor certificate authorities used to validate the server certificate",
607 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
610 * GstRTSPSrc::handle-request:
611 * @rtspsrc: a #GstRTSPSrc
612 * @request: a #GstRTSPMessage
613 * @response: a #GstRTSPMessage
615 * Handle a server request in @request and prepare @response.
617 * This signal is called from the streaming thread, you should therefore not
618 * do any state changes on @rtspsrc because this might deadlock. If you want
619 * to modify the state as a result of this signal, post a
620 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
625 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
626 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
627 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
628 G_TYPE_POINTER, G_TYPE_POINTER);
631 * GstRTSPSrc::on-sdp:
632 * @rtspsrc: a #GstRTSPSrc
633 * @sdp: a #GstSDPMessage
635 * Emited when the client has retrieved the SDP and before it configures the
636 * streams in the SDP. @sdp can be inspected and modified.
638 * This signal is called from the streaming thread, you should therefore not
639 * do any state changes on @rtspsrc because this might deadlock. If you want
640 * to modify the state as a result of this signal, post a
641 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
646 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
647 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
648 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
649 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
652 * GstRTSPSrc::select-stream:
653 * @rtspsrc: a #GstRTSPSrc
654 * @num: the stream number
655 * @caps: the stream caps
657 * Emited before the client decides to configure the stream @num with
660 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
665 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
666 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
667 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
668 (GCallback) default_select_stream, select_stream_accum, NULL,
669 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, G_TYPE_UINT,
672 * GstRTSPSrc::new-manager:
673 * @rtspsrc: a #GstRTSPSrc
674 * @manager: a #GstElement
676 * Emited after a new manager (like rtpbin) was created and the default
677 * properties were configured.
681 gst_rtspsrc_signals[SIGNAL_NEW_MANAGER] =
682 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
683 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
684 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
686 gstelement_class->send_event = gst_rtspsrc_send_event;
687 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
688 gstelement_class->change_state = gst_rtspsrc_change_state;
690 gst_element_class_add_pad_template (gstelement_class,
691 gst_static_pad_template_get (&rtptemplate));
693 gst_element_class_set_static_metadata (gstelement_class,
694 "RTSP packet receiver", "Source/Network",
695 "Receive data over the network via RTSP (RFC 2326)",
696 "Wim Taymans <wim@fluendo.com>, "
697 "Thijs Vermeir <thijs.vermeir@barco.com>, "
698 "Lutz Mueller <lutz@topfrose.de>");
700 gstbin_class->handle_message = gst_rtspsrc_handle_message;
702 gst_rtsp_ext_list_init ();
706 gst_rtspsrc_init (GstRTSPSrc * src)
708 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
709 src->protocols = DEFAULT_PROTOCOLS;
710 src->debug = DEFAULT_DEBUG;
711 src->retry = DEFAULT_RETRY;
712 src->udp_timeout = DEFAULT_TIMEOUT;
713 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
714 src->latency = DEFAULT_LATENCY_MS;
715 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
716 src->connection_speed = DEFAULT_CONNECTION_SPEED;
717 src->nat_method = DEFAULT_NAT_METHOD;
718 src->do_rtcp = DEFAULT_DO_RTCP;
719 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
720 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
721 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
722 src->user_id = g_strdup (DEFAULT_USER_ID);
723 src->user_pw = g_strdup (DEFAULT_USER_PW);
724 src->buffer_mode = DEFAULT_BUFFER_MODE;
725 src->client_port_range.min = 0;
726 src->client_port_range.max = 0;
727 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
728 src->short_header = DEFAULT_SHORT_HEADER;
729 src->probation = DEFAULT_PROBATION;
730 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
731 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
732 src->ntp_sync = DEFAULT_NTP_SYNC;
733 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
735 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
736 src->tls_database = DEFAULT_TLS_DATABASE;
738 /* get a list of all extensions */
739 src->extensions = gst_rtsp_ext_list_get ();
741 /* connect to send signal */
742 gst_rtsp_ext_list_connect (src->extensions, "send",
743 (GCallback) gst_rtspsrc_send_cb, src);
745 /* protects the streaming thread in interleaved mode or the polling
746 * thread in UDP mode. */
747 g_rec_mutex_init (&src->stream_rec_lock);
749 /* protects our state changes from multiple invocations */
750 g_rec_mutex_init (&src->state_rec_lock);
752 src->state = GST_RTSP_STATE_INVALID;
754 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
758 gst_rtspsrc_finalize (GObject * object)
762 rtspsrc = GST_RTSPSRC (object);
764 gst_rtsp_ext_list_free (rtspsrc->extensions);
765 g_free (rtspsrc->conninfo.location);
766 gst_rtsp_url_free (rtspsrc->conninfo.url);
767 g_free (rtspsrc->conninfo.url_str);
768 g_free (rtspsrc->user_id);
769 g_free (rtspsrc->user_pw);
770 g_free (rtspsrc->multi_iface);
773 gst_sdp_message_free (rtspsrc->sdp);
776 if (rtspsrc->provided_clock)
777 gst_object_unref (rtspsrc->provided_clock);
780 gst_structure_free (rtspsrc->sdes);
782 if (rtspsrc->tls_database)
783 g_object_unref (rtspsrc->tls_database);
786 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
787 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
789 G_OBJECT_CLASS (parent_class)->finalize (object);
793 gst_rtspsrc_provide_clock (GstElement * element)
795 GstRTSPSrc *src = GST_RTSPSRC (element);
798 if ((clock = src->provided_clock) != NULL)
799 gst_object_ref (clock);
804 /* a proxy string of the format [user:passwd@]host[:port] */
806 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
810 g_free (rtsp->proxy_user);
811 rtsp->proxy_user = NULL;
812 g_free (rtsp->proxy_passwd);
813 rtsp->proxy_passwd = NULL;
814 g_free (rtsp->proxy_host);
815 rtsp->proxy_host = NULL;
816 rtsp->proxy_port = 0;
823 /* we allow http:// in front but ignore it */
824 if (g_str_has_prefix (p, "http://"))
827 at = strchr (p, '@');
829 /* look for user:passwd */
830 col = strchr (proxy, ':');
831 if (col == NULL || col > at)
834 rtsp->proxy_user = g_strndup (p, col - p);
836 rtsp->proxy_passwd = g_strndup (col, at - col);
841 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
842 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
843 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
844 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
845 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
846 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
847 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
850 col = strchr (p, ':');
853 /* everything before the colon is the hostname */
854 rtsp->proxy_host = g_strndup (p, col - p);
856 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
858 rtsp->proxy_host = g_strdup (p);
859 rtsp->proxy_port = 8080;
865 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
867 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
868 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
871 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
873 rtspsrc->ptcp_timeout = NULL;
877 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
882 rtspsrc = GST_RTSPSRC (object);
886 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
887 g_value_get_string (value), NULL);
890 rtspsrc->protocols = g_value_get_flags (value);
893 rtspsrc->debug = g_value_get_boolean (value);
896 rtspsrc->retry = g_value_get_uint (value);
899 rtspsrc->udp_timeout = g_value_get_uint64 (value);
901 case PROP_TCP_TIMEOUT:
902 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
905 rtspsrc->latency = g_value_get_uint (value);
907 case PROP_DROP_ON_LATENCY:
908 rtspsrc->drop_on_latency = g_value_get_boolean (value);
910 case PROP_CONNECTION_SPEED:
911 rtspsrc->connection_speed = g_value_get_uint64 (value);
913 case PROP_NAT_METHOD:
914 rtspsrc->nat_method = g_value_get_enum (value);
917 rtspsrc->do_rtcp = g_value_get_boolean (value);
919 case PROP_DO_RTSP_KEEP_ALIVE:
920 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
923 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
926 if (rtspsrc->prop_proxy_id)
927 g_free (rtspsrc->prop_proxy_id);
928 rtspsrc->prop_proxy_id = g_value_dup_string (value);
931 if (rtspsrc->prop_proxy_pw)
932 g_free (rtspsrc->prop_proxy_pw);
933 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
935 case PROP_RTP_BLOCKSIZE:
936 rtspsrc->rtp_blocksize = g_value_get_uint (value);
939 if (rtspsrc->user_id)
940 g_free (rtspsrc->user_id);
941 rtspsrc->user_id = g_value_dup_string (value);
944 if (rtspsrc->user_pw)
945 g_free (rtspsrc->user_pw);
946 rtspsrc->user_pw = g_value_dup_string (value);
948 case PROP_BUFFER_MODE:
949 rtspsrc->buffer_mode = g_value_get_enum (value);
951 case PROP_PORT_RANGE:
955 str = g_value_get_string (value);
957 sscanf (str, "%u-%u",
958 &rtspsrc->client_port_range.min, &rtspsrc->client_port_range.max);
960 rtspsrc->client_port_range.min = 0;
961 rtspsrc->client_port_range.max = 0;
965 case PROP_UDP_BUFFER_SIZE:
966 rtspsrc->udp_buffer_size = g_value_get_int (value);
968 case PROP_SHORT_HEADER:
969 rtspsrc->short_header = g_value_get_boolean (value);
972 rtspsrc->probation = g_value_get_uint (value);
974 case PROP_UDP_RECONNECT:
975 rtspsrc->udp_reconnect = g_value_get_boolean (value);
977 case PROP_MULTICAST_IFACE:
978 g_free (rtspsrc->multi_iface);
980 if (g_value_get_string (value) == NULL)
981 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
983 rtspsrc->multi_iface = g_value_dup_string (value);
986 rtspsrc->ntp_sync = g_value_get_boolean (value);
988 case PROP_USE_PIPELINE_CLOCK:
989 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
992 rtspsrc->sdes = g_value_dup_boxed (value);
994 case PROP_TLS_VALIDATION_FLAGS:
995 rtspsrc->tls_validation_flags = g_value_get_flags (value);
997 case PROP_TLS_DATABASE:
998 g_clear_object (&rtspsrc->tls_database);
999 rtspsrc->tls_database = g_value_dup_object (value);
1002 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1008 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
1011 GstRTSPSrc *rtspsrc;
1013 rtspsrc = GST_RTSPSRC (object);
1017 g_value_set_string (value, rtspsrc->conninfo.location);
1019 case PROP_PROTOCOLS:
1020 g_value_set_flags (value, rtspsrc->protocols);
1023 g_value_set_boolean (value, rtspsrc->debug);
1026 g_value_set_uint (value, rtspsrc->retry);
1029 g_value_set_uint64 (value, rtspsrc->udp_timeout);
1031 case PROP_TCP_TIMEOUT:
1035 timeout = rtspsrc->tcp_timeout.tv_sec * G_USEC_PER_SEC +
1036 rtspsrc->tcp_timeout.tv_usec;
1037 g_value_set_uint64 (value, timeout);
1041 g_value_set_uint (value, rtspsrc->latency);
1043 case PROP_DROP_ON_LATENCY:
1044 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1046 case PROP_CONNECTION_SPEED:
1047 g_value_set_uint64 (value, rtspsrc->connection_speed);
1049 case PROP_NAT_METHOD:
1050 g_value_set_enum (value, rtspsrc->nat_method);
1053 g_value_set_boolean (value, rtspsrc->do_rtcp);
1055 case PROP_DO_RTSP_KEEP_ALIVE:
1056 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1062 if (rtspsrc->proxy_host) {
1064 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1068 g_value_take_string (value, str);
1072 g_value_set_string (value, rtspsrc->prop_proxy_id);
1075 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1077 case PROP_RTP_BLOCKSIZE:
1078 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1081 g_value_set_string (value, rtspsrc->user_id);
1084 g_value_set_string (value, rtspsrc->user_pw);
1086 case PROP_BUFFER_MODE:
1087 g_value_set_enum (value, rtspsrc->buffer_mode);
1089 case PROP_PORT_RANGE:
1093 if (rtspsrc->client_port_range.min != 0) {
1094 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1095 rtspsrc->client_port_range.max);
1099 g_value_take_string (value, str);
1102 case PROP_UDP_BUFFER_SIZE:
1103 g_value_set_int (value, rtspsrc->udp_buffer_size);
1105 case PROP_SHORT_HEADER:
1106 g_value_set_boolean (value, rtspsrc->short_header);
1108 case PROP_PROBATION:
1109 g_value_set_uint (value, rtspsrc->probation);
1111 case PROP_UDP_RECONNECT:
1112 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1114 case PROP_MULTICAST_IFACE:
1115 g_value_set_string (value, rtspsrc->multi_iface);
1118 g_value_set_boolean (value, rtspsrc->ntp_sync);
1120 case PROP_USE_PIPELINE_CLOCK:
1121 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1124 g_value_set_boxed (value, rtspsrc->sdes);
1126 case PROP_TLS_VALIDATION_FLAGS:
1127 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1129 case PROP_TLS_DATABASE:
1130 g_value_set_object (value, rtspsrc->tls_database);
1133 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1139 find_stream_by_id (GstRTSPStream * stream, gint * id)
1141 if (stream->id == *id)
1148 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1150 if (stream->channel[0] == *channel || stream->channel[1] == *channel)
1157 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1159 GstElement *src = (GstElement *) a;
1161 if (stream->udpsrc[0] == src)
1163 if (stream->udpsrc[1] == src)
1170 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1172 if (stream->conninfo.location) {
1173 /* check qualified setup_url */
1174 if (!strcmp (stream->conninfo.location, (gchar *) a))
1177 if (stream->control_url) {
1178 /* check original control_url */
1179 if (!strcmp (stream->control_url, (gchar *) a))
1182 /* check if qualified setup_url ends with string */
1183 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1190 static GstRTSPStream *
1191 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1195 /* find and get stream */
1196 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1197 return (GstRTSPStream *) lstream->data;
1202 static const GstSDPBandwidth *
1203 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1204 const GstSDPMedia * media, const gchar * type)
1208 /* first look in the media specific section */
1209 len = gst_sdp_media_bandwidths_len (media);
1210 for (i = 0; i < len; i++) {
1211 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1213 if (strcmp (bw->bwtype, type) == 0)
1216 /* then look in the message specific section */
1217 len = gst_sdp_message_bandwidths_len (sdp);
1218 for (i = 0; i < len; i++) {
1219 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1221 if (strcmp (bw->bwtype, type) == 0)
1228 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1229 const GstSDPMedia * media, GstRTSPStream * stream)
1231 const GstSDPBandwidth *bw;
1233 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1234 stream->as_bandwidth = bw->bandwidth;
1236 stream->as_bandwidth = -1;
1238 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1239 stream->rr_bandwidth = bw->bandwidth;
1241 stream->rr_bandwidth = -1;
1243 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1244 stream->rs_bandwidth = bw->bandwidth;
1246 stream->rs_bandwidth = -1;
1250 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1251 const GstSDPConnection * conn)
1253 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1256 if (conn->addrtype == NULL)
1259 /* check for IPV6 */
1260 if (strcmp (conn->addrtype, "IP4") == 0)
1261 stream->is_ipv6 = FALSE;
1262 else if (strcmp (conn->addrtype, "IP6") == 0)
1263 stream->is_ipv6 = TRUE;
1268 g_free (stream->destination);
1269 stream->destination = g_strdup (conn->address);
1271 /* check for multicast */
1272 stream->is_multicast =
1273 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1275 stream->ttl = conn->ttl;
1278 /* Go over the connections for a stream.
1279 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1281 * - If we are dealing with a localhost address, we disable multicast
1284 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1285 const GstSDPMedia * media, GstRTSPStream * stream)
1287 const GstSDPConnection *conn;
1290 /* first look in the media specific section */
1291 len = gst_sdp_media_connections_len (media);
1292 for (i = 0; i < len; i++) {
1293 conn = gst_sdp_media_get_connection (media, i);
1295 gst_rtspsrc_do_stream_connection (src, stream, conn);
1297 /* then look in the message specific section */
1298 if ((conn = gst_sdp_message_get_connection (sdp))) {
1299 gst_rtspsrc_do_stream_connection (src, stream, conn);
1303 /* m=<media> <UDP port> RTP/AVP <payload>
1306 gst_rtspsrc_collect_payloads (GstRTSPSrc * src, const GstSDPMessage * sdp,
1307 const GstSDPMedia * media, GstRTSPStream * stream)
1313 proto = gst_sdp_media_get_proto (media);
1317 if (g_str_equal (proto, "RTP/AVP"))
1318 stream->profile = GST_RTSP_PROFILE_AVP;
1319 else if (g_str_equal (proto, "RTP/SAVP"))
1320 stream->profile = GST_RTSP_PROFILE_SAVP;
1321 else if (g_str_equal (proto, "RTP/AVPF"))
1322 stream->profile = GST_RTSP_PROFILE_AVPF;
1323 else if (g_str_equal (proto, "RTP/SAVPF"))
1324 stream->profile = GST_RTSP_PROFILE_SAVPF;
1328 len = gst_sdp_media_formats_len (media);
1329 for (i = 0; i < len; i++) {
1336 pt = atoi (gst_sdp_media_get_format (media, i));
1338 GST_DEBUG_OBJECT (src, " looking at %d pt: %d", i, pt);
1341 caps = gst_rtspsrc_media_to_caps (pt, media);
1343 GST_WARNING_OBJECT (src, " skipping pt %d without caps", pt);
1347 /* do some tweaks */
1348 s = gst_caps_get_structure (caps, 0);
1349 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
1350 stream->is_real = (strstr (enc, "-REAL") != NULL);
1351 if (strcmp (enc, "X-ASF-PF") == 0)
1352 stream->container = TRUE;
1354 GST_DEBUG ("mapping sdp session level attributes to caps");
1355 gst_rtspsrc_sdp_attributes_to_caps (sdp->attributes, caps);
1356 GST_DEBUG ("mapping sdp media level attributes to caps");
1357 gst_rtspsrc_sdp_attributes_to_caps (media->attributes, caps);
1359 /* the first pt will be the default */
1360 if (stream->ptmap->len == 0)
1361 stream->default_pt = pt;
1365 g_array_append_val (stream->ptmap, item);
1371 GST_ERROR_OBJECT (src, "can't find proto in media");
1376 GST_ERROR_OBJECT (src, "unknown proto in media %s", proto);
1381 static const gchar *
1382 get_aggregate_control (GstRTSPSrc * src)
1387 base = src->control;
1388 else if (src->content_base)
1389 base = src->content_base;
1390 else if (src->conninfo.url_str)
1391 base = src->conninfo.url_str;
1399 clear_ptmap_item (PtMapItem * item)
1402 gst_caps_unref (item->caps);
1405 static GstRTSPStream *
1406 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
1408 GstRTSPStream *stream;
1409 const gchar *control_url;
1410 const GstSDPMedia *media;
1412 /* get media, should not return NULL */
1413 media = gst_sdp_message_get_media (sdp, idx);
1417 stream = g_new0 (GstRTSPStream, 1);
1418 stream->parent = src;
1419 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1421 stream->last_ret = GST_FLOW_NOT_LINKED;
1422 stream->added = FALSE;
1423 stream->setup = FALSE;
1424 stream->skipped = FALSE;
1426 stream->eos = FALSE;
1427 stream->discont = TRUE;
1428 stream->seqbase = -1;
1429 stream->timebase = -1;
1430 stream->send_ssrc = g_random_int ();
1431 stream->profile = GST_RTSP_PROFILE_AVP;
1432 stream->ptmap = g_array_new (FALSE, FALSE, sizeof (PtMapItem));
1433 g_array_set_clear_func (stream->ptmap, (GDestroyNotify) clear_ptmap_item);
1435 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1436 * session manager to scale RTCP. */
1437 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1439 /* collect connection info */
1440 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1442 /* make the payload type map */
1443 gst_rtspsrc_collect_payloads (src, sdp, media, stream);
1445 /* collect port number */
1446 stream->port = gst_sdp_media_get_port (media);
1448 /* get control url to construct the setup url. The setup url is used to
1449 * configure the transport of the stream and is used to identity the stream in
1450 * the RTP-Info header field returned from PLAY. */
1451 control_url = gst_sdp_media_get_attribute_val (media, "control");
1452 if (control_url == NULL)
1453 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1455 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1456 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1457 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1458 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1460 if (control_url != NULL) {
1461 stream->control_url = g_strdup (control_url);
1462 /* Build a fully qualified url using the content_base if any or by prefixing
1463 * the original request.
1464 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1465 * likely build a URL that the server will fail to understand, this is ok,
1466 * we will fail then. */
1467 if (g_str_has_prefix (control_url, "rtsp://"))
1468 stream->conninfo.location = g_strdup (control_url);
1473 if (g_strcmp0 (control_url, "*") == 0)
1476 base = get_aggregate_control (src);
1478 /* check if the base ends or control starts with / */
1479 has_slash = g_str_has_prefix (control_url, "/");
1480 has_slash = has_slash || g_str_has_suffix (base, "/");
1482 /* concatenate the two strings, insert / when not present */
1483 stream->conninfo.location =
1484 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1487 GST_DEBUG_OBJECT (src, " setup: %s",
1488 GST_STR_NULL (stream->conninfo.location));
1490 /* we keep track of all streams */
1491 src->streams = g_list_append (src->streams, stream);
1499 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1503 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1505 g_array_free (stream->ptmap, TRUE);
1507 g_free (stream->destination);
1508 g_free (stream->control_url);
1509 g_free (stream->conninfo.location);
1511 for (i = 0; i < 2; i++) {
1512 if (stream->udpsrc[i]) {
1513 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1514 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1515 gst_object_unref (stream->udpsrc[i]);
1517 if (stream->channelpad[i])
1518 gst_object_unref (stream->channelpad[i]);
1520 if (stream->udpsink[i]) {
1521 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1522 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1523 gst_object_unref (stream->udpsink[i]);
1526 if (stream->fakesrc) {
1527 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1528 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1529 gst_object_unref (stream->fakesrc);
1531 if (stream->srcpad) {
1532 gst_pad_set_active (stream->srcpad, FALSE);
1534 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1536 if (stream->srtpenc)
1537 gst_object_unref (stream->srtpenc);
1538 if (stream->srtpdec)
1539 gst_object_unref (stream->srtpdec);
1541 gst_buffer_unref (stream->key);
1542 if (stream->rtcppad)
1543 gst_object_unref (stream->rtcppad);
1544 if (stream->session)
1545 g_object_unref (stream->session);
1550 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1554 GST_DEBUG_OBJECT (src, "cleanup");
1556 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1557 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1559 gst_rtspsrc_stream_free (src, stream);
1561 g_list_free (src->streams);
1562 src->streams = NULL;
1564 if (src->manager_sig_id) {
1565 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1566 src->manager_sig_id = 0;
1568 gst_element_set_state (src->manager, GST_STATE_NULL);
1569 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1570 src->manager = NULL;
1573 gst_structure_free (src->props);
1576 g_free (src->content_base);
1577 src->content_base = NULL;
1579 g_free (src->control);
1580 src->control = NULL;
1583 gst_rtsp_range_free (src->range);
1586 /* don't clear the SDP when it was used in the url */
1587 if (src->sdp && !src->from_sdp) {
1588 gst_sdp_message_free (src->sdp);
1591 if (src->start_segment) {
1592 gst_event_unref (src->start_segment);
1593 src->start_segment = NULL;
1595 if (src->provided_clock) {
1596 gst_object_unref (src->provided_clock);
1597 src->provided_clock = NULL;
1601 #define PARSE_INT(p, del, res) \
1604 p = strstr (p, del); \
1614 #define PARSE_STRING(p, del, res) \
1617 p = strstr (p, del); \
1629 #define SKIP_SPACES(p) \
1630 while (*p && g_ascii_isspace (*p)) \
1635 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1638 gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
1639 gint * rate, gchar ** params)
1643 p = (gchar *) rtpmap;
1645 PARSE_INT (p, " ", *payload);
1653 PARSE_STRING (p, "/", *name);
1654 if (*name == NULL) {
1655 GST_DEBUG ("no rate, name %s", p);
1656 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
1657 * streams seem to omit the rate. */
1664 p = strstr (p, "/");
1682 parse_keymgmt (const gchar * keymgmt, GstCaps * caps)
1684 gboolean res = FALSE;
1688 GstMIKEYMessage *msg;
1689 const GstMIKEYPayload *payload;
1690 const gchar *srtp_cipher;
1691 const gchar *srtp_auth;
1693 p = (gchar *) keymgmt;
1699 PARSE_STRING (p, " ", kmpid);
1700 if (!g_str_equal (kmpid, "mikey"))
1703 data = g_base64_decode (p, &size);
1707 msg = gst_mikey_message_new_from_data (data, size, NULL, NULL);
1711 srtp_cipher = "aes-128-icm";
1712 srtp_auth = "hmac-sha1-80";
1714 /* check the Security policy if any */
1715 if ((payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, 0))) {
1716 GstMIKEYPayloadSP *p = (GstMIKEYPayloadSP *) payload;
1719 if (p->proto != GST_MIKEY_SEC_PROTO_SRTP)
1722 len = gst_mikey_payload_sp_get_n_params (payload);
1723 for (i = 0; i < len; i++) {
1724 const GstMIKEYPayloadSPParam *param =
1725 gst_mikey_payload_sp_get_param (payload, i);
1727 switch (param->type) {
1728 case GST_MIKEY_SP_SRTP_ENC_ALG:
1729 switch (param->val[0]) {
1731 srtp_cipher = "null";
1735 srtp_cipher = "aes-128-icm";
1741 case GST_MIKEY_SP_SRTP_AUTH_ALG:
1742 switch (param->val[0]) {
1748 srtp_auth = "hmac-sha1-80";
1754 case GST_MIKEY_SP_SRTP_SRTP_ENC:
1756 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
1764 if (!(payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_KEMAC, 0)))
1767 GstMIKEYPayloadKEMAC *p = (GstMIKEYPayloadKEMAC *) payload;
1768 const GstMIKEYPayload *sub;
1769 GstMIKEYPayloadKeyData *pkd;
1772 if (p->enc_alg != GST_MIKEY_ENC_NULL || p->mac_alg != GST_MIKEY_MAC_NULL)
1775 if (!(sub = gst_mikey_payload_kemac_get_sub (payload, 0)))
1778 if (sub->type != GST_MIKEY_PT_KEY_DATA)
1781 pkd = (GstMIKEYPayloadKeyData *) sub;
1783 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
1785 gst_caps_set_simple (caps, "srtp-key", GST_TYPE_BUFFER, buf, NULL);
1788 gst_caps_set_simple (caps,
1789 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
1790 "srtp-auth", G_TYPE_STRING, srtp_auth,
1791 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
1792 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
1796 gst_mikey_message_free (msg);
1802 * Mapping SDP attributes to caps
1804 * prepend 'a-' to IANA registered sdp attributes names
1805 * (ie: not prefixed with 'x-') in order to avoid
1806 * collision with gstreamer standard caps properties names
1809 gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
1811 if (attributes->len > 0) {
1815 s = gst_caps_get_structure (caps, 0);
1817 for (i = 0; i < attributes->len; i++) {
1818 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
1819 gchar *tofree, *key;
1823 /* skip some of the attribute we already handle */
1824 if (!strcmp (key, "fmtp"))
1826 if (!strcmp (key, "rtpmap"))
1828 if (!strcmp (key, "control"))
1830 if (!strcmp (key, "range"))
1832 if (g_str_equal (key, "key-mgmt")) {
1833 parse_keymgmt (attr->value, caps);
1837 /* string must be valid UTF8 */
1838 if (!g_utf8_validate (attr->value, -1, NULL))
1841 if (!g_str_has_prefix (key, "x-"))
1842 tofree = key = g_strdup_printf ("a-%s", key);
1846 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
1847 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
1853 static const gchar *
1854 rtsp_get_attribute_for_pt (const GstSDPMedia * media, const gchar * name,
1863 if ((attr = gst_sdp_media_get_attribute_val_n (media, name, i)) == NULL)
1866 if (sscanf (attr, "%d ", &val) != 1)
1876 * Mapping of caps to and from SDP fields:
1878 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1879 * a=fmtp:<payload> <param>[=<value>];...
1882 gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
1885 const gchar *rtpmap;
1889 gchar *params = NULL;
1895 /* get and parse rtpmap */
1896 rtpmap = rtsp_get_attribute_for_pt (media, "rtpmap", pt);
1899 ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
1901 g_warning ("error parsing rtpmap, ignoring");
1905 /* dynamic payloads need rtpmap or we fail */
1906 if (rtpmap == NULL && pt >= 96)
1909 /* check if we have a rate, if not, we need to look up the rate from the
1910 * default rates based on the payload types. */
1912 const GstRTPPayloadInfo *info;
1914 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
1915 /* dynamic types, use media and encoding_name */
1916 tmp = g_ascii_strdown (media->media, -1);
1917 info = gst_rtp_payload_info_for_name (tmp, name);
1920 /* static types, use payload type */
1921 info = gst_rtp_payload_info_for_pt (pt);
1925 if ((rate = info->clock_rate) == 0)
1928 /* we fail if we cannot find one */
1933 tmp = g_ascii_strdown (media->media, -1);
1934 caps = gst_caps_new_simple ("application/x-unknown",
1935 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
1937 s = gst_caps_get_structure (caps, 0);
1939 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
1941 /* encoding name must be upper case */
1943 tmp = g_ascii_strup (name, -1);
1944 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
1948 /* params must be lower case */
1949 if (params != NULL) {
1950 tmp = g_ascii_strdown (params, -1);
1951 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
1955 /* parse optional fmtp: field */
1956 if ((fmtp = rtsp_get_attribute_for_pt (media, "fmtp", pt))) {
1962 /* p is now of the format <payload> <param>[=<value>];... */
1963 PARSE_INT (p, " ", payload);
1964 if (payload != -1 && payload == pt) {
1968 /* <param>[=<value>] are separated with ';' */
1969 pairs = g_strsplit (p, ";", 0);
1970 for (i = 0; pairs[i]; i++) {
1972 const gchar *val, *key;
1974 /* the key may not have a '=', the value can have other '='s */
1975 valpos = strstr (pairs[i], "=");
1977 /* we have a '=' and thus a value, remove the '=' with \0 */
1979 /* value is everything between '=' and ';'. We split the pairs at ;
1980 * boundaries so we can take the remainder of the value. Some servers
1981 * put spaces around the value which we strip off here. Alternatively
1982 * we could strip those spaces in the depayloaders should these spaces
1983 * actually carry any meaning in the future. */
1984 val = g_strstrip (valpos + 1);
1986 /* simple <param>;.. is translated into <param>=1;... */
1989 /* strip the key of spaces, convert key to lowercase but not the value. */
1990 key = g_strstrip (pairs[i]);
1991 if (strlen (key) > 1) {
1992 tmp = g_ascii_strdown (key, -1);
1993 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
2005 g_warning ("rtpmap type not given for dynamic payload %d", pt);
2010 g_warning ("rate unknown for payload type %d", pt);
2016 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
2017 gint * rtpport, gint * rtcpport)
2020 GstStateChangeReturn ret;
2021 GstElement *udpsrc0, *udpsrc1;
2022 gint tmp_rtp, tmp_rtcp;
2026 src = stream->parent;
2032 /* Start at next port */
2033 tmp_rtp = src->next_port_num;
2035 if (stream->is_ipv6)
2036 host = "udp://[::0]";
2038 host = "udp://0.0.0.0";
2040 /* try to allocate 2 UDP ports, the RTP port should be an even
2041 * number and the RTCP port should be the next (uneven) port */
2044 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
2045 tmp_rtp >= src->client_port_range.max)
2048 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2049 if (udpsrc0 == NULL)
2050 goto no_udp_protocol;
2051 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
2053 if (src->udp_buffer_size != 0)
2054 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
2057 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
2058 if (ret == GST_STATE_CHANGE_FAILURE) {
2060 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
2063 if (++count > src->retry)
2066 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2067 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2068 gst_object_unref (udpsrc0);
2071 GST_DEBUG_OBJECT (src, "retry %d", count);
2074 goto no_udp_protocol;
2077 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
2078 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
2080 /* check if port is even */
2081 if ((tmp_rtp & 0x01) != 0) {
2082 /* port not even, close and allocate another */
2083 if (++count > src->retry)
2086 GST_DEBUG_OBJECT (src, "RTP port not even");
2088 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2089 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2090 gst_object_unref (udpsrc0);
2093 GST_DEBUG_OBJECT (src, "retry %d", count);
2098 /* allocate port+1 for RTCP now */
2099 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2100 if (udpsrc1 == NULL)
2101 goto no_udp_rtcp_protocol;
2104 tmp_rtcp = tmp_rtp + 1;
2105 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
2108 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
2110 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
2111 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
2112 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
2113 if (ret == GST_STATE_CHANGE_FAILURE) {
2114 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
2116 if (++count > src->retry)
2119 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2120 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2121 gst_object_unref (udpsrc0);
2124 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
2125 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2126 gst_object_unref (udpsrc1);
2130 GST_DEBUG_OBJECT (src, "retry %d", count);
2134 /* all fine, do port check */
2135 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
2136 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
2138 /* this should not happen... */
2139 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
2142 /* we keep these elements, we configure all in configure_transport when the
2143 * server told us to really use the UDP ports. */
2144 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
2145 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
2146 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
2147 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
2149 /* keep track of next available port number when we have a range
2151 if (src->next_port_num != 0)
2152 src->next_port_num = tmp_rtcp + 1;
2159 GST_DEBUG_OBJECT (src, "could not get UDP source");
2164 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
2168 no_udp_rtcp_protocol:
2170 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
2175 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
2176 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
2182 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2183 gst_object_unref (udpsrc0);
2186 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2187 gst_object_unref (udpsrc1);
2194 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
2199 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
2201 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2202 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2205 for (i = 0; i < 2; i++) {
2206 if (stream->udpsrc[i])
2207 gst_element_set_state (stream->udpsrc[i], state);
2213 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
2220 event = gst_event_new_flush_start ();
2221 GST_DEBUG_OBJECT (src, "start flush");
2223 state = GST_STATE_PAUSED;
2225 event = gst_event_new_flush_stop (FALSE);
2226 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2229 state = GST_STATE_PLAYING;
2231 state = GST_STATE_PAUSED;
2233 gst_rtspsrc_push_event (src, event);
2234 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2235 gst_rtspsrc_set_state (src, state);
2238 static GstRTSPResult
2239 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
2240 GstRTSPMessage * message, GTimeVal * timeout)
2245 ret = gst_rtsp_connection_send (conn, message, timeout);
2247 ret = GST_RTSP_ERROR;
2252 static GstRTSPResult
2253 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
2254 GstRTSPMessage * message, GTimeVal * timeout)
2259 ret = gst_rtsp_connection_receive (conn, message, timeout);
2261 ret = GST_RTSP_ERROR;
2267 gst_rtspsrc_get_position (GstRTSPSrc * src)
2272 query = gst_query_new_position (GST_FORMAT_TIME);
2273 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2274 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2275 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2279 if (stream->srcpad) {
2280 if (gst_pad_query (stream->srcpad, query)) {
2281 gst_query_parse_position (query, &fmt, &pos);
2282 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2283 GST_TIME_ARGS (pos));
2284 src->last_pos = pos;
2294 gst_rtspsrc_do_seek (GstRTSPSrc * src, GstSegment * segment)
2296 src->state = GST_RTSP_STATE_SEEKING;
2297 /* PLAY will add the range header now. */
2298 src->need_range = TRUE;
2304 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2309 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2311 gboolean flush, skip;
2314 GstSegment seeksegment = { 0, };
2318 GST_DEBUG_OBJECT (src, "doing seek with event");
2320 gst_event_parse_seek (event, &rate, &format, &flags,
2321 &cur_type, &cur, &stop_type, &stop);
2323 /* no negative rates yet */
2327 /* we need TIME format */
2328 if (format != src->segment.format)
2331 GST_DEBUG_OBJECT (src, "doing seek without event");
2333 cur_type = GST_SEEK_TYPE_SET;
2334 stop_type = GST_SEEK_TYPE_SET;
2337 /* get flush flag */
2338 flush = flags & GST_SEEK_FLAG_FLUSH;
2339 skip = flags & GST_SEEK_FLAG_SKIP;
2341 /* now we need to make sure the streaming thread is stopped. We do this by
2342 * either sending a FLUSH_START event downstream which will cause the
2343 * streaming thread to stop with a WRONG_STATE.
2344 * For a non-flushing seek we simply pause the task, which will happen as soon
2345 * as it completes one iteration (and thus might block when the sink is
2346 * blocking in preroll). */
2348 GST_DEBUG_OBJECT (src, "starting flush");
2349 gst_rtspsrc_flush (src, TRUE, FALSE);
2352 gst_task_pause (src->task);
2356 /* we should now be able to grab the streaming thread because we stopped it
2357 * with the above flush/pause code */
2358 GST_RTSP_STREAM_LOCK (src);
2360 GST_DEBUG_OBJECT (src, "stopped streaming");
2362 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2363 gst_rtspsrc_connection_flush (src, FALSE);
2365 /* copy segment, we need this because we still need the old
2366 * segment when we close the current segment. */
2367 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2369 /* configure the seek parameters in the seeksegment. We will then have the
2370 * right values in the segment to perform the seek */
2372 GST_DEBUG_OBJECT (src, "configuring seek");
2373 gst_segment_do_seek (&seeksegment, rate, format, flags,
2374 cur_type, cur, stop_type, stop, &update);
2377 /* figure out the last position we need to play. If it's configured (stop !=
2378 * -1), use that, else we play until the total duration of the file */
2379 if ((stop = seeksegment.stop) == -1)
2380 stop = seeksegment.duration;
2382 playing = (src->state == GST_RTSP_STATE_PLAYING);
2384 /* if we were playing, pause first */
2386 /* obtain current position in case seek fails */
2387 gst_rtspsrc_get_position (src);
2388 gst_rtspsrc_pause (src, FALSE);
2392 gst_rtspsrc_do_seek (src, &seeksegment);
2394 /* and continue playing */
2396 gst_rtspsrc_play (src, &seeksegment, FALSE);
2398 /* prepare for streaming again */
2400 /* if we started flush, we stop now */
2401 GST_DEBUG_OBJECT (src, "stopping flush");
2402 gst_rtspsrc_flush (src, FALSE, playing);
2405 /* now we did the seek and can activate the new segment values */
2406 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2408 /* if we're doing a segment seek, post a SEGMENT_START message */
2409 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2410 gst_element_post_message (GST_ELEMENT_CAST (src),
2411 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2412 src->segment.format, src->segment.position));
2415 /* now create the newsegment */
2416 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2417 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2420 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2421 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2422 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2423 stream->discont = TRUE;
2426 GST_RTSP_STREAM_UNLOCK (src);
2433 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2438 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2444 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2448 gboolean res = TRUE;
2451 src = GST_RTSPSRC_CAST (parent);
2453 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2454 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2456 switch (GST_EVENT_TYPE (event)) {
2457 case GST_EVENT_SEEK:
2458 res = gst_rtspsrc_perform_seek (src, event);
2462 case GST_EVENT_NAVIGATION:
2463 case GST_EVENT_LATENCY:
2471 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2472 res = gst_pad_send_event (target, event);
2473 gst_object_unref (target);
2475 gst_event_unref (event);
2478 gst_event_unref (event);
2484 /* this is the final event function we receive on the internal source pad when
2485 * we deal with TCP connections */
2487 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2492 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2494 switch (GST_EVENT_TYPE (event)) {
2495 case GST_EVENT_SEEK:
2497 case GST_EVENT_NAVIGATION:
2498 case GST_EVENT_LATENCY:
2500 gst_event_unref (event);
2507 /* this is the final query function we receive on the internal source pad when
2508 * we deal with TCP connections */
2510 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2514 gboolean res = TRUE;
2516 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2518 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2519 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2521 switch (GST_QUERY_TYPE (query)) {
2522 case GST_QUERY_POSITION:
2527 case GST_QUERY_DURATION:
2531 gst_query_parse_duration (query, &format, NULL);
2534 case GST_FORMAT_TIME:
2535 gst_query_set_duration (query, format, src->segment.duration);
2543 case GST_QUERY_LATENCY:
2545 /* we are live with a min latency of 0 and unlimited max latency, this
2546 * result will be updated by the session manager if there is any. */
2547 gst_query_set_latency (query, TRUE, 0, -1);
2557 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2559 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2563 gboolean res = FALSE;
2565 src = GST_RTSPSRC_CAST (parent);
2567 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2568 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2570 switch (GST_QUERY_TYPE (query)) {
2571 case GST_QUERY_DURATION:
2575 gst_query_parse_duration (query, &format, NULL);
2578 case GST_FORMAT_TIME:
2579 gst_query_set_duration (query, format, src->segment.duration);
2587 case GST_QUERY_SEEKING:
2591 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2592 if (format == GST_FORMAT_TIME) {
2594 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2596 /* seeking without duration is unlikely */
2597 seekable = seekable && src->seekable && src->segment.duration &&
2598 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2600 /* FIXME ?? should we have 0 and segment.duration here; see demuxers */
2601 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
2602 src->segment.start, src->segment.stop);
2611 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2613 gst_query_set_uri (query, uri);
2621 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2623 /* forward the query to the proxy target pad */
2625 res = gst_pad_query (target, query);
2626 gst_object_unref (target);
2635 /* callback for RTCP messages to be sent to the server when operating in TCP
2637 static GstFlowReturn
2638 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2641 GstRTSPStream *stream;
2642 GstFlowReturn res = GST_FLOW_OK;
2647 GstRTSPMessage message = { 0 };
2648 GstRTSPConnection *conn;
2650 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2651 src = stream->parent;
2653 gst_buffer_map (buffer, &map, GST_MAP_READ);
2657 gst_rtsp_message_init_data (&message, stream->channel[1]);
2659 /* lend the body data to the message */
2660 gst_rtsp_message_take_body (&message, data, size);
2662 if (stream->conninfo.connection)
2663 conn = stream->conninfo.connection;
2665 conn = src->conninfo.connection;
2667 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2668 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2669 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2671 /* and steal it away again because we will free it when unreffing the
2673 gst_rtsp_message_steal_body (&message, &data, &size);
2674 gst_rtsp_message_unset (&message);
2676 gst_buffer_unmap (buffer, &map);
2677 gst_buffer_unref (buffer);
2682 static GstPadProbeReturn
2683 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2685 GstRTSPSrc *src = user_data;
2687 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2688 GST_DEBUG_PAD_NAME (pad));
2690 /* activate the streams */
2691 GST_OBJECT_LOCK (src);
2692 if (!src->need_activate)
2695 src->need_activate = FALSE;
2696 GST_OBJECT_UNLOCK (src);
2698 gst_rtspsrc_activate_streams (src);
2700 return GST_PAD_PROBE_OK;
2704 GST_OBJECT_UNLOCK (src);
2705 return GST_PAD_PROBE_OK;
2710 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
2712 GstPad *gpad = GST_PAD_CAST (user_data);
2714 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
2715 gst_pad_store_sticky_event (gpad, *event);
2720 /* this callback is called when the session manager generated a new src pad with
2721 * payloaded RTP packets. We simply ghost the pad here. */
2723 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2726 GstPadTemplate *template;
2729 GstRTSPStream *stream;
2732 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2734 GST_RTSP_STATE_LOCK (src);
2736 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2737 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2738 goto unknown_stream;
2740 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2742 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2744 goto unknown_stream;
2747 stream->ssrc = ssrc;
2749 /* we'll add it later see below */
2750 stream->added = TRUE;
2752 /* check if we added all streams */
2754 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
2755 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
2757 GST_DEBUG_OBJECT (src, "stream %p, container %d, added %d, setup %d",
2758 ostream, ostream->container, ostream->added, ostream->setup);
2760 /* if we find a stream for which we did a setup that is not added, we
2761 * need to wait some more */
2762 if (ostream->setup && !ostream->added) {
2767 GST_RTSP_STATE_UNLOCK (src);
2769 /* create a new pad we will use to stream to */
2770 template = gst_static_pad_template_get (&rtptemplate);
2771 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2772 gst_object_unref (template);
2775 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2776 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2777 gst_pad_set_active (stream->srcpad, TRUE);
2778 gst_pad_sticky_events_foreach (pad, copy_sticky_events, stream->srcpad);
2779 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2782 GST_DEBUG_OBJECT (src, "We added all streams");
2783 /* when we get here, all stream are added and we can fire the no-more-pads
2785 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2793 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2794 GST_RTSP_STATE_UNLOCK (src);
2801 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
2805 len = stream->ptmap->len;
2806 for (i = 0; i < len; i++) {
2807 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
2815 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2817 GstRTSPStream *stream;
2820 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2822 GST_RTSP_STATE_LOCK (src);
2823 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2825 goto unknown_stream;
2827 if ((caps = stream_get_caps_for_pt (stream, pt)))
2828 gst_caps_ref (caps);
2829 GST_RTSP_STATE_UNLOCK (src);
2835 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2836 GST_RTSP_STATE_UNLOCK (src);
2842 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2844 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2850 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2856 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2862 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2864 GstRTSPSrc *src = stream->parent;
2867 g_object_get (source, "ssrc", &ssrc, NULL);
2869 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
2870 ssrc, stream->ssrc, stream->id);
2872 if (ssrc == stream->ssrc)
2873 gst_rtspsrc_do_stream_eos (src, stream);
2877 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2879 GstRTSPSrc *src = stream->parent;
2882 g_object_get (source, "ssrc", &ssrc, NULL);
2884 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
2885 ssrc, stream->ssrc, stream->id);
2887 if (ssrc == stream->ssrc)
2888 gst_rtspsrc_do_stream_eos (src, stream);
2892 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2894 GstRTSPStream *stream;
2896 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2898 /* get stream for session */
2899 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2901 gst_rtspsrc_do_stream_eos (src, stream);
2906 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2908 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2913 set_manager_buffer_mode (GstRTSPSrc * src)
2915 GObjectClass *klass;
2917 if (src->manager == NULL)
2920 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2922 if (!g_object_class_find_property (klass, "buffer-mode"))
2925 if (src->buffer_mode != BUFFER_MODE_AUTO) {
2926 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
2931 GST_DEBUG_OBJECT (src,
2932 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
2934 if (src->provided_clock) {
2935 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
2937 if (clock == src->provided_clock) {
2938 GST_DEBUG_OBJECT (src, "selected synced");
2939 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
2942 gst_object_unref (clock);
2947 /* Otherwise fall-through and use another buffer mode */
2949 gst_object_unref (clock);
2952 GST_DEBUG_OBJECT (src, "auto buffering mode");
2953 if (src->use_buffering) {
2954 GST_DEBUG_OBJECT (src, "selected buffer");
2955 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
2957 GST_DEBUG_OBJECT (src, "selected slave");
2958 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
2963 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
2965 GST_DEBUG ("request key %u", ssrc);
2966 return gst_caps_ref (stream_get_caps_for_pt (stream, stream->default_pt));
2970 request_rtp_decoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
2972 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
2973 if (stream->id != session)
2976 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
2977 stream->profile != GST_RTSP_PROFILE_SAVPF)
2980 if (stream->srtpdec == NULL) {
2983 name = g_strdup_printf ("srtpdec_%u", session);
2984 stream->srtpdec = gst_element_factory_make ("srtpdec", name);
2987 g_signal_connect (stream->srtpdec, "request-key",
2988 (GCallback) request_key, stream);
2990 return gst_object_ref (stream->srtpdec);
2994 request_rtcp_encoder (GstElement * rtpbin, guint session,
2995 GstRTSPStream * stream)
3000 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3001 if (stream->id != session)
3004 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3005 stream->profile != GST_RTSP_PROFILE_SAVPF)
3008 if (stream->srtpenc == NULL) {
3009 name = g_strdup_printf ("srtpenc_%u", session);
3010 stream->srtpenc = gst_element_factory_make ("srtpenc", name);
3013 /* key has been made before */
3014 g_object_set (stream->srtpenc, "key", stream->key, NULL);
3016 name = g_strdup_printf ("rtcp_sink_%d", session);
3017 pad = gst_element_get_request_pad (stream->srtpenc, name);
3019 gst_object_unref (pad);
3021 return gst_object_ref (stream->srtpenc);
3025 /* try to get and configure a manager */
3027 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
3028 GstRTSPTransport * transport)
3030 const gchar *manager;
3032 GstStateChangeReturn ret;
3034 /* find a manager */
3035 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
3039 GST_DEBUG_OBJECT (src, "using manager %s", manager);
3041 /* configure the manager */
3042 if (src->manager == NULL) {
3043 GObjectClass *klass;
3045 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
3047 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
3051 goto use_no_manager;
3053 if (!(src->manager = gst_element_factory_make (manager, "manager")))
3054 goto manager_failed;
3057 /* we manage this element */
3058 gst_element_set_locked_state (src->manager, TRUE);
3059 gst_bin_add (GST_BIN_CAST (src), src->manager);
3061 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
3062 if (ret == GST_STATE_CHANGE_FAILURE)
3063 goto start_manager_failure;
3065 g_object_set (src->manager, "latency", src->latency, NULL);
3067 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3069 if (g_object_class_find_property (klass, "ntp-sync")) {
3070 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
3073 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
3074 g_object_set (src->manager, "use-pipeline-clock",
3075 src->use_pipeline_clock, NULL);
3078 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
3079 g_object_set (src->manager, "sdes", src->sdes, NULL);
3082 if (g_object_class_find_property (klass, "drop-on-latency")) {
3083 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
3087 /* buffer mode pauses are handled by adding offsets to buffer times,
3088 * but some depayloaders may have a hard time syncing output times
3089 * with such input times, e.g. container ones, most notably ASF */
3090 /* TODO alternatives are having an event that indicates these shifts,
3091 * or having rtsp extensions provide suggestion on buffer mode */
3092 /* valid duration implies not likely live pipeline,
3093 * so slaving in jitterbuffer does not make much sense
3094 * (and might mess things up due to bursts) */
3095 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
3096 src->segment.duration && !stream->container) {
3097 src->use_buffering = TRUE;
3099 src->use_buffering = FALSE;
3102 set_manager_buffer_mode (src);
3104 /* connect to signals */
3105 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
3107 src->manager_sig_id =
3108 g_signal_connect (src->manager, "pad-added",
3109 (GCallback) new_manager_pad, src);
3110 src->manager_ptmap_id =
3111 g_signal_connect (src->manager, "request-pt-map",
3112 (GCallback) request_pt_map, src);
3114 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
3117 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
3120 g_signal_connect (src->manager, "request-rtp-decoder",
3121 (GCallback) request_rtp_decoder, stream);
3122 g_signal_connect (src->manager, "request-rtcp-decoder",
3123 (GCallback) request_rtp_decoder, stream);
3124 g_signal_connect (src->manager, "request-rtcp-encoder",
3125 (GCallback) request_rtcp_encoder, stream);
3127 /* we stream directly to the manager, get some pads. Each RTSP stream goes
3128 * into a separate RTP session. */
3129 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
3130 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
3132 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
3133 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
3136 /* now configure the bandwidth in the manager */
3137 if (g_signal_lookup ("get-internal-session",
3138 G_OBJECT_TYPE (src->manager)) != 0) {
3139 GObject *rtpsession;
3141 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
3144 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
3146 stream->session = rtpsession;
3148 if (stream->as_bandwidth != -1) {
3149 GST_INFO_OBJECT (src, "setting AS: %f",
3150 (gdouble) (stream->as_bandwidth * 1000));
3151 g_object_set (rtpsession, "bandwidth",
3152 (gdouble) (stream->as_bandwidth * 1000), NULL);
3154 if (stream->rr_bandwidth != -1) {
3155 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
3156 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
3159 if (stream->rs_bandwidth != -1) {
3160 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
3161 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
3165 g_object_set (rtpsession, "probation", src->probation, NULL);
3167 g_object_set (rtpsession, "internal-ssrc", stream->send_ssrc, NULL);
3169 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
3171 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
3173 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
3175 g_signal_connect (rtpsession, "on-ssrc-active",
3176 (GCallback) on_ssrc_active, stream);
3187 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3192 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
3195 start_manager_failure:
3197 GST_DEBUG_OBJECT (src, "could not start session manager");
3202 /* free the UDP sources allocated when negotiating a transport.
3203 * This function is called when the server negotiated to a transport where the
3204 * UDP sources are not needed anymore, such as TCP or multicast. */
3206 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
3210 for (i = 0; i < 2; i++) {
3211 if (stream->udpsrc[i]) {
3212 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
3213 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
3214 gst_object_unref (stream->udpsrc[i]);
3215 stream->udpsrc[i] = NULL;
3220 /* for TCP, create pads to send and receive data to and from the manager and to
3221 * intercept various events and queries
3224 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
3225 GstRTSPTransport * transport, GstPad ** outpad)
3228 GstPadTemplate *template;
3229 GstPad *pad0, *pad1;
3231 /* configure for interleaved delivery, nothing needs to be done
3232 * here, the loop function will call the chain functions of the
3233 * session manager. */
3234 stream->channel[0] = transport->interleaved.min;
3235 stream->channel[1] = transport->interleaved.max;
3236 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
3237 stream->channel[0], stream->channel[1]);
3239 /* we can remove the allocated UDP ports now */
3240 gst_rtspsrc_stream_free_udp (stream);
3242 /* no session manager, send data to srcpad directly */
3243 if (!stream->channelpad[0]) {
3244 GST_DEBUG_OBJECT (src, "no manager, creating pad");
3246 /* create a new pad we will use to stream to */
3247 name = g_strdup_printf ("stream_%u", stream->id);
3248 template = gst_static_pad_template_get (&rtptemplate);
3249 stream->channelpad[0] = gst_pad_new_from_template (template, name);
3250 gst_object_unref (template);
3253 /* set caps and activate */
3254 gst_pad_use_fixed_caps (stream->channelpad[0]);
3255 gst_pad_set_active (stream->channelpad[0], TRUE);
3257 *outpad = gst_object_ref (stream->channelpad[0]);
3259 GST_DEBUG_OBJECT (src, "using manager source pad");
3261 template = gst_static_pad_template_get (&anysrctemplate);
3263 /* allocate pads for sending the channel data into the manager */
3264 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
3265 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
3266 gst_object_unref (stream->channelpad[0]);
3267 stream->channelpad[0] = pad0;
3268 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
3269 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
3270 gst_pad_set_element_private (pad0, src);
3271 gst_pad_set_active (pad0, TRUE);
3273 if (stream->channelpad[1]) {
3274 /* if we have a sinkpad for the other channel, create a pad and link to the
3276 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
3277 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
3278 gst_pad_link_full (pad1, stream->channelpad[1],
3279 GST_PAD_LINK_CHECK_NOTHING);
3280 gst_object_unref (stream->channelpad[1]);
3281 stream->channelpad[1] = pad1;
3282 gst_pad_set_active (pad1, TRUE);
3284 gst_object_unref (template);
3286 /* setup RTCP transport back to the server if we have to. */
3287 if (src->manager && src->do_rtcp) {
3290 template = gst_static_pad_template_get (&anysinktemplate);
3292 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
3293 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
3294 gst_pad_set_element_private (stream->rtcppad, stream);
3295 gst_pad_set_active (stream->rtcppad, TRUE);
3297 /* get session RTCP pad */
3298 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3299 pad = gst_element_get_request_pad (src->manager, name);
3304 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3305 gst_object_unref (pad);
3308 gst_object_unref (template);
3314 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
3315 GstRTSPTransport * transport, const gchar ** destination, gint * min,
3316 gint * max, guint * ttl)
3318 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3320 if (!(*destination = transport->destination))
3321 *destination = stream->destination;
3324 /* transport first */
3325 *min = transport->port.min;
3326 *max = transport->port.max;
3327 if (*min == -1 && *max == -1) {
3328 /* then try from SDP */
3329 if (stream->port != 0) {
3330 *min = stream->port;
3331 *max = stream->port + 1;
3337 if (!(*ttl = transport->ttl))
3342 /* first take the source, then the endpoint to figure out where to send
3344 if (!(*destination = transport->source)) {
3345 if (src->conninfo.connection)
3346 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3347 else if (stream->conninfo.connection)
3349 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3353 /* for unicast we only expect the ports here */
3354 *min = transport->server_port.min;
3355 *max = transport->server_port.max;
3360 /* For multicast create UDP sources and join the multicast group. */
3362 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3363 GstRTSPTransport * transport, GstPad ** outpad)
3366 const gchar *destination;
3369 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3371 /* we can remove the allocated UDP ports now */
3372 gst_rtspsrc_stream_free_udp (stream);
3374 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3377 /* we need a destination now */
3378 if (destination == NULL)
3379 goto no_destination;
3381 /* we really need ports now or we won't be able to receive anything at all */
3382 if (min == -1 && max == -1)
3385 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3386 destination, min, max);
3388 /* creating UDP source for RTP */
3390 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3392 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3394 if (stream->udpsrc[0] == NULL)
3397 /* take ownership */
3398 gst_object_ref_sink (stream->udpsrc[0]);
3400 if (src->udp_buffer_size != 0)
3401 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3402 src->udp_buffer_size, NULL);
3404 if (src->multi_iface != NULL)
3405 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3406 src->multi_iface, NULL);
3409 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3410 gst_element_set_state (stream->udpsrc[0], GST_STATE_READY);
3413 /* creating another UDP source for RTCP */
3417 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3419 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3421 if (stream->udpsrc[1] == NULL)
3424 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3425 stream->profile == GST_RTSP_PROFILE_SAVPF)
3426 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3428 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3429 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3430 gst_caps_unref (caps);
3432 /* take ownership */
3433 gst_object_ref_sink (stream->udpsrc[1]);
3435 if (src->multi_iface != NULL)
3436 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3437 src->multi_iface, NULL);
3439 gst_element_set_state (stream->udpsrc[1], GST_STATE_READY);
3446 GST_DEBUG_OBJECT (src, "no UDP source element found");
3451 GST_DEBUG_OBJECT (src, "no destination found");
3456 GST_DEBUG_OBJECT (src, "no ports found");
3461 /* configure the remainder of the UDP ports */
3463 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3464 GstRTSPTransport * transport, GstPad ** outpad)
3466 /* we manage the UDP elements now. For unicast, the UDP sources where
3467 * allocated in the stream when we suggested a transport. */
3468 if (stream->udpsrc[0]) {
3471 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3472 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3474 GST_DEBUG_OBJECT (src, "setting up UDP source");
3476 /* configure a timeout on the UDP port. When the timeout message is
3477 * posted, we assume UDP transport is not possible. We reconnect using TCP
3479 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3480 src->udp_timeout * 1000, NULL);
3482 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt)))
3483 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
3485 /* get output pad of the UDP source. */
3486 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3488 /* save it so we can unblock */
3489 stream->blockedpad = *outpad;
3491 /* configure pad block on the pad. As soon as there is dataflow on the
3492 * UDP source, we know that UDP is not blocked by a firewall and we can
3493 * configure all the streams to let the application autoplug decoders. */
3495 gst_pad_add_probe (stream->blockedpad,
3496 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3497 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocked, src, NULL);
3499 if (stream->channelpad[0]) {
3500 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3501 /* configure for UDP delivery, we need to connect the UDP pads to
3502 * the session plugin. */
3503 gst_pad_link_full (*outpad, stream->channelpad[0],
3504 GST_PAD_LINK_CHECK_NOTHING);
3505 gst_object_unref (*outpad);
3507 /* we connected to pad-added signal to get pads from the manager */
3509 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3514 if (stream->udpsrc[1]) {
3517 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3518 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3520 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3521 stream->profile == GST_RTSP_PROFILE_SAVPF)
3522 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3524 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3525 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3526 gst_caps_unref (caps);
3528 if (stream->channelpad[1]) {
3531 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3533 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3534 gst_pad_link_full (pad, stream->channelpad[1],
3535 GST_PAD_LINK_CHECK_NOTHING);
3536 gst_object_unref (pad);
3538 /* leave unlinked */
3544 /* configure the UDP sink back to the server for status reports */
3546 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3547 GstRTSPStream * stream, GstRTSPTransport * transport)
3550 gint rtp_port, rtcp_port;
3551 gboolean do_rtp, do_rtcp;
3552 const gchar *destination;
3557 /* get transport info */
3558 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
3559 &rtp_port, &rtcp_port, &ttl);
3561 /* see what we need to do */
3562 do_rtp = (rtp_port != -1);
3563 /* it's possible that the server does not want us to send RTCP in which case
3565 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
3567 /* we need a destination when we have RTP or RTCP ports */
3568 if (destination == NULL && (do_rtp || do_rtcp))
3569 goto no_destination;
3571 /* try to construct the fakesrc to the RTP port of the server to open up any
3574 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
3577 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
3578 stream->udpsink[0] =
3579 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3581 if (stream->udpsink[0] == NULL)
3582 goto no_sink_element;
3584 /* don't join multicast group, we will have the source socket do that */
3585 /* no sync or async state changes needed */
3586 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
3587 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3589 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3591 if (stream->udpsrc[0]) {
3592 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
3593 * so that NAT firewalls will open a hole for us */
3594 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
3595 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
3596 /* configure socket and make sure udpsink does not close it when shutting
3597 * down, it belongs to udpsrc after all. */
3598 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
3599 "close-socket", FALSE, NULL);
3600 g_object_unref (socket);
3603 /* the source for the dummy packets to open up NAT */
3604 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
3605 if (stream->fakesrc == NULL)
3606 goto no_fakesrc_element;
3608 /* random data in 5 buffers, a size of 200 bytes should be fine */
3609 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
3610 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
3612 /* we don't want to consider this a sink */
3613 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
3615 /* keep everything locked */
3616 gst_element_set_locked_state (stream->udpsink[0], TRUE);
3617 gst_element_set_locked_state (stream->fakesrc, TRUE);
3619 gst_object_ref (stream->udpsink[0]);
3620 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
3621 gst_object_ref (stream->fakesrc);
3622 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
3624 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
3625 "sink", GST_PAD_LINK_CHECK_NOTHING);
3628 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
3631 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
3632 stream->udpsink[1] =
3633 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3635 if (stream->udpsink[1] == NULL)
3636 goto no_sink_element;
3638 /* don't join multicast group, we will have the source socket do that */
3639 /* no sync or async state changes needed */
3640 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
3641 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3643 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3645 if (stream->udpsrc[1]) {
3646 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
3647 * because some servers check the port number of where it sends RTCP to identify
3648 * the RTCP packets it receives */
3649 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
3650 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
3651 /* configure socket and make sure udpsink does not close it when shutting
3652 * down, it belongs to udpsrc after all. */
3653 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
3654 "close-socket", FALSE, NULL);
3655 g_object_unref (socket);
3658 /* we don't want to consider this a sink */
3659 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
3661 /* we keep this playing always */
3662 gst_element_set_locked_state (stream->udpsink[1], TRUE);
3663 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
3665 gst_object_ref (stream->udpsink[1]);
3666 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
3668 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
3670 /* get session RTCP pad */
3671 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3672 pad = gst_element_get_request_pad (src->manager, name);
3677 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3678 gst_object_unref (pad);
3687 GST_DEBUG_OBJECT (src, "no destination address specified");
3692 GST_DEBUG_OBJECT (src, "no UDP sink element found");
3697 GST_DEBUG_OBJECT (src, "no fakesrc element found");
3702 /* sets up all elements needed for streaming over the specified transport.
3703 * Does not yet expose the element pads, this will be done when there is actuall
3704 * dataflow detected, which might never happen when UDP is blocked in a
3705 * firewall, for example.
3708 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
3709 GstRTSPTransport * transport)
3712 GstPad *outpad = NULL;
3713 GstPadTemplate *template;
3715 const gchar *media_type;
3718 src = stream->parent;
3720 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
3722 /* get the proper media type for this stream now */
3723 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
3724 goto unknown_transport;
3726 goto unknown_transport;
3728 /* configure the final media type */
3729 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
3731 len = stream->ptmap->len;
3732 for (i = 0; i < len; i++) {
3734 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3736 if (item->caps == NULL)
3739 s = gst_caps_get_structure (item->caps, 0);
3740 gst_structure_set_name (s, media_type);
3741 /* set ssrc if known */
3742 if (transport->ssrc)
3743 gst_structure_set (s, "ssrc", G_TYPE_UINT, transport->ssrc, NULL);
3746 /* try to get and configure a manager, channelpad[0-1] will be configured with
3747 * the pads for the manager, or NULL when no manager is needed. */
3748 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
3751 switch (transport->lower_transport) {
3752 case GST_RTSP_LOWER_TRANS_TCP:
3753 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
3754 goto transport_failed;
3756 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3757 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
3758 goto transport_failed;
3759 /* fallthrough, the rest is the same for UDP and MCAST */
3760 case GST_RTSP_LOWER_TRANS_UDP:
3761 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
3762 goto transport_failed;
3763 /* configure udpsinks back to the server for RTCP messages and for the
3764 * dummy RTP messages to open NAT. */
3765 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
3766 goto transport_failed;
3769 goto unknown_transport;
3773 GST_DEBUG_OBJECT (src, "creating ghostpad");
3775 gst_pad_use_fixed_caps (outpad);
3777 /* create ghostpad, don't add just yet, this will be done when we activate
3779 name = g_strdup_printf ("stream_%u", stream->id);
3780 template = gst_static_pad_template_get (&rtptemplate);
3781 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
3782 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3783 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3784 gst_object_unref (template);
3787 gst_object_unref (outpad);
3789 /* mark pad as ok */
3790 stream->last_ret = GST_FLOW_OK;
3797 GST_DEBUG_OBJECT (src, "failed to configure transport");
3802 GST_DEBUG_OBJECT (src, "unknown transport");
3807 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3812 /* send a couple of dummy random packets on the receiver RTP port to the server,
3813 * this should make a firewall think we initiated the data transfer and
3814 * hopefully allow packets to go from the sender port to our RTP receiver port */
3816 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
3820 if (src->nat_method != GST_RTSP_NAT_DUMMY)
3823 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3824 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3826 if (stream->fakesrc && stream->udpsink[0]) {
3827 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
3828 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
3829 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
3830 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
3831 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
3837 /* Adds the source pads of all configured streams to the element.
3838 * This code is performed when we detected dataflow.
3840 * We detect dataflow from either the _loop function or with pad probes on the
3844 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
3848 GST_DEBUG_OBJECT (src, "activating streams");
3850 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3851 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3853 if (stream->udpsrc[0]) {
3854 /* remove timeout, we are streaming now and timeouts will be handled by
3855 * the session manager and jitter buffer */
3856 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
3858 if (stream->srcpad) {
3859 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
3860 gst_pad_set_active (stream->srcpad, TRUE);
3862 /* if we don't have a session manager, set the caps now. If we have a
3863 * session, we will get a notification of the pad and the caps. */
3864 if (!src->manager) {
3867 caps = stream_get_caps_for_pt (stream, stream->default_pt);
3868 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
3869 gst_pad_set_caps (stream->srcpad, caps);
3872 if (!stream->added) {
3873 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
3874 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3875 stream->added = TRUE;
3880 /* unblock all pads */
3881 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3882 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3884 if (stream->blockid) {
3885 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
3886 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
3887 stream->blockid = 0;
3895 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
3896 gboolean reset_manager)
3899 guint64 start, stop;
3900 gdouble play_speed, play_scale;
3902 GST_DEBUG_OBJECT (src, "configuring stream caps");
3904 start = segment->position;
3905 stop = segment->duration;
3906 play_speed = segment->rate;
3907 play_scale = segment->applied_rate;
3909 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3910 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3916 len = stream->ptmap->len;
3917 for (j = 0; j < len; j++) {
3919 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
3921 if (item->caps == NULL)
3924 caps = gst_caps_make_writable (item->caps);
3926 if (stream->timebase != -1)
3927 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
3928 (guint) stream->timebase, NULL);
3929 if (stream->seqbase != -1)
3930 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
3931 (guint) stream->seqbase, NULL);
3932 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
3934 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
3935 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
3936 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
3939 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
3942 if (item->pt == stream->default_pt && stream->udpsrc[0]) {
3943 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
3947 if (reset_manager && src->manager) {
3948 GST_DEBUG_OBJECT (src, "clear session");
3949 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
3953 static GstFlowReturn
3954 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
3959 /* store the value */
3960 stream->last_ret = ret;
3962 /* if it's success we can return the value right away */
3963 if (ret == GST_FLOW_OK)
3966 /* any other error that is not-linked can be returned right
3968 if (ret != GST_FLOW_NOT_LINKED)
3971 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3972 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3973 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3975 ret = ostream->last_ret;
3976 /* some other return value (must be SUCCESS but we can return
3977 * other values as well) */
3978 if (ret != GST_FLOW_NOT_LINKED)
3981 /* if we get here, all other pads were unlinked and we return
3982 * NOT_LINKED then */
3988 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
3991 gboolean res = TRUE;
3993 /* only streams that have a connection to the outside world */
3997 if (stream->udpsrc[0]) {
3998 gst_event_ref (event);
3999 res = gst_element_send_event (stream->udpsrc[0], event);
4000 } else if (stream->channelpad[0]) {
4001 gst_event_ref (event);
4002 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4003 res = gst_pad_push_event (stream->channelpad[0], event);
4005 res = gst_pad_send_event (stream->channelpad[0], event);
4008 if (stream->udpsrc[1]) {
4009 gst_event_ref (event);
4010 res &= gst_element_send_event (stream->udpsrc[1], event);
4011 } else if (stream->channelpad[1]) {
4012 gst_event_ref (event);
4013 if (GST_PAD_IS_SRC (stream->channelpad[1]))
4014 res &= gst_pad_push_event (stream->channelpad[1], event);
4016 res &= gst_pad_send_event (stream->channelpad[1], event);
4020 gst_event_unref (event);
4026 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
4029 gboolean res = TRUE;
4031 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4032 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4034 gst_event_ref (event);
4035 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
4037 gst_event_unref (event);
4042 static GstRTSPResult
4043 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4048 if (info->connection == NULL) {
4049 if (info->url == NULL) {
4050 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
4051 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
4055 /* create connection */
4056 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
4057 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
4058 goto could_not_create;
4061 g_free (info->url_str);
4062 info->url_str = gst_rtsp_url_get_request_uri (info->url);
4064 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
4066 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
4067 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
4068 src->tls_validation_flags))
4069 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
4071 if (src->tls_database)
4072 gst_rtsp_connection_set_tls_database (info->connection,
4076 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
4077 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
4079 if (src->proxy_host) {
4080 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
4082 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
4087 if (!info->connected) {
4090 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
4091 ("Connecting to %s", info->location));
4092 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
4094 gst_rtsp_connection_connect (info->connection,
4095 src->ptcp_timeout)) < 0)
4096 goto could_not_connect;
4098 info->connected = TRUE;
4105 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
4110 gchar *str = gst_rtsp_strresult (res);
4111 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
4117 gchar *str = gst_rtsp_strresult (res);
4118 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
4124 static GstRTSPResult
4125 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
4128 GST_RTSP_STATE_LOCK (src);
4129 if (info->connected) {
4130 GST_DEBUG_OBJECT (src, "closing connection...");
4131 gst_rtsp_connection_close (info->connection);
4132 info->connected = FALSE;
4134 if (free && info->connection) {
4135 /* free connection */
4136 GST_DEBUG_OBJECT (src, "freeing connection...");
4137 gst_rtsp_connection_free (info->connection);
4138 info->connection = NULL;
4140 GST_RTSP_STATE_UNLOCK (src);
4144 static GstRTSPResult
4145 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4150 GST_DEBUG_OBJECT (src, "reconnecting connection...");
4151 gst_rtsp_conninfo_close (src, info, FALSE);
4152 res = gst_rtsp_conninfo_connect (src, info, async);
4158 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
4162 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
4163 GST_RTSP_STATE_LOCK (src);
4164 if (src->conninfo.connection && src->conninfo.flushing != flush) {
4165 GST_DEBUG_OBJECT (src, "connection flush");
4166 gst_rtsp_connection_flush (src->conninfo.connection, flush);
4167 src->conninfo.flushing = flush;
4169 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4170 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4171 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
4172 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
4173 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
4174 stream->conninfo.flushing = flush;
4177 GST_RTSP_STATE_UNLOCK (src);
4180 /* FIXME, handle server request, reply with OK, for now */
4181 static GstRTSPResult
4182 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
4183 GstRTSPMessage * request)
4185 GstRTSPMessage response = { 0 };
4188 GST_DEBUG_OBJECT (src, "got server request message");
4191 gst_rtsp_message_dump (request);
4193 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
4195 if (res == GST_RTSP_ENOTIMPL) {
4196 /* default implementation, send OK */
4197 GST_DEBUG_OBJECT (src, "prepare OK reply");
4199 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
4204 /* let app parse and reply */
4205 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
4206 0, request, &response);
4209 gst_rtsp_message_dump (&response);
4211 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
4215 gst_rtsp_message_unset (&response);
4216 } else if (res == GST_RTSP_EEOF)
4224 gst_rtsp_message_unset (&response);
4229 /* send server keep-alive */
4230 static GstRTSPResult
4231 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
4233 GstRTSPMessage request = { 0 };
4235 GstRTSPMethod method;
4236 const gchar *control;
4238 if (src->do_rtsp_keep_alive == FALSE) {
4239 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
4240 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4244 GST_DEBUG_OBJECT (src, "creating server keep-alive");
4246 /* find a method to use for keep-alive */
4247 if (src->methods & GST_RTSP_GET_PARAMETER)
4248 method = GST_RTSP_GET_PARAMETER;
4250 method = GST_RTSP_OPTIONS;
4252 control = get_aggregate_control (src);
4253 if (control == NULL)
4256 res = gst_rtsp_message_init_request (&request, method, control);
4261 gst_rtsp_message_dump (&request);
4264 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
4269 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4270 gst_rtsp_message_unset (&request);
4277 GST_WARNING_OBJECT (src, "no control url to send keepalive");
4282 gchar *str = gst_rtsp_strresult (res);
4284 gst_rtsp_message_unset (&request);
4285 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
4286 ("Could not send keep-alive. (%s)", str));
4292 static GstFlowReturn
4293 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
4295 GstFlowReturn ret = GST_FLOW_OK;
4297 GstRTSPStream *stream;
4298 GstPad *outpad = NULL;
4305 channel = message->type_data.data.channel;
4307 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
4309 goto unknown_stream;
4311 if (channel == stream->channel[0]) {
4312 outpad = stream->channelpad[0];
4314 } else if (channel == stream->channel[1]) {
4315 outpad = stream->channelpad[1];
4321 /* take a look at the body to figure out what we have */
4322 gst_rtsp_message_get_body (message, &data, &size);
4324 goto invalid_length;
4326 /* channels are not correct on some servers, do extra check */
4327 if (data[1] >= 200 && data[1] <= 204) {
4328 /* hmm RTCP message switch to the RTCP pad of the same stream. */
4329 outpad = stream->channelpad[1];
4333 /* we have no clue what this is, just ignore then. */
4335 goto unknown_stream;
4337 /* take the message body for further processing */
4338 gst_rtsp_message_steal_body (message, &data, &size);
4340 /* strip the trailing \0 */
4343 buf = gst_buffer_new ();
4344 gst_buffer_append_memory (buf,
4345 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
4347 /* don't need message anymore */
4348 gst_rtsp_message_unset (message);
4350 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
4353 if (src->need_activate) {
4359 guint group_id = gst_util_group_id_next ();
4361 /* generate an SHA256 sum of the URI */
4362 cs = g_checksum_new (G_CHECKSUM_SHA256);
4363 uri = src->conninfo.location;
4364 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
4366 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4367 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4370 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
4371 event = gst_event_new_stream_start (stream_id);
4372 gst_event_set_group_id (event, group_id);
4375 gst_rtspsrc_stream_push_event (src, ostream, event);
4377 g_checksum_free (cs);
4379 gst_rtspsrc_activate_streams (src);
4380 src->need_activate = FALSE;
4382 if ((event = src->start_segment) != NULL) {
4383 src->start_segment = NULL;
4384 gst_rtspsrc_push_event (src, event);
4387 if (src->base_time == -1) {
4388 /* Take current running_time. This timestamp will be put on
4389 * the first buffer of each stream because we are a live source and so we
4390 * timestamp with the running_time. When we are dealing with TCP, we also
4391 * only timestamp the first buffer (using the DISCONT flag) because a server
4392 * typically bursts data, for which we don't want to compensate by speeding
4393 * up the media. The other timestamps will be interpollated from this one
4394 * using the RTP timestamps. */
4395 GST_OBJECT_LOCK (src);
4396 if (GST_ELEMENT_CLOCK (src)) {
4398 GstClockTime base_time;
4400 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
4401 base_time = GST_ELEMENT_CAST (src)->base_time;
4403 src->base_time = now - base_time;
4405 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
4406 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
4408 GST_OBJECT_UNLOCK (src);
4411 if (stream->discont && !is_rtcp) {
4412 /* mark first RTP buffer as discont */
4413 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4414 stream->discont = FALSE;
4415 /* first buffer gets the timestamp, other buffers are not timestamped and
4416 * their presentation time will be interpollated from the rtp timestamps. */
4417 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
4418 GST_TIME_ARGS (src->base_time));
4420 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
4423 /* chain to the peer pad */
4424 if (GST_PAD_IS_SINK (outpad))
4425 ret = gst_pad_chain (outpad, buf);
4427 ret = gst_pad_push (outpad, buf);
4430 /* combine all stream flows for the data transport */
4431 ret = gst_rtspsrc_combine_flows (src, stream, ret);
4438 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
4439 gst_rtsp_message_unset (message);
4444 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4445 ("Short message received, ignoring."));
4446 gst_rtsp_message_unset (message);
4451 static GstFlowReturn
4452 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
4454 GstRTSPMessage message = { 0 };
4456 GstFlowReturn ret = GST_FLOW_OK;
4457 GTimeVal tv_timeout;
4460 /* get the next timeout interval */
4461 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4463 /* see if the timeout period expired */
4464 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
4465 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
4466 /* send keep-alive, only act on interrupt, a warning will be posted for
4468 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4470 /* get new timeout */
4471 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4474 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
4475 tv_timeout.tv_sec, tv_timeout.tv_usec);
4477 /* protect the connection with the connection lock so that we can see when
4478 * we are finished doing server communication */
4480 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4481 &message, src->ptcp_timeout);
4485 GST_DEBUG_OBJECT (src, "we received a server message");
4487 case GST_RTSP_EINTR:
4488 /* we got interrupted this means we need to stop */
4490 case GST_RTSP_ETIMEOUT:
4491 /* no reply, send keep alive */
4492 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4493 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4497 /* go EOS when the server closed the connection */
4503 switch (message.type) {
4504 case GST_RTSP_MESSAGE_REQUEST:
4505 /* server sends us a request message, handle it */
4507 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4509 if (res == GST_RTSP_EEOF)
4512 goto handle_request_failed;
4514 case GST_RTSP_MESSAGE_RESPONSE:
4515 /* we ignore response messages */
4516 GST_DEBUG_OBJECT (src, "ignoring response message");
4518 gst_rtsp_message_dump (&message);
4520 case GST_RTSP_MESSAGE_DATA:
4521 GST_DEBUG_OBJECT (src, "got data message");
4522 ret = gst_rtspsrc_handle_data (src, &message);
4523 if (ret != GST_FLOW_OK)
4524 goto handle_data_failed;
4527 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4532 g_assert_not_reached ();
4537 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4538 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4539 ("The server closed the connection."));
4540 src->conninfo.connected = FALSE;
4541 gst_rtsp_message_unset (&message);
4542 return GST_FLOW_EOS;
4546 gst_rtsp_message_unset (&message);
4547 GST_DEBUG_OBJECT (src, "got interrupted");
4548 return GST_FLOW_FLUSHING;
4552 gchar *str = gst_rtsp_strresult (res);
4554 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4555 ("Could not receive message. (%s)", str));
4558 gst_rtsp_message_unset (&message);
4559 return GST_FLOW_ERROR;
4561 handle_request_failed:
4563 gchar *str = gst_rtsp_strresult (res);
4565 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4566 ("Could not handle server message. (%s)", str));
4568 gst_rtsp_message_unset (&message);
4569 return GST_FLOW_ERROR;
4573 GST_DEBUG_OBJECT (src, "could no handle data message");
4578 static GstFlowReturn
4579 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
4582 GstRTSPMessage message = { 0 };
4586 GTimeVal tv_timeout;
4588 /* get the next timeout interval */
4589 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4591 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
4592 (gint) tv_timeout.tv_sec);
4594 gst_rtsp_message_unset (&message);
4596 /* we should continue reading the TCP socket because the server might
4597 * send us requests. When the session timeout expires, we need to send a
4598 * keep-alive request to keep the session open. */
4599 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4600 &message, &tv_timeout);
4604 GST_DEBUG_OBJECT (src, "we received a server message");
4606 case GST_RTSP_EINTR:
4607 /* we got interrupted, see what we have to do */
4609 case GST_RTSP_ETIMEOUT:
4610 /* send keep-alive, ignore the result, a warning will be posted. */
4611 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4612 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4616 /* server closed the connection. not very fatal for UDP, reconnect and
4617 * see what happens. */
4618 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4619 ("The server closed the connection."));
4620 if (src->udp_reconnect) {
4622 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
4629 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
4631 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4632 ("Unhandled return value %d.", res));
4636 switch (message.type) {
4637 case GST_RTSP_MESSAGE_REQUEST:
4638 /* server sends us a request message, handle it */
4640 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4642 if (res == GST_RTSP_EEOF)
4645 goto handle_request_failed;
4647 case GST_RTSP_MESSAGE_RESPONSE:
4648 /* we ignore response and data messages */
4649 GST_DEBUG_OBJECT (src, "ignoring response message");
4651 gst_rtsp_message_dump (&message);
4652 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4653 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
4654 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
4655 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
4656 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4663 case GST_RTSP_MESSAGE_DATA:
4664 /* we ignore response and data messages */
4665 GST_DEBUG_OBJECT (src, "ignoring data message");
4668 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4673 g_assert_not_reached ();
4675 /* we get here when the connection got interrupted */
4678 gst_rtsp_message_unset (&message);
4679 GST_DEBUG_OBJECT (src, "got interrupted");
4680 return GST_FLOW_FLUSHING;
4684 gchar *str = gst_rtsp_strresult (res);
4687 src->conninfo.connected = FALSE;
4688 if (res != GST_RTSP_EINTR) {
4689 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
4690 ("Could not connect to server. (%s)", str));
4692 ret = GST_FLOW_ERROR;
4694 ret = GST_FLOW_FLUSHING;
4700 gchar *str = gst_rtsp_strresult (res);
4702 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4703 ("Could not receive message. (%s)", str));
4705 return GST_FLOW_ERROR;
4707 handle_request_failed:
4709 gchar *str = gst_rtsp_strresult (res);
4712 gst_rtsp_message_unset (&message);
4713 if (res != GST_RTSP_EINTR) {
4714 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4715 ("Could not handle server message. (%s)", str));
4717 ret = GST_FLOW_ERROR;
4719 ret = GST_FLOW_FLUSHING;
4725 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4726 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4727 ("The server closed the connection."));
4728 src->conninfo.connected = FALSE;
4729 gst_rtsp_message_unset (&message);
4730 return GST_FLOW_EOS;
4734 static GstRTSPResult
4735 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
4737 GstRTSPResult res = GST_RTSP_OK;
4740 GST_DEBUG_OBJECT (src, "doing reconnect");
4742 GST_OBJECT_LOCK (src);
4743 /* only restart when the pads were not yet activated, else we were
4744 * streaming over UDP */
4745 restart = src->need_activate;
4746 GST_OBJECT_UNLOCK (src);
4748 /* no need to restart, we're done */
4752 /* we can try only TCP now */
4753 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
4755 /* close and cleanup our state */
4756 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
4759 /* see if we have TCP left to try. Also don't try TCP when we were configured
4761 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
4764 /* We post a warning message now to inform the user
4765 * that nothing happened. It's most likely a firewall thing. */
4766 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4767 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4768 "firewall is blocking it. Retrying using a TCP connection.",
4769 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4771 /* open new connection using tcp */
4772 if (gst_rtspsrc_open (src, async) < 0)
4775 /* start playback */
4776 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
4785 src->cur_protocols = 0;
4786 /* no transport possible, post an error and stop */
4787 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4788 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4789 "firewall is blocking it. No other protocols to try.",
4790 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4791 return GST_RTSP_ERROR;
4795 GST_DEBUG_OBJECT (src, "open failed");
4800 GST_DEBUG_OBJECT (src, "play failed");
4806 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
4810 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
4813 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
4816 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
4819 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
4827 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
4831 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
4834 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
4837 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
4840 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
4848 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
4852 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
4855 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
4858 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
4861 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
4869 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
4873 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
4876 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
4879 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
4882 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
4890 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
4892 if (ret == GST_RTSP_OK)
4893 gst_rtspsrc_loop_complete_cmd (src, cmd);
4894 else if (ret == GST_RTSP_EINTR)
4895 gst_rtspsrc_loop_cancel_cmd (src, cmd);
4897 gst_rtspsrc_loop_error_cmd (src, cmd);
4901 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
4904 gboolean flushed = FALSE;
4906 /* start new request */
4907 gst_rtspsrc_loop_start_cmd (src, cmd);
4909 GST_DEBUG_OBJECT (src, "sending cmd %d", cmd);
4911 GST_OBJECT_LOCK (src);
4912 old = src->pending_cmd;
4913 if (old == CMD_RECONNECT) {
4914 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
4915 cmd = CMD_RECONNECT;
4917 if (old != CMD_WAIT) {
4918 src->pending_cmd = CMD_WAIT;
4919 GST_OBJECT_UNLOCK (src);
4920 /* cancel previous request */
4921 GST_DEBUG_OBJECT (src, "cancel previous request %d", old);
4922 gst_rtspsrc_loop_cancel_cmd (src, old);
4923 GST_OBJECT_LOCK (src);
4925 src->pending_cmd = cmd;
4926 /* interrupt if allowed */
4927 if (src->busy_cmd & mask) {
4928 GST_DEBUG_OBJECT (src, "connection flush busy %d", src->busy_cmd);
4929 gst_rtspsrc_connection_flush (src, TRUE);
4932 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %d", src->busy_cmd);
4935 gst_task_start (src->task);
4936 GST_OBJECT_UNLOCK (src);
4942 gst_rtspsrc_loop (GstRTSPSrc * src)
4946 if (!src->conninfo.connection || !src->conninfo.connected)
4949 if (src->interleaved)
4950 ret = gst_rtspsrc_loop_interleaved (src);
4952 ret = gst_rtspsrc_loop_udp (src);
4954 if (ret != GST_FLOW_OK)
4962 GST_WARNING_OBJECT (src, "we are not connected");
4963 ret = GST_FLOW_FLUSHING;
4968 const gchar *reason = gst_flow_get_name (ret);
4970 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
4971 src->running = FALSE;
4972 if (ret == GST_FLOW_EOS) {
4973 /* perform EOS logic */
4974 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4975 gst_element_post_message (GST_ELEMENT_CAST (src),
4976 gst_message_new_segment_done (GST_OBJECT_CAST (src),
4977 src->segment.format, src->segment.position));
4978 gst_rtspsrc_push_event (src,
4979 gst_event_new_segment_done (src->segment.format,
4980 src->segment.position));
4982 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4984 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
4985 /* for fatal errors we post an error message, post the error before the
4986 * EOS so the app knows about the error first. */
4987 GST_ELEMENT_ERROR (src, STREAM, FAILED,
4988 ("Internal data flow error."),
4989 ("streaming task paused, reason %s (%d)", reason, ret));
4990 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4992 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
4997 #ifndef GST_DISABLE_GST_DEBUG
4998 static const gchar *
4999 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
5003 while (method != 0) {
5020 static const gchar *
5021 gst_rtspsrc_skip_lws (const gchar * s)
5023 while (g_ascii_isspace (*s))
5028 static const gchar *
5029 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
5031 while (s > start && g_ascii_isspace (*(s - 1)))
5036 static const gchar *
5037 gst_rtspsrc_skip_commas (const gchar * s)
5039 /* The grammar allows for multiple commas */
5040 while (g_ascii_isspace (*s) || *s == ',')
5045 static const gchar *
5046 gst_rtspsrc_skip_item (const gchar * s)
5048 gboolean quoted = FALSE;
5049 const gchar *start = s;
5051 /* A list item ends at the last non-whitespace character
5052 * before a comma which is not inside a quoted-string. Or at
5053 * the end of the string.
5059 if (*s == '\\' && *(s + 1))
5068 return gst_rtspsrc_unskip_lws (s, start);
5072 gst_rtsp_decode_quoted_string (gchar * quoted_string)
5076 src = quoted_string + 1;
5077 dst = quoted_string;
5078 while (*src && *src != '"') {
5079 if (*src == '\\' && *(src + 1))
5086 /* Extract the authentication tokens that the server provided for each method
5087 * into an array of structures and give those to the connection object.
5090 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
5091 const gchar * header, gboolean * stale)
5093 GSList *list = NULL, *iter;
5095 gchar *item, *eq, *name_end, *value;
5097 g_return_if_fail (stale != NULL);
5099 gst_rtsp_connection_clear_auth_params (conn);
5102 /* Parse a header whose content is described by RFC2616 as
5103 * "#something", where "something" does not itself contain commas,
5104 * except as part of quoted-strings, into a list of allocated strings.
5106 header = gst_rtspsrc_skip_commas (header);
5108 end = gst_rtspsrc_skip_item (header);
5109 list = g_slist_prepend (list, g_strndup (header, end - header));
5110 header = gst_rtspsrc_skip_commas (end);
5115 list = g_slist_reverse (list);
5116 for (iter = list; iter; iter = iter->next) {
5119 eq = strchr (item, '=');
5121 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
5122 if (name_end == item) {
5123 /* That's no good... */
5130 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
5132 gst_rtsp_decode_quoted_string (value);
5136 if (value && strcmp (item, "stale") == 0 && strcmp (value, "TRUE") == 0)
5138 gst_rtsp_connection_set_auth_param (conn, item, value);
5142 g_slist_free (list);
5145 /* Parse a WWW-Authenticate Response header and determine the
5146 * available authentication methods
5148 * This code should also cope with the fact that each WWW-Authenticate
5149 * header can contain multiple challenge methods + tokens
5151 * At the moment, for Basic auth, we just do a minimal check and don't
5152 * even parse out the realm */
5154 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
5155 GstRTSPConnection * conn, gboolean * stale)
5159 g_return_if_fail (hdr != NULL);
5160 g_return_if_fail (methods != NULL);
5161 g_return_if_fail (stale != NULL);
5163 /* Skip whitespace at the start of the string */
5164 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
5166 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
5167 *methods |= GST_RTSP_AUTH_BASIC;
5168 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
5169 *methods |= GST_RTSP_AUTH_DIGEST;
5170 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
5175 * gst_rtspsrc_setup_auth:
5176 * @src: the rtsp source
5178 * Configure a username and password and auth method on the
5179 * connection object based on a response we received from the
5182 * Currently, this requires that a username and password were supplied
5183 * in the uri. In the future, they may be requested on demand by sending
5184 * a message up the bus.
5186 * Returns: TRUE if authentication information could be set up correctly.
5189 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
5193 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
5194 GstRTSPAuthMethod method;
5195 GstRTSPResult auth_result;
5197 GstRTSPConnection *conn;
5199 gboolean stale = FALSE;
5201 conn = src->conninfo.connection;
5203 /* Identify the available auth methods and see if any are supported */
5204 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
5205 &hdr, 0) == GST_RTSP_OK) {
5206 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
5209 if (avail_methods == GST_RTSP_AUTH_NONE)
5210 goto no_auth_available;
5212 /* For digest auth, if the response indicates that the session
5213 * data are stale, we just update them in the connection object and
5214 * return TRUE to retry the request */
5216 src->tried_url_auth = FALSE;
5218 url = gst_rtsp_connection_get_url (conn);
5220 /* Do we have username and password available? */
5221 if (url != NULL && !src->tried_url_auth && url->user != NULL
5222 && url->passwd != NULL) {
5225 src->tried_url_auth = TRUE;
5226 GST_DEBUG_OBJECT (src,
5227 "Attempting authentication using credentials from the URL");
5229 user = src->user_id;
5230 pass = src->user_pw;
5231 GST_DEBUG_OBJECT (src,
5232 "Attempting authentication using credentials from the properties");
5235 /* FIXME: If the url didn't contain username and password or we tried them
5236 * already, request a username and passwd from the application via some kind
5237 * of credentials request message */
5239 /* If we don't have a username and passwd at this point, bail out. */
5240 if (user == NULL || pass == NULL)
5243 /* Try to configure for each available authentication method, strongest to
5245 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
5246 /* Check if this method is available on the server */
5247 if ((method & avail_methods) == 0)
5250 /* Pass the credentials to the connection to try on the next request */
5251 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
5252 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
5253 * ignore it and end up retrying later */
5254 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
5255 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
5256 gst_rtsp_auth_method_to_string (method));
5261 if (method == GST_RTSP_AUTH_NONE)
5262 goto no_auth_available;
5268 /* Output an error indicating that we couldn't connect because there were
5269 * no supported authentication protocols */
5270 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5271 ("No supported authentication protocol was found"));
5276 /* We don't fire an error message, we just return FALSE and let the
5277 * normal NOT_AUTHORIZED error be propagated */
5282 static GstRTSPResult
5283 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5284 GstRTSPMessage * request, GstRTSPMessage * response,
5285 GstRTSPStatusCode * code)
5288 GstRTSPStatusCode thecode;
5289 gchar *content_base = NULL;
5293 if (!src->short_header)
5294 gst_rtsp_ext_list_before_send (src->extensions, request);
5296 GST_DEBUG_OBJECT (src, "sending message");
5299 gst_rtsp_message_dump (request);
5301 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
5305 gst_rtsp_connection_reset_timeout (conn);
5308 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
5313 gst_rtsp_message_dump (response);
5315 switch (response->type) {
5316 case GST_RTSP_MESSAGE_REQUEST:
5317 res = gst_rtspsrc_handle_request (src, conn, response);
5318 if (res == GST_RTSP_EEOF)
5321 goto handle_request_failed;
5323 case GST_RTSP_MESSAGE_RESPONSE:
5324 /* ok, a response is good */
5325 GST_DEBUG_OBJECT (src, "received response message");
5327 case GST_RTSP_MESSAGE_DATA:
5328 /* get next response */
5329 GST_DEBUG_OBJECT (src, "handle data response message");
5330 gst_rtspsrc_handle_data (src, response);
5333 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5338 thecode = response->type_data.response.code;
5340 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
5342 /* if the caller wanted the result code, we store it. */
5346 /* If the request didn't succeed, bail out before doing any more */
5347 if (thecode != GST_RTSP_STS_OK)
5350 /* store new content base if any */
5351 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
5354 g_free (src->content_base);
5355 src->content_base = g_strdup (content_base);
5357 gst_rtsp_ext_list_after_send (src->extensions, request, response);
5364 gchar *str = gst_rtsp_strresult (res);
5366 if (res != GST_RTSP_EINTR) {
5367 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5368 ("Could not send message. (%s)", str));
5370 GST_WARNING_OBJECT (src, "send interrupted");
5379 GST_WARNING_OBJECT (src, "server closed connection");
5380 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
5382 /* if reconnect succeeds, try again */
5384 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
5388 /* only try once after reconnect, then fallthrough and error out */
5391 gchar *str = gst_rtsp_strresult (res);
5393 if (res != GST_RTSP_EINTR) {
5394 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5395 ("Could not receive message. (%s)", str));
5397 GST_WARNING_OBJECT (src, "receive interrupted");
5405 handle_request_failed:
5407 /* ERROR was posted */
5408 gst_rtsp_message_unset (response);
5413 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5414 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5415 ("The server closed the connection."));
5416 gst_rtsp_message_unset (response);
5423 * @src: the rtsp source
5424 * @conn: the connection to send on
5425 * @request: must point to a valid request
5426 * @response: must point to an empty #GstRTSPMessage
5427 * @code: an optional code result
5429 * send @request and retrieve the response in @response. optionally @code can be
5430 * non-NULL in which case it will contain the status code of the response.
5432 * If This function returns #GST_RTSP_OK, @response will contain a valid response
5433 * message that should be cleaned with gst_rtsp_message_unset() after usage.
5435 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
5436 * @response message) if the response code was not 200 (OK).
5438 * If the attempt results in an authentication failure, then this will attempt
5439 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
5442 * Returns: #GST_RTSP_OK if the processing was successful.
5444 static GstRTSPResult
5445 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5446 GstRTSPMessage * request, GstRTSPMessage * response,
5447 GstRTSPStatusCode * code)
5449 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
5450 GstRTSPResult res = GST_RTSP_ERROR;
5453 GstRTSPMethod method = GST_RTSP_INVALID;
5459 /* make sure we don't loop forever */
5463 /* save method so we can disable it when the server complains */
5464 method = request->type_data.request.method;
5467 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
5471 case GST_RTSP_STS_UNAUTHORIZED:
5472 if (gst_rtspsrc_setup_auth (src, response)) {
5473 /* Try the request/response again after configuring the auth info
5481 } while (retry == TRUE);
5483 /* If the user requested the code, let them handle errors, otherwise
5484 * post an error below */
5487 else if (int_code != GST_RTSP_STS_OK)
5488 goto error_response;
5495 GST_DEBUG_OBJECT (src, "got error %d", res);
5500 res = GST_RTSP_ERROR;
5502 switch (response->type_data.response.code) {
5503 case GST_RTSP_STS_NOT_FOUND:
5504 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
5505 response->type_data.response.reason));
5507 case GST_RTSP_STS_MOVED_PERMANENTLY:
5508 case GST_RTSP_STS_MOVE_TEMPORARILY:
5510 gchar *new_location;
5511 GstRTSPLowerTrans transports;
5513 GST_DEBUG_OBJECT (src, "got redirection");
5514 /* if we don't have a Location Header, we must error */
5515 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
5516 &new_location, 0) < 0)
5519 /* When we receive a redirect result, we go back to the INIT state after
5520 * parsing the new URI. The caller should do the needed steps to issue
5521 * a new setup when it detects this state change. */
5522 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
5524 /* save current transports */
5525 if (src->conninfo.url)
5526 transports = src->conninfo.url->transports;
5528 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
5530 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
5532 /* set old transports */
5533 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
5534 src->conninfo.url->transports = transports;
5536 src->need_redirect = TRUE;
5537 src->state = GST_RTSP_STATE_INIT;
5541 case GST_RTSP_STS_NOT_ACCEPTABLE:
5542 case GST_RTSP_STS_NOT_IMPLEMENTED:
5543 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
5544 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
5545 gst_rtsp_method_as_text (method));
5546 src->methods &= ~method;
5550 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5551 ("Got error response: %d (%s).", response->type_data.response.code,
5552 response->type_data.response.reason));
5555 /* if we return ERROR we should unset the response ourselves */
5556 if (res == GST_RTSP_ERROR)
5557 gst_rtsp_message_unset (response);
5563 static GstRTSPResult
5564 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
5565 GstRTSPMessage * response, GstRTSPSrc * src)
5567 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
5572 /* parse the response and collect all the supported methods. We need this
5573 * information so that we don't try to send an unsupported request to the
5577 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
5579 GstRTSPHeaderField field;
5583 /* reset supported methods */
5586 /* Try Allow Header first */
5587 field = GST_RTSP_HDR_ALLOW;
5590 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5591 if (indx == 0 && !respoptions) {
5592 /* if no Allow header was found then try the Public header... */
5593 field = GST_RTSP_HDR_PUBLIC;
5594 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5599 src->methods |= gst_rtsp_options_from_text (respoptions);
5604 if (src->methods == 0) {
5605 /* neither Allow nor Public are required, assume the server supports
5606 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
5608 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
5609 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
5611 /* always assume PLAY, FIXME, extensions should be able to override
5613 src->methods |= GST_RTSP_PLAY;
5614 /* also assume it will support Range */
5615 src->seekable = TRUE;
5617 /* we need describe and setup */
5618 if (!(src->methods & GST_RTSP_DESCRIBE))
5620 if (!(src->methods & GST_RTSP_SETUP))
5628 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5629 ("Server does not support DESCRIBE."));
5634 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5635 ("Server does not support SETUP."));
5640 /* masks to be kept in sync with the hardcoded protocol order of preference
5642 static guint protocol_masks[] = {
5643 GST_RTSP_LOWER_TRANS_UDP,
5644 GST_RTSP_LOWER_TRANS_UDP_MCAST,
5645 GST_RTSP_LOWER_TRANS_TCP,
5649 static GstRTSPResult
5650 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
5651 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
5655 gboolean add_udp_str;
5660 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
5665 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
5667 /* extension listed transports, use those */
5668 if (*transports != NULL)
5671 /* it's the default */
5672 add_udp_str = FALSE;
5674 /* the default RTSP transports */
5675 result = g_string_new ("RTP");
5678 case GST_RTSP_PROFILE_AVP:
5679 g_string_append (result, "/AVP");
5681 case GST_RTSP_PROFILE_SAVP:
5682 g_string_append (result, "/SAVP");
5684 case GST_RTSP_PROFILE_AVPF:
5685 g_string_append (result, "/AVPF");
5687 case GST_RTSP_PROFILE_SAVPF:
5688 g_string_append (result, "/SAVPF");
5694 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
5695 GST_DEBUG_OBJECT (src, "adding UDP unicast");
5697 g_string_append (result, "/UDP");
5698 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
5699 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
5700 GST_DEBUG_OBJECT (src, "adding UDP multicast");
5701 /* we don't have to allocate any UDP ports yet, if the selected transport
5702 * turns out to be multicast we can create them and join the multicast
5703 * group indicated in the transport reply */
5705 g_string_append (result, "/UDP");
5706 g_string_append (result, ";multicast");
5707 if (src->next_port_num != 0) {
5708 if (src->client_port_range.max > 0 &&
5709 src->next_port_num >= src->client_port_range.max)
5712 g_string_append_printf (result, ";client_port=%d-%d",
5713 src->next_port_num, src->next_port_num + 1);
5715 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
5716 GST_DEBUG_OBJECT (src, "adding TCP");
5718 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
5720 *transports = g_string_free (result, FALSE);
5722 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
5729 GST_ERROR ("extension gave error %d", res);
5734 GST_ERROR ("no more ports available");
5735 return GST_RTSP_ERROR;
5739 static GstRTSPResult
5740 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
5741 gint orig_rtpport, gint orig_rtcpport)
5744 gint nr_udp, nr_int;
5746 gint rtpport = 0, rtcpport = 0;
5749 src = stream->parent;
5751 /* find number of placeholders first */
5752 if (strstr (*transports, "%%i2"))
5754 else if (strstr (*transports, "%%i1"))
5759 if (strstr (*transports, "%%u2"))
5761 else if (strstr (*transports, "%%u1"))
5766 if (nr_udp == 0 && nr_int == 0)
5770 if (!orig_rtpport || !orig_rtcpport) {
5771 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
5774 rtpport = orig_rtpport;
5775 rtcpport = orig_rtcpport;
5779 str = g_string_new ("");
5781 while ((next = strstr (p, "%%"))) {
5782 g_string_append_len (str, p, next - p);
5783 if (next[2] == 'u') {
5785 g_string_append_printf (str, "%d", rtpport);
5786 else if (next[3] == '2')
5787 g_string_append_printf (str, "%d", rtcpport);
5789 if (next[2] == 'i') {
5791 g_string_append_printf (str, "%d", src->free_channel);
5792 else if (next[3] == '2')
5793 g_string_append_printf (str, "%d", src->free_channel + 1);
5798 /* append final part */
5799 g_string_append (str, p);
5801 g_free (*transports);
5802 *transports = g_string_free (str, FALSE);
5810 GST_ERROR ("failed to allocate udp ports");
5811 return GST_RTSP_ERROR;
5816 gst_rtspsrc_stream_make_keymgmt (GstRTSPSrc * src, GstRTSPStream * stream)
5819 gchar *result, *base64;
5824 GstMIKEYMessage *msg;
5825 GstMIKEYPayload *payload, *pkd;
5829 key_data = g_malloc (KEY_SIZE);
5830 for (i = 0; i < KEY_SIZE; i += 4)
5831 GST_WRITE_UINT32_BE (key_data + i, g_random_int ());
5834 gst_buffer_unref (stream->key);
5835 stream->key = gst_buffer_new_wrapped (key_data, KEY_SIZE);
5837 msg = gst_mikey_message_new ();
5838 /* unencrypted MIKEY message, we send this over TLS so this is allowed */
5839 gst_mikey_message_set_info (msg, GST_MIKEY_VERSION, GST_MIKEY_TYPE_PSK_INIT,
5840 FALSE, GST_MIKEY_PRF_MIKEY_1, g_random_int (), GST_MIKEY_MAP_TYPE_SRTP);
5841 /* add policy '0' for our SSRC */
5842 gst_mikey_message_add_cs_srtp (msg, 0, stream->send_ssrc, 0);
5843 /* timestamp is now */
5844 gst_mikey_message_add_t_now_ntp_utc (msg);
5845 /* add some random data */
5846 gst_mikey_message_add_rand_len (msg, 16);
5848 /* the policy '0' is SRTP */
5849 payload = gst_mikey_payload_new (GST_MIKEY_PT_SP);
5850 gst_mikey_payload_sp_set (payload, 0, GST_MIKEY_SEC_PROTO_SRTP);
5852 /* only AES-CM is supported */
5854 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_ALG, 1, &byte);
5855 /* only HMAC-SHA1 */
5856 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_ALG, 1,
5858 /* we enable encryption on RTP and RTCP */
5859 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_ENC, 1,
5861 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTCP_ENC, 1,
5863 /* we enable authentication on RTP and RTCP */
5864 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_AUTH, 1,
5866 gst_mikey_message_add_payload (msg, payload);
5868 /* make unencrypted KEMAC */
5869 payload = gst_mikey_payload_new (GST_MIKEY_PT_KEMAC);
5870 gst_mikey_payload_kemac_set (payload, GST_MIKEY_ENC_NULL, GST_MIKEY_MAC_NULL);
5871 /* add the key in KEMAC */
5872 pkd = gst_mikey_payload_new (GST_MIKEY_PT_KEY_DATA);
5873 gst_mikey_payload_key_data_set_key (pkd, GST_MIKEY_KD_TEK, KEY_SIZE,
5875 gst_mikey_payload_kemac_add_sub (payload, pkd);
5876 gst_mikey_message_add_payload (msg, payload);
5878 /* now serialize this to bytes */
5879 bytes = gst_mikey_message_to_bytes (msg, NULL, NULL);
5880 gst_mikey_message_free (msg);
5881 /* and make it into base64 */
5882 data = g_bytes_get_data (bytes, &size);
5883 base64 = g_base64_encode (data, size);
5884 g_bytes_unref (bytes);
5886 result = g_strdup_printf ("prot=mikey;uri=\"%s\";data=\"%s\"",
5887 stream->conninfo.location, base64);
5894 /* Perform the SETUP request for all the streams.
5896 * We ask the server for a specific transport, which initially includes all the
5897 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
5898 * two local UDP ports that we send to the server.
5900 * Once the server replied with a transport, we configure the other streams
5901 * with the same transport.
5903 * This function will also configure the stream for the selected transport,
5904 * which basically means creating the pipeline.
5906 static GstRTSPResult
5907 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
5910 GstRTSPResult res = GST_RTSP_ERROR;
5911 GstRTSPMessage request = { 0 };
5912 GstRTSPMessage response = { 0 };
5913 GstRTSPStream *stream = NULL;
5914 GstRTSPLowerTrans protocols;
5915 GstRTSPStatusCode code;
5916 gboolean unsupported_real = FALSE;
5917 gint rtpport, rtcpport;
5921 if (src->conninfo.connection) {
5922 url = gst_rtsp_connection_get_url (src->conninfo.connection);
5923 /* we initially allow all configured lower transports. based on the URL
5924 * transports and the replies from the server we narrow them down. */
5925 protocols = url->transports & src->cur_protocols;
5928 protocols = src->cur_protocols;
5934 /* reset some state */
5935 src->free_channel = 0;
5936 src->interleaved = FALSE;
5937 src->need_activate = FALSE;
5938 /* keep track of next port number, 0 is random */
5939 src->next_port_num = src->client_port_range.min;
5940 rtpport = rtcpport = 0;
5942 if (G_UNLIKELY (src->streams == NULL))
5945 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5946 GstRTSPConnection *conn;
5953 stream = (GstRTSPStream *) walk->data;
5955 caps = stream_get_caps_for_pt (stream, stream->default_pt);
5957 GST_DEBUG_OBJECT (src, "skipping stream %p, no caps", stream);
5961 if (stream->skipped) {
5962 GST_DEBUG_OBJECT (src, "skipping stream %p", stream);
5966 /* see if we need to configure this stream */
5967 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
5968 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
5973 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
5974 stream->id, caps, &selected);
5976 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
5980 /* merge/overwrite global caps */
5985 s = gst_caps_get_structure (caps, 0);
5987 num = gst_structure_n_fields (src->props);
5988 for (j = 0; j < num; j++) {
5992 name = gst_structure_nth_field_name (src->props, j);
5993 val = gst_structure_get_value (src->props, name);
5994 gst_structure_set_value (s, name, val);
5996 GST_DEBUG_OBJECT (src, "copied %s", name);
6000 /* skip setup if we have no URL for it */
6001 if (stream->conninfo.location == NULL) {
6002 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
6006 if (src->conninfo.connection == NULL) {
6007 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
6008 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
6011 conn = stream->conninfo.connection;
6013 conn = src->conninfo.connection;
6015 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
6016 stream->conninfo.location);
6018 /* if we have a multicast connection, only suggest multicast from now on */
6019 if (stream->is_multicast)
6020 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
6023 /* first selectable protocol */
6024 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6026 if (!protocol_masks[mask])
6030 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
6031 protocol_masks[mask]);
6032 /* create a string with first transport in line */
6034 res = gst_rtspsrc_create_transports_string (src,
6035 protocols & protocol_masks[mask], stream->profile, &transports);
6036 if (res < 0 || transports == NULL)
6037 goto setup_transport_failed;
6039 if (strlen (transports) == 0) {
6040 g_free (transports);
6041 GST_DEBUG_OBJECT (src, "no transports found");
6046 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
6048 /* replace placeholders with real values, this function will optionally
6049 * allocate UDP ports and other info needed to execute the setup request */
6050 res = gst_rtspsrc_prepare_transports (stream, &transports,
6051 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
6053 g_free (transports);
6054 goto setup_transport_failed;
6057 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
6059 /* create SETUP request */
6061 gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
6062 stream->conninfo.location);
6064 g_free (transports);
6065 goto create_request_failed;
6068 /* select transport */
6069 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
6072 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
6073 stream->profile == GST_RTSP_PROFILE_SAVPF) {
6074 hval = gst_rtspsrc_stream_make_keymgmt (src, stream);
6075 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
6078 /* if the user wants a non default RTP packet size we add the blocksize
6080 if (src->rtp_blocksize > 0) {
6081 hval = g_strdup_printf ("%d", src->rtp_blocksize);
6082 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
6086 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
6089 /* handle the code ourselves */
6090 if ((res = gst_rtspsrc_send (src, conn, &request, &response, &code) < 0))
6094 case GST_RTSP_STS_OK:
6096 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
6097 gst_rtsp_message_unset (&request);
6098 gst_rtsp_message_unset (&response);
6099 /* cleanup of leftover transport */
6100 gst_rtspsrc_stream_free_udp (stream);
6101 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
6102 * we might be in this case */
6103 if (stream->container && rtpport && rtcpport && !retry) {
6104 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
6109 /* this transport did not go down well, but we may have others to try
6110 * that we did not send yet, try those and only give up then
6111 * but not without checking for lost cause/extension so we can
6112 * post a nicer/more useful error message later */
6113 if (!unsupported_real)
6114 unsupported_real = stream->is_real;
6115 /* select next available protocol, give up on this stream if none */
6117 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6119 if (!protocol_masks[mask] || unsupported_real)
6124 /* cleanup of leftover transport and move to the next stream */
6125 gst_rtspsrc_stream_free_udp (stream);
6126 goto response_error;
6129 /* parse response transport */
6131 gchar *resptrans = NULL;
6132 GstRTSPTransport transport = { 0 };
6134 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
6137 gst_rtspsrc_stream_free_udp (stream);
6141 /* parse transport, go to next stream on parse error */
6142 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
6143 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
6147 /* update allowed transports for other streams. once the transport of
6148 * one stream has been determined, we make sure that all other streams
6149 * are configured in the same way */
6150 switch (transport.lower_transport) {
6151 case GST_RTSP_LOWER_TRANS_TCP:
6152 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
6153 protocols = GST_RTSP_LOWER_TRANS_TCP;
6154 src->interleaved = TRUE;
6155 /* update free channels */
6157 MAX (transport.interleaved.min, src->free_channel);
6159 MAX (transport.interleaved.max, src->free_channel);
6160 src->free_channel++;
6162 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
6163 /* only allow multicast for other streams */
6164 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
6165 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
6166 /* if the server selected our ports, increment our counters so that
6167 * we select a new port later */
6168 if (src->next_port_num == transport.port.min &&
6169 src->next_port_num + 1 == transport.port.max) {
6170 src->next_port_num += 2;
6173 case GST_RTSP_LOWER_TRANS_UDP:
6174 /* only allow unicast for other streams */
6175 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
6176 protocols = GST_RTSP_LOWER_TRANS_UDP;
6179 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
6180 transport.lower_transport);
6184 if (!stream->container || (!src->interleaved && !retry)) {
6185 /* now configure the stream with the selected transport */
6186 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
6187 GST_DEBUG_OBJECT (src,
6188 "could not configure stream %p transport, skipping stream",
6191 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
6192 /* retain the first allocated UDP port pair */
6193 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
6194 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
6197 /* we need to activate at least one streams when we detect activity */
6198 src->need_activate = TRUE;
6200 /* stream is setup now */
6201 stream->setup = TRUE;
6206 GstRTSPStream *sskip;
6208 skip = g_list_next (skip);
6212 sskip = (GstRTSPStream *) skip->data;
6214 /* skip all streams with the same control url */
6215 if (g_str_equal (stream->conninfo.location, sskip->conninfo.location)) {
6216 GST_DEBUG_OBJECT (src, "found stream %p with same control %s",
6217 sskip, sskip->conninfo.location);
6218 sskip->skipped = TRUE;
6223 /* clean up our transport struct */
6224 gst_rtsp_transport_init (&transport);
6225 /* clean up used RTSP messages */
6226 gst_rtsp_message_unset (&request);
6227 gst_rtsp_message_unset (&response);
6231 /* store the transport protocol that was configured */
6232 src->cur_protocols = protocols;
6234 gst_rtsp_ext_list_stream_select (src->extensions, url);
6236 /* if there is nothing to activate, error out */
6237 if (!src->need_activate)
6238 goto nothing_to_activate;
6245 /* no transport possible, post an error and stop */
6246 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6247 ("Could not connect to server, no protocols left"));
6248 return GST_RTSP_ERROR;
6252 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6253 ("SDP contains no streams"));
6254 return GST_RTSP_ERROR;
6256 create_request_failed:
6258 gchar *str = gst_rtsp_strresult (res);
6260 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6261 ("Could not create request. (%s)", str));
6265 setup_transport_failed:
6267 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6268 ("Could not setup transport."));
6269 res = GST_RTSP_ERROR;
6274 const gchar *str = gst_rtsp_status_as_text (code);
6276 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6277 ("Error (%d): %s", code, GST_STR_NULL (str)));
6278 res = GST_RTSP_ERROR;
6283 gchar *str = gst_rtsp_strresult (res);
6285 if (res != GST_RTSP_EINTR) {
6286 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6287 ("Could not send message. (%s)", str));
6289 GST_WARNING_OBJECT (src, "send interrupted");
6296 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6297 ("Server did not select transport."));
6298 res = GST_RTSP_ERROR;
6301 nothing_to_activate:
6303 /* none of the available error codes is really right .. */
6304 if (unsupported_real) {
6305 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6306 (_("No supported stream was found. You might need to install a "
6307 "GStreamer RTSP extension plugin for Real media streams.")),
6310 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6311 (_("No supported stream was found. You might need to allow "
6312 "more transport protocols or may otherwise be missing "
6313 "the right GStreamer RTSP extension plugin.")), (NULL));
6315 return GST_RTSP_ERROR;
6319 gst_rtsp_message_unset (&request);
6320 gst_rtsp_message_unset (&response);
6326 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
6327 GstSegment * segment)
6330 GstRTSPTimeRange *therange;
6333 gst_rtsp_range_free (src->range);
6335 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
6336 GST_DEBUG_OBJECT (src, "parsed range %s", range);
6337 src->range = therange;
6339 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
6341 gst_segment_init (segment, GST_FORMAT_TIME);
6345 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
6346 therange->min.type, therange->min.seconds, therange->max.type,
6347 therange->max.seconds);
6349 if (therange->min.type == GST_RTSP_TIME_NOW)
6351 else if (therange->min.type == GST_RTSP_TIME_END)
6354 seconds = therange->min.seconds * GST_SECOND;
6356 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
6357 GST_TIME_ARGS (seconds));
6359 /* we need to start playback without clipping from the position reported by
6361 segment->start = seconds;
6362 segment->position = seconds;
6364 if (therange->max.type == GST_RTSP_TIME_NOW)
6366 else if (therange->max.type == GST_RTSP_TIME_END)
6369 seconds = therange->max.seconds * GST_SECOND;
6371 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
6372 GST_TIME_ARGS (seconds));
6374 /* live (WMS) server might send overflowed large max as its idea of infinity,
6375 * compensate to prevent problems later on */
6376 if (seconds != -1 && seconds < 0) {
6378 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
6381 /* live (WMS) might send min == max, which is not worth recording */
6382 if (segment->duration == -1 && seconds == segment->start)
6385 /* don't change duration with unknown value, we might have a valid value
6386 * there that we want to keep. */
6388 segment->duration = seconds;
6393 /* Parse clock profived by the server with following syntax:
6395 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
6398 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
6400 gboolean res = FALSE;
6402 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
6403 gchar **fields = NULL, **parts = NULL;
6404 gchar *remote_ip, *str;
6406 GstClockTime base_time;
6409 fields = g_strsplit (gstclock, " ", 0);
6411 /* wrapped clock, not very interesting for now */
6412 if (fields[1] == NULL)
6415 /* remote IP address and port */
6416 if ((str = fields[2]) == NULL)
6419 parts = g_strsplit (str, ":", 0);
6421 if ((remote_ip = parts[0]) == NULL)
6424 if ((str = parts[1]) == NULL)
6432 if ((str = fields[3]) == NULL)
6435 base_time = g_ascii_strtoull (str, NULL, 10);
6438 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
6441 if (src->provided_clock)
6442 gst_object_unref (src->provided_clock);
6443 src->provided_clock = netclock;
6445 gst_element_post_message (GST_ELEMENT_CAST (src),
6446 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
6447 src->provided_clock, TRUE));
6451 g_strfreev (fields);
6457 /* must be called with the RTSP state lock */
6458 static GstRTSPResult
6459 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
6465 /* prepare global stream caps properties */
6467 gst_structure_remove_all_fields (src->props);
6469 src->props = gst_structure_new_empty ("RTSPProperties");
6472 gst_sdp_message_dump (sdp);
6474 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
6476 /* let the app inspect and change the SDP */
6477 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
6479 gst_segment_init (&src->segment, GST_FORMAT_TIME);
6481 /* parse range for duration reporting. */
6486 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
6490 /* keep track of the range and configure it in the segment */
6491 if (gst_rtspsrc_parse_range (src, range, &src->segment))
6495 /* parse clock information. This is GStreamer specific, a server can tell the
6496 * client what clock it is using and wrap that in a network clock. The
6497 * advantage of that is that we can slave to it. */
6499 const gchar *gstclock;
6502 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
6503 if (gstclock == NULL)
6506 /* parse the clock and expose it in the provide_clock method */
6507 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
6511 /* try to find a global control attribute. Note that a '*' means that we should
6512 * do aggregate control with the current url (so we don't do anything and
6513 * leave the current connection as is) */
6515 const gchar *control;
6518 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
6519 if (control == NULL)
6522 /* only take fully qualified urls */
6523 if (g_str_has_prefix (control, "rtsp://"))
6527 g_free (src->conninfo.location);
6528 src->conninfo.location = g_strdup (control);
6529 /* make a connection for this, if there was a connection already, nothing
6531 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
6532 GST_ERROR_OBJECT (src, "could not connect");
6535 /* we need to keep the control url separate from the connection url because
6536 * the rules for constructing the media control url need it */
6537 g_free (src->control);
6538 src->control = g_strdup (control);
6541 /* create streams */
6542 n_streams = gst_sdp_message_medias_len (sdp);
6543 for (i = 0; i < n_streams; i++) {
6544 gst_rtspsrc_create_stream (src, sdp, i);
6547 src->state = GST_RTSP_STATE_INIT;
6550 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
6553 /* reset our state */
6554 src->need_range = TRUE;
6557 src->state = GST_RTSP_STATE_READY;
6564 GST_ERROR_OBJECT (src, "setup failed");
6565 gst_rtspsrc_cleanup (src);
6570 static GstRTSPResult
6571 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
6575 GstRTSPMessage request = { 0 };
6576 GstRTSPMessage response = { 0 };
6579 gchar *respcont = NULL;
6582 src->need_redirect = FALSE;
6584 /* can't continue without a valid url */
6585 if (G_UNLIKELY (src->conninfo.url == NULL)) {
6586 res = GST_RTSP_EINVAL;
6589 src->tried_url_auth = FALSE;
6591 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
6592 goto connect_failed;
6594 /* create OPTIONS */
6595 GST_DEBUG_OBJECT (src, "create options...");
6597 gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
6598 src->conninfo.url_str);
6600 goto create_request_failed;
6603 GST_DEBUG_OBJECT (src, "send options...");
6606 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
6609 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6614 if (!gst_rtspsrc_parse_methods (src, &response))
6617 /* create DESCRIBE */
6618 GST_DEBUG_OBJECT (src, "create describe...");
6620 gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
6621 src->conninfo.url_str);
6623 goto create_request_failed;
6625 /* we only accept SDP for now */
6626 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
6630 GST_DEBUG_OBJECT (src, "send describe...");
6633 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
6636 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6640 /* we only perform redirect for the describe, currently */
6641 if (src->need_redirect) {
6642 /* close connection, we don't have to send a TEARDOWN yet, ignore the
6644 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6646 gst_rtsp_message_unset (&request);
6647 gst_rtsp_message_unset (&response);
6653 /* it could be that the DESCRIBE method was not implemented */
6654 if (!src->methods & GST_RTSP_DESCRIBE)
6657 /* check if reply is SDP */
6658 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
6660 /* could not be set but since the request returned OK, we assume it
6661 * was SDP, else check it. */
6663 if (!g_ascii_strcasecmp (respcont, "application/sdp") == 0)
6664 goto wrong_content_type;
6667 /* get message body and parse as SDP */
6668 gst_rtsp_message_get_body (&response, &data, &size);
6669 if (data == NULL || size == 0)
6672 GST_DEBUG_OBJECT (src, "parse SDP...");
6673 gst_sdp_message_new (sdp);
6674 gst_sdp_message_parse_buffer (data, size, *sdp);
6676 /* clean up any messages */
6677 gst_rtsp_message_unset (&request);
6678 gst_rtsp_message_unset (&response);
6685 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
6686 ("No valid RTSP URL was provided"));
6691 gchar *str = gst_rtsp_strresult (res);
6693 if (res != GST_RTSP_EINTR) {
6694 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
6695 ("Failed to connect. (%s)", str));
6697 GST_WARNING_OBJECT (src, "connect interrupted");
6702 create_request_failed:
6704 gchar *str = gst_rtsp_strresult (res);
6706 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6707 ("Could not create request. (%s)", str));
6713 /* Don't post a message - the rtsp_send method will have
6714 * taken care of it because we passed NULL for the response code */
6719 /* error was posted */
6720 res = GST_RTSP_ERROR;
6725 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6726 ("Server does not support SDP, got %s.", respcont));
6727 res = GST_RTSP_ERROR;
6732 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6733 ("Server can not provide an SDP."));
6734 res = GST_RTSP_ERROR;
6739 if (src->conninfo.connection) {
6740 GST_DEBUG_OBJECT (src, "free connection");
6741 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6743 gst_rtsp_message_unset (&request);
6744 gst_rtsp_message_unset (&response);
6749 static GstRTSPResult
6750 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
6755 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
6757 if (src->sdp == NULL) {
6758 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
6762 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
6767 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
6774 GST_WARNING_OBJECT (src, "can't get sdp");
6775 src->open_error = TRUE;
6780 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
6781 src->open_error = TRUE;
6786 static GstRTSPResult
6787 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
6789 GstRTSPMessage request = { 0 };
6790 GstRTSPMessage response = { 0 };
6791 GstRTSPResult res = GST_RTSP_OK;
6793 const gchar *control;
6795 GST_DEBUG_OBJECT (src, "TEARDOWN...");
6797 gst_rtspsrc_set_state (src, GST_STATE_READY);
6799 if (src->state < GST_RTSP_STATE_READY) {
6800 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
6807 /* construct a control url */
6808 control = get_aggregate_control (src);
6810 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
6813 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6814 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6815 const gchar *setup_url;
6816 GstRTSPConnInfo *info;
6818 /* try aggregate control first but do non-aggregate control otherwise */
6820 setup_url = control;
6821 else if ((setup_url = stream->conninfo.location) == NULL)
6824 if (src->conninfo.connection) {
6825 info = &src->conninfo;
6826 } else if (stream->conninfo.connection) {
6827 info = &stream->conninfo;
6831 if (!info->connected)
6836 gst_rtsp_message_init_request (&request, GST_RTSP_TEARDOWN, setup_url);
6838 goto create_request_failed;
6841 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
6844 gst_rtspsrc_send (src, info->connection, &request, &response,
6848 /* FIXME, parse result? */
6849 gst_rtsp_message_unset (&request);
6850 gst_rtsp_message_unset (&response);
6853 /* early exit when we did aggregate control */
6859 /* close connections */
6860 GST_DEBUG_OBJECT (src, "closing connection...");
6861 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6862 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6863 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6864 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
6868 gst_rtspsrc_cleanup (src);
6870 src->state = GST_RTSP_STATE_INVALID;
6873 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
6878 create_request_failed:
6880 gchar *str = gst_rtsp_strresult (res);
6882 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6883 ("Could not create request. (%s)", str));
6889 gchar *str = gst_rtsp_strresult (res);
6891 gst_rtsp_message_unset (&request);
6892 if (res != GST_RTSP_EINTR) {
6893 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6894 ("Could not send message. (%s)", str));
6896 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
6903 GST_DEBUG_OBJECT (src,
6904 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
6909 /* RTP-Info is of the format:
6911 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
6913 * rtptime corresponds to the timestamp for the NPT time given in the header
6914 * seqbase corresponds to the next sequence number we received. This number
6915 * indicates the first seqnum after the seek and should be used to discard
6916 * packets that are from before the seek.
6919 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
6924 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
6926 infos = g_strsplit (rtpinfo, ",", 0);
6927 for (i = 0; infos[i]; i++) {
6929 GstRTSPStream *stream;
6933 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
6935 /* init values, types of seqbase and timebase are bigger than needed so we
6936 * can store -1 as uninitialized values */
6941 /* parse url, find stream for url.
6942 * parse seq and rtptime. The seq number should be configured in the rtp
6943 * depayloader or session manager to detect gaps. Same for the rtptime, it
6944 * should be used to create an initial time newsegment. */
6945 fields = g_strsplit (infos[i], ";", 0);
6946 for (j = 0; fields[j]; j++) {
6947 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
6948 /* remove leading whitespace */
6949 fields[j] = g_strchug (fields[j]);
6950 if (g_str_has_prefix (fields[j], "url=")) {
6951 /* get the url and the stream */
6953 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
6954 } else if (g_str_has_prefix (fields[j], "seq=")) {
6955 seqbase = atoi (fields[j] + 4);
6956 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
6957 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
6960 g_strfreev (fields);
6961 /* now we need to store the values for the caps of the stream */
6962 if (stream != NULL) {
6963 GST_DEBUG_OBJECT (src,
6964 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
6965 stream, seqbase, timebase);
6967 /* we have a stream, configure detected params */
6968 stream->seqbase = seqbase;
6969 stream->timebase = timebase;
6978 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
6983 interval = strtoul (rtcp, NULL, 10);
6984 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
6989 interval *= GST_MSECOND;
6991 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6992 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6994 /* already (optionally) retrieved this when configuring manager */
6995 if (stream->session) {
6996 GObject *rtpsession = stream->session;
6998 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
7000 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
7004 /* now it happens that (Xenon) server sending this may also provide bogus
7005 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
7006 * and just use RTP-Info to sync */
7008 GObjectClass *klass;
7010 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
7011 if (g_object_class_find_property (klass, "rtcp-sync")) {
7012 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
7013 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
7019 gst_rtspsrc_get_float (const gchar * dstr)
7021 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7023 /* canonicalise floating point string so we can handle float strings
7024 * in the form "24.930" or "24,930" irrespective of the current locale */
7025 g_strlcpy (s, dstr, sizeof (s));
7026 g_strdelimit (s, ",", '.');
7027 return g_ascii_strtod (s, NULL);
7031 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
7033 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7035 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
7036 g_strlcpy (val_str, "now", sizeof (val_str));
7038 if (segment->position == 0) {
7039 g_strlcpy (val_str, "0", sizeof (val_str));
7041 g_ascii_dtostr (val_str, sizeof (val_str),
7042 ((gdouble) segment->position) / GST_SECOND);
7045 return g_strdup_printf ("npt=%s-", val_str);
7049 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
7053 stream->timebase = -1;
7054 stream->seqbase = -1;
7056 len = stream->ptmap->len;
7057 for (i = 0; i < len; i++) {
7058 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
7061 if (item->caps == NULL)
7064 item->caps = gst_caps_make_writable (item->caps);
7065 s = gst_caps_get_structure (item->caps, 0);
7066 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
7070 static GstRTSPResult
7071 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
7073 GstRTSPResult res = GST_RTSP_OK;
7075 if (src->state < GST_RTSP_STATE_READY) {
7076 res = GST_RTSP_ERROR;
7077 if (src->open_error) {
7078 GST_DEBUG_OBJECT (src, "the stream was in error");
7082 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
7084 if ((res = gst_rtspsrc_open (src, async)) < 0) {
7085 GST_DEBUG_OBJECT (src, "failed to open stream");
7094 static GstRTSPResult
7095 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
7097 GstRTSPMessage request = { 0 };
7098 GstRTSPMessage response = { 0 };
7099 GstRTSPResult res = GST_RTSP_OK;
7103 const gchar *control;
7105 GST_DEBUG_OBJECT (src, "PLAY...");
7107 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7110 if (!(src->methods & GST_RTSP_PLAY))
7113 if (src->state == GST_RTSP_STATE_PLAYING)
7116 if (!src->conninfo.connection || !src->conninfo.connected)
7119 /* send some dummy packets before we activate the receive in the
7121 gst_rtspsrc_send_dummy_packets (src);
7123 /* require new SR packets */
7125 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
7127 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
7129 /* construct a control url */
7130 control = get_aggregate_control (src);
7132 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7133 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7134 const gchar *setup_url;
7135 GstRTSPConnection *conn;
7137 /* try aggregate control first but do non-aggregate control otherwise */
7139 setup_url = control;
7140 else if ((setup_url = stream->conninfo.location) == NULL)
7143 if (src->conninfo.connection) {
7144 conn = src->conninfo.connection;
7145 } else if (stream->conninfo.connection) {
7146 conn = stream->conninfo.connection;
7152 res = gst_rtsp_message_init_request (&request, GST_RTSP_PLAY, setup_url);
7154 goto create_request_failed;
7156 if (src->need_range) {
7157 hval = gen_range_header (src, segment);
7159 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
7161 /* store the newsegment event so it can be sent from the streaming thread. */
7162 if (src->start_segment)
7163 gst_event_unref (src->start_segment);
7164 src->start_segment = gst_event_new_segment (&src->segment);
7167 if (segment->rate != 1.0) {
7168 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
7170 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
7172 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
7174 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
7178 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
7180 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7183 /* seek may have silently failed as it is not supported */
7184 if (!(src->methods & GST_RTSP_PLAY)) {
7185 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
7186 /* obviously it is supported as we made it here */
7187 src->methods |= GST_RTSP_PLAY;
7188 src->seekable = FALSE;
7189 /* but there is nothing to parse in the response,
7190 * so convey we have no idea and not to expect anything particular */
7191 clear_rtp_base (src, stream);
7195 /* need to do for all streams */
7196 for (run = src->streams; run; run = g_list_next (run))
7197 clear_rtp_base (src, (GstRTSPStream *) run->data);
7199 /* NOTE the above also disables npt based eos detection */
7200 /* and below forces position to 0,
7201 * which is visible feedback we lost the plot */
7202 segment->start = segment->position = src->last_pos;
7205 gst_rtsp_message_unset (&request);
7207 /* parse RTP npt field. This is the current position in the stream (Normal
7208 * Play Time) and should be put in the NEWSEGMENT position field. */
7209 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
7211 gst_rtspsrc_parse_range (src, hval, segment);
7213 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
7214 segment->rate = 1.0;
7216 /* parse Speed header. This is the intended playback rate of the stream
7217 * and should be put in the NEWSEGMENT rate field. */
7218 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
7219 0) == GST_RTSP_OK) {
7220 segment->rate = gst_rtspsrc_get_float (hval);
7221 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
7222 &hval, 0) == GST_RTSP_OK) {
7223 segment->rate = gst_rtspsrc_get_float (hval);
7226 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
7227 * for the RTP packets. If this is not present, we assume all starts from 0...
7228 * This is info for the RTP session manager that we pass to it in caps. */
7230 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
7231 &hval, hval_idx++) == GST_RTSP_OK)
7232 gst_rtspsrc_parse_rtpinfo (src, hval);
7234 /* some servers indicate RTCP parameters in PLAY response,
7235 * rather than properly in SDP */
7236 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
7237 &hval, 0) == GST_RTSP_OK)
7238 gst_rtspsrc_handle_rtcp_interval (src, hval);
7240 gst_rtsp_message_unset (&response);
7242 /* early exit when we did aggregate control */
7246 /* configure the caps of the streams after we parsed all headers. Only reset
7247 * the manager object when we set a new Range header (we did a seek) */
7248 gst_rtspsrc_configure_caps (src, segment, src->need_range);
7250 /* set again when needed */
7251 src->need_range = FALSE;
7253 src->running = TRUE;
7254 src->base_time = -1;
7255 src->state = GST_RTSP_STATE_PLAYING;
7258 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
7259 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7260 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7261 stream->discont = TRUE;
7266 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
7273 GST_DEBUG_OBJECT (src, "failed to open stream");
7278 GST_DEBUG_OBJECT (src, "PLAY is not supported");
7283 GST_DEBUG_OBJECT (src, "we were already PLAYING");
7286 create_request_failed:
7288 gchar *str = gst_rtsp_strresult (res);
7290 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7291 ("Could not create request. (%s)", str));
7297 gchar *str = gst_rtsp_strresult (res);
7299 gst_rtsp_message_unset (&request);
7300 if (res != GST_RTSP_EINTR) {
7301 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7302 ("Could not send message. (%s)", str));
7304 GST_WARNING_OBJECT (src, "PLAY interrupted");
7311 static GstRTSPResult
7312 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
7314 GstRTSPResult res = GST_RTSP_OK;
7315 GstRTSPMessage request = { 0 };
7316 GstRTSPMessage response = { 0 };
7318 const gchar *control;
7320 GST_DEBUG_OBJECT (src, "PAUSE...");
7322 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7325 if (!(src->methods & GST_RTSP_PAUSE))
7328 if (src->state == GST_RTSP_STATE_READY)
7331 if (!src->conninfo.connection || !src->conninfo.connected)
7334 /* construct a control url */
7335 control = get_aggregate_control (src);
7337 /* loop over the streams. We might exit the loop early when we could do an
7338 * aggregate control */
7339 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7340 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7341 GstRTSPConnection *conn;
7342 const gchar *setup_url;
7344 /* try aggregate control first but do non-aggregate control otherwise */
7346 setup_url = control;
7347 else if ((setup_url = stream->conninfo.location) == NULL)
7350 if (src->conninfo.connection) {
7351 conn = src->conninfo.connection;
7352 } else if (stream->conninfo.connection) {
7353 conn = stream->conninfo.connection;
7359 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
7360 ("Sending PAUSE request"));
7363 gst_rtsp_message_init_request (&request, GST_RTSP_PAUSE,
7365 goto create_request_failed;
7367 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7370 gst_rtsp_message_unset (&request);
7371 gst_rtsp_message_unset (&response);
7373 /* exit early when we did agregate control */
7378 /* change element states now */
7379 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
7382 src->state = GST_RTSP_STATE_READY;
7386 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
7393 GST_DEBUG_OBJECT (src, "failed to open stream");
7398 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
7403 GST_DEBUG_OBJECT (src, "we were already PAUSED");
7406 create_request_failed:
7408 gchar *str = gst_rtsp_strresult (res);
7410 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7411 ("Could not create request. (%s)", str));
7417 gchar *str = gst_rtsp_strresult (res);
7419 gst_rtsp_message_unset (&request);
7420 if (res != GST_RTSP_EINTR) {
7421 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7422 ("Could not send message. (%s)", str));
7424 GST_WARNING_OBJECT (src, "PAUSE interrupted");
7432 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
7434 GstRTSPSrc *rtspsrc;
7436 rtspsrc = GST_RTSPSRC (bin);
7438 switch (GST_MESSAGE_TYPE (message)) {
7439 case GST_MESSAGE_EOS:
7440 gst_message_unref (message);
7442 case GST_MESSAGE_ELEMENT:
7444 const GstStructure *s = gst_message_get_structure (message);
7446 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
7447 gboolean ignore_timeout;
7449 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
7451 GST_OBJECT_LOCK (rtspsrc);
7452 ignore_timeout = rtspsrc->ignore_timeout;
7453 rtspsrc->ignore_timeout = TRUE;
7454 GST_OBJECT_UNLOCK (rtspsrc);
7456 /* we only act on the first udp timeout message, others are irrelevant
7457 * and can be ignored. */
7458 if (!ignore_timeout)
7459 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
7461 gst_message_unref (message);
7464 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7467 case GST_MESSAGE_ERROR:
7470 GstRTSPStream *stream;
7473 udpsrc = GST_MESSAGE_SRC (message);
7475 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
7476 GST_ELEMENT_NAME (udpsrc));
7478 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
7482 /* we ignore the RTCP udpsrc */
7483 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
7486 /* if we get error messages from the udp sources, that's not a problem as
7487 * long as not all of them error out. We also don't really know what the
7488 * problem is, the message does not give enough detail... */
7489 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
7490 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
7491 if (ret != GST_FLOW_OK)
7495 gst_message_unref (message);
7499 /* fatal but not our message, forward */
7500 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7505 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7511 /* the thread where everything happens */
7513 gst_rtspsrc_thread (GstRTSPSrc * src)
7517 GST_OBJECT_LOCK (src);
7518 cmd = src->pending_cmd;
7519 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
7520 || cmd == CMD_LOOP || cmd == CMD_OPEN)
7521 src->pending_cmd = CMD_LOOP;
7523 src->pending_cmd = CMD_WAIT;
7524 GST_DEBUG_OBJECT (src, "got command %d", cmd);
7526 /* we got the message command, so ensure communication is possible again */
7527 gst_rtspsrc_connection_flush (src, FALSE);
7529 src->busy_cmd = cmd;
7530 GST_OBJECT_UNLOCK (src);
7534 gst_rtspsrc_open (src, TRUE);
7537 gst_rtspsrc_play (src, &src->segment, TRUE);
7540 gst_rtspsrc_pause (src, TRUE);
7543 gst_rtspsrc_close (src, TRUE, FALSE);
7546 gst_rtspsrc_loop (src);
7549 gst_rtspsrc_reconnect (src, FALSE);
7555 GST_OBJECT_LOCK (src);
7556 /* and go back to sleep */
7557 if (src->pending_cmd == CMD_WAIT) {
7559 gst_task_pause (src->task);
7562 src->busy_cmd = CMD_WAIT;
7563 GST_OBJECT_UNLOCK (src);
7567 gst_rtspsrc_start (GstRTSPSrc * src)
7569 GST_DEBUG_OBJECT (src, "starting");
7571 GST_OBJECT_LOCK (src);
7573 src->pending_cmd = CMD_WAIT;
7575 if (src->task == NULL) {
7576 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
7577 if (src->task == NULL)
7580 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
7582 GST_OBJECT_UNLOCK (src);
7589 GST_OBJECT_UNLOCK (src);
7590 GST_ERROR_OBJECT (src, "failed to create task");
7596 gst_rtspsrc_stop (GstRTSPSrc * src)
7600 GST_DEBUG_OBJECT (src, "stopping");
7602 /* also cancels pending task */
7603 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
7605 GST_OBJECT_LOCK (src);
7606 if ((task = src->task)) {
7608 GST_OBJECT_UNLOCK (src);
7610 gst_task_stop (task);
7612 /* make sure it is not running */
7613 GST_RTSP_STREAM_LOCK (src);
7614 GST_RTSP_STREAM_UNLOCK (src);
7616 /* now wait for the task to finish */
7617 gst_task_join (task);
7619 /* and free the task */
7620 gst_object_unref (GST_OBJECT (task));
7622 GST_OBJECT_LOCK (src);
7624 GST_OBJECT_UNLOCK (src);
7626 /* ensure synchronously all is closed and clean */
7627 gst_rtspsrc_close (src, FALSE, TRUE);
7632 static GstStateChangeReturn
7633 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
7635 GstRTSPSrc *rtspsrc;
7636 GstStateChangeReturn ret;
7638 rtspsrc = GST_RTSPSRC (element);
7640 switch (transition) {
7641 case GST_STATE_CHANGE_NULL_TO_READY:
7642 if (!gst_rtspsrc_start (rtspsrc))
7645 case GST_STATE_CHANGE_READY_TO_PAUSED:
7646 /* init some state */
7647 rtspsrc->cur_protocols = rtspsrc->protocols;
7648 /* first attempt, don't ignore timeouts */
7649 rtspsrc->ignore_timeout = FALSE;
7650 rtspsrc->open_error = FALSE;
7651 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
7653 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7654 set_manager_buffer_mode (rtspsrc);
7656 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7657 /* unblock the tcp tasks and make the loop waiting */
7658 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
7659 /* make sure it is waiting before we send PAUSE or PLAY below */
7660 GST_RTSP_STREAM_LOCK (rtspsrc);
7661 GST_RTSP_STREAM_UNLOCK (rtspsrc);
7664 case GST_STATE_CHANGE_PAUSED_TO_READY:
7670 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
7671 if (ret == GST_STATE_CHANGE_FAILURE)
7674 switch (transition) {
7675 case GST_STATE_CHANGE_NULL_TO_READY:
7676 ret = GST_STATE_CHANGE_SUCCESS;
7678 case GST_STATE_CHANGE_READY_TO_PAUSED:
7679 ret = GST_STATE_CHANGE_NO_PREROLL;
7681 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7682 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
7683 ret = GST_STATE_CHANGE_SUCCESS;
7685 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7686 /* send pause request and keep the idle task around */
7687 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
7688 ret = GST_STATE_CHANGE_NO_PREROLL;
7690 case GST_STATE_CHANGE_PAUSED_TO_READY:
7691 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_PAUSE);
7692 ret = GST_STATE_CHANGE_SUCCESS;
7694 case GST_STATE_CHANGE_READY_TO_NULL:
7695 gst_rtspsrc_stop (rtspsrc);
7696 ret = GST_STATE_CHANGE_SUCCESS;
7707 GST_DEBUG_OBJECT (rtspsrc, "start failed");
7708 return GST_STATE_CHANGE_FAILURE;
7713 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
7716 GstRTSPSrc *rtspsrc;
7718 rtspsrc = GST_RTSPSRC (element);
7720 if (GST_EVENT_IS_DOWNSTREAM (event)) {
7721 res = gst_rtspsrc_push_event (rtspsrc, event);
7723 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
7730 /*** GSTURIHANDLER INTERFACE *************************************************/
7733 gst_rtspsrc_uri_get_type (GType type)
7738 static const gchar *const *
7739 gst_rtspsrc_uri_get_protocols (GType type)
7741 static const gchar *protocols[] =
7742 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
7743 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
7750 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
7752 GstRTSPSrc *src = GST_RTSPSRC (handler);
7754 /* FIXME: make thread-safe */
7755 return g_strdup (src->conninfo.location);
7759 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
7764 GstRTSPUrl *newurl = NULL;
7765 GstSDPMessage *sdp = NULL;
7767 src = GST_RTSPSRC (handler);
7769 /* same URI, we're fine */
7770 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
7773 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
7774 if ((res = gst_sdp_message_new (&sdp) < 0))
7777 GST_DEBUG_OBJECT (src, "parsing SDP message");
7778 if ((res = gst_sdp_message_parse_uri (uri, sdp) < 0))
7782 GST_DEBUG_OBJECT (src, "parsing URI");
7783 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
7787 /* if worked, free previous and store new url object along with the original
7789 GST_DEBUG_OBJECT (src, "configuring URI");
7790 g_free (src->conninfo.location);
7791 src->conninfo.location = g_strdup (uri);
7792 gst_rtsp_url_free (src->conninfo.url);
7793 src->conninfo.url = newurl;
7794 g_free (src->conninfo.url_str);
7796 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
7798 src->conninfo.url_str = NULL;
7801 gst_sdp_message_free (src->sdp);
7803 src->from_sdp = sdp != NULL;
7805 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
7806 GST_DEBUG_OBJECT (src, "request uri is: %s",
7807 GST_STR_NULL (src->conninfo.url_str));
7814 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
7819 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", res);
7820 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7821 "Could not create SDP");
7826 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", res,
7827 GST_STR_NULL (uri));
7828 gst_sdp_message_free (sdp);
7829 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7835 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
7836 GST_STR_NULL (uri), res);
7837 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7838 "Invalid RTSP URI");
7844 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
7846 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
7848 iface->get_type = gst_rtspsrc_uri_get_type;
7849 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
7850 iface->get_uri = gst_rtspsrc_uri_get_uri;
7851 iface->set_uri = gst_rtspsrc_uri_set_uri;