2 * Copyright (C) <2005,2006> Wim Taymans <wim at fluendo dot com>
3 * <2006> Lutz Mueller <lutz at topfrose dot de>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 * Unless otherwise indicated, Source Code is licensed under MIT license.
22 * See further explanation attached in License Statement (distributed in the file
25 * Permission is hereby granted, free of charge, to any person obtaining a copy of
26 * this software and associated documentation files (the "Software"), to deal in
27 * the Software without restriction, including without limitation the rights to
28 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
29 * of the Software, and to permit persons to whom the Software is furnished to do
30 * so, subject to the following conditions:
32 * The above copyright notice and this permission notice shall be included in all
33 * copies or substantial portions of the Software.
35 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
44 * SECTION:element-rtspsrc
46 * Makes a connection to an RTSP server and read the data.
47 * rtspsrc strictly follows RFC 2326 and therefore does not (yet) support
48 * RealMedia/Quicktime/Microsoft extensions.
50 * RTSP supports transport over TCP or UDP in unicast or multicast mode. By
51 * default rtspsrc will negotiate a connection in the following order:
52 * UDP unicast/UDP multicast/TCP. The order cannot be changed but the allowed
53 * protocols can be controlled with the #GstRTSPSrc:protocols property.
55 * rtspsrc currently understands SDP as the format of the session description.
56 * For each stream listed in the SDP a new rtp_stream%d pad will be created
57 * with caps derived from the SDP media description. This is a caps of mime type
58 * "application/x-rtp" that can be connected to any available RTP depayloader
61 * rtspsrc will internally instantiate an RTP session manager element
62 * that will handle the RTCP messages to and from the server, jitter removal,
63 * packet reordering along with providing a clock for the pipeline.
64 * This feature is implemented using the gstrtpbin element.
66 * rtspsrc acts like a live source and will therefore only generate data in the
70 * <title>Example launch line</title>
72 * gst-launch-1.0 rtspsrc location=rtsp://some.server/url ! fakesink
73 * ]| Establish a connection to an RTSP server and send the raw RTP packets to a
84 #endif /* HAVE_UNISTD_H */
90 #include <gst/net/gstnet.h>
91 #include <gst/sdp/gstsdpmessage.h>
92 #include <gst/sdp/gstmikey.h>
93 #include <gst/rtp/gstrtppayloads.h>
95 #include "gst/gst-i18n-plugin.h"
97 #include "gstrtspsrc.h"
99 GST_DEBUG_CATEGORY_STATIC (rtspsrc_debug);
100 #define GST_CAT_DEFAULT (rtspsrc_debug)
102 static GstStaticPadTemplate rtptemplate = GST_STATIC_PAD_TEMPLATE ("stream_%u",
105 GST_STATIC_CAPS ("application/x-rtp; application/x-rdt"));
107 /* templates used internally */
108 static GstStaticPadTemplate anysrctemplate =
109 GST_STATIC_PAD_TEMPLATE ("internalsrc_%u",
112 GST_STATIC_CAPS_ANY);
114 static GstStaticPadTemplate anysinktemplate =
115 GST_STATIC_PAD_TEMPLATE ("internalsink_%u",
118 GST_STATIC_CAPS_ANY);
122 SIGNAL_HANDLE_REQUEST,
124 SIGNAL_SELECT_STREAM,
126 SIGNAL_REQUEST_RTCP_KEY,
130 enum _GstRtspSrcRtcpSyncMode
137 enum _GstRtspSrcBufferMode
146 #define GST_TYPE_RTSP_SRC_BUFFER_MODE (gst_rtsp_src_buffer_mode_get_type())
148 gst_rtsp_src_buffer_mode_get_type (void)
150 static GType buffer_mode_type = 0;
151 static const GEnumValue buffer_modes[] = {
152 {BUFFER_MODE_NONE, "Only use RTP timestamps", "none"},
153 {BUFFER_MODE_SLAVE, "Slave receiver to sender clock", "slave"},
154 {BUFFER_MODE_BUFFER, "Do low/high watermark buffering", "buffer"},
155 {BUFFER_MODE_AUTO, "Choose mode depending on stream live", "auto"},
156 {BUFFER_MODE_SYNCED, "Synchronized sender and receiver clocks", "synced"},
160 if (!buffer_mode_type) {
162 g_enum_register_static ("GstRTSPSrcBufferMode", buffer_modes);
164 return buffer_mode_type;
167 #define AES_128_KEY_LEN 16
168 #define AES_256_KEY_LEN 32
170 #define HMAC_32_KEY_LEN 4
171 #define HMAC_80_KEY_LEN 10
173 #define DEFAULT_LOCATION NULL
174 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
175 #define DEFAULT_DEBUG FALSE
176 #define DEFAULT_RETRY 20
177 #define DEFAULT_TIMEOUT 5000000
178 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
179 #define DEFAULT_TCP_TIMEOUT 20000000
180 #define DEFAULT_LATENCY_MS 2000
181 #define DEFAULT_DROP_ON_LATENCY FALSE
182 #define DEFAULT_CONNECTION_SPEED 0
183 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
184 #define DEFAULT_DO_RTCP TRUE
185 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
186 #define DEFAULT_PROXY NULL
187 #define DEFAULT_RTP_BLOCKSIZE 0
188 #define DEFAULT_USER_ID NULL
189 #define DEFAULT_USER_PW NULL
190 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
191 #define DEFAULT_PORT_RANGE NULL
192 #define DEFAULT_SHORT_HEADER FALSE
193 #define DEFAULT_PROBATION 2
194 #define DEFAULT_UDP_RECONNECT TRUE
195 #define DEFAULT_MULTICAST_IFACE NULL
196 #define DEFAULT_NTP_SYNC FALSE
197 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
198 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
199 #define DEFAULT_TLS_DATABASE NULL
200 #define DEFAULT_DO_RETRANSMISSION TRUE
212 PROP_DROP_ON_LATENCY,
213 PROP_CONNECTION_SPEED,
216 PROP_DO_RTSP_KEEP_ALIVE,
225 PROP_UDP_BUFFER_SIZE,
229 PROP_MULTICAST_IFACE,
231 PROP_USE_PIPELINE_CLOCK,
233 PROP_TLS_VALIDATION_FLAGS,
235 PROP_DO_RETRANSMISSION,
239 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
241 gst_rtsp_nat_method_get_type (void)
243 static GType rtsp_nat_method_type = 0;
244 static const GEnumValue rtsp_nat_method[] = {
245 {GST_RTSP_NAT_NONE, "None", "none"},
246 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
250 if (!rtsp_nat_method_type) {
251 rtsp_nat_method_type =
252 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
254 return rtsp_nat_method_type;
257 static void gst_rtspsrc_finalize (GObject * object);
259 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
260 const GValue * value, GParamSpec * pspec);
261 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
262 GValue * value, GParamSpec * pspec);
264 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
266 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
267 gpointer iface_data);
269 static void gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes,
272 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
273 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
275 static GstCaps *gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media);
277 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
278 GstStateChange transition);
279 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
280 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
282 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
283 GstRTSPMessage * response);
285 static gboolean gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
287 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
288 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
290 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
291 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
293 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
294 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
295 gboolean only_close);
297 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
298 const gchar * uri, GError ** error);
299 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
301 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
302 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
303 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
304 GstRTSPStream * stream, GstEvent * event);
305 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
306 static void gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush);
314 /* commands we send to out loop to notify it of events */
315 #define CMD_OPEN (1 << 0)
316 #define CMD_PLAY (1 << 1)
317 #define CMD_PAUSE (1 << 2)
318 #define CMD_CLOSE (1 << 3)
319 #define CMD_WAIT (1 << 4)
320 #define CMD_RECONNECT (1 << 5)
321 #define CMD_LOOP (1 << 6)
323 /* mask for all commands */
324 #define CMD_ALL ((CMD_LOOP << 1) - 1)
326 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
328 gchar *__txt = _gst_element_error_printf text; \
329 gst_element_post_message (GST_ELEMENT_CAST (el), \
330 gst_message_new_progress (GST_OBJECT_CAST (el), \
331 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
335 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
337 #define gst_rtspsrc_parent_class parent_class
338 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
339 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
342 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
344 GST_DEBUG_OBJECT (src, "default handler");
349 select_stream_accum (GSignalInvocationHint * ihint,
350 GValue * return_accu, const GValue * handler_return, gpointer data)
354 myboolean = g_value_get_boolean (handler_return);
355 GST_DEBUG ("accum %d", myboolean);
356 g_value_set_boolean (return_accu, myboolean);
358 /* stop emission if FALSE */
363 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
365 GObjectClass *gobject_class;
366 GstElementClass *gstelement_class;
367 GstBinClass *gstbin_class;
369 gobject_class = (GObjectClass *) klass;
370 gstelement_class = (GstElementClass *) klass;
371 gstbin_class = (GstBinClass *) klass;
373 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
375 gobject_class->set_property = gst_rtspsrc_set_property;
376 gobject_class->get_property = gst_rtspsrc_get_property;
378 gobject_class->finalize = gst_rtspsrc_finalize;
380 g_object_class_install_property (gobject_class, PROP_LOCATION,
381 g_param_spec_string ("location", "RTSP Location",
382 "Location of the RTSP url to read",
383 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
385 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
386 g_param_spec_flags ("protocols", "Protocols",
387 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
388 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
390 g_object_class_install_property (gobject_class, PROP_DEBUG,
391 g_param_spec_boolean ("debug", "Debug",
392 "Dump request and response messages to stdout",
393 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
395 g_object_class_install_property (gobject_class, PROP_RETRY,
396 g_param_spec_uint ("retry", "Retry",
397 "Max number of retries when allocating RTP ports.",
398 0, G_MAXUINT16, DEFAULT_RETRY,
399 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
401 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
402 g_param_spec_uint64 ("timeout", "Timeout",
403 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
404 0, G_MAXUINT64, DEFAULT_TIMEOUT,
405 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
407 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
408 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
409 "Fail after timeout microseconds on TCP connections (0 = disabled)",
410 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
411 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
413 g_object_class_install_property (gobject_class, PROP_LATENCY,
414 g_param_spec_uint ("latency", "Buffer latency in ms",
415 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
416 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
418 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
419 g_param_spec_boolean ("drop-on-latency",
420 "Drop buffers when maximum latency is reached",
421 "Tells the jitterbuffer to never exceed the given latency in size",
422 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
424 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
425 g_param_spec_uint64 ("connection-speed", "Connection Speed",
426 "Network connection speed in kbps (0 = unknown)",
427 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
428 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
430 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
431 g_param_spec_enum ("nat-method", "NAT Method",
432 "Method to use for traversing firewalls and NAT",
433 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
434 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
437 * GstRTSPSrc:do-rtcp:
439 * Enable RTCP support. Some old server don't like RTCP and then this property
440 * needs to be set to FALSE.
442 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
443 g_param_spec_boolean ("do-rtcp", "Do RTCP",
444 "Send RTCP packets, disable for old incompatible server.",
445 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
448 * GstRTSPSrc:do-rtsp-keep-alive:
450 * Enable RTSP keep alive support. Some old server don't like RTSP
451 * keep alive and then this property needs to be set to FALSE.
453 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
454 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
455 "Send RTSP keep alive packets, disable for old incompatible server.",
456 DEFAULT_DO_RTSP_KEEP_ALIVE,
457 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
462 * Set the proxy parameters. This has to be a string of the format
463 * [http://][user:passwd@]host[:port].
465 g_object_class_install_property (gobject_class, PROP_PROXY,
466 g_param_spec_string ("proxy", "Proxy",
467 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
468 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
470 * GstRTSPSrc:proxy-id:
472 * Sets the proxy URI user id for authentication. If the URI set via the
473 * "proxy" property contains a user-id already, that will take precedence.
477 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
478 g_param_spec_string ("proxy-id", "proxy-id",
479 "HTTP proxy URI user id for authentication", "",
480 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
482 * GstRTSPSrc:proxy-pw:
484 * Sets the proxy URI password for authentication. If the URI set via the
485 * "proxy" property contains a password already, that will take precedence.
489 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
490 g_param_spec_string ("proxy-pw", "proxy-pw",
491 "HTTP proxy URI user password for authentication", "",
492 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
495 * GstRTSPSrc:rtp-blocksize:
497 * RTP package size to suggest to server.
499 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
500 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
501 "RTP package size to suggest to server (0 = disabled)",
502 0, 65536, DEFAULT_RTP_BLOCKSIZE,
503 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
505 g_object_class_install_property (gobject_class,
507 g_param_spec_string ("user-id", "user-id",
508 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
509 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
510 g_object_class_install_property (gobject_class, PROP_USER_PW,
511 g_param_spec_string ("user-pw", "user-pw",
512 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
513 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
516 * GstRTSPSrc:buffer-mode:
518 * Control the buffering and timestamping mode used by the jitterbuffer.
520 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
521 g_param_spec_enum ("buffer-mode", "Buffer Mode",
522 "Control the buffering algorithm in use",
523 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
524 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
527 * GstRTSPSrc:port-range:
529 * Configure the client port numbers that can be used to recieve RTP and
532 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
533 g_param_spec_string ("port-range", "Port range",
534 "Client port range that can be used to receive RTP and RTCP data, "
535 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
536 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
539 * GstRTSPSrc:udp-buffer-size:
541 * Size of the kernel UDP receive buffer in bytes.
543 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
544 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
545 "Size of the kernel UDP receive buffer in bytes, 0=default",
546 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
547 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
550 * GstRTSPSrc:short-header:
552 * Only send the basic RTSP headers for broken encoders.
554 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
555 g_param_spec_boolean ("short-header", "Short Header",
556 "Only send the basic RTSP headers for broken encoders",
557 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
559 g_object_class_install_property (gobject_class, PROP_PROBATION,
560 g_param_spec_uint ("probation", "Number of probations",
561 "Consecutive packet sequence numbers to accept the source",
562 0, G_MAXUINT, DEFAULT_PROBATION,
563 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
565 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
566 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
567 "Reconnect to the server if RTSP connection is closed when doing UDP",
568 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
570 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
571 g_param_spec_string ("multicast-iface", "Multicast Interface",
572 "The network interface on which to join the multicast group",
573 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
575 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
576 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
577 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
578 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
580 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
581 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
582 "Use the pipeline running-time to set the NTP time in the RTCP SR messages",
583 DEFAULT_USE_PIPELINE_CLOCK,
584 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
586 g_object_class_install_property (gobject_class, PROP_SDES,
587 g_param_spec_boxed ("sdes", "SDES",
588 "The SDES items of this session",
589 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
592 * GstRTSPSrc::tls-validation-flags:
594 * TLS certificate validation flags used to validate server
599 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
600 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
601 "TLS certificate validation flags used to validate the server certificate",
602 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
603 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
606 * GstRTSPSrc::tls-database:
608 * TLS database with anchor certificate authorities used to validate
609 * the server certificate.
613 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
614 g_param_spec_object ("tls-database", "TLS database",
615 "TLS database with anchor certificate authorities used to validate the server certificate",
616 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
619 * GstRTSPSrc::do-retransmission:
621 * Attempt to ask the server to retransmit lost packets according to RFC4588.
623 * Note: currently only works with SSRC-multiplexed retransmission streams
627 g_object_class_install_property (gobject_class, PROP_DO_RETRANSMISSION,
628 g_param_spec_boolean ("do-retransmission", "Retransmission",
629 "Ask the server to retransmit lost packets",
630 DEFAULT_DO_RETRANSMISSION,
631 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
634 * GstRTSPSrc::handle-request:
635 * @rtspsrc: a #GstRTSPSrc
636 * @request: a #GstRTSPMessage
637 * @response: a #GstRTSPMessage
639 * Handle a server request in @request and prepare @response.
641 * This signal is called from the streaming thread, you should therefore not
642 * do any state changes on @rtspsrc because this might deadlock. If you want
643 * to modify the state as a result of this signal, post a
644 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
649 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
650 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
651 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
652 G_TYPE_POINTER, G_TYPE_POINTER);
655 * GstRTSPSrc::on-sdp:
656 * @rtspsrc: a #GstRTSPSrc
657 * @sdp: a #GstSDPMessage
659 * Emited when the client has retrieved the SDP and before it configures the
660 * streams in the SDP. @sdp can be inspected and modified.
662 * This signal is called from the streaming thread, you should therefore not
663 * do any state changes on @rtspsrc because this might deadlock. If you want
664 * to modify the state as a result of this signal, post a
665 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
670 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
671 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
672 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
673 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
676 * GstRTSPSrc::select-stream:
677 * @rtspsrc: a #GstRTSPSrc
678 * @num: the stream number
679 * @caps: the stream caps
681 * Emited before the client decides to configure the stream @num with
684 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
689 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
690 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
691 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
692 (GCallback) default_select_stream, select_stream_accum, NULL,
693 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, G_TYPE_UINT,
696 * GstRTSPSrc::new-manager:
697 * @rtspsrc: a #GstRTSPSrc
698 * @manager: a #GstElement
700 * Emited after a new manager (like rtpbin) was created and the default
701 * properties were configured.
705 gst_rtspsrc_signals[SIGNAL_NEW_MANAGER] =
706 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
707 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
708 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
711 * GstRTSPSrc::request-rtcp-key:
712 * @rtspsrc: a #GstRTSPSrc
713 * @num: the stream number
715 * Signal emited to get the crypto parameters relevant to the RTCP
716 * stream. User should provide the key and the RTCP encryption ciphers
717 * and authentication, and return them wrapped in a GstCaps.
721 gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY] =
722 g_signal_new ("request-rtcp-key", G_TYPE_FROM_CLASS (klass),
723 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, GST_TYPE_CAPS, 1, G_TYPE_UINT);
725 gstelement_class->send_event = gst_rtspsrc_send_event;
726 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
727 gstelement_class->change_state = gst_rtspsrc_change_state;
729 gst_element_class_add_pad_template (gstelement_class,
730 gst_static_pad_template_get (&rtptemplate));
732 gst_element_class_set_static_metadata (gstelement_class,
733 "RTSP packet receiver", "Source/Network",
734 "Receive data over the network via RTSP (RFC 2326)",
735 "Wim Taymans <wim@fluendo.com>, "
736 "Thijs Vermeir <thijs.vermeir@barco.com>, "
737 "Lutz Mueller <lutz@topfrose.de>");
739 gstbin_class->handle_message = gst_rtspsrc_handle_message;
741 gst_rtsp_ext_list_init ();
745 gst_rtspsrc_init (GstRTSPSrc * src)
747 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
748 src->protocols = DEFAULT_PROTOCOLS;
749 src->debug = DEFAULT_DEBUG;
750 src->retry = DEFAULT_RETRY;
751 src->udp_timeout = DEFAULT_TIMEOUT;
752 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
753 src->latency = DEFAULT_LATENCY_MS;
754 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
755 src->connection_speed = DEFAULT_CONNECTION_SPEED;
756 src->nat_method = DEFAULT_NAT_METHOD;
757 src->do_rtcp = DEFAULT_DO_RTCP;
758 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
759 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
760 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
761 src->user_id = g_strdup (DEFAULT_USER_ID);
762 src->user_pw = g_strdup (DEFAULT_USER_PW);
763 src->buffer_mode = DEFAULT_BUFFER_MODE;
764 src->client_port_range.min = 0;
765 src->client_port_range.max = 0;
766 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
767 src->short_header = DEFAULT_SHORT_HEADER;
768 src->probation = DEFAULT_PROBATION;
769 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
770 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
771 src->ntp_sync = DEFAULT_NTP_SYNC;
772 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
774 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
775 src->tls_database = DEFAULT_TLS_DATABASE;
776 src->do_retransmission = DEFAULT_DO_RETRANSMISSION;
778 /* get a list of all extensions */
779 src->extensions = gst_rtsp_ext_list_get ();
781 /* connect to send signal */
782 gst_rtsp_ext_list_connect (src->extensions, "send",
783 (GCallback) gst_rtspsrc_send_cb, src);
785 /* protects the streaming thread in interleaved mode or the polling
786 * thread in UDP mode. */
787 g_rec_mutex_init (&src->stream_rec_lock);
789 /* protects our state changes from multiple invocations */
790 g_rec_mutex_init (&src->state_rec_lock);
792 src->state = GST_RTSP_STATE_INVALID;
794 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
798 gst_rtspsrc_finalize (GObject * object)
802 rtspsrc = GST_RTSPSRC (object);
804 gst_rtsp_ext_list_free (rtspsrc->extensions);
805 g_free (rtspsrc->conninfo.location);
806 gst_rtsp_url_free (rtspsrc->conninfo.url);
807 g_free (rtspsrc->conninfo.url_str);
808 g_free (rtspsrc->user_id);
809 g_free (rtspsrc->user_pw);
810 g_free (rtspsrc->multi_iface);
813 gst_sdp_message_free (rtspsrc->sdp);
816 if (rtspsrc->provided_clock)
817 gst_object_unref (rtspsrc->provided_clock);
820 gst_structure_free (rtspsrc->sdes);
822 if (rtspsrc->tls_database)
823 g_object_unref (rtspsrc->tls_database);
826 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
827 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
829 G_OBJECT_CLASS (parent_class)->finalize (object);
833 gst_rtspsrc_provide_clock (GstElement * element)
835 GstRTSPSrc *src = GST_RTSPSRC (element);
838 if ((clock = src->provided_clock) != NULL)
839 gst_object_ref (clock);
844 /* a proxy string of the format [user:passwd@]host[:port] */
846 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
850 g_free (rtsp->proxy_user);
851 rtsp->proxy_user = NULL;
852 g_free (rtsp->proxy_passwd);
853 rtsp->proxy_passwd = NULL;
854 g_free (rtsp->proxy_host);
855 rtsp->proxy_host = NULL;
856 rtsp->proxy_port = 0;
863 /* we allow http:// in front but ignore it */
864 if (g_str_has_prefix (p, "http://"))
867 at = strchr (p, '@');
869 /* look for user:passwd */
870 col = strchr (proxy, ':');
871 if (col == NULL || col > at)
874 rtsp->proxy_user = g_strndup (p, col - p);
876 rtsp->proxy_passwd = g_strndup (col, at - col);
881 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
882 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
883 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
884 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
885 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
886 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
887 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
890 col = strchr (p, ':');
893 /* everything before the colon is the hostname */
894 rtsp->proxy_host = g_strndup (p, col - p);
896 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
898 rtsp->proxy_host = g_strdup (p);
899 rtsp->proxy_port = 8080;
905 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
907 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
908 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
911 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
913 rtspsrc->ptcp_timeout = NULL;
917 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
922 rtspsrc = GST_RTSPSRC (object);
926 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
927 g_value_get_string (value), NULL);
930 rtspsrc->protocols = g_value_get_flags (value);
933 rtspsrc->debug = g_value_get_boolean (value);
936 rtspsrc->retry = g_value_get_uint (value);
939 rtspsrc->udp_timeout = g_value_get_uint64 (value);
941 case PROP_TCP_TIMEOUT:
942 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
945 rtspsrc->latency = g_value_get_uint (value);
947 case PROP_DROP_ON_LATENCY:
948 rtspsrc->drop_on_latency = g_value_get_boolean (value);
950 case PROP_CONNECTION_SPEED:
951 rtspsrc->connection_speed = g_value_get_uint64 (value);
953 case PROP_NAT_METHOD:
954 rtspsrc->nat_method = g_value_get_enum (value);
957 rtspsrc->do_rtcp = g_value_get_boolean (value);
959 case PROP_DO_RTSP_KEEP_ALIVE:
960 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
963 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
966 if (rtspsrc->prop_proxy_id)
967 g_free (rtspsrc->prop_proxy_id);
968 rtspsrc->prop_proxy_id = g_value_dup_string (value);
971 if (rtspsrc->prop_proxy_pw)
972 g_free (rtspsrc->prop_proxy_pw);
973 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
975 case PROP_RTP_BLOCKSIZE:
976 rtspsrc->rtp_blocksize = g_value_get_uint (value);
979 if (rtspsrc->user_id)
980 g_free (rtspsrc->user_id);
981 rtspsrc->user_id = g_value_dup_string (value);
984 if (rtspsrc->user_pw)
985 g_free (rtspsrc->user_pw);
986 rtspsrc->user_pw = g_value_dup_string (value);
988 case PROP_BUFFER_MODE:
989 rtspsrc->buffer_mode = g_value_get_enum (value);
991 case PROP_PORT_RANGE:
995 str = g_value_get_string (value);
997 sscanf (str, "%u-%u",
998 &rtspsrc->client_port_range.min, &rtspsrc->client_port_range.max);
1000 rtspsrc->client_port_range.min = 0;
1001 rtspsrc->client_port_range.max = 0;
1005 case PROP_UDP_BUFFER_SIZE:
1006 rtspsrc->udp_buffer_size = g_value_get_int (value);
1008 case PROP_SHORT_HEADER:
1009 rtspsrc->short_header = g_value_get_boolean (value);
1011 case PROP_PROBATION:
1012 rtspsrc->probation = g_value_get_uint (value);
1014 case PROP_UDP_RECONNECT:
1015 rtspsrc->udp_reconnect = g_value_get_boolean (value);
1017 case PROP_MULTICAST_IFACE:
1018 g_free (rtspsrc->multi_iface);
1020 if (g_value_get_string (value) == NULL)
1021 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1023 rtspsrc->multi_iface = g_value_dup_string (value);
1026 rtspsrc->ntp_sync = g_value_get_boolean (value);
1028 case PROP_USE_PIPELINE_CLOCK:
1029 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
1032 rtspsrc->sdes = g_value_dup_boxed (value);
1034 case PROP_TLS_VALIDATION_FLAGS:
1035 rtspsrc->tls_validation_flags = g_value_get_flags (value);
1037 case PROP_TLS_DATABASE:
1038 g_clear_object (&rtspsrc->tls_database);
1039 rtspsrc->tls_database = g_value_dup_object (value);
1041 case PROP_DO_RETRANSMISSION:
1042 rtspsrc->do_retransmission = g_value_get_boolean (value);
1045 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1051 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
1054 GstRTSPSrc *rtspsrc;
1056 rtspsrc = GST_RTSPSRC (object);
1060 g_value_set_string (value, rtspsrc->conninfo.location);
1062 case PROP_PROTOCOLS:
1063 g_value_set_flags (value, rtspsrc->protocols);
1066 g_value_set_boolean (value, rtspsrc->debug);
1069 g_value_set_uint (value, rtspsrc->retry);
1072 g_value_set_uint64 (value, rtspsrc->udp_timeout);
1074 case PROP_TCP_TIMEOUT:
1078 timeout = rtspsrc->tcp_timeout.tv_sec * G_USEC_PER_SEC +
1079 rtspsrc->tcp_timeout.tv_usec;
1080 g_value_set_uint64 (value, timeout);
1084 g_value_set_uint (value, rtspsrc->latency);
1086 case PROP_DROP_ON_LATENCY:
1087 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1089 case PROP_CONNECTION_SPEED:
1090 g_value_set_uint64 (value, rtspsrc->connection_speed);
1092 case PROP_NAT_METHOD:
1093 g_value_set_enum (value, rtspsrc->nat_method);
1096 g_value_set_boolean (value, rtspsrc->do_rtcp);
1098 case PROP_DO_RTSP_KEEP_ALIVE:
1099 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1105 if (rtspsrc->proxy_host) {
1107 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1111 g_value_take_string (value, str);
1115 g_value_set_string (value, rtspsrc->prop_proxy_id);
1118 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1120 case PROP_RTP_BLOCKSIZE:
1121 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1124 g_value_set_string (value, rtspsrc->user_id);
1127 g_value_set_string (value, rtspsrc->user_pw);
1129 case PROP_BUFFER_MODE:
1130 g_value_set_enum (value, rtspsrc->buffer_mode);
1132 case PROP_PORT_RANGE:
1136 if (rtspsrc->client_port_range.min != 0) {
1137 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1138 rtspsrc->client_port_range.max);
1142 g_value_take_string (value, str);
1145 case PROP_UDP_BUFFER_SIZE:
1146 g_value_set_int (value, rtspsrc->udp_buffer_size);
1148 case PROP_SHORT_HEADER:
1149 g_value_set_boolean (value, rtspsrc->short_header);
1151 case PROP_PROBATION:
1152 g_value_set_uint (value, rtspsrc->probation);
1154 case PROP_UDP_RECONNECT:
1155 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1157 case PROP_MULTICAST_IFACE:
1158 g_value_set_string (value, rtspsrc->multi_iface);
1161 g_value_set_boolean (value, rtspsrc->ntp_sync);
1163 case PROP_USE_PIPELINE_CLOCK:
1164 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1167 g_value_set_boxed (value, rtspsrc->sdes);
1169 case PROP_TLS_VALIDATION_FLAGS:
1170 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1172 case PROP_TLS_DATABASE:
1173 g_value_set_object (value, rtspsrc->tls_database);
1175 case PROP_DO_RETRANSMISSION:
1176 g_value_set_boolean (value, rtspsrc->do_retransmission);
1179 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1185 find_stream_by_id (GstRTSPStream * stream, gint * id)
1187 if (stream->id == *id)
1194 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1196 if (stream->channel[0] == *channel || stream->channel[1] == *channel)
1203 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1205 GstElement *src = (GstElement *) a;
1207 if (stream->udpsrc[0] == src)
1209 if (stream->udpsrc[1] == src)
1216 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1218 if (stream->conninfo.location) {
1219 /* check qualified setup_url */
1220 if (!strcmp (stream->conninfo.location, (gchar *) a))
1223 if (stream->control_url) {
1224 /* check original control_url */
1225 if (!strcmp (stream->control_url, (gchar *) a))
1228 /* check if qualified setup_url ends with string */
1229 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1236 static GstRTSPStream *
1237 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1241 /* find and get stream */
1242 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1243 return (GstRTSPStream *) lstream->data;
1248 static const GstSDPBandwidth *
1249 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1250 const GstSDPMedia * media, const gchar * type)
1254 /* first look in the media specific section */
1255 len = gst_sdp_media_bandwidths_len (media);
1256 for (i = 0; i < len; i++) {
1257 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1259 if (strcmp (bw->bwtype, type) == 0)
1262 /* then look in the message specific section */
1263 len = gst_sdp_message_bandwidths_len (sdp);
1264 for (i = 0; i < len; i++) {
1265 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1267 if (strcmp (bw->bwtype, type) == 0)
1274 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1275 const GstSDPMedia * media, GstRTSPStream * stream)
1277 const GstSDPBandwidth *bw;
1279 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1280 stream->as_bandwidth = bw->bandwidth;
1282 stream->as_bandwidth = -1;
1284 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1285 stream->rr_bandwidth = bw->bandwidth;
1287 stream->rr_bandwidth = -1;
1289 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1290 stream->rs_bandwidth = bw->bandwidth;
1292 stream->rs_bandwidth = -1;
1296 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1297 const GstSDPConnection * conn)
1299 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1302 if (conn->addrtype == NULL)
1305 /* check for IPV6 */
1306 if (strcmp (conn->addrtype, "IP4") == 0)
1307 stream->is_ipv6 = FALSE;
1308 else if (strcmp (conn->addrtype, "IP6") == 0)
1309 stream->is_ipv6 = TRUE;
1314 g_free (stream->destination);
1315 stream->destination = g_strdup (conn->address);
1317 /* check for multicast */
1318 stream->is_multicast =
1319 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1321 stream->ttl = conn->ttl;
1324 /* Go over the connections for a stream.
1325 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1327 * - If we are dealing with a localhost address, we disable multicast
1330 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1331 const GstSDPMedia * media, GstRTSPStream * stream)
1333 const GstSDPConnection *conn;
1336 /* first look in the media specific section */
1337 len = gst_sdp_media_connections_len (media);
1338 for (i = 0; i < len; i++) {
1339 conn = gst_sdp_media_get_connection (media, i);
1341 gst_rtspsrc_do_stream_connection (src, stream, conn);
1343 /* then look in the message specific section */
1344 if ((conn = gst_sdp_message_get_connection (sdp))) {
1345 gst_rtspsrc_do_stream_connection (src, stream, conn);
1349 /* m=<media> <UDP port> RTP/AVP <payload>
1352 gst_rtspsrc_collect_payloads (GstRTSPSrc * src, const GstSDPMessage * sdp,
1353 const GstSDPMedia * media, GstRTSPStream * stream)
1359 proto = gst_sdp_media_get_proto (media);
1363 if (g_str_equal (proto, "RTP/AVP"))
1364 stream->profile = GST_RTSP_PROFILE_AVP;
1365 else if (g_str_equal (proto, "RTP/SAVP"))
1366 stream->profile = GST_RTSP_PROFILE_SAVP;
1367 else if (g_str_equal (proto, "RTP/AVPF"))
1368 stream->profile = GST_RTSP_PROFILE_AVPF;
1369 else if (g_str_equal (proto, "RTP/SAVPF"))
1370 stream->profile = GST_RTSP_PROFILE_SAVPF;
1374 len = gst_sdp_media_formats_len (media);
1375 for (i = 0; i < len; i++) {
1382 pt = atoi (gst_sdp_media_get_format (media, i));
1384 GST_DEBUG_OBJECT (src, " looking at %d pt: %d", i, pt);
1387 caps = gst_rtspsrc_media_to_caps (pt, media);
1389 GST_WARNING_OBJECT (src, " skipping pt %d without caps", pt);
1393 /* do some tweaks */
1394 s = gst_caps_get_structure (caps, 0);
1395 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
1396 stream->is_real = (strstr (enc, "-REAL") != NULL);
1397 if (strcmp (enc, "X-ASF-PF") == 0)
1398 stream->container = TRUE;
1400 GST_DEBUG ("mapping sdp session level attributes to caps");
1401 gst_rtspsrc_sdp_attributes_to_caps (sdp->attributes, caps);
1402 GST_DEBUG ("mapping sdp media level attributes to caps");
1403 gst_rtspsrc_sdp_attributes_to_caps (media->attributes, caps);
1405 /* the first pt will be the default */
1406 if (stream->ptmap->len == 0)
1407 stream->default_pt = pt;
1411 g_array_append_val (stream->ptmap, item);
1417 GST_ERROR_OBJECT (src, "can't find proto in media");
1422 GST_ERROR_OBJECT (src, "unknown proto in media %s", proto);
1427 static const gchar *
1428 get_aggregate_control (GstRTSPSrc * src)
1433 base = src->control;
1434 else if (src->content_base)
1435 base = src->content_base;
1436 else if (src->conninfo.url_str)
1437 base = src->conninfo.url_str;
1445 clear_ptmap_item (PtMapItem * item)
1448 gst_caps_unref (item->caps);
1451 static GstRTSPStream *
1452 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
1454 GstRTSPStream *stream;
1455 const gchar *control_url;
1456 const GstSDPMedia *media;
1458 /* get media, should not return NULL */
1459 media = gst_sdp_message_get_media (sdp, idx);
1463 stream = g_new0 (GstRTSPStream, 1);
1464 stream->parent = src;
1465 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1467 stream->last_ret = GST_FLOW_NOT_LINKED;
1468 stream->added = FALSE;
1469 stream->setup = FALSE;
1470 stream->skipped = FALSE;
1472 stream->eos = FALSE;
1473 stream->discont = TRUE;
1474 stream->seqbase = -1;
1475 stream->timebase = -1;
1476 stream->send_ssrc = g_random_int ();
1477 stream->profile = GST_RTSP_PROFILE_AVP;
1478 stream->ptmap = g_array_new (FALSE, FALSE, sizeof (PtMapItem));
1479 g_array_set_clear_func (stream->ptmap, (GDestroyNotify) clear_ptmap_item);
1481 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1482 * session manager to scale RTCP. */
1483 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1485 /* collect connection info */
1486 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1488 /* make the payload type map */
1489 gst_rtspsrc_collect_payloads (src, sdp, media, stream);
1491 /* collect port number */
1492 stream->port = gst_sdp_media_get_port (media);
1494 /* get control url to construct the setup url. The setup url is used to
1495 * configure the transport of the stream and is used to identity the stream in
1496 * the RTP-Info header field returned from PLAY. */
1497 control_url = gst_sdp_media_get_attribute_val (media, "control");
1498 if (control_url == NULL)
1499 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1501 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1502 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1503 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1504 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1506 if (control_url != NULL) {
1507 stream->control_url = g_strdup (control_url);
1508 /* Build a fully qualified url using the content_base if any or by prefixing
1509 * the original request.
1510 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1511 * likely build a URL that the server will fail to understand, this is ok,
1512 * we will fail then. */
1513 if (g_str_has_prefix (control_url, "rtsp://"))
1514 stream->conninfo.location = g_strdup (control_url);
1519 if (g_strcmp0 (control_url, "*") == 0)
1522 base = get_aggregate_control (src);
1524 /* check if the base ends or control starts with / */
1525 has_slash = g_str_has_prefix (control_url, "/");
1526 has_slash = has_slash || g_str_has_suffix (base, "/");
1528 /* concatenate the two strings, insert / when not present */
1529 stream->conninfo.location =
1530 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1533 GST_DEBUG_OBJECT (src, " setup: %s",
1534 GST_STR_NULL (stream->conninfo.location));
1536 /* we keep track of all streams */
1537 src->streams = g_list_append (src->streams, stream);
1545 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1549 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1551 g_array_free (stream->ptmap, TRUE);
1553 g_free (stream->destination);
1554 g_free (stream->control_url);
1555 g_free (stream->conninfo.location);
1557 for (i = 0; i < 2; i++) {
1558 if (stream->udpsrc[i]) {
1559 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1560 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1561 gst_object_unref (stream->udpsrc[i]);
1563 if (stream->channelpad[i])
1564 gst_object_unref (stream->channelpad[i]);
1566 if (stream->udpsink[i]) {
1567 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1568 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1569 gst_object_unref (stream->udpsink[i]);
1572 if (stream->fakesrc) {
1573 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1574 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1575 gst_object_unref (stream->fakesrc);
1577 if (stream->srcpad) {
1578 gst_pad_set_active (stream->srcpad, FALSE);
1580 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1582 if (stream->srtpenc)
1583 gst_object_unref (stream->srtpenc);
1584 if (stream->srtpdec)
1585 gst_object_unref (stream->srtpdec);
1586 if (stream->srtcpparams)
1587 gst_caps_unref (stream->srtcpparams);
1588 if (stream->rtcppad)
1589 gst_object_unref (stream->rtcppad);
1590 if (stream->session)
1591 g_object_unref (stream->session);
1592 if (stream->rtx_pt_map)
1593 gst_structure_free (stream->rtx_pt_map);
1598 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1602 GST_DEBUG_OBJECT (src, "cleanup");
1604 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1605 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1607 gst_rtspsrc_stream_free (src, stream);
1609 g_list_free (src->streams);
1610 src->streams = NULL;
1612 if (src->manager_sig_id) {
1613 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1614 src->manager_sig_id = 0;
1616 gst_element_set_state (src->manager, GST_STATE_NULL);
1617 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1618 src->manager = NULL;
1621 gst_structure_free (src->props);
1624 g_free (src->content_base);
1625 src->content_base = NULL;
1627 g_free (src->control);
1628 src->control = NULL;
1631 gst_rtsp_range_free (src->range);
1634 /* don't clear the SDP when it was used in the url */
1635 if (src->sdp && !src->from_sdp) {
1636 gst_sdp_message_free (src->sdp);
1639 if (src->start_segment) {
1640 gst_event_unref (src->start_segment);
1641 src->start_segment = NULL;
1643 if (src->provided_clock) {
1644 gst_object_unref (src->provided_clock);
1645 src->provided_clock = NULL;
1649 #define PARSE_INT(p, del, res) \
1652 p = strstr (p, del); \
1662 #define PARSE_STRING(p, del, res) \
1665 p = strstr (p, del); \
1677 #define SKIP_SPACES(p) \
1678 while (*p && g_ascii_isspace (*p)) \
1683 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1686 gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
1687 gint * rate, gchar ** params)
1691 p = (gchar *) rtpmap;
1693 PARSE_INT (p, " ", *payload);
1701 PARSE_STRING (p, "/", *name);
1702 if (*name == NULL) {
1703 GST_DEBUG ("no rate, name %s", p);
1704 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
1705 * streams seem to omit the rate. */
1712 p = strstr (p, "/");
1730 parse_keymgmt (const gchar * keymgmt, GstCaps * caps)
1732 gboolean res = FALSE;
1736 GstMIKEYMessage *msg;
1737 const GstMIKEYPayload *payload;
1738 const gchar *srtp_cipher;
1739 const gchar *srtp_auth;
1741 p = (gchar *) keymgmt;
1747 PARSE_STRING (p, " ", kmpid);
1748 if (!g_str_equal (kmpid, "mikey"))
1751 data = g_base64_decode (p, &size);
1755 msg = gst_mikey_message_new_from_data (data, size, NULL, NULL);
1760 srtp_cipher = "aes-128-icm";
1761 srtp_auth = "hmac-sha1-80";
1763 /* check the Security policy if any */
1764 if ((payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, 0))) {
1765 GstMIKEYPayloadSP *p = (GstMIKEYPayloadSP *) payload;
1768 if (p->proto != GST_MIKEY_SEC_PROTO_SRTP)
1771 len = gst_mikey_payload_sp_get_n_params (payload);
1772 for (i = 0; i < len; i++) {
1773 const GstMIKEYPayloadSPParam *param =
1774 gst_mikey_payload_sp_get_param (payload, i);
1776 switch (param->type) {
1777 case GST_MIKEY_SP_SRTP_ENC_ALG:
1778 switch (param->val[0]) {
1780 srtp_cipher = "null";
1784 srtp_cipher = "aes-128-icm";
1790 case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
1791 switch (param->val[0]) {
1792 case AES_128_KEY_LEN:
1793 srtp_cipher = "aes-128-icm";
1795 case AES_256_KEY_LEN:
1796 srtp_cipher = "aes-256-icm";
1802 case GST_MIKEY_SP_SRTP_AUTH_ALG:
1803 switch (param->val[0]) {
1809 srtp_auth = "hmac-sha1-80";
1815 case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
1816 switch (param->val[0]) {
1817 case HMAC_32_KEY_LEN:
1818 srtp_auth = "hmac-sha1-32";
1820 case HMAC_80_KEY_LEN:
1821 srtp_auth = "hmac-sha1-80";
1827 case GST_MIKEY_SP_SRTP_SRTP_ENC:
1829 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
1837 if (!(payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_KEMAC, 0)))
1840 GstMIKEYPayloadKEMAC *p = (GstMIKEYPayloadKEMAC *) payload;
1841 const GstMIKEYPayload *sub;
1842 GstMIKEYPayloadKeyData *pkd;
1845 if (p->enc_alg != GST_MIKEY_ENC_NULL || p->mac_alg != GST_MIKEY_MAC_NULL)
1848 if (!(sub = gst_mikey_payload_kemac_get_sub (payload, 0)))
1851 if (sub->type != GST_MIKEY_PT_KEY_DATA)
1854 pkd = (GstMIKEYPayloadKeyData *) sub;
1856 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
1858 gst_caps_set_simple (caps, "srtp-key", GST_TYPE_BUFFER, buf, NULL);
1861 gst_caps_set_simple (caps,
1862 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
1863 "srtp-auth", G_TYPE_STRING, srtp_auth,
1864 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
1865 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
1869 gst_mikey_message_unref (msg);
1875 * Mapping SDP attributes to caps
1877 * prepend 'a-' to IANA registered sdp attributes names
1878 * (ie: not prefixed with 'x-') in order to avoid
1879 * collision with gstreamer standard caps properties names
1882 gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
1884 if (attributes->len > 0) {
1888 s = gst_caps_get_structure (caps, 0);
1890 for (i = 0; i < attributes->len; i++) {
1891 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
1892 gchar *tofree, *key;
1896 /* skip some of the attribute we already handle */
1897 if (!strcmp (key, "fmtp"))
1899 if (!strcmp (key, "rtpmap"))
1901 if (!strcmp (key, "control"))
1903 if (!strcmp (key, "range"))
1905 if (g_str_equal (key, "key-mgmt")) {
1906 parse_keymgmt (attr->value, caps);
1910 /* string must be valid UTF8 */
1911 if (!g_utf8_validate (attr->value, -1, NULL))
1914 if (!g_str_has_prefix (key, "x-"))
1915 tofree = key = g_strdup_printf ("a-%s", key);
1919 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
1920 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
1926 static const gchar *
1927 rtsp_get_attribute_for_pt (const GstSDPMedia * media, const gchar * name,
1936 if ((attr = gst_sdp_media_get_attribute_val_n (media, name, i)) == NULL)
1939 if (sscanf (attr, "%d ", &val) != 1)
1949 * Mapping of caps to and from SDP fields:
1951 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1952 * a=fmtp:<payload> <param>[=<value>];...
1955 gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
1958 const gchar *rtpmap;
1962 gchar *params = NULL;
1968 /* get and parse rtpmap */
1969 rtpmap = rtsp_get_attribute_for_pt (media, "rtpmap", pt);
1972 ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
1974 g_warning ("error parsing rtpmap, ignoring");
1978 /* dynamic payloads need rtpmap or we fail */
1979 if (rtpmap == NULL && pt >= 96)
1982 /* check if we have a rate, if not, we need to look up the rate from the
1983 * default rates based on the payload types. */
1985 const GstRTPPayloadInfo *info;
1987 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
1988 /* dynamic types, use media and encoding_name */
1989 tmp = g_ascii_strdown (media->media, -1);
1990 info = gst_rtp_payload_info_for_name (tmp, name);
1993 /* static types, use payload type */
1994 info = gst_rtp_payload_info_for_pt (pt);
1998 if ((rate = info->clock_rate) == 0)
2001 /* we fail if we cannot find one */
2006 tmp = g_ascii_strdown (media->media, -1);
2007 caps = gst_caps_new_simple ("application/x-unknown",
2008 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
2010 s = gst_caps_get_structure (caps, 0);
2012 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
2014 /* encoding name must be upper case */
2016 tmp = g_ascii_strup (name, -1);
2017 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
2021 /* params must be lower case */
2022 if (params != NULL) {
2023 tmp = g_ascii_strdown (params, -1);
2024 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
2028 /* parse optional fmtp: field */
2029 if ((fmtp = rtsp_get_attribute_for_pt (media, "fmtp", pt))) {
2035 /* p is now of the format <payload> <param>[=<value>];... */
2036 PARSE_INT (p, " ", payload);
2037 if (payload != -1 && payload == pt) {
2041 /* <param>[=<value>] are separated with ';' */
2042 pairs = g_strsplit (p, ";", 0);
2043 for (i = 0; pairs[i]; i++) {
2045 const gchar *val, *key;
2047 /* the key may not have a '=', the value can have other '='s */
2048 valpos = strstr (pairs[i], "=");
2050 /* we have a '=' and thus a value, remove the '=' with \0 */
2052 /* value is everything between '=' and ';'. We split the pairs at ;
2053 * boundaries so we can take the remainder of the value. Some servers
2054 * put spaces around the value which we strip off here. Alternatively
2055 * we could strip those spaces in the depayloaders should these spaces
2056 * actually carry any meaning in the future. */
2057 val = g_strstrip (valpos + 1);
2059 /* simple <param>;.. is translated into <param>=1;... */
2062 /* strip the key of spaces, convert key to lowercase but not the value. */
2063 key = g_strstrip (pairs[i]);
2064 if (strlen (key) > 1) {
2065 tmp = g_ascii_strdown (key, -1);
2066 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
2078 g_warning ("rtpmap type not given for dynamic payload %d", pt);
2083 g_warning ("rate unknown for payload type %d", pt);
2089 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
2090 gint * rtpport, gint * rtcpport)
2093 GstStateChangeReturn ret;
2094 GstElement *udpsrc0, *udpsrc1;
2095 gint tmp_rtp, tmp_rtcp;
2099 src = stream->parent;
2105 /* Start at next port */
2106 tmp_rtp = src->next_port_num;
2108 if (stream->is_ipv6)
2109 host = "udp://[::0]";
2111 host = "udp://0.0.0.0";
2113 /* try to allocate 2 UDP ports, the RTP port should be an even
2114 * number and the RTCP port should be the next (uneven) port */
2117 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
2118 tmp_rtp >= src->client_port_range.max)
2121 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2122 if (udpsrc0 == NULL)
2123 goto no_udp_protocol;
2124 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
2126 if (src->udp_buffer_size != 0)
2127 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
2130 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
2131 if (ret == GST_STATE_CHANGE_FAILURE) {
2133 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
2136 if (++count > src->retry)
2139 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2140 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2141 gst_object_unref (udpsrc0);
2144 GST_DEBUG_OBJECT (src, "retry %d", count);
2147 goto no_udp_protocol;
2150 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
2151 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
2153 /* check if port is even */
2154 if ((tmp_rtp & 0x01) != 0) {
2155 /* port not even, close and allocate another */
2156 if (++count > src->retry)
2159 GST_DEBUG_OBJECT (src, "RTP port not even");
2161 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2162 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2163 gst_object_unref (udpsrc0);
2166 GST_DEBUG_OBJECT (src, "retry %d", count);
2171 /* allocate port+1 for RTCP now */
2172 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2173 if (udpsrc1 == NULL)
2174 goto no_udp_rtcp_protocol;
2177 tmp_rtcp = tmp_rtp + 1;
2178 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
2181 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
2183 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
2184 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
2185 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
2186 if (ret == GST_STATE_CHANGE_FAILURE) {
2187 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
2189 if (++count > src->retry)
2192 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2193 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2194 gst_object_unref (udpsrc0);
2197 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
2198 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2199 gst_object_unref (udpsrc1);
2203 GST_DEBUG_OBJECT (src, "retry %d", count);
2207 /* all fine, do port check */
2208 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
2209 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
2211 /* this should not happen... */
2212 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
2215 /* we keep these elements, we configure all in configure_transport when the
2216 * server told us to really use the UDP ports. */
2217 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
2218 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
2219 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
2220 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
2222 /* keep track of next available port number when we have a range
2224 if (src->next_port_num != 0)
2225 src->next_port_num = tmp_rtcp + 1;
2232 GST_DEBUG_OBJECT (src, "could not get UDP source");
2237 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
2241 no_udp_rtcp_protocol:
2243 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
2248 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
2249 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
2255 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2256 gst_object_unref (udpsrc0);
2259 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2260 gst_object_unref (udpsrc1);
2267 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
2272 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
2274 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2275 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2278 for (i = 0; i < 2; i++) {
2279 if (stream->udpsrc[i])
2280 gst_element_set_state (stream->udpsrc[i], state);
2286 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
2293 event = gst_event_new_flush_start ();
2294 GST_DEBUG_OBJECT (src, "start flush");
2296 state = GST_STATE_PAUSED;
2298 event = gst_event_new_flush_stop (FALSE);
2299 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2302 state = GST_STATE_PLAYING;
2304 state = GST_STATE_PAUSED;
2306 gst_rtspsrc_push_event (src, event);
2307 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2308 gst_rtspsrc_set_state (src, state);
2311 static GstRTSPResult
2312 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
2313 GstRTSPMessage * message, GTimeVal * timeout)
2318 ret = gst_rtsp_connection_send (conn, message, timeout);
2320 ret = GST_RTSP_ERROR;
2325 static GstRTSPResult
2326 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
2327 GstRTSPMessage * message, GTimeVal * timeout)
2332 ret = gst_rtsp_connection_receive (conn, message, timeout);
2334 ret = GST_RTSP_ERROR;
2340 gst_rtspsrc_get_position (GstRTSPSrc * src)
2345 query = gst_query_new_position (GST_FORMAT_TIME);
2346 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2347 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2348 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2352 if (stream->srcpad) {
2353 if (gst_pad_query (stream->srcpad, query)) {
2354 gst_query_parse_position (query, &fmt, &pos);
2355 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2356 GST_TIME_ARGS (pos));
2357 src->last_pos = pos;
2367 gst_query_unref (query);
2371 gst_rtspsrc_do_seek (GstRTSPSrc * src, GstSegment * segment)
2373 src->state = GST_RTSP_STATE_SEEKING;
2374 /* PLAY will add the range header now. */
2375 src->need_range = TRUE;
2381 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2386 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2388 gboolean flush, skip;
2391 GstSegment seeksegment = { 0, };
2395 GST_DEBUG_OBJECT (src, "doing seek with event");
2397 gst_event_parse_seek (event, &rate, &format, &flags,
2398 &cur_type, &cur, &stop_type, &stop);
2400 /* no negative rates yet */
2404 /* we need TIME format */
2405 if (format != src->segment.format)
2408 GST_DEBUG_OBJECT (src, "doing seek without event");
2410 cur_type = GST_SEEK_TYPE_SET;
2411 stop_type = GST_SEEK_TYPE_SET;
2414 /* get flush flag */
2415 flush = flags & GST_SEEK_FLAG_FLUSH;
2416 skip = flags & GST_SEEK_FLAG_SKIP;
2418 /* now we need to make sure the streaming thread is stopped. We do this by
2419 * either sending a FLUSH_START event downstream which will cause the
2420 * streaming thread to stop with a WRONG_STATE.
2421 * For a non-flushing seek we simply pause the task, which will happen as soon
2422 * as it completes one iteration (and thus might block when the sink is
2423 * blocking in preroll). */
2425 GST_DEBUG_OBJECT (src, "starting flush");
2426 gst_rtspsrc_flush (src, TRUE, FALSE);
2429 gst_task_pause (src->task);
2433 /* we should now be able to grab the streaming thread because we stopped it
2434 * with the above flush/pause code */
2435 GST_RTSP_STREAM_LOCK (src);
2437 GST_DEBUG_OBJECT (src, "stopped streaming");
2439 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2440 gst_rtspsrc_connection_flush (src, FALSE);
2442 /* copy segment, we need this because we still need the old
2443 * segment when we close the current segment. */
2444 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2446 /* configure the seek parameters in the seeksegment. We will then have the
2447 * right values in the segment to perform the seek */
2449 GST_DEBUG_OBJECT (src, "configuring seek");
2450 gst_segment_do_seek (&seeksegment, rate, format, flags,
2451 cur_type, cur, stop_type, stop, &update);
2454 /* figure out the last position we need to play. If it's configured (stop !=
2455 * -1), use that, else we play until the total duration of the file */
2456 if ((stop = seeksegment.stop) == -1)
2457 stop = seeksegment.duration;
2459 playing = (src->state == GST_RTSP_STATE_PLAYING);
2461 /* if we were playing, pause first */
2463 /* obtain current position in case seek fails */
2464 gst_rtspsrc_get_position (src);
2465 gst_rtspsrc_pause (src, FALSE);
2469 gst_rtspsrc_do_seek (src, &seeksegment);
2471 /* and continue playing */
2473 gst_rtspsrc_play (src, &seeksegment, FALSE);
2475 /* prepare for streaming again */
2477 /* if we started flush, we stop now */
2478 GST_DEBUG_OBJECT (src, "stopping flush");
2479 gst_rtspsrc_flush (src, FALSE, playing);
2482 /* now we did the seek and can activate the new segment values */
2483 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2485 /* if we're doing a segment seek, post a SEGMENT_START message */
2486 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2487 gst_element_post_message (GST_ELEMENT_CAST (src),
2488 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2489 src->segment.format, src->segment.position));
2492 /* now create the newsegment */
2493 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2494 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2497 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2498 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2499 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2500 stream->discont = TRUE;
2503 GST_RTSP_STREAM_UNLOCK (src);
2510 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2515 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2521 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2525 gboolean res = TRUE;
2528 src = GST_RTSPSRC_CAST (parent);
2530 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2531 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2533 switch (GST_EVENT_TYPE (event)) {
2534 case GST_EVENT_SEEK:
2535 res = gst_rtspsrc_perform_seek (src, event);
2539 case GST_EVENT_NAVIGATION:
2540 case GST_EVENT_LATENCY:
2548 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2549 res = gst_pad_send_event (target, event);
2550 gst_object_unref (target);
2552 gst_event_unref (event);
2555 gst_event_unref (event);
2561 /* this is the final event function we receive on the internal source pad when
2562 * we deal with TCP connections */
2564 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2569 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2571 switch (GST_EVENT_TYPE (event)) {
2572 case GST_EVENT_SEEK:
2574 case GST_EVENT_NAVIGATION:
2575 case GST_EVENT_LATENCY:
2577 gst_event_unref (event);
2584 /* this is the final query function we receive on the internal source pad when
2585 * we deal with TCP connections */
2587 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2591 gboolean res = TRUE;
2593 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2595 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2596 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2598 switch (GST_QUERY_TYPE (query)) {
2599 case GST_QUERY_POSITION:
2604 case GST_QUERY_DURATION:
2608 gst_query_parse_duration (query, &format, NULL);
2611 case GST_FORMAT_TIME:
2612 gst_query_set_duration (query, format, src->segment.duration);
2620 case GST_QUERY_LATENCY:
2622 /* we are live with a min latency of 0 and unlimited max latency, this
2623 * result will be updated by the session manager if there is any. */
2624 gst_query_set_latency (query, TRUE, 0, -1);
2634 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2636 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2640 gboolean res = FALSE;
2642 src = GST_RTSPSRC_CAST (parent);
2644 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2645 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2647 switch (GST_QUERY_TYPE (query)) {
2648 case GST_QUERY_DURATION:
2652 gst_query_parse_duration (query, &format, NULL);
2655 case GST_FORMAT_TIME:
2656 gst_query_set_duration (query, format, src->segment.duration);
2664 case GST_QUERY_SEEKING:
2668 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2669 if (format == GST_FORMAT_TIME) {
2671 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2673 /* seeking without duration is unlikely */
2674 seekable = seekable && src->seekable && src->segment.duration &&
2675 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2677 /* FIXME ?? should we have 0 and segment.duration here; see demuxers */
2678 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
2679 src->segment.start, src->segment.stop);
2688 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2690 gst_query_set_uri (query, uri);
2698 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2700 /* forward the query to the proxy target pad */
2702 res = gst_pad_query (target, query);
2703 gst_object_unref (target);
2712 /* callback for RTCP messages to be sent to the server when operating in TCP
2714 static GstFlowReturn
2715 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2718 GstRTSPStream *stream;
2719 GstFlowReturn res = GST_FLOW_OK;
2724 GstRTSPMessage message = { 0 };
2725 GstRTSPConnection *conn;
2727 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2728 src = stream->parent;
2730 gst_buffer_map (buffer, &map, GST_MAP_READ);
2734 gst_rtsp_message_init_data (&message, stream->channel[1]);
2736 /* lend the body data to the message */
2737 gst_rtsp_message_take_body (&message, data, size);
2739 if (stream->conninfo.connection)
2740 conn = stream->conninfo.connection;
2742 conn = src->conninfo.connection;
2744 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2745 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2746 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2748 /* and steal it away again because we will free it when unreffing the
2750 gst_rtsp_message_steal_body (&message, &data, &size);
2751 gst_rtsp_message_unset (&message);
2753 gst_buffer_unmap (buffer, &map);
2754 gst_buffer_unref (buffer);
2759 static GstPadProbeReturn
2760 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2762 GstRTSPSrc *src = user_data;
2764 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2765 GST_DEBUG_PAD_NAME (pad));
2767 /* activate the streams */
2768 GST_OBJECT_LOCK (src);
2769 if (!src->need_activate)
2772 src->need_activate = FALSE;
2773 GST_OBJECT_UNLOCK (src);
2775 gst_rtspsrc_activate_streams (src);
2777 return GST_PAD_PROBE_OK;
2781 GST_OBJECT_UNLOCK (src);
2782 return GST_PAD_PROBE_OK;
2787 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
2789 GstPad *gpad = GST_PAD_CAST (user_data);
2791 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
2792 gst_pad_store_sticky_event (gpad, *event);
2797 /* this callback is called when the session manager generated a new src pad with
2798 * payloaded RTP packets. We simply ghost the pad here. */
2800 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2803 GstPadTemplate *template;
2806 GstRTSPStream *stream;
2809 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2811 GST_RTSP_STATE_LOCK (src);
2813 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2814 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2815 goto unknown_stream;
2817 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2819 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2821 goto unknown_stream;
2824 stream->ssrc = ssrc;
2826 /* we'll add it later see below */
2827 stream->added = TRUE;
2829 /* check if we added all streams */
2831 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
2832 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
2834 GST_DEBUG_OBJECT (src, "stream %p, container %d, added %d, setup %d",
2835 ostream, ostream->container, ostream->added, ostream->setup);
2837 /* if we find a stream for which we did a setup that is not added, we
2838 * need to wait some more */
2839 if (ostream->setup && !ostream->added) {
2844 GST_RTSP_STATE_UNLOCK (src);
2846 /* create a new pad we will use to stream to */
2847 template = gst_static_pad_template_get (&rtptemplate);
2848 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2849 gst_object_unref (template);
2852 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2853 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2854 gst_pad_set_active (stream->srcpad, TRUE);
2855 gst_pad_sticky_events_foreach (pad, copy_sticky_events, stream->srcpad);
2856 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2859 GST_DEBUG_OBJECT (src, "We added all streams");
2860 /* when we get here, all stream are added and we can fire the no-more-pads
2862 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2870 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2871 GST_RTSP_STATE_UNLOCK (src);
2878 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
2882 len = stream->ptmap->len;
2883 for (i = 0; i < len; i++) {
2884 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
2892 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2894 GstRTSPStream *stream;
2897 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2899 GST_RTSP_STATE_LOCK (src);
2900 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2902 goto unknown_stream;
2904 if ((caps = stream_get_caps_for_pt (stream, pt)))
2905 gst_caps_ref (caps);
2906 GST_RTSP_STATE_UNLOCK (src);
2912 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2913 GST_RTSP_STATE_UNLOCK (src);
2919 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2921 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2927 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2933 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2939 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2941 GstRTSPSrc *src = stream->parent;
2944 g_object_get (source, "ssrc", &ssrc, NULL);
2946 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
2947 ssrc, stream->ssrc, stream->id);
2949 if (ssrc == stream->ssrc)
2950 gst_rtspsrc_do_stream_eos (src, stream);
2954 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2956 GstRTSPSrc *src = stream->parent;
2959 g_object_get (source, "ssrc", &ssrc, NULL);
2961 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
2962 ssrc, stream->ssrc, stream->id);
2964 if (ssrc == stream->ssrc)
2965 gst_rtspsrc_do_stream_eos (src, stream);
2969 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2971 GstRTSPStream *stream;
2973 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2975 /* get stream for session */
2976 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2978 gst_rtspsrc_do_stream_eos (src, stream);
2983 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2985 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2990 set_manager_buffer_mode (GstRTSPSrc * src)
2992 GObjectClass *klass;
2994 if (src->manager == NULL)
2997 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2999 if (!g_object_class_find_property (klass, "buffer-mode"))
3002 if (src->buffer_mode != BUFFER_MODE_AUTO) {
3003 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
3008 GST_DEBUG_OBJECT (src,
3009 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
3011 if (src->provided_clock) {
3012 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
3014 if (clock == src->provided_clock) {
3015 GST_DEBUG_OBJECT (src, "selected synced");
3016 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
3019 gst_object_unref (clock);
3024 /* Otherwise fall-through and use another buffer mode */
3026 gst_object_unref (clock);
3029 GST_DEBUG_OBJECT (src, "auto buffering mode");
3030 if (src->use_buffering) {
3031 GST_DEBUG_OBJECT (src, "selected buffer");
3032 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
3034 GST_DEBUG_OBJECT (src, "selected slave");
3035 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
3040 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
3042 GST_DEBUG ("request key %u", ssrc);
3043 return gst_caps_ref (stream_get_caps_for_pt (stream, stream->default_pt));
3047 request_rtp_decoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
3049 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3050 if (stream->id != session)
3053 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3054 stream->profile != GST_RTSP_PROFILE_SAVPF)
3057 if (stream->srtpdec == NULL) {
3060 name = g_strdup_printf ("srtpdec_%u", session);
3061 stream->srtpdec = gst_element_factory_make ("srtpdec", name);
3064 g_signal_connect (stream->srtpdec, "request-key",
3065 (GCallback) request_key, stream);
3067 return gst_object_ref (stream->srtpdec);
3071 request_rtcp_encoder (GstElement * rtpbin, guint session,
3072 GstRTSPStream * stream)
3077 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3078 if (stream->id != session)
3081 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3082 stream->profile != GST_RTSP_PROFILE_SAVPF)
3085 if (stream->srtpenc == NULL) {
3088 name = g_strdup_printf ("srtpenc_%u", session);
3089 stream->srtpenc = gst_element_factory_make ("srtpenc", name);
3092 /* get RTCP crypto parameters from caps */
3093 s = gst_caps_get_structure (stream->srtcpparams, 0);
3097 GType ciphertype, authtype;
3098 GValue rtcp_cipher = G_VALUE_INIT, rtcp_auth = G_VALUE_INIT;
3100 ciphertype = g_type_from_name ("GstSrtpCipherType");
3101 authtype = g_type_from_name ("GstSrtpAuthType");
3102 g_value_init (&rtcp_cipher, ciphertype);
3103 g_value_init (&rtcp_auth, authtype);
3105 str = gst_structure_get_string (s, "srtcp-cipher");
3106 gst_value_deserialize (&rtcp_cipher, str);
3107 str = gst_structure_get_string (s, "srtcp-auth");
3108 gst_value_deserialize (&rtcp_auth, str);
3109 gst_structure_get (s, "srtp-key", GST_TYPE_BUFFER, &buf, NULL);
3111 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-cipher",
3113 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-auth",
3115 g_object_set (stream->srtpenc, "key", buf, NULL);
3117 g_value_unset (&rtcp_cipher);
3118 g_value_unset (&rtcp_auth);
3119 gst_buffer_unref (buf);
3122 name = g_strdup_printf ("rtcp_sink_%d", session);
3123 pad = gst_element_get_request_pad (stream->srtpenc, name);
3125 gst_object_unref (pad);
3127 return gst_object_ref (stream->srtpenc);
3131 request_aux_receiver (GstElement * rtpbin, guint sessid, GstRTSPSrc * src)
3133 GstElement *rtx, *bin;
3136 GstRTSPStream *stream;
3138 stream = find_stream (src, &sessid, (gpointer) find_stream_by_id);
3140 GST_INFO_OBJECT (src, "creating retransmision receiver for session %u "
3141 "with map %" GST_PTR_FORMAT, sessid, stream->rtx_pt_map);
3142 bin = gst_bin_new (NULL);
3143 rtx = gst_element_factory_make ("rtprtxreceive", NULL);
3144 g_object_set (rtx, "payload-type-map", stream->rtx_pt_map, NULL);
3145 gst_bin_add (GST_BIN (bin), rtx);
3147 pad = gst_element_get_static_pad (rtx, "src");
3148 name = g_strdup_printf ("src_%u", sessid);
3149 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3151 gst_object_unref (pad);
3153 pad = gst_element_get_static_pad (rtx, "sink");
3154 name = g_strdup_printf ("sink_%u", sessid);
3155 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3157 gst_object_unref (pad);
3163 add_retransmission (GstRTSPSrc * src, GstRTSPTransport * transport)
3168 if (transport->trans != GST_RTSP_TRANS_RTP)
3171 signal_id = g_signal_lookup ("request-aux-receiver",
3172 G_OBJECT_TYPE (src->manager));
3173 /* there's already something connected */
3174 if (g_signal_handler_find (src->manager, G_SIGNAL_MATCH_ID, signal_id, 0,
3175 NULL, NULL, NULL) != 0) {
3176 GST_DEBUG_OBJECT (src, "Not adding RTX AUX element as "
3177 "\"request-aux-receiver\" signal is "
3178 "already used by the application");
3182 /* build the retransmission payload type map */
3183 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3184 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3187 if (stream->rtx_pt_map)
3188 gst_structure_free (stream->rtx_pt_map);
3189 stream->rtx_pt_map = gst_structure_new_empty ("application/x-rtp-pt-map");
3191 for (i = 0; i < stream->ptmap->len; i++) {
3192 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3193 GstStructure *s = gst_caps_get_structure (item->caps, 0);
3194 const gchar *encoding;
3196 /* we only care about RTX streams */
3197 if ((encoding = gst_structure_get_string (s, "encoding-name"))
3198 && g_strcmp0 (encoding, "RTX") == 0) {
3199 const gchar *stream_pt_s;
3202 if (gst_structure_get_int (s, "payload", &rtx_pt)
3203 && (stream_pt_s = gst_structure_get_string (s, "apt"))) {
3206 gst_structure_set (stream->rtx_pt_map, stream_pt_s, G_TYPE_UINT,
3213 GST_DEBUG_OBJECT (src, "built retransmission payload map for stream "
3214 "id %i: %" GST_PTR_FORMAT, stream->id, stream->rtx_pt_map);
3217 g_object_set (src->manager, "do-retransmission", TRUE, NULL);
3219 /* enable RFC4588 retransmission handling by setting rtprtxreceive
3220 * as the "aux" element of rtpbin */
3221 g_signal_connect (src->manager, "request-aux-receiver",
3222 (GCallback) request_aux_receiver, src);
3225 /* try to get and configure a manager */
3227 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
3228 GstRTSPTransport * transport)
3230 const gchar *manager;
3232 GstStateChangeReturn ret;
3234 /* find a manager */
3235 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
3239 GST_DEBUG_OBJECT (src, "using manager %s", manager);
3241 /* configure the manager */
3242 if (src->manager == NULL) {
3243 GObjectClass *klass;
3245 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
3247 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
3251 goto use_no_manager;
3253 if (!(src->manager = gst_element_factory_make (manager, "manager")))
3254 goto manager_failed;
3257 /* we manage this element */
3258 gst_element_set_locked_state (src->manager, TRUE);
3259 gst_bin_add (GST_BIN_CAST (src), src->manager);
3261 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
3262 if (ret == GST_STATE_CHANGE_FAILURE)
3263 goto start_manager_failure;
3265 g_object_set (src->manager, "latency", src->latency, NULL);
3267 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3269 if (g_object_class_find_property (klass, "ntp-sync")) {
3270 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
3273 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
3274 g_object_set (src->manager, "use-pipeline-clock",
3275 src->use_pipeline_clock, NULL);
3278 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
3279 g_object_set (src->manager, "sdes", src->sdes, NULL);
3282 if (g_object_class_find_property (klass, "drop-on-latency")) {
3283 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
3287 /* buffer mode pauses are handled by adding offsets to buffer times,
3288 * but some depayloaders may have a hard time syncing output times
3289 * with such input times, e.g. container ones, most notably ASF */
3290 /* TODO alternatives are having an event that indicates these shifts,
3291 * or having rtsp extensions provide suggestion on buffer mode */
3292 /* valid duration implies not likely live pipeline,
3293 * so slaving in jitterbuffer does not make much sense
3294 * (and might mess things up due to bursts) */
3295 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
3296 src->segment.duration && !stream->container) {
3297 src->use_buffering = TRUE;
3299 src->use_buffering = FALSE;
3302 set_manager_buffer_mode (src);
3304 /* connect to signals */
3305 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
3307 src->manager_sig_id =
3308 g_signal_connect (src->manager, "pad-added",
3309 (GCallback) new_manager_pad, src);
3310 src->manager_ptmap_id =
3311 g_signal_connect (src->manager, "request-pt-map",
3312 (GCallback) request_pt_map, src);
3314 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
3317 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
3320 if (src->do_retransmission)
3321 add_retransmission (src, transport);
3323 g_signal_connect (src->manager, "request-rtp-decoder",
3324 (GCallback) request_rtp_decoder, stream);
3325 g_signal_connect (src->manager, "request-rtcp-decoder",
3326 (GCallback) request_rtp_decoder, stream);
3327 g_signal_connect (src->manager, "request-rtcp-encoder",
3328 (GCallback) request_rtcp_encoder, stream);
3330 /* we stream directly to the manager, get some pads. Each RTSP stream goes
3331 * into a separate RTP session. */
3332 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
3333 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
3335 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
3336 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
3339 /* now configure the bandwidth in the manager */
3340 if (g_signal_lookup ("get-internal-session",
3341 G_OBJECT_TYPE (src->manager)) != 0) {
3342 GObject *rtpsession;
3344 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
3347 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
3349 stream->session = rtpsession;
3351 if (stream->as_bandwidth != -1) {
3352 GST_INFO_OBJECT (src, "setting AS: %f",
3353 (gdouble) (stream->as_bandwidth * 1000));
3354 g_object_set (rtpsession, "bandwidth",
3355 (gdouble) (stream->as_bandwidth * 1000), NULL);
3357 if (stream->rr_bandwidth != -1) {
3358 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
3359 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
3362 if (stream->rs_bandwidth != -1) {
3363 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
3364 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
3368 g_object_set (rtpsession, "probation", src->probation, NULL);
3370 g_object_set (rtpsession, "internal-ssrc", stream->send_ssrc, NULL);
3372 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
3374 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
3376 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
3378 g_signal_connect (rtpsession, "on-ssrc-active",
3379 (GCallback) on_ssrc_active, stream);
3390 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3395 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
3398 start_manager_failure:
3400 GST_DEBUG_OBJECT (src, "could not start session manager");
3405 /* free the UDP sources allocated when negotiating a transport.
3406 * This function is called when the server negotiated to a transport where the
3407 * UDP sources are not needed anymore, such as TCP or multicast. */
3409 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
3413 for (i = 0; i < 2; i++) {
3414 if (stream->udpsrc[i]) {
3415 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
3416 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
3417 gst_object_unref (stream->udpsrc[i]);
3418 stream->udpsrc[i] = NULL;
3423 /* for TCP, create pads to send and receive data to and from the manager and to
3424 * intercept various events and queries
3427 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
3428 GstRTSPTransport * transport, GstPad ** outpad)
3431 GstPadTemplate *template;
3432 GstPad *pad0, *pad1;
3434 /* configure for interleaved delivery, nothing needs to be done
3435 * here, the loop function will call the chain functions of the
3436 * session manager. */
3437 stream->channel[0] = transport->interleaved.min;
3438 stream->channel[1] = transport->interleaved.max;
3439 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
3440 stream->channel[0], stream->channel[1]);
3442 /* we can remove the allocated UDP ports now */
3443 gst_rtspsrc_stream_free_udp (stream);
3445 /* no session manager, send data to srcpad directly */
3446 if (!stream->channelpad[0]) {
3447 GST_DEBUG_OBJECT (src, "no manager, creating pad");
3449 /* create a new pad we will use to stream to */
3450 name = g_strdup_printf ("stream_%u", stream->id);
3451 template = gst_static_pad_template_get (&rtptemplate);
3452 stream->channelpad[0] = gst_pad_new_from_template (template, name);
3453 gst_object_unref (template);
3456 /* set caps and activate */
3457 gst_pad_use_fixed_caps (stream->channelpad[0]);
3458 gst_pad_set_active (stream->channelpad[0], TRUE);
3460 *outpad = gst_object_ref (stream->channelpad[0]);
3462 GST_DEBUG_OBJECT (src, "using manager source pad");
3464 template = gst_static_pad_template_get (&anysrctemplate);
3466 /* allocate pads for sending the channel data into the manager */
3467 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
3468 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
3469 gst_object_unref (stream->channelpad[0]);
3470 stream->channelpad[0] = pad0;
3471 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
3472 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
3473 gst_pad_set_element_private (pad0, src);
3474 gst_pad_set_active (pad0, TRUE);
3476 if (stream->channelpad[1]) {
3477 /* if we have a sinkpad for the other channel, create a pad and link to the
3479 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
3480 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
3481 gst_pad_link_full (pad1, stream->channelpad[1],
3482 GST_PAD_LINK_CHECK_NOTHING);
3483 gst_object_unref (stream->channelpad[1]);
3484 stream->channelpad[1] = pad1;
3485 gst_pad_set_active (pad1, TRUE);
3487 gst_object_unref (template);
3489 /* setup RTCP transport back to the server if we have to. */
3490 if (src->manager && src->do_rtcp) {
3493 template = gst_static_pad_template_get (&anysinktemplate);
3495 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
3496 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
3497 gst_pad_set_element_private (stream->rtcppad, stream);
3498 gst_pad_set_active (stream->rtcppad, TRUE);
3500 /* get session RTCP pad */
3501 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3502 pad = gst_element_get_request_pad (src->manager, name);
3507 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3508 gst_object_unref (pad);
3511 gst_object_unref (template);
3517 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
3518 GstRTSPTransport * transport, const gchar ** destination, gint * min,
3519 gint * max, guint * ttl)
3521 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3523 if (!(*destination = transport->destination))
3524 *destination = stream->destination;
3527 /* transport first */
3528 *min = transport->port.min;
3529 *max = transport->port.max;
3530 if (*min == -1 && *max == -1) {
3531 /* then try from SDP */
3532 if (stream->port != 0) {
3533 *min = stream->port;
3534 *max = stream->port + 1;
3540 if (!(*ttl = transport->ttl))
3545 /* first take the source, then the endpoint to figure out where to send
3547 if (!(*destination = transport->source)) {
3548 if (src->conninfo.connection)
3549 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3550 else if (stream->conninfo.connection)
3552 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3556 /* for unicast we only expect the ports here */
3557 *min = transport->server_port.min;
3558 *max = transport->server_port.max;
3563 /* For multicast create UDP sources and join the multicast group. */
3565 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3566 GstRTSPTransport * transport, GstPad ** outpad)
3569 const gchar *destination;
3572 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3574 /* we can remove the allocated UDP ports now */
3575 gst_rtspsrc_stream_free_udp (stream);
3577 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3580 /* we need a destination now */
3581 if (destination == NULL)
3582 goto no_destination;
3584 /* we really need ports now or we won't be able to receive anything at all */
3585 if (min == -1 && max == -1)
3588 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3589 destination, min, max);
3591 /* creating UDP source for RTP */
3593 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3595 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3597 if (stream->udpsrc[0] == NULL)
3600 /* take ownership */
3601 gst_object_ref_sink (stream->udpsrc[0]);
3603 if (src->udp_buffer_size != 0)
3604 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3605 src->udp_buffer_size, NULL);
3607 if (src->multi_iface != NULL)
3608 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3609 src->multi_iface, NULL);
3612 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3613 gst_element_set_state (stream->udpsrc[0], GST_STATE_READY);
3616 /* creating another UDP source for RTCP */
3620 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3622 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3624 if (stream->udpsrc[1] == NULL)
3627 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3628 stream->profile == GST_RTSP_PROFILE_SAVPF)
3629 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3631 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3632 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3633 gst_caps_unref (caps);
3635 /* take ownership */
3636 gst_object_ref_sink (stream->udpsrc[1]);
3638 if (src->multi_iface != NULL)
3639 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3640 src->multi_iface, NULL);
3642 gst_element_set_state (stream->udpsrc[1], GST_STATE_READY);
3649 GST_DEBUG_OBJECT (src, "no UDP source element found");
3654 GST_DEBUG_OBJECT (src, "no destination found");
3659 GST_DEBUG_OBJECT (src, "no ports found");
3664 /* configure the remainder of the UDP ports */
3666 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3667 GstRTSPTransport * transport, GstPad ** outpad)
3669 /* we manage the UDP elements now. For unicast, the UDP sources where
3670 * allocated in the stream when we suggested a transport. */
3671 if (stream->udpsrc[0]) {
3674 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3675 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3677 GST_DEBUG_OBJECT (src, "setting up UDP source");
3679 /* configure a timeout on the UDP port. When the timeout message is
3680 * posted, we assume UDP transport is not possible. We reconnect using TCP
3682 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3683 src->udp_timeout * 1000, NULL);
3685 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt)))
3686 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
3688 /* get output pad of the UDP source. */
3689 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3691 /* save it so we can unblock */
3692 stream->blockedpad = *outpad;
3694 /* configure pad block on the pad. As soon as there is dataflow on the
3695 * UDP source, we know that UDP is not blocked by a firewall and we can
3696 * configure all the streams to let the application autoplug decoders. */
3698 gst_pad_add_probe (stream->blockedpad,
3699 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3700 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocked, src, NULL);
3702 if (stream->channelpad[0]) {
3703 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3704 /* configure for UDP delivery, we need to connect the UDP pads to
3705 * the session plugin. */
3706 gst_pad_link_full (*outpad, stream->channelpad[0],
3707 GST_PAD_LINK_CHECK_NOTHING);
3708 gst_object_unref (*outpad);
3710 /* we connected to pad-added signal to get pads from the manager */
3712 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3717 if (stream->udpsrc[1]) {
3720 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3721 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3723 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3724 stream->profile == GST_RTSP_PROFILE_SAVPF)
3725 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3727 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3728 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3729 gst_caps_unref (caps);
3731 if (stream->channelpad[1]) {
3734 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3736 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3737 gst_pad_link_full (pad, stream->channelpad[1],
3738 GST_PAD_LINK_CHECK_NOTHING);
3739 gst_object_unref (pad);
3741 /* leave unlinked */
3747 /* configure the UDP sink back to the server for status reports */
3749 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3750 GstRTSPStream * stream, GstRTSPTransport * transport)
3753 gint rtp_port, rtcp_port;
3754 gboolean do_rtp, do_rtcp;
3755 const gchar *destination;
3760 /* get transport info */
3761 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
3762 &rtp_port, &rtcp_port, &ttl);
3764 /* see what we need to do */
3765 do_rtp = (rtp_port != -1);
3766 /* it's possible that the server does not want us to send RTCP in which case
3768 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
3770 /* we need a destination when we have RTP or RTCP ports */
3771 if (destination == NULL && (do_rtp || do_rtcp))
3772 goto no_destination;
3774 /* try to construct the fakesrc to the RTP port of the server to open up any
3777 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
3780 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
3781 stream->udpsink[0] =
3782 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3784 if (stream->udpsink[0] == NULL)
3785 goto no_sink_element;
3787 /* don't join multicast group, we will have the source socket do that */
3788 /* no sync or async state changes needed */
3789 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
3790 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3792 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3794 if (stream->udpsrc[0]) {
3795 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
3796 * so that NAT firewalls will open a hole for us */
3797 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
3798 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
3799 /* configure socket and make sure udpsink does not close it when shutting
3800 * down, it belongs to udpsrc after all. */
3801 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
3802 "close-socket", FALSE, NULL);
3803 g_object_unref (socket);
3806 /* the source for the dummy packets to open up NAT */
3807 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
3808 if (stream->fakesrc == NULL)
3809 goto no_fakesrc_element;
3811 /* random data in 5 buffers, a size of 200 bytes should be fine */
3812 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
3813 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
3815 /* we don't want to consider this a sink */
3816 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
3818 /* keep everything locked */
3819 gst_element_set_locked_state (stream->udpsink[0], TRUE);
3820 gst_element_set_locked_state (stream->fakesrc, TRUE);
3822 gst_object_ref (stream->udpsink[0]);
3823 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
3824 gst_object_ref (stream->fakesrc);
3825 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
3827 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
3828 "sink", GST_PAD_LINK_CHECK_NOTHING);
3831 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
3834 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
3835 stream->udpsink[1] =
3836 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3838 if (stream->udpsink[1] == NULL)
3839 goto no_sink_element;
3841 /* don't join multicast group, we will have the source socket do that */
3842 /* no sync or async state changes needed */
3843 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
3844 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3846 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3848 if (stream->udpsrc[1]) {
3849 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
3850 * because some servers check the port number of where it sends RTCP to identify
3851 * the RTCP packets it receives */
3852 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
3853 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
3854 /* configure socket and make sure udpsink does not close it when shutting
3855 * down, it belongs to udpsrc after all. */
3856 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
3857 "close-socket", FALSE, NULL);
3858 g_object_unref (socket);
3861 /* we don't want to consider this a sink */
3862 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
3864 /* we keep this playing always */
3865 gst_element_set_locked_state (stream->udpsink[1], TRUE);
3866 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
3868 gst_object_ref (stream->udpsink[1]);
3869 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
3871 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
3873 /* get session RTCP pad */
3874 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3875 pad = gst_element_get_request_pad (src->manager, name);
3880 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3881 gst_object_unref (pad);
3890 GST_DEBUG_OBJECT (src, "no destination address specified");
3895 GST_DEBUG_OBJECT (src, "no UDP sink element found");
3900 GST_DEBUG_OBJECT (src, "no fakesrc element found");
3905 /* sets up all elements needed for streaming over the specified transport.
3906 * Does not yet expose the element pads, this will be done when there is actuall
3907 * dataflow detected, which might never happen when UDP is blocked in a
3908 * firewall, for example.
3911 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
3912 GstRTSPTransport * transport)
3915 GstPad *outpad = NULL;
3916 GstPadTemplate *template;
3918 const gchar *media_type;
3921 src = stream->parent;
3923 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
3925 /* get the proper media type for this stream now */
3926 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
3927 goto unknown_transport;
3929 goto unknown_transport;
3931 /* configure the final media type */
3932 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
3934 len = stream->ptmap->len;
3935 for (i = 0; i < len; i++) {
3937 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3939 if (item->caps == NULL)
3942 s = gst_caps_get_structure (item->caps, 0);
3943 gst_structure_set_name (s, media_type);
3944 /* set ssrc if known */
3945 if (transport->ssrc)
3946 gst_structure_set (s, "ssrc", G_TYPE_UINT, transport->ssrc, NULL);
3949 /* try to get and configure a manager, channelpad[0-1] will be configured with
3950 * the pads for the manager, or NULL when no manager is needed. */
3951 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
3954 switch (transport->lower_transport) {
3955 case GST_RTSP_LOWER_TRANS_TCP:
3956 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
3957 goto transport_failed;
3959 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3960 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
3961 goto transport_failed;
3962 /* fallthrough, the rest is the same for UDP and MCAST */
3963 case GST_RTSP_LOWER_TRANS_UDP:
3964 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
3965 goto transport_failed;
3966 /* configure udpsinks back to the server for RTCP messages and for the
3967 * dummy RTP messages to open NAT. */
3968 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
3969 goto transport_failed;
3972 goto unknown_transport;
3976 GST_DEBUG_OBJECT (src, "creating ghostpad");
3978 gst_pad_use_fixed_caps (outpad);
3980 /* create ghostpad, don't add just yet, this will be done when we activate
3982 name = g_strdup_printf ("stream_%u", stream->id);
3983 template = gst_static_pad_template_get (&rtptemplate);
3984 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
3985 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3986 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3987 gst_object_unref (template);
3990 gst_object_unref (outpad);
3992 /* mark pad as ok */
3993 stream->last_ret = GST_FLOW_OK;
4000 GST_DEBUG_OBJECT (src, "failed to configure transport");
4005 GST_DEBUG_OBJECT (src, "unknown transport");
4010 GST_DEBUG_OBJECT (src, "cannot get a session manager");
4015 /* send a couple of dummy random packets on the receiver RTP port to the server,
4016 * this should make a firewall think we initiated the data transfer and
4017 * hopefully allow packets to go from the sender port to our RTP receiver port */
4019 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
4023 if (src->nat_method != GST_RTSP_NAT_DUMMY)
4026 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4027 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4029 if (stream->fakesrc && stream->udpsink[0]) {
4030 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
4031 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
4032 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
4033 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
4034 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
4040 /* Adds the source pads of all configured streams to the element.
4041 * This code is performed when we detected dataflow.
4043 * We detect dataflow from either the _loop function or with pad probes on the
4047 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
4051 GST_DEBUG_OBJECT (src, "activating streams");
4053 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4054 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4056 if (stream->udpsrc[0]) {
4057 /* remove timeout, we are streaming now and timeouts will be handled by
4058 * the session manager and jitter buffer */
4059 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
4061 if (stream->srcpad) {
4062 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
4063 gst_pad_set_active (stream->srcpad, TRUE);
4065 /* if we don't have a session manager, set the caps now. If we have a
4066 * session, we will get a notification of the pad and the caps. */
4067 if (!src->manager) {
4070 caps = stream_get_caps_for_pt (stream, stream->default_pt);
4071 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
4072 gst_pad_set_caps (stream->srcpad, caps);
4075 if (!stream->added) {
4076 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
4077 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
4078 stream->added = TRUE;
4083 /* unblock all pads */
4084 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4085 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4087 if (stream->blockid) {
4088 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
4089 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
4090 stream->blockid = 0;
4098 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
4099 gboolean reset_manager)
4102 guint64 start, stop;
4103 gdouble play_speed, play_scale;
4105 GST_DEBUG_OBJECT (src, "configuring stream caps");
4107 start = segment->position;
4108 stop = segment->duration;
4109 play_speed = segment->rate;
4110 play_scale = segment->applied_rate;
4112 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4113 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4119 len = stream->ptmap->len;
4120 for (j = 0; j < len; j++) {
4122 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
4124 if (item->caps == NULL)
4127 caps = gst_caps_make_writable (item->caps);
4129 if (stream->timebase != -1)
4130 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
4131 (guint) stream->timebase, NULL);
4132 if (stream->seqbase != -1)
4133 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
4134 (guint) stream->seqbase, NULL);
4135 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
4137 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
4138 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
4139 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
4142 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
4145 if (item->pt == stream->default_pt && stream->udpsrc[0]) {
4146 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
4150 if (reset_manager && src->manager) {
4151 GST_DEBUG_OBJECT (src, "clear session");
4152 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
4156 static GstFlowReturn
4157 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
4162 /* store the value */
4163 stream->last_ret = ret;
4165 /* if it's success we can return the value right away */
4166 if (ret == GST_FLOW_OK)
4169 /* any other error that is not-linked can be returned right
4171 if (ret != GST_FLOW_NOT_LINKED)
4174 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4175 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4176 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4178 ret = ostream->last_ret;
4179 /* some other return value (must be SUCCESS but we can return
4180 * other values as well) */
4181 if (ret != GST_FLOW_NOT_LINKED)
4184 /* if we get here, all other pads were unlinked and we return
4185 * NOT_LINKED then */
4191 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
4194 gboolean res = TRUE;
4196 /* only streams that have a connection to the outside world */
4200 if (stream->udpsrc[0]) {
4201 gst_event_ref (event);
4202 res = gst_element_send_event (stream->udpsrc[0], event);
4203 } else if (stream->channelpad[0]) {
4204 gst_event_ref (event);
4205 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4206 res = gst_pad_push_event (stream->channelpad[0], event);
4208 res = gst_pad_send_event (stream->channelpad[0], event);
4211 if (stream->udpsrc[1]) {
4212 gst_event_ref (event);
4213 res &= gst_element_send_event (stream->udpsrc[1], event);
4214 } else if (stream->channelpad[1]) {
4215 gst_event_ref (event);
4216 if (GST_PAD_IS_SRC (stream->channelpad[1]))
4217 res &= gst_pad_push_event (stream->channelpad[1], event);
4219 res &= gst_pad_send_event (stream->channelpad[1], event);
4223 gst_event_unref (event);
4229 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
4232 gboolean res = TRUE;
4234 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4235 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4237 gst_event_ref (event);
4238 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
4240 gst_event_unref (event);
4245 static GstRTSPResult
4246 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4251 if (info->connection == NULL) {
4252 if (info->url == NULL) {
4253 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
4254 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
4258 /* create connection */
4259 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
4260 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
4261 goto could_not_create;
4264 g_free (info->url_str);
4265 info->url_str = gst_rtsp_url_get_request_uri (info->url);
4267 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
4269 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
4270 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
4271 src->tls_validation_flags))
4272 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
4274 if (src->tls_database)
4275 gst_rtsp_connection_set_tls_database (info->connection,
4279 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
4280 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
4282 if (src->proxy_host) {
4283 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
4285 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
4290 if (!info->connected) {
4293 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
4294 ("Connecting to %s", info->location));
4295 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
4297 gst_rtsp_connection_connect (info->connection,
4298 src->ptcp_timeout)) < 0)
4299 goto could_not_connect;
4301 info->connected = TRUE;
4308 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
4313 gchar *str = gst_rtsp_strresult (res);
4314 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
4320 gchar *str = gst_rtsp_strresult (res);
4321 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
4327 static GstRTSPResult
4328 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
4331 GST_RTSP_STATE_LOCK (src);
4332 if (info->connected) {
4333 GST_DEBUG_OBJECT (src, "closing connection...");
4334 gst_rtsp_connection_close (info->connection);
4335 info->connected = FALSE;
4337 if (free && info->connection) {
4338 /* free connection */
4339 GST_DEBUG_OBJECT (src, "freeing connection...");
4340 gst_rtsp_connection_free (info->connection);
4341 info->connection = NULL;
4343 GST_RTSP_STATE_UNLOCK (src);
4347 static GstRTSPResult
4348 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4353 GST_DEBUG_OBJECT (src, "reconnecting connection...");
4354 gst_rtsp_conninfo_close (src, info, FALSE);
4355 res = gst_rtsp_conninfo_connect (src, info, async);
4361 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
4365 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
4366 GST_RTSP_STATE_LOCK (src);
4367 if (src->conninfo.connection && src->conninfo.flushing != flush) {
4368 GST_DEBUG_OBJECT (src, "connection flush");
4369 gst_rtsp_connection_flush (src->conninfo.connection, flush);
4370 src->conninfo.flushing = flush;
4372 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4373 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4374 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
4375 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
4376 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
4377 stream->conninfo.flushing = flush;
4380 GST_RTSP_STATE_UNLOCK (src);
4383 /* FIXME, handle server request, reply with OK, for now */
4384 static GstRTSPResult
4385 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
4386 GstRTSPMessage * request)
4388 GstRTSPMessage response = { 0 };
4391 GST_DEBUG_OBJECT (src, "got server request message");
4394 gst_rtsp_message_dump (request);
4396 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
4398 if (res == GST_RTSP_ENOTIMPL) {
4399 /* default implementation, send OK */
4400 GST_DEBUG_OBJECT (src, "prepare OK reply");
4402 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
4407 /* let app parse and reply */
4408 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
4409 0, request, &response);
4412 gst_rtsp_message_dump (&response);
4414 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
4418 gst_rtsp_message_unset (&response);
4419 } else if (res == GST_RTSP_EEOF)
4427 gst_rtsp_message_unset (&response);
4432 /* send server keep-alive */
4433 static GstRTSPResult
4434 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
4436 GstRTSPMessage request = { 0 };
4438 GstRTSPMethod method;
4439 const gchar *control;
4441 if (src->do_rtsp_keep_alive == FALSE) {
4442 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
4443 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4447 GST_DEBUG_OBJECT (src, "creating server keep-alive");
4449 /* find a method to use for keep-alive */
4450 if (src->methods & GST_RTSP_GET_PARAMETER)
4451 method = GST_RTSP_GET_PARAMETER;
4453 method = GST_RTSP_OPTIONS;
4455 control = get_aggregate_control (src);
4456 if (control == NULL)
4459 res = gst_rtsp_message_init_request (&request, method, control);
4464 gst_rtsp_message_dump (&request);
4467 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
4472 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4473 gst_rtsp_message_unset (&request);
4480 GST_WARNING_OBJECT (src, "no control url to send keepalive");
4485 gchar *str = gst_rtsp_strresult (res);
4487 gst_rtsp_message_unset (&request);
4488 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
4489 ("Could not send keep-alive. (%s)", str));
4495 static GstFlowReturn
4496 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
4498 GstFlowReturn ret = GST_FLOW_OK;
4500 GstRTSPStream *stream;
4501 GstPad *outpad = NULL;
4508 channel = message->type_data.data.channel;
4510 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
4512 goto unknown_stream;
4514 if (channel == stream->channel[0]) {
4515 outpad = stream->channelpad[0];
4517 } else if (channel == stream->channel[1]) {
4518 outpad = stream->channelpad[1];
4524 /* take a look at the body to figure out what we have */
4525 gst_rtsp_message_get_body (message, &data, &size);
4527 goto invalid_length;
4529 /* channels are not correct on some servers, do extra check */
4530 if (data[1] >= 200 && data[1] <= 204) {
4531 /* hmm RTCP message switch to the RTCP pad of the same stream. */
4532 outpad = stream->channelpad[1];
4536 /* we have no clue what this is, just ignore then. */
4538 goto unknown_stream;
4540 /* take the message body for further processing */
4541 gst_rtsp_message_steal_body (message, &data, &size);
4543 /* strip the trailing \0 */
4546 buf = gst_buffer_new ();
4547 gst_buffer_append_memory (buf,
4548 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
4550 /* don't need message anymore */
4551 gst_rtsp_message_unset (message);
4553 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
4556 if (src->need_activate) {
4562 guint group_id = gst_util_group_id_next ();
4564 /* generate an SHA256 sum of the URI */
4565 cs = g_checksum_new (G_CHECKSUM_SHA256);
4566 uri = src->conninfo.location;
4567 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
4569 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4570 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4574 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
4575 event = gst_event_new_stream_start (stream_id);
4576 gst_event_set_group_id (event, group_id);
4579 gst_rtspsrc_stream_push_event (src, ostream, event);
4581 if ((caps = stream_get_caps_for_pt (ostream, ostream->default_pt))) {
4582 gst_pad_push_event (ostream->channelpad[0], gst_event_new_caps (caps));
4583 gst_caps_unref (caps);
4586 g_checksum_free (cs);
4588 gst_rtspsrc_activate_streams (src);
4589 src->need_activate = FALSE;
4592 if ((event = src->start_segment) != NULL) {
4593 src->start_segment = NULL;
4594 gst_rtspsrc_push_event (src, event);
4597 if (src->base_time == -1) {
4598 /* Take current running_time. This timestamp will be put on
4599 * the first buffer of each stream because we are a live source and so we
4600 * timestamp with the running_time. When we are dealing with TCP, we also
4601 * only timestamp the first buffer (using the DISCONT flag) because a server
4602 * typically bursts data, for which we don't want to compensate by speeding
4603 * up the media. The other timestamps will be interpollated from this one
4604 * using the RTP timestamps. */
4605 GST_OBJECT_LOCK (src);
4606 if (GST_ELEMENT_CLOCK (src)) {
4608 GstClockTime base_time;
4610 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
4611 base_time = GST_ELEMENT_CAST (src)->base_time;
4613 src->base_time = now - base_time;
4615 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
4616 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
4618 GST_OBJECT_UNLOCK (src);
4621 if (stream->discont && !is_rtcp) {
4622 /* mark first RTP buffer as discont */
4623 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4624 stream->discont = FALSE;
4625 /* first buffer gets the timestamp, other buffers are not timestamped and
4626 * their presentation time will be interpollated from the rtp timestamps. */
4627 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
4628 GST_TIME_ARGS (src->base_time));
4630 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
4633 /* chain to the peer pad */
4634 if (GST_PAD_IS_SINK (outpad))
4635 ret = gst_pad_chain (outpad, buf);
4637 ret = gst_pad_push (outpad, buf);
4640 /* combine all stream flows for the data transport */
4641 ret = gst_rtspsrc_combine_flows (src, stream, ret);
4648 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
4649 gst_rtsp_message_unset (message);
4654 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4655 ("Short message received, ignoring."));
4656 gst_rtsp_message_unset (message);
4661 static GstFlowReturn
4662 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
4664 GstRTSPMessage message = { 0 };
4666 GstFlowReturn ret = GST_FLOW_OK;
4667 GTimeVal tv_timeout;
4670 /* get the next timeout interval */
4671 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4673 /* see if the timeout period expired */
4674 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
4675 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
4676 /* send keep-alive, only act on interrupt, a warning will be posted for
4678 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4680 /* get new timeout */
4681 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4684 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
4685 tv_timeout.tv_sec, tv_timeout.tv_usec);
4687 /* protect the connection with the connection lock so that we can see when
4688 * we are finished doing server communication */
4690 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4691 &message, src->ptcp_timeout);
4695 GST_DEBUG_OBJECT (src, "we received a server message");
4697 case GST_RTSP_EINTR:
4698 /* we got interrupted this means we need to stop */
4700 case GST_RTSP_ETIMEOUT:
4701 /* no reply, send keep alive */
4702 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4703 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4707 /* go EOS when the server closed the connection */
4713 switch (message.type) {
4714 case GST_RTSP_MESSAGE_REQUEST:
4715 /* server sends us a request message, handle it */
4717 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4719 if (res == GST_RTSP_EEOF)
4722 goto handle_request_failed;
4724 case GST_RTSP_MESSAGE_RESPONSE:
4725 /* we ignore response messages */
4726 GST_DEBUG_OBJECT (src, "ignoring response message");
4728 gst_rtsp_message_dump (&message);
4730 case GST_RTSP_MESSAGE_DATA:
4731 GST_DEBUG_OBJECT (src, "got data message");
4732 ret = gst_rtspsrc_handle_data (src, &message);
4733 if (ret != GST_FLOW_OK)
4734 goto handle_data_failed;
4737 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4742 g_assert_not_reached ();
4747 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4748 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4749 ("The server closed the connection."));
4750 src->conninfo.connected = FALSE;
4751 gst_rtsp_message_unset (&message);
4752 return GST_FLOW_EOS;
4756 gst_rtsp_message_unset (&message);
4757 GST_DEBUG_OBJECT (src, "got interrupted");
4758 return GST_FLOW_FLUSHING;
4762 gchar *str = gst_rtsp_strresult (res);
4764 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4765 ("Could not receive message. (%s)", str));
4768 gst_rtsp_message_unset (&message);
4769 return GST_FLOW_ERROR;
4771 handle_request_failed:
4773 gchar *str = gst_rtsp_strresult (res);
4775 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4776 ("Could not handle server message. (%s)", str));
4778 gst_rtsp_message_unset (&message);
4779 return GST_FLOW_ERROR;
4783 GST_DEBUG_OBJECT (src, "could no handle data message");
4788 static GstFlowReturn
4789 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
4792 GstRTSPMessage message = { 0 };
4796 GTimeVal tv_timeout;
4798 /* get the next timeout interval */
4799 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4801 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
4802 (gint) tv_timeout.tv_sec);
4804 gst_rtsp_message_unset (&message);
4806 /* we should continue reading the TCP socket because the server might
4807 * send us requests. When the session timeout expires, we need to send a
4808 * keep-alive request to keep the session open. */
4809 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4810 &message, &tv_timeout);
4814 GST_DEBUG_OBJECT (src, "we received a server message");
4816 case GST_RTSP_EINTR:
4817 /* we got interrupted, see what we have to do */
4819 case GST_RTSP_ETIMEOUT:
4820 /* send keep-alive, ignore the result, a warning will be posted. */
4821 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4822 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4826 /* server closed the connection. not very fatal for UDP, reconnect and
4827 * see what happens. */
4828 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4829 ("The server closed the connection."));
4830 if (src->udp_reconnect) {
4832 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
4839 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
4841 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4842 ("Unhandled return value %d.", res));
4846 switch (message.type) {
4847 case GST_RTSP_MESSAGE_REQUEST:
4848 /* server sends us a request message, handle it */
4850 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4852 if (res == GST_RTSP_EEOF)
4855 goto handle_request_failed;
4857 case GST_RTSP_MESSAGE_RESPONSE:
4858 /* we ignore response and data messages */
4859 GST_DEBUG_OBJECT (src, "ignoring response message");
4861 gst_rtsp_message_dump (&message);
4862 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4863 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
4864 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
4865 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
4866 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4873 case GST_RTSP_MESSAGE_DATA:
4874 /* we ignore response and data messages */
4875 GST_DEBUG_OBJECT (src, "ignoring data message");
4878 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4883 g_assert_not_reached ();
4885 /* we get here when the connection got interrupted */
4888 gst_rtsp_message_unset (&message);
4889 GST_DEBUG_OBJECT (src, "got interrupted");
4890 return GST_FLOW_FLUSHING;
4894 gchar *str = gst_rtsp_strresult (res);
4897 src->conninfo.connected = FALSE;
4898 if (res != GST_RTSP_EINTR) {
4899 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
4900 ("Could not connect to server. (%s)", str));
4902 ret = GST_FLOW_ERROR;
4904 ret = GST_FLOW_FLUSHING;
4910 gchar *str = gst_rtsp_strresult (res);
4912 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4913 ("Could not receive message. (%s)", str));
4915 return GST_FLOW_ERROR;
4917 handle_request_failed:
4919 gchar *str = gst_rtsp_strresult (res);
4922 gst_rtsp_message_unset (&message);
4923 if (res != GST_RTSP_EINTR) {
4924 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4925 ("Could not handle server message. (%s)", str));
4927 ret = GST_FLOW_ERROR;
4929 ret = GST_FLOW_FLUSHING;
4935 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4936 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4937 ("The server closed the connection."));
4938 src->conninfo.connected = FALSE;
4939 gst_rtsp_message_unset (&message);
4940 return GST_FLOW_EOS;
4944 static GstRTSPResult
4945 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
4947 GstRTSPResult res = GST_RTSP_OK;
4950 GST_DEBUG_OBJECT (src, "doing reconnect");
4952 GST_OBJECT_LOCK (src);
4953 /* only restart when the pads were not yet activated, else we were
4954 * streaming over UDP */
4955 restart = src->need_activate;
4956 GST_OBJECT_UNLOCK (src);
4958 /* no need to restart, we're done */
4962 /* we can try only TCP now */
4963 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
4965 /* close and cleanup our state */
4966 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
4969 /* see if we have TCP left to try. Also don't try TCP when we were configured
4971 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
4974 /* We post a warning message now to inform the user
4975 * that nothing happened. It's most likely a firewall thing. */
4976 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4977 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4978 "firewall is blocking it. Retrying using a TCP connection.",
4979 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4981 /* open new connection using tcp */
4982 if (gst_rtspsrc_open (src, async) < 0)
4985 /* start playback */
4986 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
4995 src->cur_protocols = 0;
4996 /* no transport possible, post an error and stop */
4997 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4998 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4999 "firewall is blocking it. No other protocols to try.",
5000 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
5001 return GST_RTSP_ERROR;
5005 GST_DEBUG_OBJECT (src, "open failed");
5010 GST_DEBUG_OBJECT (src, "play failed");
5016 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
5020 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
5023 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
5026 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
5029 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
5037 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
5041 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
5044 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
5047 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
5050 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
5058 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
5062 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
5065 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
5068 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
5071 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
5079 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
5083 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
5086 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
5089 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
5092 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
5100 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
5102 if (ret == GST_RTSP_OK)
5103 gst_rtspsrc_loop_complete_cmd (src, cmd);
5104 else if (ret == GST_RTSP_EINTR)
5105 gst_rtspsrc_loop_cancel_cmd (src, cmd);
5107 gst_rtspsrc_loop_error_cmd (src, cmd);
5111 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
5114 gboolean flushed = FALSE;
5116 /* start new request */
5117 gst_rtspsrc_loop_start_cmd (src, cmd);
5119 GST_DEBUG_OBJECT (src, "sending cmd %d", cmd);
5121 GST_OBJECT_LOCK (src);
5122 old = src->pending_cmd;
5123 if (old == CMD_RECONNECT) {
5124 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
5125 cmd = CMD_RECONNECT;
5127 if (old != CMD_WAIT) {
5128 src->pending_cmd = CMD_WAIT;
5129 GST_OBJECT_UNLOCK (src);
5130 /* cancel previous request */
5131 GST_DEBUG_OBJECT (src, "cancel previous request %d", old);
5132 gst_rtspsrc_loop_cancel_cmd (src, old);
5133 GST_OBJECT_LOCK (src);
5135 src->pending_cmd = cmd;
5136 /* interrupt if allowed */
5137 if (src->busy_cmd & mask) {
5138 GST_DEBUG_OBJECT (src, "connection flush busy %d", src->busy_cmd);
5139 gst_rtspsrc_connection_flush (src, TRUE);
5142 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %d", src->busy_cmd);
5145 gst_task_start (src->task);
5146 GST_OBJECT_UNLOCK (src);
5152 gst_rtspsrc_loop (GstRTSPSrc * src)
5156 if (!src->conninfo.connection || !src->conninfo.connected)
5159 if (src->interleaved)
5160 ret = gst_rtspsrc_loop_interleaved (src);
5162 ret = gst_rtspsrc_loop_udp (src);
5164 if (ret != GST_FLOW_OK)
5172 GST_WARNING_OBJECT (src, "we are not connected");
5173 ret = GST_FLOW_FLUSHING;
5178 const gchar *reason = gst_flow_get_name (ret);
5180 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
5181 src->running = FALSE;
5182 if (ret == GST_FLOW_EOS) {
5183 /* perform EOS logic */
5184 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5185 gst_element_post_message (GST_ELEMENT_CAST (src),
5186 gst_message_new_segment_done (GST_OBJECT_CAST (src),
5187 src->segment.format, src->segment.position));
5188 gst_rtspsrc_push_event (src,
5189 gst_event_new_segment_done (src->segment.format,
5190 src->segment.position));
5192 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5194 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
5195 /* for fatal errors we post an error message, post the error before the
5196 * EOS so the app knows about the error first. */
5197 GST_ELEMENT_ERROR (src, STREAM, FAILED,
5198 ("Internal data flow error."),
5199 ("streaming task paused, reason %s (%d)", reason, ret));
5200 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5202 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
5207 #ifndef GST_DISABLE_GST_DEBUG
5208 static const gchar *
5209 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
5213 while (method != 0) {
5230 static const gchar *
5231 gst_rtspsrc_skip_lws (const gchar * s)
5233 while (g_ascii_isspace (*s))
5238 static const gchar *
5239 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
5241 while (s > start && g_ascii_isspace (*(s - 1)))
5246 static const gchar *
5247 gst_rtspsrc_skip_commas (const gchar * s)
5249 /* The grammar allows for multiple commas */
5250 while (g_ascii_isspace (*s) || *s == ',')
5255 static const gchar *
5256 gst_rtspsrc_skip_item (const gchar * s)
5258 gboolean quoted = FALSE;
5259 const gchar *start = s;
5261 /* A list item ends at the last non-whitespace character
5262 * before a comma which is not inside a quoted-string. Or at
5263 * the end of the string.
5269 if (*s == '\\' && *(s + 1))
5278 return gst_rtspsrc_unskip_lws (s, start);
5282 gst_rtsp_decode_quoted_string (gchar * quoted_string)
5286 src = quoted_string + 1;
5287 dst = quoted_string;
5288 while (*src && *src != '"') {
5289 if (*src == '\\' && *(src + 1))
5296 /* Extract the authentication tokens that the server provided for each method
5297 * into an array of structures and give those to the connection object.
5300 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
5301 const gchar * header, gboolean * stale)
5303 GSList *list = NULL, *iter;
5305 gchar *item, *eq, *name_end, *value;
5307 g_return_if_fail (stale != NULL);
5309 gst_rtsp_connection_clear_auth_params (conn);
5312 /* Parse a header whose content is described by RFC2616 as
5313 * "#something", where "something" does not itself contain commas,
5314 * except as part of quoted-strings, into a list of allocated strings.
5316 header = gst_rtspsrc_skip_commas (header);
5318 end = gst_rtspsrc_skip_item (header);
5319 list = g_slist_prepend (list, g_strndup (header, end - header));
5320 header = gst_rtspsrc_skip_commas (end);
5325 list = g_slist_reverse (list);
5326 for (iter = list; iter; iter = iter->next) {
5329 eq = strchr (item, '=');
5331 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
5332 if (name_end == item) {
5333 /* That's no good... */
5340 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
5342 gst_rtsp_decode_quoted_string (value);
5346 if (value && strcmp (item, "stale") == 0 && strcmp (value, "TRUE") == 0)
5348 gst_rtsp_connection_set_auth_param (conn, item, value);
5352 g_slist_free (list);
5355 /* Parse a WWW-Authenticate Response header and determine the
5356 * available authentication methods
5358 * This code should also cope with the fact that each WWW-Authenticate
5359 * header can contain multiple challenge methods + tokens
5361 * At the moment, for Basic auth, we just do a minimal check and don't
5362 * even parse out the realm */
5364 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
5365 GstRTSPConnection * conn, gboolean * stale)
5369 g_return_if_fail (hdr != NULL);
5370 g_return_if_fail (methods != NULL);
5371 g_return_if_fail (stale != NULL);
5373 /* Skip whitespace at the start of the string */
5374 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
5376 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
5377 *methods |= GST_RTSP_AUTH_BASIC;
5378 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
5379 *methods |= GST_RTSP_AUTH_DIGEST;
5380 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
5385 * gst_rtspsrc_setup_auth:
5386 * @src: the rtsp source
5388 * Configure a username and password and auth method on the
5389 * connection object based on a response we received from the
5392 * Currently, this requires that a username and password were supplied
5393 * in the uri. In the future, they may be requested on demand by sending
5394 * a message up the bus.
5396 * Returns: TRUE if authentication information could be set up correctly.
5399 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
5403 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
5404 GstRTSPAuthMethod method;
5405 GstRTSPResult auth_result;
5407 GstRTSPConnection *conn;
5409 gboolean stale = FALSE;
5411 conn = src->conninfo.connection;
5413 /* Identify the available auth methods and see if any are supported */
5414 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
5415 &hdr, 0) == GST_RTSP_OK) {
5416 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
5419 if (avail_methods == GST_RTSP_AUTH_NONE)
5420 goto no_auth_available;
5422 /* For digest auth, if the response indicates that the session
5423 * data are stale, we just update them in the connection object and
5424 * return TRUE to retry the request */
5426 src->tried_url_auth = FALSE;
5428 url = gst_rtsp_connection_get_url (conn);
5430 /* Do we have username and password available? */
5431 if (url != NULL && !src->tried_url_auth && url->user != NULL
5432 && url->passwd != NULL) {
5435 src->tried_url_auth = TRUE;
5436 GST_DEBUG_OBJECT (src,
5437 "Attempting authentication using credentials from the URL");
5439 user = src->user_id;
5440 pass = src->user_pw;
5441 GST_DEBUG_OBJECT (src,
5442 "Attempting authentication using credentials from the properties");
5445 /* FIXME: If the url didn't contain username and password or we tried them
5446 * already, request a username and passwd from the application via some kind
5447 * of credentials request message */
5449 /* If we don't have a username and passwd at this point, bail out. */
5450 if (user == NULL || pass == NULL)
5453 /* Try to configure for each available authentication method, strongest to
5455 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
5456 /* Check if this method is available on the server */
5457 if ((method & avail_methods) == 0)
5460 /* Pass the credentials to the connection to try on the next request */
5461 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
5462 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
5463 * ignore it and end up retrying later */
5464 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
5465 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
5466 gst_rtsp_auth_method_to_string (method));
5471 if (method == GST_RTSP_AUTH_NONE)
5472 goto no_auth_available;
5478 /* Output an error indicating that we couldn't connect because there were
5479 * no supported authentication protocols */
5480 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5481 ("No supported authentication protocol was found"));
5486 /* We don't fire an error message, we just return FALSE and let the
5487 * normal NOT_AUTHORIZED error be propagated */
5492 static GstRTSPResult
5493 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5494 GstRTSPMessage * request, GstRTSPMessage * response,
5495 GstRTSPStatusCode * code)
5498 GstRTSPStatusCode thecode;
5499 gchar *content_base = NULL;
5503 if (!src->short_header)
5504 gst_rtsp_ext_list_before_send (src->extensions, request);
5506 GST_DEBUG_OBJECT (src, "sending message");
5509 gst_rtsp_message_dump (request);
5511 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
5515 gst_rtsp_connection_reset_timeout (conn);
5518 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
5523 gst_rtsp_message_dump (response);
5525 switch (response->type) {
5526 case GST_RTSP_MESSAGE_REQUEST:
5527 res = gst_rtspsrc_handle_request (src, conn, response);
5528 if (res == GST_RTSP_EEOF)
5531 goto handle_request_failed;
5533 case GST_RTSP_MESSAGE_RESPONSE:
5534 /* ok, a response is good */
5535 GST_DEBUG_OBJECT (src, "received response message");
5537 case GST_RTSP_MESSAGE_DATA:
5538 /* get next response */
5539 GST_DEBUG_OBJECT (src, "handle data response message");
5540 gst_rtspsrc_handle_data (src, response);
5543 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5548 thecode = response->type_data.response.code;
5550 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
5552 /* if the caller wanted the result code, we store it. */
5556 /* If the request didn't succeed, bail out before doing any more */
5557 if (thecode != GST_RTSP_STS_OK)
5560 /* store new content base if any */
5561 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
5564 g_free (src->content_base);
5565 src->content_base = g_strdup (content_base);
5567 gst_rtsp_ext_list_after_send (src->extensions, request, response);
5574 gchar *str = gst_rtsp_strresult (res);
5576 if (res != GST_RTSP_EINTR) {
5577 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5578 ("Could not send message. (%s)", str));
5580 GST_WARNING_OBJECT (src, "send interrupted");
5589 GST_WARNING_OBJECT (src, "server closed connection");
5590 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
5592 /* if reconnect succeeds, try again */
5594 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
5598 /* only try once after reconnect, then fallthrough and error out */
5601 gchar *str = gst_rtsp_strresult (res);
5603 if (res != GST_RTSP_EINTR) {
5604 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5605 ("Could not receive message. (%s)", str));
5607 GST_WARNING_OBJECT (src, "receive interrupted");
5615 handle_request_failed:
5617 /* ERROR was posted */
5618 gst_rtsp_message_unset (response);
5623 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5624 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5625 ("The server closed the connection."));
5626 gst_rtsp_message_unset (response);
5633 * @src: the rtsp source
5634 * @conn: the connection to send on
5635 * @request: must point to a valid request
5636 * @response: must point to an empty #GstRTSPMessage
5637 * @code: an optional code result
5639 * send @request and retrieve the response in @response. optionally @code can be
5640 * non-NULL in which case it will contain the status code of the response.
5642 * If This function returns #GST_RTSP_OK, @response will contain a valid response
5643 * message that should be cleaned with gst_rtsp_message_unset() after usage.
5645 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
5646 * @response message) if the response code was not 200 (OK).
5648 * If the attempt results in an authentication failure, then this will attempt
5649 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
5652 * Returns: #GST_RTSP_OK if the processing was successful.
5654 static GstRTSPResult
5655 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5656 GstRTSPMessage * request, GstRTSPMessage * response,
5657 GstRTSPStatusCode * code)
5659 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
5660 GstRTSPResult res = GST_RTSP_ERROR;
5663 GstRTSPMethod method = GST_RTSP_INVALID;
5669 /* make sure we don't loop forever */
5673 /* save method so we can disable it when the server complains */
5674 method = request->type_data.request.method;
5677 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
5681 case GST_RTSP_STS_UNAUTHORIZED:
5682 if (gst_rtspsrc_setup_auth (src, response)) {
5683 /* Try the request/response again after configuring the auth info
5691 } while (retry == TRUE);
5693 /* If the user requested the code, let them handle errors, otherwise
5694 * post an error below */
5697 else if (int_code != GST_RTSP_STS_OK)
5698 goto error_response;
5705 GST_DEBUG_OBJECT (src, "got error %d", res);
5710 res = GST_RTSP_ERROR;
5712 switch (response->type_data.response.code) {
5713 case GST_RTSP_STS_NOT_FOUND:
5714 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
5715 response->type_data.response.reason));
5717 case GST_RTSP_STS_MOVED_PERMANENTLY:
5718 case GST_RTSP_STS_MOVE_TEMPORARILY:
5720 gchar *new_location;
5721 GstRTSPLowerTrans transports;
5723 GST_DEBUG_OBJECT (src, "got redirection");
5724 /* if we don't have a Location Header, we must error */
5725 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
5726 &new_location, 0) < 0)
5729 /* When we receive a redirect result, we go back to the INIT state after
5730 * parsing the new URI. The caller should do the needed steps to issue
5731 * a new setup when it detects this state change. */
5732 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
5734 /* save current transports */
5735 if (src->conninfo.url)
5736 transports = src->conninfo.url->transports;
5738 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
5740 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
5742 /* set old transports */
5743 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
5744 src->conninfo.url->transports = transports;
5746 src->need_redirect = TRUE;
5747 src->state = GST_RTSP_STATE_INIT;
5751 case GST_RTSP_STS_NOT_ACCEPTABLE:
5752 case GST_RTSP_STS_NOT_IMPLEMENTED:
5753 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
5754 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
5755 gst_rtsp_method_as_text (method));
5756 src->methods &= ~method;
5760 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5761 ("Got error response: %d (%s).", response->type_data.response.code,
5762 response->type_data.response.reason));
5765 /* if we return ERROR we should unset the response ourselves */
5766 if (res == GST_RTSP_ERROR)
5767 gst_rtsp_message_unset (response);
5773 static GstRTSPResult
5774 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
5775 GstRTSPMessage * response, GstRTSPSrc * src)
5777 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
5782 /* parse the response and collect all the supported methods. We need this
5783 * information so that we don't try to send an unsupported request to the
5787 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
5789 GstRTSPHeaderField field;
5793 /* reset supported methods */
5796 /* Try Allow Header first */
5797 field = GST_RTSP_HDR_ALLOW;
5800 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5801 if (indx == 0 && !respoptions) {
5802 /* if no Allow header was found then try the Public header... */
5803 field = GST_RTSP_HDR_PUBLIC;
5804 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5809 src->methods |= gst_rtsp_options_from_text (respoptions);
5814 if (src->methods == 0) {
5815 /* neither Allow nor Public are required, assume the server supports
5816 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
5818 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
5819 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
5821 /* always assume PLAY, FIXME, extensions should be able to override
5823 src->methods |= GST_RTSP_PLAY;
5824 /* also assume it will support Range */
5825 src->seekable = TRUE;
5827 /* we need describe and setup */
5828 if (!(src->methods & GST_RTSP_DESCRIBE))
5830 if (!(src->methods & GST_RTSP_SETUP))
5838 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5839 ("Server does not support DESCRIBE."));
5844 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5845 ("Server does not support SETUP."));
5850 /* masks to be kept in sync with the hardcoded protocol order of preference
5852 static guint protocol_masks[] = {
5853 GST_RTSP_LOWER_TRANS_UDP,
5854 GST_RTSP_LOWER_TRANS_UDP_MCAST,
5855 GST_RTSP_LOWER_TRANS_TCP,
5859 static GstRTSPResult
5860 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
5861 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
5865 gboolean add_udp_str;
5870 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
5875 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
5877 /* extension listed transports, use those */
5878 if (*transports != NULL)
5881 /* it's the default */
5882 add_udp_str = FALSE;
5884 /* the default RTSP transports */
5885 result = g_string_new ("RTP");
5888 case GST_RTSP_PROFILE_AVP:
5889 g_string_append (result, "/AVP");
5891 case GST_RTSP_PROFILE_SAVP:
5892 g_string_append (result, "/SAVP");
5894 case GST_RTSP_PROFILE_AVPF:
5895 g_string_append (result, "/AVPF");
5897 case GST_RTSP_PROFILE_SAVPF:
5898 g_string_append (result, "/SAVPF");
5904 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
5905 GST_DEBUG_OBJECT (src, "adding UDP unicast");
5907 g_string_append (result, "/UDP");
5908 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
5909 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
5910 GST_DEBUG_OBJECT (src, "adding UDP multicast");
5911 /* we don't have to allocate any UDP ports yet, if the selected transport
5912 * turns out to be multicast we can create them and join the multicast
5913 * group indicated in the transport reply */
5915 g_string_append (result, "/UDP");
5916 g_string_append (result, ";multicast");
5917 if (src->next_port_num != 0) {
5918 if (src->client_port_range.max > 0 &&
5919 src->next_port_num >= src->client_port_range.max)
5922 g_string_append_printf (result, ";client_port=%d-%d",
5923 src->next_port_num, src->next_port_num + 1);
5925 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
5926 GST_DEBUG_OBJECT (src, "adding TCP");
5928 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
5930 *transports = g_string_free (result, FALSE);
5932 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
5939 GST_ERROR ("extension gave error %d", res);
5944 GST_ERROR ("no more ports available");
5945 return GST_RTSP_ERROR;
5949 static GstRTSPResult
5950 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
5951 gint orig_rtpport, gint orig_rtcpport)
5954 gint nr_udp, nr_int;
5956 gint rtpport = 0, rtcpport = 0;
5959 src = stream->parent;
5961 /* find number of placeholders first */
5962 if (strstr (*transports, "%%i2"))
5964 else if (strstr (*transports, "%%i1"))
5969 if (strstr (*transports, "%%u2"))
5971 else if (strstr (*transports, "%%u1"))
5976 if (nr_udp == 0 && nr_int == 0)
5980 if (!orig_rtpport || !orig_rtcpport) {
5981 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
5984 rtpport = orig_rtpport;
5985 rtcpport = orig_rtcpport;
5989 str = g_string_new ("");
5991 while ((next = strstr (p, "%%"))) {
5992 g_string_append_len (str, p, next - p);
5993 if (next[2] == 'u') {
5995 g_string_append_printf (str, "%d", rtpport);
5996 else if (next[3] == '2')
5997 g_string_append_printf (str, "%d", rtcpport);
5999 if (next[2] == 'i') {
6001 g_string_append_printf (str, "%d", src->free_channel);
6002 else if (next[3] == '2')
6003 g_string_append_printf (str, "%d", src->free_channel + 1);
6008 /* append final part */
6009 g_string_append (str, p);
6011 g_free (*transports);
6012 *transports = g_string_free (str, FALSE);
6020 GST_ERROR ("failed to allocate udp ports");
6021 return GST_RTSP_ERROR;
6026 enc_key_length_from_cipher_name (const gchar * cipher)
6028 if (g_strcmp0 (cipher, "aes-128-icm") == 0)
6029 return AES_128_KEY_LEN;
6030 else if (g_strcmp0 (cipher, "aes-256-icm") == 0)
6031 return AES_256_KEY_LEN;
6033 GST_ERROR ("encryption algorithm '%s' not supported", cipher);
6039 auth_key_length_from_auth_name (const gchar * auth)
6041 if (g_strcmp0 (auth, "hmac-sha1-32") == 0)
6042 return HMAC_32_KEY_LEN;
6043 else if (g_strcmp0 (auth, "hmac-sha1-80") == 0)
6044 return HMAC_80_KEY_LEN;
6046 GST_ERROR ("authentication algorithm '%s' not supported", auth);
6052 signal_get_srtcp_params (GstRTSPSrc * src, GstRTSPStream * stream)
6054 GstCaps *caps = NULL;
6056 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY], 0,
6060 GST_DEBUG_OBJECT (src, "SRTP parameters received");
6066 default_srtcp_params (void)
6074 /* create a random key */
6075 key_data = g_malloc (KEY_SIZE);
6076 for (i = 0; i < KEY_SIZE; i += 4)
6077 GST_WRITE_UINT32_BE (key_data + i, g_random_int ());
6079 buf = gst_buffer_new_wrapped (key_data, KEY_SIZE);
6081 caps = gst_caps_new_simple ("application/x-srtp",
6082 "srtp-key", GST_TYPE_BUFFER, buf,
6083 "srtcp-cipher", G_TYPE_STRING, "aes-128-icm",
6084 "srtcp-auth", G_TYPE_STRING, "hmac-sha1-80", NULL);
6086 gst_buffer_unref (buf);
6092 gst_rtspsrc_stream_make_keymgmt (GstRTSPSrc * src, GstRTSPStream * stream)
6095 gchar *result, *base64;
6098 GstMIKEYMessage *msg;
6099 GstMIKEYPayload *payload, *pkd;
6105 const gchar *srtcpcipher, *srtcpauth;
6107 stream->srtcpparams = signal_get_srtcp_params (src, stream);
6108 if (stream->srtcpparams == NULL)
6109 stream->srtcpparams = default_srtcp_params ();
6111 s = gst_caps_get_structure (stream->srtcpparams, 0);
6113 srtcpcipher = gst_structure_get_string (s, "srtcp-cipher");
6114 srtcpauth = gst_structure_get_string (s, "srtcp-auth");
6115 val = gst_structure_get_value (s, "srtp-key");
6117 if (srtcpcipher == NULL || srtcpauth == NULL || val == NULL) {
6118 GST_ERROR_OBJECT (src, "could not find the right SRTP parameters in caps");
6122 srtpkey = gst_value_get_buffer (val);
6124 msg = gst_mikey_message_new ();
6125 /* unencrypted MIKEY message, we send this over TLS so this is allowed */
6126 gst_mikey_message_set_info (msg, GST_MIKEY_VERSION, GST_MIKEY_TYPE_PSK_INIT,
6127 FALSE, GST_MIKEY_PRF_MIKEY_1, g_random_int (), GST_MIKEY_MAP_TYPE_SRTP);
6128 /* add policy '0' for our SSRC */
6129 gst_mikey_message_add_cs_srtp (msg, 0, stream->send_ssrc, 0);
6130 /* timestamp is now */
6131 gst_mikey_message_add_t_now_ntp_utc (msg);
6132 /* add some random data */
6133 gst_mikey_message_add_rand_len (msg, 16);
6135 /* the policy '0' is SRTP */
6136 payload = gst_mikey_payload_new (GST_MIKEY_PT_SP);
6137 gst_mikey_payload_sp_set (payload, 0, GST_MIKEY_SEC_PROTO_SRTP);
6139 /* only AES-CM is supported */
6141 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_ALG, 1, &byte);
6142 /* encryption key length */
6143 byte = enc_key_length_from_cipher_name (srtcpcipher);
6144 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_KEY_LEN, 1,
6146 /* only HMAC-SHA1 */
6147 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_ALG, 1,
6149 /* authentication key length */
6150 byte = auth_key_length_from_auth_name (srtcpauth);
6151 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_KEY_LEN, 1,
6153 /* we enable encryption on RTP and RTCP */
6154 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_ENC, 1,
6156 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTCP_ENC, 1,
6158 /* we enable authentication on RTP and RTCP */
6159 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_AUTH, 1,
6161 gst_mikey_message_add_payload (msg, payload);
6163 /* make unencrypted KEMAC */
6164 payload = gst_mikey_payload_new (GST_MIKEY_PT_KEMAC);
6165 gst_mikey_payload_kemac_set (payload, GST_MIKEY_ENC_NULL, GST_MIKEY_MAC_NULL);
6166 /* add the key in KEMAC */
6167 pkd = gst_mikey_payload_new (GST_MIKEY_PT_KEY_DATA);
6168 gst_buffer_map (srtpkey, &info, GST_MAP_READ);
6169 gst_mikey_payload_key_data_set_key (pkd, GST_MIKEY_KD_TEK, info.size,
6171 gst_buffer_unmap (srtpkey, &info);
6172 gst_mikey_payload_kemac_add_sub (payload, pkd);
6173 gst_mikey_message_add_payload (msg, payload);
6175 /* now serialize this to bytes */
6176 bytes = gst_mikey_message_to_bytes (msg, NULL, NULL);
6177 gst_mikey_message_unref (msg);
6178 /* and make it into base64 */
6179 data = g_bytes_get_data (bytes, &size);
6180 base64 = g_base64_encode (data, size);
6181 g_bytes_unref (bytes);
6183 result = g_strdup_printf ("prot=mikey;uri=\"%s\";data=\"%s\"",
6184 stream->conninfo.location, base64);
6191 /* Perform the SETUP request for all the streams.
6193 * We ask the server for a specific transport, which initially includes all the
6194 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
6195 * two local UDP ports that we send to the server.
6197 * Once the server replied with a transport, we configure the other streams
6198 * with the same transport.
6200 * This function will also configure the stream for the selected transport,
6201 * which basically means creating the pipeline.
6203 static GstRTSPResult
6204 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
6207 GstRTSPResult res = GST_RTSP_ERROR;
6208 GstRTSPMessage request = { 0 };
6209 GstRTSPMessage response = { 0 };
6210 GstRTSPStream *stream = NULL;
6211 GstRTSPLowerTrans protocols;
6212 GstRTSPStatusCode code;
6213 gboolean unsupported_real = FALSE;
6214 gint rtpport, rtcpport;
6218 if (src->conninfo.connection) {
6219 url = gst_rtsp_connection_get_url (src->conninfo.connection);
6220 /* we initially allow all configured lower transports. based on the URL
6221 * transports and the replies from the server we narrow them down. */
6222 protocols = url->transports & src->cur_protocols;
6225 protocols = src->cur_protocols;
6231 /* reset some state */
6232 src->free_channel = 0;
6233 src->interleaved = FALSE;
6234 src->need_activate = FALSE;
6235 /* keep track of next port number, 0 is random */
6236 src->next_port_num = src->client_port_range.min;
6237 rtpport = rtcpport = 0;
6239 if (G_UNLIKELY (src->streams == NULL))
6242 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6243 GstRTSPConnection *conn;
6250 stream = (GstRTSPStream *) walk->data;
6252 caps = stream_get_caps_for_pt (stream, stream->default_pt);
6254 GST_DEBUG_OBJECT (src, "skipping stream %p, no caps", stream);
6258 if (stream->skipped) {
6259 GST_DEBUG_OBJECT (src, "skipping stream %p", stream);
6263 /* see if we need to configure this stream */
6264 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
6265 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
6270 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
6271 stream->id, caps, &selected);
6273 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
6277 /* merge/overwrite global caps */
6282 s = gst_caps_get_structure (caps, 0);
6284 num = gst_structure_n_fields (src->props);
6285 for (j = 0; j < num; j++) {
6289 name = gst_structure_nth_field_name (src->props, j);
6290 val = gst_structure_get_value (src->props, name);
6291 gst_structure_set_value (s, name, val);
6293 GST_DEBUG_OBJECT (src, "copied %s", name);
6297 /* skip setup if we have no URL for it */
6298 if (stream->conninfo.location == NULL) {
6299 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
6303 if (src->conninfo.connection == NULL) {
6304 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
6305 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
6308 conn = stream->conninfo.connection;
6310 conn = src->conninfo.connection;
6312 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
6313 stream->conninfo.location);
6315 /* if we have a multicast connection, only suggest multicast from now on */
6316 if (stream->is_multicast)
6317 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
6320 /* first selectable protocol */
6321 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6323 if (!protocol_masks[mask])
6327 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
6328 protocol_masks[mask]);
6329 /* create a string with first transport in line */
6331 res = gst_rtspsrc_create_transports_string (src,
6332 protocols & protocol_masks[mask], stream->profile, &transports);
6333 if (res < 0 || transports == NULL)
6334 goto setup_transport_failed;
6336 if (strlen (transports) == 0) {
6337 g_free (transports);
6338 GST_DEBUG_OBJECT (src, "no transports found");
6343 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
6345 /* replace placeholders with real values, this function will optionally
6346 * allocate UDP ports and other info needed to execute the setup request */
6347 res = gst_rtspsrc_prepare_transports (stream, &transports,
6348 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
6350 g_free (transports);
6351 goto setup_transport_failed;
6354 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
6356 /* create SETUP request */
6358 gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
6359 stream->conninfo.location);
6361 g_free (transports);
6362 goto create_request_failed;
6365 /* select transport */
6366 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
6369 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
6370 stream->profile == GST_RTSP_PROFILE_SAVPF) {
6371 hval = gst_rtspsrc_stream_make_keymgmt (src, stream);
6372 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
6375 /* if the user wants a non default RTP packet size we add the blocksize
6377 if (src->rtp_blocksize > 0) {
6378 hval = g_strdup_printf ("%d", src->rtp_blocksize);
6379 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
6383 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
6386 /* handle the code ourselves */
6387 res = gst_rtspsrc_send (src, conn, &request, &response, &code);
6392 case GST_RTSP_STS_OK:
6394 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
6395 gst_rtsp_message_unset (&request);
6396 gst_rtsp_message_unset (&response);
6397 /* cleanup of leftover transport */
6398 gst_rtspsrc_stream_free_udp (stream);
6399 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
6400 * we might be in this case */
6401 if (stream->container && rtpport && rtcpport && !retry) {
6402 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
6407 /* this transport did not go down well, but we may have others to try
6408 * that we did not send yet, try those and only give up then
6409 * but not without checking for lost cause/extension so we can
6410 * post a nicer/more useful error message later */
6411 if (!unsupported_real)
6412 unsupported_real = stream->is_real;
6413 /* select next available protocol, give up on this stream if none */
6415 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6417 if (!protocol_masks[mask] || unsupported_real)
6422 /* cleanup of leftover transport and move to the next stream */
6423 gst_rtspsrc_stream_free_udp (stream);
6424 goto response_error;
6427 /* parse response transport */
6429 gchar *resptrans = NULL;
6430 GstRTSPTransport transport = { 0 };
6432 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
6435 gst_rtspsrc_stream_free_udp (stream);
6439 /* parse transport, go to next stream on parse error */
6440 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
6441 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
6445 /* update allowed transports for other streams. once the transport of
6446 * one stream has been determined, we make sure that all other streams
6447 * are configured in the same way */
6448 switch (transport.lower_transport) {
6449 case GST_RTSP_LOWER_TRANS_TCP:
6450 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
6451 protocols = GST_RTSP_LOWER_TRANS_TCP;
6452 src->interleaved = TRUE;
6453 /* update free channels */
6455 MAX (transport.interleaved.min, src->free_channel);
6457 MAX (transport.interleaved.max, src->free_channel);
6458 src->free_channel++;
6460 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
6461 /* only allow multicast for other streams */
6462 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
6463 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
6464 /* if the server selected our ports, increment our counters so that
6465 * we select a new port later */
6466 if (src->next_port_num == transport.port.min &&
6467 src->next_port_num + 1 == transport.port.max) {
6468 src->next_port_num += 2;
6471 case GST_RTSP_LOWER_TRANS_UDP:
6472 /* only allow unicast for other streams */
6473 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
6474 protocols = GST_RTSP_LOWER_TRANS_UDP;
6477 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
6478 transport.lower_transport);
6482 if (!stream->container || (!src->interleaved && !retry)) {
6483 /* now configure the stream with the selected transport */
6484 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
6485 GST_DEBUG_OBJECT (src,
6486 "could not configure stream %p transport, skipping stream",
6489 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
6490 /* retain the first allocated UDP port pair */
6491 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
6492 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
6495 /* we need to activate at least one streams when we detect activity */
6496 src->need_activate = TRUE;
6498 /* stream is setup now */
6499 stream->setup = TRUE;
6504 GstRTSPStream *sskip;
6506 skip = g_list_next (skip);
6510 sskip = (GstRTSPStream *) skip->data;
6512 /* skip all streams with the same control url */
6513 if (g_str_equal (stream->conninfo.location, sskip->conninfo.location)) {
6514 GST_DEBUG_OBJECT (src, "found stream %p with same control %s",
6515 sskip, sskip->conninfo.location);
6516 sskip->skipped = TRUE;
6521 /* clean up our transport struct */
6522 gst_rtsp_transport_init (&transport);
6523 /* clean up used RTSP messages */
6524 gst_rtsp_message_unset (&request);
6525 gst_rtsp_message_unset (&response);
6529 /* store the transport protocol that was configured */
6530 src->cur_protocols = protocols;
6532 gst_rtsp_ext_list_stream_select (src->extensions, url);
6534 /* if there is nothing to activate, error out */
6535 if (!src->need_activate)
6536 goto nothing_to_activate;
6543 /* no transport possible, post an error and stop */
6544 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6545 ("Could not connect to server, no protocols left"));
6546 return GST_RTSP_ERROR;
6550 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6551 ("SDP contains no streams"));
6552 return GST_RTSP_ERROR;
6554 create_request_failed:
6556 gchar *str = gst_rtsp_strresult (res);
6558 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6559 ("Could not create request. (%s)", str));
6563 setup_transport_failed:
6565 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6566 ("Could not setup transport."));
6567 res = GST_RTSP_ERROR;
6572 const gchar *str = gst_rtsp_status_as_text (code);
6574 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6575 ("Error (%d): %s", code, GST_STR_NULL (str)));
6576 res = GST_RTSP_ERROR;
6581 gchar *str = gst_rtsp_strresult (res);
6583 if (res != GST_RTSP_EINTR) {
6584 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6585 ("Could not send message. (%s)", str));
6587 GST_WARNING_OBJECT (src, "send interrupted");
6594 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6595 ("Server did not select transport."));
6596 res = GST_RTSP_ERROR;
6599 nothing_to_activate:
6601 /* none of the available error codes is really right .. */
6602 if (unsupported_real) {
6603 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6604 (_("No supported stream was found. You might need to install a "
6605 "GStreamer RTSP extension plugin for Real media streams.")),
6608 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6609 (_("No supported stream was found. You might need to allow "
6610 "more transport protocols or may otherwise be missing "
6611 "the right GStreamer RTSP extension plugin.")), (NULL));
6613 return GST_RTSP_ERROR;
6617 gst_rtsp_message_unset (&request);
6618 gst_rtsp_message_unset (&response);
6624 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
6625 GstSegment * segment)
6628 GstRTSPTimeRange *therange;
6631 gst_rtsp_range_free (src->range);
6633 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
6634 GST_DEBUG_OBJECT (src, "parsed range %s", range);
6635 src->range = therange;
6637 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
6639 gst_segment_init (segment, GST_FORMAT_TIME);
6643 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
6644 therange->min.type, therange->min.seconds, therange->max.type,
6645 therange->max.seconds);
6647 if (therange->min.type == GST_RTSP_TIME_NOW)
6649 else if (therange->min.type == GST_RTSP_TIME_END)
6652 seconds = therange->min.seconds * GST_SECOND;
6654 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
6655 GST_TIME_ARGS (seconds));
6657 /* we need to start playback without clipping from the position reported by
6659 segment->start = seconds;
6660 segment->position = seconds;
6662 if (therange->max.type == GST_RTSP_TIME_NOW)
6664 else if (therange->max.type == GST_RTSP_TIME_END)
6667 seconds = therange->max.seconds * GST_SECOND;
6669 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
6670 GST_TIME_ARGS (seconds));
6672 /* live (WMS) server might send overflowed large max as its idea of infinity,
6673 * compensate to prevent problems later on */
6674 if (seconds != -1 && seconds < 0) {
6676 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
6679 /* live (WMS) might send min == max, which is not worth recording */
6680 if (segment->duration == -1 && seconds == segment->start)
6683 /* don't change duration with unknown value, we might have a valid value
6684 * there that we want to keep. */
6686 segment->duration = seconds;
6691 /* Parse clock profived by the server with following syntax:
6693 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
6696 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
6698 gboolean res = FALSE;
6700 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
6701 gchar **fields = NULL, **parts = NULL;
6702 gchar *remote_ip, *str;
6704 GstClockTime base_time;
6707 fields = g_strsplit (gstclock, " ", 0);
6709 /* wrapped clock, not very interesting for now */
6710 if (fields[1] == NULL)
6713 /* remote IP address and port */
6714 if ((str = fields[2]) == NULL)
6717 parts = g_strsplit (str, ":", 0);
6719 if ((remote_ip = parts[0]) == NULL)
6722 if ((str = parts[1]) == NULL)
6730 if ((str = fields[3]) == NULL)
6733 base_time = g_ascii_strtoull (str, NULL, 10);
6736 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
6739 if (src->provided_clock)
6740 gst_object_unref (src->provided_clock);
6741 src->provided_clock = netclock;
6743 gst_element_post_message (GST_ELEMENT_CAST (src),
6744 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
6745 src->provided_clock, TRUE));
6749 g_strfreev (fields);
6755 /* must be called with the RTSP state lock */
6756 static GstRTSPResult
6757 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
6763 /* prepare global stream caps properties */
6765 gst_structure_remove_all_fields (src->props);
6767 src->props = gst_structure_new_empty ("RTSPProperties");
6770 gst_sdp_message_dump (sdp);
6772 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
6774 /* let the app inspect and change the SDP */
6775 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
6777 gst_segment_init (&src->segment, GST_FORMAT_TIME);
6779 /* parse range for duration reporting. */
6784 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
6788 /* keep track of the range and configure it in the segment */
6789 if (gst_rtspsrc_parse_range (src, range, &src->segment))
6793 /* parse clock information. This is GStreamer specific, a server can tell the
6794 * client what clock it is using and wrap that in a network clock. The
6795 * advantage of that is that we can slave to it. */
6797 const gchar *gstclock;
6800 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
6801 if (gstclock == NULL)
6804 /* parse the clock and expose it in the provide_clock method */
6805 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
6809 /* try to find a global control attribute. Note that a '*' means that we should
6810 * do aggregate control with the current url (so we don't do anything and
6811 * leave the current connection as is) */
6813 const gchar *control;
6816 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
6817 if (control == NULL)
6820 /* only take fully qualified urls */
6821 if (g_str_has_prefix (control, "rtsp://"))
6825 g_free (src->conninfo.location);
6826 src->conninfo.location = g_strdup (control);
6827 /* make a connection for this, if there was a connection already, nothing
6829 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
6830 GST_ERROR_OBJECT (src, "could not connect");
6833 /* we need to keep the control url separate from the connection url because
6834 * the rules for constructing the media control url need it */
6835 g_free (src->control);
6836 src->control = g_strdup (control);
6839 /* create streams */
6840 n_streams = gst_sdp_message_medias_len (sdp);
6841 for (i = 0; i < n_streams; i++) {
6842 gst_rtspsrc_create_stream (src, sdp, i);
6845 src->state = GST_RTSP_STATE_INIT;
6848 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
6851 /* reset our state */
6852 src->need_range = TRUE;
6855 src->state = GST_RTSP_STATE_READY;
6862 GST_ERROR_OBJECT (src, "setup failed");
6863 gst_rtspsrc_cleanup (src);
6868 static GstRTSPResult
6869 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
6873 GstRTSPMessage request = { 0 };
6874 GstRTSPMessage response = { 0 };
6877 gchar *respcont = NULL;
6880 src->need_redirect = FALSE;
6882 /* can't continue without a valid url */
6883 if (G_UNLIKELY (src->conninfo.url == NULL)) {
6884 res = GST_RTSP_EINVAL;
6887 src->tried_url_auth = FALSE;
6889 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
6890 goto connect_failed;
6892 /* create OPTIONS */
6893 GST_DEBUG_OBJECT (src, "create options...");
6895 gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
6896 src->conninfo.url_str);
6898 goto create_request_failed;
6901 GST_DEBUG_OBJECT (src, "send options...");
6904 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
6907 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6912 if (!gst_rtspsrc_parse_methods (src, &response))
6915 /* create DESCRIBE */
6916 GST_DEBUG_OBJECT (src, "create describe...");
6918 gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
6919 src->conninfo.url_str);
6921 goto create_request_failed;
6923 /* we only accept SDP for now */
6924 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
6928 GST_DEBUG_OBJECT (src, "send describe...");
6931 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
6934 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6938 /* we only perform redirect for the describe, currently */
6939 if (src->need_redirect) {
6940 /* close connection, we don't have to send a TEARDOWN yet, ignore the
6942 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6944 gst_rtsp_message_unset (&request);
6945 gst_rtsp_message_unset (&response);
6951 /* it could be that the DESCRIBE method was not implemented */
6952 if (!src->methods & GST_RTSP_DESCRIBE)
6955 /* check if reply is SDP */
6956 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
6958 /* could not be set but since the request returned OK, we assume it
6959 * was SDP, else check it. */
6961 if (!g_ascii_strcasecmp (respcont, "application/sdp") == 0)
6962 goto wrong_content_type;
6965 /* get message body and parse as SDP */
6966 gst_rtsp_message_get_body (&response, &data, &size);
6967 if (data == NULL || size == 0)
6970 GST_DEBUG_OBJECT (src, "parse SDP...");
6971 gst_sdp_message_new (sdp);
6972 gst_sdp_message_parse_buffer (data, size, *sdp);
6974 /* clean up any messages */
6975 gst_rtsp_message_unset (&request);
6976 gst_rtsp_message_unset (&response);
6983 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
6984 ("No valid RTSP URL was provided"));
6989 gchar *str = gst_rtsp_strresult (res);
6991 if (res != GST_RTSP_EINTR) {
6992 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
6993 ("Failed to connect. (%s)", str));
6995 GST_WARNING_OBJECT (src, "connect interrupted");
7000 create_request_failed:
7002 gchar *str = gst_rtsp_strresult (res);
7004 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7005 ("Could not create request. (%s)", str));
7011 /* Don't post a message - the rtsp_send method will have
7012 * taken care of it because we passed NULL for the response code */
7017 /* error was posted */
7018 res = GST_RTSP_ERROR;
7023 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7024 ("Server does not support SDP, got %s.", respcont));
7025 res = GST_RTSP_ERROR;
7030 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7031 ("Server can not provide an SDP."));
7032 res = GST_RTSP_ERROR;
7037 if (src->conninfo.connection) {
7038 GST_DEBUG_OBJECT (src, "free connection");
7039 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7041 gst_rtsp_message_unset (&request);
7042 gst_rtsp_message_unset (&response);
7047 static GstRTSPResult
7048 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
7053 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
7055 if (src->sdp == NULL) {
7056 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
7060 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
7065 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
7072 GST_WARNING_OBJECT (src, "can't get sdp");
7073 src->open_error = TRUE;
7078 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
7079 src->open_error = TRUE;
7084 static GstRTSPResult
7085 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
7087 GstRTSPMessage request = { 0 };
7088 GstRTSPMessage response = { 0 };
7089 GstRTSPResult res = GST_RTSP_OK;
7091 const gchar *control;
7093 GST_DEBUG_OBJECT (src, "TEARDOWN...");
7095 gst_rtspsrc_set_state (src, GST_STATE_READY);
7097 if (src->state < GST_RTSP_STATE_READY) {
7098 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
7105 /* construct a control url */
7106 control = get_aggregate_control (src);
7108 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
7111 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7112 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7113 const gchar *setup_url;
7114 GstRTSPConnInfo *info;
7116 /* try aggregate control first but do non-aggregate control otherwise */
7118 setup_url = control;
7119 else if ((setup_url = stream->conninfo.location) == NULL)
7122 if (src->conninfo.connection) {
7123 info = &src->conninfo;
7124 } else if (stream->conninfo.connection) {
7125 info = &stream->conninfo;
7129 if (!info->connected)
7134 gst_rtsp_message_init_request (&request, GST_RTSP_TEARDOWN, setup_url);
7136 goto create_request_failed;
7139 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
7142 gst_rtspsrc_send (src, info->connection, &request, &response,
7146 /* FIXME, parse result? */
7147 gst_rtsp_message_unset (&request);
7148 gst_rtsp_message_unset (&response);
7151 /* early exit when we did aggregate control */
7157 /* close connections */
7158 GST_DEBUG_OBJECT (src, "closing connection...");
7159 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7160 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7161 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7162 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
7166 gst_rtspsrc_cleanup (src);
7168 src->state = GST_RTSP_STATE_INVALID;
7171 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
7176 create_request_failed:
7178 gchar *str = gst_rtsp_strresult (res);
7180 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7181 ("Could not create request. (%s)", str));
7187 gchar *str = gst_rtsp_strresult (res);
7189 gst_rtsp_message_unset (&request);
7190 if (res != GST_RTSP_EINTR) {
7191 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7192 ("Could not send message. (%s)", str));
7194 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
7201 GST_DEBUG_OBJECT (src,
7202 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
7207 /* RTP-Info is of the format:
7209 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
7211 * rtptime corresponds to the timestamp for the NPT time given in the header
7212 * seqbase corresponds to the next sequence number we received. This number
7213 * indicates the first seqnum after the seek and should be used to discard
7214 * packets that are from before the seek.
7217 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
7222 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
7224 infos = g_strsplit (rtpinfo, ",", 0);
7225 for (i = 0; infos[i]; i++) {
7227 GstRTSPStream *stream;
7231 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
7233 /* init values, types of seqbase and timebase are bigger than needed so we
7234 * can store -1 as uninitialized values */
7239 /* parse url, find stream for url.
7240 * parse seq and rtptime. The seq number should be configured in the rtp
7241 * depayloader or session manager to detect gaps. Same for the rtptime, it
7242 * should be used to create an initial time newsegment. */
7243 fields = g_strsplit (infos[i], ";", 0);
7244 for (j = 0; fields[j]; j++) {
7245 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
7246 /* remove leading whitespace */
7247 fields[j] = g_strchug (fields[j]);
7248 if (g_str_has_prefix (fields[j], "url=")) {
7249 /* get the url and the stream */
7251 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
7252 } else if (g_str_has_prefix (fields[j], "seq=")) {
7253 seqbase = atoi (fields[j] + 4);
7254 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
7255 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
7258 g_strfreev (fields);
7259 /* now we need to store the values for the caps of the stream */
7260 if (stream != NULL) {
7261 GST_DEBUG_OBJECT (src,
7262 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
7263 stream, seqbase, timebase);
7265 /* we have a stream, configure detected params */
7266 stream->seqbase = seqbase;
7267 stream->timebase = timebase;
7276 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
7281 interval = strtoul (rtcp, NULL, 10);
7282 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
7287 interval *= GST_MSECOND;
7289 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7290 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7292 /* already (optionally) retrieved this when configuring manager */
7293 if (stream->session) {
7294 GObject *rtpsession = stream->session;
7296 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
7298 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
7302 /* now it happens that (Xenon) server sending this may also provide bogus
7303 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
7304 * and just use RTP-Info to sync */
7306 GObjectClass *klass;
7308 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
7309 if (g_object_class_find_property (klass, "rtcp-sync")) {
7310 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
7311 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
7317 gst_rtspsrc_get_float (const gchar * dstr)
7319 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7321 /* canonicalise floating point string so we can handle float strings
7322 * in the form "24.930" or "24,930" irrespective of the current locale */
7323 g_strlcpy (s, dstr, sizeof (s));
7324 g_strdelimit (s, ",", '.');
7325 return g_ascii_strtod (s, NULL);
7329 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
7331 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7333 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
7334 g_strlcpy (val_str, "now", sizeof (val_str));
7336 if (segment->position == 0) {
7337 g_strlcpy (val_str, "0", sizeof (val_str));
7339 g_ascii_dtostr (val_str, sizeof (val_str),
7340 ((gdouble) segment->position) / GST_SECOND);
7343 return g_strdup_printf ("npt=%s-", val_str);
7347 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
7351 stream->timebase = -1;
7352 stream->seqbase = -1;
7354 len = stream->ptmap->len;
7355 for (i = 0; i < len; i++) {
7356 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
7359 if (item->caps == NULL)
7362 item->caps = gst_caps_make_writable (item->caps);
7363 s = gst_caps_get_structure (item->caps, 0);
7364 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
7368 static GstRTSPResult
7369 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
7371 GstRTSPResult res = GST_RTSP_OK;
7373 if (src->state < GST_RTSP_STATE_READY) {
7374 res = GST_RTSP_ERROR;
7375 if (src->open_error) {
7376 GST_DEBUG_OBJECT (src, "the stream was in error");
7380 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
7382 if ((res = gst_rtspsrc_open (src, async)) < 0) {
7383 GST_DEBUG_OBJECT (src, "failed to open stream");
7392 static GstRTSPResult
7393 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
7395 GstRTSPMessage request = { 0 };
7396 GstRTSPMessage response = { 0 };
7397 GstRTSPResult res = GST_RTSP_OK;
7401 const gchar *control;
7403 GST_DEBUG_OBJECT (src, "PLAY...");
7405 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7408 if (!(src->methods & GST_RTSP_PLAY))
7411 if (src->state == GST_RTSP_STATE_PLAYING)
7414 if (!src->conninfo.connection || !src->conninfo.connected)
7417 /* send some dummy packets before we activate the receive in the
7419 gst_rtspsrc_send_dummy_packets (src);
7421 /* require new SR packets */
7423 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
7425 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
7427 /* construct a control url */
7428 control = get_aggregate_control (src);
7430 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7431 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7432 const gchar *setup_url;
7433 GstRTSPConnection *conn;
7435 /* try aggregate control first but do non-aggregate control otherwise */
7437 setup_url = control;
7438 else if ((setup_url = stream->conninfo.location) == NULL)
7441 if (src->conninfo.connection) {
7442 conn = src->conninfo.connection;
7443 } else if (stream->conninfo.connection) {
7444 conn = stream->conninfo.connection;
7450 res = gst_rtsp_message_init_request (&request, GST_RTSP_PLAY, setup_url);
7452 goto create_request_failed;
7454 if (src->need_range) {
7455 hval = gen_range_header (src, segment);
7457 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
7459 /* store the newsegment event so it can be sent from the streaming thread. */
7460 if (src->start_segment)
7461 gst_event_unref (src->start_segment);
7462 src->start_segment = gst_event_new_segment (segment);
7465 if (segment->rate != 1.0) {
7466 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
7468 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
7470 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
7472 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
7476 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
7478 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7481 /* seek may have silently failed as it is not supported */
7482 if (!(src->methods & GST_RTSP_PLAY)) {
7483 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
7484 /* obviously it is supported as we made it here */
7485 src->methods |= GST_RTSP_PLAY;
7486 src->seekable = FALSE;
7487 /* but there is nothing to parse in the response,
7488 * so convey we have no idea and not to expect anything particular */
7489 clear_rtp_base (src, stream);
7493 /* need to do for all streams */
7494 for (run = src->streams; run; run = g_list_next (run))
7495 clear_rtp_base (src, (GstRTSPStream *) run->data);
7497 /* NOTE the above also disables npt based eos detection */
7498 /* and below forces position to 0,
7499 * which is visible feedback we lost the plot */
7500 segment->start = segment->position = src->last_pos;
7503 gst_rtsp_message_unset (&request);
7505 /* parse RTP npt field. This is the current position in the stream (Normal
7506 * Play Time) and should be put in the NEWSEGMENT position field. */
7507 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
7509 gst_rtspsrc_parse_range (src, hval, segment);
7511 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
7512 segment->rate = 1.0;
7514 /* parse Speed header. This is the intended playback rate of the stream
7515 * and should be put in the NEWSEGMENT rate field. */
7516 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
7517 0) == GST_RTSP_OK) {
7518 segment->rate = gst_rtspsrc_get_float (hval);
7519 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
7520 &hval, 0) == GST_RTSP_OK) {
7521 segment->rate = gst_rtspsrc_get_float (hval);
7524 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
7525 * for the RTP packets. If this is not present, we assume all starts from 0...
7526 * This is info for the RTP session manager that we pass to it in caps. */
7528 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
7529 &hval, hval_idx++) == GST_RTSP_OK)
7530 gst_rtspsrc_parse_rtpinfo (src, hval);
7532 /* some servers indicate RTCP parameters in PLAY response,
7533 * rather than properly in SDP */
7534 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
7535 &hval, 0) == GST_RTSP_OK)
7536 gst_rtspsrc_handle_rtcp_interval (src, hval);
7538 gst_rtsp_message_unset (&response);
7540 /* early exit when we did aggregate control */
7544 /* configure the caps of the streams after we parsed all headers. Only reset
7545 * the manager object when we set a new Range header (we did a seek) */
7546 gst_rtspsrc_configure_caps (src, segment, src->need_range);
7548 /* set again when needed */
7549 src->need_range = FALSE;
7551 src->running = TRUE;
7552 src->base_time = -1;
7553 src->state = GST_RTSP_STATE_PLAYING;
7556 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
7557 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7558 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7559 stream->discont = TRUE;
7564 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
7571 GST_DEBUG_OBJECT (src, "failed to open stream");
7576 GST_DEBUG_OBJECT (src, "PLAY is not supported");
7581 GST_DEBUG_OBJECT (src, "we were already PLAYING");
7584 create_request_failed:
7586 gchar *str = gst_rtsp_strresult (res);
7588 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7589 ("Could not create request. (%s)", str));
7595 gchar *str = gst_rtsp_strresult (res);
7597 gst_rtsp_message_unset (&request);
7598 if (res != GST_RTSP_EINTR) {
7599 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7600 ("Could not send message. (%s)", str));
7602 GST_WARNING_OBJECT (src, "PLAY interrupted");
7609 static GstRTSPResult
7610 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
7612 GstRTSPResult res = GST_RTSP_OK;
7613 GstRTSPMessage request = { 0 };
7614 GstRTSPMessage response = { 0 };
7616 const gchar *control;
7618 GST_DEBUG_OBJECT (src, "PAUSE...");
7620 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7623 if (!(src->methods & GST_RTSP_PAUSE))
7626 if (src->state == GST_RTSP_STATE_READY)
7629 if (!src->conninfo.connection || !src->conninfo.connected)
7632 /* construct a control url */
7633 control = get_aggregate_control (src);
7635 /* loop over the streams. We might exit the loop early when we could do an
7636 * aggregate control */
7637 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7638 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7639 GstRTSPConnection *conn;
7640 const gchar *setup_url;
7642 /* try aggregate control first but do non-aggregate control otherwise */
7644 setup_url = control;
7645 else if ((setup_url = stream->conninfo.location) == NULL)
7648 if (src->conninfo.connection) {
7649 conn = src->conninfo.connection;
7650 } else if (stream->conninfo.connection) {
7651 conn = stream->conninfo.connection;
7657 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
7658 ("Sending PAUSE request"));
7661 gst_rtsp_message_init_request (&request, GST_RTSP_PAUSE,
7663 goto create_request_failed;
7665 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7668 gst_rtsp_message_unset (&request);
7669 gst_rtsp_message_unset (&response);
7671 /* exit early when we did agregate control */
7676 /* change element states now */
7677 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
7680 src->state = GST_RTSP_STATE_READY;
7684 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
7691 GST_DEBUG_OBJECT (src, "failed to open stream");
7696 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
7701 GST_DEBUG_OBJECT (src, "we were already PAUSED");
7704 create_request_failed:
7706 gchar *str = gst_rtsp_strresult (res);
7708 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7709 ("Could not create request. (%s)", str));
7715 gchar *str = gst_rtsp_strresult (res);
7717 gst_rtsp_message_unset (&request);
7718 if (res != GST_RTSP_EINTR) {
7719 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7720 ("Could not send message. (%s)", str));
7722 GST_WARNING_OBJECT (src, "PAUSE interrupted");
7730 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
7732 GstRTSPSrc *rtspsrc;
7734 rtspsrc = GST_RTSPSRC (bin);
7736 switch (GST_MESSAGE_TYPE (message)) {
7737 case GST_MESSAGE_EOS:
7738 gst_message_unref (message);
7740 case GST_MESSAGE_ELEMENT:
7742 const GstStructure *s = gst_message_get_structure (message);
7744 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
7745 gboolean ignore_timeout;
7747 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
7749 GST_OBJECT_LOCK (rtspsrc);
7750 ignore_timeout = rtspsrc->ignore_timeout;
7751 rtspsrc->ignore_timeout = TRUE;
7752 GST_OBJECT_UNLOCK (rtspsrc);
7754 /* we only act on the first udp timeout message, others are irrelevant
7755 * and can be ignored. */
7756 if (!ignore_timeout)
7757 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
7759 gst_message_unref (message);
7762 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7765 case GST_MESSAGE_ERROR:
7768 GstRTSPStream *stream;
7771 udpsrc = GST_MESSAGE_SRC (message);
7773 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
7774 GST_ELEMENT_NAME (udpsrc));
7776 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
7780 /* we ignore the RTCP udpsrc */
7781 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
7784 /* if we get error messages from the udp sources, that's not a problem as
7785 * long as not all of them error out. We also don't really know what the
7786 * problem is, the message does not give enough detail... */
7787 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
7788 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
7789 if (ret != GST_FLOW_OK)
7793 gst_message_unref (message);
7797 /* fatal but not our message, forward */
7798 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7803 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7809 /* the thread where everything happens */
7811 gst_rtspsrc_thread (GstRTSPSrc * src)
7815 GST_OBJECT_LOCK (src);
7816 cmd = src->pending_cmd;
7817 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
7818 || cmd == CMD_LOOP || cmd == CMD_OPEN)
7819 src->pending_cmd = CMD_LOOP;
7821 src->pending_cmd = CMD_WAIT;
7822 GST_DEBUG_OBJECT (src, "got command %d", cmd);
7824 /* we got the message command, so ensure communication is possible again */
7825 gst_rtspsrc_connection_flush (src, FALSE);
7827 src->busy_cmd = cmd;
7828 GST_OBJECT_UNLOCK (src);
7832 gst_rtspsrc_open (src, TRUE);
7835 gst_rtspsrc_play (src, &src->segment, TRUE);
7838 gst_rtspsrc_pause (src, TRUE);
7841 gst_rtspsrc_close (src, TRUE, FALSE);
7844 gst_rtspsrc_loop (src);
7847 gst_rtspsrc_reconnect (src, FALSE);
7853 GST_OBJECT_LOCK (src);
7854 /* and go back to sleep */
7855 if (src->pending_cmd == CMD_WAIT) {
7857 gst_task_pause (src->task);
7860 src->busy_cmd = CMD_WAIT;
7861 GST_OBJECT_UNLOCK (src);
7865 gst_rtspsrc_start (GstRTSPSrc * src)
7867 GST_DEBUG_OBJECT (src, "starting");
7869 GST_OBJECT_LOCK (src);
7871 src->pending_cmd = CMD_WAIT;
7873 if (src->task == NULL) {
7874 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
7875 if (src->task == NULL)
7878 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
7880 GST_OBJECT_UNLOCK (src);
7887 GST_OBJECT_UNLOCK (src);
7888 GST_ERROR_OBJECT (src, "failed to create task");
7894 gst_rtspsrc_stop (GstRTSPSrc * src)
7898 GST_DEBUG_OBJECT (src, "stopping");
7900 /* also cancels pending task */
7901 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
7903 GST_OBJECT_LOCK (src);
7904 if ((task = src->task)) {
7906 GST_OBJECT_UNLOCK (src);
7908 gst_task_stop (task);
7910 /* make sure it is not running */
7911 GST_RTSP_STREAM_LOCK (src);
7912 GST_RTSP_STREAM_UNLOCK (src);
7914 /* now wait for the task to finish */
7915 gst_task_join (task);
7917 /* and free the task */
7918 gst_object_unref (GST_OBJECT (task));
7920 GST_OBJECT_LOCK (src);
7922 GST_OBJECT_UNLOCK (src);
7924 /* ensure synchronously all is closed and clean */
7925 gst_rtspsrc_close (src, FALSE, TRUE);
7930 static GstStateChangeReturn
7931 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
7933 GstRTSPSrc *rtspsrc;
7934 GstStateChangeReturn ret;
7936 rtspsrc = GST_RTSPSRC (element);
7938 switch (transition) {
7939 case GST_STATE_CHANGE_NULL_TO_READY:
7940 if (!gst_rtspsrc_start (rtspsrc))
7943 case GST_STATE_CHANGE_READY_TO_PAUSED:
7944 /* init some state */
7945 rtspsrc->cur_protocols = rtspsrc->protocols;
7946 /* first attempt, don't ignore timeouts */
7947 rtspsrc->ignore_timeout = FALSE;
7948 rtspsrc->open_error = FALSE;
7949 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
7951 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7952 set_manager_buffer_mode (rtspsrc);
7954 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7955 /* unblock the tcp tasks and make the loop waiting */
7956 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
7957 /* make sure it is waiting before we send PAUSE or PLAY below */
7958 GST_RTSP_STREAM_LOCK (rtspsrc);
7959 GST_RTSP_STREAM_UNLOCK (rtspsrc);
7962 case GST_STATE_CHANGE_PAUSED_TO_READY:
7968 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
7969 if (ret == GST_STATE_CHANGE_FAILURE)
7972 switch (transition) {
7973 case GST_STATE_CHANGE_NULL_TO_READY:
7974 ret = GST_STATE_CHANGE_SUCCESS;
7976 case GST_STATE_CHANGE_READY_TO_PAUSED:
7977 ret = GST_STATE_CHANGE_NO_PREROLL;
7979 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7980 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
7981 ret = GST_STATE_CHANGE_SUCCESS;
7983 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7984 /* send pause request and keep the idle task around */
7985 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
7986 ret = GST_STATE_CHANGE_NO_PREROLL;
7988 case GST_STATE_CHANGE_PAUSED_TO_READY:
7989 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_PAUSE);
7990 ret = GST_STATE_CHANGE_SUCCESS;
7992 case GST_STATE_CHANGE_READY_TO_NULL:
7993 gst_rtspsrc_stop (rtspsrc);
7994 ret = GST_STATE_CHANGE_SUCCESS;
8005 GST_DEBUG_OBJECT (rtspsrc, "start failed");
8006 return GST_STATE_CHANGE_FAILURE;
8011 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
8014 GstRTSPSrc *rtspsrc;
8016 rtspsrc = GST_RTSPSRC (element);
8018 if (GST_EVENT_IS_DOWNSTREAM (event)) {
8019 res = gst_rtspsrc_push_event (rtspsrc, event);
8021 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
8028 /*** GSTURIHANDLER INTERFACE *************************************************/
8031 gst_rtspsrc_uri_get_type (GType type)
8036 static const gchar *const *
8037 gst_rtspsrc_uri_get_protocols (GType type)
8039 static const gchar *protocols[] =
8040 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
8041 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
8048 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
8050 GstRTSPSrc *src = GST_RTSPSRC (handler);
8052 /* FIXME: make thread-safe */
8053 return g_strdup (src->conninfo.location);
8057 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
8063 GstRTSPUrl *newurl = NULL;
8064 GstSDPMessage *sdp = NULL;
8066 src = GST_RTSPSRC (handler);
8068 /* same URI, we're fine */
8069 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
8072 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
8073 sres = gst_sdp_message_new (&sdp);
8077 GST_DEBUG_OBJECT (src, "parsing SDP message");
8078 sres = gst_sdp_message_parse_uri (uri, sdp);
8083 GST_DEBUG_OBJECT (src, "parsing URI");
8084 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
8088 /* if worked, free previous and store new url object along with the original
8090 GST_DEBUG_OBJECT (src, "configuring URI");
8091 g_free (src->conninfo.location);
8092 src->conninfo.location = g_strdup (uri);
8093 gst_rtsp_url_free (src->conninfo.url);
8094 src->conninfo.url = newurl;
8095 g_free (src->conninfo.url_str);
8097 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
8099 src->conninfo.url_str = NULL;
8102 gst_sdp_message_free (src->sdp);
8104 src->from_sdp = sdp != NULL;
8106 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
8107 GST_DEBUG_OBJECT (src, "request uri is: %s",
8108 GST_STR_NULL (src->conninfo.url_str));
8115 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
8120 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", sres);
8121 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8122 "Could not create SDP");
8127 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", sres,
8128 GST_STR_NULL (uri));
8129 gst_sdp_message_free (sdp);
8130 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8136 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
8137 GST_STR_NULL (uri), res);
8138 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8139 "Invalid RTSP URI");
8145 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
8147 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
8149 iface->get_type = gst_rtspsrc_uri_get_type;
8150 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
8151 iface->get_uri = gst_rtspsrc_uri_get_uri;
8152 iface->set_uri = gst_rtspsrc_uri_set_uri;