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/rtp.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 enum _GstRtspSrcNtpTimeSource
170 NTP_TIME_SOURCE_UNIX,
171 NTP_TIME_SOURCE_RUNNING_TIME,
172 NTP_TIME_SOURCE_CLOCK_TIME
175 #define DEBUG_RTSP(__self,msg) gst_rtspsrc_print_rtsp_message (__self, msg)
176 #define DEBUG_SDP(__self,msg) gst_rtspsrc_print_sdp_message (__self, msg)
178 #define GST_TYPE_RTSP_SRC_NTP_TIME_SOURCE (gst_rtsp_src_ntp_time_source_get_type())
180 gst_rtsp_src_ntp_time_source_get_type (void)
182 static GType ntp_time_source_type = 0;
183 static const GEnumValue ntp_time_source_values[] = {
184 {NTP_TIME_SOURCE_NTP, "NTP time based on realtime clock", "ntp"},
185 {NTP_TIME_SOURCE_UNIX, "UNIX time based on realtime clock", "unix"},
186 {NTP_TIME_SOURCE_RUNNING_TIME,
187 "Running time based on pipeline clock",
189 {NTP_TIME_SOURCE_CLOCK_TIME, "Pipeline clock time", "clock-time"},
193 if (!ntp_time_source_type) {
194 ntp_time_source_type =
195 g_enum_register_static ("GstRTSPSrcNtpTimeSource",
196 ntp_time_source_values);
198 return ntp_time_source_type;
201 #define DEFAULT_LOCATION NULL
202 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
203 #define DEFAULT_DEBUG FALSE
204 #define DEFAULT_RETRY 20
205 #define DEFAULT_TIMEOUT 5000000
206 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
207 #define DEFAULT_TCP_TIMEOUT 20000000
208 #define DEFAULT_LATENCY_MS 2000
209 #define DEFAULT_DROP_ON_LATENCY FALSE
210 #define DEFAULT_CONNECTION_SPEED 0
211 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
212 #define DEFAULT_DO_RTCP TRUE
213 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
214 #define DEFAULT_PROXY NULL
215 #define DEFAULT_RTP_BLOCKSIZE 0
216 #define DEFAULT_USER_ID NULL
217 #define DEFAULT_USER_PW NULL
218 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
219 #define DEFAULT_PORT_RANGE NULL
220 #define DEFAULT_SHORT_HEADER FALSE
221 #define DEFAULT_PROBATION 2
222 #define DEFAULT_UDP_RECONNECT TRUE
223 #define DEFAULT_MULTICAST_IFACE NULL
224 #define DEFAULT_NTP_SYNC FALSE
225 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
226 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
227 #define DEFAULT_TLS_DATABASE NULL
228 #define DEFAULT_TLS_INTERACTION NULL
229 #define DEFAULT_DO_RETRANSMISSION TRUE
230 #define DEFAULT_NTP_TIME_SOURCE NTP_TIME_SOURCE_NTP
231 #define DEFAULT_USER_AGENT "GStreamer/" PACKAGE_VERSION
232 #define DEFAULT_MAX_RTCP_RTP_TIME_DIFF 1000
233 #define DEFAULT_RFC7273_SYNC FALSE
234 #define DEFAULT_MAX_TS_OFFSET_ADJUSTMENT G_GUINT64_CONSTANT(0)
235 #define DEFAULT_MAX_TS_OFFSET G_GINT64_CONSTANT(3000000000)
236 #define DEFAULT_VERSION GST_RTSP_VERSION_1_0
248 PROP_DROP_ON_LATENCY,
249 PROP_CONNECTION_SPEED,
252 PROP_DO_RTSP_KEEP_ALIVE,
261 PROP_UDP_BUFFER_SIZE,
265 PROP_MULTICAST_IFACE,
267 PROP_USE_PIPELINE_CLOCK,
269 PROP_TLS_VALIDATION_FLAGS,
271 PROP_TLS_INTERACTION,
272 PROP_DO_RETRANSMISSION,
273 PROP_NTP_TIME_SOURCE,
275 PROP_MAX_RTCP_RTP_TIME_DIFF,
277 PROP_MAX_TS_OFFSET_ADJUSTMENT,
279 PROP_DEFAULT_VERSION,
282 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
284 gst_rtsp_nat_method_get_type (void)
286 static GType rtsp_nat_method_type = 0;
287 static const GEnumValue rtsp_nat_method[] = {
288 {GST_RTSP_NAT_NONE, "None", "none"},
289 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
293 if (!rtsp_nat_method_type) {
294 rtsp_nat_method_type =
295 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
297 return rtsp_nat_method_type;
300 #define RTSP_SRC_RESPONSE_ERROR(src, response_msg, err_cat, err_code, error_message) \
302 GST_ELEMENT_ERROR_WITH_DETAILS((src), err_cat, err_code, ("%s", error_message), \
303 ("%s (%d)", (response_msg)->type_data.response.reason, (response_msg)->type_data.response.code), \
304 ("rtsp-status-code", G_TYPE_UINT, (response_msg)->type_data.response.code, \
305 "rtsp-status-reason", G_TYPE_STRING, GST_STR_NULL((response_msg)->type_data.response.reason), NULL)); \
308 static void gst_rtspsrc_finalize (GObject * object);
310 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
311 const GValue * value, GParamSpec * pspec);
312 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
313 GValue * value, GParamSpec * pspec);
315 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
317 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
318 gpointer iface_data);
320 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
321 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
323 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
324 GstStateChange transition);
325 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
326 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
328 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
329 GstRTSPMessage * response);
331 static gboolean gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
333 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
334 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
336 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
337 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
338 gboolean async, const gchar * seek_style);
339 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
340 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
341 gboolean only_close);
343 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
344 const gchar * uri, GError ** error);
345 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
347 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
348 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
349 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
350 GstRTSPStream * stream, GstEvent * event);
351 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
352 static void gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush);
353 static GstRTSPResult gst_rtsp_conninfo_close (GstRTSPSrc * src,
354 GstRTSPConnInfo * info, gboolean free);
356 gst_rtspsrc_print_rtsp_message (GstRTSPSrc * src, const GstRTSPMessage * msg);
358 gst_rtspsrc_print_sdp_message (GstRTSPSrc * src, const GstSDPMessage * msg);
366 /* commands we send to out loop to notify it of events */
367 #define CMD_OPEN (1 << 0)
368 #define CMD_PLAY (1 << 1)
369 #define CMD_PAUSE (1 << 2)
370 #define CMD_CLOSE (1 << 3)
371 #define CMD_WAIT (1 << 4)
372 #define CMD_RECONNECT (1 << 5)
373 #define CMD_LOOP (1 << 6)
375 /* mask for all commands */
376 #define CMD_ALL ((CMD_LOOP << 1) - 1)
378 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
380 gchar *__txt = _gst_element_error_printf text; \
381 gst_element_post_message (GST_ELEMENT_CAST (el), \
382 gst_message_new_progress (GST_OBJECT_CAST (el), \
383 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
387 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
389 #define gst_rtspsrc_parent_class parent_class
390 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
391 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
393 #ifndef GST_DISABLE_GST_DEBUG
394 static inline const char *
395 cmd_to_string (guint cmd)
419 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
421 GST_DEBUG_OBJECT (src, "default handler");
426 select_stream_accum (GSignalInvocationHint * ihint,
427 GValue * return_accu, const GValue * handler_return, gpointer data)
431 myboolean = g_value_get_boolean (handler_return);
432 GST_DEBUG ("accum %d", myboolean);
433 g_value_set_boolean (return_accu, myboolean);
435 /* stop emission if FALSE */
440 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
442 GObjectClass *gobject_class;
443 GstElementClass *gstelement_class;
444 GstBinClass *gstbin_class;
446 gobject_class = (GObjectClass *) klass;
447 gstelement_class = (GstElementClass *) klass;
448 gstbin_class = (GstBinClass *) klass;
450 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
452 gobject_class->set_property = gst_rtspsrc_set_property;
453 gobject_class->get_property = gst_rtspsrc_get_property;
455 gobject_class->finalize = gst_rtspsrc_finalize;
457 g_object_class_install_property (gobject_class, PROP_LOCATION,
458 g_param_spec_string ("location", "RTSP Location",
459 "Location of the RTSP url to read",
460 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
462 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
463 g_param_spec_flags ("protocols", "Protocols",
464 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
465 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
467 g_object_class_install_property (gobject_class, PROP_DEBUG,
468 g_param_spec_boolean ("debug", "Debug",
469 "Dump request and response messages to stdout"
470 "(DEPRECATED: Printed all RTSP message to gstreamer log as 'log' level)",
472 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
474 g_object_class_install_property (gobject_class, PROP_RETRY,
475 g_param_spec_uint ("retry", "Retry",
476 "Max number of retries when allocating RTP ports.",
477 0, G_MAXUINT16, DEFAULT_RETRY,
478 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
480 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
481 g_param_spec_uint64 ("timeout", "Timeout",
482 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
483 0, G_MAXUINT64, DEFAULT_TIMEOUT,
484 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
486 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
487 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
488 "Fail after timeout microseconds on TCP connections (0 = disabled)",
489 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
490 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
492 g_object_class_install_property (gobject_class, PROP_LATENCY,
493 g_param_spec_uint ("latency", "Buffer latency in ms",
494 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
495 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
497 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
498 g_param_spec_boolean ("drop-on-latency",
499 "Drop buffers when maximum latency is reached",
500 "Tells the jitterbuffer to never exceed the given latency in size",
501 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
503 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
504 g_param_spec_uint64 ("connection-speed", "Connection Speed",
505 "Network connection speed in kbps (0 = unknown)",
506 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
507 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
509 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
510 g_param_spec_enum ("nat-method", "NAT Method",
511 "Method to use for traversing firewalls and NAT",
512 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
513 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
516 * GstRTSPSrc:do-rtcp:
518 * Enable RTCP support. Some old server don't like RTCP and then this property
519 * needs to be set to FALSE.
521 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
522 g_param_spec_boolean ("do-rtcp", "Do RTCP",
523 "Send RTCP packets, disable for old incompatible server.",
524 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
527 * GstRTSPSrc:do-rtsp-keep-alive:
529 * Enable RTSP keep alive support. Some old server don't like RTSP
530 * keep alive and then this property needs to be set to FALSE.
532 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
533 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
534 "Send RTSP keep alive packets, disable for old incompatible server.",
535 DEFAULT_DO_RTSP_KEEP_ALIVE,
536 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
541 * Set the proxy parameters. This has to be a string of the format
542 * [http://][user:passwd@]host[:port].
544 g_object_class_install_property (gobject_class, PROP_PROXY,
545 g_param_spec_string ("proxy", "Proxy",
546 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
547 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
549 * GstRTSPSrc:proxy-id:
551 * Sets the proxy URI user id for authentication. If the URI set via the
552 * "proxy" property contains a user-id already, that will take precedence.
556 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
557 g_param_spec_string ("proxy-id", "proxy-id",
558 "HTTP proxy URI user id for authentication", "",
559 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
561 * GstRTSPSrc:proxy-pw:
563 * Sets the proxy URI password for authentication. If the URI set via the
564 * "proxy" property contains a password already, that will take precedence.
568 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
569 g_param_spec_string ("proxy-pw", "proxy-pw",
570 "HTTP proxy URI user password for authentication", "",
571 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
574 * GstRTSPSrc:rtp-blocksize:
576 * RTP package size to suggest to server.
578 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
579 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
580 "RTP package size to suggest to server (0 = disabled)",
581 0, 65536, DEFAULT_RTP_BLOCKSIZE,
582 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
584 g_object_class_install_property (gobject_class,
586 g_param_spec_string ("user-id", "user-id",
587 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
588 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
589 g_object_class_install_property (gobject_class, PROP_USER_PW,
590 g_param_spec_string ("user-pw", "user-pw",
591 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
592 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
595 * GstRTSPSrc:buffer-mode:
597 * Control the buffering and timestamping mode used by the jitterbuffer.
599 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
600 g_param_spec_enum ("buffer-mode", "Buffer Mode",
601 "Control the buffering algorithm in use",
602 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
603 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
606 * GstRTSPSrc:port-range:
608 * Configure the client port numbers that can be used to recieve RTP and
611 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
612 g_param_spec_string ("port-range", "Port range",
613 "Client port range that can be used to receive RTP and RTCP data, "
614 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
615 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
618 * GstRTSPSrc:udp-buffer-size:
620 * Size of the kernel UDP receive buffer in bytes.
622 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
623 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
624 "Size of the kernel UDP receive buffer in bytes, 0=default",
625 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
626 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
629 * GstRTSPSrc:short-header:
631 * Only send the basic RTSP headers for broken encoders.
633 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
634 g_param_spec_boolean ("short-header", "Short Header",
635 "Only send the basic RTSP headers for broken encoders",
636 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
638 g_object_class_install_property (gobject_class, PROP_PROBATION,
639 g_param_spec_uint ("probation", "Number of probations",
640 "Consecutive packet sequence numbers to accept the source",
641 0, G_MAXUINT, DEFAULT_PROBATION,
642 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
644 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
645 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
646 "Reconnect to the server if RTSP connection is closed when doing UDP",
647 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
649 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
650 g_param_spec_string ("multicast-iface", "Multicast Interface",
651 "The network interface on which to join the multicast group",
652 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
654 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
655 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
656 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
657 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
659 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
660 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
661 "Use the pipeline running-time to set the NTP time in the RTCP SR messages"
662 "(DEPRECATED: Use ntp-time-source property)",
663 DEFAULT_USE_PIPELINE_CLOCK,
664 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
666 g_object_class_install_property (gobject_class, PROP_SDES,
667 g_param_spec_boxed ("sdes", "SDES",
668 "The SDES items of this session",
669 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
672 * GstRTSPSrc::tls-validation-flags:
674 * TLS certificate validation flags used to validate server
679 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
680 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
681 "TLS certificate validation flags used to validate the server certificate",
682 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
683 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
686 * GstRTSPSrc::tls-database:
688 * TLS database with anchor certificate authorities used to validate
689 * the server certificate.
693 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
694 g_param_spec_object ("tls-database", "TLS database",
695 "TLS database with anchor certificate authorities used to validate the server certificate",
696 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
699 * GstRTSPSrc::tls-interaction:
701 * A #GTlsInteraction object to be used when the connection or certificate
702 * database need to interact with the user. This will be used to prompt the
703 * user for passwords where necessary.
707 g_object_class_install_property (gobject_class, PROP_TLS_INTERACTION,
708 g_param_spec_object ("tls-interaction", "TLS interaction",
709 "A GTlsInteraction object to promt the user for password or certificate",
710 G_TYPE_TLS_INTERACTION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
713 * GstRTSPSrc::do-retransmission:
715 * Attempt to ask the server to retransmit lost packets according to RFC4588.
717 * Note: currently only works with SSRC-multiplexed retransmission streams
721 g_object_class_install_property (gobject_class, PROP_DO_RETRANSMISSION,
722 g_param_spec_boolean ("do-retransmission", "Retransmission",
723 "Ask the server to retransmit lost packets",
724 DEFAULT_DO_RETRANSMISSION,
725 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
728 * GstRTSPSrc::ntp-time-source:
730 * allows to select the time source that should be used
731 * for the NTP time in RTCP packets
735 g_object_class_install_property (gobject_class, PROP_NTP_TIME_SOURCE,
736 g_param_spec_enum ("ntp-time-source", "NTP Time Source",
737 "NTP time source for RTCP packets",
738 GST_TYPE_RTSP_SRC_NTP_TIME_SOURCE, DEFAULT_NTP_TIME_SOURCE,
739 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
742 * GstRTSPSrc::user-agent:
744 * The string to set in the User-Agent header.
748 g_object_class_install_property (gobject_class, PROP_USER_AGENT,
749 g_param_spec_string ("user-agent", "User Agent",
750 "The User-Agent string to send to the server",
751 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
753 g_object_class_install_property (gobject_class, PROP_MAX_RTCP_RTP_TIME_DIFF,
754 g_param_spec_int ("max-rtcp-rtp-time-diff", "Max RTCP RTP Time Diff",
755 "Maximum amount of time in ms that the RTP time in RTCP SRs "
756 "is allowed to be ahead (-1 disabled)", -1, G_MAXINT,
757 DEFAULT_MAX_RTCP_RTP_TIME_DIFF,
758 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
760 g_object_class_install_property (gobject_class, PROP_RFC7273_SYNC,
761 g_param_spec_boolean ("rfc7273-sync", "Sync on RFC7273 clock",
762 "Synchronize received streams to the RFC7273 clock "
763 "(requires clock and offset to be provided)", DEFAULT_RFC7273_SYNC,
764 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
767 * GstRTSPSrc:default-rtsp-version:
769 * The preferred RTSP version to use while negotiating the version with the server.
773 g_object_class_install_property (gobject_class, PROP_DEFAULT_VERSION,
774 g_param_spec_enum ("default-rtsp-version",
775 "The RTSP version to try first",
776 "The RTSP version that should be tried first when negotiating version.",
777 GST_TYPE_RTSP_VERSION, DEFAULT_VERSION,
778 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
781 * GstRTSPSrc:max-ts-offset-adjustment:
783 * Syncing time stamps to NTP time adds a time offset. This parameter
784 * specifies the maximum number of nanoseconds per frame that this time offset
785 * may be adjusted with. This is used to avoid sudden large changes to time
788 g_object_class_install_property (gobject_class, PROP_MAX_TS_OFFSET_ADJUSTMENT,
789 g_param_spec_uint64 ("max-ts-offset-adjustment",
790 "Max Timestamp Offset Adjustment",
791 "The maximum number of nanoseconds per frame that time stamp offsets "
792 "may be adjusted (0 = no limit).", 0, G_MAXUINT64,
793 DEFAULT_MAX_TS_OFFSET_ADJUSTMENT, G_PARAM_READWRITE |
794 G_PARAM_STATIC_STRINGS));
797 * GstRtpBin:max-ts-offset:
799 * Used to set an upper limit of how large a time offset may be. This
800 * is used to protect against unrealistic values as a result of either
801 * client,server or clock issues.
803 g_object_class_install_property (gobject_class, PROP_MAX_TS_OFFSET,
804 g_param_spec_int64 ("max-ts-offset", "Max TS Offset",
805 "The maximum absolute value of the time offset in (nanoseconds). "
806 "Note, if the ntp-sync parameter is set the default value is "
807 "changed to 0 (no limit)", 0, G_MAXINT64, DEFAULT_MAX_TS_OFFSET,
808 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
811 * GstRTSPSrc::handle-request:
812 * @rtspsrc: a #GstRTSPSrc
813 * @request: a #GstRTSPMessage
814 * @response: a #GstRTSPMessage
816 * Handle a server request in @request and prepare @response.
818 * This signal is called from the streaming thread, you should therefore not
819 * do any state changes on @rtspsrc because this might deadlock. If you want
820 * to modify the state as a result of this signal, post a
821 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
826 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
827 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
828 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
829 G_TYPE_POINTER, G_TYPE_POINTER);
832 * GstRTSPSrc::on-sdp:
833 * @rtspsrc: a #GstRTSPSrc
834 * @sdp: a #GstSDPMessage
836 * Emited when the client has retrieved the SDP and before it configures the
837 * streams in the SDP. @sdp can be inspected and modified.
839 * This signal is called from the streaming thread, you should therefore not
840 * do any state changes on @rtspsrc because this might deadlock. If you want
841 * to modify the state as a result of this signal, post a
842 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
847 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
848 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
849 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
850 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
853 * GstRTSPSrc::select-stream:
854 * @rtspsrc: a #GstRTSPSrc
855 * @num: the stream number
856 * @caps: the stream caps
858 * Emited before the client decides to configure the stream @num with
861 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
866 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
867 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
868 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
869 (GCallback) default_select_stream, select_stream_accum, NULL,
870 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, G_TYPE_UINT,
873 * GstRTSPSrc::new-manager:
874 * @rtspsrc: a #GstRTSPSrc
875 * @manager: a #GstElement
877 * Emited after a new manager (like rtpbin) was created and the default
878 * properties were configured.
882 gst_rtspsrc_signals[SIGNAL_NEW_MANAGER] =
883 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
884 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
885 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
888 * GstRTSPSrc::request-rtcp-key:
889 * @rtspsrc: a #GstRTSPSrc
890 * @num: the stream number
892 * Signal emited to get the crypto parameters relevant to the RTCP
893 * stream. User should provide the key and the RTCP encryption ciphers
894 * and authentication, and return them wrapped in a GstCaps.
898 gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY] =
899 g_signal_new ("request-rtcp-key", G_TYPE_FROM_CLASS (klass),
900 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, GST_TYPE_CAPS, 1, G_TYPE_UINT);
902 gstelement_class->send_event = gst_rtspsrc_send_event;
903 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
904 gstelement_class->change_state = gst_rtspsrc_change_state;
906 gst_element_class_add_static_pad_template (gstelement_class, &rtptemplate);
908 gst_element_class_set_static_metadata (gstelement_class,
909 "RTSP packet receiver", "Source/Network",
910 "Receive data over the network via RTSP (RFC 2326)",
911 "Wim Taymans <wim@fluendo.com>, "
912 "Thijs Vermeir <thijs.vermeir@barco.com>, "
913 "Lutz Mueller <lutz@topfrose.de>");
915 gstbin_class->handle_message = gst_rtspsrc_handle_message;
917 gst_rtsp_ext_list_init ();
921 gst_rtspsrc_init (GstRTSPSrc * src)
923 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
924 src->protocols = DEFAULT_PROTOCOLS;
925 src->debug = DEFAULT_DEBUG;
926 src->retry = DEFAULT_RETRY;
927 src->udp_timeout = DEFAULT_TIMEOUT;
928 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
929 src->latency = DEFAULT_LATENCY_MS;
930 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
931 src->connection_speed = DEFAULT_CONNECTION_SPEED;
932 src->nat_method = DEFAULT_NAT_METHOD;
933 src->do_rtcp = DEFAULT_DO_RTCP;
934 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
935 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
936 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
937 src->user_id = g_strdup (DEFAULT_USER_ID);
938 src->user_pw = g_strdup (DEFAULT_USER_PW);
939 src->buffer_mode = DEFAULT_BUFFER_MODE;
940 src->client_port_range.min = 0;
941 src->client_port_range.max = 0;
942 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
943 src->short_header = DEFAULT_SHORT_HEADER;
944 src->probation = DEFAULT_PROBATION;
945 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
946 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
947 src->ntp_sync = DEFAULT_NTP_SYNC;
948 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
950 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
951 src->tls_database = DEFAULT_TLS_DATABASE;
952 src->tls_interaction = DEFAULT_TLS_INTERACTION;
953 src->do_retransmission = DEFAULT_DO_RETRANSMISSION;
954 src->ntp_time_source = DEFAULT_NTP_TIME_SOURCE;
955 src->user_agent = g_strdup (DEFAULT_USER_AGENT);
956 src->max_rtcp_rtp_time_diff = DEFAULT_MAX_RTCP_RTP_TIME_DIFF;
957 src->rfc7273_sync = DEFAULT_RFC7273_SYNC;
958 src->max_ts_offset_adjustment = DEFAULT_MAX_TS_OFFSET_ADJUSTMENT;
959 src->max_ts_offset = DEFAULT_MAX_TS_OFFSET;
960 src->max_ts_offset_is_set = FALSE;
961 src->default_version = DEFAULT_VERSION;
962 src->version = GST_RTSP_VERSION_INVALID;
964 /* get a list of all extensions */
965 src->extensions = gst_rtsp_ext_list_get ();
967 /* connect to send signal */
968 gst_rtsp_ext_list_connect (src->extensions, "send",
969 (GCallback) gst_rtspsrc_send_cb, src);
971 /* protects the streaming thread in interleaved mode or the polling
972 * thread in UDP mode. */
973 g_rec_mutex_init (&src->stream_rec_lock);
975 /* protects our state changes from multiple invocations */
976 g_rec_mutex_init (&src->state_rec_lock);
978 src->state = GST_RTSP_STATE_INVALID;
980 g_mutex_init (&src->conninfo.send_lock);
981 g_mutex_init (&src->conninfo.recv_lock);
983 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
984 gst_bin_set_suppressed_flags (GST_BIN (src),
985 GST_ELEMENT_FLAG_SOURCE | GST_ELEMENT_FLAG_SINK);
989 gst_rtspsrc_finalize (GObject * object)
993 rtspsrc = GST_RTSPSRC (object);
995 gst_rtsp_ext_list_free (rtspsrc->extensions);
996 g_free (rtspsrc->conninfo.location);
997 gst_rtsp_url_free (rtspsrc->conninfo.url);
998 g_free (rtspsrc->conninfo.url_str);
999 g_free (rtspsrc->user_id);
1000 g_free (rtspsrc->user_pw);
1001 g_free (rtspsrc->multi_iface);
1002 g_free (rtspsrc->user_agent);
1005 gst_sdp_message_free (rtspsrc->sdp);
1006 rtspsrc->sdp = NULL;
1008 if (rtspsrc->provided_clock)
1009 gst_object_unref (rtspsrc->provided_clock);
1012 gst_structure_free (rtspsrc->sdes);
1014 if (rtspsrc->tls_database)
1015 g_object_unref (rtspsrc->tls_database);
1017 if (rtspsrc->tls_interaction)
1018 g_object_unref (rtspsrc->tls_interaction);
1021 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
1022 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
1024 g_mutex_clear (&rtspsrc->conninfo.send_lock);
1025 g_mutex_clear (&rtspsrc->conninfo.recv_lock);
1027 G_OBJECT_CLASS (parent_class)->finalize (object);
1031 gst_rtspsrc_provide_clock (GstElement * element)
1033 GstRTSPSrc *src = GST_RTSPSRC (element);
1036 if ((clock = src->provided_clock) != NULL)
1037 return gst_object_ref (clock);
1039 return GST_ELEMENT_CLASS (parent_class)->provide_clock (element);
1042 /* a proxy string of the format [user:passwd@]host[:port] */
1044 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
1046 gchar *p, *at, *col;
1048 g_free (rtsp->proxy_user);
1049 rtsp->proxy_user = NULL;
1050 g_free (rtsp->proxy_passwd);
1051 rtsp->proxy_passwd = NULL;
1052 g_free (rtsp->proxy_host);
1053 rtsp->proxy_host = NULL;
1054 rtsp->proxy_port = 0;
1056 p = (gchar *) proxy;
1061 /* we allow http:// in front but ignore it */
1062 if (g_str_has_prefix (p, "http://"))
1065 at = strchr (p, '@');
1067 /* look for user:passwd */
1068 col = strchr (proxy, ':');
1069 if (col == NULL || col > at)
1072 rtsp->proxy_user = g_strndup (p, col - p);
1074 rtsp->proxy_passwd = g_strndup (col, at - col);
1079 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
1080 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
1081 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
1082 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
1083 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
1084 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
1085 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
1088 col = strchr (p, ':');
1091 /* everything before the colon is the hostname */
1092 rtsp->proxy_host = g_strndup (p, col - p);
1094 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
1096 rtsp->proxy_host = g_strdup (p);
1097 rtsp->proxy_port = 8080;
1103 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
1105 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
1106 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
1109 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
1111 rtspsrc->ptcp_timeout = NULL;
1115 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
1118 GstRTSPSrc *rtspsrc;
1120 rtspsrc = GST_RTSPSRC (object);
1124 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
1125 g_value_get_string (value), NULL);
1127 case PROP_PROTOCOLS:
1128 rtspsrc->protocols = g_value_get_flags (value);
1131 rtspsrc->debug = g_value_get_boolean (value);
1134 rtspsrc->retry = g_value_get_uint (value);
1137 rtspsrc->udp_timeout = g_value_get_uint64 (value);
1139 case PROP_TCP_TIMEOUT:
1140 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
1143 rtspsrc->latency = g_value_get_uint (value);
1145 case PROP_DROP_ON_LATENCY:
1146 rtspsrc->drop_on_latency = g_value_get_boolean (value);
1148 case PROP_CONNECTION_SPEED:
1149 rtspsrc->connection_speed = g_value_get_uint64 (value);
1151 case PROP_NAT_METHOD:
1152 rtspsrc->nat_method = g_value_get_enum (value);
1155 rtspsrc->do_rtcp = g_value_get_boolean (value);
1157 case PROP_DO_RTSP_KEEP_ALIVE:
1158 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
1161 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
1164 g_free (rtspsrc->prop_proxy_id);
1165 rtspsrc->prop_proxy_id = g_value_dup_string (value);
1168 g_free (rtspsrc->prop_proxy_pw);
1169 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
1171 case PROP_RTP_BLOCKSIZE:
1172 rtspsrc->rtp_blocksize = g_value_get_uint (value);
1175 g_free (rtspsrc->user_id);
1176 rtspsrc->user_id = g_value_dup_string (value);
1179 g_free (rtspsrc->user_pw);
1180 rtspsrc->user_pw = g_value_dup_string (value);
1182 case PROP_BUFFER_MODE:
1183 rtspsrc->buffer_mode = g_value_get_enum (value);
1185 case PROP_PORT_RANGE:
1189 str = g_value_get_string (value);
1190 if (sscanf (str, "%u-%u", &rtspsrc->client_port_range.min,
1191 &rtspsrc->client_port_range.max) != 2) {
1192 rtspsrc->client_port_range.min = 0;
1193 rtspsrc->client_port_range.max = 0;
1197 case PROP_UDP_BUFFER_SIZE:
1198 rtspsrc->udp_buffer_size = g_value_get_int (value);
1200 case PROP_SHORT_HEADER:
1201 rtspsrc->short_header = g_value_get_boolean (value);
1203 case PROP_PROBATION:
1204 rtspsrc->probation = g_value_get_uint (value);
1206 case PROP_UDP_RECONNECT:
1207 rtspsrc->udp_reconnect = g_value_get_boolean (value);
1209 case PROP_MULTICAST_IFACE:
1210 g_free (rtspsrc->multi_iface);
1212 if (g_value_get_string (value) == NULL)
1213 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1215 rtspsrc->multi_iface = g_value_dup_string (value);
1218 rtspsrc->ntp_sync = g_value_get_boolean (value);
1219 /* The default value of max_ts_offset depends on ntp_sync. If user
1220 * hasn't set it then change default value */
1221 if (!rtspsrc->max_ts_offset_is_set) {
1222 if (rtspsrc->ntp_sync) {
1223 rtspsrc->max_ts_offset = 0;
1225 rtspsrc->max_ts_offset = DEFAULT_MAX_TS_OFFSET;
1229 case PROP_USE_PIPELINE_CLOCK:
1230 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
1233 rtspsrc->sdes = g_value_dup_boxed (value);
1235 case PROP_TLS_VALIDATION_FLAGS:
1236 rtspsrc->tls_validation_flags = g_value_get_flags (value);
1238 case PROP_TLS_DATABASE:
1239 g_clear_object (&rtspsrc->tls_database);
1240 rtspsrc->tls_database = g_value_dup_object (value);
1242 case PROP_TLS_INTERACTION:
1243 g_clear_object (&rtspsrc->tls_interaction);
1244 rtspsrc->tls_interaction = g_value_dup_object (value);
1246 case PROP_DO_RETRANSMISSION:
1247 rtspsrc->do_retransmission = g_value_get_boolean (value);
1249 case PROP_NTP_TIME_SOURCE:
1250 rtspsrc->ntp_time_source = g_value_get_enum (value);
1252 case PROP_USER_AGENT:
1253 g_free (rtspsrc->user_agent);
1254 rtspsrc->user_agent = g_value_dup_string (value);
1256 case PROP_MAX_RTCP_RTP_TIME_DIFF:
1257 rtspsrc->max_rtcp_rtp_time_diff = g_value_get_int (value);
1259 case PROP_RFC7273_SYNC:
1260 rtspsrc->rfc7273_sync = g_value_get_boolean (value);
1262 case PROP_MAX_TS_OFFSET_ADJUSTMENT:
1263 rtspsrc->max_ts_offset_adjustment = g_value_get_uint64 (value);
1265 case PROP_MAX_TS_OFFSET:
1266 rtspsrc->max_ts_offset = g_value_get_int64 (value);
1267 rtspsrc->max_ts_offset_is_set = TRUE;
1269 case PROP_DEFAULT_VERSION:
1270 rtspsrc->default_version = g_value_get_enum (value);
1273 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1279 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
1282 GstRTSPSrc *rtspsrc;
1284 rtspsrc = GST_RTSPSRC (object);
1288 g_value_set_string (value, rtspsrc->conninfo.location);
1290 case PROP_PROTOCOLS:
1291 g_value_set_flags (value, rtspsrc->protocols);
1294 g_value_set_boolean (value, rtspsrc->debug);
1297 g_value_set_uint (value, rtspsrc->retry);
1300 g_value_set_uint64 (value, rtspsrc->udp_timeout);
1302 case PROP_TCP_TIMEOUT:
1306 timeout = ((guint64) rtspsrc->tcp_timeout.tv_sec) * G_USEC_PER_SEC +
1307 rtspsrc->tcp_timeout.tv_usec;
1308 g_value_set_uint64 (value, timeout);
1312 g_value_set_uint (value, rtspsrc->latency);
1314 case PROP_DROP_ON_LATENCY:
1315 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1317 case PROP_CONNECTION_SPEED:
1318 g_value_set_uint64 (value, rtspsrc->connection_speed);
1320 case PROP_NAT_METHOD:
1321 g_value_set_enum (value, rtspsrc->nat_method);
1324 g_value_set_boolean (value, rtspsrc->do_rtcp);
1326 case PROP_DO_RTSP_KEEP_ALIVE:
1327 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1333 if (rtspsrc->proxy_host) {
1335 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1339 g_value_take_string (value, str);
1343 g_value_set_string (value, rtspsrc->prop_proxy_id);
1346 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1348 case PROP_RTP_BLOCKSIZE:
1349 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1352 g_value_set_string (value, rtspsrc->user_id);
1355 g_value_set_string (value, rtspsrc->user_pw);
1357 case PROP_BUFFER_MODE:
1358 g_value_set_enum (value, rtspsrc->buffer_mode);
1360 case PROP_PORT_RANGE:
1364 if (rtspsrc->client_port_range.min != 0) {
1365 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1366 rtspsrc->client_port_range.max);
1370 g_value_take_string (value, str);
1373 case PROP_UDP_BUFFER_SIZE:
1374 g_value_set_int (value, rtspsrc->udp_buffer_size);
1376 case PROP_SHORT_HEADER:
1377 g_value_set_boolean (value, rtspsrc->short_header);
1379 case PROP_PROBATION:
1380 g_value_set_uint (value, rtspsrc->probation);
1382 case PROP_UDP_RECONNECT:
1383 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1385 case PROP_MULTICAST_IFACE:
1386 g_value_set_string (value, rtspsrc->multi_iface);
1389 g_value_set_boolean (value, rtspsrc->ntp_sync);
1391 case PROP_USE_PIPELINE_CLOCK:
1392 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1395 g_value_set_boxed (value, rtspsrc->sdes);
1397 case PROP_TLS_VALIDATION_FLAGS:
1398 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1400 case PROP_TLS_DATABASE:
1401 g_value_set_object (value, rtspsrc->tls_database);
1403 case PROP_TLS_INTERACTION:
1404 g_value_set_object (value, rtspsrc->tls_interaction);
1406 case PROP_DO_RETRANSMISSION:
1407 g_value_set_boolean (value, rtspsrc->do_retransmission);
1409 case PROP_NTP_TIME_SOURCE:
1410 g_value_set_enum (value, rtspsrc->ntp_time_source);
1412 case PROP_USER_AGENT:
1413 g_value_set_string (value, rtspsrc->user_agent);
1415 case PROP_MAX_RTCP_RTP_TIME_DIFF:
1416 g_value_set_int (value, rtspsrc->max_rtcp_rtp_time_diff);
1418 case PROP_RFC7273_SYNC:
1419 g_value_set_boolean (value, rtspsrc->rfc7273_sync);
1421 case PROP_MAX_TS_OFFSET_ADJUSTMENT:
1422 g_value_set_uint64 (value, rtspsrc->max_ts_offset_adjustment);
1424 case PROP_MAX_TS_OFFSET:
1425 g_value_set_int64 (value, rtspsrc->max_ts_offset);
1427 case PROP_DEFAULT_VERSION:
1428 g_value_set_enum (value, rtspsrc->default_version);
1431 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1437 find_stream_by_id (GstRTSPStream * stream, gint * id)
1439 if (stream->id == *id)
1446 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1448 /* ignore unconfigured channels here (e.g., those that
1449 * were explicitly skipped during SETUP) */
1450 if ((stream->channelpad[0] != NULL) &&
1451 (stream->channel[0] == *channel || stream->channel[1] == *channel))
1458 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1460 GstElement *src = (GstElement *) a;
1462 if (stream->udpsrc[0] == src)
1464 if (stream->udpsrc[1] == src)
1471 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1473 if (stream->conninfo.location) {
1474 /* check qualified setup_url */
1475 if (!strcmp (stream->conninfo.location, (gchar *) a))
1478 if (stream->control_url) {
1479 /* check original control_url */
1480 if (!strcmp (stream->control_url, (gchar *) a))
1483 /* check if qualified setup_url ends with string */
1484 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1491 static GstRTSPStream *
1492 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1496 /* find and get stream */
1497 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1498 return (GstRTSPStream *) lstream->data;
1503 static const GstSDPBandwidth *
1504 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1505 const GstSDPMedia * media, const gchar * type)
1509 /* first look in the media specific section */
1510 len = gst_sdp_media_bandwidths_len (media);
1511 for (i = 0; i < len; i++) {
1512 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1514 if (strcmp (bw->bwtype, type) == 0)
1517 /* then look in the message specific section */
1518 len = gst_sdp_message_bandwidths_len (sdp);
1519 for (i = 0; i < len; i++) {
1520 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1522 if (strcmp (bw->bwtype, type) == 0)
1529 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1530 const GstSDPMedia * media, GstRTSPStream * stream)
1532 const GstSDPBandwidth *bw;
1534 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1535 stream->as_bandwidth = bw->bandwidth;
1537 stream->as_bandwidth = -1;
1539 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1540 stream->rr_bandwidth = bw->bandwidth;
1542 stream->rr_bandwidth = -1;
1544 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1545 stream->rs_bandwidth = bw->bandwidth;
1547 stream->rs_bandwidth = -1;
1551 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1552 const GstSDPConnection * conn)
1554 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1557 if (conn->addrtype == NULL)
1560 /* check for IPV6 */
1561 if (strcmp (conn->addrtype, "IP4") == 0)
1562 stream->is_ipv6 = FALSE;
1563 else if (strcmp (conn->addrtype, "IP6") == 0)
1564 stream->is_ipv6 = TRUE;
1569 g_free (stream->destination);
1570 stream->destination = g_strdup (conn->address);
1572 /* check for multicast */
1573 stream->is_multicast =
1574 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1576 stream->ttl = conn->ttl;
1579 /* Go over the connections for a stream.
1580 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1582 * - If we are dealing with a localhost address, we disable multicast
1585 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1586 const GstSDPMedia * media, GstRTSPStream * stream)
1588 const GstSDPConnection *conn;
1591 /* first look in the media specific section */
1592 len = gst_sdp_media_connections_len (media);
1593 for (i = 0; i < len; i++) {
1594 conn = gst_sdp_media_get_connection (media, i);
1596 gst_rtspsrc_do_stream_connection (src, stream, conn);
1598 /* then look in the message specific section */
1599 if ((conn = gst_sdp_message_get_connection (sdp))) {
1600 gst_rtspsrc_do_stream_connection (src, stream, conn);
1605 make_stream_id (GstRTSPStream * stream, const GstSDPMedia * media)
1608 g_strdup_printf ("%s:%d:%d:%s:%d", media->media, media->port,
1609 media->num_ports, media->proto, stream->default_pt);
1611 g_strcanon (stream_id, G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS, ':');
1616 /* m=<media> <UDP port> RTP/AVP <payload>
1619 gst_rtspsrc_collect_payloads (GstRTSPSrc * src, const GstSDPMessage * sdp,
1620 const GstSDPMedia * media, GstRTSPStream * stream)
1624 GstCaps *global_caps;
1627 proto = gst_sdp_media_get_proto (media);
1631 if (g_str_equal (proto, "RTP/AVP"))
1632 stream->profile = GST_RTSP_PROFILE_AVP;
1633 else if (g_str_equal (proto, "RTP/SAVP"))
1634 stream->profile = GST_RTSP_PROFILE_SAVP;
1635 else if (g_str_equal (proto, "RTP/AVPF"))
1636 stream->profile = GST_RTSP_PROFILE_AVPF;
1637 else if (g_str_equal (proto, "RTP/SAVPF"))
1638 stream->profile = GST_RTSP_PROFILE_SAVPF;
1642 if (gst_sdp_media_get_attribute_val (media, "sendonly") != NULL)
1643 goto sendonly_media;
1645 /* Parse global SDP attributes once */
1646 global_caps = gst_caps_new_empty_simple ("application/x-unknown");
1647 GST_DEBUG ("mapping sdp session level attributes to caps");
1648 gst_sdp_message_attributes_to_caps (sdp, global_caps);
1649 GST_DEBUG ("mapping sdp media level attributes to caps");
1650 gst_sdp_media_attributes_to_caps (media, global_caps);
1652 /* Keep a copy of the SDP key management */
1653 gst_sdp_media_parse_keymgmt (media, &stream->mikey);
1654 if (stream->mikey == NULL)
1655 gst_sdp_message_parse_keymgmt (sdp, &stream->mikey);
1657 len = gst_sdp_media_formats_len (media);
1658 for (i = 0; i < len; i++) {
1660 GstCaps *caps, *outcaps;
1665 pt = atoi (gst_sdp_media_get_format (media, i));
1667 GST_DEBUG_OBJECT (src, " looking at %d pt: %d", i, pt);
1670 caps = gst_sdp_media_get_caps_from_media (media, pt);
1672 GST_WARNING_OBJECT (src, " skipping pt %d without caps", pt);
1676 /* do some tweaks */
1677 s = gst_caps_get_structure (caps, 0);
1678 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
1679 stream->is_real = (strstr (enc, "-REAL") != NULL);
1680 if (strcmp (enc, "X-ASF-PF") == 0)
1681 stream->container = TRUE;
1684 /* Merge in global caps */
1685 /* Intersect will merge in missing fields to the current caps */
1686 outcaps = gst_caps_intersect (caps, global_caps);
1687 gst_caps_unref (caps);
1689 /* the first pt will be the default */
1690 if (stream->ptmap->len == 0)
1691 stream->default_pt = pt;
1694 item.caps = outcaps;
1696 g_array_append_val (stream->ptmap, item);
1699 stream->stream_id = make_stream_id (stream, media);
1701 gst_caps_unref (global_caps);
1706 GST_ERROR_OBJECT (src, "can't find proto in media");
1711 GST_ERROR_OBJECT (src, "unknown proto in media: '%s'", proto);
1716 GST_DEBUG_OBJECT (src, "sendonly media ignored");
1721 static const gchar *
1722 get_aggregate_control (GstRTSPSrc * src)
1727 base = src->control;
1728 else if (src->content_base)
1729 base = src->content_base;
1730 else if (src->conninfo.url_str)
1731 base = src->conninfo.url_str;
1739 clear_ptmap_item (PtMapItem * item)
1742 gst_caps_unref (item->caps);
1745 static GstRTSPStream *
1746 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx,
1749 GstRTSPStream *stream;
1750 const gchar *control_url;
1751 const GstSDPMedia *media;
1753 /* get media, should not return NULL */
1754 media = gst_sdp_message_get_media (sdp, idx);
1758 stream = g_new0 (GstRTSPStream, 1);
1759 stream->parent = src;
1760 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1762 stream->last_ret = GST_FLOW_NOT_LINKED;
1763 stream->added = FALSE;
1764 stream->setup = FALSE;
1765 stream->skipped = FALSE;
1767 stream->eos = FALSE;
1768 stream->discont = TRUE;
1769 stream->seqbase = -1;
1770 stream->timebase = -1;
1771 stream->send_ssrc = g_random_int ();
1772 stream->profile = GST_RTSP_PROFILE_AVP;
1773 stream->ptmap = g_array_new (FALSE, FALSE, sizeof (PtMapItem));
1774 stream->mikey = NULL;
1775 stream->stream_id = NULL;
1776 g_mutex_init (&stream->conninfo.send_lock);
1777 g_mutex_init (&stream->conninfo.recv_lock);
1778 g_array_set_clear_func (stream->ptmap, (GDestroyNotify) clear_ptmap_item);
1780 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1781 * session manager to scale RTCP. */
1782 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1784 /* collect connection info */
1785 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1787 /* make the payload type map */
1788 gst_rtspsrc_collect_payloads (src, sdp, media, stream);
1790 /* collect port number */
1791 stream->port = gst_sdp_media_get_port (media);
1793 /* get control url to construct the setup url. The setup url is used to
1794 * configure the transport of the stream and is used to identity the stream in
1795 * the RTP-Info header field returned from PLAY. */
1796 control_url = gst_sdp_media_get_attribute_val (media, "control");
1797 if (control_url == NULL)
1798 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1800 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1801 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1802 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1803 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1805 /* RFC 2326, C.3: missing control_url permitted in case of a single stream */
1806 if (control_url == NULL && n_streams == 1) {
1810 if (control_url != NULL) {
1811 stream->control_url = g_strdup (control_url);
1812 /* Build a fully qualified url using the content_base if any or by prefixing
1813 * the original request.
1814 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1815 * likely build a URL that the server will fail to understand, this is ok,
1816 * we will fail then. */
1817 if (g_str_has_prefix (control_url, "rtsp://"))
1818 stream->conninfo.location = g_strdup (control_url);
1823 if (g_strcmp0 (control_url, "*") == 0)
1826 base = get_aggregate_control (src);
1828 /* check if the base ends or control starts with / */
1829 has_slash = g_str_has_prefix (control_url, "/");
1830 has_slash = has_slash || g_str_has_suffix (base, "/");
1832 /* concatenate the two strings, insert / when not present */
1833 stream->conninfo.location =
1834 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1837 GST_DEBUG_OBJECT (src, " setup: %s",
1838 GST_STR_NULL (stream->conninfo.location));
1840 /* we keep track of all streams */
1841 src->streams = g_list_append (src->streams, stream);
1849 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1853 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1855 g_array_free (stream->ptmap, TRUE);
1857 g_free (stream->destination);
1858 g_free (stream->control_url);
1859 g_free (stream->conninfo.location);
1860 g_free (stream->stream_id);
1862 for (i = 0; i < 2; i++) {
1863 if (stream->udpsrc[i]) {
1864 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1865 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1866 gst_object_unref (stream->udpsrc[i]);
1868 if (stream->channelpad[i])
1869 gst_object_unref (stream->channelpad[i]);
1871 if (stream->udpsink[i]) {
1872 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1873 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1874 gst_object_unref (stream->udpsink[i]);
1877 if (stream->fakesrc) {
1878 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1879 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1880 gst_object_unref (stream->fakesrc);
1882 if (stream->srcpad) {
1883 gst_pad_set_active (stream->srcpad, FALSE);
1885 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1887 if (stream->srtpenc)
1888 gst_object_unref (stream->srtpenc);
1889 if (stream->srtpdec)
1890 gst_object_unref (stream->srtpdec);
1891 if (stream->srtcpparams)
1892 gst_caps_unref (stream->srtcpparams);
1894 gst_mikey_message_unref (stream->mikey);
1895 if (stream->rtcppad)
1896 gst_object_unref (stream->rtcppad);
1897 if (stream->session)
1898 g_object_unref (stream->session);
1899 if (stream->rtx_pt_map)
1900 gst_structure_free (stream->rtx_pt_map);
1902 g_mutex_clear (&stream->conninfo.send_lock);
1903 g_mutex_clear (&stream->conninfo.recv_lock);
1909 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1913 GST_DEBUG_OBJECT (src, "cleanup");
1915 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1916 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1918 gst_rtspsrc_stream_free (src, stream);
1920 g_list_free (src->streams);
1921 src->streams = NULL;
1923 if (src->manager_sig_id) {
1924 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1925 src->manager_sig_id = 0;
1927 gst_element_set_state (src->manager, GST_STATE_NULL);
1928 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1929 src->manager = NULL;
1932 gst_structure_free (src->props);
1935 g_free (src->content_base);
1936 src->content_base = NULL;
1938 g_free (src->control);
1939 src->control = NULL;
1942 gst_rtsp_range_free (src->range);
1945 /* don't clear the SDP when it was used in the url */
1946 if (src->sdp && !src->from_sdp) {
1947 gst_sdp_message_free (src->sdp);
1951 src->need_segment = FALSE;
1953 if (src->provided_clock) {
1954 gst_object_unref (src->provided_clock);
1955 src->provided_clock = NULL;
1960 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
1961 gint * rtpport, gint * rtcpport)
1964 GstStateChangeReturn ret;
1965 GstElement *udpsrc0, *udpsrc1;
1966 gint tmp_rtp, tmp_rtcp;
1970 src = stream->parent;
1976 /* Start at next port */
1977 tmp_rtp = src->next_port_num;
1979 if (stream->is_ipv6)
1980 host = "udp://[::0]";
1982 host = "udp://0.0.0.0";
1984 /* try to allocate 2 UDP ports, the RTP port should be an even
1985 * number and the RTCP port should be the next (uneven) port */
1988 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
1989 tmp_rtp >= src->client_port_range.max)
1992 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1993 if (udpsrc0 == NULL)
1994 goto no_udp_protocol;
1995 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
1997 if (src->udp_buffer_size != 0)
1998 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
2001 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
2002 if (ret == GST_STATE_CHANGE_FAILURE) {
2004 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
2007 if (++count > src->retry)
2010 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2011 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2012 gst_object_unref (udpsrc0);
2015 GST_DEBUG_OBJECT (src, "retry %d", count);
2018 goto no_udp_protocol;
2021 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
2022 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
2024 /* check if port is even */
2025 if ((tmp_rtp & 0x01) != 0) {
2026 /* port not even, close and allocate another */
2027 if (++count > src->retry)
2030 GST_DEBUG_OBJECT (src, "RTP port not even");
2032 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2033 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2034 gst_object_unref (udpsrc0);
2037 GST_DEBUG_OBJECT (src, "retry %d", count);
2042 /* allocate port+1 for RTCP now */
2043 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2044 if (udpsrc1 == NULL)
2045 goto no_udp_rtcp_protocol;
2048 tmp_rtcp = tmp_rtp + 1;
2049 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
2052 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
2054 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
2055 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
2056 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
2057 if (ret == GST_STATE_CHANGE_FAILURE) {
2058 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
2060 if (++count > src->retry)
2063 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2064 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2065 gst_object_unref (udpsrc0);
2068 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
2069 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2070 gst_object_unref (udpsrc1);
2074 GST_DEBUG_OBJECT (src, "retry %d", count);
2078 /* all fine, do port check */
2079 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
2080 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
2082 /* this should not happen... */
2083 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
2086 /* we keep these elements, we configure all in configure_transport when the
2087 * server told us to really use the UDP ports. */
2088 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
2089 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
2090 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
2091 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
2093 /* keep track of next available port number when we have a range
2095 if (src->next_port_num != 0)
2096 src->next_port_num = tmp_rtcp + 1;
2103 GST_DEBUG_OBJECT (src, "could not get UDP source");
2108 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
2112 no_udp_rtcp_protocol:
2114 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
2119 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
2120 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
2126 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2127 gst_object_unref (udpsrc0);
2130 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2131 gst_object_unref (udpsrc1);
2138 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
2143 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
2145 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2146 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2149 for (i = 0; i < 2; i++) {
2150 if (stream->udpsrc[i])
2151 gst_element_set_state (stream->udpsrc[i], state);
2157 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
2164 event = gst_event_new_flush_start ();
2165 GST_DEBUG_OBJECT (src, "start flush");
2167 state = GST_STATE_PAUSED;
2169 event = gst_event_new_flush_stop (FALSE);
2170 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2173 state = GST_STATE_PLAYING;
2175 state = GST_STATE_PAUSED;
2177 gst_rtspsrc_push_event (src, event);
2178 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2179 gst_rtspsrc_set_state (src, state);
2182 static GstRTSPResult
2183 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
2184 GstRTSPMessage * message, GTimeVal * timeout)
2188 if (conninfo->connection) {
2189 g_mutex_lock (&conninfo->send_lock);
2190 ret = gst_rtsp_connection_send (conninfo->connection, message, timeout);
2191 g_mutex_unlock (&conninfo->send_lock);
2193 ret = GST_RTSP_ERROR;
2199 static GstRTSPResult
2200 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
2201 GstRTSPMessage * message, GTimeVal * timeout)
2205 if (conninfo->connection) {
2206 g_mutex_lock (&conninfo->recv_lock);
2207 ret = gst_rtsp_connection_receive (conninfo->connection, message, timeout);
2208 g_mutex_unlock (&conninfo->recv_lock);
2210 ret = GST_RTSP_ERROR;
2217 gst_rtspsrc_get_position (GstRTSPSrc * src)
2222 query = gst_query_new_position (GST_FORMAT_TIME);
2223 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2224 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2225 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2229 if (stream->srcpad) {
2230 if (gst_pad_query (stream->srcpad, query)) {
2231 gst_query_parse_position (query, &fmt, &pos);
2232 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2233 GST_TIME_ARGS (pos));
2234 src->last_pos = pos;
2244 gst_query_unref (query);
2248 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2253 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2255 gboolean flush, skip;
2258 GstSegment seeksegment = { 0, };
2260 const gchar *seek_style = NULL;
2263 GST_DEBUG_OBJECT (src, "doing seek with event");
2265 gst_event_parse_seek (event, &rate, &format, &flags,
2266 &cur_type, &cur, &stop_type, &stop);
2268 /* no negative rates yet */
2272 /* we need TIME format */
2273 if (format != src->segment.format)
2276 GST_DEBUG_OBJECT (src, "doing seek without event");
2278 cur_type = GST_SEEK_TYPE_SET;
2279 stop_type = GST_SEEK_TYPE_SET;
2282 /* get flush flag */
2283 flush = flags & GST_SEEK_FLAG_FLUSH;
2284 skip = flags & GST_SEEK_FLAG_SKIP;
2286 /* now we need to make sure the streaming thread is stopped. We do this by
2287 * either sending a FLUSH_START event downstream which will cause the
2288 * streaming thread to stop with a WRONG_STATE.
2289 * For a non-flushing seek we simply pause the task, which will happen as soon
2290 * as it completes one iteration (and thus might block when the sink is
2291 * blocking in preroll). */
2293 GST_DEBUG_OBJECT (src, "starting flush");
2294 gst_rtspsrc_flush (src, TRUE, FALSE);
2297 gst_task_pause (src->task);
2301 /* we should now be able to grab the streaming thread because we stopped it
2302 * with the above flush/pause code */
2303 GST_RTSP_STREAM_LOCK (src);
2305 GST_DEBUG_OBJECT (src, "stopped streaming");
2307 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2308 gst_rtspsrc_connection_flush (src, FALSE);
2310 /* copy segment, we need this because we still need the old
2311 * segment when we close the current segment. */
2312 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2314 /* configure the seek parameters in the seeksegment. We will then have the
2315 * right values in the segment to perform the seek */
2317 GST_DEBUG_OBJECT (src, "configuring seek");
2318 gst_segment_do_seek (&seeksegment, rate, format, flags,
2319 cur_type, cur, stop_type, stop, &update);
2322 /* figure out the last position we need to play. If it's configured (stop !=
2323 * -1), use that, else we play until the total duration of the file */
2324 if ((stop = seeksegment.stop) == -1)
2325 stop = seeksegment.duration;
2327 /* if we were playing, pause first */
2328 playing = (src->state == GST_RTSP_STATE_PLAYING);
2330 /* obtain current position in case seek fails */
2331 gst_rtspsrc_get_position (src);
2332 gst_rtspsrc_pause (src, FALSE);
2336 src->state = GST_RTSP_STATE_SEEKING;
2338 /* PLAY will add the range header now. */
2339 src->need_range = TRUE;
2341 /* prepare for streaming again */
2343 /* if we started flush, we stop now */
2344 GST_DEBUG_OBJECT (src, "stopping flush");
2345 gst_rtspsrc_flush (src, FALSE, playing);
2348 /* now we did the seek and can activate the new segment values */
2349 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2351 /* if we're doing a segment seek, post a SEGMENT_START message */
2352 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2353 gst_element_post_message (GST_ELEMENT_CAST (src),
2354 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2355 src->segment.format, src->segment.position));
2358 /* now create the newsegment */
2359 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2360 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2363 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2364 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2365 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2366 stream->discont = TRUE;
2369 /* and continue playing if needed */
2370 GST_OBJECT_LOCK (src);
2371 playing = (GST_STATE_PENDING (src) == GST_STATE_VOID_PENDING
2372 && GST_STATE (src) == GST_STATE_PLAYING)
2373 || (GST_STATE_PENDING (src) == GST_STATE_PLAYING);
2374 GST_OBJECT_UNLOCK (src);
2376 if (src->version >= GST_RTSP_VERSION_2_0) {
2377 if (flags & GST_SEEK_FLAG_ACCURATE)
2379 else if (flags & GST_SEEK_FLAG_KEY_UNIT)
2380 seek_style = "CoRAP";
2381 else if (flags & GST_SEEK_FLAG_KEY_UNIT
2382 && flags & GST_SEEK_FLAG_SNAP_BEFORE)
2383 seek_style = "First-Prior";
2384 else if (flags & GST_SEEK_FLAG_KEY_UNIT && flags & GST_SEEK_FLAG_SNAP_AFTER)
2385 seek_style = "Next";
2389 gst_rtspsrc_play (src, &seeksegment, FALSE, seek_style);
2391 GST_RTSP_STREAM_UNLOCK (src);
2398 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2403 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2409 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2413 gboolean res = TRUE;
2416 src = GST_RTSPSRC_CAST (parent);
2418 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2419 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2421 switch (GST_EVENT_TYPE (event)) {
2422 case GST_EVENT_SEEK:
2423 res = gst_rtspsrc_perform_seek (src, event);
2427 case GST_EVENT_NAVIGATION:
2428 case GST_EVENT_LATENCY:
2436 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2437 res = gst_pad_send_event (target, event);
2438 gst_object_unref (target);
2440 gst_event_unref (event);
2443 gst_event_unref (event);
2450 gst_rtspsrc_handle_src_sink_event (GstPad * pad, GstObject * parent,
2453 GstRTSPStream *stream;
2455 stream = gst_pad_get_element_private (pad);
2457 switch (GST_EVENT_TYPE (event)) {
2458 case GST_EVENT_STREAM_START:{
2459 const gchar *upstream_id;
2462 gst_event_parse_stream_start (event, &upstream_id);
2463 stream_id = g_strdup_printf ("%s/%s", upstream_id, stream->stream_id);
2465 gst_event_unref (event);
2466 event = gst_event_new_stream_start (stream_id);
2473 return gst_pad_push_event (stream->srcpad, event);
2476 /* this is the final event function we receive on the internal source pad when
2477 * we deal with TCP connections */
2479 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2484 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2486 switch (GST_EVENT_TYPE (event)) {
2487 case GST_EVENT_SEEK:
2489 case GST_EVENT_NAVIGATION:
2490 case GST_EVENT_LATENCY:
2492 gst_event_unref (event);
2499 /* this is the final query function we receive on the internal source pad when
2500 * we deal with TCP connections */
2502 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2506 gboolean res = TRUE;
2508 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2510 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2511 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2513 switch (GST_QUERY_TYPE (query)) {
2514 case GST_QUERY_POSITION:
2519 case GST_QUERY_DURATION:
2523 gst_query_parse_duration (query, &format, NULL);
2526 case GST_FORMAT_TIME:
2527 gst_query_set_duration (query, format, src->segment.duration);
2535 case GST_QUERY_LATENCY:
2537 /* we are live with a min latency of 0 and unlimited max latency, this
2538 * result will be updated by the session manager if there is any. */
2539 gst_query_set_latency (query, TRUE, 0, -1);
2549 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2551 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2555 gboolean res = FALSE;
2557 src = GST_RTSPSRC_CAST (parent);
2559 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2560 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2562 switch (GST_QUERY_TYPE (query)) {
2563 case GST_QUERY_DURATION:
2567 gst_query_parse_duration (query, &format, NULL);
2570 case GST_FORMAT_TIME:
2571 gst_query_set_duration (query, format, src->segment.duration);
2579 case GST_QUERY_SEEKING:
2583 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2584 if (format == GST_FORMAT_TIME) {
2586 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2587 GstClockTime start = 0, duration = src->segment.duration;
2589 /* seeking without duration is unlikely */
2590 seekable = seekable && src->seekable >= 0.0 && src->segment.duration &&
2591 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2594 if (src->seekable > 0.0) {
2595 start = src->last_pos - src->seekable * GST_SECOND;
2597 /* src->seekable == 0 means that we can only seek to 0 */
2603 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, start,
2613 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2615 gst_query_set_uri (query, uri);
2623 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2625 /* forward the query to the proxy target pad */
2627 res = gst_pad_query (target, query);
2628 gst_object_unref (target);
2637 /* callback for RTCP messages to be sent to the server when operating in TCP
2639 static GstFlowReturn
2640 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2643 GstRTSPStream *stream;
2644 GstFlowReturn res = GST_FLOW_OK;
2649 GstRTSPMessage message = { 0 };
2650 GstRTSPConnInfo *conninfo;
2652 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2653 src = stream->parent;
2655 gst_buffer_map (buffer, &map, GST_MAP_READ);
2659 gst_rtsp_message_init_data (&message, stream->channel[1]);
2661 /* lend the body data to the message */
2662 gst_rtsp_message_take_body (&message, data, size);
2664 if (stream->conninfo.connection)
2665 conninfo = &stream->conninfo;
2667 conninfo = &src->conninfo;
2669 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2670 ret = gst_rtspsrc_connection_send (src, conninfo, &message, NULL);
2671 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2673 /* and steal it away again because we will free it when unreffing the
2675 gst_rtsp_message_steal_body (&message, &data, &size);
2676 gst_rtsp_message_unset (&message);
2678 gst_buffer_unmap (buffer, &map);
2679 gst_buffer_unref (buffer);
2684 static GstPadProbeReturn
2685 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2687 GstRTSPSrc *src = user_data;
2689 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2690 GST_DEBUG_PAD_NAME (pad));
2692 /* activate the streams */
2693 GST_OBJECT_LOCK (src);
2694 if (!src->need_activate)
2697 src->need_activate = FALSE;
2698 GST_OBJECT_UNLOCK (src);
2700 gst_rtspsrc_activate_streams (src);
2702 return GST_PAD_PROBE_OK;
2706 GST_OBJECT_UNLOCK (src);
2707 return GST_PAD_PROBE_OK;
2712 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
2714 GstPad *gpad = GST_PAD_CAST (user_data);
2716 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
2717 gst_pad_store_sticky_event (gpad, *event);
2722 /* this callback is called when the session manager generated a new src pad with
2723 * payloaded RTP packets. We simply ghost the pad here. */
2725 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2728 GstPadTemplate *template;
2731 GstRTSPStream *stream;
2733 GstPad *internal_src;
2735 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2737 GST_RTSP_STATE_LOCK (src);
2739 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2740 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2741 goto unknown_stream;
2743 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2745 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2747 goto unknown_stream;
2750 stream->ssrc = ssrc;
2752 /* we'll add it later see below */
2753 stream->added = TRUE;
2755 /* check if we added all streams */
2757 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
2758 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
2760 GST_DEBUG_OBJECT (src, "stream %p, container %d, added %d, setup %d",
2761 ostream, ostream->container, ostream->added, ostream->setup);
2763 /* if we find a stream for which we did a setup that is not added, we
2764 * need to wait some more */
2765 if (ostream->setup && !ostream->added) {
2770 GST_RTSP_STATE_UNLOCK (src);
2772 /* create a new pad we will use to stream to */
2773 template = gst_static_pad_template_get (&rtptemplate);
2774 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2775 gst_object_unref (template);
2778 /* We intercept and modify the stream start event */
2780 GST_PAD (gst_proxy_pad_get_internal (GST_PROXY_PAD (stream->srcpad)));
2781 gst_pad_set_element_private (internal_src, stream);
2782 gst_pad_set_event_function (internal_src, gst_rtspsrc_handle_src_sink_event);
2783 gst_object_unref (internal_src);
2785 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2786 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2787 gst_pad_set_active (stream->srcpad, TRUE);
2788 gst_pad_sticky_events_foreach (pad, copy_sticky_events, stream->srcpad);
2789 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2792 GST_DEBUG_OBJECT (src, "We added all streams");
2793 /* when we get here, all stream are added and we can fire the no-more-pads
2795 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2803 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2804 GST_RTSP_STATE_UNLOCK (src);
2811 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
2815 len = stream->ptmap->len;
2816 for (i = 0; i < len; i++) {
2817 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
2825 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2827 GstRTSPStream *stream;
2830 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2832 GST_RTSP_STATE_LOCK (src);
2833 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2835 goto unknown_stream;
2837 if ((caps = stream_get_caps_for_pt (stream, pt)))
2838 gst_caps_ref (caps);
2839 GST_RTSP_STATE_UNLOCK (src);
2845 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2846 GST_RTSP_STATE_UNLOCK (src);
2852 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2854 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2860 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2866 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2872 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2874 GstRTSPSrc *src = stream->parent;
2877 g_object_get (source, "ssrc", &ssrc, NULL);
2879 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
2880 ssrc, stream->ssrc, stream->id);
2882 if (ssrc == stream->ssrc)
2883 gst_rtspsrc_do_stream_eos (src, stream);
2887 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2889 GstRTSPSrc *src = stream->parent;
2892 g_object_get (source, "ssrc", &ssrc, NULL);
2894 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
2895 ssrc, stream->ssrc, stream->id);
2897 if (ssrc == stream->ssrc)
2898 gst_rtspsrc_do_stream_eos (src, stream);
2902 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2904 GstRTSPStream *stream;
2906 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2908 /* get stream for session */
2909 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2911 gst_rtspsrc_do_stream_eos (src, stream);
2916 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2918 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2923 set_manager_buffer_mode (GstRTSPSrc * src)
2925 GObjectClass *klass;
2927 if (src->manager == NULL)
2930 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2932 if (!g_object_class_find_property (klass, "buffer-mode"))
2935 if (src->buffer_mode != BUFFER_MODE_AUTO) {
2936 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
2941 GST_DEBUG_OBJECT (src,
2942 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
2944 if (src->provided_clock) {
2945 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
2947 if (clock == src->provided_clock) {
2948 GST_DEBUG_OBJECT (src, "selected synced");
2949 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
2952 gst_object_unref (clock);
2957 /* Otherwise fall-through and use another buffer mode */
2959 gst_object_unref (clock);
2962 GST_DEBUG_OBJECT (src, "auto buffering mode");
2963 if (src->use_buffering) {
2964 GST_DEBUG_OBJECT (src, "selected buffer");
2965 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
2967 GST_DEBUG_OBJECT (src, "selected slave");
2968 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
2973 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
2977 GstMIKEYMessage *msg = stream->mikey;
2979 GST_DEBUG ("request key SSRC %u", ssrc);
2981 caps = gst_caps_ref (stream_get_caps_for_pt (stream, stream->default_pt));
2982 caps = gst_caps_make_writable (caps);
2984 /* parse crypto sessions and look for the SSRC rollover counter */
2985 msg = stream->mikey;
2986 for (i = 0; msg && i < gst_mikey_message_get_n_cs (msg); i++) {
2987 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
2989 if (ssrc == map->ssrc) {
2990 gst_caps_set_simple (caps, "roc", G_TYPE_UINT, map->roc, NULL);
2999 request_rtp_decoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
3001 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3002 if (stream->id != session)
3005 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3006 stream->profile != GST_RTSP_PROFILE_SAVPF)
3009 if (stream->srtpdec == NULL) {
3012 name = g_strdup_printf ("srtpdec_%u", session);
3013 stream->srtpdec = gst_element_factory_make ("srtpdec", name);
3016 if (stream->srtpdec == NULL) {
3017 GST_ELEMENT_ERROR (stream->parent, CORE, MISSING_PLUGIN, (NULL),
3018 ("no srtpdec element present!"));
3021 g_signal_connect (stream->srtpdec, "request-key",
3022 (GCallback) request_key, stream);
3024 return gst_object_ref (stream->srtpdec);
3028 request_rtcp_encoder (GstElement * rtpbin, guint session,
3029 GstRTSPStream * stream)
3034 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3035 if (stream->id != session)
3038 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3039 stream->profile != GST_RTSP_PROFILE_SAVPF)
3042 if (stream->srtpenc == NULL) {
3045 name = g_strdup_printf ("srtpenc_%u", session);
3046 stream->srtpenc = gst_element_factory_make ("srtpenc", name);
3049 if (stream->srtpenc == NULL) {
3050 GST_ELEMENT_ERROR (stream->parent, CORE, MISSING_PLUGIN, (NULL),
3051 ("no srtpenc element present!"));
3055 /* get RTCP crypto parameters from caps */
3056 s = gst_caps_get_structure (stream->srtcpparams, 0);
3060 GType ciphertype, authtype;
3061 GValue rtcp_cipher = G_VALUE_INIT, rtcp_auth = G_VALUE_INIT;
3063 ciphertype = g_type_from_name ("GstSrtpCipherType");
3064 authtype = g_type_from_name ("GstSrtpAuthType");
3065 g_value_init (&rtcp_cipher, ciphertype);
3066 g_value_init (&rtcp_auth, authtype);
3068 str = gst_structure_get_string (s, "srtcp-cipher");
3069 gst_value_deserialize (&rtcp_cipher, str);
3070 str = gst_structure_get_string (s, "srtcp-auth");
3071 gst_value_deserialize (&rtcp_auth, str);
3072 gst_structure_get (s, "srtp-key", GST_TYPE_BUFFER, &buf, NULL);
3074 g_object_set_property (G_OBJECT (stream->srtpenc), "rtp-cipher",
3076 g_object_set_property (G_OBJECT (stream->srtpenc), "rtp-auth",
3078 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-cipher",
3080 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-auth",
3082 g_object_set (stream->srtpenc, "key", buf, NULL);
3084 g_value_unset (&rtcp_cipher);
3085 g_value_unset (&rtcp_auth);
3086 gst_buffer_unref (buf);
3089 name = g_strdup_printf ("rtcp_sink_%d", session);
3090 pad = gst_element_get_request_pad (stream->srtpenc, name);
3092 gst_object_unref (pad);
3094 return gst_object_ref (stream->srtpenc);
3098 request_aux_receiver (GstElement * rtpbin, guint sessid, GstRTSPSrc * src)
3100 GstElement *rtx, *bin;
3103 GstRTSPStream *stream;
3105 stream = find_stream (src, &sessid, (gpointer) find_stream_by_id);
3107 GST_WARNING_OBJECT (src, "Stream %u not found", sessid);
3111 GST_INFO_OBJECT (src, "creating retransmision receiver for session %u "
3112 "with map %" GST_PTR_FORMAT, sessid, stream->rtx_pt_map);
3113 bin = gst_bin_new (NULL);
3114 rtx = gst_element_factory_make ("rtprtxreceive", NULL);
3115 g_object_set (rtx, "payload-type-map", stream->rtx_pt_map, NULL);
3116 gst_bin_add (GST_BIN (bin), rtx);
3118 pad = gst_element_get_static_pad (rtx, "src");
3119 name = g_strdup_printf ("src_%u", sessid);
3120 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3122 gst_object_unref (pad);
3124 pad = gst_element_get_static_pad (rtx, "sink");
3125 name = g_strdup_printf ("sink_%u", sessid);
3126 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3128 gst_object_unref (pad);
3134 add_retransmission (GstRTSPSrc * src, GstRTSPTransport * transport)
3138 gboolean do_retransmission = FALSE;
3140 if (transport->trans != GST_RTSP_TRANS_RTP)
3142 if (transport->profile != GST_RTSP_PROFILE_AVPF &&
3143 transport->profile != GST_RTSP_PROFILE_SAVPF)
3146 signal_id = g_signal_lookup ("request-aux-receiver",
3147 G_OBJECT_TYPE (src->manager));
3148 /* there's already something connected */
3149 if (g_signal_handler_find (src->manager, G_SIGNAL_MATCH_ID, signal_id, 0,
3150 NULL, NULL, NULL) != 0) {
3151 GST_DEBUG_OBJECT (src, "Not adding RTX AUX element as "
3152 "\"request-aux-receiver\" signal is "
3153 "already used by the application");
3157 /* build the retransmission payload type map */
3158 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3159 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3160 gboolean do_retransmission_stream = FALSE;
3163 if (stream->rtx_pt_map)
3164 gst_structure_free (stream->rtx_pt_map);
3165 stream->rtx_pt_map = gst_structure_new_empty ("application/x-rtp-pt-map");
3167 for (i = 0; i < stream->ptmap->len; i++) {
3168 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3169 GstStructure *s = gst_caps_get_structure (item->caps, 0);
3170 const gchar *encoding;
3172 /* we only care about RTX streams */
3173 if ((encoding = gst_structure_get_string (s, "encoding-name"))
3174 && g_strcmp0 (encoding, "RTX") == 0) {
3175 const gchar *stream_pt_s;
3178 if (gst_structure_get_int (s, "payload", &rtx_pt)
3179 && (stream_pt_s = gst_structure_get_string (s, "apt"))) {
3182 gst_structure_set (stream->rtx_pt_map, stream_pt_s, G_TYPE_UINT,
3184 do_retransmission_stream = TRUE;
3190 if (do_retransmission_stream) {
3191 GST_DEBUG_OBJECT (src, "built retransmission payload map for stream "
3192 "id %i: %" GST_PTR_FORMAT, stream->id, stream->rtx_pt_map);
3193 do_retransmission = TRUE;
3195 GST_DEBUG_OBJECT (src, "no retransmission payload map for stream "
3196 "id %i", stream->id);
3197 gst_structure_free (stream->rtx_pt_map);
3198 stream->rtx_pt_map = NULL;
3202 if (do_retransmission) {
3203 GST_DEBUG_OBJECT (src, "Enabling retransmissions");
3205 g_object_set (src->manager, "do-retransmission", TRUE, NULL);
3207 /* enable RFC4588 retransmission handling by setting rtprtxreceive
3208 * as the "aux" element of rtpbin */
3209 g_signal_connect (src->manager, "request-aux-receiver",
3210 (GCallback) request_aux_receiver, src);
3212 GST_DEBUG_OBJECT (src,
3213 "Not enabling retransmissions as no stream had a retransmission payload map");
3217 /* try to get and configure a manager */
3219 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
3220 GstRTSPTransport * transport)
3222 const gchar *manager;
3224 GstStateChangeReturn ret;
3226 /* find a manager */
3227 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
3231 GST_DEBUG_OBJECT (src, "using manager %s", manager);
3233 /* configure the manager */
3234 if (src->manager == NULL) {
3235 GObjectClass *klass;
3237 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
3239 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
3243 goto use_no_manager;
3245 if (!(src->manager = gst_element_factory_make (manager, "manager")))
3246 goto manager_failed;
3249 /* we manage this element */
3250 gst_element_set_locked_state (src->manager, TRUE);
3251 gst_bin_add (GST_BIN_CAST (src), src->manager);
3253 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
3254 if (ret == GST_STATE_CHANGE_FAILURE)
3255 goto start_manager_failure;
3257 g_object_set (src->manager, "latency", src->latency, NULL);
3259 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3261 if (g_object_class_find_property (klass, "ntp-sync")) {
3262 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
3265 if (g_object_class_find_property (klass, "rfc7273-sync")) {
3266 g_object_set (src->manager, "rfc7273-sync", src->rfc7273_sync, NULL);
3269 if (src->use_pipeline_clock) {
3270 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
3271 g_object_set (src->manager, "use-pipeline-clock", TRUE, NULL);
3274 if (g_object_class_find_property (klass, "ntp-time-source")) {
3275 g_object_set (src->manager, "ntp-time-source", src->ntp_time_source,
3280 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
3281 g_object_set (src->manager, "sdes", src->sdes, NULL);
3284 if (g_object_class_find_property (klass, "drop-on-latency")) {
3285 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
3289 if (g_object_class_find_property (klass, "max-rtcp-rtp-time-diff")) {
3290 g_object_set (src->manager, "max-rtcp-rtp-time-diff",
3291 src->max_rtcp_rtp_time_diff, NULL);
3294 if (g_object_class_find_property (klass, "max-ts-offset-adjustment")) {
3295 g_object_set (src->manager, "max-ts-offset-adjustment",
3296 src->max_ts_offset_adjustment, NULL);
3299 if (g_object_class_find_property (klass, "max-ts-offset")) {
3300 gint64 max_ts_offset;
3302 /* setting max-ts-offset in the manager has side effects so only do it
3303 * if the value differs */
3304 g_object_get (src->manager, "max-ts-offset", &max_ts_offset, NULL);
3305 if (max_ts_offset != src->max_ts_offset) {
3306 g_object_set (src->manager, "max-ts-offset", src->max_ts_offset,
3311 /* buffer mode pauses are handled by adding offsets to buffer times,
3312 * but some depayloaders may have a hard time syncing output times
3313 * with such input times, e.g. container ones, most notably ASF */
3314 /* TODO alternatives are having an event that indicates these shifts,
3315 * or having rtsp extensions provide suggestion on buffer mode */
3316 /* valid duration implies not likely live pipeline,
3317 * so slaving in jitterbuffer does not make much sense
3318 * (and might mess things up due to bursts) */
3319 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
3320 src->segment.duration && stream->container) {
3321 src->use_buffering = TRUE;
3323 src->use_buffering = FALSE;
3326 set_manager_buffer_mode (src);
3328 /* connect to signals */
3329 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
3331 src->manager_sig_id =
3332 g_signal_connect (src->manager, "pad-added",
3333 (GCallback) new_manager_pad, src);
3334 src->manager_ptmap_id =
3335 g_signal_connect (src->manager, "request-pt-map",
3336 (GCallback) request_pt_map, src);
3338 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
3341 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
3344 if (src->do_retransmission)
3345 add_retransmission (src, transport);
3347 g_signal_connect (src->manager, "request-rtp-decoder",
3348 (GCallback) request_rtp_decoder, stream);
3349 g_signal_connect (src->manager, "request-rtcp-decoder",
3350 (GCallback) request_rtp_decoder, stream);
3351 g_signal_connect (src->manager, "request-rtcp-encoder",
3352 (GCallback) request_rtcp_encoder, stream);
3354 /* we stream directly to the manager, get some pads. Each RTSP stream goes
3355 * into a separate RTP session. */
3356 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
3357 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
3359 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
3360 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
3363 /* now configure the bandwidth in the manager */
3364 if (g_signal_lookup ("get-internal-session",
3365 G_OBJECT_TYPE (src->manager)) != 0) {
3366 GObject *rtpsession;
3368 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
3371 GstRTPProfile rtp_profile;
3373 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
3375 stream->session = rtpsession;
3377 if (stream->as_bandwidth != -1) {
3378 GST_INFO_OBJECT (src, "setting AS: %f",
3379 (gdouble) (stream->as_bandwidth * 1000));
3380 g_object_set (rtpsession, "bandwidth",
3381 (gdouble) (stream->as_bandwidth * 1000), NULL);
3383 if (stream->rr_bandwidth != -1) {
3384 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
3385 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
3388 if (stream->rs_bandwidth != -1) {
3389 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
3390 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
3394 switch (stream->profile) {
3395 case GST_RTSP_PROFILE_AVPF:
3396 rtp_profile = GST_RTP_PROFILE_AVPF;
3398 case GST_RTSP_PROFILE_SAVP:
3399 rtp_profile = GST_RTP_PROFILE_SAVP;
3401 case GST_RTSP_PROFILE_SAVPF:
3402 rtp_profile = GST_RTP_PROFILE_SAVPF;
3404 case GST_RTSP_PROFILE_AVP:
3406 rtp_profile = GST_RTP_PROFILE_AVP;
3410 g_object_set (rtpsession, "rtp-profile", rtp_profile, NULL);
3412 g_object_set (rtpsession, "probation", src->probation, NULL);
3414 g_object_set (rtpsession, "internal-ssrc", stream->send_ssrc, NULL);
3416 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
3418 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
3420 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
3422 g_signal_connect (rtpsession, "on-ssrc-active",
3423 (GCallback) on_ssrc_active, stream);
3434 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3439 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
3442 start_manager_failure:
3444 GST_DEBUG_OBJECT (src, "could not start session manager");
3449 /* free the UDP sources allocated when negotiating a transport.
3450 * This function is called when the server negotiated to a transport where the
3451 * UDP sources are not needed anymore, such as TCP or multicast. */
3453 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
3457 for (i = 0; i < 2; i++) {
3458 if (stream->udpsrc[i]) {
3459 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
3460 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
3461 gst_object_unref (stream->udpsrc[i]);
3462 stream->udpsrc[i] = NULL;
3467 /* for TCP, create pads to send and receive data to and from the manager and to
3468 * intercept various events and queries
3471 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
3472 GstRTSPTransport * transport, GstPad ** outpad)
3475 GstPadTemplate *template;
3476 GstPad *pad0, *pad1;
3478 /* configure for interleaved delivery, nothing needs to be done
3479 * here, the loop function will call the chain functions of the
3480 * session manager. */
3481 stream->channel[0] = transport->interleaved.min;
3482 stream->channel[1] = transport->interleaved.max;
3483 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
3484 stream->channel[0], stream->channel[1]);
3486 /* we can remove the allocated UDP ports now */
3487 gst_rtspsrc_stream_free_udp (stream);
3489 /* no session manager, send data to srcpad directly */
3490 if (!stream->channelpad[0]) {
3491 GST_DEBUG_OBJECT (src, "no manager, creating pad");
3493 /* create a new pad we will use to stream to */
3494 name = g_strdup_printf ("stream_%u", stream->id);
3495 template = gst_static_pad_template_get (&rtptemplate);
3496 stream->channelpad[0] = gst_pad_new_from_template (template, name);
3497 gst_object_unref (template);
3500 /* set caps and activate */
3501 gst_pad_use_fixed_caps (stream->channelpad[0]);
3502 gst_pad_set_active (stream->channelpad[0], TRUE);
3504 *outpad = gst_object_ref (stream->channelpad[0]);
3506 GST_DEBUG_OBJECT (src, "using manager source pad");
3508 template = gst_static_pad_template_get (&anysrctemplate);
3510 /* allocate pads for sending the channel data into the manager */
3511 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
3512 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
3513 gst_object_unref (stream->channelpad[0]);
3514 stream->channelpad[0] = pad0;
3515 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
3516 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
3517 gst_pad_set_element_private (pad0, src);
3518 gst_pad_set_active (pad0, TRUE);
3520 if (stream->channelpad[1]) {
3521 /* if we have a sinkpad for the other channel, create a pad and link to the
3523 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
3524 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
3525 gst_pad_link_full (pad1, stream->channelpad[1],
3526 GST_PAD_LINK_CHECK_NOTHING);
3527 gst_object_unref (stream->channelpad[1]);
3528 stream->channelpad[1] = pad1;
3529 gst_pad_set_active (pad1, TRUE);
3531 gst_object_unref (template);
3533 /* setup RTCP transport back to the server if we have to. */
3534 if (src->manager && src->do_rtcp) {
3537 template = gst_static_pad_template_get (&anysinktemplate);
3539 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
3540 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
3541 gst_pad_set_element_private (stream->rtcppad, stream);
3542 gst_pad_set_active (stream->rtcppad, TRUE);
3544 /* get session RTCP pad */
3545 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3546 pad = gst_element_get_request_pad (src->manager, name);
3551 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3552 gst_object_unref (pad);
3555 gst_object_unref (template);
3561 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
3562 GstRTSPTransport * transport, const gchar ** destination, gint * min,
3563 gint * max, guint * ttl)
3565 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3567 if (!(*destination = transport->destination))
3568 *destination = stream->destination;
3571 /* transport first */
3572 *min = transport->port.min;
3573 *max = transport->port.max;
3574 if (*min == -1 && *max == -1) {
3575 /* then try from SDP */
3576 if (stream->port != 0) {
3577 *min = stream->port;
3578 *max = stream->port + 1;
3584 if (!(*ttl = transport->ttl))
3589 /* first take the source, then the endpoint to figure out where to send
3591 if (!(*destination = transport->source)) {
3592 if (src->conninfo.connection)
3593 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3594 else if (stream->conninfo.connection)
3596 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3600 /* for unicast we only expect the ports here */
3601 *min = transport->server_port.min;
3602 *max = transport->server_port.max;
3607 /* For multicast create UDP sources and join the multicast group. */
3609 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3610 GstRTSPTransport * transport, GstPad ** outpad)
3613 const gchar *destination;
3616 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3618 /* we can remove the allocated UDP ports now */
3619 gst_rtspsrc_stream_free_udp (stream);
3621 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3624 /* we need a destination now */
3625 if (destination == NULL)
3626 goto no_destination;
3628 /* we really need ports now or we won't be able to receive anything at all */
3629 if (min == -1 && max == -1)
3632 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3633 destination, min, max);
3635 /* creating UDP source for RTP */
3637 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3639 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3641 if (stream->udpsrc[0] == NULL)
3644 /* take ownership */
3645 gst_object_ref_sink (stream->udpsrc[0]);
3647 if (src->udp_buffer_size != 0)
3648 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3649 src->udp_buffer_size, NULL);
3651 if (src->multi_iface != NULL)
3652 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3653 src->multi_iface, NULL);
3656 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3657 gst_element_set_state (stream->udpsrc[0], GST_STATE_READY);
3660 /* creating another UDP source for RTCP */
3664 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3666 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3668 if (stream->udpsrc[1] == NULL)
3671 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3672 stream->profile == GST_RTSP_PROFILE_SAVPF)
3673 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3675 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3676 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3677 gst_caps_unref (caps);
3679 /* take ownership */
3680 gst_object_ref_sink (stream->udpsrc[1]);
3682 if (src->multi_iface != NULL)
3683 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3684 src->multi_iface, NULL);
3686 gst_element_set_state (stream->udpsrc[1], GST_STATE_READY);
3693 GST_DEBUG_OBJECT (src, "no UDP source element found");
3698 GST_DEBUG_OBJECT (src, "no destination found");
3703 GST_DEBUG_OBJECT (src, "no ports found");
3708 /* configure the remainder of the UDP ports */
3710 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3711 GstRTSPTransport * transport, GstPad ** outpad)
3713 /* we manage the UDP elements now. For unicast, the UDP sources where
3714 * allocated in the stream when we suggested a transport. */
3715 if (stream->udpsrc[0]) {
3718 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3719 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3721 GST_DEBUG_OBJECT (src, "setting up UDP source");
3723 /* configure a timeout on the UDP port. When the timeout message is
3724 * posted, we assume UDP transport is not possible. We reconnect using TCP
3726 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3727 src->udp_timeout * 1000, NULL);
3729 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt)))
3730 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
3732 /* get output pad of the UDP source. */
3733 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3735 /* save it so we can unblock */
3736 stream->blockedpad = *outpad;
3738 /* configure pad block on the pad. As soon as there is dataflow on the
3739 * UDP source, we know that UDP is not blocked by a firewall and we can
3740 * configure all the streams to let the application autoplug decoders. */
3742 gst_pad_add_probe (stream->blockedpad,
3743 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3744 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocked, src, NULL);
3746 if (stream->channelpad[0]) {
3747 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3748 /* configure for UDP delivery, we need to connect the UDP pads to
3749 * the session plugin. */
3750 gst_pad_link_full (*outpad, stream->channelpad[0],
3751 GST_PAD_LINK_CHECK_NOTHING);
3752 gst_object_unref (*outpad);
3754 /* we connected to pad-added signal to get pads from the manager */
3756 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3761 if (stream->udpsrc[1]) {
3764 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3765 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3767 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3768 stream->profile == GST_RTSP_PROFILE_SAVPF)
3769 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3771 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3772 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3773 gst_caps_unref (caps);
3775 if (stream->channelpad[1]) {
3778 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3780 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3781 gst_pad_link_full (pad, stream->channelpad[1],
3782 GST_PAD_LINK_CHECK_NOTHING);
3783 gst_object_unref (pad);
3785 /* leave unlinked */
3791 /* configure the UDP sink back to the server for status reports */
3793 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3794 GstRTSPStream * stream, GstRTSPTransport * transport)
3797 gint rtp_port, rtcp_port;
3798 gboolean do_rtp, do_rtcp;
3799 const gchar *destination;
3804 /* get transport info */
3805 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
3806 &rtp_port, &rtcp_port, &ttl);
3808 /* see what we need to do */
3809 do_rtp = (rtp_port != -1);
3810 /* it's possible that the server does not want us to send RTCP in which case
3812 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
3814 /* we need a destination when we have RTP or RTCP ports */
3815 if (destination == NULL && (do_rtp || do_rtcp))
3816 goto no_destination;
3818 /* try to construct the fakesrc to the RTP port of the server to open up any
3821 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
3824 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
3825 stream->udpsink[0] =
3826 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3828 if (stream->udpsink[0] == NULL)
3829 goto no_sink_element;
3831 /* don't join multicast group, we will have the source socket do that */
3832 /* no sync or async state changes needed */
3833 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
3834 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3836 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3838 if (stream->udpsrc[0]) {
3839 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
3840 * so that NAT firewalls will open a hole for us */
3841 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
3845 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
3846 /* configure socket and make sure udpsink does not close it when shutting
3847 * down, it belongs to udpsrc after all. */
3848 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
3849 "close-socket", FALSE, NULL);
3850 g_object_unref (socket);
3853 /* the source for the dummy packets to open up NAT */
3854 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
3855 if (stream->fakesrc == NULL)
3856 goto no_fakesrc_element;
3858 /* random data in 5 buffers, a size of 200 bytes should be fine */
3859 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
3860 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
3862 /* keep everything locked */
3863 gst_element_set_locked_state (stream->udpsink[0], TRUE);
3864 gst_element_set_locked_state (stream->fakesrc, TRUE);
3866 gst_object_ref (stream->udpsink[0]);
3867 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
3868 gst_object_ref (stream->fakesrc);
3869 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
3871 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
3872 "sink", GST_PAD_LINK_CHECK_NOTHING);
3875 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
3878 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
3879 stream->udpsink[1] =
3880 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3882 if (stream->udpsink[1] == NULL)
3883 goto no_sink_element;
3885 /* don't join multicast group, we will have the source socket do that */
3886 /* no sync or async state changes needed */
3887 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
3888 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3890 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3892 if (stream->udpsrc[1]) {
3893 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
3894 * because some servers check the port number of where it sends RTCP to identify
3895 * the RTCP packets it receives */
3896 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
3900 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
3901 /* configure socket and make sure udpsink does not close it when shutting
3902 * down, it belongs to udpsrc after all. */
3903 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
3904 "close-socket", FALSE, NULL);
3905 g_object_unref (socket);
3908 /* we keep this playing always */
3909 gst_element_set_locked_state (stream->udpsink[1], TRUE);
3910 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
3912 gst_object_ref (stream->udpsink[1]);
3913 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
3915 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
3917 /* get session RTCP pad */
3918 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3919 pad = gst_element_get_request_pad (src->manager, name);
3924 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3925 gst_object_unref (pad);
3934 GST_ERROR_OBJECT (src, "no destination address specified");
3939 GST_ERROR_OBJECT (src, "no UDP sink element found");
3944 GST_ERROR_OBJECT (src, "no fakesrc element found");
3949 GST_ERROR_OBJECT (src, "failed to create socket");
3954 /* sets up all elements needed for streaming over the specified transport.
3955 * Does not yet expose the element pads, this will be done when there is actuall
3956 * dataflow detected, which might never happen when UDP is blocked in a
3957 * firewall, for example.
3960 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
3961 GstRTSPTransport * transport)
3964 GstPad *outpad = NULL;
3965 GstPadTemplate *template;
3967 const gchar *media_type;
3970 src = stream->parent;
3972 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
3974 /* get the proper media type for this stream now */
3975 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
3976 goto unknown_transport;
3978 goto unknown_transport;
3980 /* configure the final media type */
3981 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
3983 len = stream->ptmap->len;
3984 for (i = 0; i < len; i++) {
3986 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3988 if (item->caps == NULL)
3991 s = gst_caps_get_structure (item->caps, 0);
3992 gst_structure_set_name (s, media_type);
3993 /* set ssrc if known */
3994 if (transport->ssrc)
3995 gst_structure_set (s, "ssrc", G_TYPE_UINT, transport->ssrc, NULL);
3998 /* try to get and configure a manager, channelpad[0-1] will be configured with
3999 * the pads for the manager, or NULL when no manager is needed. */
4000 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
4003 switch (transport->lower_transport) {
4004 case GST_RTSP_LOWER_TRANS_TCP:
4005 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
4006 goto transport_failed;
4008 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
4009 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
4010 goto transport_failed;
4011 /* fallthrough, the rest is the same for UDP and MCAST */
4012 case GST_RTSP_LOWER_TRANS_UDP:
4013 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
4014 goto transport_failed;
4015 /* configure udpsinks back to the server for RTCP messages and for the
4016 * dummy RTP messages to open NAT. */
4017 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
4018 goto transport_failed;
4021 goto unknown_transport;
4025 GST_DEBUG_OBJECT (src, "creating ghostpad");
4027 gst_pad_use_fixed_caps (outpad);
4029 /* create ghostpad, don't add just yet, this will be done when we activate
4031 name = g_strdup_printf ("stream_%u", stream->id);
4032 template = gst_static_pad_template_get (&rtptemplate);
4033 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
4034 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
4035 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
4036 gst_object_unref (template);
4039 gst_object_unref (outpad);
4041 /* mark pad as ok */
4042 stream->last_ret = GST_FLOW_OK;
4049 GST_DEBUG_OBJECT (src, "failed to configure transport");
4054 GST_DEBUG_OBJECT (src, "unknown transport");
4059 GST_DEBUG_OBJECT (src, "cannot get a session manager");
4064 /* send a couple of dummy random packets on the receiver RTP port to the server,
4065 * this should make a firewall think we initiated the data transfer and
4066 * hopefully allow packets to go from the sender port to our RTP receiver port */
4068 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
4072 if (src->nat_method != GST_RTSP_NAT_DUMMY)
4075 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4076 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4078 if (stream->fakesrc && stream->udpsink[0]) {
4079 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
4080 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
4081 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
4082 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
4083 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
4089 /* Adds the source pads of all configured streams to the element.
4090 * This code is performed when we detected dataflow.
4092 * We detect dataflow from either the _loop function or with pad probes on the
4096 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
4100 GST_DEBUG_OBJECT (src, "activating streams");
4102 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4103 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4105 if (stream->udpsrc[0]) {
4106 /* remove timeout, we are streaming now and timeouts will be handled by
4107 * the session manager and jitter buffer */
4108 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
4110 if (stream->srcpad) {
4111 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
4112 gst_pad_set_active (stream->srcpad, TRUE);
4114 /* if we don't have a session manager, set the caps now. If we have a
4115 * session, we will get a notification of the pad and the caps. */
4116 if (!src->manager) {
4119 caps = stream_get_caps_for_pt (stream, stream->default_pt);
4120 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
4121 gst_pad_set_caps (stream->srcpad, caps);
4124 if (!stream->added) {
4125 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
4126 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
4127 stream->added = TRUE;
4132 /* unblock all pads */
4133 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4134 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4136 if (stream->blockid) {
4137 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
4138 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
4139 stream->blockid = 0;
4147 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
4148 gboolean reset_manager)
4151 guint64 start, stop;
4152 gdouble play_speed, play_scale;
4154 GST_DEBUG_OBJECT (src, "configuring stream caps");
4156 start = segment->position;
4157 stop = segment->duration;
4158 play_speed = segment->rate;
4159 play_scale = segment->applied_rate;
4161 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4162 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4168 len = stream->ptmap->len;
4169 for (j = 0; j < len; j++) {
4171 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
4173 if (item->caps == NULL)
4176 caps = gst_caps_make_writable (item->caps);
4178 if (stream->timebase != -1)
4179 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
4180 (guint) stream->timebase, NULL);
4181 if (stream->seqbase != -1)
4182 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
4183 (guint) stream->seqbase, NULL);
4184 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
4186 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
4187 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
4188 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
4191 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
4194 if (item->pt == stream->default_pt) {
4195 if (stream->udpsrc[0])
4196 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
4197 stream->need_caps = TRUE;
4201 if (reset_manager && src->manager) {
4202 GST_DEBUG_OBJECT (src, "clear session");
4203 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
4207 static GstFlowReturn
4208 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
4213 /* store the value */
4214 stream->last_ret = ret;
4216 /* if it's success we can return the value right away */
4217 if (ret == GST_FLOW_OK)
4220 /* any other error that is not-linked can be returned right
4222 if (ret != GST_FLOW_NOT_LINKED)
4225 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4226 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4227 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4229 ret = ostream->last_ret;
4230 /* some other return value (must be SUCCESS but we can return
4231 * other values as well) */
4232 if (ret != GST_FLOW_NOT_LINKED)
4235 /* if we get here, all other pads were unlinked and we return
4236 * NOT_LINKED then */
4242 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
4245 gboolean res = TRUE;
4247 /* only streams that have a connection to the outside world */
4251 if (stream->udpsrc[0]) {
4252 gst_event_ref (event);
4253 res = gst_element_send_event (stream->udpsrc[0], event);
4254 } else if (stream->channelpad[0]) {
4255 gst_event_ref (event);
4256 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4257 res = gst_pad_push_event (stream->channelpad[0], event);
4259 res = gst_pad_send_event (stream->channelpad[0], event);
4262 if (stream->udpsrc[1]) {
4263 gst_event_ref (event);
4264 res &= gst_element_send_event (stream->udpsrc[1], event);
4265 } else if (stream->channelpad[1]) {
4266 gst_event_ref (event);
4267 if (GST_PAD_IS_SRC (stream->channelpad[1]))
4268 res &= gst_pad_push_event (stream->channelpad[1], event);
4270 res &= gst_pad_send_event (stream->channelpad[1], event);
4274 gst_event_unref (event);
4280 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
4283 gboolean res = TRUE;
4285 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4286 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4288 gst_event_ref (event);
4289 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
4291 gst_event_unref (event);
4296 static GstRTSPResult
4297 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4301 GstRTSPMessage response;
4302 gboolean retry = FALSE;
4303 memset (&response, 0, sizeof (response));
4304 gst_rtsp_message_init (&response);
4306 if (info->connection == NULL) {
4307 if (info->url == NULL) {
4308 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
4309 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
4312 /* create connection */
4313 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
4314 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
4315 goto could_not_create;
4318 gst_rtspsrc_setup_auth (src, &response);
4321 g_free (info->url_str);
4322 info->url_str = gst_rtsp_url_get_request_uri (info->url);
4324 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
4326 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
4327 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
4328 src->tls_validation_flags))
4329 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
4331 if (src->tls_database)
4332 gst_rtsp_connection_set_tls_database (info->connection,
4335 if (src->tls_interaction)
4336 gst_rtsp_connection_set_tls_interaction (info->connection,
4337 src->tls_interaction);
4340 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
4341 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
4343 if (src->proxy_host) {
4344 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
4346 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
4351 if (!info->connected) {
4354 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
4355 ("Connecting to %s", info->location));
4356 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
4357 res = gst_rtsp_connection_connect_with_response (info->connection,
4358 src->ptcp_timeout, &response);
4360 if (response.type == GST_RTSP_MESSAGE_HTTP_RESPONSE &&
4361 response.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4362 gst_rtsp_conninfo_close (src, info, TRUE);
4366 retry = FALSE; // we should not retry more than once
4371 if (res == GST_RTSP_OK)
4372 info->connected = TRUE;
4374 goto could_not_connect;
4376 } while (!info->connected && retry);
4378 gst_rtsp_message_unset (&response);
4384 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
4385 gst_rtsp_message_unset (&response);
4390 gchar *str = gst_rtsp_strresult (res);
4391 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
4393 gst_rtsp_message_unset (&response);
4398 gchar *str = gst_rtsp_strresult (res);
4399 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
4401 gst_rtsp_message_unset (&response);
4406 static GstRTSPResult
4407 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
4410 GST_RTSP_STATE_LOCK (src);
4411 if (info->connected) {
4412 GST_DEBUG_OBJECT (src, "closing connection...");
4413 gst_rtsp_connection_close (info->connection);
4414 info->connected = FALSE;
4416 if (free && info->connection) {
4417 /* free connection */
4418 GST_DEBUG_OBJECT (src, "freeing connection...");
4419 gst_rtsp_connection_free (info->connection);
4420 info->connection = NULL;
4421 info->flushing = FALSE;
4423 GST_RTSP_STATE_UNLOCK (src);
4427 static GstRTSPResult
4428 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4433 GST_DEBUG_OBJECT (src, "reconnecting connection...");
4434 gst_rtsp_conninfo_close (src, info, FALSE);
4435 res = gst_rtsp_conninfo_connect (src, info, async);
4441 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
4445 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
4446 GST_RTSP_STATE_LOCK (src);
4447 if (src->conninfo.connection && src->conninfo.flushing != flush) {
4448 GST_DEBUG_OBJECT (src, "connection flush");
4449 gst_rtsp_connection_flush (src->conninfo.connection, flush);
4450 src->conninfo.flushing = flush;
4452 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4453 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4454 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
4455 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
4456 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
4457 stream->conninfo.flushing = flush;
4460 GST_RTSP_STATE_UNLOCK (src);
4463 static GstRTSPResult
4464 gst_rtspsrc_init_request (GstRTSPSrc * src, GstRTSPMessage * msg,
4465 GstRTSPMethod method, const gchar * uri)
4469 res = gst_rtsp_message_init_request (msg, method, uri);
4473 /* set user-agent */
4474 if (src->user_agent)
4475 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_USER_AGENT, src->user_agent);
4480 /* FIXME, handle server request, reply with OK, for now */
4481 static GstRTSPResult
4482 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
4483 GstRTSPMessage * request)
4485 GstRTSPMessage response = { 0 };
4488 GST_DEBUG_OBJECT (src, "got server request message");
4490 DEBUG_RTSP (src, request);
4492 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
4494 if (res == GST_RTSP_ENOTIMPL) {
4495 /* default implementation, send OK */
4496 GST_DEBUG_OBJECT (src, "prepare OK reply");
4498 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
4503 /* let app parse and reply */
4504 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
4505 0, request, &response);
4507 DEBUG_RTSP (src, &response);
4509 res = gst_rtspsrc_connection_send (src, conninfo, &response, NULL);
4513 gst_rtsp_message_unset (&response);
4514 } else if (res == GST_RTSP_EEOF)
4522 gst_rtsp_message_unset (&response);
4527 /* send server keep-alive */
4528 static GstRTSPResult
4529 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
4531 GstRTSPMessage request = { 0 };
4533 GstRTSPMethod method;
4534 const gchar *control;
4536 if (src->do_rtsp_keep_alive == FALSE) {
4537 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
4538 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4542 GST_DEBUG_OBJECT (src, "creating server keep-alive");
4544 /* find a method to use for keep-alive */
4545 if (src->methods & GST_RTSP_GET_PARAMETER)
4546 method = GST_RTSP_GET_PARAMETER;
4548 method = GST_RTSP_OPTIONS;
4550 control = get_aggregate_control (src);
4551 if (control == NULL)
4554 res = gst_rtspsrc_init_request (src, &request, method, control);
4558 request.type_data.request.version = src->version;
4560 res = gst_rtspsrc_connection_send (src, &src->conninfo, &request, NULL);
4564 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4565 gst_rtsp_message_unset (&request);
4572 GST_WARNING_OBJECT (src, "no control url to send keepalive");
4577 gchar *str = gst_rtsp_strresult (res);
4579 gst_rtsp_message_unset (&request);
4580 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
4581 ("Could not send keep-alive. (%s)", str));
4587 static GstFlowReturn
4588 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
4590 GstFlowReturn ret = GST_FLOW_OK;
4592 GstRTSPStream *stream;
4593 GstPad *outpad = NULL;
4599 channel = message->type_data.data.channel;
4601 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
4603 goto unknown_stream;
4605 if (channel == stream->channel[0]) {
4606 outpad = stream->channelpad[0];
4608 } else if (channel == stream->channel[1]) {
4609 outpad = stream->channelpad[1];
4615 /* take a look at the body to figure out what we have */
4616 gst_rtsp_message_get_body (message, &data, &size);
4618 goto invalid_length;
4620 /* channels are not correct on some servers, do extra check */
4621 if (data[1] >= 200 && data[1] <= 204) {
4622 /* hmm RTCP message switch to the RTCP pad of the same stream. */
4623 outpad = stream->channelpad[1];
4627 /* we have no clue what this is, just ignore then. */
4629 goto unknown_stream;
4631 /* take the message body for further processing */
4632 gst_rtsp_message_steal_body (message, &data, &size);
4634 /* strip the trailing \0 */
4637 buf = gst_buffer_new ();
4638 gst_buffer_append_memory (buf,
4639 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
4641 /* don't need message anymore */
4642 gst_rtsp_message_unset (message);
4644 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
4647 if (src->need_activate) {
4653 guint group_id = gst_util_group_id_next ();
4655 /* generate an SHA256 sum of the URI */
4656 cs = g_checksum_new (G_CHECKSUM_SHA256);
4657 uri = src->conninfo.location;
4658 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
4660 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4661 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4665 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
4666 event = gst_event_new_stream_start (stream_id);
4667 gst_event_set_group_id (event, group_id);
4670 gst_rtspsrc_stream_push_event (src, ostream, event);
4672 if ((caps = stream_get_caps_for_pt (ostream, ostream->default_pt))) {
4673 /* only streams that have a connection to the outside world */
4674 if (ostream->setup) {
4675 if (ostream->udpsrc[0]) {
4676 gst_element_send_event (ostream->udpsrc[0],
4677 gst_event_new_caps (caps));
4678 } else if (ostream->channelpad[0]) {
4679 if (GST_PAD_IS_SRC (ostream->channelpad[0]))
4680 gst_pad_push_event (ostream->channelpad[0],
4681 gst_event_new_caps (caps));
4683 gst_pad_send_event (ostream->channelpad[0],
4684 gst_event_new_caps (caps));
4686 ostream->need_caps = FALSE;
4688 if (ostream->profile == GST_RTSP_PROFILE_SAVP ||
4689 ostream->profile == GST_RTSP_PROFILE_SAVPF)
4690 caps = gst_caps_new_empty_simple ("application/x-srtcp");
4692 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4694 if (ostream->udpsrc[1]) {
4695 gst_element_send_event (ostream->udpsrc[1],
4696 gst_event_new_caps (caps));
4697 } else if (ostream->channelpad[1]) {
4698 if (GST_PAD_IS_SRC (ostream->channelpad[1]))
4699 gst_pad_push_event (ostream->channelpad[1],
4700 gst_event_new_caps (caps));
4702 gst_pad_send_event (ostream->channelpad[1],
4703 gst_event_new_caps (caps));
4706 gst_caps_unref (caps);
4710 g_checksum_free (cs);
4712 gst_rtspsrc_activate_streams (src);
4713 src->need_activate = FALSE;
4714 src->need_segment = TRUE;
4717 if (src->base_time == -1) {
4718 /* Take current running_time. This timestamp will be put on
4719 * the first buffer of each stream because we are a live source and so we
4720 * timestamp with the running_time. When we are dealing with TCP, we also
4721 * only timestamp the first buffer (using the DISCONT flag) because a server
4722 * typically bursts data, for which we don't want to compensate by speeding
4723 * up the media. The other timestamps will be interpollated from this one
4724 * using the RTP timestamps. */
4725 GST_OBJECT_LOCK (src);
4726 if (GST_ELEMENT_CLOCK (src)) {
4728 GstClockTime base_time;
4730 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
4731 base_time = GST_ELEMENT_CAST (src)->base_time;
4733 src->base_time = now - base_time;
4735 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
4736 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
4738 GST_OBJECT_UNLOCK (src);
4741 /* If needed send a new segment, don't forget we are live and buffer are
4742 * timestamped with running time */
4743 if (src->need_segment) {
4745 src->need_segment = FALSE;
4746 gst_segment_init (&segment, GST_FORMAT_TIME);
4747 gst_rtspsrc_push_event (src, gst_event_new_segment (&segment));
4750 if (stream->need_caps) {
4753 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt))) {
4754 /* only streams that have a connection to the outside world */
4755 if (stream->setup) {
4756 /* Only need to update the TCP caps here, UDP is already handled */
4757 if (stream->channelpad[0]) {
4758 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4759 gst_pad_push_event (stream->channelpad[0],
4760 gst_event_new_caps (caps));
4762 gst_pad_send_event (stream->channelpad[0],
4763 gst_event_new_caps (caps));
4765 stream->need_caps = FALSE;
4769 stream->need_caps = FALSE;
4772 if (stream->discont && !is_rtcp) {
4773 /* mark first RTP buffer as discont */
4774 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4775 stream->discont = FALSE;
4776 /* first buffer gets the timestamp, other buffers are not timestamped and
4777 * their presentation time will be interpollated from the rtp timestamps. */
4778 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
4779 GST_TIME_ARGS (src->base_time));
4781 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
4784 /* chain to the peer pad */
4785 if (GST_PAD_IS_SINK (outpad))
4786 ret = gst_pad_chain (outpad, buf);
4788 ret = gst_pad_push (outpad, buf);
4791 /* combine all stream flows for the data transport */
4792 ret = gst_rtspsrc_combine_flows (src, stream, ret);
4799 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
4800 gst_rtsp_message_unset (message);
4805 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4806 ("Short message received, ignoring."));
4807 gst_rtsp_message_unset (message);
4812 static GstFlowReturn
4813 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
4815 GstRTSPMessage message = { 0 };
4817 GstFlowReturn ret = GST_FLOW_OK;
4818 GTimeVal tv_timeout;
4821 /* get the next timeout interval */
4822 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4824 /* see if the timeout period expired */
4825 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
4826 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
4827 /* send keep-alive, only act on interrupt, a warning will be posted for
4829 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4831 /* get new timeout */
4832 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4835 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
4836 tv_timeout.tv_sec, tv_timeout.tv_usec);
4838 /* protect the connection with the connection lock so that we can see when
4839 * we are finished doing server communication */
4841 gst_rtspsrc_connection_receive (src, &src->conninfo,
4842 &message, src->ptcp_timeout);
4846 GST_DEBUG_OBJECT (src, "we received a server message");
4848 case GST_RTSP_EINTR:
4849 /* we got interrupted this means we need to stop */
4851 case GST_RTSP_ETIMEOUT:
4852 /* no reply, send keep alive */
4853 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4854 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4858 /* go EOS when the server closed the connection */
4864 switch (message.type) {
4865 case GST_RTSP_MESSAGE_REQUEST:
4866 /* server sends us a request message, handle it */
4867 res = gst_rtspsrc_handle_request (src, &src->conninfo, &message);
4868 if (res == GST_RTSP_EEOF)
4871 goto handle_request_failed;
4873 case GST_RTSP_MESSAGE_RESPONSE:
4874 /* we ignore response messages */
4875 GST_DEBUG_OBJECT (src, "ignoring response message");
4876 DEBUG_RTSP (src, &message);
4878 case GST_RTSP_MESSAGE_DATA:
4879 GST_DEBUG_OBJECT (src, "got data message");
4880 ret = gst_rtspsrc_handle_data (src, &message);
4881 if (ret != GST_FLOW_OK)
4882 goto handle_data_failed;
4885 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4890 g_assert_not_reached ();
4895 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4896 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4897 ("The server closed the connection."));
4898 src->conninfo.connected = FALSE;
4899 gst_rtsp_message_unset (&message);
4900 return GST_FLOW_EOS;
4904 gst_rtsp_message_unset (&message);
4905 GST_DEBUG_OBJECT (src, "got interrupted");
4906 return 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));
4916 gst_rtsp_message_unset (&message);
4917 return GST_FLOW_ERROR;
4919 handle_request_failed:
4921 gchar *str = gst_rtsp_strresult (res);
4923 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4924 ("Could not handle server message. (%s)", str));
4926 gst_rtsp_message_unset (&message);
4927 return GST_FLOW_ERROR;
4931 GST_DEBUG_OBJECT (src, "could no handle data message");
4936 static GstFlowReturn
4937 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
4940 GstRTSPMessage message = { 0 };
4944 GTimeVal tv_timeout;
4946 /* get the next timeout interval */
4947 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4949 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
4950 (gint) tv_timeout.tv_sec);
4952 gst_rtsp_message_unset (&message);
4954 /* we should continue reading the TCP socket because the server might
4955 * send us requests. When the session timeout expires, we need to send a
4956 * keep-alive request to keep the session open. */
4957 res = gst_rtspsrc_connection_receive (src, &src->conninfo,
4958 &message, &tv_timeout);
4962 GST_DEBUG_OBJECT (src, "we received a server message");
4964 case GST_RTSP_EINTR:
4965 /* we got interrupted, see what we have to do */
4967 case GST_RTSP_ETIMEOUT:
4968 /* send keep-alive, ignore the result, a warning will be posted. */
4969 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4970 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4974 /* server closed the connection. not very fatal for UDP, reconnect and
4975 * see what happens. */
4976 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4977 ("The server closed the connection."));
4978 if (src->udp_reconnect) {
4980 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
4987 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
4989 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4990 ("Unhandled return value %d.", res));
4994 switch (message.type) {
4995 case GST_RTSP_MESSAGE_REQUEST:
4996 /* server sends us a request message, handle it */
4997 res = gst_rtspsrc_handle_request (src, &src->conninfo, &message);
4998 if (res == GST_RTSP_EEOF)
5001 goto handle_request_failed;
5003 case GST_RTSP_MESSAGE_RESPONSE:
5004 /* we ignore response and data messages */
5005 GST_DEBUG_OBJECT (src, "ignoring response message");
5006 DEBUG_RTSP (src, &message);
5007 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
5008 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
5009 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
5010 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
5011 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5018 case GST_RTSP_MESSAGE_DATA:
5019 /* we ignore response and data messages */
5020 GST_DEBUG_OBJECT (src, "ignoring data message");
5023 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5028 g_assert_not_reached ();
5030 /* we get here when the connection got interrupted */
5033 gst_rtsp_message_unset (&message);
5034 GST_DEBUG_OBJECT (src, "got interrupted");
5035 return GST_FLOW_FLUSHING;
5039 gchar *str = gst_rtsp_strresult (res);
5042 src->conninfo.connected = FALSE;
5043 if (res != GST_RTSP_EINTR) {
5044 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
5045 ("Could not connect to server. (%s)", str));
5047 ret = GST_FLOW_ERROR;
5049 ret = GST_FLOW_FLUSHING;
5055 gchar *str = gst_rtsp_strresult (res);
5057 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5058 ("Could not receive message. (%s)", str));
5060 return GST_FLOW_ERROR;
5062 handle_request_failed:
5064 gchar *str = gst_rtsp_strresult (res);
5067 gst_rtsp_message_unset (&message);
5068 if (res != GST_RTSP_EINTR) {
5069 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5070 ("Could not handle server message. (%s)", str));
5072 ret = GST_FLOW_ERROR;
5074 ret = GST_FLOW_FLUSHING;
5080 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5081 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5082 ("The server closed the connection."));
5083 src->conninfo.connected = FALSE;
5084 gst_rtsp_message_unset (&message);
5085 return GST_FLOW_EOS;
5089 static GstRTSPResult
5090 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
5092 GstRTSPResult res = GST_RTSP_OK;
5095 GST_DEBUG_OBJECT (src, "doing reconnect");
5097 GST_OBJECT_LOCK (src);
5098 /* only restart when the pads were not yet activated, else we were
5099 * streaming over UDP */
5100 restart = src->need_activate;
5101 GST_OBJECT_UNLOCK (src);
5103 /* no need to restart, we're done */
5107 /* we can try only TCP now */
5108 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
5110 /* close and cleanup our state */
5111 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
5114 /* see if we have TCP left to try. Also don't try TCP when we were configured
5116 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
5119 /* We post a warning message now to inform the user
5120 * that nothing happened. It's most likely a firewall thing. */
5121 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5122 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5123 "firewall is blocking it. Retrying using a tcp connection.",
5124 gst_guint64_to_gdouble (src->udp_timeout) / 1000000.0));
5126 /* open new connection using tcp */
5127 if (gst_rtspsrc_open (src, async) < 0)
5130 /* start playback */
5131 if (gst_rtspsrc_play (src, &src->segment, async, NULL) < 0)
5140 src->cur_protocols = 0;
5141 /* no transport possible, post an error and stop */
5142 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5143 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5144 "firewall is blocking it. No other protocols to try.",
5145 gst_guint64_to_gdouble (src->udp_timeout) / 1000000.0));
5146 return GST_RTSP_ERROR;
5150 GST_DEBUG_OBJECT (src, "open failed");
5155 GST_DEBUG_OBJECT (src, "play failed");
5161 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
5165 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
5168 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
5171 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
5174 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
5182 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
5186 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
5189 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
5192 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
5195 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
5203 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
5207 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
5210 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
5213 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
5216 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
5224 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
5228 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
5231 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
5234 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
5237 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
5245 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
5247 if (ret == GST_RTSP_OK)
5248 gst_rtspsrc_loop_complete_cmd (src, cmd);
5249 else if (ret == GST_RTSP_EINTR)
5250 gst_rtspsrc_loop_cancel_cmd (src, cmd);
5252 gst_rtspsrc_loop_error_cmd (src, cmd);
5256 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
5259 gboolean flushed = FALSE;
5261 /* start new request */
5262 gst_rtspsrc_loop_start_cmd (src, cmd);
5264 GST_DEBUG_OBJECT (src, "sending cmd %s", cmd_to_string (cmd));
5266 GST_OBJECT_LOCK (src);
5267 old = src->pending_cmd;
5268 if (old == CMD_RECONNECT) {
5269 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
5270 cmd = CMD_RECONNECT;
5271 } else if (old == CMD_CLOSE) {
5272 /* our CMD_CLOSE might have interrutped CMD_LOOP. gst_rtspsrc_loop
5273 * will send a CMD_WAIT which would cancel our pending CMD_CLOSE (if
5274 * still pending). We just avoid it here by making sure CMD_CLOSE is
5275 * still the pending command. */
5276 GST_DEBUG_OBJECT (src, "ignore, we were closing");
5278 } else if (old != CMD_WAIT) {
5279 src->pending_cmd = CMD_WAIT;
5280 GST_OBJECT_UNLOCK (src);
5281 /* cancel previous request */
5282 GST_DEBUG_OBJECT (src, "cancel previous request %s", cmd_to_string (old));
5283 gst_rtspsrc_loop_cancel_cmd (src, old);
5284 GST_OBJECT_LOCK (src);
5286 src->pending_cmd = cmd;
5287 /* interrupt if allowed */
5288 if (src->busy_cmd & mask) {
5289 GST_DEBUG_OBJECT (src, "connection flush busy %s",
5290 cmd_to_string (src->busy_cmd));
5291 gst_rtspsrc_connection_flush (src, TRUE);
5294 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %s",
5295 cmd_to_string (src->busy_cmd));
5298 gst_task_start (src->task);
5299 GST_OBJECT_UNLOCK (src);
5305 gst_rtspsrc_loop (GstRTSPSrc * src)
5309 if (!src->conninfo.connection || !src->conninfo.connected)
5312 if (src->interleaved)
5313 ret = gst_rtspsrc_loop_interleaved (src);
5315 ret = gst_rtspsrc_loop_udp (src);
5317 if (ret != GST_FLOW_OK)
5325 GST_WARNING_OBJECT (src, "we are not connected");
5326 ret = GST_FLOW_FLUSHING;
5331 const gchar *reason = gst_flow_get_name (ret);
5333 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
5334 src->running = FALSE;
5335 if (ret == GST_FLOW_EOS) {
5336 /* perform EOS logic */
5337 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5338 gst_element_post_message (GST_ELEMENT_CAST (src),
5339 gst_message_new_segment_done (GST_OBJECT_CAST (src),
5340 src->segment.format, src->segment.position));
5341 gst_rtspsrc_push_event (src,
5342 gst_event_new_segment_done (src->segment.format,
5343 src->segment.position));
5345 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5347 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
5348 /* for fatal errors we post an error message, post the error before the
5349 * EOS so the app knows about the error first. */
5350 GST_ELEMENT_FLOW_ERROR (src, ret);
5351 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5353 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
5358 #ifndef GST_DISABLE_GST_DEBUG
5359 static const gchar *
5360 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
5364 while (method != 0) {
5381 /* Parse a WWW-Authenticate Response header and determine the
5382 * available authentication methods
5384 * This code should also cope with the fact that each WWW-Authenticate
5385 * header can contain multiple challenge methods + tokens
5387 * At the moment, for Basic auth, we just do a minimal check and don't
5388 * even parse out the realm */
5390 gst_rtspsrc_parse_auth_hdr (GstRTSPMessage * response,
5391 GstRTSPAuthMethod * methods, GstRTSPConnection * conn, gboolean * stale)
5393 GstRTSPAuthCredential **credentials, **credential;
5395 g_return_if_fail (response != NULL);
5396 g_return_if_fail (methods != NULL);
5397 g_return_if_fail (stale != NULL);
5400 gst_rtsp_message_parse_auth_credentials (response,
5401 GST_RTSP_HDR_WWW_AUTHENTICATE);
5405 credential = credentials;
5406 while (*credential) {
5407 if ((*credential)->scheme == GST_RTSP_AUTH_BASIC) {
5408 *methods |= GST_RTSP_AUTH_BASIC;
5409 } else if ((*credential)->scheme == GST_RTSP_AUTH_DIGEST) {
5410 GstRTSPAuthParam **param = (*credential)->params;
5412 *methods |= GST_RTSP_AUTH_DIGEST;
5414 gst_rtsp_connection_clear_auth_params (conn);
5418 if (strcmp ((*param)->name, "stale") == 0
5419 && g_ascii_strcasecmp ((*param)->value, "TRUE") == 0)
5421 gst_rtsp_connection_set_auth_param (conn, (*param)->name,
5430 gst_rtsp_auth_credentials_free (credentials);
5434 * gst_rtspsrc_setup_auth:
5435 * @src: the rtsp source
5437 * Configure a username and password and auth method on the
5438 * connection object based on a response we received from the
5441 * Currently, this requires that a username and password were supplied
5442 * in the uri. In the future, they may be requested on demand by sending
5443 * a message up the bus.
5445 * Returns: TRUE if authentication information could be set up correctly.
5448 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
5452 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
5453 GstRTSPAuthMethod method;
5454 GstRTSPResult auth_result;
5456 GstRTSPConnection *conn;
5457 gboolean stale = FALSE;
5459 conn = src->conninfo.connection;
5461 /* Identify the available auth methods and see if any are supported */
5462 gst_rtspsrc_parse_auth_hdr (response, &avail_methods, conn, &stale);
5464 if (avail_methods == GST_RTSP_AUTH_NONE)
5465 goto no_auth_available;
5467 /* For digest auth, if the response indicates that the session
5468 * data are stale, we just update them in the connection object and
5469 * return TRUE to retry the request */
5471 src->tried_url_auth = FALSE;
5473 url = gst_rtsp_connection_get_url (conn);
5475 /* Do we have username and password available? */
5476 if (url != NULL && !src->tried_url_auth && url->user != NULL
5477 && url->passwd != NULL) {
5480 src->tried_url_auth = TRUE;
5481 GST_DEBUG_OBJECT (src,
5482 "Attempting authentication using credentials from the URL");
5484 user = src->user_id;
5485 pass = src->user_pw;
5486 GST_DEBUG_OBJECT (src,
5487 "Attempting authentication using credentials from the properties");
5490 /* FIXME: If the url didn't contain username and password or we tried them
5491 * already, request a username and passwd from the application via some kind
5492 * of credentials request message */
5494 /* If we don't have a username and passwd at this point, bail out. */
5495 if (user == NULL || pass == NULL)
5498 /* Try to configure for each available authentication method, strongest to
5500 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
5501 /* Check if this method is available on the server */
5502 if ((method & avail_methods) == 0)
5505 /* Pass the credentials to the connection to try on the next request */
5506 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
5507 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
5508 * ignore it and end up retrying later */
5509 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
5510 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
5511 gst_rtsp_auth_method_to_string (method));
5516 if (method == GST_RTSP_AUTH_NONE)
5517 goto no_auth_available;
5523 /* Output an error indicating that we couldn't connect because there were
5524 * no supported authentication protocols */
5525 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5526 ("No supported authentication protocol was found"));
5531 /* We don't fire an error message, we just return FALSE and let the
5532 * normal NOT_AUTHORIZED error be propagated */
5537 static GstRTSPResult
5538 gst_rtsp_src_receive_response (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
5539 GstRTSPMessage * response, GstRTSPStatusCode * code)
5541 GstRTSPStatusCode thecode;
5542 gchar *content_base = NULL;
5543 GstRTSPResult res = gst_rtspsrc_connection_receive (src, conninfo,
5544 response, src->ptcp_timeout);
5549 DEBUG_RTSP (src, response);
5551 switch (response->type) {
5552 case GST_RTSP_MESSAGE_REQUEST:
5553 res = gst_rtspsrc_handle_request (src, conninfo, response);
5554 if (res == GST_RTSP_EEOF)
5557 goto handle_request_failed;
5559 /* Not a response, receive next message */
5560 return gst_rtsp_src_receive_response (src, conninfo, response, code);
5561 case GST_RTSP_MESSAGE_RESPONSE:
5562 /* ok, a response is good */
5563 GST_DEBUG_OBJECT (src, "received response message");
5565 case GST_RTSP_MESSAGE_DATA:
5566 /* get next response */
5567 GST_DEBUG_OBJECT (src, "handle data response message");
5568 gst_rtspsrc_handle_data (src, response);
5570 /* Not a response, receive next message */
5571 return gst_rtsp_src_receive_response (src, conninfo, response, code);
5573 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5576 /* Not a response, receive next message */
5577 return gst_rtsp_src_receive_response (src, conninfo, response, code);
5580 thecode = response->type_data.response.code;
5582 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
5584 /* if the caller wanted the result code, we store it. */
5588 /* If the request didn't succeed, bail out before doing any more */
5589 if (thecode != GST_RTSP_STS_OK)
5592 /* store new content base if any */
5593 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
5596 g_free (src->content_base);
5597 src->content_base = g_strdup (content_base);
5607 return GST_RTSP_EEOF;
5610 gchar *str = gst_rtsp_strresult (res);
5612 if (res != GST_RTSP_EINTR) {
5613 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5614 ("Could not receive message. (%s)", str));
5616 GST_WARNING_OBJECT (src, "receive interrupted");
5624 handle_request_failed:
5626 /* ERROR was posted */
5627 gst_rtsp_message_unset (response);
5632 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5633 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5634 ("The server closed the connection."));
5635 gst_rtsp_message_unset (response);
5641 static GstRTSPResult
5642 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
5643 GstRTSPMessage * request, GstRTSPMessage * response,
5644 GstRTSPStatusCode * code)
5650 if (!src->short_header)
5651 gst_rtsp_ext_list_before_send (src->extensions, request);
5653 GST_DEBUG_OBJECT (src, "sending message");
5655 DEBUG_RTSP (src, request);
5657 res = gst_rtspsrc_connection_send (src, conninfo, request, src->ptcp_timeout);
5661 gst_rtsp_connection_reset_timeout (conninfo->connection);
5665 res = gst_rtsp_src_receive_response (src, conninfo, response, code);
5666 if (res == GST_RTSP_EEOF) {
5667 GST_WARNING_OBJECT (src, "server closed connection");
5668 /* only try once after reconnect, then fallthrough and error out */
5669 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
5671 /* if reconnect succeeds, try again */
5672 if ((res = gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) == 0)
5676 gst_rtsp_ext_list_after_send (src->extensions, request, response);
5682 gchar *str = gst_rtsp_strresult (res);
5684 if (res != GST_RTSP_EINTR) {
5685 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5686 ("Could not send message. (%s)", str));
5688 GST_WARNING_OBJECT (src, "send interrupted");
5697 * @src: the rtsp source
5698 * @conninfo: the connection information to send on
5699 * @request: must point to a valid request
5700 * @response: must point to an empty #GstRTSPMessage
5701 * @code: an optional code result
5702 * @versions: List of versions to try, setting it back onto the @request message
5703 * if not set, `src->version` will be used as RTSP version.
5705 * send @request and retrieve the response in @response. optionally @code can be
5706 * non-NULL in which case it will contain the status code of the response.
5708 * If This function returns #GST_RTSP_OK, @response will contain a valid response
5709 * message that should be cleaned with gst_rtsp_message_unset() after usage.
5711 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
5712 * @response message) if the response code was not 200 (OK).
5714 * If the attempt results in an authentication failure, then this will attempt
5715 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
5718 * Returns: #GST_RTSP_OK if the processing was successful.
5720 static GstRTSPResult
5721 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
5722 GstRTSPMessage * request, GstRTSPMessage * response,
5723 GstRTSPStatusCode * code, GstRTSPVersion * versions)
5725 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
5726 GstRTSPResult res = GST_RTSP_ERROR;
5729 GstRTSPMethod method = GST_RTSP_INVALID;
5730 gint version_retry = 0;
5736 /* make sure we don't loop forever */
5740 /* save method so we can disable it when the server complains */
5741 method = request->type_data.request.method;
5744 request->type_data.request.version = src->version;
5747 gst_rtspsrc_try_send (src, conninfo, request, response,
5752 case GST_RTSP_STS_UNAUTHORIZED:
5753 case GST_RTSP_STS_NOT_FOUND:
5754 if (gst_rtspsrc_setup_auth (src, response)) {
5755 /* Try the request/response again after configuring the auth info
5760 case GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED:
5761 GST_INFO_OBJECT (src, "Version %s not supported by the server",
5762 versions ? gst_rtsp_version_as_text (versions[version_retry]) :
5764 if (versions && versions[version_retry] != GST_RTSP_VERSION_INVALID) {
5765 GST_INFO_OBJECT (src, "Unsupported version %s => trying %s",
5766 gst_rtsp_version_as_text (request->type_data.request.version),
5767 gst_rtsp_version_as_text (versions[version_retry]));
5768 request->type_data.request.version = versions[version_retry];
5777 } while (retry == TRUE);
5779 /* If the user requested the code, let them handle errors, otherwise
5780 * post an error below */
5783 else if (int_code != GST_RTSP_STS_OK)
5784 goto error_response;
5791 GST_DEBUG_OBJECT (src, "got error %d", res);
5796 res = GST_RTSP_ERROR;
5798 switch (response->type_data.response.code) {
5799 case GST_RTSP_STS_NOT_FOUND:
5800 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, NOT_FOUND,
5803 case GST_RTSP_STS_UNAUTHORIZED:
5804 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, NOT_AUTHORIZED,
5807 case GST_RTSP_STS_MOVED_PERMANENTLY:
5808 case GST_RTSP_STS_MOVE_TEMPORARILY:
5810 gchar *new_location;
5811 GstRTSPLowerTrans transports;
5813 GST_DEBUG_OBJECT (src, "got redirection");
5814 /* if we don't have a Location Header, we must error */
5815 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
5816 &new_location, 0) < 0)
5819 /* When we receive a redirect result, we go back to the INIT state after
5820 * parsing the new URI. The caller should do the needed steps to issue
5821 * a new setup when it detects this state change. */
5822 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
5824 /* save current transports */
5825 if (src->conninfo.url)
5826 transports = src->conninfo.url->transports;
5828 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
5830 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
5832 /* set old transports */
5833 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
5834 src->conninfo.url->transports = transports;
5836 src->need_redirect = TRUE;
5840 case GST_RTSP_STS_NOT_ACCEPTABLE:
5841 case GST_RTSP_STS_NOT_IMPLEMENTED:
5842 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
5843 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
5844 gst_rtsp_method_as_text (method));
5845 src->methods &= ~method;
5849 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, READ,
5853 /* if we return ERROR we should unset the response ourselves */
5854 if (res == GST_RTSP_ERROR)
5855 gst_rtsp_message_unset (response);
5861 static GstRTSPResult
5862 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
5863 GstRTSPMessage * response, GstRTSPSrc * src)
5865 return gst_rtspsrc_send (src, &src->conninfo, request, response, NULL, NULL);
5869 /* parse the response and collect all the supported methods. We need this
5870 * information so that we don't try to send an unsupported request to the
5874 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
5876 GstRTSPHeaderField field;
5880 /* reset supported methods */
5883 /* Try Allow Header first */
5884 field = GST_RTSP_HDR_ALLOW;
5887 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5891 src->methods |= gst_rtsp_options_from_text (respoptions);
5897 field = GST_RTSP_HDR_PUBLIC;
5900 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5904 src->methods |= gst_rtsp_options_from_text (respoptions);
5909 if (src->methods == 0) {
5910 /* neither Allow nor Public are required, assume the server supports
5911 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
5913 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
5914 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
5916 /* always assume PLAY, FIXME, extensions should be able to override
5918 src->methods |= GST_RTSP_PLAY;
5919 /* also assume it will support Range */
5920 src->seekable = G_MAXDOUBLE;
5922 /* we need describe and setup */
5923 if (!(src->methods & GST_RTSP_DESCRIBE))
5925 if (!(src->methods & GST_RTSP_SETUP))
5933 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5934 ("Server does not support DESCRIBE."));
5939 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5940 ("Server does not support SETUP."));
5945 /* masks to be kept in sync with the hardcoded protocol order of preference
5947 static const guint protocol_masks[] = {
5948 GST_RTSP_LOWER_TRANS_UDP,
5949 GST_RTSP_LOWER_TRANS_UDP_MCAST,
5950 GST_RTSP_LOWER_TRANS_TCP,
5954 static GstRTSPResult
5955 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
5956 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
5960 gboolean add_udp_str;
5965 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
5970 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
5972 /* extension listed transports, use those */
5973 if (*transports != NULL)
5976 /* it's the default */
5977 add_udp_str = FALSE;
5979 /* the default RTSP transports */
5980 result = g_string_new ("RTP");
5983 case GST_RTSP_PROFILE_AVP:
5984 g_string_append (result, "/AVP");
5986 case GST_RTSP_PROFILE_SAVP:
5987 g_string_append (result, "/SAVP");
5989 case GST_RTSP_PROFILE_AVPF:
5990 g_string_append (result, "/AVPF");
5992 case GST_RTSP_PROFILE_SAVPF:
5993 g_string_append (result, "/SAVPF");
5999 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
6000 GST_DEBUG_OBJECT (src, "adding UDP unicast");
6002 g_string_append (result, "/UDP");
6003 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
6004 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
6005 GST_DEBUG_OBJECT (src, "adding UDP multicast");
6006 /* we don't have to allocate any UDP ports yet, if the selected transport
6007 * turns out to be multicast we can create them and join the multicast
6008 * group indicated in the transport reply */
6010 g_string_append (result, "/UDP");
6011 g_string_append (result, ";multicast");
6012 if (src->next_port_num != 0) {
6013 if (src->client_port_range.max > 0 &&
6014 src->next_port_num >= src->client_port_range.max)
6017 g_string_append_printf (result, ";client_port=%d-%d",
6018 src->next_port_num, src->next_port_num + 1);
6020 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
6021 GST_DEBUG_OBJECT (src, "adding TCP");
6023 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
6025 *transports = g_string_free (result, FALSE);
6027 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
6034 GST_ERROR ("extension gave error %d", res);
6039 GST_ERROR ("no more ports available");
6040 return GST_RTSP_ERROR;
6044 static GstRTSPResult
6045 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
6046 gint orig_rtpport, gint orig_rtcpport)
6049 gint nr_udp, nr_int;
6051 gint rtpport = 0, rtcpport = 0;
6054 src = stream->parent;
6056 /* find number of placeholders first */
6057 if (strstr (*transports, "%%i2"))
6059 else if (strstr (*transports, "%%i1"))
6064 if (strstr (*transports, "%%u2"))
6066 else if (strstr (*transports, "%%u1"))
6071 if (nr_udp == 0 && nr_int == 0)
6075 if (!orig_rtpport || !orig_rtcpport) {
6076 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
6079 rtpport = orig_rtpport;
6080 rtcpport = orig_rtcpport;
6084 str = g_string_new ("");
6086 while ((next = strstr (p, "%%"))) {
6087 g_string_append_len (str, p, next - p);
6088 if (next[2] == 'u') {
6090 g_string_append_printf (str, "%d", rtpport);
6091 else if (next[3] == '2')
6092 g_string_append_printf (str, "%d", rtcpport);
6094 if (next[2] == 'i') {
6096 g_string_append_printf (str, "%d", src->free_channel);
6097 else if (next[3] == '2')
6098 g_string_append_printf (str, "%d", src->free_channel + 1);
6104 if (src->version >= GST_RTSP_VERSION_2_0)
6105 src->free_channel += 2;
6107 /* append final part */
6108 g_string_append (str, p);
6110 g_free (*transports);
6111 *transports = g_string_free (str, FALSE);
6119 GST_ERROR ("failed to allocate udp ports");
6120 return GST_RTSP_ERROR;
6125 signal_get_srtcp_params (GstRTSPSrc * src, GstRTSPStream * stream)
6127 GstCaps *caps = NULL;
6129 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY], 0,
6133 GST_DEBUG_OBJECT (src, "SRTP parameters received");
6139 default_srtcp_params (void)
6146 guint data_size = GST_ROUND_UP_4 (KEY_SIZE);
6148 /* create a random key */
6149 key_data = g_malloc (data_size);
6150 for (i = 0; i < data_size; i += 4)
6151 GST_WRITE_UINT32_BE (key_data + i, g_random_int ());
6153 buf = gst_buffer_new_wrapped (key_data, KEY_SIZE);
6155 caps = gst_caps_new_simple ("application/x-srtcp",
6156 "srtp-key", GST_TYPE_BUFFER, buf,
6157 "srtp-cipher", G_TYPE_STRING, "aes-128-icm",
6158 "srtp-auth", G_TYPE_STRING, "hmac-sha1-80",
6159 "srtcp-cipher", G_TYPE_STRING, "aes-128-icm",
6160 "srtcp-auth", G_TYPE_STRING, "hmac-sha1-80", NULL);
6162 gst_buffer_unref (buf);
6168 gst_rtspsrc_stream_make_keymgmt (GstRTSPSrc * src, GstRTSPStream * stream)
6170 gchar *base64, *result = NULL;
6171 GstMIKEYMessage *mikey_msg;
6173 stream->srtcpparams = signal_get_srtcp_params (src, stream);
6174 if (stream->srtcpparams == NULL)
6175 stream->srtcpparams = default_srtcp_params ();
6177 mikey_msg = gst_mikey_message_new_from_caps (stream->srtcpparams);
6179 /* add policy '0' for our SSRC */
6180 gst_mikey_message_add_cs_srtp (mikey_msg, 0, stream->send_ssrc, 0);
6182 base64 = gst_mikey_message_base64_encode (mikey_msg);
6183 gst_mikey_message_unref (mikey_msg);
6186 result = gst_sdp_make_keymgmt (stream->conninfo.location, base64);
6194 static GstRTSPResult
6195 gst_rtsp_src_setup_stream_from_response (GstRTSPSrc * src,
6196 GstRTSPStream * stream, GstRTSPMessage * response,
6197 GstRTSPLowerTrans * protocols, gint retry, gint * rtpport, gint * rtcpport)
6199 gchar *resptrans = NULL;
6200 GstRTSPTransport transport = { 0 };
6202 gst_rtsp_message_get_header (response, GST_RTSP_HDR_TRANSPORT, &resptrans, 0);
6204 gst_rtspsrc_stream_free_udp (stream);
6208 /* parse transport, go to next stream on parse error */
6209 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
6210 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
6211 return GST_RTSP_ELAST;
6214 /* update allowed transports for other streams. once the transport of
6215 * one stream has been determined, we make sure that all other streams
6216 * are configured in the same way */
6217 switch (transport.lower_transport) {
6218 case GST_RTSP_LOWER_TRANS_TCP:
6219 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
6221 *protocols = GST_RTSP_LOWER_TRANS_TCP;
6222 src->interleaved = TRUE;
6223 if (src->version < GST_RTSP_VERSION_2_0) {
6224 /* update free channels */
6225 src->free_channel = MAX (transport.interleaved.min, src->free_channel);
6226 src->free_channel = MAX (transport.interleaved.max, src->free_channel);
6227 src->free_channel++;
6230 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
6231 /* only allow multicast for other streams */
6232 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
6234 *protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
6235 /* if the server selected our ports, increment our counters so that
6236 * we select a new port later */
6237 if (src->next_port_num == transport.port.min &&
6238 src->next_port_num + 1 == transport.port.max) {
6239 src->next_port_num += 2;
6242 case GST_RTSP_LOWER_TRANS_UDP:
6243 /* only allow unicast for other streams */
6244 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
6246 *protocols = GST_RTSP_LOWER_TRANS_UDP;
6249 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
6250 transport.lower_transport);
6254 if (!src->interleaved || !retry) {
6255 /* now configure the stream with the selected transport */
6256 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
6257 GST_DEBUG_OBJECT (src,
6258 "could not configure stream %p transport, skipping stream", stream);
6260 } else if (stream->udpsrc[0] && stream->udpsrc[1] && rtpport && rtcpport) {
6261 /* retain the first allocated UDP port pair */
6262 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", rtpport, NULL);
6263 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", rtcpport, NULL);
6266 /* we need to activate at least one stream when we detect activity */
6267 src->need_activate = TRUE;
6269 /* stream is setup now */
6270 stream->setup = TRUE;
6271 stream->waiting_setup_response = FALSE;
6273 if (src->version >= GST_RTSP_VERSION_2_0) {
6274 gchar *prop, *media_properties;
6278 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_MEDIA_PROPERTIES,
6279 &media_properties, 0) != GST_RTSP_OK) {
6280 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6281 ("Error: No MEDIA_PROPERTY header in a SETUP request in RTSP 2.0"
6282 " - this header is mandatory."));
6284 gst_rtsp_message_unset (response);
6285 return GST_RTSP_ERROR;
6288 props = g_strsplit (media_properties, ",", -2);
6289 for (i = 0; props[i]; i++) {
6292 while (*prop == ' ')
6295 if (strstr (prop, "Random-Access")) {
6296 gchar **random_seekable_val = g_strsplit (prop, "=", 2);
6298 if (!random_seekable_val[1])
6299 src->seekable = G_MAXDOUBLE;
6301 src->seekable = g_ascii_strtod (random_seekable_val[1], NULL);
6303 g_strfreev (random_seekable_val);
6304 } else if (!g_strcmp0 (prop, "No-Seeking")) {
6305 src->seekable = -1.0;
6306 } else if (!g_strcmp0 (prop, "Beginning-Only")) {
6307 src->seekable = 0.0;
6315 /* clean up our transport struct */
6316 gst_rtsp_transport_init (&transport);
6317 /* clean up used RTSP messages */
6318 gst_rtsp_message_unset (response);
6324 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6325 ("Server did not select transport."));
6327 gst_rtsp_message_unset (response);
6328 return GST_RTSP_ERROR;
6332 static GstRTSPResult
6333 gst_rtspsrc_setup_streams_end (GstRTSPSrc * src, gboolean async)
6336 GstRTSPConnInfo *conninfo;
6338 g_assert (src->version >= GST_RTSP_VERSION_2_0);
6340 conninfo = &src->conninfo;
6341 for (tmp = src->streams; tmp; tmp = tmp->next) {
6342 GstRTSPStream *stream = (GstRTSPStream *) tmp->data;
6343 GstRTSPMessage response = { 0, };
6345 if (!stream->waiting_setup_response)
6348 if (!src->conninfo.connection)
6349 conninfo = &((GstRTSPStream *) tmp->data)->conninfo;
6351 gst_rtsp_src_receive_response (src, conninfo, &response, NULL);
6353 gst_rtsp_src_setup_stream_from_response (src, stream,
6354 &response, NULL, 0, NULL, NULL);
6360 /* Perform the SETUP request for all the streams.
6362 * We ask the server for a specific transport, which initially includes all the
6363 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
6364 * two local UDP ports that we send to the server.
6366 * Once the server replied with a transport, we configure the other streams
6367 * with the same transport.
6369 * In case setup request are not pipelined, this function will also configure the
6370 * stream for the selected transport, * which basically means creating the pipeline.
6371 * Otherwise, the first stream is setup right away from the reply and a
6372 * CMD_FINALIZE_SETUP command is set for the stream pipelines to happen on the
6373 * remaining streams from the RTSP thread.
6375 static GstRTSPResult
6376 gst_rtspsrc_setup_streams_start (GstRTSPSrc * src, gboolean async)
6379 GstRTSPResult res = GST_RTSP_ERROR;
6380 GstRTSPMessage request = { 0 };
6381 GstRTSPMessage response = { 0 };
6382 GstRTSPStream *stream = NULL;
6383 GstRTSPLowerTrans protocols;
6384 GstRTSPStatusCode code;
6385 gboolean unsupported_real = FALSE;
6386 gint rtpport, rtcpport;
6389 gchar *pipelined_request_id = NULL;
6391 if (src->conninfo.connection) {
6392 url = gst_rtsp_connection_get_url (src->conninfo.connection);
6393 /* we initially allow all configured lower transports. based on the URL
6394 * transports and the replies from the server we narrow them down. */
6395 protocols = url->transports & src->cur_protocols;
6398 protocols = src->cur_protocols;
6404 /* reset some state */
6405 src->free_channel = 0;
6406 src->interleaved = FALSE;
6407 src->need_activate = FALSE;
6408 /* keep track of next port number, 0 is random */
6409 src->next_port_num = src->client_port_range.min;
6410 rtpport = rtcpport = 0;
6412 if (G_UNLIKELY (src->streams == NULL))
6415 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6416 GstRTSPConnInfo *conninfo;
6423 stream = (GstRTSPStream *) walk->data;
6425 caps = stream_get_caps_for_pt (stream, stream->default_pt);
6427 GST_DEBUG_OBJECT (src, "skipping stream %p, no caps", stream);
6431 if (stream->skipped) {
6432 GST_DEBUG_OBJECT (src, "skipping stream %p", stream);
6436 /* see if we need to configure this stream */
6437 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
6438 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
6443 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
6444 stream->id, caps, &selected);
6446 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
6450 /* merge/overwrite global caps */
6455 s = gst_caps_get_structure (caps, 0);
6457 num = gst_structure_n_fields (src->props);
6458 for (j = 0; j < num; j++) {
6462 name = gst_structure_nth_field_name (src->props, j);
6463 val = gst_structure_get_value (src->props, name);
6464 gst_structure_set_value (s, name, val);
6466 GST_DEBUG_OBJECT (src, "copied %s", name);
6470 /* skip setup if we have no URL for it */
6471 if (stream->conninfo.location == NULL) {
6472 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
6476 if (src->conninfo.connection == NULL) {
6477 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
6478 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
6481 conninfo = &stream->conninfo;
6483 conninfo = &src->conninfo;
6485 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
6486 stream->conninfo.location);
6488 /* if we have a multicast connection, only suggest multicast from now on */
6489 if (stream->is_multicast)
6490 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
6493 /* first selectable protocol */
6494 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6496 if (!protocol_masks[mask])
6500 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
6501 protocol_masks[mask]);
6502 /* create a string with first transport in line */
6504 res = gst_rtspsrc_create_transports_string (src,
6505 protocols & protocol_masks[mask], stream->profile, &transports);
6506 if (res < 0 || transports == NULL)
6507 goto setup_transport_failed;
6509 if (strlen (transports) == 0) {
6510 g_free (transports);
6511 GST_DEBUG_OBJECT (src, "no transports found");
6516 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
6518 /* replace placeholders with real values, this function will optionally
6519 * allocate UDP ports and other info needed to execute the setup request */
6520 res = gst_rtspsrc_prepare_transports (stream, &transports,
6521 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
6523 g_free (transports);
6524 goto setup_transport_failed;
6527 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
6528 /* create SETUP request */
6530 gst_rtspsrc_init_request (src, &request, GST_RTSP_SETUP,
6531 stream->conninfo.location);
6533 g_free (transports);
6534 goto create_request_failed;
6537 if (src->version >= GST_RTSP_VERSION_2_0) {
6538 if (!pipelined_request_id)
6539 pipelined_request_id = g_strdup_printf ("%d",
6540 g_random_int_range (0, G_MAXINT32));
6542 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_PIPELINED_REQUESTS,
6543 pipelined_request_id);
6544 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT_RANGES,
6545 "npt, clock, smpte, clock");
6548 /* select transport */
6549 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
6552 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
6553 stream->profile == GST_RTSP_PROFILE_SAVPF) {
6554 hval = gst_rtspsrc_stream_make_keymgmt (src, stream);
6555 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
6558 /* if the user wants a non default RTP packet size we add the blocksize
6560 if (src->rtp_blocksize > 0) {
6561 hval = g_strdup_printf ("%d", src->rtp_blocksize);
6562 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
6566 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
6569 /* handle the code ourselves */
6571 gst_rtspsrc_send (src, conninfo, &request,
6572 pipelined_request_id ? NULL : &response, &code, NULL);
6577 case GST_RTSP_STS_OK:
6579 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
6580 gst_rtsp_message_unset (&request);
6581 gst_rtsp_message_unset (&response);
6582 /* cleanup of leftover transport */
6583 gst_rtspsrc_stream_free_udp (stream);
6584 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
6585 * we might be in this case */
6586 if (stream->container && rtpport && rtcpport && !retry) {
6587 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
6592 /* this transport did not go down well, but we may have others to try
6593 * that we did not send yet, try those and only give up then
6594 * but not without checking for lost cause/extension so we can
6595 * post a nicer/more useful error message later */
6596 if (!unsupported_real)
6597 unsupported_real = stream->is_real;
6598 /* select next available protocol, give up on this stream if none */
6600 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6602 if (!protocol_masks[mask] || unsupported_real)
6607 /* cleanup of leftover transport and move to the next stream */
6608 gst_rtspsrc_stream_free_udp (stream);
6609 goto response_error;
6613 if (!pipelined_request_id) {
6614 /* parse response transport */
6615 res = gst_rtsp_src_setup_stream_from_response (src, stream,
6616 &response, &protocols, retry, &rtpport, &rtcpport);
6618 case GST_RTSP_ERROR:
6620 case GST_RTSP_ELAST:
6626 stream->waiting_setup_response = TRUE;
6627 /* we need to activate at least one stream when we detect activity */
6628 src->need_activate = TRUE;
6635 GstRTSPStream *sskip;
6637 skip = g_list_next (skip);
6641 sskip = (GstRTSPStream *) skip->data;
6643 /* skip all streams with the same control url */
6644 if (g_str_equal (stream->conninfo.location, sskip->conninfo.location)) {
6645 GST_DEBUG_OBJECT (src, "found stream %p with same control %s",
6646 sskip, sskip->conninfo.location);
6647 sskip->skipped = TRUE;
6651 gst_rtsp_message_unset (&request);
6654 if (pipelined_request_id) {
6655 gst_rtspsrc_setup_streams_end (src, TRUE);
6658 /* store the transport protocol that was configured */
6659 src->cur_protocols = protocols;
6661 gst_rtsp_ext_list_stream_select (src->extensions, url);
6663 /* if there is nothing to activate, error out */
6664 if (!src->need_activate)
6665 goto nothing_to_activate;
6672 /* no transport possible, post an error and stop */
6673 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6674 ("Could not connect to server, no protocols left"));
6675 return GST_RTSP_ERROR;
6679 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6680 ("SDP contains no streams"));
6681 return GST_RTSP_ERROR;
6683 create_request_failed:
6685 gchar *str = gst_rtsp_strresult (res);
6687 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6688 ("Could not create request. (%s)", str));
6692 setup_transport_failed:
6694 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6695 ("Could not setup transport."));
6696 res = GST_RTSP_ERROR;
6701 const gchar *str = gst_rtsp_status_as_text (code);
6703 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6704 ("Error (%d): %s", code, GST_STR_NULL (str)));
6705 res = GST_RTSP_ERROR;
6710 gchar *str = gst_rtsp_strresult (res);
6712 if (res != GST_RTSP_EINTR) {
6713 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6714 ("Could not send message. (%s)", str));
6716 GST_WARNING_OBJECT (src, "send interrupted");
6721 nothing_to_activate:
6723 /* none of the available error codes is really right .. */
6724 if (unsupported_real) {
6725 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6726 (_("No supported stream was found. You might need to install a "
6727 "GStreamer RTSP extension plugin for Real media streams.")),
6730 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6731 (_("No supported stream was found. You might need to allow "
6732 "more transport protocols or may otherwise be missing "
6733 "the right GStreamer RTSP extension plugin.")), (NULL));
6735 return GST_RTSP_ERROR;
6739 gst_rtsp_message_unset (&request);
6740 gst_rtsp_message_unset (&response);
6746 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
6747 GstSegment * segment)
6750 GstRTSPTimeRange *therange;
6753 gst_rtsp_range_free (src->range);
6755 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
6756 GST_DEBUG_OBJECT (src, "parsed range %s", range);
6757 src->range = therange;
6759 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
6761 gst_segment_init (segment, GST_FORMAT_TIME);
6765 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
6766 therange->min.type, therange->min.seconds, therange->max.type,
6767 therange->max.seconds);
6769 if (therange->min.type == GST_RTSP_TIME_NOW)
6771 else if (therange->min.type == GST_RTSP_TIME_END)
6774 seconds = therange->min.seconds * GST_SECOND;
6776 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
6777 GST_TIME_ARGS (seconds));
6779 /* we need to start playback without clipping from the position reported by
6781 segment->start = seconds;
6782 segment->position = seconds;
6784 if (therange->max.type == GST_RTSP_TIME_NOW)
6786 else if (therange->max.type == GST_RTSP_TIME_END)
6789 seconds = therange->max.seconds * GST_SECOND;
6791 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
6792 GST_TIME_ARGS (seconds));
6794 /* live (WMS) server might send overflowed large max as its idea of infinity,
6795 * compensate to prevent problems later on */
6796 if (seconds != -1 && seconds < 0) {
6798 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
6801 /* live (WMS) might send min == max, which is not worth recording */
6802 if (segment->duration == -1 && seconds == segment->start)
6805 /* don't change duration with unknown value, we might have a valid value
6806 * there that we want to keep. */
6808 segment->duration = seconds;
6813 /* Parse clock profived by the server with following syntax:
6815 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
6818 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
6820 gboolean res = FALSE;
6822 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
6823 gchar **fields = NULL, **parts = NULL;
6824 gchar *remote_ip, *str;
6826 GstClockTime base_time;
6829 fields = g_strsplit (gstclock, " ", 0);
6831 /* wrapped clock, not very interesting for now */
6832 if (fields[1] == NULL)
6835 /* remote IP address and port */
6836 if ((str = fields[2]) == NULL)
6839 parts = g_strsplit (str, ":", 0);
6841 if ((remote_ip = parts[0]) == NULL)
6844 if ((str = parts[1]) == NULL)
6852 if ((str = fields[3]) == NULL)
6855 base_time = g_ascii_strtoull (str, NULL, 10);
6858 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
6861 if (src->provided_clock)
6862 gst_object_unref (src->provided_clock);
6863 src->provided_clock = netclock;
6865 gst_element_post_message (GST_ELEMENT_CAST (src),
6866 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
6867 src->provided_clock, TRUE));
6871 g_strfreev (fields);
6877 /* must be called with the RTSP state lock */
6878 static GstRTSPResult
6879 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
6885 /* prepare global stream caps properties */
6887 gst_structure_remove_all_fields (src->props);
6889 src->props = gst_structure_new_empty ("RTSPProperties");
6891 DEBUG_SDP (src, sdp);
6893 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
6895 /* let the app inspect and change the SDP */
6896 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
6898 gst_segment_init (&src->segment, GST_FORMAT_TIME);
6900 /* parse range for duration reporting. */
6905 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
6909 /* keep track of the range and configure it in the segment */
6910 if (gst_rtspsrc_parse_range (src, range, &src->segment))
6914 /* parse clock information. This is GStreamer specific, a server can tell the
6915 * client what clock it is using and wrap that in a network clock. The
6916 * advantage of that is that we can slave to it. */
6918 const gchar *gstclock;
6921 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
6922 if (gstclock == NULL)
6925 /* parse the clock and expose it in the provide_clock method */
6926 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
6930 /* try to find a global control attribute. Note that a '*' means that we should
6931 * do aggregate control with the current url (so we don't do anything and
6932 * leave the current connection as is) */
6934 const gchar *control;
6937 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
6938 if (control == NULL)
6941 /* only take fully qualified urls */
6942 if (g_str_has_prefix (control, "rtsp://"))
6946 g_free (src->conninfo.location);
6947 src->conninfo.location = g_strdup (control);
6948 /* make a connection for this, if there was a connection already, nothing
6950 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
6951 GST_ERROR_OBJECT (src, "could not connect");
6954 /* we need to keep the control url separate from the connection url because
6955 * the rules for constructing the media control url need it */
6956 g_free (src->control);
6957 src->control = g_strdup (control);
6960 /* create streams */
6961 n_streams = gst_sdp_message_medias_len (sdp);
6962 for (i = 0; i < n_streams; i++) {
6963 gst_rtspsrc_create_stream (src, sdp, i, n_streams);
6966 src->state = GST_RTSP_STATE_INIT;
6969 if ((res = gst_rtspsrc_setup_streams_start (src, async)) < 0)
6972 /* reset our state */
6973 src->need_range = TRUE;
6976 src->state = GST_RTSP_STATE_READY;
6983 GST_ERROR_OBJECT (src, "setup failed");
6984 gst_rtspsrc_cleanup (src);
6989 static GstRTSPResult
6990 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
6994 GstRTSPMessage request = { 0 };
6995 GstRTSPMessage response = { 0 };
6998 gchar *respcont = NULL;
6999 GstRTSPVersion versions[] =
7000 { GST_RTSP_VERSION_2_0, GST_RTSP_VERSION_INVALID };
7002 src->version = src->default_version;
7003 if (src->default_version == GST_RTSP_VERSION_2_0) {
7004 versions[0] = GST_RTSP_VERSION_1_0;
7008 src->need_redirect = FALSE;
7010 /* can't continue without a valid url */
7011 if (G_UNLIKELY (src->conninfo.url == NULL)) {
7012 res = GST_RTSP_EINVAL;
7015 src->tried_url_auth = FALSE;
7017 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
7018 goto connect_failed;
7020 /* create OPTIONS */
7021 GST_DEBUG_OBJECT (src, "create options... (%s)", async ? "async" : "sync");
7023 gst_rtspsrc_init_request (src, &request, GST_RTSP_OPTIONS,
7024 src->conninfo.url_str);
7026 goto create_request_failed;
7029 request.type_data.request.version = src->version;
7030 GST_DEBUG_OBJECT (src, "send options...");
7033 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
7036 gst_rtspsrc_send (src, &src->conninfo, &request, &response,
7037 NULL, versions)) < 0) {
7041 src->version = request.type_data.request.version;
7042 GST_INFO_OBJECT (src, "Now using version: %s",
7043 gst_rtsp_version_as_text (src->version));
7046 if (!gst_rtspsrc_parse_methods (src, &response))
7049 /* create DESCRIBE */
7050 GST_DEBUG_OBJECT (src, "create describe...");
7052 gst_rtspsrc_init_request (src, &request, GST_RTSP_DESCRIBE,
7053 src->conninfo.url_str);
7055 goto create_request_failed;
7057 /* we only accept SDP for now */
7058 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
7062 GST_DEBUG_OBJECT (src, "send describe...");
7065 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
7068 gst_rtspsrc_send (src, &src->conninfo, &request, &response,
7072 /* we only perform redirect for describe and play, currently */
7073 if (src->need_redirect) {
7074 /* close connection, we don't have to send a TEARDOWN yet, ignore the
7076 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7078 gst_rtsp_message_unset (&request);
7079 gst_rtsp_message_unset (&response);
7085 /* it could be that the DESCRIBE method was not implemented */
7086 if (!(src->methods & GST_RTSP_DESCRIBE))
7089 /* check if reply is SDP */
7090 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
7092 /* could not be set but since the request returned OK, we assume it
7093 * was SDP, else check it. */
7095 const gchar *props = strchr (respcont, ';');
7098 gchar *mimetype = g_strndup (respcont, props - respcont);
7100 mimetype = g_strstrip (mimetype);
7101 if (g_ascii_strcasecmp (mimetype, "application/sdp") != 0) {
7103 goto wrong_content_type;
7106 /* TODO: Check for charset property and do conversions of all messages if
7107 * needed. Some servers actually send that property */
7110 } else if (g_ascii_strcasecmp (respcont, "application/sdp") != 0) {
7111 goto wrong_content_type;
7115 /* get message body and parse as SDP */
7116 gst_rtsp_message_get_body (&response, &data, &size);
7117 if (data == NULL || size == 0)
7120 GST_DEBUG_OBJECT (src, "parse SDP...");
7121 gst_sdp_message_new (sdp);
7122 gst_sdp_message_parse_buffer (data, size, *sdp);
7124 /* clean up any messages */
7125 gst_rtsp_message_unset (&request);
7126 gst_rtsp_message_unset (&response);
7133 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
7134 ("No valid RTSP URL was provided"));
7139 gchar *str = gst_rtsp_strresult (res);
7141 if (res != GST_RTSP_EINTR) {
7142 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
7143 ("Failed to connect. (%s)", str));
7145 GST_WARNING_OBJECT (src, "connect interrupted");
7150 create_request_failed:
7152 gchar *str = gst_rtsp_strresult (res);
7154 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7155 ("Could not create request. (%s)", str));
7161 /* Don't post a message - the rtsp_send method will have
7162 * taken care of it because we passed NULL for the response code */
7167 /* error was posted */
7168 res = GST_RTSP_ERROR;
7173 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7174 ("Server does not support SDP, got %s.", respcont));
7175 res = GST_RTSP_ERROR;
7180 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7181 ("Server can not provide an SDP."));
7182 res = GST_RTSP_ERROR;
7187 if (src->conninfo.connection) {
7188 GST_DEBUG_OBJECT (src, "free connection");
7189 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7191 gst_rtsp_message_unset (&request);
7192 gst_rtsp_message_unset (&response);
7197 static GstRTSPResult
7198 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
7203 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
7205 if (src->sdp == NULL) {
7206 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
7210 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
7215 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
7222 GST_WARNING_OBJECT (src, "can't get sdp");
7223 src->open_error = TRUE;
7228 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
7229 src->open_error = TRUE;
7234 static GstRTSPResult
7235 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
7237 GstRTSPMessage request = { 0 };
7238 GstRTSPMessage response = { 0 };
7239 GstRTSPResult res = GST_RTSP_OK;
7241 const gchar *control;
7243 GST_DEBUG_OBJECT (src, "TEARDOWN...");
7245 gst_rtspsrc_set_state (src, GST_STATE_READY);
7247 if (src->state < GST_RTSP_STATE_READY) {
7248 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
7255 /* construct a control url */
7256 control = get_aggregate_control (src);
7258 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
7261 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7262 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7263 const gchar *setup_url;
7264 GstRTSPConnInfo *info;
7266 /* try aggregate control first but do non-aggregate control otherwise */
7268 setup_url = control;
7269 else if ((setup_url = stream->conninfo.location) == NULL)
7272 if (src->conninfo.connection) {
7273 info = &src->conninfo;
7274 } else if (stream->conninfo.connection) {
7275 info = &stream->conninfo;
7279 if (!info->connected)
7284 gst_rtspsrc_init_request (src, &request, GST_RTSP_TEARDOWN, setup_url);
7286 goto create_request_failed;
7289 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
7292 gst_rtspsrc_send (src, info, &request, &response, NULL, NULL)) < 0)
7295 /* FIXME, parse result? */
7296 gst_rtsp_message_unset (&request);
7297 gst_rtsp_message_unset (&response);
7300 /* early exit when we did aggregate control */
7306 /* close connections */
7307 GST_DEBUG_OBJECT (src, "closing connection...");
7308 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7309 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7310 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7311 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
7315 gst_rtspsrc_cleanup (src);
7317 src->state = GST_RTSP_STATE_INVALID;
7320 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
7325 create_request_failed:
7327 gchar *str = gst_rtsp_strresult (res);
7329 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7330 ("Could not create request. (%s)", str));
7336 gchar *str = gst_rtsp_strresult (res);
7338 gst_rtsp_message_unset (&request);
7339 if (res != GST_RTSP_EINTR) {
7340 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7341 ("Could not send message. (%s)", str));
7343 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
7350 GST_DEBUG_OBJECT (src,
7351 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
7356 /* RTP-Info is of the format:
7358 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
7360 * rtptime corresponds to the timestamp for the NPT time given in the header
7361 * seqbase corresponds to the next sequence number we received. This number
7362 * indicates the first seqnum after the seek and should be used to discard
7363 * packets that are from before the seek.
7366 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
7371 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
7373 infos = g_strsplit (rtpinfo, ",", 0);
7374 for (i = 0; infos[i]; i++) {
7376 GstRTSPStream *stream;
7380 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
7382 /* init values, types of seqbase and timebase are bigger than needed so we
7383 * can store -1 as uninitialized values */
7388 /* parse url, find stream for url.
7389 * parse seq and rtptime. The seq number should be configured in the rtp
7390 * depayloader or session manager to detect gaps. Same for the rtptime, it
7391 * should be used to create an initial time newsegment. */
7392 fields = g_strsplit (infos[i], ";", 0);
7393 for (j = 0; fields[j]; j++) {
7394 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
7395 /* remove leading whitespace */
7396 fields[j] = g_strchug (fields[j]);
7397 if (g_str_has_prefix (fields[j], "url=")) {
7398 /* get the url and the stream */
7400 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
7401 } else if (g_str_has_prefix (fields[j], "seq=")) {
7402 seqbase = atoi (fields[j] + 4);
7403 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
7404 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
7407 g_strfreev (fields);
7408 /* now we need to store the values for the caps of the stream */
7409 if (stream != NULL) {
7410 GST_DEBUG_OBJECT (src,
7411 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
7412 stream, seqbase, timebase);
7414 /* we have a stream, configure detected params */
7415 stream->seqbase = seqbase;
7416 stream->timebase = timebase;
7425 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
7430 interval = strtoul (rtcp, NULL, 10);
7431 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
7436 interval *= GST_MSECOND;
7438 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7439 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7441 /* already (optionally) retrieved this when configuring manager */
7442 if (stream->session) {
7443 GObject *rtpsession = stream->session;
7445 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
7447 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
7451 /* now it happens that (Xenon) server sending this may also provide bogus
7452 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
7453 * and just use RTP-Info to sync */
7455 GObjectClass *klass;
7457 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
7458 if (g_object_class_find_property (klass, "rtcp-sync")) {
7459 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
7460 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
7466 gst_rtspsrc_get_float (const gchar * dstr)
7468 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7470 /* canonicalise floating point string so we can handle float strings
7471 * in the form "24.930" or "24,930" irrespective of the current locale */
7472 g_strlcpy (s, dstr, sizeof (s));
7473 g_strdelimit (s, ",", '.');
7474 return g_ascii_strtod (s, NULL);
7478 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
7480 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7482 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
7483 g_strlcpy (val_str, "now", sizeof (val_str));
7485 if (segment->position == 0) {
7486 g_strlcpy (val_str, "0", sizeof (val_str));
7488 g_ascii_dtostr (val_str, sizeof (val_str),
7489 ((gdouble) segment->position) / GST_SECOND);
7492 return g_strdup_printf ("npt=%s-", val_str);
7496 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
7500 stream->timebase = -1;
7501 stream->seqbase = -1;
7503 len = stream->ptmap->len;
7504 for (i = 0; i < len; i++) {
7505 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
7508 if (item->caps == NULL)
7511 item->caps = gst_caps_make_writable (item->caps);
7512 s = gst_caps_get_structure (item->caps, 0);
7513 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
7514 if (item->pt == stream->default_pt && stream->udpsrc[0])
7515 g_object_set (stream->udpsrc[0], "caps", item->caps, NULL);
7517 stream->need_caps = TRUE;
7520 static GstRTSPResult
7521 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
7523 GstRTSPResult res = GST_RTSP_OK;
7525 if (src->state < GST_RTSP_STATE_READY) {
7526 res = GST_RTSP_ERROR;
7527 if (src->open_error) {
7528 GST_DEBUG_OBJECT (src, "the stream was in error");
7532 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
7534 if ((res = gst_rtspsrc_open (src, async)) < 0) {
7535 GST_DEBUG_OBJECT (src, "failed to open stream");
7544 static GstRTSPResult
7545 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async,
7546 const gchar * seek_style)
7548 GstRTSPMessage request = { 0 };
7549 GstRTSPMessage response = { 0 };
7550 GstRTSPResult res = GST_RTSP_OK;
7554 const gchar *control;
7556 GST_DEBUG_OBJECT (src, "PLAY...");
7559 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7562 if (!(src->methods & GST_RTSP_PLAY))
7565 if (src->state == GST_RTSP_STATE_PLAYING)
7568 if (!src->conninfo.connection || !src->conninfo.connected)
7571 /* send some dummy packets before we activate the receive in the
7573 gst_rtspsrc_send_dummy_packets (src);
7575 /* require new SR packets */
7577 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
7579 /* construct a control url */
7580 control = get_aggregate_control (src);
7582 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7583 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7584 const gchar *setup_url;
7585 GstRTSPConnInfo *conninfo;
7587 /* try aggregate control first but do non-aggregate control otherwise */
7589 setup_url = control;
7590 else if ((setup_url = stream->conninfo.location) == NULL)
7593 if (src->conninfo.connection) {
7594 conninfo = &src->conninfo;
7595 } else if (stream->conninfo.connection) {
7596 conninfo = &stream->conninfo;
7602 res = gst_rtspsrc_init_request (src, &request, GST_RTSP_PLAY, setup_url);
7604 goto create_request_failed;
7606 if (src->need_range && src->seekable >= 0.0) {
7607 hval = gen_range_header (src, segment);
7609 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
7611 /* store the newsegment event so it can be sent from the streaming thread. */
7612 src->need_segment = TRUE;
7615 if (segment->rate != 1.0) {
7616 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
7618 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
7620 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
7622 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
7626 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SEEK_STYLE,
7630 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
7633 gst_rtspsrc_send (src, conninfo, &request, &response, NULL, NULL))
7637 if (src->need_redirect) {
7638 GST_DEBUG_OBJECT (src,
7639 "redirect: tearing down and restarting with new url");
7640 /* teardown and restart with new url */
7641 gst_rtspsrc_close (src, TRUE, FALSE);
7642 /* reset protocols to force re-negotiation with redirected url */
7643 src->cur_protocols = src->protocols;
7644 gst_rtsp_message_unset (&request);
7645 gst_rtsp_message_unset (&response);
7649 /* seek may have silently failed as it is not supported */
7650 if (!(src->methods & GST_RTSP_PLAY)) {
7651 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
7653 if (src->version >= GST_RTSP_VERSION_2_0 && src->seekable >= 0.0) {
7654 GST_WARNING_OBJECT (src, "Server declared stream as seekable but"
7655 " playing with range failed... Ignoring information.");
7657 /* obviously it is supported as we made it here */
7658 src->methods |= GST_RTSP_PLAY;
7659 src->seekable = -1.0;
7660 /* but there is nothing to parse in the response,
7661 * so convey we have no idea and not to expect anything particular */
7662 clear_rtp_base (src, stream);
7666 /* need to do for all streams */
7667 for (run = src->streams; run; run = g_list_next (run))
7668 clear_rtp_base (src, (GstRTSPStream *) run->data);
7670 /* NOTE the above also disables npt based eos detection */
7671 /* and below forces position to 0,
7672 * which is visible feedback we lost the plot */
7673 segment->start = segment->position = src->last_pos;
7676 gst_rtsp_message_unset (&request);
7678 /* parse RTP npt field. This is the current position in the stream (Normal
7679 * Play Time) and should be put in the NEWSEGMENT position field. */
7680 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
7682 gst_rtspsrc_parse_range (src, hval, segment);
7684 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
7685 segment->rate = 1.0;
7687 /* parse Speed header. This is the intended playback rate of the stream
7688 * and should be put in the NEWSEGMENT rate field. */
7689 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
7690 0) == GST_RTSP_OK) {
7691 segment->rate = gst_rtspsrc_get_float (hval);
7692 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
7693 &hval, 0) == GST_RTSP_OK) {
7694 segment->rate = gst_rtspsrc_get_float (hval);
7697 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
7698 * for the RTP packets. If this is not present, we assume all starts from 0...
7699 * This is info for the RTP session manager that we pass to it in caps. */
7701 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
7702 &hval, hval_idx++) == GST_RTSP_OK)
7703 gst_rtspsrc_parse_rtpinfo (src, hval);
7705 /* some servers indicate RTCP parameters in PLAY response,
7706 * rather than properly in SDP */
7707 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
7708 &hval, 0) == GST_RTSP_OK)
7709 gst_rtspsrc_handle_rtcp_interval (src, hval);
7711 gst_rtsp_message_unset (&response);
7713 /* early exit when we did aggregate control */
7717 /* configure the caps of the streams after we parsed all headers. Only reset
7718 * the manager object when we set a new Range header (we did a seek) */
7719 gst_rtspsrc_configure_caps (src, segment, src->need_range);
7721 /* set to PLAYING after we have configured the caps, otherwise we
7722 * might end up calling request_key (with SRTP) while caps are still
7723 * being configured. */
7724 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
7726 /* set again when needed */
7727 src->need_range = FALSE;
7729 src->running = TRUE;
7730 src->base_time = -1;
7731 src->state = GST_RTSP_STATE_PLAYING;
7734 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
7735 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7736 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7737 stream->discont = TRUE;
7742 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
7749 GST_DEBUG_OBJECT (src, "failed to open stream");
7754 GST_DEBUG_OBJECT (src, "PLAY is not supported");
7759 GST_DEBUG_OBJECT (src, "we were already PLAYING");
7762 create_request_failed:
7764 gchar *str = gst_rtsp_strresult (res);
7766 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7767 ("Could not create request. (%s)", str));
7773 gchar *str = gst_rtsp_strresult (res);
7775 gst_rtsp_message_unset (&request);
7776 if (res != GST_RTSP_EINTR) {
7777 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7778 ("Could not send message. (%s)", str));
7780 GST_WARNING_OBJECT (src, "PLAY interrupted");
7787 static GstRTSPResult
7788 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
7790 GstRTSPResult res = GST_RTSP_OK;
7791 GstRTSPMessage request = { 0 };
7792 GstRTSPMessage response = { 0 };
7794 const gchar *control;
7796 GST_DEBUG_OBJECT (src, "PAUSE...");
7798 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7801 if (!(src->methods & GST_RTSP_PAUSE))
7804 if (src->state == GST_RTSP_STATE_READY)
7807 if (!src->conninfo.connection || !src->conninfo.connected)
7810 /* construct a control url */
7811 control = get_aggregate_control (src);
7813 /* loop over the streams. We might exit the loop early when we could do an
7814 * aggregate control */
7815 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7816 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7817 GstRTSPConnInfo *conninfo;
7818 const gchar *setup_url;
7820 /* try aggregate control first but do non-aggregate control otherwise */
7822 setup_url = control;
7823 else if ((setup_url = stream->conninfo.location) == NULL)
7826 if (src->conninfo.connection) {
7827 conninfo = &src->conninfo;
7828 } else if (stream->conninfo.connection) {
7829 conninfo = &stream->conninfo;
7835 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
7836 ("Sending PAUSE request"));
7839 gst_rtspsrc_init_request (src, &request, GST_RTSP_PAUSE,
7841 goto create_request_failed;
7844 gst_rtspsrc_send (src, conninfo, &request, &response, NULL,
7848 gst_rtsp_message_unset (&request);
7849 gst_rtsp_message_unset (&response);
7851 /* exit early when we did agregate control */
7856 /* change element states now */
7857 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
7860 src->state = GST_RTSP_STATE_READY;
7864 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
7871 GST_DEBUG_OBJECT (src, "failed to open stream");
7876 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
7881 GST_DEBUG_OBJECT (src, "we were already PAUSED");
7884 create_request_failed:
7886 gchar *str = gst_rtsp_strresult (res);
7888 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7889 ("Could not create request. (%s)", str));
7895 gchar *str = gst_rtsp_strresult (res);
7897 gst_rtsp_message_unset (&request);
7898 if (res != GST_RTSP_EINTR) {
7899 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7900 ("Could not send message. (%s)", str));
7902 GST_WARNING_OBJECT (src, "PAUSE interrupted");
7910 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
7912 GstRTSPSrc *rtspsrc;
7914 rtspsrc = GST_RTSPSRC (bin);
7916 switch (GST_MESSAGE_TYPE (message)) {
7917 case GST_MESSAGE_EOS:
7918 gst_message_unref (message);
7920 case GST_MESSAGE_ELEMENT:
7922 const GstStructure *s = gst_message_get_structure (message);
7924 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
7925 gboolean ignore_timeout;
7927 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
7929 GST_OBJECT_LOCK (rtspsrc);
7930 ignore_timeout = rtspsrc->ignore_timeout;
7931 rtspsrc->ignore_timeout = TRUE;
7932 GST_OBJECT_UNLOCK (rtspsrc);
7934 /* we only act on the first udp timeout message, others are irrelevant
7935 * and can be ignored. */
7936 if (!ignore_timeout)
7937 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
7939 gst_message_unref (message);
7942 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7945 case GST_MESSAGE_ERROR:
7948 GstRTSPStream *stream;
7951 udpsrc = GST_MESSAGE_SRC (message);
7953 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
7954 GST_ELEMENT_NAME (udpsrc));
7956 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
7960 /* we ignore the RTCP udpsrc */
7961 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
7964 /* if we get error messages from the udp sources, that's not a problem as
7965 * long as not all of them error out. We also don't really know what the
7966 * problem is, the message does not give enough detail... */
7967 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
7968 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
7969 if (ret != GST_FLOW_OK)
7973 gst_message_unref (message);
7977 /* fatal but not our message, forward */
7978 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7983 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7989 /* the thread where everything happens */
7991 gst_rtspsrc_thread (GstRTSPSrc * src)
7995 GST_OBJECT_LOCK (src);
7996 cmd = src->pending_cmd;
7997 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
7998 || cmd == CMD_LOOP || cmd == CMD_OPEN)
7999 src->pending_cmd = CMD_LOOP;
8001 src->pending_cmd = CMD_WAIT;
8002 GST_DEBUG_OBJECT (src, "got command %s", cmd_to_string (cmd));
8004 /* we got the message command, so ensure communication is possible again */
8005 gst_rtspsrc_connection_flush (src, FALSE);
8007 src->busy_cmd = cmd;
8008 GST_OBJECT_UNLOCK (src);
8012 gst_rtspsrc_open (src, TRUE);
8015 gst_rtspsrc_play (src, &src->segment, TRUE, NULL);
8018 gst_rtspsrc_pause (src, TRUE);
8021 gst_rtspsrc_close (src, TRUE, FALSE);
8024 gst_rtspsrc_loop (src);
8027 gst_rtspsrc_reconnect (src, FALSE);
8033 GST_OBJECT_LOCK (src);
8034 /* and go back to sleep */
8035 if (src->pending_cmd == CMD_WAIT) {
8037 gst_task_pause (src->task);
8040 src->busy_cmd = CMD_WAIT;
8041 GST_OBJECT_UNLOCK (src);
8045 gst_rtspsrc_start (GstRTSPSrc * src)
8047 GST_DEBUG_OBJECT (src, "starting");
8049 GST_OBJECT_LOCK (src);
8051 src->pending_cmd = CMD_WAIT;
8053 if (src->task == NULL) {
8054 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
8055 if (src->task == NULL)
8058 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
8060 GST_OBJECT_UNLOCK (src);
8067 GST_OBJECT_UNLOCK (src);
8068 GST_ERROR_OBJECT (src, "failed to create task");
8074 gst_rtspsrc_stop (GstRTSPSrc * src)
8078 GST_DEBUG_OBJECT (src, "stopping");
8080 /* also cancels pending task */
8081 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
8083 GST_OBJECT_LOCK (src);
8084 if ((task = src->task)) {
8086 GST_OBJECT_UNLOCK (src);
8088 gst_task_stop (task);
8090 /* make sure it is not running */
8091 GST_RTSP_STREAM_LOCK (src);
8092 GST_RTSP_STREAM_UNLOCK (src);
8094 /* now wait for the task to finish */
8095 gst_task_join (task);
8097 /* and free the task */
8098 gst_object_unref (GST_OBJECT (task));
8100 GST_OBJECT_LOCK (src);
8102 GST_OBJECT_UNLOCK (src);
8104 /* ensure synchronously all is closed and clean */
8105 gst_rtspsrc_close (src, FALSE, TRUE);
8110 static GstStateChangeReturn
8111 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
8113 GstRTSPSrc *rtspsrc;
8114 GstStateChangeReturn ret;
8116 rtspsrc = GST_RTSPSRC (element);
8118 switch (transition) {
8119 case GST_STATE_CHANGE_NULL_TO_READY:
8120 if (!gst_rtspsrc_start (rtspsrc))
8123 case GST_STATE_CHANGE_READY_TO_PAUSED:
8124 /* init some state */
8125 rtspsrc->cur_protocols = rtspsrc->protocols;
8126 /* first attempt, don't ignore timeouts */
8127 rtspsrc->ignore_timeout = FALSE;
8128 rtspsrc->open_error = FALSE;
8129 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
8131 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
8132 set_manager_buffer_mode (rtspsrc);
8134 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
8135 /* unblock the tcp tasks and make the loop waiting */
8136 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
8137 /* make sure it is waiting before we send PAUSE or PLAY below */
8138 GST_RTSP_STREAM_LOCK (rtspsrc);
8139 GST_RTSP_STREAM_UNLOCK (rtspsrc);
8142 case GST_STATE_CHANGE_PAUSED_TO_READY:
8148 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
8149 if (ret == GST_STATE_CHANGE_FAILURE)
8152 switch (transition) {
8153 case GST_STATE_CHANGE_NULL_TO_READY:
8154 ret = GST_STATE_CHANGE_SUCCESS;
8156 case GST_STATE_CHANGE_READY_TO_PAUSED:
8157 ret = GST_STATE_CHANGE_NO_PREROLL;
8159 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
8160 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
8161 ret = GST_STATE_CHANGE_SUCCESS;
8163 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
8164 /* send pause request and keep the idle task around */
8165 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
8166 ret = GST_STATE_CHANGE_NO_PREROLL;
8168 case GST_STATE_CHANGE_PAUSED_TO_READY:
8169 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_ALL);
8170 ret = GST_STATE_CHANGE_SUCCESS;
8172 case GST_STATE_CHANGE_READY_TO_NULL:
8173 gst_rtspsrc_stop (rtspsrc);
8174 ret = GST_STATE_CHANGE_SUCCESS;
8177 /* Otherwise it's success, we don't want to return spurious
8178 * NO_PREROLL or ASYNC from internal elements as we care for
8179 * state changes ourselves here
8181 * This is to catch PAUSED->PAUSED and PLAYING->PLAYING transitions.
8183 if (GST_STATE_TRANSITION_NEXT (transition) == GST_STATE_PAUSED)
8184 ret = GST_STATE_CHANGE_NO_PREROLL;
8186 ret = GST_STATE_CHANGE_SUCCESS;
8195 GST_DEBUG_OBJECT (rtspsrc, "start failed");
8196 return GST_STATE_CHANGE_FAILURE;
8201 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
8204 GstRTSPSrc *rtspsrc;
8206 rtspsrc = GST_RTSPSRC (element);
8208 if (GST_EVENT_IS_DOWNSTREAM (event)) {
8209 res = gst_rtspsrc_push_event (rtspsrc, event);
8211 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
8218 /*** GSTURIHANDLER INTERFACE *************************************************/
8221 gst_rtspsrc_uri_get_type (GType type)
8226 static const gchar *const *
8227 gst_rtspsrc_uri_get_protocols (GType type)
8229 static const gchar *protocols[] =
8230 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
8231 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
8238 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
8240 GstRTSPSrc *src = GST_RTSPSRC (handler);
8242 /* FIXME: make thread-safe */
8243 return g_strdup (src->conninfo.location);
8247 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
8253 GstRTSPUrl *newurl = NULL;
8254 GstSDPMessage *sdp = NULL;
8256 src = GST_RTSPSRC (handler);
8258 /* same URI, we're fine */
8259 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
8262 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
8263 sres = gst_sdp_message_new (&sdp);
8267 GST_DEBUG_OBJECT (src, "parsing SDP message");
8268 sres = gst_sdp_message_parse_uri (uri, sdp);
8273 GST_DEBUG_OBJECT (src, "parsing URI");
8274 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
8278 /* if worked, free previous and store new url object along with the original
8280 GST_DEBUG_OBJECT (src, "configuring URI");
8281 g_free (src->conninfo.location);
8282 src->conninfo.location = g_strdup (uri);
8283 gst_rtsp_url_free (src->conninfo.url);
8284 src->conninfo.url = newurl;
8285 g_free (src->conninfo.url_str);
8287 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
8289 src->conninfo.url_str = NULL;
8292 gst_sdp_message_free (src->sdp);
8294 src->from_sdp = sdp != NULL;
8296 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
8297 GST_DEBUG_OBJECT (src, "request uri is: %s",
8298 GST_STR_NULL (src->conninfo.url_str));
8305 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
8310 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", sres);
8311 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8312 "Could not create SDP");
8317 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", sres,
8318 GST_STR_NULL (uri));
8319 gst_sdp_message_free (sdp);
8320 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8326 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
8327 GST_STR_NULL (uri), res);
8328 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8329 "Invalid RTSP URI");
8335 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
8337 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
8339 iface->get_type = gst_rtspsrc_uri_get_type;
8340 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
8341 iface->get_uri = gst_rtspsrc_uri_get_uri;
8342 iface->set_uri = gst_rtspsrc_uri_set_uri;
8345 typedef struct _RTSPKeyValue
8347 GstRTSPHeaderField field;
8349 gchar *custom_key; /* custom header string (field is INVALID then) */
8353 key_value_foreach (GArray * array, GFunc func, gpointer user_data)
8357 g_return_if_fail (array != NULL);
8359 for (i = 0; i < array->len; i++) {
8360 (*func) (&g_array_index (array, RTSPKeyValue, i), user_data);
8365 dump_key_value (gpointer data, gpointer user_data G_GNUC_UNUSED)
8367 RTSPKeyValue *key_value = (RTSPKeyValue *) data;
8368 GstRTSPSrc *src = GST_RTSPSRC (user_data);
8369 const gchar *key_string;
8371 if (key_value->custom_key != NULL)
8372 key_string = key_value->custom_key;
8374 key_string = gst_rtsp_header_as_text (key_value->field);
8376 GST_LOG_OBJECT (src, " key: '%s', value: '%s'", key_string,
8381 gst_rtspsrc_print_rtsp_message (GstRTSPSrc * src, const GstRTSPMessage * msg)
8385 GString *body_string = NULL;
8387 g_return_if_fail (src != NULL);
8388 g_return_if_fail (msg != NULL);
8390 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) < GST_LEVEL_LOG)
8393 GST_LOG_OBJECT (src, "--------------------------------------------");
8394 switch (msg->type) {
8395 case GST_RTSP_MESSAGE_REQUEST:
8396 GST_LOG_OBJECT (src, "RTSP request message %p", msg);
8397 GST_LOG_OBJECT (src, " request line:");
8398 GST_LOG_OBJECT (src, " method: '%s'",
8399 gst_rtsp_method_as_text (msg->type_data.request.method));
8400 GST_LOG_OBJECT (src, " uri: '%s'", msg->type_data.request.uri);
8401 GST_LOG_OBJECT (src, " version: '%s'",
8402 gst_rtsp_version_as_text (msg->type_data.request.version));
8403 GST_LOG_OBJECT (src, " headers:");
8404 key_value_foreach (msg->hdr_fields, dump_key_value, src);
8405 GST_LOG_OBJECT (src, " body:");
8406 gst_rtsp_message_get_body (msg, &data, &size);
8408 body_string = g_string_new_len ((const gchar *) data, size);
8409 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
8410 g_string_free (body_string, TRUE);
8414 case GST_RTSP_MESSAGE_RESPONSE:
8415 GST_LOG_OBJECT (src, "RTSP response message %p", msg);
8416 GST_LOG_OBJECT (src, " status line:");
8417 GST_LOG_OBJECT (src, " code: '%d'", msg->type_data.response.code);
8418 GST_LOG_OBJECT (src, " reason: '%s'", msg->type_data.response.reason);
8419 GST_LOG_OBJECT (src, " version: '%s",
8420 gst_rtsp_version_as_text (msg->type_data.response.version));
8421 GST_LOG_OBJECT (src, " headers:");
8422 key_value_foreach (msg->hdr_fields, dump_key_value, src);
8423 gst_rtsp_message_get_body (msg, &data, &size);
8424 GST_LOG_OBJECT (src, " body: length %d", size);
8426 body_string = g_string_new_len ((const gchar *) data, size);
8427 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
8428 g_string_free (body_string, TRUE);
8432 case GST_RTSP_MESSAGE_HTTP_REQUEST:
8433 GST_LOG_OBJECT (src, "HTTP request message %p", msg);
8434 GST_LOG_OBJECT (src, " request line:");
8435 GST_LOG_OBJECT (src, " method: '%s'",
8436 gst_rtsp_method_as_text (msg->type_data.request.method));
8437 GST_LOG_OBJECT (src, " uri: '%s'", msg->type_data.request.uri);
8438 GST_LOG_OBJECT (src, " version: '%s'",
8439 gst_rtsp_version_as_text (msg->type_data.request.version));
8440 GST_LOG_OBJECT (src, " headers:");
8441 key_value_foreach (msg->hdr_fields, dump_key_value, src);
8442 GST_LOG_OBJECT (src, " body:");
8443 gst_rtsp_message_get_body (msg, &data, &size);
8445 body_string = g_string_new_len ((const gchar *) data, size);
8446 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
8447 g_string_free (body_string, TRUE);
8451 case GST_RTSP_MESSAGE_HTTP_RESPONSE:
8452 GST_LOG_OBJECT (src, "HTTP response message %p", msg);
8453 GST_LOG_OBJECT (src, " status line:");
8454 GST_LOG_OBJECT (src, " code: '%d'", msg->type_data.response.code);
8455 GST_LOG_OBJECT (src, " reason: '%s'", msg->type_data.response.reason);
8456 GST_LOG_OBJECT (src, " version: '%s'",
8457 gst_rtsp_version_as_text (msg->type_data.response.version));
8458 GST_LOG_OBJECT (src, " headers:");
8459 key_value_foreach (msg->hdr_fields, dump_key_value, src);
8460 gst_rtsp_message_get_body (msg, &data, &size);
8461 GST_LOG_OBJECT (src, " body: length %d", size);
8463 body_string = g_string_new_len ((const gchar *) data, size);
8464 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
8465 g_string_free (body_string, TRUE);
8469 case GST_RTSP_MESSAGE_DATA:
8470 GST_LOG_OBJECT (src, "RTSP data message %p", msg);
8471 GST_LOG_OBJECT (src, " channel: '%d'", msg->type_data.data.channel);
8472 GST_LOG_OBJECT (src, " size: '%d'", msg->body_size);
8473 gst_rtsp_message_get_body (msg, &data, &size);
8475 body_string = g_string_new_len ((const gchar *) data, size);
8476 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
8477 g_string_free (body_string, TRUE);
8482 GST_LOG_OBJECT (src, "unsupported message type %d", msg->type);
8485 GST_LOG_OBJECT (src, "--------------------------------------------");
8489 gst_rtspsrc_print_sdp_media (GstRTSPSrc * src, GstSDPMedia * media)
8491 GST_LOG_OBJECT (src, " media: '%s'", GST_STR_NULL (media->media));
8492 GST_LOG_OBJECT (src, " port: '%u'", media->port);
8493 GST_LOG_OBJECT (src, " num_ports: '%u'", media->num_ports);
8494 GST_LOG_OBJECT (src, " proto: '%s'", GST_STR_NULL (media->proto));
8495 if (media->fmts && media->fmts->len > 0) {
8498 GST_LOG_OBJECT (src, " formats:");
8499 for (i = 0; i < media->fmts->len; i++) {
8500 GST_LOG_OBJECT (src, " format '%s'", g_array_index (media->fmts,
8504 GST_LOG_OBJECT (src, " information: '%s'",
8505 GST_STR_NULL (media->information));
8506 if (media->connections && media->connections->len > 0) {
8509 GST_LOG_OBJECT (src, " connections:");
8510 for (i = 0; i < media->connections->len; i++) {
8511 GstSDPConnection *conn =
8512 &g_array_index (media->connections, GstSDPConnection, i);
8514 GST_LOG_OBJECT (src, " nettype: '%s'",
8515 GST_STR_NULL (conn->nettype));
8516 GST_LOG_OBJECT (src, " addrtype: '%s'",
8517 GST_STR_NULL (conn->addrtype));
8518 GST_LOG_OBJECT (src, " address: '%s'",
8519 GST_STR_NULL (conn->address));
8520 GST_LOG_OBJECT (src, " ttl: '%u'", conn->ttl);
8521 GST_LOG_OBJECT (src, " addr_number: '%u'", conn->addr_number);
8524 if (media->bandwidths && media->bandwidths->len > 0) {
8527 GST_LOG_OBJECT (src, " bandwidths:");
8528 for (i = 0; i < media->bandwidths->len; i++) {
8529 GstSDPBandwidth *bw =
8530 &g_array_index (media->bandwidths, GstSDPBandwidth, i);
8532 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (bw->bwtype));
8533 GST_LOG_OBJECT (src, " bandwidth: '%u'", bw->bandwidth);
8536 GST_LOG_OBJECT (src, " key:");
8537 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (media->key.type));
8538 GST_LOG_OBJECT (src, " data: '%s'", GST_STR_NULL (media->key.data));
8539 if (media->attributes && media->attributes->len > 0) {
8542 GST_LOG_OBJECT (src, " attributes:");
8543 for (i = 0; i < media->attributes->len; i++) {
8544 GstSDPAttribute *attr =
8545 &g_array_index (media->attributes, GstSDPAttribute, i);
8547 GST_LOG_OBJECT (src, " attribute '%s' : '%s'", attr->key, attr->value);
8553 gst_rtspsrc_print_sdp_message (GstRTSPSrc * src, const GstSDPMessage * msg)
8555 g_return_if_fail (src != NULL);
8556 g_return_if_fail (msg != NULL);
8558 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) < GST_LEVEL_LOG)
8561 GST_LOG_OBJECT (src, "--------------------------------------------");
8562 GST_LOG_OBJECT (src, "sdp packet %p:", msg);
8563 GST_LOG_OBJECT (src, " version: '%s'", GST_STR_NULL (msg->version));
8564 GST_LOG_OBJECT (src, " origin:");
8565 GST_LOG_OBJECT (src, " username: '%s'",
8566 GST_STR_NULL (msg->origin.username));
8567 GST_LOG_OBJECT (src, " sess_id: '%s'",
8568 GST_STR_NULL (msg->origin.sess_id));
8569 GST_LOG_OBJECT (src, " sess_version: '%s'",
8570 GST_STR_NULL (msg->origin.sess_version));
8571 GST_LOG_OBJECT (src, " nettype: '%s'",
8572 GST_STR_NULL (msg->origin.nettype));
8573 GST_LOG_OBJECT (src, " addrtype: '%s'",
8574 GST_STR_NULL (msg->origin.addrtype));
8575 GST_LOG_OBJECT (src, " addr: '%s'", GST_STR_NULL (msg->origin.addr));
8576 GST_LOG_OBJECT (src, " session_name: '%s'",
8577 GST_STR_NULL (msg->session_name));
8578 GST_LOG_OBJECT (src, " information: '%s'", GST_STR_NULL (msg->information));
8579 GST_LOG_OBJECT (src, " uri: '%s'", GST_STR_NULL (msg->uri));
8581 if (msg->emails && msg->emails->len > 0) {
8584 GST_LOG_OBJECT (src, " emails:");
8585 for (i = 0; i < msg->emails->len; i++) {
8586 GST_LOG_OBJECT (src, " email '%s'", g_array_index (msg->emails, gchar *,
8590 if (msg->phones && msg->phones->len > 0) {
8593 GST_LOG_OBJECT (src, " phones:");
8594 for (i = 0; i < msg->phones->len; i++) {
8595 GST_LOG_OBJECT (src, " phone '%s'", g_array_index (msg->phones, gchar *,
8599 GST_LOG_OBJECT (src, " connection:");
8600 GST_LOG_OBJECT (src, " nettype: '%s'",
8601 GST_STR_NULL (msg->connection.nettype));
8602 GST_LOG_OBJECT (src, " addrtype: '%s'",
8603 GST_STR_NULL (msg->connection.addrtype));
8604 GST_LOG_OBJECT (src, " address: '%s'",
8605 GST_STR_NULL (msg->connection.address));
8606 GST_LOG_OBJECT (src, " ttl: '%u'", msg->connection.ttl);
8607 GST_LOG_OBJECT (src, " addr_number: '%u'", msg->connection.addr_number);
8608 if (msg->bandwidths && msg->bandwidths->len > 0) {
8611 GST_LOG_OBJECT (src, " bandwidths:");
8612 for (i = 0; i < msg->bandwidths->len; i++) {
8613 GstSDPBandwidth *bw =
8614 &g_array_index (msg->bandwidths, GstSDPBandwidth, i);
8616 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (bw->bwtype));
8617 GST_LOG_OBJECT (src, " bandwidth: '%u'", bw->bandwidth);
8620 GST_LOG_OBJECT (src, " key:");
8621 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (msg->key.type));
8622 GST_LOG_OBJECT (src, " data: '%s'", GST_STR_NULL (msg->key.data));
8623 if (msg->attributes && msg->attributes->len > 0) {
8626 GST_LOG_OBJECT (src, " attributes:");
8627 for (i = 0; i < msg->attributes->len; i++) {
8628 GstSDPAttribute *attr =
8629 &g_array_index (msg->attributes, GstSDPAttribute, i);
8631 GST_LOG_OBJECT (src, " attribute '%s' : '%s'", attr->key, attr->value);
8634 if (msg->medias && msg->medias->len > 0) {
8637 GST_LOG_OBJECT (src, " medias:");
8638 for (i = 0; i < msg->medias->len; i++) {
8639 GST_LOG_OBJECT (src, " media %u:", i);
8640 gst_rtspsrc_print_sdp_media (src, &g_array_index (msg->medias,
8644 GST_LOG_OBJECT (src, "--------------------------------------------");