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_DO_RETRANSMISSION FALSE
176 #define DEFAULT_CONNECTION_SPEED 0
177 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
178 #define DEFAULT_DO_RTCP TRUE
179 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
180 #define DEFAULT_PROXY NULL
181 #define DEFAULT_RTP_BLOCKSIZE 0
182 #define DEFAULT_USER_ID NULL
183 #define DEFAULT_USER_PW NULL
184 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
185 #define DEFAULT_PORT_RANGE NULL
186 #define DEFAULT_SHORT_HEADER FALSE
187 #define DEFAULT_PROBATION 2
188 #define DEFAULT_UDP_RECONNECT TRUE
189 #define DEFAULT_MULTICAST_IFACE NULL
190 #define DEFAULT_NTP_SYNC FALSE
191 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
192 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
204 PROP_DROP_ON_LATENCY,
205 PROP_DO_RETRANSMISSION,
206 PROP_CONNECTION_SPEED,
209 PROP_DO_RTSP_KEEP_ALIVE,
218 PROP_UDP_BUFFER_SIZE,
222 PROP_MULTICAST_IFACE,
224 PROP_USE_PIPELINE_CLOCK,
226 PROP_TLS_VALIDATION_FLAGS,
230 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
232 gst_rtsp_nat_method_get_type (void)
234 static GType rtsp_nat_method_type = 0;
235 static const GEnumValue rtsp_nat_method[] = {
236 {GST_RTSP_NAT_NONE, "None", "none"},
237 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
241 if (!rtsp_nat_method_type) {
242 rtsp_nat_method_type =
243 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
245 return rtsp_nat_method_type;
248 static void gst_rtspsrc_finalize (GObject * object);
250 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
251 const GValue * value, GParamSpec * pspec);
252 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
253 GValue * value, GParamSpec * pspec);
255 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
257 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
258 gpointer iface_data);
260 static void gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes,
263 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
264 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
266 static GstCaps *gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media);
268 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
269 GstStateChange transition);
270 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
271 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
273 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
274 GstRTSPMessage * response);
276 static gboolean gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
278 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
279 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
281 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
282 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
284 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
285 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
286 gboolean only_close);
288 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
289 const gchar * uri, GError ** error);
290 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
292 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
293 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
294 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
295 GstRTSPStream * stream, GstEvent * event);
296 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
298 /* commands we send to out loop to notify it of events */
299 #define CMD_OPEN (1 << 0)
300 #define CMD_PLAY (1 << 1)
301 #define CMD_PAUSE (1 << 2)
302 #define CMD_CLOSE (1 << 3)
303 #define CMD_WAIT (1 << 4)
304 #define CMD_RECONNECT (1 << 5)
305 #define CMD_LOOP (1 << 6)
307 /* mask for all commands */
308 #define CMD_ALL ((CMD_LOOP << 1) - 1)
310 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
312 gchar *__txt = _gst_element_error_printf text; \
313 gst_element_post_message (GST_ELEMENT_CAST (el), \
314 gst_message_new_progress (GST_OBJECT_CAST (el), \
315 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
319 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
321 #define gst_rtspsrc_parent_class parent_class
322 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
323 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
326 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
328 GST_DEBUG_OBJECT (src, "default handler");
333 select_stream_accum (GSignalInvocationHint * ihint,
334 GValue * return_accu, const GValue * handler_return, gpointer data)
338 myboolean = g_value_get_boolean (handler_return);
339 GST_DEBUG ("accum %d", myboolean);
340 g_value_set_boolean (return_accu, myboolean);
342 /* stop emission if FALSE */
347 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
349 GObjectClass *gobject_class;
350 GstElementClass *gstelement_class;
351 GstBinClass *gstbin_class;
353 gobject_class = (GObjectClass *) klass;
354 gstelement_class = (GstElementClass *) klass;
355 gstbin_class = (GstBinClass *) klass;
357 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
359 gobject_class->set_property = gst_rtspsrc_set_property;
360 gobject_class->get_property = gst_rtspsrc_get_property;
362 gobject_class->finalize = gst_rtspsrc_finalize;
364 g_object_class_install_property (gobject_class, PROP_LOCATION,
365 g_param_spec_string ("location", "RTSP Location",
366 "Location of the RTSP url to read",
367 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
369 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
370 g_param_spec_flags ("protocols", "Protocols",
371 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
372 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
374 g_object_class_install_property (gobject_class, PROP_DEBUG,
375 g_param_spec_boolean ("debug", "Debug",
376 "Dump request and response messages to stdout",
377 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
379 g_object_class_install_property (gobject_class, PROP_RETRY,
380 g_param_spec_uint ("retry", "Retry",
381 "Max number of retries when allocating RTP ports.",
382 0, G_MAXUINT16, DEFAULT_RETRY,
383 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
385 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
386 g_param_spec_uint64 ("timeout", "Timeout",
387 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
388 0, G_MAXUINT64, DEFAULT_TIMEOUT,
389 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
391 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
392 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
393 "Fail after timeout microseconds on TCP connections (0 = disabled)",
394 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
395 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
397 g_object_class_install_property (gobject_class, PROP_LATENCY,
398 g_param_spec_uint ("latency", "Buffer latency in ms",
399 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
400 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
402 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
403 g_param_spec_boolean ("drop-on-latency",
404 "Drop buffers when maximum latency is reached",
405 "Tells the jitterbuffer to never exceed the given latency in size",
406 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
408 g_object_class_install_property (gobject_class, PROP_DO_RETRANSMISSION,
409 g_param_spec_boolean ("do-retransmission", "Do retransmission",
410 "Send retransmission events upstream when a packet is late",
411 DEFAULT_DO_RETRANSMISSION,
412 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
414 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
415 g_param_spec_uint64 ("connection-speed", "Connection Speed",
416 "Network connection speed in kbps (0 = unknown)",
417 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
418 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
420 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
421 g_param_spec_enum ("nat-method", "NAT Method",
422 "Method to use for traversing firewalls and NAT",
423 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
424 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
427 * GstRTSPSrc:do-rtcp:
429 * Enable RTCP support. Some old server don't like RTCP and then this property
430 * needs to be set to FALSE.
432 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
433 g_param_spec_boolean ("do-rtcp", "Do RTCP",
434 "Send RTCP packets, disable for old incompatible server.",
435 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
438 * GstRTSPSrc:do-rtsp-keep-alive:
440 * Enable RTSP keep alive support. Some old server don't like RTSP
441 * keep alive and then this property needs to be set to FALSE.
443 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
444 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
445 "Send RTSP keep alive packets, disable for old incompatible server.",
446 DEFAULT_DO_RTSP_KEEP_ALIVE,
447 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
452 * Set the proxy parameters. This has to be a string of the format
453 * [http://][user:passwd@]host[:port].
455 g_object_class_install_property (gobject_class, PROP_PROXY,
456 g_param_spec_string ("proxy", "Proxy",
457 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
458 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
460 * GstRTSPSrc:proxy-id:
462 * Sets the proxy URI user id for authentication. If the URI set via the
463 * "proxy" property contains a user-id already, that will take precedence.
467 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
468 g_param_spec_string ("proxy-id", "proxy-id",
469 "HTTP proxy URI user id for authentication", "",
470 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
472 * GstRTSPSrc:proxy-pw:
474 * Sets the proxy URI password for authentication. If the URI set via the
475 * "proxy" property contains a password already, that will take precedence.
479 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
480 g_param_spec_string ("proxy-pw", "proxy-pw",
481 "HTTP proxy URI user password for authentication", "",
482 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
485 * GstRTSPSrc:rtp-blocksize:
487 * RTP package size to suggest to server.
489 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
490 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
491 "RTP package size to suggest to server (0 = disabled)",
492 0, 65536, DEFAULT_RTP_BLOCKSIZE,
493 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
495 g_object_class_install_property (gobject_class,
497 g_param_spec_string ("user-id", "user-id",
498 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
499 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
500 g_object_class_install_property (gobject_class, PROP_USER_PW,
501 g_param_spec_string ("user-pw", "user-pw",
502 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
503 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
506 * GstRTSPSrc:buffer-mode:
508 * Control the buffering and timestamping mode used by the jitterbuffer.
510 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
511 g_param_spec_enum ("buffer-mode", "Buffer Mode",
512 "Control the buffering algorithm in use",
513 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
514 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
517 * GstRTSPSrc:port-range:
519 * Configure the client port numbers that can be used to recieve RTP and
522 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
523 g_param_spec_string ("port-range", "Port range",
524 "Client port range that can be used to receive RTP and RTCP data, "
525 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
526 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
529 * GstRTSPSrc:udp-buffer-size:
531 * Size of the kernel UDP receive buffer in bytes.
533 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
534 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
535 "Size of the kernel UDP receive buffer in bytes, 0=default",
536 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
537 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
540 * GstRTSPSrc:short-header:
542 * Only send the basic RTSP headers for broken encoders.
544 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
545 g_param_spec_boolean ("short-header", "Short Header",
546 "Only send the basic RTSP headers for broken encoders",
547 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
549 g_object_class_install_property (gobject_class, PROP_PROBATION,
550 g_param_spec_uint ("probation", "Number of probations",
551 "Consecutive packet sequence numbers to accept the source",
552 0, G_MAXUINT, DEFAULT_PROBATION,
553 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
555 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
556 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
557 "Reconnect to the server if RTSP connection is closed when doing UDP",
558 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
560 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
561 g_param_spec_string ("multicast-iface", "Multicast Interface",
562 "The network interface on which to join the multicast group",
563 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
565 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
566 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
567 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
568 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
570 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
571 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
572 "Use the pipeline running-time to set the NTP time in the RTCP SR messages",
573 DEFAULT_USE_PIPELINE_CLOCK,
574 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
576 g_object_class_install_property (gobject_class, PROP_SDES,
577 g_param_spec_boxed ("sdes", "SDES",
578 "The SDES items of this session",
579 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
582 * GstRTSPSrc::tls-validation-flags:
584 * TLS certificate validation flags used to validate server
589 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
590 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
591 "TLS certificate validation flags used to validate the server certificate",
592 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
593 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
596 * GstRTSPSrc::handle-request:
597 * @rtspsrc: a #GstRTSPSrc
598 * @request: a #GstRTSPMessage
599 * @response: a #GstRTSPMessage
601 * Handle a server request in @request and prepare @response.
603 * This signal is called from the streaming thread, you should therefore not
604 * do any state changes on @rtspsrc because this might deadlock. If you want
605 * to modify the state as a result of this signal, post a
606 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
611 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
612 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
613 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
614 G_TYPE_POINTER, G_TYPE_POINTER);
617 * GstRTSPSrc::on-sdp:
618 * @rtspsrc: a #GstRTSPSrc
619 * @sdp: a #GstSDPMessage
621 * Emited when the client has retrieved the SDP and before it configures the
622 * streams in the SDP. @sdp can be inspected and modified.
624 * This signal is called from the streaming thread, you should therefore not
625 * do any state changes on @rtspsrc because this might deadlock. If you want
626 * to modify the state as a result of this signal, post a
627 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
632 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
633 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
634 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
635 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
638 * GstRTSPSrc::select-stream:
639 * @rtspsrc: a #GstRTSPSrc
640 * @num: the stream number
641 * @caps: the stream caps
643 * Emited before the client decides to configure the stream @num with
646 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
651 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
652 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
653 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
654 (GCallback) default_select_stream, select_stream_accum, NULL,
655 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, G_TYPE_UINT,
658 gstelement_class->send_event = gst_rtspsrc_send_event;
659 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
660 gstelement_class->change_state = gst_rtspsrc_change_state;
662 gst_element_class_add_pad_template (gstelement_class,
663 gst_static_pad_template_get (&rtptemplate));
665 gst_element_class_set_static_metadata (gstelement_class,
666 "RTSP packet receiver", "Source/Network",
667 "Receive data over the network via RTSP (RFC 2326)",
668 "Wim Taymans <wim@fluendo.com>, "
669 "Thijs Vermeir <thijs.vermeir@barco.com>, "
670 "Lutz Mueller <lutz@topfrose.de>");
672 gstbin_class->handle_message = gst_rtspsrc_handle_message;
674 gst_rtsp_ext_list_init ();
678 gst_rtspsrc_init (GstRTSPSrc * src)
680 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
681 src->protocols = DEFAULT_PROTOCOLS;
682 src->debug = DEFAULT_DEBUG;
683 src->retry = DEFAULT_RETRY;
684 src->udp_timeout = DEFAULT_TIMEOUT;
685 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
686 src->latency = DEFAULT_LATENCY_MS;
687 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
688 src->do_retransmission = DEFAULT_DO_RETRANSMISSION;
689 src->connection_speed = DEFAULT_CONNECTION_SPEED;
690 src->nat_method = DEFAULT_NAT_METHOD;
691 src->do_rtcp = DEFAULT_DO_RTCP;
692 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
693 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
694 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
695 src->user_id = g_strdup (DEFAULT_USER_ID);
696 src->user_pw = g_strdup (DEFAULT_USER_PW);
697 src->buffer_mode = DEFAULT_BUFFER_MODE;
698 src->client_port_range.min = 0;
699 src->client_port_range.max = 0;
700 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
701 src->short_header = DEFAULT_SHORT_HEADER;
702 src->probation = DEFAULT_PROBATION;
703 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
704 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
705 src->ntp_sync = DEFAULT_NTP_SYNC;
706 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
708 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
710 /* get a list of all extensions */
711 src->extensions = gst_rtsp_ext_list_get ();
713 /* connect to send signal */
714 gst_rtsp_ext_list_connect (src->extensions, "send",
715 (GCallback) gst_rtspsrc_send_cb, src);
717 /* protects the streaming thread in interleaved mode or the polling
718 * thread in UDP mode. */
719 g_rec_mutex_init (&src->stream_rec_lock);
721 /* protects our state changes from multiple invocations */
722 g_rec_mutex_init (&src->state_rec_lock);
724 src->state = GST_RTSP_STATE_INVALID;
726 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
730 gst_rtspsrc_finalize (GObject * object)
734 rtspsrc = GST_RTSPSRC (object);
736 gst_rtsp_ext_list_free (rtspsrc->extensions);
737 g_free (rtspsrc->conninfo.location);
738 gst_rtsp_url_free (rtspsrc->conninfo.url);
739 g_free (rtspsrc->conninfo.url_str);
740 g_free (rtspsrc->user_id);
741 g_free (rtspsrc->user_pw);
742 g_free (rtspsrc->multi_iface);
745 gst_sdp_message_free (rtspsrc->sdp);
748 if (rtspsrc->provided_clock)
749 gst_object_unref (rtspsrc->provided_clock);
752 gst_structure_free (rtspsrc->sdes);
755 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
756 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
758 G_OBJECT_CLASS (parent_class)->finalize (object);
762 gst_rtspsrc_provide_clock (GstElement * element)
764 GstRTSPSrc *src = GST_RTSPSRC (element);
767 if ((clock = src->provided_clock) != NULL)
768 gst_object_ref (clock);
773 /* a proxy string of the format [user:passwd@]host[:port] */
775 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
779 g_free (rtsp->proxy_user);
780 rtsp->proxy_user = NULL;
781 g_free (rtsp->proxy_passwd);
782 rtsp->proxy_passwd = NULL;
783 g_free (rtsp->proxy_host);
784 rtsp->proxy_host = NULL;
785 rtsp->proxy_port = 0;
792 /* we allow http:// in front but ignore it */
793 if (g_str_has_prefix (p, "http://"))
796 at = strchr (p, '@');
798 /* look for user:passwd */
799 col = strchr (proxy, ':');
800 if (col == NULL || col > at)
803 rtsp->proxy_user = g_strndup (p, col - p);
805 rtsp->proxy_passwd = g_strndup (col, at - col);
810 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
811 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
812 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
813 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
814 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
815 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
816 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
819 col = strchr (p, ':');
822 /* everything before the colon is the hostname */
823 rtsp->proxy_host = g_strndup (p, col - p);
825 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
827 rtsp->proxy_host = g_strdup (p);
828 rtsp->proxy_port = 8080;
834 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
836 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
837 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
840 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
842 rtspsrc->ptcp_timeout = NULL;
846 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
851 rtspsrc = GST_RTSPSRC (object);
855 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
856 g_value_get_string (value), NULL);
859 rtspsrc->protocols = g_value_get_flags (value);
862 rtspsrc->debug = g_value_get_boolean (value);
865 rtspsrc->retry = g_value_get_uint (value);
868 rtspsrc->udp_timeout = g_value_get_uint64 (value);
870 case PROP_TCP_TIMEOUT:
871 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
874 rtspsrc->latency = g_value_get_uint (value);
876 case PROP_DROP_ON_LATENCY:
877 rtspsrc->drop_on_latency = g_value_get_boolean (value);
879 case PROP_DO_RETRANSMISSION:
880 rtspsrc->do_retransmission = g_value_get_boolean (value);
882 case PROP_CONNECTION_SPEED:
883 rtspsrc->connection_speed = g_value_get_uint64 (value);
885 case PROP_NAT_METHOD:
886 rtspsrc->nat_method = g_value_get_enum (value);
889 rtspsrc->do_rtcp = g_value_get_boolean (value);
891 case PROP_DO_RTSP_KEEP_ALIVE:
892 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
895 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
898 if (rtspsrc->prop_proxy_id)
899 g_free (rtspsrc->prop_proxy_id);
900 rtspsrc->prop_proxy_id = g_value_dup_string (value);
903 if (rtspsrc->prop_proxy_pw)
904 g_free (rtspsrc->prop_proxy_pw);
905 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
907 case PROP_RTP_BLOCKSIZE:
908 rtspsrc->rtp_blocksize = g_value_get_uint (value);
911 if (rtspsrc->user_id)
912 g_free (rtspsrc->user_id);
913 rtspsrc->user_id = g_value_dup_string (value);
916 if (rtspsrc->user_pw)
917 g_free (rtspsrc->user_pw);
918 rtspsrc->user_pw = g_value_dup_string (value);
920 case PROP_BUFFER_MODE:
921 rtspsrc->buffer_mode = g_value_get_enum (value);
923 case PROP_PORT_RANGE:
927 str = g_value_get_string (value);
929 sscanf (str, "%u-%u",
930 &rtspsrc->client_port_range.min, &rtspsrc->client_port_range.max);
932 rtspsrc->client_port_range.min = 0;
933 rtspsrc->client_port_range.max = 0;
937 case PROP_UDP_BUFFER_SIZE:
938 rtspsrc->udp_buffer_size = g_value_get_int (value);
940 case PROP_SHORT_HEADER:
941 rtspsrc->short_header = g_value_get_boolean (value);
944 rtspsrc->probation = g_value_get_uint (value);
946 case PROP_UDP_RECONNECT:
947 rtspsrc->udp_reconnect = g_value_get_boolean (value);
949 case PROP_MULTICAST_IFACE:
950 g_free (rtspsrc->multi_iface);
952 if (g_value_get_string (value) == NULL)
953 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
955 rtspsrc->multi_iface = g_value_dup_string (value);
958 rtspsrc->ntp_sync = g_value_get_boolean (value);
960 case PROP_USE_PIPELINE_CLOCK:
961 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
964 rtspsrc->sdes = g_value_dup_boxed (value);
966 case PROP_TLS_VALIDATION_FLAGS:
967 rtspsrc->tls_validation_flags = g_value_get_flags (value);
970 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
976 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
981 rtspsrc = GST_RTSPSRC (object);
985 g_value_set_string (value, rtspsrc->conninfo.location);
988 g_value_set_flags (value, rtspsrc->protocols);
991 g_value_set_boolean (value, rtspsrc->debug);
994 g_value_set_uint (value, rtspsrc->retry);
997 g_value_set_uint64 (value, rtspsrc->udp_timeout);
999 case PROP_TCP_TIMEOUT:
1003 timeout = rtspsrc->tcp_timeout.tv_sec * G_USEC_PER_SEC +
1004 rtspsrc->tcp_timeout.tv_usec;
1005 g_value_set_uint64 (value, timeout);
1009 g_value_set_uint (value, rtspsrc->latency);
1011 case PROP_DROP_ON_LATENCY:
1012 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1014 case PROP_DO_RETRANSMISSION:
1015 g_value_set_boolean (value, rtspsrc->do_retransmission);
1017 case PROP_CONNECTION_SPEED:
1018 g_value_set_uint64 (value, rtspsrc->connection_speed);
1020 case PROP_NAT_METHOD:
1021 g_value_set_enum (value, rtspsrc->nat_method);
1024 g_value_set_boolean (value, rtspsrc->do_rtcp);
1026 case PROP_DO_RTSP_KEEP_ALIVE:
1027 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1033 if (rtspsrc->proxy_host) {
1035 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1039 g_value_take_string (value, str);
1043 g_value_set_string (value, rtspsrc->prop_proxy_id);
1046 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1048 case PROP_RTP_BLOCKSIZE:
1049 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1052 g_value_set_string (value, rtspsrc->user_id);
1055 g_value_set_string (value, rtspsrc->user_pw);
1057 case PROP_BUFFER_MODE:
1058 g_value_set_enum (value, rtspsrc->buffer_mode);
1060 case PROP_PORT_RANGE:
1064 if (rtspsrc->client_port_range.min != 0) {
1065 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1066 rtspsrc->client_port_range.max);
1070 g_value_take_string (value, str);
1073 case PROP_UDP_BUFFER_SIZE:
1074 g_value_set_int (value, rtspsrc->udp_buffer_size);
1076 case PROP_SHORT_HEADER:
1077 g_value_set_boolean (value, rtspsrc->short_header);
1079 case PROP_PROBATION:
1080 g_value_set_uint (value, rtspsrc->probation);
1082 case PROP_UDP_RECONNECT:
1083 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1085 case PROP_MULTICAST_IFACE:
1086 g_value_set_string (value, rtspsrc->multi_iface);
1089 g_value_set_boolean (value, rtspsrc->ntp_sync);
1091 case PROP_USE_PIPELINE_CLOCK:
1092 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1095 g_value_set_boxed (value, rtspsrc->sdes);
1097 case PROP_TLS_VALIDATION_FLAGS:
1098 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1101 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1107 find_stream_by_id (GstRTSPStream * stream, gint * id)
1109 if (stream->id == *id)
1116 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1118 if (stream->channel[0] == *channel || stream->channel[1] == *channel)
1125 find_stream_by_pt (GstRTSPStream * stream, gint * pt)
1127 if (stream->pt == *pt)
1134 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1136 GstElement *src = (GstElement *) a;
1138 if (stream->udpsrc[0] == src)
1140 if (stream->udpsrc[1] == src)
1147 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1149 /* check qualified setup_url */
1150 if (!strcmp (stream->conninfo.location, (gchar *) a))
1152 /* check original control_url */
1153 if (!strcmp (stream->control_url, (gchar *) a))
1156 /* check if qualified setup_url ends with string */
1157 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1163 static GstRTSPStream *
1164 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1168 /* find and get stream */
1169 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1170 return (GstRTSPStream *) lstream->data;
1175 static const GstSDPBandwidth *
1176 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1177 const GstSDPMedia * media, const gchar * type)
1181 /* first look in the media specific section */
1182 len = gst_sdp_media_bandwidths_len (media);
1183 for (i = 0; i < len; i++) {
1184 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1186 if (strcmp (bw->bwtype, type) == 0)
1189 /* then look in the message specific section */
1190 len = gst_sdp_message_bandwidths_len (sdp);
1191 for (i = 0; i < len; i++) {
1192 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1194 if (strcmp (bw->bwtype, type) == 0)
1201 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1202 const GstSDPMedia * media, GstRTSPStream * stream)
1204 const GstSDPBandwidth *bw;
1206 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1207 stream->as_bandwidth = bw->bandwidth;
1209 stream->as_bandwidth = -1;
1211 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1212 stream->rr_bandwidth = bw->bandwidth;
1214 stream->rr_bandwidth = -1;
1216 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1217 stream->rs_bandwidth = bw->bandwidth;
1219 stream->rs_bandwidth = -1;
1223 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1224 const GstSDPConnection * conn)
1226 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1229 if (conn->addrtype == NULL)
1232 /* check for IPV6 */
1233 if (strcmp (conn->addrtype, "IP4") == 0)
1234 stream->is_ipv6 = FALSE;
1235 else if (strcmp (conn->addrtype, "IP6") == 0)
1236 stream->is_ipv6 = TRUE;
1241 g_free (stream->destination);
1242 stream->destination = g_strdup (conn->address);
1244 /* check for multicast */
1245 stream->is_multicast =
1246 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1248 stream->ttl = conn->ttl;
1251 /* Go over the connections for a stream.
1252 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1254 * - If we are dealing with a localhost address, we disable multicast
1257 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1258 const GstSDPMedia * media, GstRTSPStream * stream)
1260 const GstSDPConnection *conn;
1263 /* first look in the media specific section */
1264 len = gst_sdp_media_connections_len (media);
1265 for (i = 0; i < len; i++) {
1266 conn = gst_sdp_media_get_connection (media, i);
1268 gst_rtspsrc_do_stream_connection (src, stream, conn);
1270 /* then look in the message specific section */
1271 if ((conn = gst_sdp_message_get_connection (sdp))) {
1272 gst_rtspsrc_do_stream_connection (src, stream, conn);
1276 static const gchar *
1277 get_aggregate_control (GstRTSPSrc * src)
1282 base = src->control;
1283 else if (src->content_base)
1284 base = src->content_base;
1285 else if (src->conninfo.url_str)
1286 base = src->conninfo.url_str;
1293 static GstRTSPStream *
1294 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
1296 GstRTSPStream *stream;
1297 const gchar *control_url;
1298 const gchar *payload;
1299 const GstSDPMedia *media;
1301 /* get media, should not return NULL */
1302 media = gst_sdp_message_get_media (sdp, idx);
1306 stream = g_new0 (GstRTSPStream, 1);
1307 stream->parent = src;
1308 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1310 stream->last_ret = GST_FLOW_NOT_LINKED;
1311 stream->added = FALSE;
1312 stream->disabled = FALSE;
1313 stream->id = src->numstreams++;
1314 stream->eos = FALSE;
1315 stream->discont = TRUE;
1316 stream->seqbase = -1;
1317 stream->timebase = -1;
1319 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1320 * session manager to scale RTCP. */
1321 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1323 /* collect connection info */
1324 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1326 /* we must have a payload. No payload means we cannot create caps */
1327 /* FIXME, handle multiple formats. The problem here is that we just want to
1328 * take the first available format that we can handle but in order to do that
1329 * we need to scan for depayloader plugins. Scanning for payloader plugins is
1330 * also suboptimal because the user maybe just wants to save the raw stream
1331 * and then we don't care. */
1332 if ((payload = gst_sdp_media_get_format (media, 0))) {
1333 stream->pt = atoi (payload);
1335 stream->caps = gst_rtspsrc_media_to_caps (stream->pt, media);
1337 GST_DEBUG ("mapping sdp session level attributes to caps");
1338 gst_rtspsrc_sdp_attributes_to_caps (sdp->attributes, stream->caps);
1339 GST_DEBUG ("mapping sdp media level attributes to caps");
1340 gst_rtspsrc_sdp_attributes_to_caps (media->attributes, stream->caps);
1342 if (stream->pt >= 96) {
1343 /* If we have a dynamic payload type, see if we have a stream with the
1344 * same payload number. If there is one, they are part of the same
1345 * container and we only need to add one pad. */
1346 if (find_stream (src, &stream->pt, (gpointer) find_stream_by_pt)) {
1347 stream->container = TRUE;
1348 GST_DEBUG ("found another stream with pt %d, marking as container",
1353 /* collect port number */
1354 stream->port = gst_sdp_media_get_port (media);
1356 /* get control url to construct the setup url. The setup url is used to
1357 * configure the transport of the stream and is used to identity the stream in
1358 * the RTP-Info header field returned from PLAY. */
1359 control_url = gst_sdp_media_get_attribute_val (media, "control");
1360 if (control_url == NULL)
1361 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1363 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1364 GST_DEBUG_OBJECT (src, " pt: %d", stream->pt);
1365 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1366 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1367 GST_DEBUG_OBJECT (src, " caps: %" GST_PTR_FORMAT, stream->caps);
1368 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1370 if (control_url != NULL) {
1371 stream->control_url = g_strdup (control_url);
1372 /* Build a fully qualified url using the content_base if any or by prefixing
1373 * the original request.
1374 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1375 * likely build a URL that the server will fail to understand, this is ok,
1376 * we will fail then. */
1377 if (g_str_has_prefix (control_url, "rtsp://"))
1378 stream->conninfo.location = g_strdup (control_url);
1383 if (g_strcmp0 (control_url, "*") == 0)
1386 base = get_aggregate_control (src);
1388 /* check if the base ends or control starts with / */
1389 has_slash = g_str_has_prefix (control_url, "/");
1390 has_slash = has_slash || g_str_has_suffix (base, "/");
1392 /* concatenate the two strings, insert / when not present */
1393 stream->conninfo.location =
1394 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1397 GST_DEBUG_OBJECT (src, " setup: %s",
1398 GST_STR_NULL (stream->conninfo.location));
1400 /* we keep track of all streams */
1401 src->streams = g_list_append (src->streams, stream);
1409 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1413 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1416 gst_caps_unref (stream->caps);
1418 g_free (stream->destination);
1419 g_free (stream->control_url);
1420 g_free (stream->conninfo.location);
1422 for (i = 0; i < 2; i++) {
1423 if (stream->udpsrc[i]) {
1424 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1425 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1426 gst_object_unref (stream->udpsrc[i]);
1427 stream->udpsrc[i] = NULL;
1429 if (stream->channelpad[i]) {
1430 gst_object_unref (stream->channelpad[i]);
1431 stream->channelpad[i] = NULL;
1433 if (stream->udpsink[i]) {
1434 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1435 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1436 gst_object_unref (stream->udpsink[i]);
1437 stream->udpsink[i] = NULL;
1440 if (stream->fakesrc) {
1441 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1442 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1443 gst_object_unref (stream->fakesrc);
1444 stream->fakesrc = NULL;
1446 if (stream->srcpad) {
1447 gst_pad_set_active (stream->srcpad, FALSE);
1448 if (stream->added) {
1449 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1450 stream->added = FALSE;
1452 stream->srcpad = NULL;
1454 if (stream->rtcppad) {
1455 gst_object_unref (stream->rtcppad);
1456 stream->rtcppad = NULL;
1458 if (stream->session) {
1459 g_object_unref (stream->session);
1460 stream->session = NULL;
1466 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1470 GST_DEBUG_OBJECT (src, "cleanup");
1472 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1473 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1475 gst_rtspsrc_stream_free (src, stream);
1477 g_list_free (src->streams);
1478 src->streams = NULL;
1480 if (src->manager_sig_id) {
1481 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1482 src->manager_sig_id = 0;
1484 gst_element_set_state (src->manager, GST_STATE_NULL);
1485 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1486 src->manager = NULL;
1488 src->numstreams = 0;
1490 gst_structure_free (src->props);
1493 g_free (src->content_base);
1494 src->content_base = NULL;
1496 g_free (src->control);
1497 src->control = NULL;
1500 gst_rtsp_range_free (src->range);
1503 /* don't clear the SDP when it was used in the url */
1504 if (src->sdp && !src->from_sdp) {
1505 gst_sdp_message_free (src->sdp);
1508 if (src->start_segment) {
1509 gst_event_unref (src->start_segment);
1510 src->start_segment = NULL;
1512 if (src->provided_clock) {
1513 gst_object_unref (src->provided_clock);
1514 src->provided_clock = NULL;
1518 #define PARSE_INT(p, del, res) \
1521 p = strstr (p, del); \
1531 #define PARSE_STRING(p, del, res) \
1534 p = strstr (p, del); \
1546 #define SKIP_SPACES(p) \
1547 while (*p && g_ascii_isspace (*p)) \
1552 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1555 gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
1556 gint * rate, gchar ** params)
1560 p = (gchar *) rtpmap;
1562 PARSE_INT (p, " ", *payload);
1570 PARSE_STRING (p, "/", *name);
1571 if (*name == NULL) {
1572 GST_DEBUG ("no rate, name %s", p);
1573 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
1574 * streams seem to omit the rate. */
1581 p = strstr (p, "/");
1599 * Mapping SDP attributes to caps
1601 * prepend 'a-' to IANA registered sdp attributes names
1602 * (ie: not prefixed with 'x-') in order to avoid
1603 * collision with gstreamer standard caps properties names
1606 gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
1608 if (attributes->len > 0) {
1612 s = gst_caps_get_structure (caps, 0);
1614 for (i = 0; i < attributes->len; i++) {
1615 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
1616 gchar *tofree, *key;
1620 /* skip some of the attribute we already handle */
1621 if (!strcmp (key, "fmtp"))
1623 if (!strcmp (key, "rtpmap"))
1625 if (!strcmp (key, "control"))
1627 if (!strcmp (key, "range"))
1630 /* string must be valid UTF8 */
1631 if (!g_utf8_validate (attr->value, -1, NULL))
1634 if (!g_str_has_prefix (key, "x-"))
1635 tofree = key = g_strdup_printf ("a-%s", key);
1639 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
1640 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
1647 * Mapping of caps to and from SDP fields:
1649 * m=<media> <UDP port> RTP/AVP <payload>
1650 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1651 * a=fmtp:<payload> <param>[=<value>];...
1654 gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
1657 const gchar *rtpmap;
1661 gchar *params = NULL;
1667 /* get and parse rtpmap */
1668 if ((rtpmap = gst_sdp_media_get_attribute_val (media, "rtpmap"))) {
1669 ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
1671 if (payload != pt) {
1672 /* we ignore the rtpmap if the payload type is different. */
1673 g_warning ("rtpmap of wrong payload type, ignoring");
1679 /* if we failed to parse the rtpmap for a dynamic payload type, we have an
1683 /* else we can ignore */
1684 g_warning ("error parsing rtpmap, ignoring");
1687 /* dynamic payloads need rtpmap or we fail */
1691 /* check if we have a rate, if not, we need to look up the rate from the
1692 * default rates based on the payload types. */
1694 const GstRTPPayloadInfo *info;
1696 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
1697 /* dynamic types, use media and encoding_name */
1698 tmp = g_ascii_strdown (media->media, -1);
1699 info = gst_rtp_payload_info_for_name (tmp, name);
1702 /* static types, use payload type */
1703 info = gst_rtp_payload_info_for_pt (pt);
1707 if ((rate = info->clock_rate) == 0)
1710 /* we fail if we cannot find one */
1715 tmp = g_ascii_strdown (media->media, -1);
1716 caps = gst_caps_new_simple ("application/x-unknown",
1717 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
1719 s = gst_caps_get_structure (caps, 0);
1721 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
1723 /* encoding name must be upper case */
1725 tmp = g_ascii_strup (name, -1);
1726 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
1730 /* params must be lower case */
1731 if (params != NULL) {
1732 tmp = g_ascii_strdown (params, -1);
1733 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
1737 /* parse optional fmtp: field */
1738 if ((fmtp = gst_sdp_media_get_attribute_val (media, "fmtp"))) {
1744 /* p is now of the format <payload> <param>[=<value>];... */
1745 PARSE_INT (p, " ", payload);
1746 if (payload != -1 && payload == pt) {
1750 /* <param>[=<value>] are separated with ';' */
1751 pairs = g_strsplit (p, ";", 0);
1752 for (i = 0; pairs[i]; i++) {
1754 const gchar *val, *key;
1756 /* the key may not have a '=', the value can have other '='s */
1757 valpos = strstr (pairs[i], "=");
1759 /* we have a '=' and thus a value, remove the '=' with \0 */
1761 /* value is everything between '=' and ';'. We split the pairs at ;
1762 * boundaries so we can take the remainder of the value. Some servers
1763 * put spaces around the value which we strip off here. Alternatively
1764 * we could strip those spaces in the depayloaders should these spaces
1765 * actually carry any meaning in the future. */
1766 val = g_strstrip (valpos + 1);
1768 /* simple <param>;.. is translated into <param>=1;... */
1771 /* strip the key of spaces, convert key to lowercase but not the value. */
1772 key = g_strstrip (pairs[i]);
1773 if (strlen (key) > 1) {
1774 tmp = g_ascii_strdown (key, -1);
1775 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
1787 g_warning ("rtpmap type not given for dynamic payload %d", pt);
1792 g_warning ("rate unknown for payload type %d", pt);
1798 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
1799 gint * rtpport, gint * rtcpport)
1802 GstStateChangeReturn ret;
1803 GstElement *udpsrc0, *udpsrc1;
1804 gint tmp_rtp, tmp_rtcp;
1808 src = stream->parent;
1814 /* Start at next port */
1815 tmp_rtp = src->next_port_num;
1817 if (stream->is_ipv6)
1818 host = "udp://[::0]";
1820 host = "udp://0.0.0.0";
1822 /* try to allocate 2 UDP ports, the RTP port should be an even
1823 * number and the RTCP port should be the next (uneven) port */
1826 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
1827 tmp_rtp >= src->client_port_range.max)
1830 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1831 if (udpsrc0 == NULL)
1832 goto no_udp_protocol;
1833 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
1835 if (src->udp_buffer_size != 0)
1836 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
1839 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
1840 if (ret == GST_STATE_CHANGE_FAILURE) {
1842 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
1845 if (++count > src->retry)
1848 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1849 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1850 gst_object_unref (udpsrc0);
1853 GST_DEBUG_OBJECT (src, "retry %d", count);
1856 goto no_udp_protocol;
1859 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
1860 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
1862 /* check if port is even */
1863 if ((tmp_rtp & 0x01) != 0) {
1864 /* port not even, close and allocate another */
1865 if (++count > src->retry)
1868 GST_DEBUG_OBJECT (src, "RTP port not even");
1870 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1871 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1872 gst_object_unref (udpsrc0);
1875 GST_DEBUG_OBJECT (src, "retry %d", count);
1880 /* allocate port+1 for RTCP now */
1881 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1882 if (udpsrc1 == NULL)
1883 goto no_udp_rtcp_protocol;
1886 tmp_rtcp = tmp_rtp + 1;
1887 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
1890 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
1892 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
1893 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
1894 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
1895 if (ret == GST_STATE_CHANGE_FAILURE) {
1896 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
1898 if (++count > src->retry)
1901 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1902 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1903 gst_object_unref (udpsrc0);
1906 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
1907 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1908 gst_object_unref (udpsrc1);
1912 GST_DEBUG_OBJECT (src, "retry %d", count);
1916 /* all fine, do port check */
1917 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
1918 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
1920 /* this should not happen... */
1921 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
1924 /* we keep these elements, we configure all in configure_transport when the
1925 * server told us to really use the UDP ports. */
1926 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
1927 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
1928 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
1929 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
1931 /* keep track of next available port number when we have a range
1933 if (src->next_port_num != 0)
1934 src->next_port_num = tmp_rtcp + 1;
1941 GST_DEBUG_OBJECT (src, "could not get UDP source");
1946 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
1950 no_udp_rtcp_protocol:
1952 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
1957 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
1958 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
1964 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1965 gst_object_unref (udpsrc0);
1968 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1969 gst_object_unref (udpsrc1);
1976 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
1981 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
1983 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1984 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1987 for (i = 0; i < 2; i++) {
1988 if (stream->udpsrc[i])
1989 gst_element_set_state (stream->udpsrc[i], state);
1995 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
2002 event = gst_event_new_flush_start ();
2003 GST_DEBUG_OBJECT (src, "start flush");
2005 state = GST_STATE_PAUSED;
2007 event = gst_event_new_flush_stop (FALSE);
2008 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2011 state = GST_STATE_PLAYING;
2013 state = GST_STATE_PAUSED;
2015 gst_rtspsrc_push_event (src, event);
2016 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2017 gst_rtspsrc_set_state (src, state);
2020 static GstRTSPResult
2021 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
2022 GstRTSPMessage * message, GTimeVal * timeout)
2027 ret = gst_rtsp_connection_send (conn, message, timeout);
2029 ret = GST_RTSP_ERROR;
2034 static GstRTSPResult
2035 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
2036 GstRTSPMessage * message, GTimeVal * timeout)
2041 ret = gst_rtsp_connection_receive (conn, message, timeout);
2043 ret = GST_RTSP_ERROR;
2049 gst_rtspsrc_get_position (GstRTSPSrc * src)
2054 query = gst_query_new_position (GST_FORMAT_TIME);
2055 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2056 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2057 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2061 if (stream->srcpad) {
2062 if (gst_pad_query (stream->srcpad, query)) {
2063 gst_query_parse_position (query, &fmt, &pos);
2064 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2065 GST_TIME_ARGS (pos));
2066 src->last_pos = pos;
2076 gst_rtspsrc_do_seek (GstRTSPSrc * src, GstSegment * segment)
2078 src->state = GST_RTSP_STATE_SEEKING;
2079 /* PLAY will add the range header now. */
2080 src->need_range = TRUE;
2086 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2091 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2093 gboolean flush, skip;
2096 GstSegment seeksegment = { 0, };
2100 GST_DEBUG_OBJECT (src, "doing seek with event");
2102 gst_event_parse_seek (event, &rate, &format, &flags,
2103 &cur_type, &cur, &stop_type, &stop);
2105 /* no negative rates yet */
2109 /* we need TIME format */
2110 if (format != src->segment.format)
2113 GST_DEBUG_OBJECT (src, "doing seek without event");
2115 cur_type = GST_SEEK_TYPE_SET;
2116 stop_type = GST_SEEK_TYPE_SET;
2119 /* get flush flag */
2120 flush = flags & GST_SEEK_FLAG_FLUSH;
2121 skip = flags & GST_SEEK_FLAG_SKIP;
2123 /* now we need to make sure the streaming thread is stopped. We do this by
2124 * either sending a FLUSH_START event downstream which will cause the
2125 * streaming thread to stop with a WRONG_STATE.
2126 * For a non-flushing seek we simply pause the task, which will happen as soon
2127 * as it completes one iteration (and thus might block when the sink is
2128 * blocking in preroll). */
2130 GST_DEBUG_OBJECT (src, "starting flush");
2131 gst_rtspsrc_flush (src, TRUE, FALSE);
2134 gst_task_pause (src->task);
2138 /* we should now be able to grab the streaming thread because we stopped it
2139 * with the above flush/pause code */
2140 GST_RTSP_STREAM_LOCK (src);
2142 GST_DEBUG_OBJECT (src, "stopped streaming");
2144 /* copy segment, we need this because we still need the old
2145 * segment when we close the current segment. */
2146 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2148 /* configure the seek parameters in the seeksegment. We will then have the
2149 * right values in the segment to perform the seek */
2151 GST_DEBUG_OBJECT (src, "configuring seek");
2152 gst_segment_do_seek (&seeksegment, rate, format, flags,
2153 cur_type, cur, stop_type, stop, &update);
2156 /* figure out the last position we need to play. If it's configured (stop !=
2157 * -1), use that, else we play until the total duration of the file */
2158 if ((stop = seeksegment.stop) == -1)
2159 stop = seeksegment.duration;
2161 playing = (src->state == GST_RTSP_STATE_PLAYING);
2163 /* if we were playing, pause first */
2165 /* obtain current position in case seek fails */
2166 gst_rtspsrc_get_position (src);
2167 gst_rtspsrc_pause (src, FALSE);
2171 gst_rtspsrc_do_seek (src, &seeksegment);
2173 /* and continue playing */
2175 gst_rtspsrc_play (src, &seeksegment, FALSE);
2177 /* prepare for streaming again */
2179 /* if we started flush, we stop now */
2180 GST_DEBUG_OBJECT (src, "stopping flush");
2181 gst_rtspsrc_flush (src, FALSE, playing);
2184 /* now we did the seek and can activate the new segment values */
2185 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2187 /* if we're doing a segment seek, post a SEGMENT_START message */
2188 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2189 gst_element_post_message (GST_ELEMENT_CAST (src),
2190 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2191 src->segment.format, src->segment.position));
2194 /* now create the newsegment */
2195 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2196 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2199 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2200 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2201 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2202 stream->discont = TRUE;
2205 GST_RTSP_STREAM_UNLOCK (src);
2212 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2217 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2223 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2227 gboolean res = TRUE;
2230 src = GST_RTSPSRC_CAST (parent);
2232 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2233 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2235 switch (GST_EVENT_TYPE (event)) {
2236 case GST_EVENT_SEEK:
2237 res = gst_rtspsrc_perform_seek (src, event);
2241 case GST_EVENT_NAVIGATION:
2242 case GST_EVENT_LATENCY:
2250 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2251 res = gst_pad_send_event (target, event);
2252 gst_object_unref (target);
2254 gst_event_unref (event);
2257 gst_event_unref (event);
2263 /* this is the final event function we receive on the internal source pad when
2264 * we deal with TCP connections */
2266 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2271 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2273 switch (GST_EVENT_TYPE (event)) {
2274 case GST_EVENT_SEEK:
2276 case GST_EVENT_NAVIGATION:
2277 case GST_EVENT_LATENCY:
2279 gst_event_unref (event);
2286 /* this is the final query function we receive on the internal source pad when
2287 * we deal with TCP connections */
2289 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2293 gboolean res = TRUE;
2295 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2297 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2298 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2300 switch (GST_QUERY_TYPE (query)) {
2301 case GST_QUERY_POSITION:
2306 case GST_QUERY_DURATION:
2310 gst_query_parse_duration (query, &format, NULL);
2313 case GST_FORMAT_TIME:
2314 gst_query_set_duration (query, format, src->segment.duration);
2322 case GST_QUERY_LATENCY:
2324 /* we are live with a min latency of 0 and unlimited max latency, this
2325 * result will be updated by the session manager if there is any. */
2326 gst_query_set_latency (query, TRUE, 0, -1);
2336 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2338 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2342 gboolean res = FALSE;
2344 src = GST_RTSPSRC_CAST (parent);
2346 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2347 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2349 switch (GST_QUERY_TYPE (query)) {
2350 case GST_QUERY_DURATION:
2354 gst_query_parse_duration (query, &format, NULL);
2357 case GST_FORMAT_TIME:
2358 gst_query_set_duration (query, format, src->segment.duration);
2366 case GST_QUERY_SEEKING:
2370 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2371 if (format == GST_FORMAT_TIME) {
2373 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2375 /* seeking without duration is unlikely */
2376 seekable = seekable && src->seekable && src->segment.duration &&
2377 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2379 /* FIXME ?? should we have 0 and segment.duration here; see demuxers */
2380 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
2381 src->segment.start, src->segment.stop);
2390 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2392 gst_query_set_uri (query, uri);
2400 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2402 /* forward the query to the proxy target pad */
2404 res = gst_pad_query (target, query);
2405 gst_object_unref (target);
2414 /* callback for RTCP messages to be sent to the server when operating in TCP
2416 static GstFlowReturn
2417 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2420 GstRTSPStream *stream;
2421 GstFlowReturn res = GST_FLOW_OK;
2426 GstRTSPMessage message = { 0 };
2427 GstRTSPConnection *conn;
2429 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2430 src = stream->parent;
2432 gst_buffer_map (buffer, &map, GST_MAP_READ);
2436 gst_rtsp_message_init_data (&message, stream->channel[1]);
2438 /* lend the body data to the message */
2439 gst_rtsp_message_take_body (&message, data, size);
2441 if (stream->conninfo.connection)
2442 conn = stream->conninfo.connection;
2444 conn = src->conninfo.connection;
2446 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2447 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2448 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2450 /* and steal it away again because we will free it when unreffing the
2452 gst_rtsp_message_steal_body (&message, &data, &size);
2453 gst_rtsp_message_unset (&message);
2455 gst_buffer_unmap (buffer, &map);
2456 gst_buffer_unref (buffer);
2461 static GstPadProbeReturn
2462 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2464 GstRTSPSrc *src = user_data;
2466 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2467 GST_DEBUG_PAD_NAME (pad));
2469 /* activate the streams */
2470 GST_OBJECT_LOCK (src);
2471 if (!src->need_activate)
2474 src->need_activate = FALSE;
2475 GST_OBJECT_UNLOCK (src);
2477 gst_rtspsrc_activate_streams (src);
2479 return GST_PAD_PROBE_OK;
2483 GST_OBJECT_UNLOCK (src);
2484 return GST_PAD_PROBE_OK;
2488 /* this callback is called when the session manager generated a new src pad with
2489 * payloaded RTP packets. We simply ghost the pad here. */
2491 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2494 GstPadTemplate *template;
2497 GstRTSPStream *stream;
2500 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2502 GST_RTSP_STATE_LOCK (src);
2504 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2505 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2506 goto unknown_stream;
2508 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2510 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2512 goto unknown_stream;
2515 stream->ssrc = ssrc;
2517 /* we'll add it later see below */
2518 stream->added = TRUE;
2520 /* check if we added all streams */
2522 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
2523 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
2525 GST_DEBUG_OBJECT (src, "stream %p, container %d, disabled %d, added %d",
2526 ostream, ostream->container, ostream->disabled, ostream->added);
2528 /* a container stream only needs one pad added. Also disabled streams don't
2530 if (!ostream->container && !ostream->disabled && !ostream->added) {
2535 GST_RTSP_STATE_UNLOCK (src);
2537 /* create a new pad we will use to stream to */
2538 template = gst_static_pad_template_get (&rtptemplate);
2539 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2540 gst_object_unref (template);
2543 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2544 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2545 gst_pad_set_active (stream->srcpad, TRUE);
2546 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2549 GST_DEBUG_OBJECT (src, "We added all streams");
2550 /* when we get here, all stream are added and we can fire the no-more-pads
2552 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2560 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2561 GST_RTSP_STATE_UNLOCK (src);
2568 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2570 GstRTSPStream *stream;
2573 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2575 GST_RTSP_STATE_LOCK (src);
2576 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2578 goto unknown_stream;
2580 caps = stream->caps;
2582 gst_caps_ref (caps);
2583 GST_RTSP_STATE_UNLOCK (src);
2589 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2590 GST_RTSP_STATE_UNLOCK (src);
2596 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2598 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2604 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2610 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2616 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2618 GstRTSPSrc *src = stream->parent;
2621 g_object_get (source, "ssrc", &ssrc, NULL);
2623 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
2624 ssrc, stream->ssrc, stream->id);
2626 if (ssrc == stream->ssrc)
2627 gst_rtspsrc_do_stream_eos (src, stream);
2631 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2633 GstRTSPSrc *src = stream->parent;
2636 g_object_get (source, "ssrc", &ssrc, NULL);
2638 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
2639 ssrc, stream->ssrc, stream->id);
2641 if (ssrc == stream->ssrc)
2642 gst_rtspsrc_do_stream_eos (src, stream);
2646 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2648 GstRTSPStream *stream;
2650 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2652 /* get stream for session */
2653 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2655 gst_rtspsrc_do_stream_eos (src, stream);
2660 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2662 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2667 set_manager_buffer_mode (GstRTSPSrc * src)
2669 GObjectClass *klass;
2671 if (src->manager == NULL)
2674 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2676 if (!g_object_class_find_property (klass, "buffer-mode"))
2679 if (src->buffer_mode != BUFFER_MODE_AUTO) {
2680 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
2685 GST_DEBUG_OBJECT (src,
2686 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
2688 if (src->provided_clock) {
2689 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
2691 if (clock == src->provided_clock) {
2692 GST_DEBUG_OBJECT (src, "selected synced");
2693 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
2696 gst_object_unref (clock);
2701 /* Otherwise fall-through and use another buffer mode */
2703 gst_object_unref (clock);
2706 GST_DEBUG_OBJECT (src, "auto buffering mode");
2707 if (src->use_buffering) {
2708 GST_DEBUG_OBJECT (src, "selected buffer");
2709 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
2711 GST_DEBUG_OBJECT (src, "selected slave");
2712 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
2716 /* try to get and configure a manager */
2718 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
2719 GstRTSPTransport * transport)
2721 const gchar *manager;
2723 GstStateChangeReturn ret;
2725 /* find a manager */
2726 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
2730 GST_DEBUG_OBJECT (src, "using manager %s", manager);
2732 /* configure the manager */
2733 if (src->manager == NULL) {
2734 GObjectClass *klass;
2736 const gchar *encoding;
2737 gboolean need_slave;
2739 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
2741 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
2745 goto use_no_manager;
2747 if (!(src->manager = gst_element_factory_make (manager, "manager")))
2748 goto manager_failed;
2751 /* we manage this element */
2752 gst_element_set_locked_state (src->manager, TRUE);
2753 gst_bin_add (GST_BIN_CAST (src), src->manager);
2755 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
2756 if (ret == GST_STATE_CHANGE_FAILURE)
2757 goto start_manager_failure;
2759 g_object_set (src->manager, "latency", src->latency, NULL);
2761 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2763 if (g_object_class_find_property (klass, "ntp-sync")) {
2764 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
2767 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
2768 g_object_set (src->manager, "use-pipeline-clock",
2769 src->use_pipeline_clock, NULL);
2772 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
2773 g_object_set (src->manager, "sdes", src->sdes, NULL);
2776 if (g_object_class_find_property (klass, "drop-on-latency")) {
2777 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
2781 if (g_object_class_find_property (klass, "do-retransmission")) {
2782 g_object_set (src->manager, "do-retransmission", src->do_retransmission,
2786 /* buffer mode pauses are handled by adding offsets to buffer times,
2787 * but some depayloaders may have a hard time syncing output times
2788 * with such input times, e.g. container ones, most notably ASF */
2789 /* TODO alternatives are having an event that indicates these shifts,
2790 * or having rtsp extensions provide suggestion on buffer mode */
2791 need_slave = stream->container;
2792 if (stream->caps && (s = gst_caps_get_structure (stream->caps, 0)) &&
2793 (encoding = gst_structure_get_string (s, "encoding-name")))
2794 need_slave = need_slave || (strcmp (encoding, "X-ASF-PF") == 0);
2795 /* valid duration implies not likely live pipeline,
2796 * so slaving in jitterbuffer does not make much sense
2797 * (and might mess things up due to bursts) */
2798 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
2799 src->segment.duration && !need_slave) {
2800 src->use_buffering = TRUE;
2802 src->use_buffering = FALSE;
2805 set_manager_buffer_mode (src);
2807 /* connect to signals if we did not already do so */
2808 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
2810 src->manager_sig_id =
2811 g_signal_connect (src->manager, "pad-added",
2812 (GCallback) new_manager_pad, src);
2813 src->manager_ptmap_id =
2814 g_signal_connect (src->manager, "request-pt-map",
2815 (GCallback) request_pt_map, src);
2817 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
2821 /* we stream directly to the manager, get some pads. Each RTSP stream goes
2822 * into a separate RTP session. */
2823 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
2824 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
2826 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
2827 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
2830 /* now configure the bandwidth in the manager */
2831 if (g_signal_lookup ("get-internal-session",
2832 G_OBJECT_TYPE (src->manager)) != 0) {
2833 GObject *rtpsession;
2835 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
2838 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
2840 stream->session = rtpsession;
2842 if (stream->as_bandwidth != -1) {
2843 GST_INFO_OBJECT (src, "setting AS: %f",
2844 (gdouble) (stream->as_bandwidth * 1000));
2845 g_object_set (rtpsession, "bandwidth",
2846 (gdouble) (stream->as_bandwidth * 1000), NULL);
2848 if (stream->rr_bandwidth != -1) {
2849 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
2850 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
2853 if (stream->rs_bandwidth != -1) {
2854 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
2855 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
2859 g_object_set (rtpsession, "probation", src->probation, NULL);
2861 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2863 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
2865 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
2867 g_signal_connect (rtpsession, "on-ssrc-active",
2868 (GCallback) on_ssrc_active, stream);
2879 GST_DEBUG_OBJECT (src, "cannot get a session manager");
2884 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
2887 start_manager_failure:
2889 GST_DEBUG_OBJECT (src, "could not start session manager");
2894 /* free the UDP sources allocated when negotiating a transport.
2895 * This function is called when the server negotiated to a transport where the
2896 * UDP sources are not needed anymore, such as TCP or multicast. */
2898 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
2902 for (i = 0; i < 2; i++) {
2903 if (stream->udpsrc[i]) {
2904 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
2905 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
2906 gst_object_unref (stream->udpsrc[i]);
2907 stream->udpsrc[i] = NULL;
2912 /* for TCP, create pads to send and receive data to and from the manager and to
2913 * intercept various events and queries
2916 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
2917 GstRTSPTransport * transport, GstPad ** outpad)
2920 GstPadTemplate *template;
2921 GstPad *pad0, *pad1;
2923 /* configure for interleaved delivery, nothing needs to be done
2924 * here, the loop function will call the chain functions of the
2925 * session manager. */
2926 stream->channel[0] = transport->interleaved.min;
2927 stream->channel[1] = transport->interleaved.max;
2928 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
2929 stream->channel[0], stream->channel[1]);
2931 /* we can remove the allocated UDP ports now */
2932 gst_rtspsrc_stream_free_udp (stream);
2934 /* no session manager, send data to srcpad directly */
2935 if (!stream->channelpad[0]) {
2936 GST_DEBUG_OBJECT (src, "no manager, creating pad");
2938 /* create a new pad we will use to stream to */
2939 name = g_strdup_printf ("stream_%u", stream->id);
2940 template = gst_static_pad_template_get (&rtptemplate);
2941 stream->channelpad[0] = gst_pad_new_from_template (template, name);
2942 gst_object_unref (template);
2945 /* set caps and activate */
2946 gst_pad_use_fixed_caps (stream->channelpad[0]);
2947 gst_pad_set_active (stream->channelpad[0], TRUE);
2949 *outpad = gst_object_ref (stream->channelpad[0]);
2951 GST_DEBUG_OBJECT (src, "using manager source pad");
2953 template = gst_static_pad_template_get (&anysrctemplate);
2955 /* allocate pads for sending the channel data into the manager */
2956 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
2957 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
2958 gst_object_unref (stream->channelpad[0]);
2959 stream->channelpad[0] = pad0;
2960 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
2961 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
2962 gst_pad_set_element_private (pad0, src);
2963 gst_pad_set_active (pad0, TRUE);
2965 if (stream->channelpad[1]) {
2966 /* if we have a sinkpad for the other channel, create a pad and link to the
2968 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
2969 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
2970 gst_pad_link_full (pad1, stream->channelpad[1],
2971 GST_PAD_LINK_CHECK_NOTHING);
2972 gst_object_unref (stream->channelpad[1]);
2973 stream->channelpad[1] = pad1;
2974 gst_pad_set_active (pad1, TRUE);
2976 gst_object_unref (template);
2978 /* setup RTCP transport back to the server if we have to. */
2979 if (src->manager && src->do_rtcp) {
2982 template = gst_static_pad_template_get (&anysinktemplate);
2984 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
2985 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
2986 gst_pad_set_element_private (stream->rtcppad, stream);
2987 gst_pad_set_active (stream->rtcppad, TRUE);
2989 /* get session RTCP pad */
2990 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
2991 pad = gst_element_get_request_pad (src->manager, name);
2996 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
2997 gst_object_unref (pad);
3000 gst_object_unref (template);
3006 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
3007 GstRTSPTransport * transport, const gchar ** destination, gint * min,
3008 gint * max, guint * ttl)
3010 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3012 if (!(*destination = transport->destination))
3013 *destination = stream->destination;
3016 /* transport first */
3017 *min = transport->port.min;
3018 *max = transport->port.max;
3019 if (*min == -1 && *max == -1) {
3020 /* then try from SDP */
3021 if (stream->port != 0) {
3022 *min = stream->port;
3023 *max = stream->port + 1;
3029 if (!(*ttl = transport->ttl))
3034 /* first take the source, then the endpoint to figure out where to send
3036 if (!(*destination = transport->source)) {
3037 if (src->conninfo.connection)
3038 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3039 else if (stream->conninfo.connection)
3041 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3045 /* for unicast we only expect the ports here */
3046 *min = transport->server_port.min;
3047 *max = transport->server_port.max;
3052 /* For multicast create UDP sources and join the multicast group. */
3054 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3055 GstRTSPTransport * transport, GstPad ** outpad)
3058 const gchar *destination;
3061 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3063 /* we can remove the allocated UDP ports now */
3064 gst_rtspsrc_stream_free_udp (stream);
3066 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3069 /* we need a destination now */
3070 if (destination == NULL)
3071 goto no_destination;
3073 /* we really need ports now or we won't be able to receive anything at all */
3074 if (min == -1 && max == -1)
3077 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3078 destination, min, max);
3080 /* creating UDP source for RTP */
3082 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3084 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3086 if (stream->udpsrc[0] == NULL)
3089 /* take ownership */
3090 gst_object_ref_sink (stream->udpsrc[0]);
3092 if (src->udp_buffer_size != 0)
3093 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3094 src->udp_buffer_size, NULL);
3096 if (src->multi_iface != NULL)
3097 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3098 src->multi_iface, NULL);
3101 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3102 gst_element_set_state (stream->udpsrc[0], GST_STATE_PAUSED);
3105 /* creating another UDP source for RTCP */
3109 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3111 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3113 if (stream->udpsrc[1] == NULL)
3116 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3117 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3118 gst_caps_unref (caps);
3120 /* take ownership */
3121 gst_object_ref_sink (stream->udpsrc[1]);
3123 if (src->multi_iface != NULL)
3124 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3125 src->multi_iface, NULL);
3127 gst_element_set_state (stream->udpsrc[1], GST_STATE_PAUSED);
3134 GST_DEBUG_OBJECT (src, "no UDP source element found");
3139 GST_DEBUG_OBJECT (src, "no destination found");
3144 GST_DEBUG_OBJECT (src, "no ports found");
3149 /* configure the remainder of the UDP ports */
3151 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3152 GstRTSPTransport * transport, GstPad ** outpad)
3154 /* we manage the UDP elements now. For unicast, the UDP sources where
3155 * allocated in the stream when we suggested a transport. */
3156 if (stream->udpsrc[0]) {
3157 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3158 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3160 GST_DEBUG_OBJECT (src, "setting up UDP source");
3162 /* configure a timeout on the UDP port. When the timeout message is
3163 * posted, we assume UDP transport is not possible. We reconnect using TCP
3165 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3166 src->udp_timeout * 1000, NULL);
3168 /* get output pad of the UDP source. */
3169 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3171 /* save it so we can unblock */
3172 stream->blockedpad = *outpad;
3174 /* configure pad block on the pad. As soon as there is dataflow on the
3175 * UDP source, we know that UDP is not blocked by a firewall and we can
3176 * configure all the streams to let the application autoplug decoders. */
3178 gst_pad_add_probe (stream->blockedpad,
3179 GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM, pad_blocked, src, NULL);
3181 if (stream->channelpad[0]) {
3182 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3183 /* configure for UDP delivery, we need to connect the UDP pads to
3184 * the session plugin. */
3185 gst_pad_link_full (*outpad, stream->channelpad[0],
3186 GST_PAD_LINK_CHECK_NOTHING);
3187 gst_object_unref (*outpad);
3189 /* we connected to pad-added signal to get pads from the manager */
3191 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3196 if (stream->udpsrc[1]) {
3199 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3200 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3202 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3203 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3204 gst_caps_unref (caps);
3206 if (stream->channelpad[1]) {
3209 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3211 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3212 gst_pad_link_full (pad, stream->channelpad[1],
3213 GST_PAD_LINK_CHECK_NOTHING);
3214 gst_object_unref (pad);
3216 /* leave unlinked */
3222 /* configure the UDP sink back to the server for status reports */
3224 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3225 GstRTSPStream * stream, GstRTSPTransport * transport)
3228 gint rtp_port, rtcp_port;
3229 gboolean do_rtp, do_rtcp;
3230 const gchar *destination;
3235 /* get transport info */
3236 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
3237 &rtp_port, &rtcp_port, &ttl);
3239 /* see what we need to do */
3240 do_rtp = (rtp_port != -1);
3241 /* it's possible that the server does not want us to send RTCP in which case
3243 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
3245 /* we need a destination when we have RTP or RTCP ports */
3246 if (destination == NULL && (do_rtp || do_rtcp))
3247 goto no_destination;
3249 /* try to construct the fakesrc to the RTP port of the server to open up any
3252 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
3255 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
3256 stream->udpsink[0] =
3257 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3259 if (stream->udpsink[0] == NULL)
3260 goto no_sink_element;
3262 /* don't join multicast group, we will have the source socket do that */
3263 /* no sync or async state changes needed */
3264 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
3265 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3267 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3269 if (stream->udpsrc[0]) {
3270 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
3271 * so that NAT firewalls will open a hole for us */
3272 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
3273 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
3274 /* configure socket and make sure udpsink does not close it when shutting
3275 * down, it belongs to udpsrc after all. */
3276 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
3277 "close-socket", FALSE, NULL);
3278 g_object_unref (socket);
3281 /* the source for the dummy packets to open up NAT */
3282 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
3283 if (stream->fakesrc == NULL)
3284 goto no_fakesrc_element;
3286 /* random data in 5 buffers, a size of 200 bytes should be fine */
3287 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
3288 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
3290 /* we don't want to consider this a sink */
3291 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
3293 /* keep everything locked */
3294 gst_element_set_locked_state (stream->udpsink[0], TRUE);
3295 gst_element_set_locked_state (stream->fakesrc, TRUE);
3297 gst_object_ref (stream->udpsink[0]);
3298 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
3299 gst_object_ref (stream->fakesrc);
3300 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
3302 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
3303 "sink", GST_PAD_LINK_CHECK_NOTHING);
3306 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
3309 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
3310 stream->udpsink[1] =
3311 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3313 if (stream->udpsink[1] == NULL)
3314 goto no_sink_element;
3316 /* don't join multicast group, we will have the source socket do that */
3317 /* no sync or async state changes needed */
3318 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
3319 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3321 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3323 if (stream->udpsrc[1]) {
3324 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
3325 * because some servers check the port number of where it sends RTCP to identify
3326 * the RTCP packets it receives */
3327 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
3328 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
3329 /* configure socket and make sure udpsink does not close it when shutting
3330 * down, it belongs to udpsrc after all. */
3331 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
3332 "close-socket", FALSE, NULL);
3333 g_object_unref (socket);
3336 /* we don't want to consider this a sink */
3337 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
3339 /* we keep this playing always */
3340 gst_element_set_locked_state (stream->udpsink[1], TRUE);
3341 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
3343 gst_object_ref (stream->udpsink[1]);
3344 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
3346 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
3348 /* get session RTCP pad */
3349 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3350 pad = gst_element_get_request_pad (src->manager, name);
3355 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3356 gst_object_unref (pad);
3365 GST_DEBUG_OBJECT (src, "no destination address specified");
3370 GST_DEBUG_OBJECT (src, "no UDP sink element found");
3375 GST_DEBUG_OBJECT (src, "no fakesrc element found");
3380 /* sets up all elements needed for streaming over the specified transport.
3381 * Does not yet expose the element pads, this will be done when there is actuall
3382 * dataflow detected, which might never happen when UDP is blocked in a
3383 * firewall, for example.
3386 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
3387 GstRTSPTransport * transport)
3390 GstPad *outpad = NULL;
3391 GstPadTemplate *template;
3394 const gchar *media_type;
3396 src = stream->parent;
3398 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
3400 s = gst_caps_get_structure (stream->caps, 0);
3402 /* get the proper media type for this stream now */
3403 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
3404 goto unknown_transport;
3406 goto unknown_transport;
3408 /* configure the final media type */
3409 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
3410 gst_structure_set_name (s, media_type);
3412 /* try to get and configure a manager, channelpad[0-1] will be configured with
3413 * the pads for the manager, or NULL when no manager is needed. */
3414 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
3417 switch (transport->lower_transport) {
3418 case GST_RTSP_LOWER_TRANS_TCP:
3419 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
3420 goto transport_failed;
3422 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3423 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
3424 goto transport_failed;
3425 /* fallthrough, the rest is the same for UDP and MCAST */
3426 case GST_RTSP_LOWER_TRANS_UDP:
3427 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
3428 goto transport_failed;
3429 /* configure udpsinks back to the server for RTCP messages and for the
3430 * dummy RTP messages to open NAT. */
3431 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
3432 goto transport_failed;
3435 goto unknown_transport;
3439 GST_DEBUG_OBJECT (src, "creating ghostpad");
3441 gst_pad_use_fixed_caps (outpad);
3443 /* create ghostpad, don't add just yet, this will be done when we activate
3445 name = g_strdup_printf ("stream_%u", stream->id);
3446 template = gst_static_pad_template_get (&rtptemplate);
3447 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
3448 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3449 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3450 gst_object_unref (template);
3453 gst_object_unref (outpad);
3455 /* mark pad as ok */
3456 stream->last_ret = GST_FLOW_OK;
3463 GST_DEBUG_OBJECT (src, "failed to configure transport");
3468 GST_DEBUG_OBJECT (src, "unknown transport");
3473 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3478 /* send a couple of dummy random packets on the receiver RTP port to the server,
3479 * this should make a firewall think we initiated the data transfer and
3480 * hopefully allow packets to go from the sender port to our RTP receiver port */
3482 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
3486 if (src->nat_method != GST_RTSP_NAT_DUMMY)
3489 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3490 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3492 if (stream->fakesrc && stream->udpsink[0]) {
3493 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
3494 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
3495 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
3496 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
3497 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
3503 /* Adds the source pads of all configured streams to the element.
3504 * This code is performed when we detected dataflow.
3506 * We detect dataflow from either the _loop function or with pad probes on the
3510 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
3514 GST_DEBUG_OBJECT (src, "activating streams");
3516 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3517 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3519 if (stream->udpsrc[0]) {
3520 /* remove timeout, we are streaming now and timeouts will be handled by
3521 * the session manager and jitter buffer */
3522 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
3524 if (stream->srcpad) {
3525 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
3526 gst_pad_set_active (stream->srcpad, TRUE);
3528 /* if we don't have a session manager, set the caps now. If we have a
3529 * session, we will get a notification of the pad and the caps. */
3530 if (!src->manager) {
3531 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
3532 gst_pad_set_caps (stream->srcpad, stream->caps);
3535 if (!stream->added) {
3536 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
3537 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3538 stream->added = TRUE;
3543 /* unblock all pads */
3544 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3545 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3547 if (stream->blockid) {
3548 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
3549 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
3550 stream->blockid = 0;
3558 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
3559 gboolean reset_manager)
3562 guint64 start, stop;
3563 gdouble play_speed, play_scale;
3565 GST_DEBUG_OBJECT (src, "configuring stream caps");
3567 start = segment->position;
3568 stop = segment->duration;
3569 play_speed = segment->rate;
3570 play_scale = segment->applied_rate;
3572 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3573 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3576 if ((caps = stream->caps)) {
3577 caps = gst_caps_make_writable (caps);
3579 if (stream->timebase != -1)
3580 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
3581 (guint) stream->timebase, NULL);
3582 if (stream->seqbase != -1)
3583 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
3584 (guint) stream->seqbase, NULL);
3585 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
3587 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
3588 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
3589 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
3591 stream->caps = caps;
3593 GST_DEBUG_OBJECT (src, "stream %p, caps %" GST_PTR_FORMAT, stream, caps);
3595 if (reset_manager && src->manager) {
3596 GST_DEBUG_OBJECT (src, "clear session");
3597 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
3601 static GstFlowReturn
3602 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
3607 /* store the value */
3608 stream->last_ret = ret;
3610 /* if it's success we can return the value right away */
3611 if (ret == GST_FLOW_OK)
3614 /* any other error that is not-linked can be returned right
3616 if (ret != GST_FLOW_NOT_LINKED)
3619 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3620 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3621 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3623 ret = ostream->last_ret;
3624 /* some other return value (must be SUCCESS but we can return
3625 * other values as well) */
3626 if (ret != GST_FLOW_NOT_LINKED)
3629 /* if we get here, all other pads were unlinked and we return
3630 * NOT_LINKED then */
3636 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
3639 gboolean res = TRUE;
3641 /* only streams that have a connection to the outside world */
3642 if (stream->container || stream->disabled)
3645 if (stream->udpsrc[0]) {
3646 gst_event_ref (event);
3647 res = gst_element_send_event (stream->udpsrc[0], event);
3648 } else if (stream->channelpad[0]) {
3649 gst_event_ref (event);
3650 if (GST_PAD_IS_SRC (stream->channelpad[0]))
3651 res = gst_pad_push_event (stream->channelpad[0], event);
3653 res = gst_pad_send_event (stream->channelpad[0], event);
3656 if (stream->udpsrc[1]) {
3657 gst_event_ref (event);
3658 res &= gst_element_send_event (stream->udpsrc[1], event);
3659 } else if (stream->channelpad[1]) {
3660 gst_event_ref (event);
3661 if (GST_PAD_IS_SRC (stream->channelpad[1]))
3662 res &= gst_pad_push_event (stream->channelpad[1], event);
3664 res &= gst_pad_send_event (stream->channelpad[1], event);
3668 gst_event_unref (event);
3674 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
3677 gboolean res = TRUE;
3679 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3680 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3682 gst_event_ref (event);
3683 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
3685 gst_event_unref (event);
3690 static GstRTSPResult
3691 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3696 if (info->connection == NULL) {
3697 if (info->url == NULL) {
3698 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
3699 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
3703 /* create connection */
3704 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
3705 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
3706 goto could_not_create;
3709 g_free (info->url_str);
3710 info->url_str = gst_rtsp_url_get_request_uri (info->url);
3712 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
3714 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
3715 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
3716 src->tls_validation_flags))
3717 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
3720 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
3721 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
3723 if (src->proxy_host) {
3724 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
3726 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
3731 if (!info->connected) {
3734 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
3735 ("Connecting to %s", info->location));
3736 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
3738 gst_rtsp_connection_connect (info->connection,
3739 src->ptcp_timeout)) < 0)
3740 goto could_not_connect;
3742 info->connected = TRUE;
3749 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
3754 gchar *str = gst_rtsp_strresult (res);
3755 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
3761 gchar *str = gst_rtsp_strresult (res);
3762 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
3768 static GstRTSPResult
3769 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
3772 GST_RTSP_STATE_LOCK (src);
3773 if (info->connected) {
3774 GST_DEBUG_OBJECT (src, "closing connection...");
3775 gst_rtsp_connection_close (info->connection);
3776 info->connected = FALSE;
3778 if (free && info->connection) {
3779 /* free connection */
3780 GST_DEBUG_OBJECT (src, "freeing connection...");
3781 gst_rtsp_connection_free (info->connection);
3782 info->connection = NULL;
3784 GST_RTSP_STATE_UNLOCK (src);
3788 static GstRTSPResult
3789 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3794 GST_DEBUG_OBJECT (src, "reconnecting connection...");
3795 gst_rtsp_conninfo_close (src, info, FALSE);
3796 res = gst_rtsp_conninfo_connect (src, info, async);
3802 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
3806 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
3807 GST_RTSP_STATE_LOCK (src);
3808 if (src->conninfo.connection && src->conninfo.flushing != flush) {
3809 GST_DEBUG_OBJECT (src, "connection flush");
3810 gst_rtsp_connection_flush (src->conninfo.connection, flush);
3811 src->conninfo.flushing = flush;
3813 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3814 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3815 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
3816 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
3817 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
3818 stream->conninfo.flushing = flush;
3821 GST_RTSP_STATE_UNLOCK (src);
3824 /* FIXME, handle server request, reply with OK, for now */
3825 static GstRTSPResult
3826 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
3827 GstRTSPMessage * request)
3829 GstRTSPMessage response = { 0 };
3832 GST_DEBUG_OBJECT (src, "got server request message");
3835 gst_rtsp_message_dump (request);
3837 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
3839 if (res == GST_RTSP_ENOTIMPL) {
3840 /* default implementation, send OK */
3841 GST_DEBUG_OBJECT (src, "prepare OK reply");
3843 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
3848 /* let app parse and reply */
3849 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
3850 0, request, &response);
3853 gst_rtsp_message_dump (&response);
3855 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
3859 gst_rtsp_message_unset (&response);
3860 } else if (res == GST_RTSP_EEOF)
3868 gst_rtsp_message_unset (&response);
3873 /* send server keep-alive */
3874 static GstRTSPResult
3875 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
3877 GstRTSPMessage request = { 0 };
3879 GstRTSPMethod method;
3880 const gchar *control;
3882 if (src->do_rtsp_keep_alive == FALSE) {
3883 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
3884 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
3888 GST_DEBUG_OBJECT (src, "creating server keep-alive");
3890 /* find a method to use for keep-alive */
3891 if (src->methods & GST_RTSP_GET_PARAMETER)
3892 method = GST_RTSP_GET_PARAMETER;
3894 method = GST_RTSP_OPTIONS;
3896 control = get_aggregate_control (src);
3897 if (control == NULL)
3900 res = gst_rtsp_message_init_request (&request, method, control);
3905 gst_rtsp_message_dump (&request);
3908 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
3913 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
3914 gst_rtsp_message_unset (&request);
3921 GST_WARNING_OBJECT (src, "no control url to send keepalive");
3926 gchar *str = gst_rtsp_strresult (res);
3928 gst_rtsp_message_unset (&request);
3929 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
3930 ("Could not send keep-alive. (%s)", str));
3936 static GstFlowReturn
3937 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
3939 GstFlowReturn ret = GST_FLOW_OK;
3941 GstRTSPStream *stream;
3942 GstPad *outpad = NULL;
3949 channel = message->type_data.data.channel;
3951 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
3953 goto unknown_stream;
3955 if (channel == stream->channel[0]) {
3956 outpad = stream->channelpad[0];
3958 } else if (channel == stream->channel[1]) {
3959 outpad = stream->channelpad[1];
3965 /* take a look at the body to figure out what we have */
3966 gst_rtsp_message_get_body (message, &data, &size);
3968 goto invalid_length;
3970 /* channels are not correct on some servers, do extra check */
3971 if (data[1] >= 200 && data[1] <= 204) {
3972 /* hmm RTCP message switch to the RTCP pad of the same stream. */
3973 outpad = stream->channelpad[1];
3977 /* we have no clue what this is, just ignore then. */
3979 goto unknown_stream;
3981 /* take the message body for further processing */
3982 gst_rtsp_message_steal_body (message, &data, &size);
3984 /* strip the trailing \0 */
3987 buf = gst_buffer_new ();
3988 gst_buffer_append_memory (buf,
3989 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
3991 /* don't need message anymore */
3992 gst_rtsp_message_unset (message);
3994 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
3997 if (src->need_activate) {
4003 guint group_id = gst_util_group_id_next ();
4005 /* generate an SHA256 sum of the URI */
4006 cs = g_checksum_new (G_CHECKSUM_SHA256);
4007 uri = src->conninfo.location;
4008 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
4010 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4011 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4014 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
4015 event = gst_event_new_stream_start (stream_id);
4016 gst_event_set_group_id (event, group_id);
4019 gst_rtspsrc_stream_push_event (src, ostream, event);
4021 g_checksum_free (cs);
4023 gst_rtspsrc_activate_streams (src);
4024 src->need_activate = FALSE;
4026 if ((event = src->start_segment) != NULL) {
4027 src->start_segment = NULL;
4028 gst_rtspsrc_push_event (src, event);
4031 if (src->base_time == -1) {
4032 /* Take current running_time. This timestamp will be put on
4033 * the first buffer of each stream because we are a live source and so we
4034 * timestamp with the running_time. When we are dealing with TCP, we also
4035 * only timestamp the first buffer (using the DISCONT flag) because a server
4036 * typically bursts data, for which we don't want to compensate by speeding
4037 * up the media. The other timestamps will be interpollated from this one
4038 * using the RTP timestamps. */
4039 GST_OBJECT_LOCK (src);
4040 if (GST_ELEMENT_CLOCK (src)) {
4042 GstClockTime base_time;
4044 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
4045 base_time = GST_ELEMENT_CAST (src)->base_time;
4047 src->base_time = now - base_time;
4049 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
4050 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
4052 GST_OBJECT_UNLOCK (src);
4055 if (stream->discont && !is_rtcp) {
4056 /* mark first RTP buffer as discont */
4057 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4058 stream->discont = FALSE;
4059 /* first buffer gets the timestamp, other buffers are not timestamped and
4060 * their presentation time will be interpollated from the rtp timestamps. */
4061 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
4062 GST_TIME_ARGS (src->base_time));
4064 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
4067 /* chain to the peer pad */
4068 if (GST_PAD_IS_SINK (outpad))
4069 ret = gst_pad_chain (outpad, buf);
4071 ret = gst_pad_push (outpad, buf);
4074 /* combine all stream flows for the data transport */
4075 ret = gst_rtspsrc_combine_flows (src, stream, ret);
4082 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
4083 gst_rtsp_message_unset (message);
4088 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4089 ("Short message received, ignoring."));
4090 gst_rtsp_message_unset (message);
4095 static GstFlowReturn
4096 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
4098 GstRTSPMessage message = { 0 };
4100 GstFlowReturn ret = GST_FLOW_OK;
4101 GTimeVal tv_timeout;
4104 /* get the next timeout interval */
4105 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4107 /* see if the timeout period expired */
4108 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
4109 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
4110 /* send keep-alive, only act on interrupt, a warning will be posted for
4112 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4114 /* get new timeout */
4115 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4118 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
4119 tv_timeout.tv_sec, tv_timeout.tv_usec);
4121 /* protect the connection with the connection lock so that we can see when
4122 * we are finished doing server communication */
4124 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4125 &message, src->ptcp_timeout);
4129 GST_DEBUG_OBJECT (src, "we received a server message");
4131 case GST_RTSP_EINTR:
4132 /* we got interrupted this means we need to stop */
4134 case GST_RTSP_ETIMEOUT:
4135 /* no reply, send keep alive */
4136 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4137 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4141 /* go EOS when the server closed the connection */
4147 switch (message.type) {
4148 case GST_RTSP_MESSAGE_REQUEST:
4149 /* server sends us a request message, handle it */
4151 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4153 if (res == GST_RTSP_EEOF)
4156 goto handle_request_failed;
4158 case GST_RTSP_MESSAGE_RESPONSE:
4159 /* we ignore response messages */
4160 GST_DEBUG_OBJECT (src, "ignoring response message");
4162 gst_rtsp_message_dump (&message);
4164 case GST_RTSP_MESSAGE_DATA:
4165 GST_DEBUG_OBJECT (src, "got data message");
4166 ret = gst_rtspsrc_handle_data (src, &message);
4167 if (ret != GST_FLOW_OK)
4168 goto handle_data_failed;
4171 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4176 g_assert_not_reached ();
4181 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4182 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4183 ("The server closed the connection."));
4184 src->conninfo.connected = FALSE;
4185 gst_rtsp_message_unset (&message);
4186 return GST_FLOW_EOS;
4190 gst_rtsp_message_unset (&message);
4191 GST_DEBUG_OBJECT (src, "got interrupted");
4192 return GST_FLOW_FLUSHING;
4196 gchar *str = gst_rtsp_strresult (res);
4198 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4199 ("Could not receive message. (%s)", str));
4202 gst_rtsp_message_unset (&message);
4203 return GST_FLOW_ERROR;
4205 handle_request_failed:
4207 gchar *str = gst_rtsp_strresult (res);
4209 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4210 ("Could not handle server message. (%s)", str));
4212 gst_rtsp_message_unset (&message);
4213 return GST_FLOW_ERROR;
4217 GST_DEBUG_OBJECT (src, "could no handle data message");
4222 static GstFlowReturn
4223 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
4226 GstRTSPMessage message = { 0 };
4230 GTimeVal tv_timeout;
4232 /* get the next timeout interval */
4233 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4235 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
4236 (gint) tv_timeout.tv_sec);
4238 gst_rtsp_message_unset (&message);
4240 /* we should continue reading the TCP socket because the server might
4241 * send us requests. When the session timeout expires, we need to send a
4242 * keep-alive request to keep the session open. */
4243 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4244 &message, &tv_timeout);
4248 GST_DEBUG_OBJECT (src, "we received a server message");
4250 case GST_RTSP_EINTR:
4251 /* we got interrupted, see what we have to do */
4253 case GST_RTSP_ETIMEOUT:
4254 /* send keep-alive, ignore the result, a warning will be posted. */
4255 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4256 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4260 /* server closed the connection. not very fatal for UDP, reconnect and
4261 * see what happens. */
4262 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4263 ("The server closed the connection."));
4264 if (src->udp_reconnect) {
4266 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
4273 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
4275 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4276 ("Unhandled return value %d.", res));
4280 switch (message.type) {
4281 case GST_RTSP_MESSAGE_REQUEST:
4282 /* server sends us a request message, handle it */
4284 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4286 if (res == GST_RTSP_EEOF)
4289 goto handle_request_failed;
4291 case GST_RTSP_MESSAGE_RESPONSE:
4292 /* we ignore response and data messages */
4293 GST_DEBUG_OBJECT (src, "ignoring response message");
4295 gst_rtsp_message_dump (&message);
4296 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4297 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
4298 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
4299 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
4300 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4307 case GST_RTSP_MESSAGE_DATA:
4308 /* we ignore response and data messages */
4309 GST_DEBUG_OBJECT (src, "ignoring data message");
4312 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4317 g_assert_not_reached ();
4319 /* we get here when the connection got interrupted */
4322 gst_rtsp_message_unset (&message);
4323 GST_DEBUG_OBJECT (src, "got interrupted");
4324 return GST_FLOW_FLUSHING;
4328 gchar *str = gst_rtsp_strresult (res);
4331 src->conninfo.connected = FALSE;
4332 if (res != GST_RTSP_EINTR) {
4333 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
4334 ("Could not connect to server. (%s)", str));
4336 ret = GST_FLOW_ERROR;
4338 ret = GST_FLOW_FLUSHING;
4344 gchar *str = gst_rtsp_strresult (res);
4346 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4347 ("Could not receive message. (%s)", str));
4349 return GST_FLOW_ERROR;
4351 handle_request_failed:
4353 gchar *str = gst_rtsp_strresult (res);
4356 gst_rtsp_message_unset (&message);
4357 if (res != GST_RTSP_EINTR) {
4358 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4359 ("Could not handle server message. (%s)", str));
4361 ret = GST_FLOW_ERROR;
4363 ret = GST_FLOW_FLUSHING;
4369 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4370 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4371 ("The server closed the connection."));
4372 src->conninfo.connected = FALSE;
4373 gst_rtsp_message_unset (&message);
4374 return GST_FLOW_EOS;
4378 static GstRTSPResult
4379 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
4381 GstRTSPResult res = GST_RTSP_OK;
4384 GST_DEBUG_OBJECT (src, "doing reconnect");
4386 GST_OBJECT_LOCK (src);
4387 /* only restart when the pads were not yet activated, else we were
4388 * streaming over UDP */
4389 restart = src->need_activate;
4390 GST_OBJECT_UNLOCK (src);
4392 /* no need to restart, we're done */
4396 /* we can try only TCP now */
4397 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
4399 /* close and cleanup our state */
4400 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
4403 /* see if we have TCP left to try. Also don't try TCP when we were configured
4405 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
4408 /* We post a warning message now to inform the user
4409 * that nothing happened. It's most likely a firewall thing. */
4410 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4411 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4412 "firewall is blocking it. Retrying using a TCP connection.",
4413 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4415 /* open new connection using tcp */
4416 if (gst_rtspsrc_open (src, async) < 0)
4419 /* start playback */
4420 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
4429 src->cur_protocols = 0;
4430 /* no transport possible, post an error and stop */
4431 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4432 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4433 "firewall is blocking it. No other protocols to try.",
4434 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4435 return GST_RTSP_ERROR;
4439 GST_DEBUG_OBJECT (src, "open failed");
4444 GST_DEBUG_OBJECT (src, "play failed");
4450 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
4454 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
4457 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
4460 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
4463 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
4471 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
4475 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
4478 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
4481 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
4484 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
4492 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
4496 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
4499 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
4502 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
4505 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
4513 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
4517 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
4520 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
4523 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
4526 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
4534 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
4536 if (ret == GST_RTSP_OK)
4537 gst_rtspsrc_loop_complete_cmd (src, cmd);
4538 else if (ret == GST_RTSP_EINTR)
4539 gst_rtspsrc_loop_cancel_cmd (src, cmd);
4541 gst_rtspsrc_loop_error_cmd (src, cmd);
4545 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
4548 gboolean flushed = FALSE;
4550 /* start new request */
4551 gst_rtspsrc_loop_start_cmd (src, cmd);
4553 GST_DEBUG_OBJECT (src, "sending cmd %d", cmd);
4555 GST_OBJECT_LOCK (src);
4556 old = src->pending_cmd;
4557 if (old == CMD_RECONNECT) {
4558 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
4559 cmd = CMD_RECONNECT;
4561 if (old != CMD_WAIT) {
4562 src->pending_cmd = CMD_WAIT;
4563 GST_OBJECT_UNLOCK (src);
4564 /* cancel previous request */
4565 GST_DEBUG_OBJECT (src, "cancel previous request %d", old);
4566 gst_rtspsrc_loop_cancel_cmd (src, old);
4567 GST_OBJECT_LOCK (src);
4569 src->pending_cmd = cmd;
4570 /* interrupt if allowed */
4571 if (src->busy_cmd & mask) {
4572 GST_DEBUG_OBJECT (src, "connection flush busy %d", src->busy_cmd);
4573 gst_rtspsrc_connection_flush (src, TRUE);
4576 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %d", src->busy_cmd);
4579 gst_task_start (src->task);
4580 GST_OBJECT_UNLOCK (src);
4586 gst_rtspsrc_loop (GstRTSPSrc * src)
4590 if (!src->conninfo.connection || !src->conninfo.connected)
4593 if (src->interleaved)
4594 ret = gst_rtspsrc_loop_interleaved (src);
4596 ret = gst_rtspsrc_loop_udp (src);
4598 if (ret != GST_FLOW_OK)
4606 GST_WARNING_OBJECT (src, "we are not connected");
4607 ret = GST_FLOW_FLUSHING;
4612 const gchar *reason = gst_flow_get_name (ret);
4614 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
4615 src->running = FALSE;
4616 if (ret == GST_FLOW_EOS) {
4617 /* perform EOS logic */
4618 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4619 gst_element_post_message (GST_ELEMENT_CAST (src),
4620 gst_message_new_segment_done (GST_OBJECT_CAST (src),
4621 src->segment.format, src->segment.position));
4622 gst_rtspsrc_push_event (src,
4623 gst_event_new_segment_done (src->segment.format,
4624 src->segment.position));
4626 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4628 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
4629 /* for fatal errors we post an error message, post the error before the
4630 * EOS so the app knows about the error first. */
4631 GST_ELEMENT_ERROR (src, STREAM, FAILED,
4632 ("Internal data flow error."),
4633 ("streaming task paused, reason %s (%d)", reason, ret));
4634 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4636 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
4641 #ifndef GST_DISABLE_GST_DEBUG
4642 static const gchar *
4643 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
4647 while (method != 0) {
4664 static const gchar *
4665 gst_rtspsrc_skip_lws (const gchar * s)
4667 while (g_ascii_isspace (*s))
4672 static const gchar *
4673 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
4675 while (s > start && g_ascii_isspace (*(s - 1)))
4680 static const gchar *
4681 gst_rtspsrc_skip_commas (const gchar * s)
4683 /* The grammar allows for multiple commas */
4684 while (g_ascii_isspace (*s) || *s == ',')
4689 static const gchar *
4690 gst_rtspsrc_skip_item (const gchar * s)
4692 gboolean quoted = FALSE;
4693 const gchar *start = s;
4695 /* A list item ends at the last non-whitespace character
4696 * before a comma which is not inside a quoted-string. Or at
4697 * the end of the string.
4703 if (*s == '\\' && *(s + 1))
4712 return gst_rtspsrc_unskip_lws (s, start);
4716 gst_rtsp_decode_quoted_string (gchar * quoted_string)
4720 src = quoted_string + 1;
4721 dst = quoted_string;
4722 while (*src && *src != '"') {
4723 if (*src == '\\' && *(src + 1))
4730 /* Extract the authentication tokens that the server provided for each method
4731 * into an array of structures and give those to the connection object.
4734 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
4735 const gchar * header, gboolean * stale)
4737 GSList *list = NULL, *iter;
4739 gchar *item, *eq, *name_end, *value;
4741 g_return_if_fail (stale != NULL);
4743 gst_rtsp_connection_clear_auth_params (conn);
4746 /* Parse a header whose content is described by RFC2616 as
4747 * "#something", where "something" does not itself contain commas,
4748 * except as part of quoted-strings, into a list of allocated strings.
4750 header = gst_rtspsrc_skip_commas (header);
4752 end = gst_rtspsrc_skip_item (header);
4753 list = g_slist_prepend (list, g_strndup (header, end - header));
4754 header = gst_rtspsrc_skip_commas (end);
4759 list = g_slist_reverse (list);
4760 for (iter = list; iter; iter = iter->next) {
4763 eq = strchr (item, '=');
4765 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
4766 if (name_end == item) {
4767 /* That's no good... */
4774 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
4776 gst_rtsp_decode_quoted_string (value);
4780 if (item && (strcmp (item, "stale") == 0) &&
4781 value && (strcmp (value, "TRUE") == 0))
4783 gst_rtsp_connection_set_auth_param (conn, item, value);
4787 g_slist_free (list);
4790 /* Parse a WWW-Authenticate Response header and determine the
4791 * available authentication methods
4793 * This code should also cope with the fact that each WWW-Authenticate
4794 * header can contain multiple challenge methods + tokens
4796 * At the moment, for Basic auth, we just do a minimal check and don't
4797 * even parse out the realm */
4799 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
4800 GstRTSPConnection * conn, gboolean * stale)
4804 g_return_if_fail (hdr != NULL);
4805 g_return_if_fail (methods != NULL);
4806 g_return_if_fail (stale != NULL);
4808 /* Skip whitespace at the start of the string */
4809 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
4811 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
4812 *methods |= GST_RTSP_AUTH_BASIC;
4813 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
4814 *methods |= GST_RTSP_AUTH_DIGEST;
4815 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
4820 * gst_rtspsrc_setup_auth:
4821 * @src: the rtsp source
4823 * Configure a username and password and auth method on the
4824 * connection object based on a response we received from the
4827 * Currently, this requires that a username and password were supplied
4828 * in the uri. In the future, they may be requested on demand by sending
4829 * a message up the bus.
4831 * Returns: TRUE if authentication information could be set up correctly.
4834 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
4838 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
4839 GstRTSPAuthMethod method;
4840 GstRTSPResult auth_result;
4842 GstRTSPConnection *conn;
4844 gboolean stale = FALSE;
4846 conn = src->conninfo.connection;
4848 /* Identify the available auth methods and see if any are supported */
4849 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
4850 &hdr, 0) == GST_RTSP_OK) {
4851 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
4854 if (avail_methods == GST_RTSP_AUTH_NONE)
4855 goto no_auth_available;
4857 /* For digest auth, if the response indicates that the session
4858 * data are stale, we just update them in the connection object and
4859 * return TRUE to retry the request */
4861 src->tried_url_auth = FALSE;
4863 url = gst_rtsp_connection_get_url (conn);
4865 /* Do we have username and password available? */
4866 if (url != NULL && !src->tried_url_auth && url->user != NULL
4867 && url->passwd != NULL) {
4870 src->tried_url_auth = TRUE;
4871 GST_DEBUG_OBJECT (src,
4872 "Attempting authentication using credentials from the URL");
4874 user = src->user_id;
4875 pass = src->user_pw;
4876 GST_DEBUG_OBJECT (src,
4877 "Attempting authentication using credentials from the properties");
4880 /* FIXME: If the url didn't contain username and password or we tried them
4881 * already, request a username and passwd from the application via some kind
4882 * of credentials request message */
4884 /* If we don't have a username and passwd at this point, bail out. */
4885 if (user == NULL || pass == NULL)
4888 /* Try to configure for each available authentication method, strongest to
4890 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
4891 /* Check if this method is available on the server */
4892 if ((method & avail_methods) == 0)
4895 /* Pass the credentials to the connection to try on the next request */
4896 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
4897 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
4898 * ignore it and end up retrying later */
4899 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
4900 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
4901 gst_rtsp_auth_method_to_string (method));
4906 if (method == GST_RTSP_AUTH_NONE)
4907 goto no_auth_available;
4913 /* Output an error indicating that we couldn't connect because there were
4914 * no supported authentication protocols */
4915 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4916 ("No supported authentication protocol was found"));
4921 /* We don't fire an error message, we just return FALSE and let the
4922 * normal NOT_AUTHORIZED error be propagated */
4927 static GstRTSPResult
4928 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
4929 GstRTSPMessage * request, GstRTSPMessage * response,
4930 GstRTSPStatusCode * code)
4933 GstRTSPStatusCode thecode;
4934 gchar *content_base = NULL;
4938 if (!src->short_header)
4939 gst_rtsp_ext_list_before_send (src->extensions, request);
4941 GST_DEBUG_OBJECT (src, "sending message");
4944 gst_rtsp_message_dump (request);
4946 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
4950 gst_rtsp_connection_reset_timeout (conn);
4953 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
4958 gst_rtsp_message_dump (response);
4960 switch (response->type) {
4961 case GST_RTSP_MESSAGE_REQUEST:
4962 res = gst_rtspsrc_handle_request (src, conn, response);
4963 if (res == GST_RTSP_EEOF)
4966 goto handle_request_failed;
4968 case GST_RTSP_MESSAGE_RESPONSE:
4969 /* ok, a response is good */
4970 GST_DEBUG_OBJECT (src, "received response message");
4972 case GST_RTSP_MESSAGE_DATA:
4973 /* get next response */
4974 GST_DEBUG_OBJECT (src, "handle data response message");
4975 gst_rtspsrc_handle_data (src, response);
4978 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4983 thecode = response->type_data.response.code;
4985 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
4987 /* if the caller wanted the result code, we store it. */
4991 /* If the request didn't succeed, bail out before doing any more */
4992 if (thecode != GST_RTSP_STS_OK)
4995 /* store new content base if any */
4996 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
4999 g_free (src->content_base);
5000 src->content_base = g_strdup (content_base);
5002 gst_rtsp_ext_list_after_send (src->extensions, request, response);
5009 gchar *str = gst_rtsp_strresult (res);
5011 if (res != GST_RTSP_EINTR) {
5012 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5013 ("Could not send message. (%s)", str));
5015 GST_WARNING_OBJECT (src, "send interrupted");
5024 GST_WARNING_OBJECT (src, "server closed connection");
5025 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
5027 /* if reconnect succeeds, try again */
5029 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
5033 /* only try once after reconnect, then fallthrough and error out */
5036 gchar *str = gst_rtsp_strresult (res);
5038 if (res != GST_RTSP_EINTR) {
5039 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5040 ("Could not receive message. (%s)", str));
5042 GST_WARNING_OBJECT (src, "receive interrupted");
5050 handle_request_failed:
5052 /* ERROR was posted */
5053 gst_rtsp_message_unset (response);
5058 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5059 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5060 ("The server closed the connection."));
5061 gst_rtsp_message_unset (response);
5068 * @src: the rtsp source
5069 * @conn: the connection to send on
5070 * @request: must point to a valid request
5071 * @response: must point to an empty #GstRTSPMessage
5072 * @code: an optional code result
5074 * send @request and retrieve the response in @response. optionally @code can be
5075 * non-NULL in which case it will contain the status code of the response.
5077 * If This function returns #GST_RTSP_OK, @response will contain a valid response
5078 * message that should be cleaned with gst_rtsp_message_unset() after usage.
5080 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
5081 * @response message) if the response code was not 200 (OK).
5083 * If the attempt results in an authentication failure, then this will attempt
5084 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
5087 * Returns: #GST_RTSP_OK if the processing was successful.
5089 static GstRTSPResult
5090 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5091 GstRTSPMessage * request, GstRTSPMessage * response,
5092 GstRTSPStatusCode * code)
5094 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
5095 GstRTSPResult res = GST_RTSP_ERROR;
5098 GstRTSPMethod method = GST_RTSP_INVALID;
5104 /* make sure we don't loop forever */
5108 /* save method so we can disable it when the server complains */
5109 method = request->type_data.request.method;
5112 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
5116 case GST_RTSP_STS_UNAUTHORIZED:
5117 if (gst_rtspsrc_setup_auth (src, response)) {
5118 /* Try the request/response again after configuring the auth info
5126 } while (retry == TRUE);
5128 /* If the user requested the code, let them handle errors, otherwise
5129 * post an error below */
5132 else if (int_code != GST_RTSP_STS_OK)
5133 goto error_response;
5140 GST_DEBUG_OBJECT (src, "got error %d", res);
5145 res = GST_RTSP_ERROR;
5147 switch (response->type_data.response.code) {
5148 case GST_RTSP_STS_NOT_FOUND:
5149 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
5150 response->type_data.response.reason));
5152 case GST_RTSP_STS_MOVED_PERMANENTLY:
5153 case GST_RTSP_STS_MOVE_TEMPORARILY:
5155 gchar *new_location;
5156 GstRTSPLowerTrans transports;
5158 GST_DEBUG_OBJECT (src, "got redirection");
5159 /* if we don't have a Location Header, we must error */
5160 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
5161 &new_location, 0) < 0)
5164 /* When we receive a redirect result, we go back to the INIT state after
5165 * parsing the new URI. The caller should do the needed steps to issue
5166 * a new setup when it detects this state change. */
5167 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
5169 /* save current transports */
5170 if (src->conninfo.url)
5171 transports = src->conninfo.url->transports;
5173 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
5175 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
5177 /* set old transports */
5178 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
5179 src->conninfo.url->transports = transports;
5181 src->need_redirect = TRUE;
5182 src->state = GST_RTSP_STATE_INIT;
5186 case GST_RTSP_STS_NOT_ACCEPTABLE:
5187 case GST_RTSP_STS_NOT_IMPLEMENTED:
5188 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
5189 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
5190 gst_rtsp_method_as_text (method));
5191 src->methods &= ~method;
5195 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5196 ("Got error response: %d (%s).", response->type_data.response.code,
5197 response->type_data.response.reason));
5200 /* if we return ERROR we should unset the response ourselves */
5201 if (res == GST_RTSP_ERROR)
5202 gst_rtsp_message_unset (response);
5208 static GstRTSPResult
5209 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
5210 GstRTSPMessage * response, GstRTSPSrc * src)
5212 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
5217 /* parse the response and collect all the supported methods. We need this
5218 * information so that we don't try to send an unsupported request to the
5222 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
5224 GstRTSPHeaderField field;
5228 /* reset supported methods */
5231 /* Try Allow Header first */
5232 field = GST_RTSP_HDR_ALLOW;
5235 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5236 if (indx == 0 && !respoptions) {
5237 /* if no Allow header was found then try the Public header... */
5238 field = GST_RTSP_HDR_PUBLIC;
5239 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5244 src->methods |= gst_rtsp_options_from_text (respoptions);
5249 if (src->methods == 0) {
5250 /* neither Allow nor Public are required, assume the server supports
5251 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
5253 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
5254 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
5256 /* always assume PLAY, FIXME, extensions should be able to override
5258 src->methods |= GST_RTSP_PLAY;
5259 /* also assume it will support Range */
5260 src->seekable = TRUE;
5262 /* we need describe and setup */
5263 if (!(src->methods & GST_RTSP_DESCRIBE))
5265 if (!(src->methods & GST_RTSP_SETUP))
5273 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5274 ("Server does not support DESCRIBE."));
5279 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5280 ("Server does not support SETUP."));
5285 /* masks to be kept in sync with the hardcoded protocol order of preference
5287 static guint protocol_masks[] = {
5288 GST_RTSP_LOWER_TRANS_UDP,
5289 GST_RTSP_LOWER_TRANS_UDP_MCAST,
5290 GST_RTSP_LOWER_TRANS_TCP,
5294 static GstRTSPResult
5295 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
5296 GstRTSPLowerTrans protocols, gchar ** transports)
5300 gboolean add_udp_str;
5305 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
5310 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
5312 /* extension listed transports, use those */
5313 if (*transports != NULL)
5316 /* it's the default */
5317 add_udp_str = FALSE;
5319 /* the default RTSP transports */
5320 result = g_string_new ("");
5321 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
5322 GST_DEBUG_OBJECT (src, "adding UDP unicast");
5324 g_string_append (result, "RTP/AVP");
5326 g_string_append (result, "/UDP");
5327 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
5328 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
5329 GST_DEBUG_OBJECT (src, "adding UDP multicast");
5331 /* we don't have to allocate any UDP ports yet, if the selected transport
5332 * turns out to be multicast we can create them and join the multicast
5333 * group indicated in the transport reply */
5334 if (result->len > 0)
5335 g_string_append (result, ",");
5336 g_string_append (result, "RTP/AVP");
5338 g_string_append (result, "/UDP");
5339 g_string_append (result, ";multicast");
5340 if (src->next_port_num != 0) {
5341 if (src->client_port_range.max > 0 &&
5342 src->next_port_num >= src->client_port_range.max)
5345 g_string_append_printf (result, ";client_port=%d-%d",
5346 src->next_port_num, src->next_port_num + 1);
5348 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
5349 GST_DEBUG_OBJECT (src, "adding TCP");
5351 if (result->len > 0)
5352 g_string_append (result, ",");
5353 g_string_append (result, "RTP/AVP/TCP;unicast;interleaved=%%i1-%%i2");
5355 *transports = g_string_free (result, FALSE);
5357 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
5364 GST_ERROR ("extension gave error %d", res);
5369 GST_ERROR ("no more ports available");
5370 return GST_RTSP_ERROR;
5374 static GstRTSPResult
5375 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
5376 gint orig_rtpport, gint orig_rtcpport)
5379 gint nr_udp, nr_int;
5381 gint rtpport = 0, rtcpport = 0;
5384 src = stream->parent;
5386 /* find number of placeholders first */
5387 if (strstr (*transports, "%%i2"))
5389 else if (strstr (*transports, "%%i1"))
5394 if (strstr (*transports, "%%u2"))
5396 else if (strstr (*transports, "%%u1"))
5401 if (nr_udp == 0 && nr_int == 0)
5405 if (!orig_rtpport || !orig_rtcpport) {
5406 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
5409 rtpport = orig_rtpport;
5410 rtcpport = orig_rtcpport;
5414 str = g_string_new ("");
5416 while ((next = strstr (p, "%%"))) {
5417 g_string_append_len (str, p, next - p);
5418 if (next[2] == 'u') {
5420 g_string_append_printf (str, "%d", rtpport);
5421 else if (next[3] == '2')
5422 g_string_append_printf (str, "%d", rtcpport);
5424 if (next[2] == 'i') {
5426 g_string_append_printf (str, "%d", src->free_channel);
5427 else if (next[3] == '2')
5428 g_string_append_printf (str, "%d", src->free_channel + 1);
5433 /* append final part */
5434 g_string_append (str, p);
5436 g_free (*transports);
5437 *transports = g_string_free (str, FALSE);
5445 GST_ERROR ("failed to allocate udp ports");
5446 return GST_RTSP_ERROR;
5451 gst_rtspsrc_stream_is_real_media (GstRTSPStream * stream)
5453 gboolean res = FALSE;
5457 const gchar *enc = NULL;
5459 s = gst_caps_get_structure (stream->caps, 0);
5460 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
5461 res = (strstr (enc, "-REAL") != NULL);
5467 /* Perform the SETUP request for all the streams.
5469 * We ask the server for a specific transport, which initially includes all the
5470 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
5471 * two local UDP ports that we send to the server.
5473 * Once the server replied with a transport, we configure the other streams
5474 * with the same transport.
5476 * This function will also configure the stream for the selected transport,
5477 * which basically means creating the pipeline.
5479 static GstRTSPResult
5480 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
5483 GstRTSPResult res = GST_RTSP_ERROR;
5484 GstRTSPMessage request = { 0 };
5485 GstRTSPMessage response = { 0 };
5486 GstRTSPStream *stream = NULL;
5487 GstRTSPLowerTrans protocols;
5488 GstRTSPStatusCode code;
5489 gboolean unsupported_real = FALSE;
5490 gint rtpport, rtcpport;
5494 if (src->conninfo.connection) {
5495 url = gst_rtsp_connection_get_url (src->conninfo.connection);
5496 /* we initially allow all configured lower transports. based on the URL
5497 * transports and the replies from the server we narrow them down. */
5498 protocols = url->transports & src->cur_protocols;
5501 protocols = src->cur_protocols;
5507 /* reset some state */
5508 src->free_channel = 0;
5509 src->interleaved = FALSE;
5510 src->need_activate = FALSE;
5511 /* keep track of next port number, 0 is random */
5512 src->next_port_num = src->client_port_range.min;
5513 rtpport = rtcpport = 0;
5515 if (G_UNLIKELY (src->streams == NULL))
5518 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5519 GstRTSPConnection *conn;
5525 stream = (GstRTSPStream *) walk->data;
5527 /* see if we need to configure this stream */
5528 if (!gst_rtsp_ext_list_configure_stream (src->extensions, stream->caps)) {
5529 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
5531 stream->disabled = TRUE;
5535 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
5536 stream->id, stream->caps, &selected);
5538 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
5539 stream->disabled = TRUE;
5542 stream->disabled = FALSE;
5544 /* merge/overwrite global caps */
5549 s = gst_caps_get_structure (stream->caps, 0);
5551 num = gst_structure_n_fields (src->props);
5552 for (j = 0; j < num; j++) {
5556 name = gst_structure_nth_field_name (src->props, j);
5557 val = gst_structure_get_value (src->props, name);
5558 gst_structure_set_value (s, name, val);
5560 GST_DEBUG_OBJECT (src, "copied %s", name);
5564 /* skip setup if we have no URL for it */
5565 if (stream->conninfo.location == NULL) {
5566 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
5570 if (src->conninfo.connection == NULL) {
5571 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
5572 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
5575 conn = stream->conninfo.connection;
5577 conn = src->conninfo.connection;
5579 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
5580 stream->conninfo.location);
5582 /* if we have a multicast connection, only suggest multicast from now on */
5583 if (stream->is_multicast)
5584 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
5587 /* first selectable protocol */
5588 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5590 if (!protocol_masks[mask])
5594 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
5595 protocol_masks[mask]);
5596 /* create a string with first transport in line */
5598 res = gst_rtspsrc_create_transports_string (src,
5599 protocols & protocol_masks[mask], &transports);
5600 if (res < 0 || transports == NULL)
5601 goto setup_transport_failed;
5603 if (strlen (transports) == 0) {
5604 g_free (transports);
5605 GST_DEBUG_OBJECT (src, "no transports found");
5610 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
5612 /* replace placeholders with real values, this function will optionally
5613 * allocate UDP ports and other info needed to execute the setup request */
5614 res = gst_rtspsrc_prepare_transports (stream, &transports,
5615 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
5617 g_free (transports);
5618 goto setup_transport_failed;
5621 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
5623 /* create SETUP request */
5625 gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
5626 stream->conninfo.location);
5628 g_free (transports);
5629 goto create_request_failed;
5632 /* select transport */
5633 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
5635 /* if the user wants a non default RTP packet size we add the blocksize
5637 if (src->rtp_blocksize > 0) {
5638 hval = g_strdup_printf ("%d", src->rtp_blocksize);
5639 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
5643 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
5646 /* handle the code ourselves */
5647 if ((res = gst_rtspsrc_send (src, conn, &request, &response, &code) < 0))
5651 case GST_RTSP_STS_OK:
5653 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
5654 gst_rtsp_message_unset (&request);
5655 gst_rtsp_message_unset (&response);
5656 /* cleanup of leftover transport */
5657 gst_rtspsrc_stream_free_udp (stream);
5658 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
5659 * we might be in this case */
5660 if (stream->container && rtpport && rtcpport && !retry) {
5661 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
5666 /* this transport did not go down well, but we may have others to try
5667 * that we did not send yet, try those and only give up then
5668 * but not without checking for lost cause/extension so we can
5669 * post a nicer/more useful error message later */
5670 if (!unsupported_real)
5671 unsupported_real = gst_rtspsrc_stream_is_real_media (stream);
5672 /* select next available protocol, give up on this stream if none */
5674 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5676 if (!protocol_masks[mask] || unsupported_real)
5681 /* cleanup of leftover transport and move to the next stream */
5682 gst_rtspsrc_stream_free_udp (stream);
5683 goto response_error;
5686 /* parse response transport */
5688 gchar *resptrans = NULL;
5689 GstRTSPTransport transport = { 0 };
5691 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
5694 gst_rtspsrc_stream_free_udp (stream);
5698 /* parse transport, go to next stream on parse error */
5699 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
5700 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
5704 /* update allowed transports for other streams. once the transport of
5705 * one stream has been determined, we make sure that all other streams
5706 * are configured in the same way */
5707 switch (transport.lower_transport) {
5708 case GST_RTSP_LOWER_TRANS_TCP:
5709 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
5710 protocols = GST_RTSP_LOWER_TRANS_TCP;
5711 src->interleaved = TRUE;
5712 /* update free channels */
5714 MAX (transport.interleaved.min, src->free_channel);
5716 MAX (transport.interleaved.max, src->free_channel);
5717 src->free_channel++;
5719 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
5720 /* only allow multicast for other streams */
5721 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
5722 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
5723 /* if the server selected our ports, increment our counters so that
5724 * we select a new port later */
5725 if (src->next_port_num == transport.port.min &&
5726 src->next_port_num + 1 == transport.port.max) {
5727 src->next_port_num += 2;
5730 case GST_RTSP_LOWER_TRANS_UDP:
5731 /* only allow unicast for other streams */
5732 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
5733 protocols = GST_RTSP_LOWER_TRANS_UDP;
5736 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
5737 transport.lower_transport);
5741 if (!stream->container || (!src->interleaved && !retry)) {
5742 /* now configure the stream with the selected transport */
5743 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
5744 GST_DEBUG_OBJECT (src,
5745 "could not configure stream %p transport, skipping stream",
5748 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
5749 /* retain the first allocated UDP port pair */
5750 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
5751 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
5754 /* we need to activate at least one streams when we detect activity */
5755 src->need_activate = TRUE;
5757 /* clean up our transport struct */
5758 gst_rtsp_transport_init (&transport);
5759 /* clean up used RTSP messages */
5760 gst_rtsp_message_unset (&request);
5761 gst_rtsp_message_unset (&response);
5765 /* store the transport protocol that was configured */
5766 src->cur_protocols = protocols;
5768 gst_rtsp_ext_list_stream_select (src->extensions, url);
5770 /* if there is nothing to activate, error out */
5771 if (!src->need_activate)
5772 goto nothing_to_activate;
5779 /* no transport possible, post an error and stop */
5780 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5781 ("Could not connect to server, no protocols left"));
5782 return GST_RTSP_ERROR;
5786 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5787 ("SDP contains no streams"));
5788 return GST_RTSP_ERROR;
5790 create_request_failed:
5792 gchar *str = gst_rtsp_strresult (res);
5794 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5795 ("Could not create request. (%s)", str));
5799 setup_transport_failed:
5801 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5802 ("Could not setup transport."));
5803 res = GST_RTSP_ERROR;
5808 const gchar *str = gst_rtsp_status_as_text (code);
5810 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5811 ("Error (%d): %s", code, GST_STR_NULL (str)));
5812 res = GST_RTSP_ERROR;
5817 gchar *str = gst_rtsp_strresult (res);
5819 if (res != GST_RTSP_EINTR) {
5820 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5821 ("Could not send message. (%s)", str));
5823 GST_WARNING_OBJECT (src, "send interrupted");
5830 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5831 ("Server did not select transport."));
5832 res = GST_RTSP_ERROR;
5835 nothing_to_activate:
5837 /* none of the available error codes is really right .. */
5838 if (unsupported_real) {
5839 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5840 (_("No supported stream was found. You might need to install a "
5841 "GStreamer RTSP extension plugin for Real media streams.")),
5844 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5845 (_("No supported stream was found. You might need to allow "
5846 "more transport protocols or may otherwise be missing "
5847 "the right GStreamer RTSP extension plugin.")), (NULL));
5849 return GST_RTSP_ERROR;
5853 gst_rtsp_message_unset (&request);
5854 gst_rtsp_message_unset (&response);
5860 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
5861 GstSegment * segment)
5864 GstRTSPTimeRange *therange;
5867 gst_rtsp_range_free (src->range);
5869 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
5870 GST_DEBUG_OBJECT (src, "parsed range %s", range);
5871 src->range = therange;
5873 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
5875 gst_segment_init (segment, GST_FORMAT_TIME);
5879 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
5880 therange->min.type, therange->min.seconds, therange->max.type,
5881 therange->max.seconds);
5883 if (therange->min.type == GST_RTSP_TIME_NOW)
5885 else if (therange->min.type == GST_RTSP_TIME_END)
5888 seconds = therange->min.seconds * GST_SECOND;
5890 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
5891 GST_TIME_ARGS (seconds));
5893 /* we need to start playback without clipping from the position reported by
5895 segment->start = seconds;
5896 segment->position = seconds;
5898 if (therange->max.type == GST_RTSP_TIME_NOW)
5900 else if (therange->max.type == GST_RTSP_TIME_END)
5903 seconds = therange->max.seconds * GST_SECOND;
5905 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
5906 GST_TIME_ARGS (seconds));
5908 /* live (WMS) server might send overflowed large max as its idea of infinity,
5909 * compensate to prevent problems later on */
5910 if (seconds != -1 && seconds < 0) {
5912 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
5915 /* live (WMS) might send min == max, which is not worth recording */
5916 if (segment->duration == -1 && seconds == segment->start)
5919 /* don't change duration with unknown value, we might have a valid value
5920 * there that we want to keep. */
5922 segment->duration = seconds;
5927 /* Parse clock profived by the server with following syntax:
5929 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
5932 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
5934 gboolean res = FALSE;
5936 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
5937 gchar **fields = NULL, **parts = NULL;
5938 gchar *remote_ip, *str;
5940 GstClockTime base_time;
5943 fields = g_strsplit (gstclock, " ", 0);
5945 /* wrapped clock, not very interesting for now */
5946 if (fields[1] == NULL)
5949 /* remote IP address and port */
5950 if ((str = fields[2]) == NULL)
5953 parts = g_strsplit (str, ":", 0);
5955 if ((remote_ip = parts[0]) == NULL)
5958 if ((str = parts[1]) == NULL)
5966 if ((str = fields[3]) == NULL)
5969 base_time = g_ascii_strtoull (str, NULL, 10);
5972 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
5975 if (src->provided_clock)
5976 gst_object_unref (src->provided_clock);
5977 src->provided_clock = netclock;
5979 gst_element_post_message (GST_ELEMENT_CAST (src),
5980 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
5981 src->provided_clock, TRUE));
5985 g_strfreev (fields);
5991 /* must be called with the RTSP state lock */
5992 static GstRTSPResult
5993 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
5999 /* prepare global stream caps properties */
6001 gst_structure_remove_all_fields (src->props);
6003 src->props = gst_structure_new_empty ("RTSPProperties");
6006 gst_sdp_message_dump (sdp);
6008 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
6010 /* let the app inspect and change the SDP */
6011 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
6013 gst_segment_init (&src->segment, GST_FORMAT_TIME);
6015 /* parse range for duration reporting. */
6020 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
6024 /* keep track of the range and configure it in the segment */
6025 if (gst_rtspsrc_parse_range (src, range, &src->segment))
6029 /* parse clock information. This is GStreamer specific, a server can tell the
6030 * client what clock it is using and wrap that in a network clock. The
6031 * advantage of that is that we can slave to it. */
6033 const gchar *gstclock;
6036 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
6037 if (gstclock == NULL)
6040 /* parse the clock and expose it in the provide_clock method */
6041 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
6045 /* try to find a global control attribute. Note that a '*' means that we should
6046 * do aggregate control with the current url (so we don't do anything and
6047 * leave the current connection as is) */
6049 const gchar *control;
6052 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
6053 if (control == NULL)
6056 /* only take fully qualified urls */
6057 if (g_str_has_prefix (control, "rtsp://"))
6061 g_free (src->conninfo.location);
6062 src->conninfo.location = g_strdup (control);
6063 /* make a connection for this, if there was a connection already, nothing
6065 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
6066 GST_ERROR_OBJECT (src, "could not connect");
6069 /* we need to keep the control url separate from the connection url because
6070 * the rules for constructing the media control url need it */
6071 g_free (src->control);
6072 src->control = g_strdup (control);
6075 /* create streams */
6076 n_streams = gst_sdp_message_medias_len (sdp);
6077 for (i = 0; i < n_streams; i++) {
6078 gst_rtspsrc_create_stream (src, sdp, i);
6081 src->state = GST_RTSP_STATE_INIT;
6084 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
6087 /* reset our state */
6088 src->need_range = TRUE;
6091 src->state = GST_RTSP_STATE_READY;
6098 GST_ERROR_OBJECT (src, "setup failed");
6099 gst_rtspsrc_cleanup (src);
6104 static GstRTSPResult
6105 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
6109 GstRTSPMessage request = { 0 };
6110 GstRTSPMessage response = { 0 };
6113 gchar *respcont = NULL;
6116 src->need_redirect = FALSE;
6118 /* can't continue without a valid url */
6119 if (G_UNLIKELY (src->conninfo.url == NULL)) {
6120 res = GST_RTSP_EINVAL;
6123 src->tried_url_auth = FALSE;
6125 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
6126 goto connect_failed;
6128 /* create OPTIONS */
6129 GST_DEBUG_OBJECT (src, "create options...");
6131 gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
6132 src->conninfo.url_str);
6134 goto create_request_failed;
6137 GST_DEBUG_OBJECT (src, "send options...");
6140 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
6143 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6148 if (!gst_rtspsrc_parse_methods (src, &response))
6151 /* create DESCRIBE */
6152 GST_DEBUG_OBJECT (src, "create describe...");
6154 gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
6155 src->conninfo.url_str);
6157 goto create_request_failed;
6159 /* we only accept SDP for now */
6160 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
6164 GST_DEBUG_OBJECT (src, "send describe...");
6167 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
6170 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6174 /* we only perform redirect for the describe, currently */
6175 if (src->need_redirect) {
6176 /* close connection, we don't have to send a TEARDOWN yet, ignore the
6178 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6180 gst_rtsp_message_unset (&request);
6181 gst_rtsp_message_unset (&response);
6187 /* it could be that the DESCRIBE method was not implemented */
6188 if (!src->methods & GST_RTSP_DESCRIBE)
6191 /* check if reply is SDP */
6192 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
6194 /* could not be set but since the request returned OK, we assume it
6195 * was SDP, else check it. */
6197 if (!g_ascii_strcasecmp (respcont, "application/sdp") == 0)
6198 goto wrong_content_type;
6201 /* get message body and parse as SDP */
6202 gst_rtsp_message_get_body (&response, &data, &size);
6203 if (data == NULL || size == 0)
6206 GST_DEBUG_OBJECT (src, "parse SDP...");
6207 gst_sdp_message_new (sdp);
6208 gst_sdp_message_parse_buffer (data, size, *sdp);
6210 /* clean up any messages */
6211 gst_rtsp_message_unset (&request);
6212 gst_rtsp_message_unset (&response);
6219 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
6220 ("No valid RTSP URL was provided"));
6225 gchar *str = gst_rtsp_strresult (res);
6227 if (res != GST_RTSP_EINTR) {
6228 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
6229 ("Failed to connect. (%s)", str));
6231 GST_WARNING_OBJECT (src, "connect interrupted");
6236 create_request_failed:
6238 gchar *str = gst_rtsp_strresult (res);
6240 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6241 ("Could not create request. (%s)", str));
6247 /* Don't post a message - the rtsp_send method will have
6248 * taken care of it because we passed NULL for the response code */
6253 /* error was posted */
6254 res = GST_RTSP_ERROR;
6259 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6260 ("Server does not support SDP, got %s.", respcont));
6261 res = GST_RTSP_ERROR;
6266 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6267 ("Server can not provide an SDP."));
6268 res = GST_RTSP_ERROR;
6273 if (src->conninfo.connection) {
6274 GST_DEBUG_OBJECT (src, "free connection");
6275 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6277 gst_rtsp_message_unset (&request);
6278 gst_rtsp_message_unset (&response);
6283 static GstRTSPResult
6284 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
6289 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
6291 if (src->sdp == NULL) {
6292 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
6296 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
6301 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
6308 GST_WARNING_OBJECT (src, "can't get sdp");
6309 src->open_error = TRUE;
6314 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
6315 src->open_error = TRUE;
6320 static GstRTSPResult
6321 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
6323 GstRTSPMessage request = { 0 };
6324 GstRTSPMessage response = { 0 };
6325 GstRTSPResult res = GST_RTSP_OK;
6327 const gchar *control;
6329 GST_DEBUG_OBJECT (src, "TEARDOWN...");
6331 gst_rtspsrc_set_state (src, GST_STATE_READY);
6333 if (src->state < GST_RTSP_STATE_READY) {
6334 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
6341 /* construct a control url */
6342 control = get_aggregate_control (src);
6344 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
6347 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6348 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6349 const gchar *setup_url;
6350 GstRTSPConnInfo *info;
6352 /* try aggregate control first but do non-aggregate control otherwise */
6354 setup_url = control;
6355 else if ((setup_url = stream->conninfo.location) == NULL)
6358 if (src->conninfo.connection) {
6359 info = &src->conninfo;
6360 } else if (stream->conninfo.connection) {
6361 info = &stream->conninfo;
6365 if (!info->connected)
6370 gst_rtsp_message_init_request (&request, GST_RTSP_TEARDOWN, setup_url);
6372 goto create_request_failed;
6375 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
6378 gst_rtspsrc_send (src, info->connection, &request, &response,
6382 /* FIXME, parse result? */
6383 gst_rtsp_message_unset (&request);
6384 gst_rtsp_message_unset (&response);
6387 /* early exit when we did aggregate control */
6393 /* close connections */
6394 GST_DEBUG_OBJECT (src, "closing connection...");
6395 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6396 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6397 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6398 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
6402 gst_rtspsrc_cleanup (src);
6404 src->state = GST_RTSP_STATE_INVALID;
6407 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
6412 create_request_failed:
6414 gchar *str = gst_rtsp_strresult (res);
6416 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6417 ("Could not create request. (%s)", str));
6423 gchar *str = gst_rtsp_strresult (res);
6425 gst_rtsp_message_unset (&request);
6426 if (res != GST_RTSP_EINTR) {
6427 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6428 ("Could not send message. (%s)", str));
6430 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
6437 GST_DEBUG_OBJECT (src,
6438 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
6443 /* RTP-Info is of the format:
6445 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
6447 * rtptime corresponds to the timestamp for the NPT time given in the header
6448 * seqbase corresponds to the next sequence number we received. This number
6449 * indicates the first seqnum after the seek and should be used to discard
6450 * packets that are from before the seek.
6453 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
6458 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
6460 infos = g_strsplit (rtpinfo, ",", 0);
6461 for (i = 0; infos[i]; i++) {
6463 GstRTSPStream *stream;
6467 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
6469 /* init values, types of seqbase and timebase are bigger than needed so we
6470 * can store -1 as uninitialized values */
6475 /* parse url, find stream for url.
6476 * parse seq and rtptime. The seq number should be configured in the rtp
6477 * depayloader or session manager to detect gaps. Same for the rtptime, it
6478 * should be used to create an initial time newsegment. */
6479 fields = g_strsplit (infos[i], ";", 0);
6480 for (j = 0; fields[j]; j++) {
6481 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
6482 /* remove leading whitespace */
6483 fields[j] = g_strchug (fields[j]);
6484 if (g_str_has_prefix (fields[j], "url=")) {
6485 /* get the url and the stream */
6487 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
6488 } else if (g_str_has_prefix (fields[j], "seq=")) {
6489 seqbase = atoi (fields[j] + 4);
6490 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
6491 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
6494 g_strfreev (fields);
6495 /* now we need to store the values for the caps of the stream */
6496 if (stream != NULL) {
6497 GST_DEBUG_OBJECT (src,
6498 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
6499 stream, seqbase, timebase);
6501 /* we have a stream, configure detected params */
6502 stream->seqbase = seqbase;
6503 stream->timebase = timebase;
6512 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
6517 interval = strtoul (rtcp, NULL, 10);
6518 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
6523 interval *= GST_MSECOND;
6525 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6526 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6528 /* already (optionally) retrieved this when configuring manager */
6529 if (stream->session) {
6530 GObject *rtpsession = stream->session;
6532 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
6534 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
6538 /* now it happens that (Xenon) server sending this may also provide bogus
6539 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
6540 * and just use RTP-Info to sync */
6542 GObjectClass *klass;
6544 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
6545 if (g_object_class_find_property (klass, "rtcp-sync")) {
6546 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
6547 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
6553 gst_rtspsrc_get_float (const gchar * dstr)
6555 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
6557 /* canonicalise floating point string so we can handle float strings
6558 * in the form "24.930" or "24,930" irrespective of the current locale */
6559 g_strlcpy (s, dstr, sizeof (s));
6560 g_strdelimit (s, ",", '.');
6561 return g_ascii_strtod (s, NULL);
6565 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
6567 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
6569 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
6570 g_strlcpy (val_str, "now", sizeof (val_str));
6572 if (segment->position == 0) {
6573 g_strlcpy (val_str, "0", sizeof (val_str));
6575 g_ascii_dtostr (val_str, sizeof (val_str),
6576 ((gdouble) segment->position) / GST_SECOND);
6579 return g_strdup_printf ("npt=%s-", val_str);
6583 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
6585 stream->timebase = -1;
6586 stream->seqbase = -1;
6590 stream->caps = gst_caps_make_writable (stream->caps);
6591 s = gst_caps_get_structure (stream->caps, 0);
6592 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
6596 static GstRTSPResult
6597 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
6599 GstRTSPResult res = GST_RTSP_OK;
6601 if (src->state < GST_RTSP_STATE_READY) {
6602 res = GST_RTSP_ERROR;
6603 if (src->open_error) {
6604 GST_DEBUG_OBJECT (src, "the stream was in error");
6608 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
6610 if ((res = gst_rtspsrc_open (src, async)) < 0) {
6611 GST_DEBUG_OBJECT (src, "failed to open stream");
6620 static GstRTSPResult
6621 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
6623 GstRTSPMessage request = { 0 };
6624 GstRTSPMessage response = { 0 };
6625 GstRTSPResult res = GST_RTSP_OK;
6629 const gchar *control;
6631 GST_DEBUG_OBJECT (src, "PLAY...");
6633 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6636 if (!(src->methods & GST_RTSP_PLAY))
6639 if (src->state == GST_RTSP_STATE_PLAYING)
6642 if (!src->conninfo.connection || !src->conninfo.connected)
6645 /* send some dummy packets before we activate the receive in the
6647 gst_rtspsrc_send_dummy_packets (src);
6649 /* require new SR packets */
6651 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
6653 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
6655 /* construct a control url */
6656 control = get_aggregate_control (src);
6658 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6659 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6660 const gchar *setup_url;
6661 GstRTSPConnection *conn;
6663 /* try aggregate control first but do non-aggregate control otherwise */
6665 setup_url = control;
6666 else if ((setup_url = stream->conninfo.location) == NULL)
6669 if (src->conninfo.connection) {
6670 conn = src->conninfo.connection;
6671 } else if (stream->conninfo.connection) {
6672 conn = stream->conninfo.connection;
6678 res = gst_rtsp_message_init_request (&request, GST_RTSP_PLAY, setup_url);
6680 goto create_request_failed;
6682 if (src->need_range) {
6683 hval = gen_range_header (src, segment);
6685 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
6687 /* store the newsegment event so it can be sent from the streaming thread. */
6688 if (src->start_segment)
6689 gst_event_unref (src->start_segment);
6690 src->start_segment = gst_event_new_segment (&src->segment);
6693 if (segment->rate != 1.0) {
6694 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
6696 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
6698 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
6700 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
6704 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
6706 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6709 /* seek may have silently failed as it is not supported */
6710 if (!(src->methods & GST_RTSP_PLAY)) {
6711 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
6712 /* obviously it is supported as we made it here */
6713 src->methods |= GST_RTSP_PLAY;
6714 src->seekable = FALSE;
6715 /* but there is nothing to parse in the response,
6716 * so convey we have no idea and not to expect anything particular */
6717 clear_rtp_base (src, stream);
6721 /* need to do for all streams */
6722 for (run = src->streams; run; run = g_list_next (run))
6723 clear_rtp_base (src, (GstRTSPStream *) run->data);
6725 /* NOTE the above also disables npt based eos detection */
6726 /* and below forces position to 0,
6727 * which is visible feedback we lost the plot */
6728 segment->start = segment->position = src->last_pos;
6731 gst_rtsp_message_unset (&request);
6733 /* parse RTP npt field. This is the current position in the stream (Normal
6734 * Play Time) and should be put in the NEWSEGMENT position field. */
6735 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
6737 gst_rtspsrc_parse_range (src, hval, segment);
6739 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
6740 segment->rate = 1.0;
6742 /* parse Speed header. This is the intended playback rate of the stream
6743 * and should be put in the NEWSEGMENT rate field. */
6744 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
6745 0) == GST_RTSP_OK) {
6746 segment->rate = gst_rtspsrc_get_float (hval);
6747 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
6748 &hval, 0) == GST_RTSP_OK) {
6749 segment->rate = gst_rtspsrc_get_float (hval);
6752 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
6753 * for the RTP packets. If this is not present, we assume all starts from 0...
6754 * This is info for the RTP session manager that we pass to it in caps. */
6756 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
6757 &hval, hval_idx++) == GST_RTSP_OK)
6758 gst_rtspsrc_parse_rtpinfo (src, hval);
6760 /* some servers indicate RTCP parameters in PLAY response,
6761 * rather than properly in SDP */
6762 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
6763 &hval, 0) == GST_RTSP_OK)
6764 gst_rtspsrc_handle_rtcp_interval (src, hval);
6766 gst_rtsp_message_unset (&response);
6768 /* early exit when we did aggregate control */
6772 /* configure the caps of the streams after we parsed all headers. Only reset
6773 * the manager object when we set a new Range header (we did a seek) */
6774 gst_rtspsrc_configure_caps (src, segment, src->need_range);
6776 /* set again when needed */
6777 src->need_range = FALSE;
6779 src->running = TRUE;
6780 src->base_time = -1;
6781 src->state = GST_RTSP_STATE_PLAYING;
6784 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
6785 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6786 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6787 stream->discont = TRUE;
6792 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
6799 GST_DEBUG_OBJECT (src, "failed to open stream");
6804 GST_DEBUG_OBJECT (src, "PLAY is not supported");
6809 GST_DEBUG_OBJECT (src, "we were already PLAYING");
6812 create_request_failed:
6814 gchar *str = gst_rtsp_strresult (res);
6816 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6817 ("Could not create request. (%s)", str));
6823 gchar *str = gst_rtsp_strresult (res);
6825 gst_rtsp_message_unset (&request);
6826 if (res != GST_RTSP_EINTR) {
6827 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6828 ("Could not send message. (%s)", str));
6830 GST_WARNING_OBJECT (src, "PLAY interrupted");
6837 static GstRTSPResult
6838 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
6840 GstRTSPResult res = GST_RTSP_OK;
6841 GstRTSPMessage request = { 0 };
6842 GstRTSPMessage response = { 0 };
6844 const gchar *control;
6846 GST_DEBUG_OBJECT (src, "PAUSE...");
6848 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6851 if (!(src->methods & GST_RTSP_PAUSE))
6854 if (src->state == GST_RTSP_STATE_READY)
6857 if (!src->conninfo.connection || !src->conninfo.connected)
6860 /* construct a control url */
6861 control = get_aggregate_control (src);
6863 /* loop over the streams. We might exit the loop early when we could do an
6864 * aggregate control */
6865 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6866 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6867 GstRTSPConnection *conn;
6868 const gchar *setup_url;
6870 /* try aggregate control first but do non-aggregate control otherwise */
6872 setup_url = control;
6873 else if ((setup_url = stream->conninfo.location) == NULL)
6876 if (src->conninfo.connection) {
6877 conn = src->conninfo.connection;
6878 } else if (stream->conninfo.connection) {
6879 conn = stream->conninfo.connection;
6885 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
6886 ("Sending PAUSE request"));
6889 gst_rtsp_message_init_request (&request, GST_RTSP_PAUSE,
6891 goto create_request_failed;
6893 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6896 gst_rtsp_message_unset (&request);
6897 gst_rtsp_message_unset (&response);
6899 /* exit early when we did agregate control */
6904 /* change element states now */
6905 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
6908 src->state = GST_RTSP_STATE_READY;
6912 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
6919 GST_DEBUG_OBJECT (src, "failed to open stream");
6924 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
6929 GST_DEBUG_OBJECT (src, "we were already PAUSED");
6932 create_request_failed:
6934 gchar *str = gst_rtsp_strresult (res);
6936 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6937 ("Could not create request. (%s)", str));
6943 gchar *str = gst_rtsp_strresult (res);
6945 gst_rtsp_message_unset (&request);
6946 if (res != GST_RTSP_EINTR) {
6947 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6948 ("Could not send message. (%s)", str));
6950 GST_WARNING_OBJECT (src, "PAUSE interrupted");
6958 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
6960 GstRTSPSrc *rtspsrc;
6962 rtspsrc = GST_RTSPSRC (bin);
6964 switch (GST_MESSAGE_TYPE (message)) {
6965 case GST_MESSAGE_EOS:
6966 gst_message_unref (message);
6968 case GST_MESSAGE_ELEMENT:
6970 const GstStructure *s = gst_message_get_structure (message);
6972 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
6973 gboolean ignore_timeout;
6975 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
6977 GST_OBJECT_LOCK (rtspsrc);
6978 ignore_timeout = rtspsrc->ignore_timeout;
6979 rtspsrc->ignore_timeout = TRUE;
6980 GST_OBJECT_UNLOCK (rtspsrc);
6982 /* we only act on the first udp timeout message, others are irrelevant
6983 * and can be ignored. */
6984 if (!ignore_timeout)
6985 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
6987 gst_message_unref (message);
6990 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6993 case GST_MESSAGE_ERROR:
6996 GstRTSPStream *stream;
6999 udpsrc = GST_MESSAGE_SRC (message);
7001 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
7002 GST_ELEMENT_NAME (udpsrc));
7004 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
7008 /* we ignore the RTCP udpsrc */
7009 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
7012 /* if we get error messages from the udp sources, that's not a problem as
7013 * long as not all of them error out. We also don't really know what the
7014 * problem is, the message does not give enough detail... */
7015 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
7016 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
7017 if (ret != GST_FLOW_OK)
7021 gst_message_unref (message);
7025 /* fatal but not our message, forward */
7026 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7031 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7037 /* the thread where everything happens */
7039 gst_rtspsrc_thread (GstRTSPSrc * src)
7043 GST_OBJECT_LOCK (src);
7044 cmd = src->pending_cmd;
7045 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
7046 || cmd == CMD_LOOP || cmd == CMD_OPEN)
7047 src->pending_cmd = CMD_LOOP;
7049 src->pending_cmd = CMD_WAIT;
7050 GST_DEBUG_OBJECT (src, "got command %d", cmd);
7052 /* we got the message command, so ensure communication is possible again */
7053 gst_rtspsrc_connection_flush (src, FALSE);
7055 src->busy_cmd = cmd;
7056 GST_OBJECT_UNLOCK (src);
7060 gst_rtspsrc_open (src, TRUE);
7063 gst_rtspsrc_play (src, &src->segment, TRUE);
7066 gst_rtspsrc_pause (src, TRUE);
7069 gst_rtspsrc_close (src, TRUE, FALSE);
7072 gst_rtspsrc_loop (src);
7075 gst_rtspsrc_reconnect (src, FALSE);
7081 GST_OBJECT_LOCK (src);
7082 /* and go back to sleep */
7083 if (src->pending_cmd == CMD_WAIT) {
7085 gst_task_pause (src->task);
7088 src->busy_cmd = CMD_WAIT;
7089 GST_OBJECT_UNLOCK (src);
7093 gst_rtspsrc_start (GstRTSPSrc * src)
7095 GST_DEBUG_OBJECT (src, "starting");
7097 GST_OBJECT_LOCK (src);
7099 src->pending_cmd = CMD_WAIT;
7101 if (src->task == NULL) {
7102 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
7103 if (src->task == NULL)
7106 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
7108 GST_OBJECT_UNLOCK (src);
7115 GST_ERROR_OBJECT (src, "failed to create task");
7121 gst_rtspsrc_stop (GstRTSPSrc * src)
7125 GST_DEBUG_OBJECT (src, "stopping");
7127 /* also cancels pending task */
7128 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
7130 GST_OBJECT_LOCK (src);
7131 if ((task = src->task)) {
7133 GST_OBJECT_UNLOCK (src);
7135 gst_task_stop (task);
7137 /* make sure it is not running */
7138 GST_RTSP_STREAM_LOCK (src);
7139 GST_RTSP_STREAM_UNLOCK (src);
7141 /* now wait for the task to finish */
7142 gst_task_join (task);
7144 /* and free the task */
7145 gst_object_unref (GST_OBJECT (task));
7147 GST_OBJECT_LOCK (src);
7149 GST_OBJECT_UNLOCK (src);
7151 /* ensure synchronously all is closed and clean */
7152 gst_rtspsrc_close (src, FALSE, TRUE);
7157 static GstStateChangeReturn
7158 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
7160 GstRTSPSrc *rtspsrc;
7161 GstStateChangeReturn ret;
7163 rtspsrc = GST_RTSPSRC (element);
7165 switch (transition) {
7166 case GST_STATE_CHANGE_NULL_TO_READY:
7167 if (!gst_rtspsrc_start (rtspsrc))
7170 case GST_STATE_CHANGE_READY_TO_PAUSED:
7171 /* init some state */
7172 rtspsrc->cur_protocols = rtspsrc->protocols;
7173 /* first attempt, don't ignore timeouts */
7174 rtspsrc->ignore_timeout = FALSE;
7175 rtspsrc->open_error = FALSE;
7176 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
7178 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7179 set_manager_buffer_mode (rtspsrc);
7181 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7182 /* unblock the tcp tasks and make the loop waiting */
7183 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
7184 /* make sure it is waiting before we send PAUSE or PLAY below */
7185 GST_RTSP_STREAM_LOCK (rtspsrc);
7186 GST_RTSP_STREAM_UNLOCK (rtspsrc);
7189 case GST_STATE_CHANGE_PAUSED_TO_READY:
7195 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
7196 if (ret == GST_STATE_CHANGE_FAILURE)
7199 switch (transition) {
7200 case GST_STATE_CHANGE_NULL_TO_READY:
7201 ret = GST_STATE_CHANGE_SUCCESS;
7203 case GST_STATE_CHANGE_READY_TO_PAUSED:
7204 ret = GST_STATE_CHANGE_NO_PREROLL;
7206 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7207 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
7208 ret = GST_STATE_CHANGE_SUCCESS;
7210 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7211 /* send pause request and keep the idle task around */
7212 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
7213 ret = GST_STATE_CHANGE_NO_PREROLL;
7215 case GST_STATE_CHANGE_PAUSED_TO_READY:
7216 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_PAUSE);
7217 ret = GST_STATE_CHANGE_SUCCESS;
7219 case GST_STATE_CHANGE_READY_TO_NULL:
7220 gst_rtspsrc_stop (rtspsrc);
7221 ret = GST_STATE_CHANGE_SUCCESS;
7232 GST_DEBUG_OBJECT (rtspsrc, "start failed");
7233 return GST_STATE_CHANGE_FAILURE;
7238 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
7241 GstRTSPSrc *rtspsrc;
7243 rtspsrc = GST_RTSPSRC (element);
7245 if (GST_EVENT_IS_DOWNSTREAM (event)) {
7246 res = gst_rtspsrc_push_event (rtspsrc, event);
7248 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
7255 /*** GSTURIHANDLER INTERFACE *************************************************/
7258 gst_rtspsrc_uri_get_type (GType type)
7263 static const gchar *const *
7264 gst_rtspsrc_uri_get_protocols (GType type)
7266 static const gchar *protocols[] =
7267 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
7268 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
7275 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
7277 GstRTSPSrc *src = GST_RTSPSRC (handler);
7279 /* FIXME: make thread-safe */
7280 return g_strdup (src->conninfo.location);
7284 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
7289 GstRTSPUrl *newurl = NULL;
7290 GstSDPMessage *sdp = NULL;
7292 src = GST_RTSPSRC (handler);
7294 /* same URI, we're fine */
7295 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
7298 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
7299 if ((res = gst_sdp_message_new (&sdp) < 0))
7302 GST_DEBUG_OBJECT (src, "parsing SDP message");
7303 if ((res = gst_sdp_message_parse_uri (uri, sdp) < 0))
7307 GST_DEBUG_OBJECT (src, "parsing URI");
7308 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
7312 /* if worked, free previous and store new url object along with the original
7314 GST_DEBUG_OBJECT (src, "configuring URI");
7315 g_free (src->conninfo.location);
7316 src->conninfo.location = g_strdup (uri);
7317 gst_rtsp_url_free (src->conninfo.url);
7318 src->conninfo.url = newurl;
7319 g_free (src->conninfo.url_str);
7321 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
7323 src->conninfo.url_str = NULL;
7326 gst_sdp_message_free (src->sdp);
7328 src->from_sdp = sdp != NULL;
7330 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
7331 GST_DEBUG_OBJECT (src, "request uri is: %s",
7332 GST_STR_NULL (src->conninfo.url_str));
7339 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
7344 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", res);
7345 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7346 "Could not create SDP");
7351 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", res,
7352 GST_STR_NULL (uri));
7353 gst_sdp_message_free (sdp);
7354 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7360 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
7361 GST_STR_NULL (uri), res);
7362 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7363 "Invalid RTSP URI");
7369 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
7371 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
7373 iface->get_type = gst_rtspsrc_uri_get_type;
7374 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
7375 iface->get_uri = gst_rtspsrc_uri_get_uri;
7376 iface->set_uri = gst_rtspsrc_uri_set_uri;