2 * Copyright (C) <2005,2006> Wim Taymans <wim at fluendo dot com>
3 * <2006> Lutz Mueller <lutz at topfrose dot de>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 * Unless otherwise indicated, Source Code is licensed under MIT license.
22 * See further explanation attached in License Statement (distributed in the file
25 * Permission is hereby granted, free of charge, to any person obtaining a copy of
26 * this software and associated documentation files (the "Software"), to deal in
27 * the Software without restriction, including without limitation the rights to
28 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
29 * of the Software, and to permit persons to whom the Software is furnished to do
30 * so, subject to the following conditions:
32 * The above copyright notice and this permission notice shall be included in all
33 * copies or substantial portions of the Software.
35 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
44 * SECTION:element-rtspsrc
46 * Makes a connection to an RTSP server and read the data.
47 * rtspsrc strictly follows RFC 2326 and therefore does not (yet) support
48 * RealMedia/Quicktime/Microsoft extensions.
50 * RTSP supports transport over TCP or UDP in unicast or multicast mode. By
51 * default rtspsrc will negotiate a connection in the following order:
52 * UDP unicast/UDP multicast/TCP. The order cannot be changed but the allowed
53 * protocols can be controlled with the #GstRTSPSrc:protocols property.
55 * rtspsrc currently understands SDP as the format of the session description.
56 * For each stream listed in the SDP a new rtp_stream%d pad will be created
57 * with caps derived from the SDP media description. This is a caps of mime type
58 * "application/x-rtp" that can be connected to any available RTP depayloader
61 * rtspsrc will internally instantiate an RTP session manager element
62 * that will handle the RTCP messages to and from the server, jitter removal,
63 * packet reordering along with providing a clock for the pipeline.
64 * This feature is implemented using the gstrtpbin element.
66 * rtspsrc acts like a live source and will therefore only generate data in the
70 * <title>Example launch line</title>
72 * gst-launch-1.0 rtspsrc location=rtsp://some.server/url ! fakesink
73 * ]| Establish a connection to an RTSP server and send the raw RTP packets to a
77 * Last reviewed on 2006-08-18 (0.10.5)
86 #endif /* HAVE_UNISTD_H */
92 #include <gst/net/gstnet.h>
93 #include <gst/sdp/gstsdpmessage.h>
94 #include <gst/rtp/gstrtppayloads.h>
96 #include "gst/gst-i18n-plugin.h"
98 #include "gstrtspsrc.h"
100 GST_DEBUG_CATEGORY_STATIC (rtspsrc_debug);
101 #define GST_CAT_DEFAULT (rtspsrc_debug)
103 static GstStaticPadTemplate rtptemplate = GST_STATIC_PAD_TEMPLATE ("stream_%u",
106 GST_STATIC_CAPS ("application/x-rtp; application/x-rdt"));
108 /* templates used internally */
109 static GstStaticPadTemplate anysrctemplate =
110 GST_STATIC_PAD_TEMPLATE ("internalsrc_%u",
113 GST_STATIC_CAPS_ANY);
115 static GstStaticPadTemplate anysinktemplate =
116 GST_STATIC_PAD_TEMPLATE ("internalsink_%u",
119 GST_STATIC_CAPS_ANY);
123 SIGNAL_HANDLE_REQUEST,
125 SIGNAL_SELECT_STREAM,
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
203 PROP_DROP_ON_LATENCY,
204 PROP_CONNECTION_SPEED,
207 PROP_DO_RTSP_KEEP_ALIVE,
216 PROP_UDP_BUFFER_SIZE,
220 PROP_MULTICAST_IFACE,
222 PROP_USE_PIPELINE_CLOCK,
224 PROP_TLS_VALIDATION_FLAGS,
228 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
230 gst_rtsp_nat_method_get_type (void)
232 static GType rtsp_nat_method_type = 0;
233 static const GEnumValue rtsp_nat_method[] = {
234 {GST_RTSP_NAT_NONE, "None", "none"},
235 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
239 if (!rtsp_nat_method_type) {
240 rtsp_nat_method_type =
241 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
243 return rtsp_nat_method_type;
246 static void gst_rtspsrc_finalize (GObject * object);
248 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
249 const GValue * value, GParamSpec * pspec);
250 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
251 GValue * value, GParamSpec * pspec);
253 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
255 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
256 gpointer iface_data);
258 static void gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes,
261 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
262 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
264 static GstCaps *gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media);
266 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
267 GstStateChange transition);
268 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
269 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
271 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
272 GstRTSPMessage * response);
274 static gboolean gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
276 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
277 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
279 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
280 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
282 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
283 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
284 gboolean only_close);
286 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
287 const gchar * uri, GError ** error);
288 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
290 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
291 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
292 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
293 GstRTSPStream * stream, GstEvent * event);
294 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
296 /* commands we send to out loop to notify it of events */
297 #define CMD_OPEN (1 << 0)
298 #define CMD_PLAY (1 << 1)
299 #define CMD_PAUSE (1 << 2)
300 #define CMD_CLOSE (1 << 3)
301 #define CMD_WAIT (1 << 4)
302 #define CMD_RECONNECT (1 << 5)
303 #define CMD_LOOP (1 << 6)
305 /* mask for all commands */
306 #define CMD_ALL ((CMD_LOOP << 1) - 1)
308 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
310 gchar *__txt = _gst_element_error_printf text; \
311 gst_element_post_message (GST_ELEMENT_CAST (el), \
312 gst_message_new_progress (GST_OBJECT_CAST (el), \
313 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
317 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
319 #define gst_rtspsrc_parent_class parent_class
320 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
321 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
324 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
326 GST_DEBUG_OBJECT (src, "default handler");
331 select_stream_accum (GSignalInvocationHint * ihint,
332 GValue * return_accu, const GValue * handler_return, gpointer data)
336 myboolean = g_value_get_boolean (handler_return);
337 GST_DEBUG ("accum %d", myboolean);
338 g_value_set_boolean (return_accu, myboolean);
340 /* stop emission if FALSE */
345 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
347 GObjectClass *gobject_class;
348 GstElementClass *gstelement_class;
349 GstBinClass *gstbin_class;
351 gobject_class = (GObjectClass *) klass;
352 gstelement_class = (GstElementClass *) klass;
353 gstbin_class = (GstBinClass *) klass;
355 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
357 gobject_class->set_property = gst_rtspsrc_set_property;
358 gobject_class->get_property = gst_rtspsrc_get_property;
360 gobject_class->finalize = gst_rtspsrc_finalize;
362 g_object_class_install_property (gobject_class, PROP_LOCATION,
363 g_param_spec_string ("location", "RTSP Location",
364 "Location of the RTSP url to read",
365 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
367 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
368 g_param_spec_flags ("protocols", "Protocols",
369 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
370 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
372 g_object_class_install_property (gobject_class, PROP_DEBUG,
373 g_param_spec_boolean ("debug", "Debug",
374 "Dump request and response messages to stdout",
375 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
377 g_object_class_install_property (gobject_class, PROP_RETRY,
378 g_param_spec_uint ("retry", "Retry",
379 "Max number of retries when allocating RTP ports.",
380 0, G_MAXUINT16, DEFAULT_RETRY,
381 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
383 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
384 g_param_spec_uint64 ("timeout", "Timeout",
385 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
386 0, G_MAXUINT64, DEFAULT_TIMEOUT,
387 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
389 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
390 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
391 "Fail after timeout microseconds on TCP connections (0 = disabled)",
392 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
393 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
395 g_object_class_install_property (gobject_class, PROP_LATENCY,
396 g_param_spec_uint ("latency", "Buffer latency in ms",
397 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
398 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
400 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
401 g_param_spec_boolean ("drop-on-latency",
402 "Drop buffers when maximum latency is reached",
403 "Tells the jitterbuffer to never exceed the given latency in size",
404 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
406 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
407 g_param_spec_uint64 ("connection-speed", "Connection Speed",
408 "Network connection speed in kbps (0 = unknown)",
409 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
410 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
412 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
413 g_param_spec_enum ("nat-method", "NAT Method",
414 "Method to use for traversing firewalls and NAT",
415 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
416 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
419 * GstRTSPSrc:do-rtcp:
421 * Enable RTCP support. Some old server don't like RTCP and then this property
422 * needs to be set to FALSE.
424 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
425 g_param_spec_boolean ("do-rtcp", "Do RTCP",
426 "Send RTCP packets, disable for old incompatible server.",
427 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
430 * GstRTSPSrc:do-rtsp-keep-alive:
432 * Enable RTSP keep alive support. Some old server don't like RTSP
433 * keep alive and then this property needs to be set to FALSE.
435 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
436 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
437 "Send RTSP keep alive packets, disable for old incompatible server.",
438 DEFAULT_DO_RTSP_KEEP_ALIVE,
439 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
444 * Set the proxy parameters. This has to be a string of the format
445 * [http://][user:passwd@]host[:port].
447 g_object_class_install_property (gobject_class, PROP_PROXY,
448 g_param_spec_string ("proxy", "Proxy",
449 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
450 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
452 * GstRTSPSrc:proxy-id:
454 * Sets the proxy URI user id for authentication. If the URI set via the
455 * "proxy" property contains a user-id already, that will take precedence.
459 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
460 g_param_spec_string ("proxy-id", "proxy-id",
461 "HTTP proxy URI user id for authentication", "",
462 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
464 * GstRTSPSrc:proxy-pw:
466 * Sets the proxy URI password for authentication. If the URI set via the
467 * "proxy" property contains a password already, that will take precedence.
471 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
472 g_param_spec_string ("proxy-pw", "proxy-pw",
473 "HTTP proxy URI user password for authentication", "",
474 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
477 * GstRTSPSrc:rtp-blocksize:
479 * RTP package size to suggest to server.
481 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
482 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
483 "RTP package size to suggest to server (0 = disabled)",
484 0, 65536, DEFAULT_RTP_BLOCKSIZE,
485 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
487 g_object_class_install_property (gobject_class,
489 g_param_spec_string ("user-id", "user-id",
490 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
491 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
492 g_object_class_install_property (gobject_class, PROP_USER_PW,
493 g_param_spec_string ("user-pw", "user-pw",
494 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
495 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
498 * GstRTSPSrc:buffer-mode:
500 * Control the buffering and timestamping mode used by the jitterbuffer.
502 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
503 g_param_spec_enum ("buffer-mode", "Buffer Mode",
504 "Control the buffering algorithm in use",
505 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
506 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
509 * GstRTSPSrc:port-range:
511 * Configure the client port numbers that can be used to recieve RTP and
514 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
515 g_param_spec_string ("port-range", "Port range",
516 "Client port range that can be used to receive RTP and RTCP data, "
517 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
518 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
521 * GstRTSPSrc:udp-buffer-size:
523 * Size of the kernel UDP receive buffer in bytes.
525 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
526 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
527 "Size of the kernel UDP receive buffer in bytes, 0=default",
528 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
529 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
532 * GstRTSPSrc:short-header:
534 * Only send the basic RTSP headers for broken encoders.
536 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
537 g_param_spec_boolean ("short-header", "Short Header",
538 "Only send the basic RTSP headers for broken encoders",
539 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
541 g_object_class_install_property (gobject_class, PROP_PROBATION,
542 g_param_spec_uint ("probation", "Number of probations",
543 "Consecutive packet sequence numbers to accept the source",
544 0, G_MAXUINT, DEFAULT_PROBATION,
545 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
547 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
548 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
549 "Reconnect to the server if RTSP connection is closed when doing UDP",
550 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
552 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
553 g_param_spec_string ("multicast-iface", "Multicast Interface",
554 "The network interface on which to join the multicast group",
555 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
557 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
558 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
559 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
560 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
562 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
563 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
564 "Use the pipeline running-time to set the NTP time in the RTCP SR messages",
565 DEFAULT_USE_PIPELINE_CLOCK,
566 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
568 g_object_class_install_property (gobject_class, PROP_SDES,
569 g_param_spec_boxed ("sdes", "SDES",
570 "The SDES items of this session",
571 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
574 * GstRTSPSrc::tls-validation-flags:
576 * TLS certificate validation flags used to validate server
581 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
582 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
583 "TLS certificate validation flags used to validate the server certificate",
584 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
585 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
588 * GstRTSPSrc::handle-request:
589 * @rtspsrc: a #GstRTSPSrc
590 * @request: a #GstRTSPMessage
591 * @response: a #GstRTSPMessage
593 * Handle a server request in @request and prepare @response.
595 * This signal is called from the streaming thread, you should therefore not
596 * do any state changes on @rtspsrc because this might deadlock. If you want
597 * to modify the state as a result of this signal, post a
598 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
603 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
604 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
605 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
606 G_TYPE_POINTER, G_TYPE_POINTER);
609 * GstRTSPSrc::on-sdp:
610 * @rtspsrc: a #GstRTSPSrc
611 * @sdp: a #GstSDPMessage
613 * Emited when the client has retrieved the SDP and before it configures the
614 * streams in the SDP. @sdp can be inspected and modified.
616 * This signal is called from the streaming thread, you should therefore not
617 * do any state changes on @rtspsrc because this might deadlock. If you want
618 * to modify the state as a result of this signal, post a
619 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
624 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
625 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
626 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
627 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
630 * GstRTSPSrc::select-stream:
631 * @rtspsrc: a #GstRTSPSrc
632 * @num: the stream number
633 * @caps: the stream caps
635 * Emited before the client decides to configure the stream @num with
638 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
643 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
644 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
645 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
646 (GCallback) default_select_stream, select_stream_accum, NULL,
647 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, G_TYPE_UINT,
650 gstelement_class->send_event = gst_rtspsrc_send_event;
651 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
652 gstelement_class->change_state = gst_rtspsrc_change_state;
654 gst_element_class_add_pad_template (gstelement_class,
655 gst_static_pad_template_get (&rtptemplate));
657 gst_element_class_set_static_metadata (gstelement_class,
658 "RTSP packet receiver", "Source/Network",
659 "Receive data over the network via RTSP (RFC 2326)",
660 "Wim Taymans <wim@fluendo.com>, "
661 "Thijs Vermeir <thijs.vermeir@barco.com>, "
662 "Lutz Mueller <lutz@topfrose.de>");
664 gstbin_class->handle_message = gst_rtspsrc_handle_message;
666 gst_rtsp_ext_list_init ();
670 gst_rtspsrc_init (GstRTSPSrc * src)
672 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
673 src->protocols = DEFAULT_PROTOCOLS;
674 src->debug = DEFAULT_DEBUG;
675 src->retry = DEFAULT_RETRY;
676 src->udp_timeout = DEFAULT_TIMEOUT;
677 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
678 src->latency = DEFAULT_LATENCY_MS;
679 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
680 src->connection_speed = DEFAULT_CONNECTION_SPEED;
681 src->nat_method = DEFAULT_NAT_METHOD;
682 src->do_rtcp = DEFAULT_DO_RTCP;
683 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
684 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
685 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
686 src->user_id = g_strdup (DEFAULT_USER_ID);
687 src->user_pw = g_strdup (DEFAULT_USER_PW);
688 src->buffer_mode = DEFAULT_BUFFER_MODE;
689 src->client_port_range.min = 0;
690 src->client_port_range.max = 0;
691 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
692 src->short_header = DEFAULT_SHORT_HEADER;
693 src->probation = DEFAULT_PROBATION;
694 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
695 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
696 src->ntp_sync = DEFAULT_NTP_SYNC;
697 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
699 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
701 /* get a list of all extensions */
702 src->extensions = gst_rtsp_ext_list_get ();
704 /* connect to send signal */
705 gst_rtsp_ext_list_connect (src->extensions, "send",
706 (GCallback) gst_rtspsrc_send_cb, src);
708 /* protects the streaming thread in interleaved mode or the polling
709 * thread in UDP mode. */
710 g_rec_mutex_init (&src->stream_rec_lock);
712 /* protects our state changes from multiple invocations */
713 g_rec_mutex_init (&src->state_rec_lock);
715 src->state = GST_RTSP_STATE_INVALID;
717 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
721 gst_rtspsrc_finalize (GObject * object)
725 rtspsrc = GST_RTSPSRC (object);
727 gst_rtsp_ext_list_free (rtspsrc->extensions);
728 g_free (rtspsrc->conninfo.location);
729 gst_rtsp_url_free (rtspsrc->conninfo.url);
730 g_free (rtspsrc->conninfo.url_str);
731 g_free (rtspsrc->user_id);
732 g_free (rtspsrc->user_pw);
733 g_free (rtspsrc->multi_iface);
736 gst_sdp_message_free (rtspsrc->sdp);
739 if (rtspsrc->provided_clock)
740 gst_object_unref (rtspsrc->provided_clock);
743 gst_structure_free (rtspsrc->sdes);
746 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
747 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
749 G_OBJECT_CLASS (parent_class)->finalize (object);
753 gst_rtspsrc_provide_clock (GstElement * element)
755 GstRTSPSrc *src = GST_RTSPSRC (element);
758 if ((clock = src->provided_clock) != NULL)
759 gst_object_ref (clock);
764 /* a proxy string of the format [user:passwd@]host[:port] */
766 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
770 g_free (rtsp->proxy_user);
771 rtsp->proxy_user = NULL;
772 g_free (rtsp->proxy_passwd);
773 rtsp->proxy_passwd = NULL;
774 g_free (rtsp->proxy_host);
775 rtsp->proxy_host = NULL;
776 rtsp->proxy_port = 0;
783 /* we allow http:// in front but ignore it */
784 if (g_str_has_prefix (p, "http://"))
787 at = strchr (p, '@');
789 /* look for user:passwd */
790 col = strchr (proxy, ':');
791 if (col == NULL || col > at)
794 rtsp->proxy_user = g_strndup (p, col - p);
796 rtsp->proxy_passwd = g_strndup (col, at - col);
801 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
802 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
803 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
804 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
805 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
806 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
807 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
810 col = strchr (p, ':');
813 /* everything before the colon is the hostname */
814 rtsp->proxy_host = g_strndup (p, col - p);
816 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
818 rtsp->proxy_host = g_strdup (p);
819 rtsp->proxy_port = 8080;
825 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
827 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
828 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
831 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
833 rtspsrc->ptcp_timeout = NULL;
837 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
842 rtspsrc = GST_RTSPSRC (object);
846 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
847 g_value_get_string (value), NULL);
850 rtspsrc->protocols = g_value_get_flags (value);
853 rtspsrc->debug = g_value_get_boolean (value);
856 rtspsrc->retry = g_value_get_uint (value);
859 rtspsrc->udp_timeout = g_value_get_uint64 (value);
861 case PROP_TCP_TIMEOUT:
862 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
865 rtspsrc->latency = g_value_get_uint (value);
867 case PROP_DROP_ON_LATENCY:
868 rtspsrc->drop_on_latency = g_value_get_boolean (value);
870 case PROP_CONNECTION_SPEED:
871 rtspsrc->connection_speed = g_value_get_uint64 (value);
873 case PROP_NAT_METHOD:
874 rtspsrc->nat_method = g_value_get_enum (value);
877 rtspsrc->do_rtcp = g_value_get_boolean (value);
879 case PROP_DO_RTSP_KEEP_ALIVE:
880 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
883 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
886 if (rtspsrc->prop_proxy_id)
887 g_free (rtspsrc->prop_proxy_id);
888 rtspsrc->prop_proxy_id = g_value_dup_string (value);
891 if (rtspsrc->prop_proxy_pw)
892 g_free (rtspsrc->prop_proxy_pw);
893 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
895 case PROP_RTP_BLOCKSIZE:
896 rtspsrc->rtp_blocksize = g_value_get_uint (value);
899 if (rtspsrc->user_id)
900 g_free (rtspsrc->user_id);
901 rtspsrc->user_id = g_value_dup_string (value);
904 if (rtspsrc->user_pw)
905 g_free (rtspsrc->user_pw);
906 rtspsrc->user_pw = g_value_dup_string (value);
908 case PROP_BUFFER_MODE:
909 rtspsrc->buffer_mode = g_value_get_enum (value);
911 case PROP_PORT_RANGE:
915 str = g_value_get_string (value);
917 sscanf (str, "%u-%u",
918 &rtspsrc->client_port_range.min, &rtspsrc->client_port_range.max);
920 rtspsrc->client_port_range.min = 0;
921 rtspsrc->client_port_range.max = 0;
925 case PROP_UDP_BUFFER_SIZE:
926 rtspsrc->udp_buffer_size = g_value_get_int (value);
928 case PROP_SHORT_HEADER:
929 rtspsrc->short_header = g_value_get_boolean (value);
932 rtspsrc->probation = g_value_get_uint (value);
934 case PROP_UDP_RECONNECT:
935 rtspsrc->udp_reconnect = g_value_get_boolean (value);
937 case PROP_MULTICAST_IFACE:
938 g_free (rtspsrc->multi_iface);
940 if (g_value_get_string (value) == NULL)
941 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
943 rtspsrc->multi_iface = g_value_dup_string (value);
946 rtspsrc->ntp_sync = g_value_get_boolean (value);
948 case PROP_USE_PIPELINE_CLOCK:
949 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
952 rtspsrc->sdes = g_value_dup_boxed (value);
954 case PROP_TLS_VALIDATION_FLAGS:
955 rtspsrc->tls_validation_flags = g_value_get_flags (value);
958 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
964 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
969 rtspsrc = GST_RTSPSRC (object);
973 g_value_set_string (value, rtspsrc->conninfo.location);
976 g_value_set_flags (value, rtspsrc->protocols);
979 g_value_set_boolean (value, rtspsrc->debug);
982 g_value_set_uint (value, rtspsrc->retry);
985 g_value_set_uint64 (value, rtspsrc->udp_timeout);
987 case PROP_TCP_TIMEOUT:
991 timeout = rtspsrc->tcp_timeout.tv_sec * G_USEC_PER_SEC +
992 rtspsrc->tcp_timeout.tv_usec;
993 g_value_set_uint64 (value, timeout);
997 g_value_set_uint (value, rtspsrc->latency);
999 case PROP_DROP_ON_LATENCY:
1000 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1002 case PROP_CONNECTION_SPEED:
1003 g_value_set_uint64 (value, rtspsrc->connection_speed);
1005 case PROP_NAT_METHOD:
1006 g_value_set_enum (value, rtspsrc->nat_method);
1009 g_value_set_boolean (value, rtspsrc->do_rtcp);
1011 case PROP_DO_RTSP_KEEP_ALIVE:
1012 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1018 if (rtspsrc->proxy_host) {
1020 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1024 g_value_take_string (value, str);
1028 g_value_set_string (value, rtspsrc->prop_proxy_id);
1031 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1033 case PROP_RTP_BLOCKSIZE:
1034 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1037 g_value_set_string (value, rtspsrc->user_id);
1040 g_value_set_string (value, rtspsrc->user_pw);
1042 case PROP_BUFFER_MODE:
1043 g_value_set_enum (value, rtspsrc->buffer_mode);
1045 case PROP_PORT_RANGE:
1049 if (rtspsrc->client_port_range.min != 0) {
1050 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1051 rtspsrc->client_port_range.max);
1055 g_value_take_string (value, str);
1058 case PROP_UDP_BUFFER_SIZE:
1059 g_value_set_int (value, rtspsrc->udp_buffer_size);
1061 case PROP_SHORT_HEADER:
1062 g_value_set_boolean (value, rtspsrc->short_header);
1064 case PROP_PROBATION:
1065 g_value_set_uint (value, rtspsrc->probation);
1067 case PROP_UDP_RECONNECT:
1068 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1070 case PROP_MULTICAST_IFACE:
1071 g_value_set_string (value, rtspsrc->multi_iface);
1074 g_value_set_boolean (value, rtspsrc->ntp_sync);
1076 case PROP_USE_PIPELINE_CLOCK:
1077 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1080 g_value_set_boxed (value, rtspsrc->sdes);
1082 case PROP_TLS_VALIDATION_FLAGS:
1083 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1086 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1092 find_stream_by_id (GstRTSPStream * stream, gint * id)
1094 if (stream->id == *id)
1101 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1103 if (stream->channel[0] == *channel || stream->channel[1] == *channel)
1110 find_stream_by_pt (GstRTSPStream * stream, gint * pt)
1112 if (stream->pt == *pt)
1119 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1121 GstElement *src = (GstElement *) a;
1123 if (stream->udpsrc[0] == src)
1125 if (stream->udpsrc[1] == src)
1132 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1134 /* check qualified setup_url */
1135 if (!strcmp (stream->conninfo.location, (gchar *) a))
1137 /* check original control_url */
1138 if (!strcmp (stream->control_url, (gchar *) a))
1141 /* check if qualified setup_url ends with string */
1142 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1148 static GstRTSPStream *
1149 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1153 /* find and get stream */
1154 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1155 return (GstRTSPStream *) lstream->data;
1160 static const GstSDPBandwidth *
1161 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1162 const GstSDPMedia * media, const gchar * type)
1166 /* first look in the media specific section */
1167 len = gst_sdp_media_bandwidths_len (media);
1168 for (i = 0; i < len; i++) {
1169 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1171 if (strcmp (bw->bwtype, type) == 0)
1174 /* then look in the message specific section */
1175 len = gst_sdp_message_bandwidths_len (sdp);
1176 for (i = 0; i < len; i++) {
1177 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1179 if (strcmp (bw->bwtype, type) == 0)
1186 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1187 const GstSDPMedia * media, GstRTSPStream * stream)
1189 const GstSDPBandwidth *bw;
1191 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1192 stream->as_bandwidth = bw->bandwidth;
1194 stream->as_bandwidth = -1;
1196 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1197 stream->rr_bandwidth = bw->bandwidth;
1199 stream->rr_bandwidth = -1;
1201 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1202 stream->rs_bandwidth = bw->bandwidth;
1204 stream->rs_bandwidth = -1;
1208 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1209 const GstSDPConnection * conn)
1211 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1214 if (conn->addrtype == NULL)
1217 /* check for IPV6 */
1218 if (strcmp (conn->addrtype, "IP4") == 0)
1219 stream->is_ipv6 = FALSE;
1220 else if (strcmp (conn->addrtype, "IP6") == 0)
1221 stream->is_ipv6 = TRUE;
1226 g_free (stream->destination);
1227 stream->destination = g_strdup (conn->address);
1229 /* check for multicast */
1230 stream->is_multicast =
1231 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1233 stream->ttl = conn->ttl;
1236 /* Go over the connections for a stream.
1237 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1239 * - If we are dealing with a localhost address, we disable multicast
1242 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1243 const GstSDPMedia * media, GstRTSPStream * stream)
1245 const GstSDPConnection *conn;
1248 /* first look in the media specific section */
1249 len = gst_sdp_media_connections_len (media);
1250 for (i = 0; i < len; i++) {
1251 conn = gst_sdp_media_get_connection (media, i);
1253 gst_rtspsrc_do_stream_connection (src, stream, conn);
1255 /* then look in the message specific section */
1256 if ((conn = gst_sdp_message_get_connection (sdp))) {
1257 gst_rtspsrc_do_stream_connection (src, stream, conn);
1261 static GstRTSPStream *
1262 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
1264 GstRTSPStream *stream;
1265 const gchar *control_url;
1266 const gchar *payload;
1267 const GstSDPMedia *media;
1269 /* get media, should not return NULL */
1270 media = gst_sdp_message_get_media (sdp, idx);
1274 stream = g_new0 (GstRTSPStream, 1);
1275 stream->parent = src;
1276 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1278 stream->last_ret = GST_FLOW_NOT_LINKED;
1279 stream->added = FALSE;
1280 stream->disabled = FALSE;
1281 stream->id = src->numstreams++;
1282 stream->eos = FALSE;
1283 stream->discont = TRUE;
1284 stream->seqbase = -1;
1285 stream->timebase = -1;
1287 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1288 * session manager to scale RTCP. */
1289 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1291 /* collect connection info */
1292 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1294 /* we must have a payload. No payload means we cannot create caps */
1295 /* FIXME, handle multiple formats. The problem here is that we just want to
1296 * take the first available format that we can handle but in order to do that
1297 * we need to scan for depayloader plugins. Scanning for payloader plugins is
1298 * also suboptimal because the user maybe just wants to save the raw stream
1299 * and then we don't care. */
1300 if ((payload = gst_sdp_media_get_format (media, 0))) {
1301 stream->pt = atoi (payload);
1303 stream->caps = gst_rtspsrc_media_to_caps (stream->pt, media);
1305 GST_DEBUG ("mapping sdp session level attributes to caps");
1306 gst_rtspsrc_sdp_attributes_to_caps (sdp->attributes, stream->caps);
1307 GST_DEBUG ("mapping sdp media level attributes to caps");
1308 gst_rtspsrc_sdp_attributes_to_caps (media->attributes, stream->caps);
1310 if (stream->pt >= 96) {
1311 /* If we have a dynamic payload type, see if we have a stream with the
1312 * same payload number. If there is one, they are part of the same
1313 * container and we only need to add one pad. */
1314 if (find_stream (src, &stream->pt, (gpointer) find_stream_by_pt)) {
1315 stream->container = TRUE;
1316 GST_DEBUG ("found another stream with pt %d, marking as container",
1321 /* collect port number */
1322 stream->port = gst_sdp_media_get_port (media);
1324 /* get control url to construct the setup url. The setup url is used to
1325 * configure the transport of the stream and is used to identity the stream in
1326 * the RTP-Info header field returned from PLAY. */
1327 control_url = gst_sdp_media_get_attribute_val (media, "control");
1328 if (control_url == NULL)
1329 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1331 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1332 GST_DEBUG_OBJECT (src, " pt: %d", stream->pt);
1333 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1334 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1335 GST_DEBUG_OBJECT (src, " caps: %" GST_PTR_FORMAT, stream->caps);
1336 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1338 if (control_url != NULL) {
1339 stream->control_url = g_strdup (control_url);
1340 /* Build a fully qualified url using the content_base if any or by prefixing
1341 * the original request.
1342 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1343 * likely build a URL that the server will fail to understand, this is ok,
1344 * we will fail then. */
1345 if (g_str_has_prefix (control_url, "rtsp://"))
1346 stream->conninfo.location = g_strdup (control_url);
1351 if (g_strcmp0 (control_url, "*") == 0)
1355 base = src->control;
1356 else if (src->content_base)
1357 base = src->content_base;
1358 else if (src->conninfo.url_str)
1359 base = src->conninfo.url_str;
1363 /* check if the base ends or control starts with / */
1364 has_slash = g_str_has_prefix (control_url, "/");
1365 has_slash = has_slash || g_str_has_suffix (base, "/");
1367 /* concatenate the two strings, insert / when not present */
1368 stream->conninfo.location =
1369 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1372 GST_DEBUG_OBJECT (src, " setup: %s",
1373 GST_STR_NULL (stream->conninfo.location));
1375 /* we keep track of all streams */
1376 src->streams = g_list_append (src->streams, stream);
1384 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1388 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1391 gst_caps_unref (stream->caps);
1393 g_free (stream->destination);
1394 g_free (stream->control_url);
1395 g_free (stream->conninfo.location);
1397 for (i = 0; i < 2; i++) {
1398 if (stream->udpsrc[i]) {
1399 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1400 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1401 gst_object_unref (stream->udpsrc[i]);
1402 stream->udpsrc[i] = NULL;
1404 if (stream->channelpad[i]) {
1405 gst_object_unref (stream->channelpad[i]);
1406 stream->channelpad[i] = NULL;
1408 if (stream->udpsink[i]) {
1409 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1410 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1411 gst_object_unref (stream->udpsink[i]);
1412 stream->udpsink[i] = NULL;
1415 if (stream->fakesrc) {
1416 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1417 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1418 gst_object_unref (stream->fakesrc);
1419 stream->fakesrc = NULL;
1421 if (stream->srcpad) {
1422 gst_pad_set_active (stream->srcpad, FALSE);
1423 if (stream->added) {
1424 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1425 stream->added = FALSE;
1427 stream->srcpad = NULL;
1429 if (stream->rtcppad) {
1430 gst_object_unref (stream->rtcppad);
1431 stream->rtcppad = NULL;
1433 if (stream->session) {
1434 g_object_unref (stream->session);
1435 stream->session = NULL;
1441 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1445 GST_DEBUG_OBJECT (src, "cleanup");
1447 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1448 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1450 gst_rtspsrc_stream_free (src, stream);
1452 g_list_free (src->streams);
1453 src->streams = NULL;
1455 if (src->manager_sig_id) {
1456 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1457 src->manager_sig_id = 0;
1459 gst_element_set_state (src->manager, GST_STATE_NULL);
1460 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1461 src->manager = NULL;
1463 src->numstreams = 0;
1465 gst_structure_free (src->props);
1468 g_free (src->content_base);
1469 src->content_base = NULL;
1471 g_free (src->control);
1472 src->control = NULL;
1475 gst_rtsp_range_free (src->range);
1478 /* don't clear the SDP when it was used in the url */
1479 if (src->sdp && !src->from_sdp) {
1480 gst_sdp_message_free (src->sdp);
1483 if (src->start_segment) {
1484 gst_event_unref (src->start_segment);
1485 src->start_segment = NULL;
1487 if (src->provided_clock) {
1488 gst_object_unref (src->provided_clock);
1489 src->provided_clock = NULL;
1493 #define PARSE_INT(p, del, res) \
1496 p = strstr (p, del); \
1506 #define PARSE_STRING(p, del, res) \
1509 p = strstr (p, del); \
1521 #define SKIP_SPACES(p) \
1522 while (*p && g_ascii_isspace (*p)) \
1527 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1530 gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
1531 gint * rate, gchar ** params)
1535 p = (gchar *) rtpmap;
1537 PARSE_INT (p, " ", *payload);
1545 PARSE_STRING (p, "/", *name);
1546 if (*name == NULL) {
1547 GST_DEBUG ("no rate, name %s", p);
1548 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
1549 * streams seem to omit the rate. */
1556 p = strstr (p, "/");
1574 * Mapping SDP attributes to caps
1576 * prepend 'a-' to IANA registered sdp attributes names
1577 * (ie: not prefixed with 'x-') in order to avoid
1578 * collision with gstreamer standard caps properties names
1581 gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
1583 if (attributes->len > 0) {
1587 s = gst_caps_get_structure (caps, 0);
1589 for (i = 0; i < attributes->len; i++) {
1590 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
1591 gchar *tofree, *key;
1595 /* skip some of the attribute we already handle */
1596 if (!strcmp (key, "fmtp"))
1598 if (!strcmp (key, "rtpmap"))
1600 if (!strcmp (key, "control"))
1602 if (!strcmp (key, "range"))
1605 /* string must be valid UTF8 */
1606 if (!g_utf8_validate (attr->value, -1, NULL))
1609 if (!g_str_has_prefix (key, "x-"))
1610 tofree = key = g_strdup_printf ("a-%s", key);
1614 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
1615 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
1622 * Mapping of caps to and from SDP fields:
1624 * m=<media> <UDP port> RTP/AVP <payload>
1625 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1626 * a=fmtp:<payload> <param>[=<value>];...
1629 gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
1632 const gchar *rtpmap;
1636 gchar *params = NULL;
1642 /* get and parse rtpmap */
1643 if ((rtpmap = gst_sdp_media_get_attribute_val (media, "rtpmap"))) {
1644 ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
1646 if (payload != pt) {
1647 /* we ignore the rtpmap if the payload type is different. */
1648 g_warning ("rtpmap of wrong payload type, ignoring");
1654 /* if we failed to parse the rtpmap for a dynamic payload type, we have an
1658 /* else we can ignore */
1659 g_warning ("error parsing rtpmap, ignoring");
1662 /* dynamic payloads need rtpmap or we fail */
1666 /* check if we have a rate, if not, we need to look up the rate from the
1667 * default rates based on the payload types. */
1669 const GstRTPPayloadInfo *info;
1671 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
1672 /* dynamic types, use media and encoding_name */
1673 tmp = g_ascii_strdown (media->media, -1);
1674 info = gst_rtp_payload_info_for_name (tmp, name);
1677 /* static types, use payload type */
1678 info = gst_rtp_payload_info_for_pt (pt);
1682 if ((rate = info->clock_rate) == 0)
1685 /* we fail if we cannot find one */
1690 tmp = g_ascii_strdown (media->media, -1);
1691 caps = gst_caps_new_simple ("application/x-unknown",
1692 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
1694 s = gst_caps_get_structure (caps, 0);
1696 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
1698 /* encoding name must be upper case */
1700 tmp = g_ascii_strup (name, -1);
1701 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
1705 /* params must be lower case */
1706 if (params != NULL) {
1707 tmp = g_ascii_strdown (params, -1);
1708 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
1712 /* parse optional fmtp: field */
1713 if ((fmtp = gst_sdp_media_get_attribute_val (media, "fmtp"))) {
1719 /* p is now of the format <payload> <param>[=<value>];... */
1720 PARSE_INT (p, " ", payload);
1721 if (payload != -1 && payload == pt) {
1725 /* <param>[=<value>] are separated with ';' */
1726 pairs = g_strsplit (p, ";", 0);
1727 for (i = 0; pairs[i]; i++) {
1729 const gchar *val, *key;
1731 /* the key may not have a '=', the value can have other '='s */
1732 valpos = strstr (pairs[i], "=");
1734 /* we have a '=' and thus a value, remove the '=' with \0 */
1736 /* value is everything between '=' and ';'. We split the pairs at ;
1737 * boundaries so we can take the remainder of the value. Some servers
1738 * put spaces around the value which we strip off here. Alternatively
1739 * we could strip those spaces in the depayloaders should these spaces
1740 * actually carry any meaning in the future. */
1741 val = g_strstrip (valpos + 1);
1743 /* simple <param>;.. is translated into <param>=1;... */
1746 /* strip the key of spaces, convert key to lowercase but not the value. */
1747 key = g_strstrip (pairs[i]);
1748 if (strlen (key) > 1) {
1749 tmp = g_ascii_strdown (key, -1);
1750 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
1762 g_warning ("rtpmap type not given for dynamic payload %d", pt);
1767 g_warning ("rate unknown for payload type %d", pt);
1773 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
1774 gint * rtpport, gint * rtcpport)
1777 GstStateChangeReturn ret;
1778 GstElement *udpsrc0, *udpsrc1;
1779 gint tmp_rtp, tmp_rtcp;
1783 src = stream->parent;
1789 /* Start at next port */
1790 tmp_rtp = src->next_port_num;
1792 if (stream->is_ipv6)
1793 host = "udp://[::0]";
1795 host = "udp://0.0.0.0";
1797 /* try to allocate 2 UDP ports, the RTP port should be an even
1798 * number and the RTCP port should be the next (uneven) port */
1801 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
1802 tmp_rtp >= src->client_port_range.max)
1805 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1806 if (udpsrc0 == NULL)
1807 goto no_udp_protocol;
1808 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
1810 if (src->udp_buffer_size != 0)
1811 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
1814 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
1815 if (ret == GST_STATE_CHANGE_FAILURE) {
1817 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
1820 if (++count > src->retry)
1823 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1824 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1825 gst_object_unref (udpsrc0);
1828 GST_DEBUG_OBJECT (src, "retry %d", count);
1831 goto no_udp_protocol;
1834 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
1835 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
1837 /* check if port is even */
1838 if ((tmp_rtp & 0x01) != 0) {
1839 /* port not even, close and allocate another */
1840 if (++count > src->retry)
1843 GST_DEBUG_OBJECT (src, "RTP port not even");
1845 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1846 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1847 gst_object_unref (udpsrc0);
1850 GST_DEBUG_OBJECT (src, "retry %d", count);
1855 /* allocate port+1 for RTCP now */
1856 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1857 if (udpsrc1 == NULL)
1858 goto no_udp_rtcp_protocol;
1861 tmp_rtcp = tmp_rtp + 1;
1862 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
1865 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
1867 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
1868 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
1869 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
1870 if (ret == GST_STATE_CHANGE_FAILURE) {
1871 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
1873 if (++count > src->retry)
1876 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1877 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1878 gst_object_unref (udpsrc0);
1881 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
1882 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1883 gst_object_unref (udpsrc1);
1887 GST_DEBUG_OBJECT (src, "retry %d", count);
1891 /* all fine, do port check */
1892 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
1893 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
1895 /* this should not happen... */
1896 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
1899 /* we keep these elements, we configure all in configure_transport when the
1900 * server told us to really use the UDP ports. */
1901 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
1902 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
1903 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
1904 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
1906 /* keep track of next available port number when we have a range
1908 if (src->next_port_num != 0)
1909 src->next_port_num = tmp_rtcp + 1;
1916 GST_DEBUG_OBJECT (src, "could not get UDP source");
1921 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
1925 no_udp_rtcp_protocol:
1927 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
1932 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
1933 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
1939 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1940 gst_object_unref (udpsrc0);
1943 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1944 gst_object_unref (udpsrc1);
1951 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
1956 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
1958 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1959 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1962 for (i = 0; i < 2; i++) {
1963 if (stream->udpsrc[i])
1964 gst_element_set_state (stream->udpsrc[i], state);
1970 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
1977 event = gst_event_new_flush_start ();
1978 GST_DEBUG_OBJECT (src, "start flush");
1980 state = GST_STATE_PAUSED;
1982 event = gst_event_new_flush_stop (FALSE);
1983 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
1986 state = GST_STATE_PLAYING;
1988 state = GST_STATE_PAUSED;
1990 gst_rtspsrc_push_event (src, event);
1991 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
1992 gst_rtspsrc_set_state (src, state);
1995 static GstRTSPResult
1996 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
1997 GstRTSPMessage * message, GTimeVal * timeout)
2002 ret = gst_rtsp_connection_send (conn, message, timeout);
2004 ret = GST_RTSP_ERROR;
2009 static GstRTSPResult
2010 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
2011 GstRTSPMessage * message, GTimeVal * timeout)
2016 ret = gst_rtsp_connection_receive (conn, message, timeout);
2018 ret = GST_RTSP_ERROR;
2024 gst_rtspsrc_get_position (GstRTSPSrc * src)
2029 query = gst_query_new_position (GST_FORMAT_TIME);
2030 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2031 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2032 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2036 if (stream->srcpad) {
2037 if (gst_pad_query (stream->srcpad, query)) {
2038 gst_query_parse_position (query, &fmt, &pos);
2039 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2040 GST_TIME_ARGS (pos));
2041 src->last_pos = pos;
2051 gst_rtspsrc_do_seek (GstRTSPSrc * src, GstSegment * segment)
2053 src->state = GST_RTSP_STATE_SEEKING;
2054 /* PLAY will add the range header now. */
2055 src->need_range = TRUE;
2061 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2066 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2068 gboolean flush, skip;
2071 GstSegment seeksegment = { 0, };
2075 GST_DEBUG_OBJECT (src, "doing seek with event");
2077 gst_event_parse_seek (event, &rate, &format, &flags,
2078 &cur_type, &cur, &stop_type, &stop);
2080 /* no negative rates yet */
2084 /* we need TIME format */
2085 if (format != src->segment.format)
2088 GST_DEBUG_OBJECT (src, "doing seek without event");
2090 cur_type = GST_SEEK_TYPE_SET;
2091 stop_type = GST_SEEK_TYPE_SET;
2094 /* get flush flag */
2095 flush = flags & GST_SEEK_FLAG_FLUSH;
2096 skip = flags & GST_SEEK_FLAG_SKIP;
2098 /* now we need to make sure the streaming thread is stopped. We do this by
2099 * either sending a FLUSH_START event downstream which will cause the
2100 * streaming thread to stop with a WRONG_STATE.
2101 * For a non-flushing seek we simply pause the task, which will happen as soon
2102 * as it completes one iteration (and thus might block when the sink is
2103 * blocking in preroll). */
2105 GST_DEBUG_OBJECT (src, "starting flush");
2106 gst_rtspsrc_flush (src, TRUE, FALSE);
2109 gst_task_pause (src->task);
2113 /* we should now be able to grab the streaming thread because we stopped it
2114 * with the above flush/pause code */
2115 GST_RTSP_STREAM_LOCK (src);
2117 GST_DEBUG_OBJECT (src, "stopped streaming");
2119 /* copy segment, we need this because we still need the old
2120 * segment when we close the current segment. */
2121 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2123 /* configure the seek parameters in the seeksegment. We will then have the
2124 * right values in the segment to perform the seek */
2126 GST_DEBUG_OBJECT (src, "configuring seek");
2127 gst_segment_do_seek (&seeksegment, rate, format, flags,
2128 cur_type, cur, stop_type, stop, &update);
2131 /* figure out the last position we need to play. If it's configured (stop !=
2132 * -1), use that, else we play until the total duration of the file */
2133 if ((stop = seeksegment.stop) == -1)
2134 stop = seeksegment.duration;
2136 playing = (src->state == GST_RTSP_STATE_PLAYING);
2138 /* if we were playing, pause first */
2140 /* obtain current position in case seek fails */
2141 gst_rtspsrc_get_position (src);
2142 gst_rtspsrc_pause (src, FALSE);
2146 gst_rtspsrc_do_seek (src, &seeksegment);
2148 /* and continue playing */
2150 gst_rtspsrc_play (src, &seeksegment, FALSE);
2152 /* prepare for streaming again */
2154 /* if we started flush, we stop now */
2155 GST_DEBUG_OBJECT (src, "stopping flush");
2156 gst_rtspsrc_flush (src, FALSE, playing);
2159 /* now we did the seek and can activate the new segment values */
2160 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2162 /* if we're doing a segment seek, post a SEGMENT_START message */
2163 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2164 gst_element_post_message (GST_ELEMENT_CAST (src),
2165 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2166 src->segment.format, src->segment.position));
2169 /* now create the newsegment */
2170 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2171 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2174 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2175 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2176 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2177 stream->discont = TRUE;
2180 GST_RTSP_STREAM_UNLOCK (src);
2187 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2192 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2198 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2202 gboolean res = TRUE;
2205 src = GST_RTSPSRC_CAST (parent);
2207 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2208 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2210 switch (GST_EVENT_TYPE (event)) {
2211 case GST_EVENT_SEEK:
2212 res = gst_rtspsrc_perform_seek (src, event);
2216 case GST_EVENT_NAVIGATION:
2217 case GST_EVENT_LATENCY:
2225 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2226 res = gst_pad_send_event (target, event);
2227 gst_object_unref (target);
2229 gst_event_unref (event);
2232 gst_event_unref (event);
2238 /* this is the final event function we receive on the internal source pad when
2239 * we deal with TCP connections */
2241 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2246 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2248 switch (GST_EVENT_TYPE (event)) {
2249 case GST_EVENT_SEEK:
2251 case GST_EVENT_NAVIGATION:
2252 case GST_EVENT_LATENCY:
2254 gst_event_unref (event);
2261 /* this is the final query function we receive on the internal source pad when
2262 * we deal with TCP connections */
2264 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2268 gboolean res = TRUE;
2270 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2272 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2273 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2275 switch (GST_QUERY_TYPE (query)) {
2276 case GST_QUERY_POSITION:
2281 case GST_QUERY_DURATION:
2285 gst_query_parse_duration (query, &format, NULL);
2288 case GST_FORMAT_TIME:
2289 gst_query_set_duration (query, format, src->segment.duration);
2297 case GST_QUERY_LATENCY:
2299 /* we are live with a min latency of 0 and unlimited max latency, this
2300 * result will be updated by the session manager if there is any. */
2301 gst_query_set_latency (query, TRUE, 0, -1);
2311 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2313 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2317 gboolean res = FALSE;
2319 src = GST_RTSPSRC_CAST (parent);
2321 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2322 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2324 switch (GST_QUERY_TYPE (query)) {
2325 case GST_QUERY_DURATION:
2329 gst_query_parse_duration (query, &format, NULL);
2332 case GST_FORMAT_TIME:
2333 gst_query_set_duration (query, format, src->segment.duration);
2341 case GST_QUERY_SEEKING:
2345 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2346 if (format == GST_FORMAT_TIME) {
2348 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2350 /* seeking without duration is unlikely */
2351 seekable = seekable && src->seekable && src->segment.duration &&
2352 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2354 /* FIXME ?? should we have 0 and segment.duration here; see demuxers */
2355 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
2356 src->segment.start, src->segment.stop);
2365 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2367 gst_query_set_uri (query, uri);
2375 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2377 /* forward the query to the proxy target pad */
2379 res = gst_pad_query (target, query);
2380 gst_object_unref (target);
2389 /* callback for RTCP messages to be sent to the server when operating in TCP
2391 static GstFlowReturn
2392 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2395 GstRTSPStream *stream;
2396 GstFlowReturn res = GST_FLOW_OK;
2401 GstRTSPMessage message = { 0 };
2402 GstRTSPConnection *conn;
2404 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2405 src = stream->parent;
2407 gst_buffer_map (buffer, &map, GST_MAP_READ);
2411 gst_rtsp_message_init_data (&message, stream->channel[1]);
2413 /* lend the body data to the message */
2414 gst_rtsp_message_take_body (&message, data, size);
2416 if (stream->conninfo.connection)
2417 conn = stream->conninfo.connection;
2419 conn = src->conninfo.connection;
2421 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2422 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2423 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2425 /* and steal it away again because we will free it when unreffing the
2427 gst_rtsp_message_steal_body (&message, &data, &size);
2428 gst_rtsp_message_unset (&message);
2430 gst_buffer_unmap (buffer, &map);
2431 gst_buffer_unref (buffer);
2436 static GstPadProbeReturn
2437 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2439 GstRTSPSrc *src = user_data;
2441 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2442 GST_DEBUG_PAD_NAME (pad));
2444 /* activate the streams */
2445 GST_OBJECT_LOCK (src);
2446 if (!src->need_activate)
2449 src->need_activate = FALSE;
2450 GST_OBJECT_UNLOCK (src);
2452 gst_rtspsrc_activate_streams (src);
2454 return GST_PAD_PROBE_OK;
2458 GST_OBJECT_UNLOCK (src);
2459 return GST_PAD_PROBE_OK;
2463 /* this callback is called when the session manager generated a new src pad with
2464 * payloaded RTP packets. We simply ghost the pad here. */
2466 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2469 GstPadTemplate *template;
2472 GstRTSPStream *stream;
2475 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2477 GST_RTSP_STATE_LOCK (src);
2479 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2480 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2481 goto unknown_stream;
2483 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2485 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2487 goto unknown_stream;
2490 stream->ssrc = ssrc;
2492 /* we'll add it later see below */
2493 stream->added = TRUE;
2495 /* check if we added all streams */
2497 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
2498 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
2500 GST_DEBUG_OBJECT (src, "stream %p, container %d, disabled %d, added %d",
2501 ostream, ostream->container, ostream->disabled, ostream->added);
2503 /* a container stream only needs one pad added. Also disabled streams don't
2505 if (!ostream->container && !ostream->disabled && !ostream->added) {
2510 GST_RTSP_STATE_UNLOCK (src);
2512 /* create a new pad we will use to stream to */
2513 template = gst_static_pad_template_get (&rtptemplate);
2514 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2515 gst_object_unref (template);
2518 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2519 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2520 gst_pad_set_active (stream->srcpad, TRUE);
2521 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2524 GST_DEBUG_OBJECT (src, "We added all streams");
2525 /* when we get here, all stream are added and we can fire the no-more-pads
2527 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2535 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2536 GST_RTSP_STATE_UNLOCK (src);
2543 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2545 GstRTSPStream *stream;
2548 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2550 GST_RTSP_STATE_LOCK (src);
2551 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2553 goto unknown_stream;
2555 caps = stream->caps;
2557 gst_caps_ref (caps);
2558 GST_RTSP_STATE_UNLOCK (src);
2564 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2565 GST_RTSP_STATE_UNLOCK (src);
2571 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2573 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2579 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2585 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2591 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2593 GstRTSPSrc *src = stream->parent;
2596 g_object_get (source, "ssrc", &ssrc, NULL);
2598 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
2599 ssrc, stream->ssrc, stream->id);
2601 if (ssrc == stream->ssrc)
2602 gst_rtspsrc_do_stream_eos (src, stream);
2606 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2608 GstRTSPSrc *src = stream->parent;
2611 g_object_get (source, "ssrc", &ssrc, NULL);
2613 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
2614 ssrc, stream->ssrc, stream->id);
2616 if (ssrc == stream->ssrc)
2617 gst_rtspsrc_do_stream_eos (src, stream);
2621 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2623 GstRTSPStream *stream;
2625 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2627 /* get stream for session */
2628 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2630 gst_rtspsrc_do_stream_eos (src, stream);
2635 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2637 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2642 set_manager_buffer_mode (GstRTSPSrc * src)
2644 GObjectClass *klass;
2646 if (src->manager == NULL)
2649 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2651 if (!g_object_class_find_property (klass, "buffer-mode"))
2654 if (src->buffer_mode != BUFFER_MODE_AUTO) {
2655 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
2660 GST_DEBUG_OBJECT (src,
2661 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
2663 if (src->provided_clock) {
2664 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
2666 if (clock == src->provided_clock) {
2667 GST_DEBUG_OBJECT (src, "selected synced");
2668 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
2671 gst_object_unref (clock);
2676 /* Otherwise fall-through and use another buffer mode */
2678 gst_object_unref (clock);
2681 GST_DEBUG_OBJECT (src, "auto buffering mode");
2682 if (src->use_buffering) {
2683 GST_DEBUG_OBJECT (src, "selected buffer");
2684 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
2686 GST_DEBUG_OBJECT (src, "selected slave");
2687 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
2691 /* try to get and configure a manager */
2693 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
2694 GstRTSPTransport * transport)
2696 const gchar *manager;
2698 GstStateChangeReturn ret;
2700 /* find a manager */
2701 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
2705 GST_DEBUG_OBJECT (src, "using manager %s", manager);
2707 /* configure the manager */
2708 if (src->manager == NULL) {
2709 GObjectClass *klass;
2711 const gchar *encoding;
2712 gboolean need_slave;
2714 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
2716 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
2720 goto use_no_manager;
2722 if (!(src->manager = gst_element_factory_make (manager, "manager")))
2723 goto manager_failed;
2726 /* we manage this element */
2727 gst_element_set_locked_state (src->manager, TRUE);
2728 gst_bin_add (GST_BIN_CAST (src), src->manager);
2730 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
2731 if (ret == GST_STATE_CHANGE_FAILURE)
2732 goto start_manager_failure;
2734 g_object_set (src->manager, "latency", src->latency, NULL);
2736 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2738 if (g_object_class_find_property (klass, "ntp-sync")) {
2739 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
2742 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
2743 g_object_set (src->manager, "use-pipeline-clock",
2744 src->use_pipeline_clock, NULL);
2747 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
2748 g_object_set (src->manager, "sdes", src->sdes, NULL);
2751 if (g_object_class_find_property (klass, "drop-on-latency")) {
2752 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
2756 /* buffer mode pauses are handled by adding offsets to buffer times,
2757 * but some depayloaders may have a hard time syncing output times
2758 * with such input times, e.g. container ones, most notably ASF */
2759 /* TODO alternatives are having an event that indicates these shifts,
2760 * or having rtsp extensions provide suggestion on buffer mode */
2761 need_slave = stream->container;
2762 if (stream->caps && (s = gst_caps_get_structure (stream->caps, 0)) &&
2763 (encoding = gst_structure_get_string (s, "encoding-name")))
2764 need_slave = need_slave || (strcmp (encoding, "X-ASF-PF") == 0);
2765 /* valid duration implies not likely live pipeline,
2766 * so slaving in jitterbuffer does not make much sense
2767 * (and might mess things up due to bursts) */
2768 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
2769 src->segment.duration && !need_slave) {
2770 src->use_buffering = TRUE;
2772 src->use_buffering = FALSE;
2775 set_manager_buffer_mode (src);
2777 /* connect to signals if we did not already do so */
2778 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
2780 src->manager_sig_id =
2781 g_signal_connect (src->manager, "pad-added",
2782 (GCallback) new_manager_pad, src);
2783 src->manager_ptmap_id =
2784 g_signal_connect (src->manager, "request-pt-map",
2785 (GCallback) request_pt_map, src);
2787 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
2791 /* we stream directly to the manager, get some pads. Each RTSP stream goes
2792 * into a separate RTP session. */
2793 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
2794 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
2796 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
2797 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
2800 /* now configure the bandwidth in the manager */
2801 if (g_signal_lookup ("get-internal-session",
2802 G_OBJECT_TYPE (src->manager)) != 0) {
2803 GObject *rtpsession;
2805 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
2808 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
2810 stream->session = rtpsession;
2812 if (stream->as_bandwidth != -1) {
2813 GST_INFO_OBJECT (src, "setting AS: %f",
2814 (gdouble) (stream->as_bandwidth * 1000));
2815 g_object_set (rtpsession, "bandwidth",
2816 (gdouble) (stream->as_bandwidth * 1000), NULL);
2818 if (stream->rr_bandwidth != -1) {
2819 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
2820 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
2823 if (stream->rs_bandwidth != -1) {
2824 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
2825 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
2829 g_object_set (rtpsession, "probation", src->probation, NULL);
2831 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2833 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
2835 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
2837 g_signal_connect (rtpsession, "on-ssrc-active",
2838 (GCallback) on_ssrc_active, stream);
2849 GST_DEBUG_OBJECT (src, "cannot get a session manager");
2854 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
2857 start_manager_failure:
2859 GST_DEBUG_OBJECT (src, "could not start session manager");
2864 /* free the UDP sources allocated when negotiating a transport.
2865 * This function is called when the server negotiated to a transport where the
2866 * UDP sources are not needed anymore, such as TCP or multicast. */
2868 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
2872 for (i = 0; i < 2; i++) {
2873 if (stream->udpsrc[i]) {
2874 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
2875 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
2876 gst_object_unref (stream->udpsrc[i]);
2877 stream->udpsrc[i] = NULL;
2882 /* for TCP, create pads to send and receive data to and from the manager and to
2883 * intercept various events and queries
2886 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
2887 GstRTSPTransport * transport, GstPad ** outpad)
2890 GstPadTemplate *template;
2891 GstPad *pad0, *pad1;
2893 /* configure for interleaved delivery, nothing needs to be done
2894 * here, the loop function will call the chain functions of the
2895 * session manager. */
2896 stream->channel[0] = transport->interleaved.min;
2897 stream->channel[1] = transport->interleaved.max;
2898 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
2899 stream->channel[0], stream->channel[1]);
2901 /* we can remove the allocated UDP ports now */
2902 gst_rtspsrc_stream_free_udp (stream);
2904 /* no session manager, send data to srcpad directly */
2905 if (!stream->channelpad[0]) {
2906 GST_DEBUG_OBJECT (src, "no manager, creating pad");
2908 /* create a new pad we will use to stream to */
2909 name = g_strdup_printf ("stream_%u", stream->id);
2910 template = gst_static_pad_template_get (&rtptemplate);
2911 stream->channelpad[0] = gst_pad_new_from_template (template, name);
2912 gst_object_unref (template);
2915 /* set caps and activate */
2916 gst_pad_use_fixed_caps (stream->channelpad[0]);
2917 gst_pad_set_active (stream->channelpad[0], TRUE);
2919 *outpad = gst_object_ref (stream->channelpad[0]);
2921 GST_DEBUG_OBJECT (src, "using manager source pad");
2923 template = gst_static_pad_template_get (&anysrctemplate);
2925 /* allocate pads for sending the channel data into the manager */
2926 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
2927 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
2928 gst_object_unref (stream->channelpad[0]);
2929 stream->channelpad[0] = pad0;
2930 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
2931 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
2932 gst_pad_set_element_private (pad0, src);
2933 gst_pad_set_active (pad0, TRUE);
2935 if (stream->channelpad[1]) {
2936 /* if we have a sinkpad for the other channel, create a pad and link to the
2938 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
2939 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
2940 gst_pad_link_full (pad1, stream->channelpad[1],
2941 GST_PAD_LINK_CHECK_NOTHING);
2942 gst_object_unref (stream->channelpad[1]);
2943 stream->channelpad[1] = pad1;
2944 gst_pad_set_active (pad1, TRUE);
2946 gst_object_unref (template);
2948 /* setup RTCP transport back to the server if we have to. */
2949 if (src->manager && src->do_rtcp) {
2952 template = gst_static_pad_template_get (&anysinktemplate);
2954 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
2955 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
2956 gst_pad_set_element_private (stream->rtcppad, stream);
2957 gst_pad_set_active (stream->rtcppad, TRUE);
2959 /* get session RTCP pad */
2960 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
2961 pad = gst_element_get_request_pad (src->manager, name);
2966 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
2967 gst_object_unref (pad);
2970 gst_object_unref (template);
2976 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
2977 GstRTSPTransport * transport, const gchar ** destination, gint * min,
2978 gint * max, guint * ttl)
2980 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
2982 if (!(*destination = transport->destination))
2983 *destination = stream->destination;
2986 /* transport first */
2987 *min = transport->port.min;
2988 *max = transport->port.max;
2989 if (*min == -1 && *max == -1) {
2990 /* then try from SDP */
2991 if (stream->port != 0) {
2992 *min = stream->port;
2993 *max = stream->port + 1;
2999 if (!(*ttl = transport->ttl))
3004 /* first take the source, then the endpoint to figure out where to send
3006 if (!(*destination = transport->source)) {
3007 if (src->conninfo.connection)
3008 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3009 else if (stream->conninfo.connection)
3011 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3015 /* for unicast we only expect the ports here */
3016 *min = transport->server_port.min;
3017 *max = transport->server_port.max;
3022 /* For multicast create UDP sources and join the multicast group. */
3024 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3025 GstRTSPTransport * transport, GstPad ** outpad)
3028 const gchar *destination;
3031 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3033 /* we can remove the allocated UDP ports now */
3034 gst_rtspsrc_stream_free_udp (stream);
3036 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3039 /* we need a destination now */
3040 if (destination == NULL)
3041 goto no_destination;
3043 /* we really need ports now or we won't be able to receive anything at all */
3044 if (min == -1 && max == -1)
3047 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3048 destination, min, max);
3050 /* creating UDP source for RTP */
3052 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3054 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3056 if (stream->udpsrc[0] == NULL)
3059 /* take ownership */
3060 gst_object_ref_sink (stream->udpsrc[0]);
3062 if (src->udp_buffer_size != 0)
3063 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3064 src->udp_buffer_size, NULL);
3066 if (src->multi_iface != NULL)
3067 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3068 src->multi_iface, NULL);
3071 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3072 gst_element_set_state (stream->udpsrc[0], GST_STATE_PAUSED);
3075 /* creating another UDP source for RTCP */
3079 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3081 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3083 if (stream->udpsrc[1] == NULL)
3086 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3087 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3088 gst_caps_unref (caps);
3090 /* take ownership */
3091 gst_object_ref_sink (stream->udpsrc[1]);
3093 if (src->multi_iface != NULL)
3094 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3095 src->multi_iface, NULL);
3097 gst_element_set_state (stream->udpsrc[1], GST_STATE_PAUSED);
3104 GST_DEBUG_OBJECT (src, "no UDP source element found");
3109 GST_DEBUG_OBJECT (src, "no destination found");
3114 GST_DEBUG_OBJECT (src, "no ports found");
3119 /* configure the remainder of the UDP ports */
3121 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3122 GstRTSPTransport * transport, GstPad ** outpad)
3124 /* we manage the UDP elements now. For unicast, the UDP sources where
3125 * allocated in the stream when we suggested a transport. */
3126 if (stream->udpsrc[0]) {
3127 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3128 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3130 GST_DEBUG_OBJECT (src, "setting up UDP source");
3132 /* configure a timeout on the UDP port. When the timeout message is
3133 * posted, we assume UDP transport is not possible. We reconnect using TCP
3135 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3136 src->udp_timeout * 1000, NULL);
3138 /* get output pad of the UDP source. */
3139 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3141 /* save it so we can unblock */
3142 stream->blockedpad = *outpad;
3144 /* configure pad block on the pad. As soon as there is dataflow on the
3145 * UDP source, we know that UDP is not blocked by a firewall and we can
3146 * configure all the streams to let the application autoplug decoders. */
3148 gst_pad_add_probe (stream->blockedpad,
3149 GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM, pad_blocked, src, NULL);
3151 if (stream->channelpad[0]) {
3152 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3153 /* configure for UDP delivery, we need to connect the UDP pads to
3154 * the session plugin. */
3155 gst_pad_link_full (*outpad, stream->channelpad[0],
3156 GST_PAD_LINK_CHECK_NOTHING);
3157 gst_object_unref (*outpad);
3159 /* we connected to pad-added signal to get pads from the manager */
3161 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3166 if (stream->udpsrc[1]) {
3169 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3170 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3172 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3173 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3174 gst_caps_unref (caps);
3176 if (stream->channelpad[1]) {
3179 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3181 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3182 gst_pad_link_full (pad, stream->channelpad[1],
3183 GST_PAD_LINK_CHECK_NOTHING);
3184 gst_object_unref (pad);
3186 /* leave unlinked */
3192 /* configure the UDP sink back to the server for status reports */
3194 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3195 GstRTSPStream * stream, GstRTSPTransport * transport)
3198 gint rtp_port, rtcp_port;
3199 gboolean do_rtp, do_rtcp;
3200 const gchar *destination;
3205 /* get transport info */
3206 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
3207 &rtp_port, &rtcp_port, &ttl);
3209 /* see what we need to do */
3210 do_rtp = (rtp_port != -1);
3211 /* it's possible that the server does not want us to send RTCP in which case
3213 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
3215 /* we need a destination when we have RTP or RTCP ports */
3216 if (destination == NULL && (do_rtp || do_rtcp))
3217 goto no_destination;
3219 /* try to construct the fakesrc to the RTP port of the server to open up any
3222 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
3225 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
3226 stream->udpsink[0] =
3227 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3229 if (stream->udpsink[0] == NULL)
3230 goto no_sink_element;
3232 /* don't join multicast group, we will have the source socket do that */
3233 /* no sync or async state changes needed */
3234 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
3235 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3237 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3239 if (stream->udpsrc[0]) {
3240 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
3241 * so that NAT firewalls will open a hole for us */
3242 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
3243 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
3244 /* configure socket and make sure udpsink does not close it when shutting
3245 * down, it belongs to udpsrc after all. */
3246 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
3247 "close-socket", FALSE, NULL);
3248 g_object_unref (socket);
3251 /* the source for the dummy packets to open up NAT */
3252 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
3253 if (stream->fakesrc == NULL)
3254 goto no_fakesrc_element;
3256 /* random data in 5 buffers, a size of 200 bytes should be fine */
3257 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
3258 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
3260 /* we don't want to consider this a sink */
3261 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
3263 /* keep everything locked */
3264 gst_element_set_locked_state (stream->udpsink[0], TRUE);
3265 gst_element_set_locked_state (stream->fakesrc, TRUE);
3267 gst_object_ref (stream->udpsink[0]);
3268 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
3269 gst_object_ref (stream->fakesrc);
3270 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
3272 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
3273 "sink", GST_PAD_LINK_CHECK_NOTHING);
3276 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
3279 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
3280 stream->udpsink[1] =
3281 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3283 if (stream->udpsink[1] == NULL)
3284 goto no_sink_element;
3286 /* don't join multicast group, we will have the source socket do that */
3287 /* no sync or async state changes needed */
3288 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
3289 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3291 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3293 if (stream->udpsrc[1]) {
3294 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
3295 * because some servers check the port number of where it sends RTCP to identify
3296 * the RTCP packets it receives */
3297 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
3298 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
3299 /* configure socket and make sure udpsink does not close it when shutting
3300 * down, it belongs to udpsrc after all. */
3301 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
3302 "close-socket", FALSE, NULL);
3303 g_object_unref (socket);
3306 /* we don't want to consider this a sink */
3307 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
3309 /* we keep this playing always */
3310 gst_element_set_locked_state (stream->udpsink[1], TRUE);
3311 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
3313 gst_object_ref (stream->udpsink[1]);
3314 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
3316 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
3318 /* get session RTCP pad */
3319 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3320 pad = gst_element_get_request_pad (src->manager, name);
3325 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3326 gst_object_unref (pad);
3335 GST_DEBUG_OBJECT (src, "no destination address specified");
3340 GST_DEBUG_OBJECT (src, "no UDP sink element found");
3345 GST_DEBUG_OBJECT (src, "no fakesrc element found");
3350 /* sets up all elements needed for streaming over the specified transport.
3351 * Does not yet expose the element pads, this will be done when there is actuall
3352 * dataflow detected, which might never happen when UDP is blocked in a
3353 * firewall, for example.
3356 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
3357 GstRTSPTransport * transport)
3360 GstPad *outpad = NULL;
3361 GstPadTemplate *template;
3366 src = stream->parent;
3368 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
3370 s = gst_caps_get_structure (stream->caps, 0);
3372 /* get the proper mime type for this stream now */
3373 if (gst_rtsp_transport_get_mime (transport->trans, &mime) < 0)
3374 goto unknown_transport;
3376 goto unknown_transport;
3378 /* configure the final mime type */
3379 GST_DEBUG_OBJECT (src, "setting mime to %s", mime);
3380 gst_structure_set_name (s, mime);
3382 /* try to get and configure a manager, channelpad[0-1] will be configured with
3383 * the pads for the manager, or NULL when no manager is needed. */
3384 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
3387 switch (transport->lower_transport) {
3388 case GST_RTSP_LOWER_TRANS_TCP:
3389 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
3390 goto transport_failed;
3392 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3393 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
3394 goto transport_failed;
3395 /* fallthrough, the rest is the same for UDP and MCAST */
3396 case GST_RTSP_LOWER_TRANS_UDP:
3397 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
3398 goto transport_failed;
3399 /* configure udpsinks back to the server for RTCP messages and for the
3400 * dummy RTP messages to open NAT. */
3401 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
3402 goto transport_failed;
3405 goto unknown_transport;
3409 GST_DEBUG_OBJECT (src, "creating ghostpad");
3411 gst_pad_use_fixed_caps (outpad);
3413 /* create ghostpad, don't add just yet, this will be done when we activate
3415 name = g_strdup_printf ("stream_%u", stream->id);
3416 template = gst_static_pad_template_get (&rtptemplate);
3417 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
3418 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3419 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3420 gst_object_unref (template);
3423 gst_object_unref (outpad);
3425 /* mark pad as ok */
3426 stream->last_ret = GST_FLOW_OK;
3433 GST_DEBUG_OBJECT (src, "failed to configure transport");
3438 GST_DEBUG_OBJECT (src, "unknown transport");
3443 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3448 /* send a couple of dummy random packets on the receiver RTP port to the server,
3449 * this should make a firewall think we initiated the data transfer and
3450 * hopefully allow packets to go from the sender port to our RTP receiver port */
3452 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
3456 if (src->nat_method != GST_RTSP_NAT_DUMMY)
3459 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3460 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3462 if (stream->fakesrc && stream->udpsink[0]) {
3463 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
3464 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
3465 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
3466 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
3467 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
3473 /* Adds the source pads of all configured streams to the element.
3474 * This code is performed when we detected dataflow.
3476 * We detect dataflow from either the _loop function or with pad probes on the
3480 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
3484 GST_DEBUG_OBJECT (src, "activating streams");
3486 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3487 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3489 if (stream->udpsrc[0]) {
3490 /* remove timeout, we are streaming now and timeouts will be handled by
3491 * the session manager and jitter buffer */
3492 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
3494 if (stream->srcpad) {
3495 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
3496 gst_pad_set_active (stream->srcpad, TRUE);
3498 /* if we don't have a session manager, set the caps now. If we have a
3499 * session, we will get a notification of the pad and the caps. */
3500 if (!src->manager) {
3501 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
3502 gst_pad_set_caps (stream->srcpad, stream->caps);
3505 if (!stream->added) {
3506 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
3507 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3508 stream->added = TRUE;
3513 /* unblock all pads */
3514 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3515 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3517 if (stream->blockid) {
3518 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
3519 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
3520 stream->blockid = 0;
3528 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
3529 gboolean reset_manager)
3532 guint64 start, stop;
3533 gdouble play_speed, play_scale;
3535 GST_DEBUG_OBJECT (src, "configuring stream caps");
3537 start = segment->position;
3538 stop = segment->duration;
3539 play_speed = segment->rate;
3540 play_scale = segment->applied_rate;
3542 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3543 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3546 if ((caps = stream->caps)) {
3547 caps = gst_caps_make_writable (caps);
3549 if (stream->timebase != -1)
3550 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
3551 (guint) stream->timebase, NULL);
3552 if (stream->seqbase != -1)
3553 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
3554 (guint) stream->seqbase, NULL);
3555 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
3557 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
3558 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
3559 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
3561 stream->caps = caps;
3563 GST_DEBUG_OBJECT (src, "stream %p, caps %" GST_PTR_FORMAT, stream, caps);
3565 if (reset_manager && src->manager) {
3566 GST_DEBUG_OBJECT (src, "clear session");
3567 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
3571 static GstFlowReturn
3572 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
3577 /* store the value */
3578 stream->last_ret = ret;
3580 /* if it's success we can return the value right away */
3581 if (ret == GST_FLOW_OK)
3584 /* any other error that is not-linked can be returned right
3586 if (ret != GST_FLOW_NOT_LINKED)
3589 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3590 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3591 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3593 ret = ostream->last_ret;
3594 /* some other return value (must be SUCCESS but we can return
3595 * other values as well) */
3596 if (ret != GST_FLOW_NOT_LINKED)
3599 /* if we get here, all other pads were unlinked and we return
3600 * NOT_LINKED then */
3606 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
3609 gboolean res = TRUE;
3611 /* only streams that have a connection to the outside world */
3612 if (stream->container || stream->disabled)
3615 if (stream->udpsrc[0]) {
3616 gst_event_ref (event);
3617 res = gst_element_send_event (stream->udpsrc[0], event);
3618 } else if (stream->channelpad[0]) {
3619 gst_event_ref (event);
3620 if (GST_PAD_IS_SRC (stream->channelpad[0]))
3621 res = gst_pad_push_event (stream->channelpad[0], event);
3623 res = gst_pad_send_event (stream->channelpad[0], event);
3626 if (stream->udpsrc[1]) {
3627 gst_event_ref (event);
3628 res &= gst_element_send_event (stream->udpsrc[1], event);
3629 } else if (stream->channelpad[1]) {
3630 gst_event_ref (event);
3631 if (GST_PAD_IS_SRC (stream->channelpad[1]))
3632 res &= gst_pad_push_event (stream->channelpad[1], event);
3634 res &= gst_pad_send_event (stream->channelpad[1], event);
3638 gst_event_unref (event);
3644 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
3647 gboolean res = TRUE;
3649 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3650 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3652 gst_event_ref (event);
3653 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
3655 gst_event_unref (event);
3660 static GstRTSPResult
3661 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3666 if (info->connection == NULL) {
3667 if (info->url == NULL) {
3668 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
3669 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
3673 /* create connection */
3674 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
3675 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
3676 goto could_not_create;
3679 g_free (info->url_str);
3680 info->url_str = gst_rtsp_url_get_request_uri (info->url);
3682 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
3684 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
3685 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
3686 src->tls_validation_flags))
3687 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
3690 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
3691 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
3693 if (src->proxy_host) {
3694 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
3696 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
3701 if (!info->connected) {
3704 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
3705 ("Connecting to %s", info->location));
3706 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
3708 gst_rtsp_connection_connect (info->connection,
3709 src->ptcp_timeout)) < 0)
3710 goto could_not_connect;
3712 info->connected = TRUE;
3719 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
3724 gchar *str = gst_rtsp_strresult (res);
3725 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
3731 gchar *str = gst_rtsp_strresult (res);
3732 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
3738 static GstRTSPResult
3739 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
3742 GST_RTSP_STATE_LOCK (src);
3743 if (info->connected) {
3744 GST_DEBUG_OBJECT (src, "closing connection...");
3745 gst_rtsp_connection_close (info->connection);
3746 info->connected = FALSE;
3748 if (free && info->connection) {
3749 /* free connection */
3750 GST_DEBUG_OBJECT (src, "freeing connection...");
3751 gst_rtsp_connection_free (info->connection);
3752 info->connection = NULL;
3754 GST_RTSP_STATE_UNLOCK (src);
3758 static GstRTSPResult
3759 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3764 GST_DEBUG_OBJECT (src, "reconnecting connection...");
3765 gst_rtsp_conninfo_close (src, info, FALSE);
3766 res = gst_rtsp_conninfo_connect (src, info, async);
3772 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
3776 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
3777 GST_RTSP_STATE_LOCK (src);
3778 if (src->conninfo.connection && src->conninfo.flushing != flush) {
3779 GST_DEBUG_OBJECT (src, "connection flush");
3780 gst_rtsp_connection_flush (src->conninfo.connection, flush);
3781 src->conninfo.flushing = flush;
3783 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3784 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3785 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
3786 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
3787 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
3788 stream->conninfo.flushing = flush;
3791 GST_RTSP_STATE_UNLOCK (src);
3794 /* FIXME, handle server request, reply with OK, for now */
3795 static GstRTSPResult
3796 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
3797 GstRTSPMessage * request)
3799 GstRTSPMessage response = { 0 };
3802 GST_DEBUG_OBJECT (src, "got server request message");
3805 gst_rtsp_message_dump (request);
3807 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
3809 if (res == GST_RTSP_ENOTIMPL) {
3810 /* default implementation, send OK */
3811 GST_DEBUG_OBJECT (src, "prepare OK reply");
3813 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
3818 /* let app parse and reply */
3819 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
3820 0, request, &response);
3823 gst_rtsp_message_dump (&response);
3825 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
3829 gst_rtsp_message_unset (&response);
3830 } else if (res == GST_RTSP_EEOF)
3838 gst_rtsp_message_unset (&response);
3843 /* send server keep-alive */
3844 static GstRTSPResult
3845 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
3847 GstRTSPMessage request = { 0 };
3849 GstRTSPMethod method;
3852 if (src->do_rtsp_keep_alive == FALSE) {
3853 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
3854 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
3858 GST_DEBUG_OBJECT (src, "creating server keep-alive");
3860 /* find a method to use for keep-alive */
3861 if (src->methods & GST_RTSP_GET_PARAMETER)
3862 method = GST_RTSP_GET_PARAMETER;
3864 method = GST_RTSP_OPTIONS;
3867 control = src->control;
3869 control = src->conninfo.url_str;
3871 if (control == NULL)
3874 res = gst_rtsp_message_init_request (&request, method, control);
3879 gst_rtsp_message_dump (&request);
3882 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
3887 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
3888 gst_rtsp_message_unset (&request);
3895 GST_WARNING_OBJECT (src, "no control url to send keepalive");
3900 gchar *str = gst_rtsp_strresult (res);
3902 gst_rtsp_message_unset (&request);
3903 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
3904 ("Could not send keep-alive. (%s)", str));
3910 static GstFlowReturn
3911 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
3913 GstFlowReturn ret = GST_FLOW_OK;
3915 GstRTSPStream *stream;
3916 GstPad *outpad = NULL;
3923 channel = message->type_data.data.channel;
3925 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
3927 goto unknown_stream;
3929 if (channel == stream->channel[0]) {
3930 outpad = stream->channelpad[0];
3932 } else if (channel == stream->channel[1]) {
3933 outpad = stream->channelpad[1];
3939 /* take a look at the body to figure out what we have */
3940 gst_rtsp_message_get_body (message, &data, &size);
3942 goto invalid_length;
3944 /* channels are not correct on some servers, do extra check */
3945 if (data[1] >= 200 && data[1] <= 204) {
3946 /* hmm RTCP message switch to the RTCP pad of the same stream. */
3947 outpad = stream->channelpad[1];
3951 /* we have no clue what this is, just ignore then. */
3953 goto unknown_stream;
3955 /* take the message body for further processing */
3956 gst_rtsp_message_steal_body (message, &data, &size);
3958 /* strip the trailing \0 */
3961 buf = gst_buffer_new ();
3962 gst_buffer_append_memory (buf,
3963 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
3965 /* don't need message anymore */
3966 gst_rtsp_message_unset (message);
3968 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
3971 if (src->need_activate) {
3977 guint group_id = gst_util_group_id_next ();
3979 /* generate an SHA256 sum of the URI */
3980 cs = g_checksum_new (G_CHECKSUM_SHA256);
3981 uri = src->conninfo.location;
3982 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
3984 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3985 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3988 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
3989 event = gst_event_new_stream_start (stream_id);
3990 gst_event_set_group_id (event, group_id);
3993 gst_rtspsrc_stream_push_event (src, ostream, event);
3995 g_checksum_free (cs);
3997 gst_rtspsrc_activate_streams (src);
3998 src->need_activate = FALSE;
4000 if ((event = src->start_segment) != NULL) {
4001 src->start_segment = NULL;
4002 gst_rtspsrc_push_event (src, event);
4005 if (src->base_time == -1) {
4006 /* Take current running_time. This timestamp will be put on
4007 * the first buffer of each stream because we are a live source and so we
4008 * timestamp with the running_time. When we are dealing with TCP, we also
4009 * only timestamp the first buffer (using the DISCONT flag) because a server
4010 * typically bursts data, for which we don't want to compensate by speeding
4011 * up the media. The other timestamps will be interpollated from this one
4012 * using the RTP timestamps. */
4013 GST_OBJECT_LOCK (src);
4014 if (GST_ELEMENT_CLOCK (src)) {
4016 GstClockTime base_time;
4018 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
4019 base_time = GST_ELEMENT_CAST (src)->base_time;
4021 src->base_time = now - base_time;
4023 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
4024 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
4026 GST_OBJECT_UNLOCK (src);
4029 if (stream->discont && !is_rtcp) {
4030 /* mark first RTP buffer as discont */
4031 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4032 stream->discont = FALSE;
4033 /* first buffer gets the timestamp, other buffers are not timestamped and
4034 * their presentation time will be interpollated from the rtp timestamps. */
4035 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
4036 GST_TIME_ARGS (src->base_time));
4038 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
4041 /* chain to the peer pad */
4042 if (GST_PAD_IS_SINK (outpad))
4043 ret = gst_pad_chain (outpad, buf);
4045 ret = gst_pad_push (outpad, buf);
4048 /* combine all stream flows for the data transport */
4049 ret = gst_rtspsrc_combine_flows (src, stream, ret);
4056 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
4057 gst_rtsp_message_unset (message);
4062 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4063 ("Short message received, ignoring."));
4064 gst_rtsp_message_unset (message);
4069 static GstFlowReturn
4070 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
4072 GstRTSPMessage message = { 0 };
4074 GstFlowReturn ret = GST_FLOW_OK;
4075 GTimeVal tv_timeout;
4078 /* get the next timeout interval */
4079 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4081 /* see if the timeout period expired */
4082 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
4083 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
4084 /* send keep-alive, only act on interrupt, a warning will be posted for
4086 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4088 /* get new timeout */
4089 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4092 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
4093 tv_timeout.tv_sec, tv_timeout.tv_usec);
4095 /* protect the connection with the connection lock so that we can see when
4096 * we are finished doing server communication */
4098 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4099 &message, src->ptcp_timeout);
4103 GST_DEBUG_OBJECT (src, "we received a server message");
4105 case GST_RTSP_EINTR:
4106 /* we got interrupted this means we need to stop */
4108 case GST_RTSP_ETIMEOUT:
4109 /* no reply, send keep alive */
4110 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4111 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4115 /* go EOS when the server closed the connection */
4121 switch (message.type) {
4122 case GST_RTSP_MESSAGE_REQUEST:
4123 /* server sends us a request message, handle it */
4125 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4127 if (res == GST_RTSP_EEOF)
4130 goto handle_request_failed;
4132 case GST_RTSP_MESSAGE_RESPONSE:
4133 /* we ignore response messages */
4134 GST_DEBUG_OBJECT (src, "ignoring response message");
4136 gst_rtsp_message_dump (&message);
4138 case GST_RTSP_MESSAGE_DATA:
4139 GST_DEBUG_OBJECT (src, "got data message");
4140 ret = gst_rtspsrc_handle_data (src, &message);
4141 if (ret != GST_FLOW_OK)
4142 goto handle_data_failed;
4145 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4150 g_assert_not_reached ();
4155 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4156 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4157 ("The server closed the connection."));
4158 src->conninfo.connected = FALSE;
4159 gst_rtsp_message_unset (&message);
4160 return GST_FLOW_EOS;
4164 gst_rtsp_message_unset (&message);
4165 GST_DEBUG_OBJECT (src, "got interrupted");
4166 return GST_FLOW_FLUSHING;
4170 gchar *str = gst_rtsp_strresult (res);
4172 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4173 ("Could not receive message. (%s)", str));
4176 gst_rtsp_message_unset (&message);
4177 return GST_FLOW_ERROR;
4179 handle_request_failed:
4181 gchar *str = gst_rtsp_strresult (res);
4183 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4184 ("Could not handle server message. (%s)", str));
4186 gst_rtsp_message_unset (&message);
4187 return GST_FLOW_ERROR;
4191 GST_DEBUG_OBJECT (src, "could no handle data message");
4196 static GstFlowReturn
4197 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
4200 GstRTSPMessage message = { 0 };
4204 GTimeVal tv_timeout;
4206 /* get the next timeout interval */
4207 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4209 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
4210 (gint) tv_timeout.tv_sec);
4212 gst_rtsp_message_unset (&message);
4214 /* we should continue reading the TCP socket because the server might
4215 * send us requests. When the session timeout expires, we need to send a
4216 * keep-alive request to keep the session open. */
4217 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4218 &message, &tv_timeout);
4222 GST_DEBUG_OBJECT (src, "we received a server message");
4224 case GST_RTSP_EINTR:
4225 /* we got interrupted, see what we have to do */
4227 case GST_RTSP_ETIMEOUT:
4228 /* send keep-alive, ignore the result, a warning will be posted. */
4229 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4230 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4234 /* server closed the connection. not very fatal for UDP, reconnect and
4235 * see what happens. */
4236 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4237 ("The server closed the connection."));
4238 if (src->udp_reconnect) {
4240 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
4247 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
4249 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4250 ("Unhandled return value %d.", res));
4254 switch (message.type) {
4255 case GST_RTSP_MESSAGE_REQUEST:
4256 /* server sends us a request message, handle it */
4258 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4260 if (res == GST_RTSP_EEOF)
4263 goto handle_request_failed;
4265 case GST_RTSP_MESSAGE_RESPONSE:
4266 /* we ignore response and data messages */
4267 GST_DEBUG_OBJECT (src, "ignoring response message");
4269 gst_rtsp_message_dump (&message);
4270 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4271 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
4272 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
4273 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
4274 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4281 case GST_RTSP_MESSAGE_DATA:
4282 /* we ignore response and data messages */
4283 GST_DEBUG_OBJECT (src, "ignoring data message");
4286 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4291 g_assert_not_reached ();
4293 /* we get here when the connection got interrupted */
4296 gst_rtsp_message_unset (&message);
4297 GST_DEBUG_OBJECT (src, "got interrupted");
4298 return GST_FLOW_FLUSHING;
4302 gchar *str = gst_rtsp_strresult (res);
4305 src->conninfo.connected = FALSE;
4306 if (res != GST_RTSP_EINTR) {
4307 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
4308 ("Could not connect to server. (%s)", str));
4310 ret = GST_FLOW_ERROR;
4312 ret = GST_FLOW_FLUSHING;
4318 gchar *str = gst_rtsp_strresult (res);
4320 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4321 ("Could not receive message. (%s)", str));
4323 return GST_FLOW_ERROR;
4325 handle_request_failed:
4327 gchar *str = gst_rtsp_strresult (res);
4330 gst_rtsp_message_unset (&message);
4331 if (res != GST_RTSP_EINTR) {
4332 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4333 ("Could not handle server message. (%s)", str));
4335 ret = GST_FLOW_ERROR;
4337 ret = GST_FLOW_FLUSHING;
4343 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4344 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4345 ("The server closed the connection."));
4346 src->conninfo.connected = FALSE;
4347 gst_rtsp_message_unset (&message);
4348 return GST_FLOW_EOS;
4352 static GstRTSPResult
4353 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
4355 GstRTSPResult res = GST_RTSP_OK;
4358 GST_DEBUG_OBJECT (src, "doing reconnect");
4360 GST_OBJECT_LOCK (src);
4361 /* only restart when the pads were not yet activated, else we were
4362 * streaming over UDP */
4363 restart = src->need_activate;
4364 GST_OBJECT_UNLOCK (src);
4366 /* no need to restart, we're done */
4370 /* we can try only TCP now */
4371 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
4373 /* close and cleanup our state */
4374 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
4377 /* see if we have TCP left to try. Also don't try TCP when we were configured
4379 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
4382 /* We post a warning message now to inform the user
4383 * that nothing happened. It's most likely a firewall thing. */
4384 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4385 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4386 "firewall is blocking it. Retrying using a TCP connection.",
4387 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4389 /* open new connection using tcp */
4390 if (gst_rtspsrc_open (src, async) < 0)
4393 /* start playback */
4394 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
4403 src->cur_protocols = 0;
4404 /* no transport possible, post an error and stop */
4405 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4406 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4407 "firewall is blocking it. No other protocols to try.",
4408 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4409 return GST_RTSP_ERROR;
4413 GST_DEBUG_OBJECT (src, "open failed");
4418 GST_DEBUG_OBJECT (src, "play failed");
4424 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
4428 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
4431 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
4434 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
4437 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
4445 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
4449 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
4452 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
4455 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
4458 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
4466 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
4470 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
4473 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
4476 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
4479 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
4487 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
4491 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
4494 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
4497 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
4500 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
4508 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
4510 if (ret == GST_RTSP_OK)
4511 gst_rtspsrc_loop_complete_cmd (src, cmd);
4512 else if (ret == GST_RTSP_EINTR)
4513 gst_rtspsrc_loop_cancel_cmd (src, cmd);
4515 gst_rtspsrc_loop_error_cmd (src, cmd);
4519 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
4522 gboolean flushed = FALSE;
4524 /* start new request */
4525 gst_rtspsrc_loop_start_cmd (src, cmd);
4527 GST_DEBUG_OBJECT (src, "sending cmd %d", cmd);
4529 GST_OBJECT_LOCK (src);
4530 old = src->pending_cmd;
4531 if (old == CMD_RECONNECT) {
4532 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
4533 cmd = CMD_RECONNECT;
4535 if (old != CMD_WAIT) {
4536 src->pending_cmd = CMD_WAIT;
4537 GST_OBJECT_UNLOCK (src);
4538 /* cancel previous request */
4539 GST_DEBUG_OBJECT (src, "cancel previous request %d", old);
4540 gst_rtspsrc_loop_cancel_cmd (src, old);
4541 GST_OBJECT_LOCK (src);
4543 src->pending_cmd = cmd;
4544 /* interrupt if allowed */
4545 if (src->busy_cmd & mask) {
4546 GST_DEBUG_OBJECT (src, "connection flush busy %d", src->busy_cmd);
4547 gst_rtspsrc_connection_flush (src, TRUE);
4550 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %d", src->busy_cmd);
4553 gst_task_start (src->task);
4554 GST_OBJECT_UNLOCK (src);
4560 gst_rtspsrc_loop (GstRTSPSrc * src)
4564 if (!src->conninfo.connection || !src->conninfo.connected)
4567 if (src->interleaved)
4568 ret = gst_rtspsrc_loop_interleaved (src);
4570 ret = gst_rtspsrc_loop_udp (src);
4572 if (ret != GST_FLOW_OK)
4580 GST_WARNING_OBJECT (src, "we are not connected");
4581 ret = GST_FLOW_FLUSHING;
4586 const gchar *reason = gst_flow_get_name (ret);
4588 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
4589 src->running = FALSE;
4590 if (ret == GST_FLOW_EOS) {
4591 /* perform EOS logic */
4592 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4593 gst_element_post_message (GST_ELEMENT_CAST (src),
4594 gst_message_new_segment_done (GST_OBJECT_CAST (src),
4595 src->segment.format, src->segment.position));
4596 gst_rtspsrc_push_event (src,
4597 gst_event_new_segment_done (src->segment.format,
4598 src->segment.position));
4600 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4602 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
4603 /* for fatal errors we post an error message, post the error before the
4604 * EOS so the app knows about the error first. */
4605 GST_ELEMENT_ERROR (src, STREAM, FAILED,
4606 ("Internal data flow error."),
4607 ("streaming task paused, reason %s (%d)", reason, ret));
4608 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4610 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
4615 #ifndef GST_DISABLE_GST_DEBUG
4616 static const gchar *
4617 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
4621 while (method != 0) {
4638 static const gchar *
4639 gst_rtspsrc_skip_lws (const gchar * s)
4641 while (g_ascii_isspace (*s))
4646 static const gchar *
4647 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
4649 while (s > start && g_ascii_isspace (*(s - 1)))
4654 static const gchar *
4655 gst_rtspsrc_skip_commas (const gchar * s)
4657 /* The grammar allows for multiple commas */
4658 while (g_ascii_isspace (*s) || *s == ',')
4663 static const gchar *
4664 gst_rtspsrc_skip_item (const gchar * s)
4666 gboolean quoted = FALSE;
4667 const gchar *start = s;
4669 /* A list item ends at the last non-whitespace character
4670 * before a comma which is not inside a quoted-string. Or at
4671 * the end of the string.
4677 if (*s == '\\' && *(s + 1))
4686 return gst_rtspsrc_unskip_lws (s, start);
4690 gst_rtsp_decode_quoted_string (gchar * quoted_string)
4694 src = quoted_string + 1;
4695 dst = quoted_string;
4696 while (*src && *src != '"') {
4697 if (*src == '\\' && *(src + 1))
4704 /* Extract the authentication tokens that the server provided for each method
4705 * into an array of structures and give those to the connection object.
4708 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
4709 const gchar * header, gboolean * stale)
4711 GSList *list = NULL, *iter;
4713 gchar *item, *eq, *name_end, *value;
4715 g_return_if_fail (stale != NULL);
4717 gst_rtsp_connection_clear_auth_params (conn);
4720 /* Parse a header whose content is described by RFC2616 as
4721 * "#something", where "something" does not itself contain commas,
4722 * except as part of quoted-strings, into a list of allocated strings.
4724 header = gst_rtspsrc_skip_commas (header);
4726 end = gst_rtspsrc_skip_item (header);
4727 list = g_slist_prepend (list, g_strndup (header, end - header));
4728 header = gst_rtspsrc_skip_commas (end);
4733 list = g_slist_reverse (list);
4734 for (iter = list; iter; iter = iter->next) {
4737 eq = strchr (item, '=');
4739 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
4740 if (name_end == item) {
4741 /* That's no good... */
4748 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
4750 gst_rtsp_decode_quoted_string (value);
4754 if (item && (strcmp (item, "stale") == 0) &&
4755 value && (strcmp (value, "TRUE") == 0))
4757 gst_rtsp_connection_set_auth_param (conn, item, value);
4761 g_slist_free (list);
4764 /* Parse a WWW-Authenticate Response header and determine the
4765 * available authentication methods
4767 * This code should also cope with the fact that each WWW-Authenticate
4768 * header can contain multiple challenge methods + tokens
4770 * At the moment, for Basic auth, we just do a minimal check and don't
4771 * even parse out the realm */
4773 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
4774 GstRTSPConnection * conn, gboolean * stale)
4778 g_return_if_fail (hdr != NULL);
4779 g_return_if_fail (methods != NULL);
4780 g_return_if_fail (stale != NULL);
4782 /* Skip whitespace at the start of the string */
4783 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
4785 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
4786 *methods |= GST_RTSP_AUTH_BASIC;
4787 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
4788 *methods |= GST_RTSP_AUTH_DIGEST;
4789 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
4794 * gst_rtspsrc_setup_auth:
4795 * @src: the rtsp source
4797 * Configure a username and password and auth method on the
4798 * connection object based on a response we received from the
4801 * Currently, this requires that a username and password were supplied
4802 * in the uri. In the future, they may be requested on demand by sending
4803 * a message up the bus.
4805 * Returns: TRUE if authentication information could be set up correctly.
4808 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
4812 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
4813 GstRTSPAuthMethod method;
4814 GstRTSPResult auth_result;
4816 GstRTSPConnection *conn;
4818 gboolean stale = FALSE;
4820 conn = src->conninfo.connection;
4822 /* Identify the available auth methods and see if any are supported */
4823 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
4824 &hdr, 0) == GST_RTSP_OK) {
4825 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
4828 if (avail_methods == GST_RTSP_AUTH_NONE)
4829 goto no_auth_available;
4831 /* For digest auth, if the response indicates that the session
4832 * data are stale, we just update them in the connection object and
4833 * return TRUE to retry the request */
4835 src->tried_url_auth = FALSE;
4837 url = gst_rtsp_connection_get_url (conn);
4839 /* Do we have username and password available? */
4840 if (url != NULL && !src->tried_url_auth && url->user != NULL
4841 && url->passwd != NULL) {
4844 src->tried_url_auth = TRUE;
4845 GST_DEBUG_OBJECT (src,
4846 "Attempting authentication using credentials from the URL");
4848 user = src->user_id;
4849 pass = src->user_pw;
4850 GST_DEBUG_OBJECT (src,
4851 "Attempting authentication using credentials from the properties");
4854 /* FIXME: If the url didn't contain username and password or we tried them
4855 * already, request a username and passwd from the application via some kind
4856 * of credentials request message */
4858 /* If we don't have a username and passwd at this point, bail out. */
4859 if (user == NULL || pass == NULL)
4862 /* Try to configure for each available authentication method, strongest to
4864 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
4865 /* Check if this method is available on the server */
4866 if ((method & avail_methods) == 0)
4869 /* Pass the credentials to the connection to try on the next request */
4870 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
4871 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
4872 * ignore it and end up retrying later */
4873 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
4874 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
4875 gst_rtsp_auth_method_to_string (method));
4880 if (method == GST_RTSP_AUTH_NONE)
4881 goto no_auth_available;
4887 /* Output an error indicating that we couldn't connect because there were
4888 * no supported authentication protocols */
4889 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4890 ("No supported authentication protocol was found"));
4895 /* We don't fire an error message, we just return FALSE and let the
4896 * normal NOT_AUTHORIZED error be propagated */
4901 static GstRTSPResult
4902 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
4903 GstRTSPMessage * request, GstRTSPMessage * response,
4904 GstRTSPStatusCode * code)
4907 GstRTSPStatusCode thecode;
4908 gchar *content_base = NULL;
4912 if (!src->short_header)
4913 gst_rtsp_ext_list_before_send (src->extensions, request);
4915 GST_DEBUG_OBJECT (src, "sending message");
4918 gst_rtsp_message_dump (request);
4920 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
4924 gst_rtsp_connection_reset_timeout (conn);
4927 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
4932 gst_rtsp_message_dump (response);
4934 switch (response->type) {
4935 case GST_RTSP_MESSAGE_REQUEST:
4936 res = gst_rtspsrc_handle_request (src, conn, response);
4937 if (res == GST_RTSP_EEOF)
4940 goto handle_request_failed;
4942 case GST_RTSP_MESSAGE_RESPONSE:
4943 /* ok, a response is good */
4944 GST_DEBUG_OBJECT (src, "received response message");
4946 case GST_RTSP_MESSAGE_DATA:
4947 /* get next response */
4948 GST_DEBUG_OBJECT (src, "handle data response message");
4949 gst_rtspsrc_handle_data (src, response);
4952 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4957 thecode = response->type_data.response.code;
4959 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
4961 /* if the caller wanted the result code, we store it. */
4965 /* If the request didn't succeed, bail out before doing any more */
4966 if (thecode != GST_RTSP_STS_OK)
4969 /* store new content base if any */
4970 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
4973 g_free (src->content_base);
4974 src->content_base = g_strdup (content_base);
4976 gst_rtsp_ext_list_after_send (src->extensions, request, response);
4983 gchar *str = gst_rtsp_strresult (res);
4985 if (res != GST_RTSP_EINTR) {
4986 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4987 ("Could not send message. (%s)", str));
4989 GST_WARNING_OBJECT (src, "send interrupted");
4998 GST_WARNING_OBJECT (src, "server closed connection");
4999 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
5001 /* if reconnect succeeds, try again */
5003 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
5007 /* only try once after reconnect, then fallthrough and error out */
5010 gchar *str = gst_rtsp_strresult (res);
5012 if (res != GST_RTSP_EINTR) {
5013 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5014 ("Could not receive message. (%s)", str));
5016 GST_WARNING_OBJECT (src, "receive interrupted");
5024 handle_request_failed:
5026 /* ERROR was posted */
5027 gst_rtsp_message_unset (response);
5032 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5033 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5034 ("The server closed the connection."));
5035 gst_rtsp_message_unset (response);
5042 * @src: the rtsp source
5043 * @conn: the connection to send on
5044 * @request: must point to a valid request
5045 * @response: must point to an empty #GstRTSPMessage
5046 * @code: an optional code result
5048 * send @request and retrieve the response in @response. optionally @code can be
5049 * non-NULL in which case it will contain the status code of the response.
5051 * If This function returns #GST_RTSP_OK, @response will contain a valid response
5052 * message that should be cleaned with gst_rtsp_message_unset() after usage.
5054 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
5055 * @response message) if the response code was not 200 (OK).
5057 * If the attempt results in an authentication failure, then this will attempt
5058 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
5061 * Returns: #GST_RTSP_OK if the processing was successful.
5063 static GstRTSPResult
5064 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5065 GstRTSPMessage * request, GstRTSPMessage * response,
5066 GstRTSPStatusCode * code)
5068 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
5069 GstRTSPResult res = GST_RTSP_ERROR;
5072 GstRTSPMethod method = GST_RTSP_INVALID;
5078 /* make sure we don't loop forever */
5082 /* save method so we can disable it when the server complains */
5083 method = request->type_data.request.method;
5086 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
5090 case GST_RTSP_STS_UNAUTHORIZED:
5091 if (gst_rtspsrc_setup_auth (src, response)) {
5092 /* Try the request/response again after configuring the auth info
5100 } while (retry == TRUE);
5102 /* If the user requested the code, let them handle errors, otherwise
5103 * post an error below */
5106 else if (int_code != GST_RTSP_STS_OK)
5107 goto error_response;
5114 GST_DEBUG_OBJECT (src, "got error %d", res);
5119 res = GST_RTSP_ERROR;
5121 switch (response->type_data.response.code) {
5122 case GST_RTSP_STS_NOT_FOUND:
5123 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
5124 response->type_data.response.reason));
5126 case GST_RTSP_STS_MOVED_PERMANENTLY:
5127 case GST_RTSP_STS_MOVE_TEMPORARILY:
5129 gchar *new_location;
5130 GstRTSPLowerTrans transports;
5132 GST_DEBUG_OBJECT (src, "got redirection");
5133 /* if we don't have a Location Header, we must error */
5134 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
5135 &new_location, 0) < 0)
5138 /* When we receive a redirect result, we go back to the INIT state after
5139 * parsing the new URI. The caller should do the needed steps to issue
5140 * a new setup when it detects this state change. */
5141 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
5143 /* save current transports */
5144 if (src->conninfo.url)
5145 transports = src->conninfo.url->transports;
5147 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
5149 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
5151 /* set old transports */
5152 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
5153 src->conninfo.url->transports = transports;
5155 src->need_redirect = TRUE;
5156 src->state = GST_RTSP_STATE_INIT;
5160 case GST_RTSP_STS_NOT_ACCEPTABLE:
5161 case GST_RTSP_STS_NOT_IMPLEMENTED:
5162 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
5163 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
5164 gst_rtsp_method_as_text (method));
5165 src->methods &= ~method;
5169 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5170 ("Got error response: %d (%s).", response->type_data.response.code,
5171 response->type_data.response.reason));
5174 /* if we return ERROR we should unset the response ourselves */
5175 if (res == GST_RTSP_ERROR)
5176 gst_rtsp_message_unset (response);
5182 static GstRTSPResult
5183 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
5184 GstRTSPMessage * response, GstRTSPSrc * src)
5186 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
5191 /* parse the response and collect all the supported methods. We need this
5192 * information so that we don't try to send an unsupported request to the
5196 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
5198 GstRTSPHeaderField field;
5202 /* reset supported methods */
5205 /* Try Allow Header first */
5206 field = GST_RTSP_HDR_ALLOW;
5209 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5210 if (indx == 0 && !respoptions) {
5211 /* if no Allow header was found then try the Public header... */
5212 field = GST_RTSP_HDR_PUBLIC;
5213 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5218 src->methods |= gst_rtsp_options_from_text (respoptions);
5223 if (src->methods == 0) {
5224 /* neither Allow nor Public are required, assume the server supports
5225 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
5227 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
5228 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
5230 /* always assume PLAY, FIXME, extensions should be able to override
5232 src->methods |= GST_RTSP_PLAY;
5233 /* also assume it will support Range */
5234 src->seekable = TRUE;
5236 /* we need describe and setup */
5237 if (!(src->methods & GST_RTSP_DESCRIBE))
5239 if (!(src->methods & GST_RTSP_SETUP))
5247 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5248 ("Server does not support DESCRIBE."));
5253 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5254 ("Server does not support SETUP."));
5259 /* masks to be kept in sync with the hardcoded protocol order of preference
5261 static guint protocol_masks[] = {
5262 GST_RTSP_LOWER_TRANS_UDP,
5263 GST_RTSP_LOWER_TRANS_UDP_MCAST,
5264 GST_RTSP_LOWER_TRANS_TCP,
5268 static GstRTSPResult
5269 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
5270 GstRTSPLowerTrans protocols, gchar ** transports)
5274 gboolean add_udp_str;
5279 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
5284 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
5286 /* extension listed transports, use those */
5287 if (*transports != NULL)
5290 /* it's the default */
5291 add_udp_str = FALSE;
5293 /* the default RTSP transports */
5294 result = g_string_new ("");
5295 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
5296 GST_DEBUG_OBJECT (src, "adding UDP unicast");
5298 g_string_append (result, "RTP/AVP");
5300 g_string_append (result, "/UDP");
5301 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
5302 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
5303 GST_DEBUG_OBJECT (src, "adding UDP multicast");
5305 /* we don't have to allocate any UDP ports yet, if the selected transport
5306 * turns out to be multicast we can create them and join the multicast
5307 * group indicated in the transport reply */
5308 if (result->len > 0)
5309 g_string_append (result, ",");
5310 g_string_append (result, "RTP/AVP");
5312 g_string_append (result, "/UDP");
5313 g_string_append (result, ";multicast");
5314 if (src->next_port_num != 0) {
5315 if (src->client_port_range.max > 0 &&
5316 src->next_port_num >= src->client_port_range.max)
5319 g_string_append_printf (result, ";client_port=%d-%d",
5320 src->next_port_num, src->next_port_num + 1);
5322 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
5323 GST_DEBUG_OBJECT (src, "adding TCP");
5325 if (result->len > 0)
5326 g_string_append (result, ",");
5327 g_string_append (result, "RTP/AVP/TCP;unicast;interleaved=%%i1-%%i2");
5329 *transports = g_string_free (result, FALSE);
5331 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
5338 GST_ERROR ("extension gave error %d", res);
5343 GST_ERROR ("no more ports available");
5344 return GST_RTSP_ERROR;
5348 static GstRTSPResult
5349 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
5350 gint orig_rtpport, gint orig_rtcpport)
5353 gint nr_udp, nr_int;
5355 gint rtpport = 0, rtcpport = 0;
5358 src = stream->parent;
5360 /* find number of placeholders first */
5361 if (strstr (*transports, "%%i2"))
5363 else if (strstr (*transports, "%%i1"))
5368 if (strstr (*transports, "%%u2"))
5370 else if (strstr (*transports, "%%u1"))
5375 if (nr_udp == 0 && nr_int == 0)
5379 if (!orig_rtpport || !orig_rtcpport) {
5380 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
5383 rtpport = orig_rtpport;
5384 rtcpport = orig_rtcpport;
5388 str = g_string_new ("");
5390 while ((next = strstr (p, "%%"))) {
5391 g_string_append_len (str, p, next - p);
5392 if (next[2] == 'u') {
5394 g_string_append_printf (str, "%d", rtpport);
5395 else if (next[3] == '2')
5396 g_string_append_printf (str, "%d", rtcpport);
5398 if (next[2] == 'i') {
5400 g_string_append_printf (str, "%d", src->free_channel);
5401 else if (next[3] == '2')
5402 g_string_append_printf (str, "%d", src->free_channel + 1);
5407 /* append final part */
5408 g_string_append (str, p);
5410 g_free (*transports);
5411 *transports = g_string_free (str, FALSE);
5419 GST_ERROR ("failed to allocate udp ports");
5420 return GST_RTSP_ERROR;
5425 gst_rtspsrc_stream_is_real_media (GstRTSPStream * stream)
5427 gboolean res = FALSE;
5431 const gchar *enc = NULL;
5433 s = gst_caps_get_structure (stream->caps, 0);
5434 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
5435 res = (strstr (enc, "-REAL") != NULL);
5441 /* Perform the SETUP request for all the streams.
5443 * We ask the server for a specific transport, which initially includes all the
5444 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
5445 * two local UDP ports that we send to the server.
5447 * Once the server replied with a transport, we configure the other streams
5448 * with the same transport.
5450 * This function will also configure the stream for the selected transport,
5451 * which basically means creating the pipeline.
5453 static GstRTSPResult
5454 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
5457 GstRTSPResult res = GST_RTSP_ERROR;
5458 GstRTSPMessage request = { 0 };
5459 GstRTSPMessage response = { 0 };
5460 GstRTSPStream *stream = NULL;
5461 GstRTSPLowerTrans protocols;
5462 GstRTSPStatusCode code;
5463 gboolean unsupported_real = FALSE;
5464 gint rtpport, rtcpport;
5468 if (src->conninfo.connection) {
5469 url = gst_rtsp_connection_get_url (src->conninfo.connection);
5470 /* we initially allow all configured lower transports. based on the URL
5471 * transports and the replies from the server we narrow them down. */
5472 protocols = url->transports & src->cur_protocols;
5475 protocols = src->cur_protocols;
5481 /* reset some state */
5482 src->free_channel = 0;
5483 src->interleaved = FALSE;
5484 src->need_activate = FALSE;
5485 /* keep track of next port number, 0 is random */
5486 src->next_port_num = src->client_port_range.min;
5487 rtpport = rtcpport = 0;
5489 if (G_UNLIKELY (src->streams == NULL))
5492 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5493 GstRTSPConnection *conn;
5499 stream = (GstRTSPStream *) walk->data;
5501 /* see if we need to configure this stream */
5502 if (!gst_rtsp_ext_list_configure_stream (src->extensions, stream->caps)) {
5503 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
5505 stream->disabled = TRUE;
5509 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
5510 stream->id, stream->caps, &selected);
5512 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
5513 stream->disabled = TRUE;
5516 stream->disabled = FALSE;
5518 /* merge/overwrite global caps */
5523 s = gst_caps_get_structure (stream->caps, 0);
5525 num = gst_structure_n_fields (src->props);
5526 for (j = 0; j < num; j++) {
5530 name = gst_structure_nth_field_name (src->props, j);
5531 val = gst_structure_get_value (src->props, name);
5532 gst_structure_set_value (s, name, val);
5534 GST_DEBUG_OBJECT (src, "copied %s", name);
5538 /* skip setup if we have no URL for it */
5539 if (stream->conninfo.location == NULL) {
5540 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
5544 if (src->conninfo.connection == NULL) {
5545 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
5546 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
5549 conn = stream->conninfo.connection;
5551 conn = src->conninfo.connection;
5553 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
5554 stream->conninfo.location);
5556 /* if we have a multicast connection, only suggest multicast from now on */
5557 if (stream->is_multicast)
5558 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
5561 /* first selectable protocol */
5562 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5564 if (!protocol_masks[mask])
5568 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
5569 protocol_masks[mask]);
5570 /* create a string with first transport in line */
5572 res = gst_rtspsrc_create_transports_string (src,
5573 protocols & protocol_masks[mask], &transports);
5574 if (res < 0 || transports == NULL)
5575 goto setup_transport_failed;
5577 if (strlen (transports) == 0) {
5578 g_free (transports);
5579 GST_DEBUG_OBJECT (src, "no transports found");
5584 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
5586 /* replace placeholders with real values, this function will optionally
5587 * allocate UDP ports and other info needed to execute the setup request */
5588 res = gst_rtspsrc_prepare_transports (stream, &transports,
5589 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
5591 g_free (transports);
5592 goto setup_transport_failed;
5595 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
5597 /* create SETUP request */
5599 gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
5600 stream->conninfo.location);
5602 g_free (transports);
5603 goto create_request_failed;
5606 /* select transport */
5607 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
5609 /* if the user wants a non default RTP packet size we add the blocksize
5611 if (src->rtp_blocksize > 0) {
5612 hval = g_strdup_printf ("%d", src->rtp_blocksize);
5613 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
5617 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
5620 /* handle the code ourselves */
5621 if ((res = gst_rtspsrc_send (src, conn, &request, &response, &code) < 0))
5625 case GST_RTSP_STS_OK:
5627 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
5628 gst_rtsp_message_unset (&request);
5629 gst_rtsp_message_unset (&response);
5630 /* cleanup of leftover transport */
5631 gst_rtspsrc_stream_free_udp (stream);
5632 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
5633 * we might be in this case */
5634 if (stream->container && rtpport && rtcpport && !retry) {
5635 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
5640 /* this transport did not go down well, but we may have others to try
5641 * that we did not send yet, try those and only give up then
5642 * but not without checking for lost cause/extension so we can
5643 * post a nicer/more useful error message later */
5644 if (!unsupported_real)
5645 unsupported_real = gst_rtspsrc_stream_is_real_media (stream);
5646 /* select next available protocol, give up on this stream if none */
5648 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5650 if (!protocol_masks[mask] || unsupported_real)
5655 /* cleanup of leftover transport and move to the next stream */
5656 gst_rtspsrc_stream_free_udp (stream);
5657 goto response_error;
5660 /* parse response transport */
5662 gchar *resptrans = NULL;
5663 GstRTSPTransport transport = { 0 };
5665 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
5668 gst_rtspsrc_stream_free_udp (stream);
5672 /* parse transport, go to next stream on parse error */
5673 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
5674 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
5678 /* update allowed transports for other streams. once the transport of
5679 * one stream has been determined, we make sure that all other streams
5680 * are configured in the same way */
5681 switch (transport.lower_transport) {
5682 case GST_RTSP_LOWER_TRANS_TCP:
5683 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
5684 protocols = GST_RTSP_LOWER_TRANS_TCP;
5685 src->interleaved = TRUE;
5686 /* update free channels */
5688 MAX (transport.interleaved.min, src->free_channel);
5690 MAX (transport.interleaved.max, src->free_channel);
5691 src->free_channel++;
5693 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
5694 /* only allow multicast for other streams */
5695 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
5696 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
5697 /* if the server selected our ports, increment our counters so that
5698 * we select a new port later */
5699 if (src->next_port_num == transport.port.min &&
5700 src->next_port_num + 1 == transport.port.max) {
5701 src->next_port_num += 2;
5704 case GST_RTSP_LOWER_TRANS_UDP:
5705 /* only allow unicast for other streams */
5706 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
5707 protocols = GST_RTSP_LOWER_TRANS_UDP;
5710 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
5711 transport.lower_transport);
5715 if (!stream->container || (!src->interleaved && !retry)) {
5716 /* now configure the stream with the selected transport */
5717 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
5718 GST_DEBUG_OBJECT (src,
5719 "could not configure stream %p transport, skipping stream",
5722 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
5723 /* retain the first allocated UDP port pair */
5724 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
5725 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
5728 /* we need to activate at least one streams when we detect activity */
5729 src->need_activate = TRUE;
5731 /* clean up our transport struct */
5732 gst_rtsp_transport_init (&transport);
5733 /* clean up used RTSP messages */
5734 gst_rtsp_message_unset (&request);
5735 gst_rtsp_message_unset (&response);
5739 /* store the transport protocol that was configured */
5740 src->cur_protocols = protocols;
5742 gst_rtsp_ext_list_stream_select (src->extensions, url);
5744 /* if there is nothing to activate, error out */
5745 if (!src->need_activate)
5746 goto nothing_to_activate;
5753 /* no transport possible, post an error and stop */
5754 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5755 ("Could not connect to server, no protocols left"));
5756 return GST_RTSP_ERROR;
5760 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5761 ("SDP contains no streams"));
5762 return GST_RTSP_ERROR;
5764 create_request_failed:
5766 gchar *str = gst_rtsp_strresult (res);
5768 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5769 ("Could not create request. (%s)", str));
5773 setup_transport_failed:
5775 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5776 ("Could not setup transport."));
5777 res = GST_RTSP_ERROR;
5782 const gchar *str = gst_rtsp_status_as_text (code);
5784 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5785 ("Error (%d): %s", code, GST_STR_NULL (str)));
5786 res = GST_RTSP_ERROR;
5791 gchar *str = gst_rtsp_strresult (res);
5793 if (res != GST_RTSP_EINTR) {
5794 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5795 ("Could not send message. (%s)", str));
5797 GST_WARNING_OBJECT (src, "send interrupted");
5804 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5805 ("Server did not select transport."));
5806 res = GST_RTSP_ERROR;
5809 nothing_to_activate:
5811 /* none of the available error codes is really right .. */
5812 if (unsupported_real) {
5813 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5814 (_("No supported stream was found. You might need to install a "
5815 "GStreamer RTSP extension plugin for Real media streams.")),
5818 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5819 (_("No supported stream was found. You might need to allow "
5820 "more transport protocols or may otherwise be missing "
5821 "the right GStreamer RTSP extension plugin.")), (NULL));
5823 return GST_RTSP_ERROR;
5827 gst_rtsp_message_unset (&request);
5828 gst_rtsp_message_unset (&response);
5834 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
5835 GstSegment * segment)
5838 GstRTSPTimeRange *therange;
5841 gst_rtsp_range_free (src->range);
5843 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
5844 GST_DEBUG_OBJECT (src, "parsed range %s", range);
5845 src->range = therange;
5847 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
5849 gst_segment_init (segment, GST_FORMAT_TIME);
5853 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
5854 therange->min.type, therange->min.seconds, therange->max.type,
5855 therange->max.seconds);
5857 if (therange->min.type == GST_RTSP_TIME_NOW)
5859 else if (therange->min.type == GST_RTSP_TIME_END)
5862 seconds = therange->min.seconds * GST_SECOND;
5864 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
5865 GST_TIME_ARGS (seconds));
5867 /* we need to start playback without clipping from the position reported by
5869 segment->start = seconds;
5870 segment->position = seconds;
5872 if (therange->max.type == GST_RTSP_TIME_NOW)
5874 else if (therange->max.type == GST_RTSP_TIME_END)
5877 seconds = therange->max.seconds * GST_SECOND;
5879 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
5880 GST_TIME_ARGS (seconds));
5882 /* live (WMS) server might send overflowed large max as its idea of infinity,
5883 * compensate to prevent problems later on */
5884 if (seconds != -1 && seconds < 0) {
5886 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
5889 /* live (WMS) might send min == max, which is not worth recording */
5890 if (segment->duration == -1 && seconds == segment->start)
5893 /* don't change duration with unknown value, we might have a valid value
5894 * there that we want to keep. */
5896 segment->duration = seconds;
5901 /* Parse clock profived by the server with following syntax:
5903 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
5906 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
5908 gboolean res = FALSE;
5910 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
5911 gchar **fields = NULL, **parts = NULL;
5912 gchar *remote_ip, *str;
5914 GstClockTime base_time;
5917 fields = g_strsplit (gstclock, " ", 0);
5919 /* wrapped clock, not very interesting for now */
5920 if (fields[1] == NULL)
5923 /* remote IP address and port */
5924 if ((str = fields[2]) == NULL)
5927 parts = g_strsplit (str, ":", 0);
5929 if ((remote_ip = parts[0]) == NULL)
5932 if ((str = parts[1]) == NULL)
5940 if ((str = fields[3]) == NULL)
5943 base_time = g_ascii_strtoull (str, NULL, 10);
5946 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
5949 if (src->provided_clock)
5950 gst_object_unref (src->provided_clock);
5951 src->provided_clock = netclock;
5953 gst_element_post_message (GST_ELEMENT_CAST (src),
5954 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
5955 src->provided_clock, TRUE));
5959 g_strfreev (fields);
5965 /* must be called with the RTSP state lock */
5966 static GstRTSPResult
5967 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
5973 /* prepare global stream caps properties */
5975 gst_structure_remove_all_fields (src->props);
5977 src->props = gst_structure_new_empty ("RTSPProperties");
5980 gst_sdp_message_dump (sdp);
5982 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
5984 /* let the app inspect and change the SDP */
5985 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
5987 gst_segment_init (&src->segment, GST_FORMAT_TIME);
5989 /* parse range for duration reporting. */
5994 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
5998 /* keep track of the range and configure it in the segment */
5999 if (gst_rtspsrc_parse_range (src, range, &src->segment))
6003 /* parse clock information. This is GStreamer specific, a server can tell the
6004 * client what clock it is using and wrap that in a network clock. The
6005 * advantage of that is that we can slave to it. */
6007 const gchar *gstclock;
6010 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
6011 if (gstclock == NULL)
6014 /* parse the clock and expose it in the provide_clock method */
6015 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
6019 /* try to find a global control attribute. Note that a '*' means that we should
6020 * do aggregate control with the current url (so we don't do anything and
6021 * leave the current connection as is) */
6023 const gchar *control;
6026 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
6027 if (control == NULL)
6030 /* only take fully qualified urls */
6031 if (g_str_has_prefix (control, "rtsp://"))
6035 g_free (src->conninfo.location);
6036 src->conninfo.location = g_strdup (control);
6037 /* make a connection for this, if there was a connection already, nothing
6039 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
6040 GST_ERROR_OBJECT (src, "could not connect");
6043 /* we need to keep the control url separate from the connection url because
6044 * the rules for constructing the media control url need it */
6045 g_free (src->control);
6046 src->control = g_strdup (control);
6049 /* create streams */
6050 n_streams = gst_sdp_message_medias_len (sdp);
6051 for (i = 0; i < n_streams; i++) {
6052 gst_rtspsrc_create_stream (src, sdp, i);
6055 src->state = GST_RTSP_STATE_INIT;
6058 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
6061 /* reset our state */
6062 src->need_range = TRUE;
6065 src->state = GST_RTSP_STATE_READY;
6072 GST_ERROR_OBJECT (src, "setup failed");
6073 gst_rtspsrc_cleanup (src);
6078 static GstRTSPResult
6079 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
6083 GstRTSPMessage request = { 0 };
6084 GstRTSPMessage response = { 0 };
6087 gchar *respcont = NULL;
6090 src->need_redirect = FALSE;
6092 /* can't continue without a valid url */
6093 if (G_UNLIKELY (src->conninfo.url == NULL)) {
6094 res = GST_RTSP_EINVAL;
6097 src->tried_url_auth = FALSE;
6099 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
6100 goto connect_failed;
6102 /* create OPTIONS */
6103 GST_DEBUG_OBJECT (src, "create options...");
6105 gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
6106 src->conninfo.url_str);
6108 goto create_request_failed;
6111 GST_DEBUG_OBJECT (src, "send options...");
6114 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
6117 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6122 if (!gst_rtspsrc_parse_methods (src, &response))
6125 /* create DESCRIBE */
6126 GST_DEBUG_OBJECT (src, "create describe...");
6128 gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
6129 src->conninfo.url_str);
6131 goto create_request_failed;
6133 /* we only accept SDP for now */
6134 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
6138 GST_DEBUG_OBJECT (src, "send describe...");
6141 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
6144 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6148 /* we only perform redirect for the describe, currently */
6149 if (src->need_redirect) {
6150 /* close connection, we don't have to send a TEARDOWN yet, ignore the
6152 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6154 gst_rtsp_message_unset (&request);
6155 gst_rtsp_message_unset (&response);
6161 /* it could be that the DESCRIBE method was not implemented */
6162 if (!src->methods & GST_RTSP_DESCRIBE)
6165 /* check if reply is SDP */
6166 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
6168 /* could not be set but since the request returned OK, we assume it
6169 * was SDP, else check it. */
6171 if (!g_ascii_strcasecmp (respcont, "application/sdp") == 0)
6172 goto wrong_content_type;
6175 /* get message body and parse as SDP */
6176 gst_rtsp_message_get_body (&response, &data, &size);
6177 if (data == NULL || size == 0)
6180 GST_DEBUG_OBJECT (src, "parse SDP...");
6181 gst_sdp_message_new (sdp);
6182 gst_sdp_message_parse_buffer (data, size, *sdp);
6184 /* clean up any messages */
6185 gst_rtsp_message_unset (&request);
6186 gst_rtsp_message_unset (&response);
6193 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
6194 ("No valid RTSP URL was provided"));
6199 gchar *str = gst_rtsp_strresult (res);
6201 if (res != GST_RTSP_EINTR) {
6202 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
6203 ("Failed to connect. (%s)", str));
6205 GST_WARNING_OBJECT (src, "connect interrupted");
6210 create_request_failed:
6212 gchar *str = gst_rtsp_strresult (res);
6214 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6215 ("Could not create request. (%s)", str));
6221 /* Don't post a message - the rtsp_send method will have
6222 * taken care of it because we passed NULL for the response code */
6227 /* error was posted */
6228 res = GST_RTSP_ERROR;
6233 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6234 ("Server does not support SDP, got %s.", respcont));
6235 res = GST_RTSP_ERROR;
6240 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6241 ("Server can not provide an SDP."));
6242 res = GST_RTSP_ERROR;
6247 if (src->conninfo.connection) {
6248 GST_DEBUG_OBJECT (src, "free connection");
6249 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6251 gst_rtsp_message_unset (&request);
6252 gst_rtsp_message_unset (&response);
6257 static GstRTSPResult
6258 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
6263 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
6265 if (src->sdp == NULL) {
6266 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
6270 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
6275 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
6282 GST_WARNING_OBJECT (src, "can't get sdp");
6283 src->open_error = TRUE;
6288 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
6289 src->open_error = TRUE;
6294 static GstRTSPResult
6295 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
6297 GstRTSPMessage request = { 0 };
6298 GstRTSPMessage response = { 0 };
6299 GstRTSPResult res = GST_RTSP_OK;
6303 GST_DEBUG_OBJECT (src, "TEARDOWN...");
6305 gst_rtspsrc_set_state (src, GST_STATE_READY);
6307 if (src->state < GST_RTSP_STATE_READY) {
6308 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
6315 /* construct a control url */
6317 control = src->control;
6319 control = src->conninfo.url_str;
6321 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
6324 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6325 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6327 GstRTSPConnInfo *info;
6329 /* try aggregate control first but do non-aggregate control otherwise */
6331 setup_url = control;
6332 else if ((setup_url = stream->conninfo.location) == NULL)
6335 if (src->conninfo.connection) {
6336 info = &src->conninfo;
6337 } else if (stream->conninfo.connection) {
6338 info = &stream->conninfo;
6342 if (!info->connected)
6347 gst_rtsp_message_init_request (&request, GST_RTSP_TEARDOWN, setup_url);
6349 goto create_request_failed;
6352 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
6355 gst_rtspsrc_send (src, info->connection, &request, &response,
6359 /* FIXME, parse result? */
6360 gst_rtsp_message_unset (&request);
6361 gst_rtsp_message_unset (&response);
6364 /* early exit when we did aggregate control */
6370 /* close connections */
6371 GST_DEBUG_OBJECT (src, "closing connection...");
6372 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6373 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6374 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6375 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
6379 gst_rtspsrc_cleanup (src);
6381 src->state = GST_RTSP_STATE_INVALID;
6384 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
6389 create_request_failed:
6391 gchar *str = gst_rtsp_strresult (res);
6393 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6394 ("Could not create request. (%s)", str));
6400 gchar *str = gst_rtsp_strresult (res);
6402 gst_rtsp_message_unset (&request);
6403 if (res != GST_RTSP_EINTR) {
6404 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6405 ("Could not send message. (%s)", str));
6407 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
6414 GST_DEBUG_OBJECT (src,
6415 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
6420 /* RTP-Info is of the format:
6422 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
6424 * rtptime corresponds to the timestamp for the NPT time given in the header
6425 * seqbase corresponds to the next sequence number we received. This number
6426 * indicates the first seqnum after the seek and should be used to discard
6427 * packets that are from before the seek.
6430 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
6435 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
6437 infos = g_strsplit (rtpinfo, ",", 0);
6438 for (i = 0; infos[i]; i++) {
6440 GstRTSPStream *stream;
6444 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
6446 /* init values, types of seqbase and timebase are bigger than needed so we
6447 * can store -1 as uninitialized values */
6452 /* parse url, find stream for url.
6453 * parse seq and rtptime. The seq number should be configured in the rtp
6454 * depayloader or session manager to detect gaps. Same for the rtptime, it
6455 * should be used to create an initial time newsegment. */
6456 fields = g_strsplit (infos[i], ";", 0);
6457 for (j = 0; fields[j]; j++) {
6458 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
6459 /* remove leading whitespace */
6460 fields[j] = g_strchug (fields[j]);
6461 if (g_str_has_prefix (fields[j], "url=")) {
6462 /* get the url and the stream */
6464 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
6465 } else if (g_str_has_prefix (fields[j], "seq=")) {
6466 seqbase = atoi (fields[j] + 4);
6467 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
6468 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
6471 g_strfreev (fields);
6472 /* now we need to store the values for the caps of the stream */
6473 if (stream != NULL) {
6474 GST_DEBUG_OBJECT (src,
6475 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
6476 stream, seqbase, timebase);
6478 /* we have a stream, configure detected params */
6479 stream->seqbase = seqbase;
6480 stream->timebase = timebase;
6489 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
6494 interval = strtoul (rtcp, NULL, 10);
6495 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
6500 interval *= GST_MSECOND;
6502 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6503 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6505 /* already (optionally) retrieved this when configuring manager */
6506 if (stream->session) {
6507 GObject *rtpsession = stream->session;
6509 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
6511 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
6515 /* now it happens that (Xenon) server sending this may also provide bogus
6516 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
6517 * and just use RTP-Info to sync */
6519 GObjectClass *klass;
6521 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
6522 if (g_object_class_find_property (klass, "rtcp-sync")) {
6523 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
6524 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
6530 gst_rtspsrc_get_float (const gchar * dstr)
6532 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
6534 /* canonicalise floating point string so we can handle float strings
6535 * in the form "24.930" or "24,930" irrespective of the current locale */
6536 g_strlcpy (s, dstr, sizeof (s));
6537 g_strdelimit (s, ",", '.');
6538 return g_ascii_strtod (s, NULL);
6542 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
6544 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
6546 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
6547 g_strlcpy (val_str, "now", sizeof (val_str));
6549 if (segment->position == 0) {
6550 g_strlcpy (val_str, "0", sizeof (val_str));
6552 g_ascii_dtostr (val_str, sizeof (val_str),
6553 ((gdouble) segment->position) / GST_SECOND);
6556 return g_strdup_printf ("npt=%s-", val_str);
6560 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
6562 stream->timebase = -1;
6563 stream->seqbase = -1;
6567 stream->caps = gst_caps_make_writable (stream->caps);
6568 s = gst_caps_get_structure (stream->caps, 0);
6569 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
6573 static GstRTSPResult
6574 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
6576 GstRTSPResult res = GST_RTSP_OK;
6578 if (src->state < GST_RTSP_STATE_READY) {
6579 res = GST_RTSP_ERROR;
6580 if (src->open_error) {
6581 GST_DEBUG_OBJECT (src, "the stream was in error");
6585 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
6587 if ((res = gst_rtspsrc_open (src, async)) < 0) {
6588 GST_DEBUG_OBJECT (src, "failed to open stream");
6597 static GstRTSPResult
6598 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
6600 GstRTSPMessage request = { 0 };
6601 GstRTSPMessage response = { 0 };
6602 GstRTSPResult res = GST_RTSP_OK;
6608 GST_DEBUG_OBJECT (src, "PLAY...");
6610 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6613 if (!(src->methods & GST_RTSP_PLAY))
6616 if (src->state == GST_RTSP_STATE_PLAYING)
6619 if (!src->conninfo.connection || !src->conninfo.connected)
6622 /* send some dummy packets before we activate the receive in the
6624 gst_rtspsrc_send_dummy_packets (src);
6626 /* require new SR packets */
6628 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
6630 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
6632 /* construct a control url */
6634 control = src->control;
6636 control = src->conninfo.url_str;
6638 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6639 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6641 GstRTSPConnection *conn;
6643 /* try aggregate control first but do non-aggregate control otherwise */
6645 setup_url = control;
6646 else if ((setup_url = stream->conninfo.location) == NULL)
6649 if (src->conninfo.connection) {
6650 conn = src->conninfo.connection;
6651 } else if (stream->conninfo.connection) {
6652 conn = stream->conninfo.connection;
6658 res = gst_rtsp_message_init_request (&request, GST_RTSP_PLAY, setup_url);
6660 goto create_request_failed;
6662 if (src->need_range) {
6663 hval = gen_range_header (src, segment);
6665 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
6667 /* store the newsegment event so it can be sent from the streaming thread. */
6668 if (src->start_segment)
6669 gst_event_unref (src->start_segment);
6670 src->start_segment = gst_event_new_segment (&src->segment);
6673 if (segment->rate != 1.0) {
6674 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
6676 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
6678 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
6680 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
6684 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
6686 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6689 /* seek may have silently failed as it is not supported */
6690 if (!(src->methods & GST_RTSP_PLAY)) {
6691 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
6692 /* obviously it is supported as we made it here */
6693 src->methods |= GST_RTSP_PLAY;
6694 src->seekable = FALSE;
6695 /* but there is nothing to parse in the response,
6696 * so convey we have no idea and not to expect anything particular */
6697 clear_rtp_base (src, stream);
6701 /* need to do for all streams */
6702 for (run = src->streams; run; run = g_list_next (run))
6703 clear_rtp_base (src, (GstRTSPStream *) run->data);
6705 /* NOTE the above also disables npt based eos detection */
6706 /* and below forces position to 0,
6707 * which is visible feedback we lost the plot */
6708 segment->start = segment->position = src->last_pos;
6711 gst_rtsp_message_unset (&request);
6713 /* parse RTP npt field. This is the current position in the stream (Normal
6714 * Play Time) and should be put in the NEWSEGMENT position field. */
6715 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
6717 gst_rtspsrc_parse_range (src, hval, segment);
6719 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
6720 segment->rate = 1.0;
6722 /* parse Speed header. This is the intended playback rate of the stream
6723 * and should be put in the NEWSEGMENT rate field. */
6724 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
6725 0) == GST_RTSP_OK) {
6726 segment->rate = gst_rtspsrc_get_float (hval);
6727 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
6728 &hval, 0) == GST_RTSP_OK) {
6729 segment->rate = gst_rtspsrc_get_float (hval);
6732 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
6733 * for the RTP packets. If this is not present, we assume all starts from 0...
6734 * This is info for the RTP session manager that we pass to it in caps. */
6736 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
6737 &hval, hval_idx++) == GST_RTSP_OK)
6738 gst_rtspsrc_parse_rtpinfo (src, hval);
6740 /* some servers indicate RTCP parameters in PLAY response,
6741 * rather than properly in SDP */
6742 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
6743 &hval, 0) == GST_RTSP_OK)
6744 gst_rtspsrc_handle_rtcp_interval (src, hval);
6746 gst_rtsp_message_unset (&response);
6748 /* early exit when we did aggregate control */
6752 /* configure the caps of the streams after we parsed all headers. Only reset
6753 * the manager object when we set a new Range header (we did a seek) */
6754 gst_rtspsrc_configure_caps (src, segment, src->need_range);
6756 /* set again when needed */
6757 src->need_range = FALSE;
6759 src->running = TRUE;
6760 src->base_time = -1;
6761 src->state = GST_RTSP_STATE_PLAYING;
6764 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
6765 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6766 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6767 stream->discont = TRUE;
6772 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
6779 GST_DEBUG_OBJECT (src, "failed to open stream");
6784 GST_DEBUG_OBJECT (src, "PLAY is not supported");
6789 GST_DEBUG_OBJECT (src, "we were already PLAYING");
6792 create_request_failed:
6794 gchar *str = gst_rtsp_strresult (res);
6796 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6797 ("Could not create request. (%s)", str));
6803 gchar *str = gst_rtsp_strresult (res);
6805 gst_rtsp_message_unset (&request);
6806 if (res != GST_RTSP_EINTR) {
6807 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6808 ("Could not send message. (%s)", str));
6810 GST_WARNING_OBJECT (src, "PLAY interrupted");
6817 static GstRTSPResult
6818 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
6820 GstRTSPResult res = GST_RTSP_OK;
6821 GstRTSPMessage request = { 0 };
6822 GstRTSPMessage response = { 0 };
6826 GST_DEBUG_OBJECT (src, "PAUSE...");
6828 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6831 if (!(src->methods & GST_RTSP_PAUSE))
6834 if (src->state == GST_RTSP_STATE_READY)
6837 if (!src->conninfo.connection || !src->conninfo.connected)
6840 /* construct a control url */
6842 control = src->control;
6844 control = src->conninfo.url_str;
6846 /* loop over the streams. We might exit the loop early when we could do an
6847 * aggregate control */
6848 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6849 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6850 GstRTSPConnection *conn;
6853 /* try aggregate control first but do non-aggregate control otherwise */
6855 setup_url = control;
6856 else if ((setup_url = stream->conninfo.location) == NULL)
6859 if (src->conninfo.connection) {
6860 conn = src->conninfo.connection;
6861 } else if (stream->conninfo.connection) {
6862 conn = stream->conninfo.connection;
6868 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
6869 ("Sending PAUSE request"));
6872 gst_rtsp_message_init_request (&request, GST_RTSP_PAUSE,
6874 goto create_request_failed;
6876 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6879 gst_rtsp_message_unset (&request);
6880 gst_rtsp_message_unset (&response);
6882 /* exit early when we did agregate control */
6887 /* change element states now */
6888 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
6891 src->state = GST_RTSP_STATE_READY;
6895 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
6902 GST_DEBUG_OBJECT (src, "failed to open stream");
6907 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
6912 GST_DEBUG_OBJECT (src, "we were already PAUSED");
6915 create_request_failed:
6917 gchar *str = gst_rtsp_strresult (res);
6919 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6920 ("Could not create request. (%s)", str));
6926 gchar *str = gst_rtsp_strresult (res);
6928 gst_rtsp_message_unset (&request);
6929 if (res != GST_RTSP_EINTR) {
6930 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6931 ("Could not send message. (%s)", str));
6933 GST_WARNING_OBJECT (src, "PAUSE interrupted");
6941 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
6943 GstRTSPSrc *rtspsrc;
6945 rtspsrc = GST_RTSPSRC (bin);
6947 switch (GST_MESSAGE_TYPE (message)) {
6948 case GST_MESSAGE_EOS:
6949 gst_message_unref (message);
6951 case GST_MESSAGE_ELEMENT:
6953 const GstStructure *s = gst_message_get_structure (message);
6955 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
6956 gboolean ignore_timeout;
6958 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
6960 GST_OBJECT_LOCK (rtspsrc);
6961 ignore_timeout = rtspsrc->ignore_timeout;
6962 rtspsrc->ignore_timeout = TRUE;
6963 GST_OBJECT_UNLOCK (rtspsrc);
6965 /* we only act on the first udp timeout message, others are irrelevant
6966 * and can be ignored. */
6967 if (!ignore_timeout)
6968 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
6970 gst_message_unref (message);
6973 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6976 case GST_MESSAGE_ERROR:
6979 GstRTSPStream *stream;
6982 udpsrc = GST_MESSAGE_SRC (message);
6984 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
6985 GST_ELEMENT_NAME (udpsrc));
6987 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
6991 /* we ignore the RTCP udpsrc */
6992 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
6995 /* if we get error messages from the udp sources, that's not a problem as
6996 * long as not all of them error out. We also don't really know what the
6997 * problem is, the message does not give enough detail... */
6998 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
6999 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
7000 if (ret != GST_FLOW_OK)
7004 gst_message_unref (message);
7008 /* fatal but not our message, forward */
7009 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7014 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7020 /* the thread where everything happens */
7022 gst_rtspsrc_thread (GstRTSPSrc * src)
7026 GST_OBJECT_LOCK (src);
7027 cmd = src->pending_cmd;
7028 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
7029 || cmd == CMD_LOOP || cmd == CMD_OPEN)
7030 src->pending_cmd = CMD_LOOP;
7032 src->pending_cmd = CMD_WAIT;
7033 GST_DEBUG_OBJECT (src, "got command %d", cmd);
7035 /* we got the message command, so ensure communication is possible again */
7036 gst_rtspsrc_connection_flush (src, FALSE);
7038 src->busy_cmd = cmd;
7039 GST_OBJECT_UNLOCK (src);
7043 gst_rtspsrc_open (src, TRUE);
7046 gst_rtspsrc_play (src, &src->segment, TRUE);
7049 gst_rtspsrc_pause (src, TRUE);
7052 gst_rtspsrc_close (src, TRUE, FALSE);
7055 gst_rtspsrc_loop (src);
7058 gst_rtspsrc_reconnect (src, FALSE);
7064 GST_OBJECT_LOCK (src);
7065 /* and go back to sleep */
7066 if (src->pending_cmd == CMD_WAIT) {
7068 gst_task_pause (src->task);
7071 src->busy_cmd = CMD_WAIT;
7072 GST_OBJECT_UNLOCK (src);
7076 gst_rtspsrc_start (GstRTSPSrc * src)
7078 GST_DEBUG_OBJECT (src, "starting");
7080 GST_OBJECT_LOCK (src);
7082 src->pending_cmd = CMD_WAIT;
7084 if (src->task == NULL) {
7085 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
7086 if (src->task == NULL)
7089 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
7091 GST_OBJECT_UNLOCK (src);
7098 GST_ERROR_OBJECT (src, "failed to create task");
7104 gst_rtspsrc_stop (GstRTSPSrc * src)
7108 GST_DEBUG_OBJECT (src, "stopping");
7110 /* also cancels pending task */
7111 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
7113 GST_OBJECT_LOCK (src);
7114 if ((task = src->task)) {
7116 GST_OBJECT_UNLOCK (src);
7118 gst_task_stop (task);
7120 /* make sure it is not running */
7121 GST_RTSP_STREAM_LOCK (src);
7122 GST_RTSP_STREAM_UNLOCK (src);
7124 /* now wait for the task to finish */
7125 gst_task_join (task);
7127 /* and free the task */
7128 gst_object_unref (GST_OBJECT (task));
7130 GST_OBJECT_LOCK (src);
7132 GST_OBJECT_UNLOCK (src);
7134 /* ensure synchronously all is closed and clean */
7135 gst_rtspsrc_close (src, FALSE, TRUE);
7140 static GstStateChangeReturn
7141 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
7143 GstRTSPSrc *rtspsrc;
7144 GstStateChangeReturn ret;
7146 rtspsrc = GST_RTSPSRC (element);
7148 switch (transition) {
7149 case GST_STATE_CHANGE_NULL_TO_READY:
7150 if (!gst_rtspsrc_start (rtspsrc))
7153 case GST_STATE_CHANGE_READY_TO_PAUSED:
7154 /* init some state */
7155 rtspsrc->cur_protocols = rtspsrc->protocols;
7156 /* first attempt, don't ignore timeouts */
7157 rtspsrc->ignore_timeout = FALSE;
7158 rtspsrc->open_error = FALSE;
7159 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
7161 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7162 set_manager_buffer_mode (rtspsrc);
7164 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7165 /* unblock the tcp tasks and make the loop waiting */
7166 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
7167 /* make sure it is waiting before we send PAUSE or PLAY below */
7168 GST_RTSP_STREAM_LOCK (rtspsrc);
7169 GST_RTSP_STREAM_UNLOCK (rtspsrc);
7172 case GST_STATE_CHANGE_PAUSED_TO_READY:
7178 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
7179 if (ret == GST_STATE_CHANGE_FAILURE)
7182 switch (transition) {
7183 case GST_STATE_CHANGE_NULL_TO_READY:
7184 ret = GST_STATE_CHANGE_SUCCESS;
7186 case GST_STATE_CHANGE_READY_TO_PAUSED:
7187 ret = GST_STATE_CHANGE_NO_PREROLL;
7189 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7190 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
7191 ret = GST_STATE_CHANGE_SUCCESS;
7193 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7194 /* send pause request and keep the idle task around */
7195 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
7196 ret = GST_STATE_CHANGE_NO_PREROLL;
7198 case GST_STATE_CHANGE_PAUSED_TO_READY:
7199 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_PAUSE);
7200 ret = GST_STATE_CHANGE_SUCCESS;
7202 case GST_STATE_CHANGE_READY_TO_NULL:
7203 gst_rtspsrc_stop (rtspsrc);
7204 ret = GST_STATE_CHANGE_SUCCESS;
7215 GST_DEBUG_OBJECT (rtspsrc, "start failed");
7216 return GST_STATE_CHANGE_FAILURE;
7221 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
7224 GstRTSPSrc *rtspsrc;
7226 rtspsrc = GST_RTSPSRC (element);
7228 if (GST_EVENT_IS_DOWNSTREAM (event)) {
7229 res = gst_rtspsrc_push_event (rtspsrc, event);
7231 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
7238 /*** GSTURIHANDLER INTERFACE *************************************************/
7241 gst_rtspsrc_uri_get_type (GType type)
7246 static const gchar *const *
7247 gst_rtspsrc_uri_get_protocols (GType type)
7249 static const gchar *protocols[] =
7250 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
7251 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
7258 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
7260 GstRTSPSrc *src = GST_RTSPSRC (handler);
7262 /* FIXME: make thread-safe */
7263 return g_strdup (src->conninfo.location);
7267 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
7272 GstRTSPUrl *newurl = NULL;
7273 GstSDPMessage *sdp = NULL;
7275 src = GST_RTSPSRC (handler);
7277 /* same URI, we're fine */
7278 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
7281 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
7282 if ((res = gst_sdp_message_new (&sdp) < 0))
7285 GST_DEBUG_OBJECT (src, "parsing SDP message");
7286 if ((res = gst_sdp_message_parse_uri (uri, sdp) < 0))
7290 GST_DEBUG_OBJECT (src, "parsing URI");
7291 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
7295 /* if worked, free previous and store new url object along with the original
7297 GST_DEBUG_OBJECT (src, "configuring URI");
7298 g_free (src->conninfo.location);
7299 src->conninfo.location = g_strdup (uri);
7300 gst_rtsp_url_free (src->conninfo.url);
7301 src->conninfo.url = newurl;
7302 g_free (src->conninfo.url_str);
7304 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
7306 src->conninfo.url_str = NULL;
7309 gst_sdp_message_free (src->sdp);
7311 src->from_sdp = sdp != NULL;
7313 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
7314 GST_DEBUG_OBJECT (src, "request uri is: %s",
7315 GST_STR_NULL (src->conninfo.url_str));
7322 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
7327 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", res);
7328 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7329 "Could not create SDP");
7334 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", res,
7335 GST_STR_NULL (uri));
7336 gst_sdp_message_free (sdp);
7337 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7343 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
7344 GST_STR_NULL (uri), res);
7345 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7346 "Invalid RTSP URI");
7352 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
7354 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
7356 iface->get_type = gst_rtspsrc_uri_get_type;
7357 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
7358 iface->get_uri = gst_rtspsrc_uri_get_uri;
7359 iface->set_uri = gst_rtspsrc_uri_set_uri;