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
47 * Makes a connection to an RTSP server and read the data.
48 * rtspsrc strictly follows RFC 2326 and therefore does not (yet) support
49 * RealMedia/Quicktime/Microsoft extensions.
51 * RTSP supports transport over TCP or UDP in unicast or multicast mode. By
52 * default rtspsrc will negotiate a connection in the following order:
53 * UDP unicast/UDP multicast/TCP. The order cannot be changed but the allowed
54 * protocols can be controlled with the #GstRTSPSrc:protocols property.
56 * rtspsrc currently understands SDP as the format of the session description.
57 * For each stream listed in the SDP a new rtp_stream\%d pad will be created
58 * with caps derived from the SDP media description. This is a caps of mime type
59 * "application/x-rtp" that can be connected to any available RTP depayloader
62 * rtspsrc will internally instantiate an RTP session manager element
63 * that will handle the RTCP messages to and from the server, jitter removal,
64 * packet reordering along with providing a clock for the pipeline.
65 * This feature is implemented using the gstrtpbin element.
67 * rtspsrc acts like a live source and will therefore only generate data in the
70 * If a RTP session times out then the rtspsrc will generate an element message
71 * named "GstRTSPSrcTimeout". Currently this is only supported for timeouts
74 * The message's structure contains three fields:
76 * #GstRTSPSrcTimeoutCause `cause`: the cause of the timeout.
78 * #gint `stream-number`: an internal identifier of the stream that timed out.
80 * #guint `ssrc`: the SSRC of the stream that timed out.
82 * ## Example launch line
84 * gst-launch-1.0 rtspsrc location=rtsp://some.server/url ! fakesink
85 * ]| Establish a connection to an RTSP server and send the raw RTP packets to a
96 #endif /* HAVE_UNISTD_H */
102 #include <gst/net/gstnet.h>
103 #include <gst/sdp/gstsdpmessage.h>
104 #include <gst/sdp/gstmikey.h>
105 #include <gst/rtp/rtp.h>
107 #include "gst/gst-i18n-plugin.h"
109 #include "gstrtspsrc.h"
111 GST_DEBUG_CATEGORY_STATIC (rtspsrc_debug);
112 #define GST_CAT_DEFAULT (rtspsrc_debug)
114 static GstStaticPadTemplate rtptemplate = GST_STATIC_PAD_TEMPLATE ("stream_%u",
117 GST_STATIC_CAPS ("application/x-rtp; application/x-rdt"));
119 /* templates used internally */
120 static GstStaticPadTemplate anysrctemplate =
121 GST_STATIC_PAD_TEMPLATE ("internalsrc_%u",
124 GST_STATIC_CAPS_ANY);
126 static GstStaticPadTemplate anysinktemplate =
127 GST_STATIC_PAD_TEMPLATE ("internalsink_%u",
130 GST_STATIC_CAPS_ANY);
134 SIGNAL_HANDLE_REQUEST,
136 SIGNAL_SELECT_STREAM,
138 SIGNAL_REQUEST_RTCP_KEY,
139 SIGNAL_ACCEPT_CERTIFICATE,
141 SIGNAL_PUSH_BACKCHANNEL_BUFFER,
142 SIGNAL_GET_PARAMETER,
143 SIGNAL_GET_PARAMETERS,
144 SIGNAL_SET_PARAMETER,
148 enum _GstRtspSrcRtcpSyncMode
155 enum _GstRtspSrcBufferMode
164 #define GST_TYPE_RTSP_SRC_BUFFER_MODE (gst_rtsp_src_buffer_mode_get_type())
166 gst_rtsp_src_buffer_mode_get_type (void)
168 static GType buffer_mode_type = 0;
169 static const GEnumValue buffer_modes[] = {
170 {BUFFER_MODE_NONE, "Only use RTP timestamps", "none"},
171 {BUFFER_MODE_SLAVE, "Slave receiver to sender clock", "slave"},
172 {BUFFER_MODE_BUFFER, "Do low/high watermark buffering", "buffer"},
173 {BUFFER_MODE_AUTO, "Choose mode depending on stream live", "auto"},
174 {BUFFER_MODE_SYNCED, "Synchronized sender and receiver clocks", "synced"},
178 if (!buffer_mode_type) {
180 g_enum_register_static ("GstRTSPSrcBufferMode", buffer_modes);
182 return buffer_mode_type;
185 enum _GstRtspSrcNtpTimeSource
188 NTP_TIME_SOURCE_UNIX,
189 NTP_TIME_SOURCE_RUNNING_TIME,
190 NTP_TIME_SOURCE_CLOCK_TIME
193 #define DEBUG_RTSP(__self,msg) gst_rtspsrc_print_rtsp_message (__self, msg)
194 #define DEBUG_SDP(__self,msg) gst_rtspsrc_print_sdp_message (__self, msg)
196 #define GST_TYPE_RTSP_SRC_NTP_TIME_SOURCE (gst_rtsp_src_ntp_time_source_get_type())
198 gst_rtsp_src_ntp_time_source_get_type (void)
200 static GType ntp_time_source_type = 0;
201 static const GEnumValue ntp_time_source_values[] = {
202 {NTP_TIME_SOURCE_NTP, "NTP time based on realtime clock", "ntp"},
203 {NTP_TIME_SOURCE_UNIX, "UNIX time based on realtime clock", "unix"},
204 {NTP_TIME_SOURCE_RUNNING_TIME,
205 "Running time based on pipeline clock",
207 {NTP_TIME_SOURCE_CLOCK_TIME, "Pipeline clock time", "clock-time"},
211 if (!ntp_time_source_type) {
212 ntp_time_source_type =
213 g_enum_register_static ("GstRTSPSrcNtpTimeSource",
214 ntp_time_source_values);
216 return ntp_time_source_type;
219 enum _GstRtspBackchannel
225 #define GST_TYPE_RTSP_BACKCHANNEL (gst_rtsp_backchannel_get_type())
227 gst_rtsp_backchannel_get_type (void)
229 static GType backchannel_type = 0;
230 static const GEnumValue backchannel_values[] = {
231 {BACKCHANNEL_NONE, "No backchannel", "none"},
232 {BACKCHANNEL_ONVIF, "ONVIF audio backchannel", "onvif"},
236 if (G_UNLIKELY (backchannel_type == 0)) {
238 g_enum_register_static ("GstRTSPBackchannel", backchannel_values);
240 return backchannel_type;
243 #define BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL "www.onvif.org/ver20/backchannel"
245 #define DEFAULT_LOCATION NULL
246 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
247 #define DEFAULT_DEBUG FALSE
248 #define DEFAULT_RETRY 20
249 #define DEFAULT_TIMEOUT 5000000
250 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
251 #define DEFAULT_TCP_TIMEOUT 20000000
252 #define DEFAULT_LATENCY_MS 2000
253 #define DEFAULT_DROP_ON_LATENCY FALSE
254 #define DEFAULT_CONNECTION_SPEED 0
255 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
256 #define DEFAULT_DO_RTCP TRUE
257 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
258 #define DEFAULT_PROXY NULL
259 #define DEFAULT_RTP_BLOCKSIZE 0
260 #define DEFAULT_USER_ID NULL
261 #define DEFAULT_USER_PW NULL
262 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
263 #define DEFAULT_PORT_RANGE NULL
264 #define DEFAULT_SHORT_HEADER FALSE
265 #define DEFAULT_PROBATION 2
266 #define DEFAULT_UDP_RECONNECT TRUE
267 #define DEFAULT_MULTICAST_IFACE NULL
268 #define DEFAULT_NTP_SYNC FALSE
269 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
270 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
271 #define DEFAULT_TLS_DATABASE NULL
272 #define DEFAULT_TLS_INTERACTION NULL
273 #define DEFAULT_DO_RETRANSMISSION TRUE
274 #define DEFAULT_NTP_TIME_SOURCE NTP_TIME_SOURCE_NTP
275 #define DEFAULT_USER_AGENT "GStreamer/" PACKAGE_VERSION
276 #define DEFAULT_MAX_RTCP_RTP_TIME_DIFF 1000
277 #define DEFAULT_RFC7273_SYNC FALSE
278 #define DEFAULT_MAX_TS_OFFSET_ADJUSTMENT G_GUINT64_CONSTANT(0)
279 #define DEFAULT_MAX_TS_OFFSET G_GINT64_CONSTANT(3000000000)
280 #define DEFAULT_VERSION GST_RTSP_VERSION_1_0
281 #define DEFAULT_BACKCHANNEL GST_RTSP_BACKCHANNEL_NONE
282 #define DEFAULT_TEARDOWN_TIMEOUT (100 * GST_MSECOND)
294 PROP_DROP_ON_LATENCY,
295 PROP_CONNECTION_SPEED,
298 PROP_DO_RTSP_KEEP_ALIVE,
307 PROP_UDP_BUFFER_SIZE,
311 PROP_MULTICAST_IFACE,
313 PROP_USE_PIPELINE_CLOCK,
315 PROP_TLS_VALIDATION_FLAGS,
317 PROP_TLS_INTERACTION,
318 PROP_DO_RETRANSMISSION,
319 PROP_NTP_TIME_SOURCE,
321 PROP_MAX_RTCP_RTP_TIME_DIFF,
323 PROP_MAX_TS_OFFSET_ADJUSTMENT,
325 PROP_DEFAULT_VERSION,
327 PROP_TEARDOWN_TIMEOUT,
330 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
332 gst_rtsp_nat_method_get_type (void)
334 static GType rtsp_nat_method_type = 0;
335 static const GEnumValue rtsp_nat_method[] = {
336 {GST_RTSP_NAT_NONE, "None", "none"},
337 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
341 if (!rtsp_nat_method_type) {
342 rtsp_nat_method_type =
343 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
345 return rtsp_nat_method_type;
348 #define RTSP_SRC_RESPONSE_ERROR(src, response_msg, err_cat, err_code, error_message) \
350 GST_ELEMENT_ERROR_WITH_DETAILS((src), err_cat, err_code, ("%s", error_message), \
351 ("%s (%d)", (response_msg)->type_data.response.reason, (response_msg)->type_data.response.code), \
352 ("rtsp-status-code", G_TYPE_UINT, (response_msg)->type_data.response.code, \
353 "rtsp-status-reason", G_TYPE_STRING, GST_STR_NULL((response_msg)->type_data.response.reason), NULL)); \
356 typedef struct _ParameterRequest
364 static void gst_rtspsrc_finalize (GObject * object);
366 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
367 const GValue * value, GParamSpec * pspec);
368 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
369 GValue * value, GParamSpec * pspec);
371 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
373 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
374 gpointer iface_data);
376 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
377 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
379 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
380 GstStateChange transition);
381 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
382 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
384 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
385 GstRTSPMessage * response);
387 static gboolean gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
389 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
390 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
392 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
393 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
394 gboolean async, const gchar * seek_style);
395 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
396 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
397 gboolean only_close);
399 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
400 const gchar * uri, GError ** error);
401 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
403 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
404 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
405 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
406 GstRTSPStream * stream, GstEvent * event);
407 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
408 static void gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush);
409 static GstRTSPResult gst_rtsp_conninfo_close (GstRTSPSrc * src,
410 GstRTSPConnInfo * info, gboolean free);
412 gst_rtspsrc_print_rtsp_message (GstRTSPSrc * src, const GstRTSPMessage * msg);
414 gst_rtspsrc_print_sdp_message (GstRTSPSrc * src, const GstSDPMessage * msg);
417 gst_rtspsrc_get_parameter (GstRTSPSrc * src, ParameterRequest * req);
420 gst_rtspsrc_set_parameter (GstRTSPSrc * src, ParameterRequest * req);
422 static gboolean get_parameter (GstRTSPSrc * src, const gchar * parameter,
423 const gchar * content_type, GstPromise * promise);
425 static gboolean get_parameters (GstRTSPSrc * src, gchar ** parameters,
426 const gchar * content_type, GstPromise * promise);
428 static gboolean set_parameter (GstRTSPSrc * src, const gchar * name,
429 const gchar * value, const gchar * content_type, GstPromise * promise);
431 static GstFlowReturn gst_rtspsrc_push_backchannel_buffer (GstRTSPSrc * src,
432 guint id, GstSample * sample);
440 /* commands we send to out loop to notify it of events */
441 #define CMD_OPEN (1 << 0)
442 #define CMD_PLAY (1 << 1)
443 #define CMD_PAUSE (1 << 2)
444 #define CMD_CLOSE (1 << 3)
445 #define CMD_WAIT (1 << 4)
446 #define CMD_RECONNECT (1 << 5)
447 #define CMD_LOOP (1 << 6)
448 #define CMD_GET_PARAMETER (1 << 7)
449 #define CMD_SET_PARAMETER (1 << 8)
451 /* mask for all commands */
452 #define CMD_ALL ((CMD_SET_PARAMETER << 1) - 1)
454 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
456 gchar *__txt = _gst_element_error_printf text; \
457 gst_element_post_message (GST_ELEMENT_CAST (el), \
458 gst_message_new_progress (GST_OBJECT_CAST (el), \
459 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
463 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
465 #define gst_rtspsrc_parent_class parent_class
466 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
467 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
469 #ifndef GST_DISABLE_GST_DEBUG
470 static inline const char *
471 cmd_to_string (guint cmd)
488 case CMD_GET_PARAMETER:
489 return "GET_PARAMETER";
490 case CMD_SET_PARAMETER:
491 return "SET_PARAMETER";
499 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
501 GST_DEBUG_OBJECT (src, "default handler");
506 select_stream_accum (GSignalInvocationHint * ihint,
507 GValue * return_accu, const GValue * handler_return, gpointer data)
511 myboolean = g_value_get_boolean (handler_return);
512 GST_DEBUG ("accum %d", myboolean);
513 g_value_set_boolean (return_accu, myboolean);
515 /* stop emission if FALSE */
520 default_before_send (GstRTSPSrc * src, GstRTSPMessage * msg)
522 GST_DEBUG_OBJECT (src, "default handler");
527 before_send_accum (GSignalInvocationHint * ihint,
528 GValue * return_accu, const GValue * handler_return, gpointer data)
532 myboolean = g_value_get_boolean (handler_return);
533 g_value_set_boolean (return_accu, myboolean);
535 /* prevent send if FALSE */
540 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
542 GObjectClass *gobject_class;
543 GstElementClass *gstelement_class;
544 GstBinClass *gstbin_class;
546 gobject_class = (GObjectClass *) klass;
547 gstelement_class = (GstElementClass *) klass;
548 gstbin_class = (GstBinClass *) klass;
550 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
552 gobject_class->set_property = gst_rtspsrc_set_property;
553 gobject_class->get_property = gst_rtspsrc_get_property;
555 gobject_class->finalize = gst_rtspsrc_finalize;
557 g_object_class_install_property (gobject_class, PROP_LOCATION,
558 g_param_spec_string ("location", "RTSP Location",
559 "Location of the RTSP url to read",
560 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
562 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
563 g_param_spec_flags ("protocols", "Protocols",
564 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
565 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
567 g_object_class_install_property (gobject_class, PROP_DEBUG,
568 g_param_spec_boolean ("debug", "Debug",
569 "Dump request and response messages to stdout"
570 "(DEPRECATED: Printed all RTSP message to gstreamer log as 'log' level)",
572 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
574 g_object_class_install_property (gobject_class, PROP_RETRY,
575 g_param_spec_uint ("retry", "Retry",
576 "Max number of retries when allocating RTP ports.",
577 0, G_MAXUINT16, DEFAULT_RETRY,
578 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
580 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
581 g_param_spec_uint64 ("timeout", "Timeout",
582 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
583 0, G_MAXUINT64, DEFAULT_TIMEOUT,
584 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
586 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
587 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
588 "Fail after timeout microseconds on TCP connections (0 = disabled)",
589 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
590 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
592 g_object_class_install_property (gobject_class, PROP_LATENCY,
593 g_param_spec_uint ("latency", "Buffer latency in ms",
594 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
595 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
597 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
598 g_param_spec_boolean ("drop-on-latency",
599 "Drop buffers when maximum latency is reached",
600 "Tells the jitterbuffer to never exceed the given latency in size",
601 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
603 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
604 g_param_spec_uint64 ("connection-speed", "Connection Speed",
605 "Network connection speed in kbps (0 = unknown)",
606 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
607 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
609 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
610 g_param_spec_enum ("nat-method", "NAT Method",
611 "Method to use for traversing firewalls and NAT",
612 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
613 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
616 * GstRTSPSrc:do-rtcp:
618 * Enable RTCP support. Some old server don't like RTCP and then this property
619 * needs to be set to FALSE.
621 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
622 g_param_spec_boolean ("do-rtcp", "Do RTCP",
623 "Send RTCP packets, disable for old incompatible server.",
624 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
627 * GstRTSPSrc:do-rtsp-keep-alive:
629 * Enable RTSP keep alive support. Some old server don't like RTSP
630 * keep alive and then this property needs to be set to FALSE.
632 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
633 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
634 "Send RTSP keep alive packets, disable for old incompatible server.",
635 DEFAULT_DO_RTSP_KEEP_ALIVE,
636 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
641 * Set the proxy parameters. This has to be a string of the format
642 * [http://][user:passwd@]host[:port].
644 g_object_class_install_property (gobject_class, PROP_PROXY,
645 g_param_spec_string ("proxy", "Proxy",
646 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
647 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
649 * GstRTSPSrc:proxy-id:
651 * Sets the proxy URI user id for authentication. If the URI set via the
652 * "proxy" property contains a user-id already, that will take precedence.
656 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
657 g_param_spec_string ("proxy-id", "proxy-id",
658 "HTTP proxy URI user id for authentication", "",
659 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
661 * GstRTSPSrc:proxy-pw:
663 * Sets the proxy URI password for authentication. If the URI set via the
664 * "proxy" property contains a password already, that will take precedence.
668 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
669 g_param_spec_string ("proxy-pw", "proxy-pw",
670 "HTTP proxy URI user password for authentication", "",
671 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
674 * GstRTSPSrc:rtp-blocksize:
676 * RTP package size to suggest to server.
678 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
679 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
680 "RTP package size to suggest to server (0 = disabled)",
681 0, 65536, DEFAULT_RTP_BLOCKSIZE,
682 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
684 g_object_class_install_property (gobject_class,
686 g_param_spec_string ("user-id", "user-id",
687 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
688 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
689 g_object_class_install_property (gobject_class, PROP_USER_PW,
690 g_param_spec_string ("user-pw", "user-pw",
691 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
692 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
695 * GstRTSPSrc:buffer-mode:
697 * Control the buffering and timestamping mode used by the jitterbuffer.
699 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
700 g_param_spec_enum ("buffer-mode", "Buffer Mode",
701 "Control the buffering algorithm in use",
702 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
703 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
706 * GstRTSPSrc:port-range:
708 * Configure the client port numbers that can be used to receive RTP and
711 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
712 g_param_spec_string ("port-range", "Port range",
713 "Client port range that can be used to receive RTP and RTCP data, "
714 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
715 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
718 * GstRTSPSrc:udp-buffer-size:
720 * Size of the kernel UDP receive buffer in bytes.
722 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
723 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
724 "Size of the kernel UDP receive buffer in bytes, 0=default",
725 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
726 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
729 * GstRTSPSrc:short-header:
731 * Only send the basic RTSP headers for broken encoders.
733 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
734 g_param_spec_boolean ("short-header", "Short Header",
735 "Only send the basic RTSP headers for broken encoders",
736 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
738 g_object_class_install_property (gobject_class, PROP_PROBATION,
739 g_param_spec_uint ("probation", "Number of probations",
740 "Consecutive packet sequence numbers to accept the source",
741 0, G_MAXUINT, DEFAULT_PROBATION,
742 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
744 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
745 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
746 "Reconnect to the server if RTSP connection is closed when doing UDP",
747 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
749 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
750 g_param_spec_string ("multicast-iface", "Multicast Interface",
751 "The network interface on which to join the multicast group",
752 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
754 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
755 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
756 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
757 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
759 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
760 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
761 "Use the pipeline running-time to set the NTP time in the RTCP SR messages"
762 "(DEPRECATED: Use ntp-time-source property)",
763 DEFAULT_USE_PIPELINE_CLOCK,
764 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
766 g_object_class_install_property (gobject_class, PROP_SDES,
767 g_param_spec_boxed ("sdes", "SDES",
768 "The SDES items of this session",
769 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
772 * GstRTSPSrc::tls-validation-flags:
774 * TLS certificate validation flags used to validate server
779 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
780 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
781 "TLS certificate validation flags used to validate the server certificate",
782 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
783 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
786 * GstRTSPSrc::tls-database:
788 * TLS database with anchor certificate authorities used to validate
789 * the server certificate.
793 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
794 g_param_spec_object ("tls-database", "TLS database",
795 "TLS database with anchor certificate authorities used to validate the server certificate",
796 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
799 * GstRTSPSrc::tls-interaction:
801 * A #GTlsInteraction object to be used when the connection or certificate
802 * database need to interact with the user. This will be used to prompt the
803 * user for passwords where necessary.
807 g_object_class_install_property (gobject_class, PROP_TLS_INTERACTION,
808 g_param_spec_object ("tls-interaction", "TLS interaction",
809 "A GTlsInteraction object to promt the user for password or certificate",
810 G_TYPE_TLS_INTERACTION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
813 * GstRTSPSrc::do-retransmission:
815 * Attempt to ask the server to retransmit lost packets according to RFC4588.
817 * Note: currently only works with SSRC-multiplexed retransmission streams
821 g_object_class_install_property (gobject_class, PROP_DO_RETRANSMISSION,
822 g_param_spec_boolean ("do-retransmission", "Retransmission",
823 "Ask the server to retransmit lost packets",
824 DEFAULT_DO_RETRANSMISSION,
825 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
828 * GstRTSPSrc::ntp-time-source:
830 * allows to select the time source that should be used
831 * for the NTP time in RTCP packets
835 g_object_class_install_property (gobject_class, PROP_NTP_TIME_SOURCE,
836 g_param_spec_enum ("ntp-time-source", "NTP Time Source",
837 "NTP time source for RTCP packets",
838 GST_TYPE_RTSP_SRC_NTP_TIME_SOURCE, DEFAULT_NTP_TIME_SOURCE,
839 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
842 * GstRTSPSrc::user-agent:
844 * The string to set in the User-Agent header.
848 g_object_class_install_property (gobject_class, PROP_USER_AGENT,
849 g_param_spec_string ("user-agent", "User Agent",
850 "The User-Agent string to send to the server",
851 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
853 g_object_class_install_property (gobject_class, PROP_MAX_RTCP_RTP_TIME_DIFF,
854 g_param_spec_int ("max-rtcp-rtp-time-diff", "Max RTCP RTP Time Diff",
855 "Maximum amount of time in ms that the RTP time in RTCP SRs "
856 "is allowed to be ahead (-1 disabled)", -1, G_MAXINT,
857 DEFAULT_MAX_RTCP_RTP_TIME_DIFF,
858 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
860 g_object_class_install_property (gobject_class, PROP_RFC7273_SYNC,
861 g_param_spec_boolean ("rfc7273-sync", "Sync on RFC7273 clock",
862 "Synchronize received streams to the RFC7273 clock "
863 "(requires clock and offset to be provided)", DEFAULT_RFC7273_SYNC,
864 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
867 * GstRTSPSrc:default-rtsp-version:
869 * The preferred RTSP version to use while negotiating the version with the server.
873 g_object_class_install_property (gobject_class, PROP_DEFAULT_VERSION,
874 g_param_spec_enum ("default-rtsp-version",
875 "The RTSP version to try first",
876 "The RTSP version that should be tried first when negotiating version.",
877 GST_TYPE_RTSP_VERSION, DEFAULT_VERSION,
878 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
881 * GstRTSPSrc:max-ts-offset-adjustment:
883 * Syncing time stamps to NTP time adds a time offset. This parameter
884 * specifies the maximum number of nanoseconds per frame that this time offset
885 * may be adjusted with. This is used to avoid sudden large changes to time
888 g_object_class_install_property (gobject_class, PROP_MAX_TS_OFFSET_ADJUSTMENT,
889 g_param_spec_uint64 ("max-ts-offset-adjustment",
890 "Max Timestamp Offset Adjustment",
891 "The maximum number of nanoseconds per frame that time stamp offsets "
892 "may be adjusted (0 = no limit).", 0, G_MAXUINT64,
893 DEFAULT_MAX_TS_OFFSET_ADJUSTMENT, G_PARAM_READWRITE |
894 G_PARAM_STATIC_STRINGS));
897 * GstRTSPSrc:max-ts-offset:
899 * Used to set an upper limit of how large a time offset may be. This
900 * is used to protect against unrealistic values as a result of either
901 * client,server or clock issues.
903 g_object_class_install_property (gobject_class, PROP_MAX_TS_OFFSET,
904 g_param_spec_int64 ("max-ts-offset", "Max TS Offset",
905 "The maximum absolute value of the time offset in (nanoseconds). "
906 "Note, if the ntp-sync parameter is set the default value is "
907 "changed to 0 (no limit)", 0, G_MAXINT64, DEFAULT_MAX_TS_OFFSET,
908 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
911 * GstRTSPSrc:backchannel
913 * Select a type of backchannel to setup with the RTSP server.
914 * Default value is "none". Allowed values are "none" and "onvif".
918 g_object_class_install_property (gobject_class, PROP_BACKCHANNEL,
919 g_param_spec_enum ("backchannel", "Backchannel type",
920 "The type of backchannel to setup. Default is 'none'.",
921 GST_TYPE_RTSP_BACKCHANNEL, BACKCHANNEL_NONE,
922 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
925 * GstRtspSrc:teardown-timeout
927 * When transitioning PAUSED-READY, allow up to timeout (in nanoseconds)
928 * delay in order to send teardown (0 = disabled)
932 g_object_class_install_property (gobject_class, PROP_TEARDOWN_TIMEOUT,
933 g_param_spec_uint64 ("teardown-timeout", "Teardown Timeout",
934 "When transitioning PAUSED-READY, allow up to timeout (in nanoseconds) "
935 "delay in order to send teardown (0 = disabled)",
936 0, G_MAXUINT64, DEFAULT_TEARDOWN_TIMEOUT,
937 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
940 * GstRTSPSrc::handle-request:
941 * @rtspsrc: a #GstRTSPSrc
942 * @request: a #GstRTSPMessage
943 * @response: a #GstRTSPMessage
945 * Handle a server request in @request and prepare @response.
947 * This signal is called from the streaming thread, you should therefore not
948 * do any state changes on @rtspsrc because this might deadlock. If you want
949 * to modify the state as a result of this signal, post a
950 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
955 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
956 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
957 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
958 G_TYPE_POINTER, G_TYPE_POINTER);
961 * GstRTSPSrc::on-sdp:
962 * @rtspsrc: a #GstRTSPSrc
963 * @sdp: a #GstSDPMessage
965 * Emitted when the client has retrieved the SDP and before it configures the
966 * streams in the SDP. @sdp can be inspected and modified.
968 * This signal is called from the streaming thread, you should therefore not
969 * do any state changes on @rtspsrc because this might deadlock. If you want
970 * to modify the state as a result of this signal, post a
971 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
976 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
977 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
978 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
979 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
982 * GstRTSPSrc::select-stream:
983 * @rtspsrc: a #GstRTSPSrc
984 * @num: the stream number
985 * @caps: the stream caps
987 * Emitted before the client decides to configure the stream @num with
990 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
995 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
996 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
997 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
998 (GCallback) default_select_stream, select_stream_accum, NULL,
999 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, G_TYPE_UINT,
1002 * GstRTSPSrc::new-manager:
1003 * @rtspsrc: a #GstRTSPSrc
1004 * @manager: a #GstElement
1006 * Emitted after a new manager (like rtpbin) was created and the default
1007 * properties were configured.
1011 gst_rtspsrc_signals[SIGNAL_NEW_MANAGER] =
1012 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
1013 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
1014 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
1017 * GstRTSPSrc::request-rtcp-key:
1018 * @rtspsrc: a #GstRTSPSrc
1019 * @num: the stream number
1021 * Signal emitted to get the crypto parameters relevant to the RTCP
1022 * stream. User should provide the key and the RTCP encryption ciphers
1023 * and authentication, and return them wrapped in a GstCaps.
1027 gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY] =
1028 g_signal_new ("request-rtcp-key", G_TYPE_FROM_CLASS (klass),
1029 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, GST_TYPE_CAPS, 1, G_TYPE_UINT);
1032 * GstRTSPSrc::accept-certificate:
1033 * @rtspsrc: a #GstRTSPSrc
1034 * @peer_cert: the peer's #GTlsCertificate
1035 * @errors: the problems with @peer_cert
1036 * @user_data: user data set when the signal handler was connected.
1038 * This will directly map to #GTlsConnection 's "accept-certificate"
1039 * signal and be performed after the default checks of #GstRTSPConnection
1040 * (checking against the #GTlsDatabase with the given #GTlsCertificateFlags)
1041 * have failed. If no #GTlsDatabase is set on this connection, only this
1042 * signal will be emitted.
1046 gst_rtspsrc_signals[SIGNAL_ACCEPT_CERTIFICATE] =
1047 g_signal_new ("accept-certificate", G_TYPE_FROM_CLASS (klass),
1048 G_SIGNAL_RUN_LAST, 0, g_signal_accumulator_true_handled, NULL, NULL,
1049 G_TYPE_BOOLEAN, 3, G_TYPE_TLS_CONNECTION, G_TYPE_TLS_CERTIFICATE,
1050 G_TYPE_TLS_CERTIFICATE_FLAGS);
1053 * GstRTSPSrc::before-send
1054 * @rtspsrc: a #GstRTSPSrc
1055 * @num: the stream number
1057 * Emitted before each RTSP request is sent, in order to allow
1058 * the application to modify send parameters or to skip the message entirely.
1059 * This can be used, for example, to work with ONVIF Profile G servers,
1060 * which need a different/additional range, rate-control, and intra/x
1063 * Returns: %TRUE when the command should be sent, %FALSE when the
1064 * command should be dropped.
1068 gst_rtspsrc_signals[SIGNAL_BEFORE_SEND] =
1069 g_signal_new_class_handler ("before-send", G_TYPE_FROM_CLASS (klass),
1070 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
1071 (GCallback) default_before_send, before_send_accum, NULL,
1072 g_cclosure_marshal_generic, G_TYPE_BOOLEAN,
1073 1, GST_TYPE_RTSP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
1076 * GstRTSPSrc::push-backchannel-buffer:
1077 * @rtspsrc: a #GstRTSPSrc
1078 * @buffer: RTP buffer to send back
1082 gst_rtspsrc_signals[SIGNAL_PUSH_BACKCHANNEL_BUFFER] =
1083 g_signal_new ("push-backchannel-buffer", G_TYPE_FROM_CLASS (klass),
1084 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRTSPSrcClass,
1085 push_backchannel_buffer), NULL, NULL, NULL, GST_TYPE_FLOW_RETURN, 2,
1086 G_TYPE_UINT, GST_TYPE_BUFFER);
1089 * GstRTSPSrc::get-parameter:
1090 * @rtspsrc: a #GstRTSPSrc
1091 * @parameter: the parameter name
1092 * @parameter: the content type
1093 * @parameter: a pointer to #GstPromise
1095 * Handle the GET_PARAMETER signal.
1097 * Returns: %TRUE when the command could be issued, %FALSE otherwise
1100 gst_rtspsrc_signals[SIGNAL_GET_PARAMETER] =
1101 g_signal_new ("get-parameter", G_TYPE_FROM_CLASS (klass),
1102 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRTSPSrcClass,
1103 get_parameter), NULL, NULL, g_cclosure_marshal_generic,
1104 G_TYPE_BOOLEAN, 3, G_TYPE_STRING, G_TYPE_STRING, GST_TYPE_PROMISE);
1107 * GstRTSPSrc::get-parameters:
1108 * @rtspsrc: a #GstRTSPSrc
1109 * @parameter: a NULL-terminated array of parameters
1110 * @parameter: the content type
1111 * @parameter: a pointer to #GstPromise
1113 * Handle the GET_PARAMETERS signal.
1115 * Returns: %TRUE when the command could be issued, %FALSE otherwise
1118 gst_rtspsrc_signals[SIGNAL_GET_PARAMETERS] =
1119 g_signal_new ("get-parameters", G_TYPE_FROM_CLASS (klass),
1120 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRTSPSrcClass,
1121 get_parameters), NULL, NULL, g_cclosure_marshal_generic,
1122 G_TYPE_BOOLEAN, 3, G_TYPE_STRV, G_TYPE_STRING, GST_TYPE_PROMISE);
1125 * GstRTSPSrc::set-parameter:
1126 * @rtspsrc: a #GstRTSPSrc
1127 * @parameter: the parameter name
1128 * @parameter: the parameter value
1129 * @parameter: the content type
1130 * @parameter: a pointer to #GstPromise
1132 * Handle the SET_PARAMETER signal.
1134 * Returns: %TRUE when the command could be issued, %FALSE otherwise
1137 gst_rtspsrc_signals[SIGNAL_SET_PARAMETER] =
1138 g_signal_new ("set-parameter", G_TYPE_FROM_CLASS (klass),
1139 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRTSPSrcClass,
1140 set_parameter), NULL, NULL, g_cclosure_marshal_generic,
1141 G_TYPE_BOOLEAN, 4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
1144 gstelement_class->send_event = gst_rtspsrc_send_event;
1145 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
1146 gstelement_class->change_state = gst_rtspsrc_change_state;
1148 gst_element_class_add_static_pad_template (gstelement_class, &rtptemplate);
1150 gst_element_class_set_static_metadata (gstelement_class,
1151 "RTSP packet receiver", "Source/Network",
1152 "Receive data over the network via RTSP (RFC 2326)",
1153 "Wim Taymans <wim@fluendo.com>, "
1154 "Thijs Vermeir <thijs.vermeir@barco.com>, "
1155 "Lutz Mueller <lutz@topfrose.de>");
1157 gstbin_class->handle_message = gst_rtspsrc_handle_message;
1159 klass->push_backchannel_buffer = gst_rtspsrc_push_backchannel_buffer;
1160 klass->get_parameter = GST_DEBUG_FUNCPTR (get_parameter);
1161 klass->get_parameters = GST_DEBUG_FUNCPTR (get_parameters);
1162 klass->set_parameter = GST_DEBUG_FUNCPTR (set_parameter);
1164 gst_rtsp_ext_list_init ();
1168 validate_set_get_parameter_name (const gchar * parameter_name)
1170 gchar *ptr = (gchar *) parameter_name;
1173 /* Don't allow '\r', '\n', \'t', ' ' etc in the parameter name */
1174 if (g_ascii_isspace (*ptr) || g_ascii_iscntrl (*ptr)) {
1175 GST_DEBUG ("invalid parameter name '%s'", parameter_name);
1184 validate_set_get_parameters (gchar ** parameter_names)
1186 while (*parameter_names) {
1187 if (!validate_set_get_parameter_name (*parameter_names)) {
1196 get_parameter (GstRTSPSrc * src, const gchar * parameter,
1197 const gchar * content_type, GstPromise * promise)
1199 gchar *parameters[] = { (gchar *) parameter, NULL };
1201 GST_LOG_OBJECT (src, "get_parameter: %s", GST_STR_NULL (parameter));
1203 if (parameter == NULL || parameter[0] == '\0' || promise == NULL) {
1204 GST_DEBUG ("invalid input");
1208 return get_parameters (src, parameters, content_type, promise);
1212 get_parameters (GstRTSPSrc * src, gchar ** parameters,
1213 const gchar * content_type, GstPromise * promise)
1215 ParameterRequest *req;
1217 GST_LOG_OBJECT (src, "get_parameters: %d", g_strv_length (parameters));
1219 if (parameters == NULL || promise == NULL) {
1220 GST_DEBUG ("invalid input");
1224 if (src->state == GST_RTSP_STATE_INVALID) {
1225 GST_DEBUG ("invalid state");
1229 if (!validate_set_get_parameters (parameters)) {
1233 req = g_new0 (ParameterRequest, 1);
1234 req->promise = gst_promise_ref (promise);
1235 req->cmd = CMD_GET_PARAMETER;
1236 /* Set the request body according to RFC 2326 or RFC 7826 */
1237 req->body = g_string_new (NULL);
1238 while (*parameters) {
1239 g_string_append_printf (req->body, "%s:\r\n", *parameters);
1243 req->content_type = g_strdup (content_type);
1245 GST_OBJECT_LOCK (src);
1246 g_queue_push_tail (&src->set_get_param_q, req);
1247 GST_OBJECT_UNLOCK (src);
1249 gst_rtspsrc_loop_send_cmd (src, CMD_GET_PARAMETER, CMD_LOOP);
1255 set_parameter (GstRTSPSrc * src, const gchar * name, const gchar * value,
1256 const gchar * content_type, GstPromise * promise)
1258 ParameterRequest *req;
1260 GST_LOG_OBJECT (src, "set_parameter: %s: %s", GST_STR_NULL (name),
1261 GST_STR_NULL (value));
1263 if (name == NULL || name[0] == '\0' || value == NULL || promise == NULL) {
1264 GST_DEBUG ("invalid input");
1268 if (src->state == GST_RTSP_STATE_INVALID) {
1269 GST_DEBUG ("invalid state");
1273 if (!validate_set_get_parameter_name (name)) {
1277 req = g_new0 (ParameterRequest, 1);
1278 req->cmd = CMD_SET_PARAMETER;
1279 req->promise = gst_promise_ref (promise);
1280 req->body = g_string_new (NULL);
1281 /* Set the request body according to RFC 2326 or RFC 7826 */
1282 g_string_append_printf (req->body, "%s: %s\r\n", name, value);
1284 req->content_type = g_strdup (content_type);
1286 GST_OBJECT_LOCK (src);
1287 g_queue_push_tail (&src->set_get_param_q, req);
1288 GST_OBJECT_UNLOCK (src);
1290 gst_rtspsrc_loop_send_cmd (src, CMD_SET_PARAMETER, CMD_LOOP);
1296 gst_rtspsrc_init (GstRTSPSrc * src)
1298 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
1299 src->protocols = DEFAULT_PROTOCOLS;
1300 src->debug = DEFAULT_DEBUG;
1301 src->retry = DEFAULT_RETRY;
1302 src->udp_timeout = DEFAULT_TIMEOUT;
1303 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
1304 src->latency = DEFAULT_LATENCY_MS;
1305 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
1306 src->connection_speed = DEFAULT_CONNECTION_SPEED;
1307 src->nat_method = DEFAULT_NAT_METHOD;
1308 src->do_rtcp = DEFAULT_DO_RTCP;
1309 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
1310 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
1311 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
1312 src->user_id = g_strdup (DEFAULT_USER_ID);
1313 src->user_pw = g_strdup (DEFAULT_USER_PW);
1314 src->buffer_mode = DEFAULT_BUFFER_MODE;
1315 src->client_port_range.min = 0;
1316 src->client_port_range.max = 0;
1317 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
1318 src->short_header = DEFAULT_SHORT_HEADER;
1319 src->probation = DEFAULT_PROBATION;
1320 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
1321 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1322 src->ntp_sync = DEFAULT_NTP_SYNC;
1323 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
1325 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
1326 src->tls_database = DEFAULT_TLS_DATABASE;
1327 src->tls_interaction = DEFAULT_TLS_INTERACTION;
1328 src->do_retransmission = DEFAULT_DO_RETRANSMISSION;
1329 src->ntp_time_source = DEFAULT_NTP_TIME_SOURCE;
1330 src->user_agent = g_strdup (DEFAULT_USER_AGENT);
1331 src->max_rtcp_rtp_time_diff = DEFAULT_MAX_RTCP_RTP_TIME_DIFF;
1332 src->rfc7273_sync = DEFAULT_RFC7273_SYNC;
1333 src->max_ts_offset_adjustment = DEFAULT_MAX_TS_OFFSET_ADJUSTMENT;
1334 src->max_ts_offset = DEFAULT_MAX_TS_OFFSET;
1335 src->max_ts_offset_is_set = FALSE;
1336 src->default_version = DEFAULT_VERSION;
1337 src->version = GST_RTSP_VERSION_INVALID;
1338 src->teardown_timeout = DEFAULT_TEARDOWN_TIMEOUT;
1340 /* get a list of all extensions */
1341 src->extensions = gst_rtsp_ext_list_get ();
1343 /* connect to send signal */
1344 gst_rtsp_ext_list_connect (src->extensions, "send",
1345 (GCallback) gst_rtspsrc_send_cb, src);
1347 /* protects the streaming thread in interleaved mode or the polling
1348 * thread in UDP mode. */
1349 g_rec_mutex_init (&src->stream_rec_lock);
1351 /* protects our state changes from multiple invocations */
1352 g_rec_mutex_init (&src->state_rec_lock);
1354 g_queue_init (&src->set_get_param_q);
1356 src->state = GST_RTSP_STATE_INVALID;
1358 g_mutex_init (&src->conninfo.send_lock);
1359 g_mutex_init (&src->conninfo.recv_lock);
1360 g_cond_init (&src->cmd_cond);
1362 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
1363 gst_bin_set_suppressed_flags (GST_BIN (src),
1364 GST_ELEMENT_FLAG_SOURCE | GST_ELEMENT_FLAG_SINK);
1368 free_param_data (ParameterRequest * req)
1370 gst_promise_unref (req->promise);
1372 g_string_free (req->body, TRUE);
1373 g_free (req->content_type);
1378 free_param_queue (gpointer data)
1380 ParameterRequest *req = data;
1382 gst_promise_expire (req->promise);
1383 free_param_data (req);
1387 gst_rtspsrc_finalize (GObject * object)
1389 GstRTSPSrc *rtspsrc;
1391 rtspsrc = GST_RTSPSRC (object);
1393 gst_rtsp_ext_list_free (rtspsrc->extensions);
1394 g_free (rtspsrc->conninfo.location);
1395 gst_rtsp_url_free (rtspsrc->conninfo.url);
1396 g_free (rtspsrc->conninfo.url_str);
1397 g_free (rtspsrc->user_id);
1398 g_free (rtspsrc->user_pw);
1399 g_free (rtspsrc->multi_iface);
1400 g_free (rtspsrc->user_agent);
1403 gst_sdp_message_free (rtspsrc->sdp);
1404 rtspsrc->sdp = NULL;
1406 if (rtspsrc->provided_clock)
1407 gst_object_unref (rtspsrc->provided_clock);
1410 gst_structure_free (rtspsrc->sdes);
1412 if (rtspsrc->tls_database)
1413 g_object_unref (rtspsrc->tls_database);
1415 if (rtspsrc->tls_interaction)
1416 g_object_unref (rtspsrc->tls_interaction);
1419 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
1420 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
1422 g_mutex_clear (&rtspsrc->conninfo.send_lock);
1423 g_mutex_clear (&rtspsrc->conninfo.recv_lock);
1424 g_cond_clear (&rtspsrc->cmd_cond);
1426 G_OBJECT_CLASS (parent_class)->finalize (object);
1430 gst_rtspsrc_provide_clock (GstElement * element)
1432 GstRTSPSrc *src = GST_RTSPSRC (element);
1435 if ((clock = src->provided_clock) != NULL)
1436 return gst_object_ref (clock);
1438 return GST_ELEMENT_CLASS (parent_class)->provide_clock (element);
1441 /* a proxy string of the format [user:passwd@]host[:port] */
1443 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
1445 gchar *p, *at, *col;
1447 g_free (rtsp->proxy_user);
1448 rtsp->proxy_user = NULL;
1449 g_free (rtsp->proxy_passwd);
1450 rtsp->proxy_passwd = NULL;
1451 g_free (rtsp->proxy_host);
1452 rtsp->proxy_host = NULL;
1453 rtsp->proxy_port = 0;
1455 p = (gchar *) proxy;
1460 /* we allow http:// in front but ignore it */
1461 if (g_str_has_prefix (p, "http://"))
1464 at = strchr (p, '@');
1466 /* look for user:passwd */
1467 col = strchr (proxy, ':');
1468 if (col == NULL || col > at)
1471 rtsp->proxy_user = g_strndup (p, col - p);
1473 rtsp->proxy_passwd = g_strndup (col, at - col);
1478 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
1479 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
1480 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
1481 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
1482 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
1483 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
1484 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
1487 col = strchr (p, ':');
1490 /* everything before the colon is the hostname */
1491 rtsp->proxy_host = g_strndup (p, col - p);
1493 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
1495 rtsp->proxy_host = g_strdup (p);
1496 rtsp->proxy_port = 8080;
1502 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
1504 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
1505 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
1508 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
1510 rtspsrc->ptcp_timeout = NULL;
1514 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
1517 GstRTSPSrc *rtspsrc;
1519 rtspsrc = GST_RTSPSRC (object);
1523 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
1524 g_value_get_string (value), NULL);
1526 case PROP_PROTOCOLS:
1527 rtspsrc->protocols = g_value_get_flags (value);
1530 rtspsrc->debug = g_value_get_boolean (value);
1533 rtspsrc->retry = g_value_get_uint (value);
1536 rtspsrc->udp_timeout = g_value_get_uint64 (value);
1538 case PROP_TCP_TIMEOUT:
1539 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
1542 rtspsrc->latency = g_value_get_uint (value);
1544 case PROP_DROP_ON_LATENCY:
1545 rtspsrc->drop_on_latency = g_value_get_boolean (value);
1547 case PROP_CONNECTION_SPEED:
1548 rtspsrc->connection_speed = g_value_get_uint64 (value);
1550 case PROP_NAT_METHOD:
1551 rtspsrc->nat_method = g_value_get_enum (value);
1554 rtspsrc->do_rtcp = g_value_get_boolean (value);
1556 case PROP_DO_RTSP_KEEP_ALIVE:
1557 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
1560 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
1563 g_free (rtspsrc->prop_proxy_id);
1564 rtspsrc->prop_proxy_id = g_value_dup_string (value);
1567 g_free (rtspsrc->prop_proxy_pw);
1568 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
1570 case PROP_RTP_BLOCKSIZE:
1571 rtspsrc->rtp_blocksize = g_value_get_uint (value);
1574 g_free (rtspsrc->user_id);
1575 rtspsrc->user_id = g_value_dup_string (value);
1578 g_free (rtspsrc->user_pw);
1579 rtspsrc->user_pw = g_value_dup_string (value);
1581 case PROP_BUFFER_MODE:
1582 rtspsrc->buffer_mode = g_value_get_enum (value);
1584 case PROP_PORT_RANGE:
1588 str = g_value_get_string (value);
1589 if (str == NULL || sscanf (str, "%u-%u", &rtspsrc->client_port_range.min,
1590 &rtspsrc->client_port_range.max) != 2) {
1591 rtspsrc->client_port_range.min = 0;
1592 rtspsrc->client_port_range.max = 0;
1596 case PROP_UDP_BUFFER_SIZE:
1597 rtspsrc->udp_buffer_size = g_value_get_int (value);
1599 case PROP_SHORT_HEADER:
1600 rtspsrc->short_header = g_value_get_boolean (value);
1602 case PROP_PROBATION:
1603 rtspsrc->probation = g_value_get_uint (value);
1605 case PROP_UDP_RECONNECT:
1606 rtspsrc->udp_reconnect = g_value_get_boolean (value);
1608 case PROP_MULTICAST_IFACE:
1609 g_free (rtspsrc->multi_iface);
1611 if (g_value_get_string (value) == NULL)
1612 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1614 rtspsrc->multi_iface = g_value_dup_string (value);
1617 rtspsrc->ntp_sync = g_value_get_boolean (value);
1618 /* The default value of max_ts_offset depends on ntp_sync. If user
1619 * hasn't set it then change default value */
1620 if (!rtspsrc->max_ts_offset_is_set) {
1621 if (rtspsrc->ntp_sync) {
1622 rtspsrc->max_ts_offset = 0;
1624 rtspsrc->max_ts_offset = DEFAULT_MAX_TS_OFFSET;
1628 case PROP_USE_PIPELINE_CLOCK:
1629 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
1632 rtspsrc->sdes = g_value_dup_boxed (value);
1634 case PROP_TLS_VALIDATION_FLAGS:
1635 rtspsrc->tls_validation_flags = g_value_get_flags (value);
1637 case PROP_TLS_DATABASE:
1638 g_clear_object (&rtspsrc->tls_database);
1639 rtspsrc->tls_database = g_value_dup_object (value);
1641 case PROP_TLS_INTERACTION:
1642 g_clear_object (&rtspsrc->tls_interaction);
1643 rtspsrc->tls_interaction = g_value_dup_object (value);
1645 case PROP_DO_RETRANSMISSION:
1646 rtspsrc->do_retransmission = g_value_get_boolean (value);
1648 case PROP_NTP_TIME_SOURCE:
1649 rtspsrc->ntp_time_source = g_value_get_enum (value);
1651 case PROP_USER_AGENT:
1652 g_free (rtspsrc->user_agent);
1653 rtspsrc->user_agent = g_value_dup_string (value);
1655 case PROP_MAX_RTCP_RTP_TIME_DIFF:
1656 rtspsrc->max_rtcp_rtp_time_diff = g_value_get_int (value);
1658 case PROP_RFC7273_SYNC:
1659 rtspsrc->rfc7273_sync = g_value_get_boolean (value);
1661 case PROP_MAX_TS_OFFSET_ADJUSTMENT:
1662 rtspsrc->max_ts_offset_adjustment = g_value_get_uint64 (value);
1664 case PROP_MAX_TS_OFFSET:
1665 rtspsrc->max_ts_offset = g_value_get_int64 (value);
1666 rtspsrc->max_ts_offset_is_set = TRUE;
1668 case PROP_DEFAULT_VERSION:
1669 rtspsrc->default_version = g_value_get_enum (value);
1671 case PROP_BACKCHANNEL:
1672 rtspsrc->backchannel = g_value_get_enum (value);
1674 case PROP_TEARDOWN_TIMEOUT:
1675 rtspsrc->teardown_timeout = g_value_get_uint64 (value);
1678 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1684 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
1687 GstRTSPSrc *rtspsrc;
1689 rtspsrc = GST_RTSPSRC (object);
1693 g_value_set_string (value, rtspsrc->conninfo.location);
1695 case PROP_PROTOCOLS:
1696 g_value_set_flags (value, rtspsrc->protocols);
1699 g_value_set_boolean (value, rtspsrc->debug);
1702 g_value_set_uint (value, rtspsrc->retry);
1705 g_value_set_uint64 (value, rtspsrc->udp_timeout);
1707 case PROP_TCP_TIMEOUT:
1711 timeout = ((guint64) rtspsrc->tcp_timeout.tv_sec) * G_USEC_PER_SEC +
1712 rtspsrc->tcp_timeout.tv_usec;
1713 g_value_set_uint64 (value, timeout);
1717 g_value_set_uint (value, rtspsrc->latency);
1719 case PROP_DROP_ON_LATENCY:
1720 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1722 case PROP_CONNECTION_SPEED:
1723 g_value_set_uint64 (value, rtspsrc->connection_speed);
1725 case PROP_NAT_METHOD:
1726 g_value_set_enum (value, rtspsrc->nat_method);
1729 g_value_set_boolean (value, rtspsrc->do_rtcp);
1731 case PROP_DO_RTSP_KEEP_ALIVE:
1732 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1738 if (rtspsrc->proxy_host) {
1740 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1744 g_value_take_string (value, str);
1748 g_value_set_string (value, rtspsrc->prop_proxy_id);
1751 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1753 case PROP_RTP_BLOCKSIZE:
1754 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1757 g_value_set_string (value, rtspsrc->user_id);
1760 g_value_set_string (value, rtspsrc->user_pw);
1762 case PROP_BUFFER_MODE:
1763 g_value_set_enum (value, rtspsrc->buffer_mode);
1765 case PROP_PORT_RANGE:
1769 if (rtspsrc->client_port_range.min != 0) {
1770 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1771 rtspsrc->client_port_range.max);
1775 g_value_take_string (value, str);
1778 case PROP_UDP_BUFFER_SIZE:
1779 g_value_set_int (value, rtspsrc->udp_buffer_size);
1781 case PROP_SHORT_HEADER:
1782 g_value_set_boolean (value, rtspsrc->short_header);
1784 case PROP_PROBATION:
1785 g_value_set_uint (value, rtspsrc->probation);
1787 case PROP_UDP_RECONNECT:
1788 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1790 case PROP_MULTICAST_IFACE:
1791 g_value_set_string (value, rtspsrc->multi_iface);
1794 g_value_set_boolean (value, rtspsrc->ntp_sync);
1796 case PROP_USE_PIPELINE_CLOCK:
1797 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1800 g_value_set_boxed (value, rtspsrc->sdes);
1802 case PROP_TLS_VALIDATION_FLAGS:
1803 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1805 case PROP_TLS_DATABASE:
1806 g_value_set_object (value, rtspsrc->tls_database);
1808 case PROP_TLS_INTERACTION:
1809 g_value_set_object (value, rtspsrc->tls_interaction);
1811 case PROP_DO_RETRANSMISSION:
1812 g_value_set_boolean (value, rtspsrc->do_retransmission);
1814 case PROP_NTP_TIME_SOURCE:
1815 g_value_set_enum (value, rtspsrc->ntp_time_source);
1817 case PROP_USER_AGENT:
1818 g_value_set_string (value, rtspsrc->user_agent);
1820 case PROP_MAX_RTCP_RTP_TIME_DIFF:
1821 g_value_set_int (value, rtspsrc->max_rtcp_rtp_time_diff);
1823 case PROP_RFC7273_SYNC:
1824 g_value_set_boolean (value, rtspsrc->rfc7273_sync);
1826 case PROP_MAX_TS_OFFSET_ADJUSTMENT:
1827 g_value_set_uint64 (value, rtspsrc->max_ts_offset_adjustment);
1829 case PROP_MAX_TS_OFFSET:
1830 g_value_set_int64 (value, rtspsrc->max_ts_offset);
1832 case PROP_DEFAULT_VERSION:
1833 g_value_set_enum (value, rtspsrc->default_version);
1835 case PROP_BACKCHANNEL:
1836 g_value_set_enum (value, rtspsrc->backchannel);
1838 case PROP_TEARDOWN_TIMEOUT:
1839 g_value_set_uint64 (value, rtspsrc->teardown_timeout);
1842 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1848 find_stream_by_id (GstRTSPStream * stream, gint * id)
1850 if (stream->id == *id)
1857 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1859 /* ignore unconfigured channels here (e.g., those that
1860 * were explicitly skipped during SETUP) */
1861 if ((stream->channelpad[0] != NULL) &&
1862 (stream->channel[0] == *channel || stream->channel[1] == *channel))
1869 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1871 GstElement *src = (GstElement *) a;
1873 if (stream->udpsrc[0] == src)
1875 if (stream->udpsrc[1] == src)
1882 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1884 if (stream->conninfo.location) {
1885 /* check qualified setup_url */
1886 if (!strcmp (stream->conninfo.location, (gchar *) a))
1889 if (stream->control_url) {
1890 /* check original control_url */
1891 if (!strcmp (stream->control_url, (gchar *) a))
1894 /* check if qualified setup_url ends with string */
1895 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1902 static GstRTSPStream *
1903 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1907 /* find and get stream */
1908 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1909 return (GstRTSPStream *) lstream->data;
1914 static const GstSDPBandwidth *
1915 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1916 const GstSDPMedia * media, const gchar * type)
1920 /* first look in the media specific section */
1921 len = gst_sdp_media_bandwidths_len (media);
1922 for (i = 0; i < len; i++) {
1923 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1925 if (strcmp (bw->bwtype, type) == 0)
1928 /* then look in the message specific section */
1929 len = gst_sdp_message_bandwidths_len (sdp);
1930 for (i = 0; i < len; i++) {
1931 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1933 if (strcmp (bw->bwtype, type) == 0)
1940 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1941 const GstSDPMedia * media, GstRTSPStream * stream)
1943 const GstSDPBandwidth *bw;
1945 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1946 stream->as_bandwidth = bw->bandwidth;
1948 stream->as_bandwidth = -1;
1950 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1951 stream->rr_bandwidth = bw->bandwidth;
1953 stream->rr_bandwidth = -1;
1955 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1956 stream->rs_bandwidth = bw->bandwidth;
1958 stream->rs_bandwidth = -1;
1962 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1963 const GstSDPConnection * conn)
1965 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1968 if (conn->addrtype == NULL)
1971 /* check for IPV6 */
1972 if (strcmp (conn->addrtype, "IP4") == 0)
1973 stream->is_ipv6 = FALSE;
1974 else if (strcmp (conn->addrtype, "IP6") == 0)
1975 stream->is_ipv6 = TRUE;
1980 g_free (stream->destination);
1981 stream->destination = g_strdup (conn->address);
1983 /* check for multicast */
1984 stream->is_multicast =
1985 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1987 stream->ttl = conn->ttl;
1990 /* Go over the connections for a stream.
1991 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1993 * - If we are dealing with a localhost address, we disable multicast
1996 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1997 const GstSDPMedia * media, GstRTSPStream * stream)
1999 const GstSDPConnection *conn;
2002 /* first look in the media specific section */
2003 len = gst_sdp_media_connections_len (media);
2004 for (i = 0; i < len; i++) {
2005 conn = gst_sdp_media_get_connection (media, i);
2007 gst_rtspsrc_do_stream_connection (src, stream, conn);
2009 /* then look in the message specific section */
2010 if ((conn = gst_sdp_message_get_connection (sdp))) {
2011 gst_rtspsrc_do_stream_connection (src, stream, conn);
2016 make_stream_id (GstRTSPStream * stream, const GstSDPMedia * media)
2019 g_strdup_printf ("%s:%d:%d:%s:%d", media->media, media->port,
2020 media->num_ports, media->proto, stream->default_pt);
2022 g_strcanon (stream_id, G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS, ':');
2027 /* m=<media> <UDP port> RTP/AVP <payload>
2030 gst_rtspsrc_collect_payloads (GstRTSPSrc * src, const GstSDPMessage * sdp,
2031 const GstSDPMedia * media, GstRTSPStream * stream)
2035 GstCaps *global_caps;
2038 proto = gst_sdp_media_get_proto (media);
2042 if (g_str_equal (proto, "RTP/AVP"))
2043 stream->profile = GST_RTSP_PROFILE_AVP;
2044 else if (g_str_equal (proto, "RTP/SAVP"))
2045 stream->profile = GST_RTSP_PROFILE_SAVP;
2046 else if (g_str_equal (proto, "RTP/AVPF"))
2047 stream->profile = GST_RTSP_PROFILE_AVPF;
2048 else if (g_str_equal (proto, "RTP/SAVPF"))
2049 stream->profile = GST_RTSP_PROFILE_SAVPF;
2053 if (gst_sdp_media_get_attribute_val (media, "sendonly") != NULL &&
2054 /* We want to setup caps for streams configured as backchannel */
2055 !stream->is_backchannel && src->backchannel != BACKCHANNEL_NONE)
2056 goto sendonly_media;
2058 /* Parse global SDP attributes once */
2059 global_caps = gst_caps_new_empty_simple ("application/x-unknown");
2060 GST_DEBUG ("mapping sdp session level attributes to caps");
2061 gst_sdp_message_attributes_to_caps (sdp, global_caps);
2062 GST_DEBUG ("mapping sdp media level attributes to caps");
2063 gst_sdp_media_attributes_to_caps (media, global_caps);
2065 /* Keep a copy of the SDP key management */
2066 gst_sdp_media_parse_keymgmt (media, &stream->mikey);
2067 if (stream->mikey == NULL)
2068 gst_sdp_message_parse_keymgmt (sdp, &stream->mikey);
2070 len = gst_sdp_media_formats_len (media);
2071 for (i = 0; i < len; i++) {
2073 GstCaps *caps, *outcaps;
2078 pt = atoi (gst_sdp_media_get_format (media, i));
2080 GST_DEBUG_OBJECT (src, " looking at %d pt: %d", i, pt);
2083 caps = gst_sdp_media_get_caps_from_media (media, pt);
2085 GST_WARNING_OBJECT (src, " skipping pt %d without caps", pt);
2089 /* do some tweaks */
2090 s = gst_caps_get_structure (caps, 0);
2091 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
2092 stream->is_real = (strstr (enc, "-REAL") != NULL);
2093 if (strcmp (enc, "X-ASF-PF") == 0)
2094 stream->container = TRUE;
2097 /* Merge in global caps */
2098 /* Intersect will merge in missing fields to the current caps */
2099 outcaps = gst_caps_intersect (caps, global_caps);
2100 gst_caps_unref (caps);
2102 /* the first pt will be the default */
2103 if (stream->ptmap->len == 0)
2104 stream->default_pt = pt;
2107 item.caps = outcaps;
2109 g_array_append_val (stream->ptmap, item);
2112 stream->stream_id = make_stream_id (stream, media);
2114 gst_caps_unref (global_caps);
2119 GST_ERROR_OBJECT (src, "can't find proto in media");
2124 GST_ERROR_OBJECT (src, "unknown proto in media: '%s'", proto);
2129 GST_DEBUG_OBJECT (src, "sendonly media ignored, no backchannel");
2134 static const gchar *
2135 get_aggregate_control (GstRTSPSrc * src)
2140 base = src->control;
2141 else if (src->content_base)
2142 base = src->content_base;
2143 else if (src->conninfo.url_str)
2144 base = src->conninfo.url_str;
2152 clear_ptmap_item (PtMapItem * item)
2155 gst_caps_unref (item->caps);
2158 static GstRTSPStream *
2159 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx,
2162 GstRTSPStream *stream;
2163 const gchar *control_url;
2164 const GstSDPMedia *media;
2166 /* get media, should not return NULL */
2167 media = gst_sdp_message_get_media (sdp, idx);
2171 stream = g_new0 (GstRTSPStream, 1);
2172 stream->parent = src;
2173 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
2175 stream->last_ret = GST_FLOW_NOT_LINKED;
2176 stream->added = FALSE;
2177 stream->setup = FALSE;
2178 stream->skipped = FALSE;
2180 stream->eos = FALSE;
2181 stream->discont = TRUE;
2182 stream->seqbase = -1;
2183 stream->timebase = -1;
2184 stream->send_ssrc = g_random_int ();
2185 stream->profile = GST_RTSP_PROFILE_AVP;
2186 stream->ptmap = g_array_new (FALSE, FALSE, sizeof (PtMapItem));
2187 stream->mikey = NULL;
2188 stream->stream_id = NULL;
2189 stream->is_backchannel = FALSE;
2190 g_mutex_init (&stream->conninfo.send_lock);
2191 g_mutex_init (&stream->conninfo.recv_lock);
2192 g_array_set_clear_func (stream->ptmap, (GDestroyNotify) clear_ptmap_item);
2194 /* stream is sendonly and onvif backchannel is requested */
2195 if (gst_sdp_media_get_attribute_val (media, "sendonly") != NULL &&
2196 src->backchannel != BACKCHANNEL_NONE)
2197 stream->is_backchannel = TRUE;
2199 /* collect bandwidth information for this steam. FIXME, configure in the RTP
2200 * session manager to scale RTCP. */
2201 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
2203 /* collect connection info */
2204 gst_rtspsrc_collect_connections (src, sdp, media, stream);
2206 /* make the payload type map */
2207 gst_rtspsrc_collect_payloads (src, sdp, media, stream);
2209 /* collect port number */
2210 stream->port = gst_sdp_media_get_port (media);
2212 /* get control url to construct the setup url. The setup url is used to
2213 * configure the transport of the stream and is used to identity the stream in
2214 * the RTP-Info header field returned from PLAY. */
2215 control_url = gst_sdp_media_get_attribute_val (media, "control");
2216 if (control_url == NULL)
2217 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
2219 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
2220 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
2221 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
2222 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
2224 /* RFC 2326, C.3: missing control_url permitted in case of a single stream */
2225 if (control_url == NULL && n_streams == 1) {
2229 if (control_url != NULL) {
2230 stream->control_url = g_strdup (control_url);
2231 /* Build a fully qualified url using the content_base if any or by prefixing
2232 * the original request.
2233 * If the control_url starts with a '/' or a non rtsp: protocol we will most
2234 * likely build a URL that the server will fail to understand, this is ok,
2235 * we will fail then. */
2236 if (g_str_has_prefix (control_url, "rtsp://"))
2237 stream->conninfo.location = g_strdup (control_url);
2242 if (g_strcmp0 (control_url, "*") == 0)
2245 base = get_aggregate_control (src);
2247 /* check if the base ends or control starts with / */
2248 has_slash = g_str_has_prefix (control_url, "/");
2249 has_slash = has_slash || g_str_has_suffix (base, "/");
2251 /* concatenate the two strings, insert / when not present */
2252 stream->conninfo.location =
2253 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
2256 GST_DEBUG_OBJECT (src, " setup: %s",
2257 GST_STR_NULL (stream->conninfo.location));
2259 /* we keep track of all streams */
2260 src->streams = g_list_append (src->streams, stream);
2268 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
2272 GST_DEBUG_OBJECT (src, "free stream %p", stream);
2274 g_array_free (stream->ptmap, TRUE);
2276 g_free (stream->destination);
2277 g_free (stream->control_url);
2278 g_free (stream->conninfo.location);
2279 g_free (stream->stream_id);
2281 for (i = 0; i < 2; i++) {
2282 if (stream->udpsrc[i]) {
2283 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
2284 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
2285 gst_object_unref (stream->udpsrc[i]);
2287 if (stream->channelpad[i])
2288 gst_object_unref (stream->channelpad[i]);
2290 if (stream->udpsink[i]) {
2291 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
2292 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
2293 gst_object_unref (stream->udpsink[i]);
2296 if (stream->rtpsrc) {
2297 gst_element_set_state (stream->rtpsrc, GST_STATE_NULL);
2298 gst_bin_remove (GST_BIN_CAST (src), stream->rtpsrc);
2299 gst_object_unref (stream->rtpsrc);
2301 if (stream->srcpad) {
2302 gst_pad_set_active (stream->srcpad, FALSE);
2304 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2306 if (stream->srtpenc)
2307 gst_object_unref (stream->srtpenc);
2308 if (stream->srtpdec)
2309 gst_object_unref (stream->srtpdec);
2310 if (stream->srtcpparams)
2311 gst_caps_unref (stream->srtcpparams);
2313 gst_mikey_message_unref (stream->mikey);
2314 if (stream->rtcppad)
2315 gst_object_unref (stream->rtcppad);
2316 if (stream->session)
2317 g_object_unref (stream->session);
2318 if (stream->rtx_pt_map)
2319 gst_structure_free (stream->rtx_pt_map);
2321 g_mutex_clear (&stream->conninfo.send_lock);
2322 g_mutex_clear (&stream->conninfo.recv_lock);
2328 gst_rtspsrc_cleanup (GstRTSPSrc * src)
2332 GST_DEBUG_OBJECT (src, "cleanup");
2334 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2335 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2337 gst_rtspsrc_stream_free (src, stream);
2339 g_list_free (src->streams);
2340 src->streams = NULL;
2342 if (src->manager_sig_id) {
2343 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
2344 src->manager_sig_id = 0;
2346 gst_element_set_state (src->manager, GST_STATE_NULL);
2347 gst_bin_remove (GST_BIN_CAST (src), src->manager);
2348 src->manager = NULL;
2351 gst_structure_free (src->props);
2354 g_free (src->content_base);
2355 src->content_base = NULL;
2357 g_free (src->control);
2358 src->control = NULL;
2361 gst_rtsp_range_free (src->range);
2364 /* don't clear the SDP when it was used in the url */
2365 if (src->sdp && !src->from_sdp) {
2366 gst_sdp_message_free (src->sdp);
2370 src->need_segment = FALSE;
2372 if (src->provided_clock) {
2373 gst_object_unref (src->provided_clock);
2374 src->provided_clock = NULL;
2377 /* free parameter requests queue */
2378 if (!g_queue_is_empty (&src->set_get_param_q))
2379 g_queue_free_full (&src->set_get_param_q, free_param_queue);
2384 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
2385 gint * rtpport, gint * rtcpport)
2388 GstStateChangeReturn ret;
2389 GstElement *udpsrc0, *udpsrc1;
2390 gint tmp_rtp, tmp_rtcp;
2394 src = stream->parent;
2400 /* Start at next port */
2401 tmp_rtp = src->next_port_num;
2403 if (stream->is_ipv6)
2404 host = "udp://[::0]";
2406 host = "udp://0.0.0.0";
2408 /* try to allocate 2 UDP ports, the RTP port should be an even
2409 * number and the RTCP port should be the next (uneven) port */
2412 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
2413 tmp_rtp >= src->client_port_range.max)
2416 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2417 if (udpsrc0 == NULL)
2418 goto no_udp_protocol;
2419 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
2421 if (src->udp_buffer_size != 0)
2422 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
2425 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
2426 if (ret == GST_STATE_CHANGE_FAILURE) {
2428 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
2431 if (++count > src->retry)
2434 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2435 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2436 gst_object_unref (udpsrc0);
2439 GST_DEBUG_OBJECT (src, "retry %d", count);
2442 goto no_udp_protocol;
2445 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
2446 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
2448 /* check if port is even */
2449 if ((tmp_rtp & 0x01) != 0) {
2450 /* port not even, close and allocate another */
2451 if (++count > src->retry)
2454 GST_DEBUG_OBJECT (src, "RTP port not even");
2456 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2457 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2458 gst_object_unref (udpsrc0);
2461 GST_DEBUG_OBJECT (src, "retry %d", count);
2466 /* allocate port+1 for RTCP now */
2467 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2468 if (udpsrc1 == NULL)
2469 goto no_udp_rtcp_protocol;
2472 tmp_rtcp = tmp_rtp + 1;
2473 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
2476 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
2478 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
2479 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
2480 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
2481 if (ret == GST_STATE_CHANGE_FAILURE) {
2482 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
2484 if (++count > src->retry)
2487 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2488 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2489 gst_object_unref (udpsrc0);
2492 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
2493 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2494 gst_object_unref (udpsrc1);
2498 GST_DEBUG_OBJECT (src, "retry %d", count);
2502 /* all fine, do port check */
2503 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
2504 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
2506 /* this should not happen... */
2507 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
2510 /* we keep these elements, we configure all in configure_transport when the
2511 * server told us to really use the UDP ports. */
2512 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
2513 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
2514 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
2515 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
2517 /* keep track of next available port number when we have a range
2519 if (src->next_port_num != 0)
2520 src->next_port_num = tmp_rtcp + 1;
2527 GST_DEBUG_OBJECT (src, "could not get UDP source");
2532 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
2536 no_udp_rtcp_protocol:
2538 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
2543 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
2544 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
2550 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2551 gst_object_unref (udpsrc0);
2554 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2555 gst_object_unref (udpsrc1);
2562 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
2567 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
2569 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2570 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2573 for (i = 0; i < 2; i++) {
2574 if (stream->udpsrc[i])
2575 gst_element_set_state (stream->udpsrc[i], state);
2581 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing,
2589 event = gst_event_new_flush_start ();
2590 gst_event_set_seqnum (event, seqnum);
2591 GST_DEBUG_OBJECT (src, "start flush");
2593 state = GST_STATE_PAUSED;
2595 event = gst_event_new_flush_stop (FALSE);
2596 gst_event_set_seqnum (event, seqnum);
2597 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2600 state = GST_STATE_PLAYING;
2602 state = GST_STATE_PAUSED;
2604 gst_rtspsrc_push_event (src, event);
2605 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2606 gst_rtspsrc_set_state (src, state);
2609 static GstRTSPResult
2610 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
2611 GstRTSPMessage * message, GTimeVal * timeout)
2615 if (conninfo->connection) {
2616 g_mutex_lock (&conninfo->send_lock);
2617 ret = gst_rtsp_connection_send (conninfo->connection, message, timeout);
2618 g_mutex_unlock (&conninfo->send_lock);
2620 ret = GST_RTSP_ERROR;
2626 static GstRTSPResult
2627 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
2628 GstRTSPMessage * message, GTimeVal * timeout)
2632 if (conninfo->connection) {
2633 g_mutex_lock (&conninfo->recv_lock);
2634 ret = gst_rtsp_connection_receive (conninfo->connection, message, timeout);
2635 g_mutex_unlock (&conninfo->recv_lock);
2637 ret = GST_RTSP_ERROR;
2644 gst_rtspsrc_get_position (GstRTSPSrc * src)
2649 query = gst_query_new_position (GST_FORMAT_TIME);
2650 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2651 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2652 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2656 if (stream->srcpad) {
2657 if (gst_pad_query (stream->srcpad, query)) {
2658 gst_query_parse_position (query, &fmt, &pos);
2659 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2660 GST_TIME_ARGS (pos));
2661 src->last_pos = pos;
2671 gst_query_unref (query);
2675 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2680 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2682 gboolean flush, skip;
2685 GstSegment seeksegment = { 0, };
2687 const gchar *seek_style = NULL;
2689 GST_DEBUG_OBJECT (src, "doing seek with event %" GST_PTR_FORMAT, event);
2691 gst_event_parse_seek (event, &rate, &format, &flags,
2692 &cur_type, &cur, &stop_type, &stop);
2694 /* no negative rates yet */
2698 /* we need TIME format */
2699 if (format != src->segment.format)
2702 /* Check if we are not at all seekable */
2703 if (src->seekable == -1.0)
2706 /* Additional seeking-to-beginning-only check */
2707 if (src->seekable == 0.0 && cur != 0)
2710 if (flags & GST_SEEK_FLAG_SEGMENT)
2711 goto invalid_segment_flag;
2713 /* get flush flag */
2714 flush = flags & GST_SEEK_FLAG_FLUSH;
2715 skip = flags & GST_SEEK_FLAG_SKIP;
2717 /* now we need to make sure the streaming thread is stopped. We do this by
2718 * either sending a FLUSH_START event downstream which will cause the
2719 * streaming thread to stop with a WRONG_STATE.
2720 * For a non-flushing seek we simply pause the task, which will happen as soon
2721 * as it completes one iteration (and thus might block when the sink is
2722 * blocking in preroll). */
2724 GST_DEBUG_OBJECT (src, "starting flush");
2725 gst_rtspsrc_flush (src, TRUE, FALSE, gst_event_get_seqnum (event));
2728 gst_task_pause (src->task);
2732 /* we should now be able to grab the streaming thread because we stopped it
2733 * with the above flush/pause code */
2734 GST_RTSP_STREAM_LOCK (src);
2736 GST_DEBUG_OBJECT (src, "stopped streaming");
2738 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2739 gst_rtspsrc_connection_flush (src, FALSE);
2741 /* copy segment, we need this because we still need the old
2742 * segment when we close the current segment. */
2743 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2745 /* configure the seek parameters in the seeksegment. We will then have the
2746 * right values in the segment to perform the seek */
2747 GST_DEBUG_OBJECT (src, "configuring seek");
2748 gst_segment_do_seek (&seeksegment, rate, format, flags,
2749 cur_type, cur, stop_type, stop, &update);
2751 /* figure out the last position we need to play. If it's configured (stop !=
2752 * -1), use that, else we play until the total duration of the file */
2753 if ((stop = seeksegment.stop) == -1)
2754 stop = seeksegment.duration;
2756 /* if we were playing, pause first */
2757 playing = (src->state == GST_RTSP_STATE_PLAYING);
2759 /* obtain current position in case seek fails */
2760 gst_rtspsrc_get_position (src);
2761 gst_rtspsrc_pause (src, FALSE);
2765 src->state = GST_RTSP_STATE_SEEKING;
2767 /* PLAY will add the range header now. */
2768 src->need_range = TRUE;
2770 /* prepare for streaming again */
2772 /* if we started flush, we stop now */
2773 GST_DEBUG_OBJECT (src, "stopping flush");
2774 gst_rtspsrc_flush (src, FALSE, playing, gst_event_get_seqnum (event));
2777 /* now we did the seek and can activate the new segment values */
2778 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2780 /* if we're doing a segment seek, post a SEGMENT_START message */
2781 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2782 gst_element_post_message (GST_ELEMENT_CAST (src),
2783 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2784 src->segment.format, src->segment.position));
2787 /* now create the newsegment */
2788 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2789 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2792 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2793 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2794 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2795 stream->discont = TRUE;
2798 /* and continue playing if needed */
2799 GST_OBJECT_LOCK (src);
2800 playing = (GST_STATE_PENDING (src) == GST_STATE_VOID_PENDING
2801 && GST_STATE (src) == GST_STATE_PLAYING)
2802 || (GST_STATE_PENDING (src) == GST_STATE_PLAYING);
2803 GST_OBJECT_UNLOCK (src);
2805 if (src->version >= GST_RTSP_VERSION_2_0) {
2806 if (flags & GST_SEEK_FLAG_ACCURATE)
2808 else if (flags & GST_SEEK_FLAG_KEY_UNIT)
2809 seek_style = "CoRAP";
2810 else if (flags & GST_SEEK_FLAG_KEY_UNIT
2811 && flags & GST_SEEK_FLAG_SNAP_BEFORE)
2812 seek_style = "First-Prior";
2813 else if (flags & GST_SEEK_FLAG_KEY_UNIT && flags & GST_SEEK_FLAG_SNAP_AFTER)
2814 seek_style = "Next";
2818 gst_rtspsrc_play (src, &seeksegment, FALSE, seek_style);
2820 GST_RTSP_STREAM_UNLOCK (src);
2827 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2832 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2837 GST_DEBUG_OBJECT (src, "stream is not seekable");
2840 invalid_segment_flag:
2842 GST_WARNING_OBJECT (src, "Segment seeks not supported");
2848 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2852 gboolean res = TRUE;
2855 src = GST_RTSPSRC_CAST (parent);
2857 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2858 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2860 switch (GST_EVENT_TYPE (event)) {
2861 case GST_EVENT_SEEK:
2862 res = gst_rtspsrc_perform_seek (src, event);
2866 case GST_EVENT_NAVIGATION:
2867 case GST_EVENT_LATENCY:
2875 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2876 res = gst_pad_send_event (target, event);
2877 gst_object_unref (target);
2879 gst_event_unref (event);
2882 gst_event_unref (event);
2889 gst_rtspsrc_handle_src_sink_event (GstPad * pad, GstObject * parent,
2892 GstRTSPStream *stream;
2894 stream = gst_pad_get_element_private (pad);
2896 switch (GST_EVENT_TYPE (event)) {
2897 case GST_EVENT_STREAM_START:{
2898 const gchar *upstream_id;
2901 gst_event_parse_stream_start (event, &upstream_id);
2902 stream_id = g_strdup_printf ("%s/%s", upstream_id, stream->stream_id);
2904 gst_event_unref (event);
2905 event = gst_event_new_stream_start (stream_id);
2913 return gst_pad_push_event (stream->srcpad, event);
2916 /* this is the final event function we receive on the internal source pad when
2917 * we deal with TCP connections */
2919 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2924 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2926 switch (GST_EVENT_TYPE (event)) {
2927 case GST_EVENT_SEEK:
2929 case GST_EVENT_NAVIGATION:
2930 case GST_EVENT_LATENCY:
2932 gst_event_unref (event);
2939 /* this is the final query function we receive on the internal source pad when
2940 * we deal with TCP connections */
2942 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2946 gboolean res = TRUE;
2948 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2950 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2951 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2953 switch (GST_QUERY_TYPE (query)) {
2954 case GST_QUERY_POSITION:
2959 case GST_QUERY_DURATION:
2963 gst_query_parse_duration (query, &format, NULL);
2966 case GST_FORMAT_TIME:
2967 gst_query_set_duration (query, format, src->segment.duration);
2975 case GST_QUERY_LATENCY:
2977 /* we are live with a min latency of 0 and unlimited max latency, this
2978 * result will be updated by the session manager if there is any. */
2979 gst_query_set_latency (query, TRUE, 0, -1);
2989 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2991 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2995 gboolean res = FALSE;
2997 src = GST_RTSPSRC_CAST (parent);
2999 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
3000 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3002 switch (GST_QUERY_TYPE (query)) {
3003 case GST_QUERY_DURATION:
3007 gst_query_parse_duration (query, &format, NULL);
3010 case GST_FORMAT_TIME:
3011 gst_query_set_duration (query, format, src->segment.duration);
3019 case GST_QUERY_SEEKING:
3023 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
3024 if (format == GST_FORMAT_TIME) {
3026 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
3027 GstClockTime start = 0, duration = src->segment.duration;
3029 /* seeking without duration is unlikely */
3030 seekable = seekable && src->seekable >= 0.0 && src->segment.duration &&
3031 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
3034 if (src->seekable > 0.0) {
3035 start = src->last_pos - src->seekable * GST_SECOND;
3037 /* src->seekable == 0 means that we can only seek to 0 */
3043 GST_LOG_OBJECT (src, "seekable : %d", seekable);
3045 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, start,
3055 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
3057 gst_query_set_uri (query, uri);
3065 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
3067 /* forward the query to the proxy target pad */
3069 res = gst_pad_query (target, query);
3070 gst_object_unref (target);
3079 /* callback for RTCP messages to be sent to the server when operating in TCP
3081 static GstFlowReturn
3082 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
3085 GstRTSPStream *stream;
3086 GstFlowReturn res = GST_FLOW_OK;
3091 GstRTSPMessage message = { 0 };
3092 GstRTSPConnInfo *conninfo;
3094 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
3095 src = stream->parent;
3097 gst_buffer_map (buffer, &map, GST_MAP_READ);
3101 gst_rtsp_message_init_data (&message, stream->channel[1]);
3103 /* lend the body data to the message */
3104 gst_rtsp_message_take_body (&message, data, size);
3106 if (stream->conninfo.connection)
3107 conninfo = &stream->conninfo;
3109 conninfo = &src->conninfo;
3111 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
3112 ret = gst_rtspsrc_connection_send (src, conninfo, &message, NULL);
3113 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
3115 /* and steal it away again because we will free it when unreffing the
3117 gst_rtsp_message_steal_body (&message, &data, &size);
3118 gst_rtsp_message_unset (&message);
3120 gst_buffer_unmap (buffer, &map);
3121 gst_buffer_unref (buffer);
3126 static GstFlowReturn
3127 gst_rtspsrc_push_backchannel_buffer (GstRTSPSrc * src, guint id,
3130 GstFlowReturn res = GST_FLOW_OK;
3131 GstRTSPStream *stream;
3133 if (!src->conninfo.connected || src->state != GST_RTSP_STATE_PLAYING)
3136 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
3137 if (stream == NULL) {
3138 GST_ERROR_OBJECT (src, "no stream with id %u", id);
3142 if (src->interleaved) {
3148 GstRTSPMessage message = { 0 };
3149 GstRTSPConnInfo *conninfo;
3151 buffer = gst_sample_get_buffer (sample);
3153 gst_buffer_map (buffer, &map, GST_MAP_READ);
3157 gst_rtsp_message_init_data (&message, stream->channel[0]);
3159 /* lend the body data to the message */
3160 gst_rtsp_message_take_body (&message, data, size);
3162 if (stream->conninfo.connection)
3163 conninfo = &stream->conninfo;
3165 conninfo = &src->conninfo;
3167 GST_DEBUG_OBJECT (src, "sending %u bytes backchannel RTP", size);
3168 ret = gst_rtspsrc_connection_send (src, conninfo, &message, NULL);
3169 GST_DEBUG_OBJECT (src, "sent backchannel RTP, %d", ret);
3171 /* and steal it away again because we will free it when unreffing the
3173 gst_rtsp_message_steal_body (&message, &data, &size);
3174 gst_rtsp_message_unset (&message);
3176 gst_buffer_unmap (buffer, &map);
3180 g_signal_emit_by_name (stream->rtpsrc, "push-sample", sample, &res);
3181 GST_DEBUG_OBJECT (src, "sent backchannel RTP sample %p: %s", sample,
3182 gst_flow_get_name (res));
3186 gst_sample_unref (sample);
3191 static GstPadProbeReturn
3192 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3194 GstRTSPSrc *src = user_data;
3196 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
3197 GST_DEBUG_PAD_NAME (pad));
3199 /* activate the streams */
3200 GST_OBJECT_LOCK (src);
3201 if (!src->need_activate)
3204 src->need_activate = FALSE;
3205 GST_OBJECT_UNLOCK (src);
3207 gst_rtspsrc_activate_streams (src);
3209 return GST_PAD_PROBE_OK;
3213 GST_OBJECT_UNLOCK (src);
3214 return GST_PAD_PROBE_OK;
3218 static GstPadProbeReturn
3219 udpsrc_probe_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3221 guint32 *segment_seqnum = user_data;
3223 switch (GST_EVENT_TYPE (info->data)) {
3224 case GST_EVENT_SEGMENT:
3225 if (!gst_event_is_writable (info->data))
3226 info->data = gst_event_make_writable (info->data);
3228 *segment_seqnum = gst_event_get_seqnum (info->data);
3233 return GST_PAD_PROBE_OK;
3237 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
3239 GstPad *gpad = GST_PAD_CAST (user_data);
3241 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
3242 gst_pad_store_sticky_event (gpad, *event);
3248 add_backchannel_fakesink (GstRTSPSrc * src, GstRTSPStream * stream,
3252 GstElement *fakesink;
3254 fakesink = gst_element_factory_make ("fakesink", NULL);
3255 if (fakesink == NULL) {
3256 GST_ERROR_OBJECT (src, "no fakesink");
3260 sinkpad = gst_element_get_static_pad (fakesink, "sink");
3262 GST_DEBUG_OBJECT (src, "backchannel stream %p, hooking fakesink", stream);
3264 gst_bin_add (GST_BIN_CAST (src), fakesink);
3265 if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK) {
3266 GST_WARNING_OBJECT (src, "could not link to fakesink");
3270 gst_object_unref (sinkpad);
3272 gst_element_sync_state_with_parent (fakesink);
3276 /* this callback is called when the session manager generated a new src pad with
3277 * payloaded RTP packets. We simply ghost the pad here. */
3279 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
3282 GstPadTemplate *template;
3285 GstRTSPStream *stream;
3287 GstPad *internal_src;
3289 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
3291 GST_RTSP_STATE_LOCK (src);
3293 name = gst_object_get_name (GST_OBJECT_CAST (pad));
3294 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
3295 goto unknown_stream;
3297 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
3299 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
3301 goto unknown_stream;
3304 stream->ssrc = ssrc;
3306 /* we'll add it later see below */
3307 stream->added = TRUE;
3309 /* check if we added all streams */
3311 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
3312 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
3314 GST_DEBUG_OBJECT (src, "stream %p, container %d, added %d, setup %d",
3315 ostream, ostream->container, ostream->added, ostream->setup);
3317 /* if we find a stream for which we did a setup that is not added, we
3318 * need to wait some more */
3319 if (ostream->setup && !ostream->added) {
3324 GST_RTSP_STATE_UNLOCK (src);
3326 /* create a new pad we will use to stream to */
3327 template = gst_static_pad_template_get (&rtptemplate);
3328 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
3329 gst_object_unref (template);
3332 /* We intercept and modify the stream start event */
3334 GST_PAD (gst_proxy_pad_get_internal (GST_PROXY_PAD (stream->srcpad)));
3335 gst_pad_set_element_private (internal_src, stream);
3336 gst_pad_set_event_function (internal_src, gst_rtspsrc_handle_src_sink_event);
3337 gst_object_unref (internal_src);
3339 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3340 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3341 gst_pad_set_active (stream->srcpad, TRUE);
3342 gst_pad_sticky_events_foreach (pad, copy_sticky_events, stream->srcpad);
3344 /* don't add the srcpad if this is a sendonly stream */
3345 if (stream->is_backchannel)
3346 add_backchannel_fakesink (src, stream, stream->srcpad);
3348 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3351 GST_DEBUG_OBJECT (src, "We added all streams");
3352 /* when we get here, all stream are added and we can fire the no-more-pads
3354 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
3362 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
3363 GST_RTSP_STATE_UNLOCK (src);
3370 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
3374 len = stream->ptmap->len;
3375 for (i = 0; i < len; i++) {
3376 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3384 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
3386 GstRTSPStream *stream;
3389 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
3391 GST_RTSP_STATE_LOCK (src);
3392 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
3394 goto unknown_stream;
3396 if ((caps = stream_get_caps_for_pt (stream, pt)))
3397 gst_caps_ref (caps);
3398 GST_RTSP_STATE_UNLOCK (src);
3404 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
3405 GST_RTSP_STATE_UNLOCK (src);
3411 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
3413 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
3419 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
3425 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
3431 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
3433 GstRTSPSrc *src = stream->parent;
3436 g_object_get (source, "ssrc", &ssrc, NULL);
3438 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
3439 ssrc, stream->ssrc, stream->id);
3441 if (ssrc == stream->ssrc)
3442 gst_rtspsrc_do_stream_eos (src, stream);
3446 on_timeout_common (GObject * session, GObject * source, GstRTSPStream * stream)
3448 GstRTSPSrc *src = stream->parent;
3451 g_object_get (source, "ssrc", &ssrc, NULL);
3453 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
3454 ssrc, stream->ssrc, stream->id);
3456 if (ssrc == stream->ssrc)
3457 gst_rtspsrc_do_stream_eos (src, stream);
3461 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
3463 GstRTSPSrc *src = stream->parent;
3465 /* timeout, post element message */
3466 gst_element_post_message (GST_ELEMENT_CAST (src),
3467 gst_message_new_element (GST_OBJECT_CAST (src),
3468 gst_structure_new ("GstRTSPSrcTimeout",
3469 "cause", G_TYPE_ENUM, GST_RTSP_SRC_TIMEOUT_CAUSE_RTCP,
3470 "stream-number", G_TYPE_INT, stream->id, "ssrc", G_TYPE_UINT,
3471 stream->ssrc, NULL)));
3473 on_timeout_common (session, source, stream);
3477 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
3479 GstRTSPStream *stream;
3481 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
3483 /* get stream for session */
3484 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
3486 gst_rtspsrc_do_stream_eos (src, stream);
3491 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
3493 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
3498 set_manager_buffer_mode (GstRTSPSrc * src)
3500 GObjectClass *klass;
3502 if (src->manager == NULL)
3505 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3507 if (!g_object_class_find_property (klass, "buffer-mode"))
3510 if (src->buffer_mode != BUFFER_MODE_AUTO) {
3511 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
3516 GST_DEBUG_OBJECT (src,
3517 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
3519 if (src->provided_clock) {
3520 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
3522 if (clock == src->provided_clock) {
3523 GST_DEBUG_OBJECT (src, "selected synced");
3524 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
3527 gst_object_unref (clock);
3532 /* Otherwise fall-through and use another buffer mode */
3534 gst_object_unref (clock);
3537 GST_DEBUG_OBJECT (src, "auto buffering mode");
3538 if (src->use_buffering) {
3539 GST_DEBUG_OBJECT (src, "selected buffer");
3540 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
3542 GST_DEBUG_OBJECT (src, "selected slave");
3543 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
3548 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
3552 GstMIKEYMessage *msg = stream->mikey;
3554 GST_DEBUG ("request key SSRC %u", ssrc);
3556 caps = gst_caps_ref (stream_get_caps_for_pt (stream, stream->default_pt));
3557 caps = gst_caps_make_writable (caps);
3559 /* parse crypto sessions and look for the SSRC rollover counter */
3560 msg = stream->mikey;
3561 for (i = 0; msg && i < gst_mikey_message_get_n_cs (msg); i++) {
3562 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
3564 if (ssrc == map->ssrc) {
3565 gst_caps_set_simple (caps, "roc", G_TYPE_UINT, map->roc, NULL);
3574 request_rtp_decoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
3576 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3577 if (stream->id != session)
3580 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3581 stream->profile != GST_RTSP_PROFILE_SAVPF)
3584 if (stream->srtpdec == NULL) {
3587 name = g_strdup_printf ("srtpdec_%u", session);
3588 stream->srtpdec = gst_element_factory_make ("srtpdec", name);
3591 if (stream->srtpdec == NULL) {
3592 GST_ELEMENT_ERROR (stream->parent, CORE, MISSING_PLUGIN, (NULL),
3593 ("no srtpdec element present!"));
3596 g_signal_connect (stream->srtpdec, "request-key",
3597 (GCallback) request_key, stream);
3599 return gst_object_ref (stream->srtpdec);
3603 request_rtcp_encoder (GstElement * rtpbin, guint session,
3604 GstRTSPStream * stream)
3609 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3610 if (stream->id != session)
3613 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3614 stream->profile != GST_RTSP_PROFILE_SAVPF)
3617 if (stream->srtpenc == NULL) {
3620 name = g_strdup_printf ("srtpenc_%u", session);
3621 stream->srtpenc = gst_element_factory_make ("srtpenc", name);
3624 if (stream->srtpenc == NULL) {
3625 GST_ELEMENT_ERROR (stream->parent, CORE, MISSING_PLUGIN, (NULL),
3626 ("no srtpenc element present!"));
3630 /* get RTCP crypto parameters from caps */
3631 s = gst_caps_get_structure (stream->srtcpparams, 0);
3635 GType ciphertype, authtype;
3636 GValue rtcp_cipher = G_VALUE_INIT, rtcp_auth = G_VALUE_INIT;
3638 ciphertype = g_type_from_name ("GstSrtpCipherType");
3639 authtype = g_type_from_name ("GstSrtpAuthType");
3640 g_value_init (&rtcp_cipher, ciphertype);
3641 g_value_init (&rtcp_auth, authtype);
3643 str = gst_structure_get_string (s, "srtcp-cipher");
3644 gst_value_deserialize (&rtcp_cipher, str);
3645 str = gst_structure_get_string (s, "srtcp-auth");
3646 gst_value_deserialize (&rtcp_auth, str);
3647 gst_structure_get (s, "srtp-key", GST_TYPE_BUFFER, &buf, NULL);
3649 g_object_set_property (G_OBJECT (stream->srtpenc), "rtp-cipher",
3651 g_object_set_property (G_OBJECT (stream->srtpenc), "rtp-auth",
3653 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-cipher",
3655 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-auth",
3657 g_object_set (stream->srtpenc, "key", buf, NULL);
3659 g_value_unset (&rtcp_cipher);
3660 g_value_unset (&rtcp_auth);
3661 gst_buffer_unref (buf);
3664 name = g_strdup_printf ("rtcp_sink_%d", session);
3665 pad = gst_element_get_request_pad (stream->srtpenc, name);
3667 gst_object_unref (pad);
3669 return gst_object_ref (stream->srtpenc);
3673 request_aux_receiver (GstElement * rtpbin, guint sessid, GstRTSPSrc * src)
3675 GstElement *rtx, *bin;
3678 GstRTSPStream *stream;
3680 stream = find_stream (src, &sessid, (gpointer) find_stream_by_id);
3682 GST_WARNING_OBJECT (src, "Stream %u not found", sessid);
3686 GST_INFO_OBJECT (src, "creating retransmision receiver for session %u "
3687 "with map %" GST_PTR_FORMAT, sessid, stream->rtx_pt_map);
3688 bin = gst_bin_new (NULL);
3689 rtx = gst_element_factory_make ("rtprtxreceive", NULL);
3690 g_object_set (rtx, "payload-type-map", stream->rtx_pt_map, NULL);
3691 gst_bin_add (GST_BIN (bin), rtx);
3693 pad = gst_element_get_static_pad (rtx, "src");
3694 name = g_strdup_printf ("src_%u", sessid);
3695 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3697 gst_object_unref (pad);
3699 pad = gst_element_get_static_pad (rtx, "sink");
3700 name = g_strdup_printf ("sink_%u", sessid);
3701 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3703 gst_object_unref (pad);
3709 add_retransmission (GstRTSPSrc * src, GstRTSPTransport * transport)
3713 gboolean do_retransmission = FALSE;
3715 if (transport->trans != GST_RTSP_TRANS_RTP)
3717 if (transport->profile != GST_RTSP_PROFILE_AVPF &&
3718 transport->profile != GST_RTSP_PROFILE_SAVPF)
3721 signal_id = g_signal_lookup ("request-aux-receiver",
3722 G_OBJECT_TYPE (src->manager));
3723 /* there's already something connected */
3724 if (g_signal_handler_find (src->manager, G_SIGNAL_MATCH_ID, signal_id, 0,
3725 NULL, NULL, NULL) != 0) {
3726 GST_DEBUG_OBJECT (src, "Not adding RTX AUX element as "
3727 "\"request-aux-receiver\" signal is "
3728 "already used by the application");
3732 /* build the retransmission payload type map */
3733 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3734 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3735 gboolean do_retransmission_stream = FALSE;
3738 if (stream->rtx_pt_map)
3739 gst_structure_free (stream->rtx_pt_map);
3740 stream->rtx_pt_map = gst_structure_new_empty ("application/x-rtp-pt-map");
3742 for (i = 0; i < stream->ptmap->len; i++) {
3743 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3744 GstStructure *s = gst_caps_get_structure (item->caps, 0);
3745 const gchar *encoding;
3747 /* we only care about RTX streams */
3748 if ((encoding = gst_structure_get_string (s, "encoding-name"))
3749 && g_strcmp0 (encoding, "RTX") == 0) {
3750 const gchar *stream_pt_s;
3753 if (gst_structure_get_int (s, "payload", &rtx_pt)
3754 && (stream_pt_s = gst_structure_get_string (s, "apt"))) {
3757 gst_structure_set (stream->rtx_pt_map, stream_pt_s, G_TYPE_UINT,
3759 do_retransmission_stream = TRUE;
3765 if (do_retransmission_stream) {
3766 GST_DEBUG_OBJECT (src, "built retransmission payload map for stream "
3767 "id %i: %" GST_PTR_FORMAT, stream->id, stream->rtx_pt_map);
3768 do_retransmission = TRUE;
3770 GST_DEBUG_OBJECT (src, "no retransmission payload map for stream "
3771 "id %i", stream->id);
3772 gst_structure_free (stream->rtx_pt_map);
3773 stream->rtx_pt_map = NULL;
3777 if (do_retransmission) {
3778 GST_DEBUG_OBJECT (src, "Enabling retransmissions");
3780 g_object_set (src->manager, "do-retransmission", TRUE, NULL);
3782 /* enable RFC4588 retransmission handling by setting rtprtxreceive
3783 * as the "aux" element of rtpbin */
3784 g_signal_connect (src->manager, "request-aux-receiver",
3785 (GCallback) request_aux_receiver, src);
3787 GST_DEBUG_OBJECT (src,
3788 "Not enabling retransmissions as no stream had a retransmission payload map");
3792 /* try to get and configure a manager */
3794 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
3795 GstRTSPTransport * transport)
3797 const gchar *manager;
3799 GstStateChangeReturn ret;
3801 /* find a manager */
3802 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
3806 GST_DEBUG_OBJECT (src, "using manager %s", manager);
3808 /* configure the manager */
3809 if (src->manager == NULL) {
3810 GObjectClass *klass;
3812 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
3814 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
3818 goto use_no_manager;
3820 if (!(src->manager = gst_element_factory_make (manager, "manager")))
3821 goto manager_failed;
3824 /* we manage this element */
3825 gst_element_set_locked_state (src->manager, TRUE);
3826 gst_bin_add (GST_BIN_CAST (src), src->manager);
3828 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
3829 if (ret == GST_STATE_CHANGE_FAILURE)
3830 goto start_manager_failure;
3832 g_object_set (src->manager, "latency", src->latency, NULL);
3834 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3836 if (g_object_class_find_property (klass, "ntp-sync")) {
3837 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
3840 if (g_object_class_find_property (klass, "rfc7273-sync")) {
3841 g_object_set (src->manager, "rfc7273-sync", src->rfc7273_sync, NULL);
3844 if (src->use_pipeline_clock) {
3845 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
3846 g_object_set (src->manager, "use-pipeline-clock", TRUE, NULL);
3849 if (g_object_class_find_property (klass, "ntp-time-source")) {
3850 g_object_set (src->manager, "ntp-time-source", src->ntp_time_source,
3855 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
3856 g_object_set (src->manager, "sdes", src->sdes, NULL);
3859 if (g_object_class_find_property (klass, "drop-on-latency")) {
3860 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
3864 if (g_object_class_find_property (klass, "max-rtcp-rtp-time-diff")) {
3865 g_object_set (src->manager, "max-rtcp-rtp-time-diff",
3866 src->max_rtcp_rtp_time_diff, NULL);
3869 if (g_object_class_find_property (klass, "max-ts-offset-adjustment")) {
3870 g_object_set (src->manager, "max-ts-offset-adjustment",
3871 src->max_ts_offset_adjustment, NULL);
3874 if (g_object_class_find_property (klass, "max-ts-offset")) {
3875 gint64 max_ts_offset;
3877 /* setting max-ts-offset in the manager has side effects so only do it
3878 * if the value differs */
3879 g_object_get (src->manager, "max-ts-offset", &max_ts_offset, NULL);
3880 if (max_ts_offset != src->max_ts_offset) {
3881 g_object_set (src->manager, "max-ts-offset", src->max_ts_offset,
3886 /* buffer mode pauses are handled by adding offsets to buffer times,
3887 * but some depayloaders may have a hard time syncing output times
3888 * with such input times, e.g. container ones, most notably ASF */
3889 /* TODO alternatives are having an event that indicates these shifts,
3890 * or having rtsp extensions provide suggestion on buffer mode */
3891 /* valid duration implies not likely live pipeline,
3892 * so slaving in jitterbuffer does not make much sense
3893 * (and might mess things up due to bursts) */
3894 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
3895 src->segment.duration && stream->container) {
3896 src->use_buffering = TRUE;
3898 src->use_buffering = FALSE;
3901 set_manager_buffer_mode (src);
3903 /* connect to signals */
3904 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
3906 src->manager_sig_id =
3907 g_signal_connect (src->manager, "pad-added",
3908 (GCallback) new_manager_pad, src);
3909 src->manager_ptmap_id =
3910 g_signal_connect (src->manager, "request-pt-map",
3911 (GCallback) request_pt_map, src);
3913 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
3916 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
3919 if (src->do_retransmission)
3920 add_retransmission (src, transport);
3922 g_signal_connect (src->manager, "request-rtp-decoder",
3923 (GCallback) request_rtp_decoder, stream);
3924 g_signal_connect (src->manager, "request-rtcp-decoder",
3925 (GCallback) request_rtp_decoder, stream);
3926 g_signal_connect (src->manager, "request-rtcp-encoder",
3927 (GCallback) request_rtcp_encoder, stream);
3929 /* we stream directly to the manager, get some pads. Each RTSP stream goes
3930 * into a separate RTP session. */
3931 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
3932 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
3934 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
3935 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
3938 /* now configure the bandwidth in the manager */
3939 if (g_signal_lookup ("get-internal-session",
3940 G_OBJECT_TYPE (src->manager)) != 0) {
3941 GObject *rtpsession;
3943 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
3946 GstRTPProfile rtp_profile;
3948 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
3950 stream->session = rtpsession;
3952 if (stream->as_bandwidth != -1) {
3953 GST_INFO_OBJECT (src, "setting AS: %f",
3954 (gdouble) (stream->as_bandwidth * 1000));
3955 g_object_set (rtpsession, "bandwidth",
3956 (gdouble) (stream->as_bandwidth * 1000), NULL);
3958 if (stream->rr_bandwidth != -1) {
3959 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
3960 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
3963 if (stream->rs_bandwidth != -1) {
3964 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
3965 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
3969 switch (stream->profile) {
3970 case GST_RTSP_PROFILE_AVPF:
3971 rtp_profile = GST_RTP_PROFILE_AVPF;
3973 case GST_RTSP_PROFILE_SAVP:
3974 rtp_profile = GST_RTP_PROFILE_SAVP;
3976 case GST_RTSP_PROFILE_SAVPF:
3977 rtp_profile = GST_RTP_PROFILE_SAVPF;
3979 case GST_RTSP_PROFILE_AVP:
3981 rtp_profile = GST_RTP_PROFILE_AVP;
3985 g_object_set (rtpsession, "rtp-profile", rtp_profile, NULL);
3987 g_object_set (rtpsession, "probation", src->probation, NULL);
3989 g_object_set (rtpsession, "internal-ssrc", stream->send_ssrc, NULL);
3991 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
3993 g_signal_connect (rtpsession, "on-bye-timeout",
3994 (GCallback) on_timeout_common, stream);
3995 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
3997 g_signal_connect (rtpsession, "on-ssrc-active",
3998 (GCallback) on_ssrc_active, stream);
4009 GST_DEBUG_OBJECT (src, "cannot get a session manager");
4014 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
4017 start_manager_failure:
4019 GST_DEBUG_OBJECT (src, "could not start session manager");
4024 /* free the UDP sources allocated when negotiating a transport.
4025 * This function is called when the server negotiated to a transport where the
4026 * UDP sources are not needed anymore, such as TCP or multicast. */
4028 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
4032 for (i = 0; i < 2; i++) {
4033 if (stream->udpsrc[i]) {
4034 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
4035 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
4036 gst_object_unref (stream->udpsrc[i]);
4037 stream->udpsrc[i] = NULL;
4042 /* for TCP, create pads to send and receive data to and from the manager and to
4043 * intercept various events and queries
4046 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
4047 GstRTSPTransport * transport, GstPad ** outpad)
4050 GstPadTemplate *template;
4051 GstPad *pad0, *pad1;
4053 /* configure for interleaved delivery, nothing needs to be done
4054 * here, the loop function will call the chain functions of the
4055 * session manager. */
4056 stream->channel[0] = transport->interleaved.min;
4057 stream->channel[1] = transport->interleaved.max;
4058 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
4059 stream->channel[0], stream->channel[1]);
4061 /* we can remove the allocated UDP ports now */
4062 gst_rtspsrc_stream_free_udp (stream);
4064 /* no session manager, send data to srcpad directly */
4065 if (!stream->channelpad[0]) {
4066 GST_DEBUG_OBJECT (src, "no manager, creating pad");
4068 /* create a new pad we will use to stream to */
4069 name = g_strdup_printf ("stream_%u", stream->id);
4070 template = gst_static_pad_template_get (&rtptemplate);
4071 stream->channelpad[0] = gst_pad_new_from_template (template, name);
4072 gst_object_unref (template);
4075 /* set caps and activate */
4076 gst_pad_use_fixed_caps (stream->channelpad[0]);
4077 gst_pad_set_active (stream->channelpad[0], TRUE);
4079 *outpad = gst_object_ref (stream->channelpad[0]);
4081 GST_DEBUG_OBJECT (src, "using manager source pad");
4083 template = gst_static_pad_template_get (&anysrctemplate);
4085 /* allocate pads for sending the channel data into the manager */
4086 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
4087 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
4088 gst_object_unref (stream->channelpad[0]);
4089 stream->channelpad[0] = pad0;
4090 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
4091 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
4092 gst_pad_set_element_private (pad0, src);
4093 gst_pad_set_active (pad0, TRUE);
4095 if (stream->channelpad[1]) {
4096 /* if we have a sinkpad for the other channel, create a pad and link to the
4098 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
4099 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
4100 gst_pad_link_full (pad1, stream->channelpad[1],
4101 GST_PAD_LINK_CHECK_NOTHING);
4102 gst_object_unref (stream->channelpad[1]);
4103 stream->channelpad[1] = pad1;
4104 gst_pad_set_active (pad1, TRUE);
4106 gst_object_unref (template);
4108 /* setup RTCP transport back to the server if we have to. */
4109 if (src->manager && src->do_rtcp) {
4112 template = gst_static_pad_template_get (&anysinktemplate);
4114 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
4115 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
4116 gst_pad_set_element_private (stream->rtcppad, stream);
4117 gst_pad_set_active (stream->rtcppad, TRUE);
4119 /* get session RTCP pad */
4120 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
4121 pad = gst_element_get_request_pad (src->manager, name);
4126 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
4127 gst_object_unref (pad);
4130 gst_object_unref (template);
4136 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
4137 GstRTSPTransport * transport, const gchar ** destination, gint * min,
4138 gint * max, guint * ttl)
4140 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
4142 if (!(*destination = transport->destination))
4143 *destination = stream->destination;
4146 /* transport first */
4147 *min = transport->port.min;
4148 *max = transport->port.max;
4149 if (*min == -1 && *max == -1) {
4150 /* then try from SDP */
4151 if (stream->port != 0) {
4152 *min = stream->port;
4153 *max = stream->port + 1;
4159 if (!(*ttl = transport->ttl))
4164 /* first take the source, then the endpoint to figure out where to send
4166 if (!(*destination = transport->source)) {
4167 if (src->conninfo.connection)
4168 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
4169 else if (stream->conninfo.connection)
4171 gst_rtsp_connection_get_ip (stream->conninfo.connection);
4175 /* for unicast we only expect the ports here */
4176 *min = transport->server_port.min;
4177 *max = transport->server_port.max;
4182 /* For multicast create UDP sources and join the multicast group. */
4184 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
4185 GstRTSPTransport * transport, GstPad ** outpad)
4188 const gchar *destination;
4191 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
4193 /* we can remove the allocated UDP ports now */
4194 gst_rtspsrc_stream_free_udp (stream);
4196 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
4199 /* we need a destination now */
4200 if (destination == NULL)
4201 goto no_destination;
4203 /* we really need ports now or we won't be able to receive anything at all */
4204 if (min == -1 && max == -1)
4207 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
4208 destination, min, max);
4210 /* creating UDP source for RTP */
4212 uri = g_strdup_printf ("udp://%s:%d", destination, min);
4214 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
4216 if (stream->udpsrc[0] == NULL)
4219 /* take ownership */
4220 gst_object_ref_sink (stream->udpsrc[0]);
4222 if (src->udp_buffer_size != 0)
4223 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
4224 src->udp_buffer_size, NULL);
4226 if (src->multi_iface != NULL)
4227 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
4228 src->multi_iface, NULL);
4231 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
4232 gst_element_set_state (stream->udpsrc[0], GST_STATE_READY);
4235 /* creating another UDP source for RTCP */
4239 uri = g_strdup_printf ("udp://%s:%d", destination, max);
4241 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
4243 if (stream->udpsrc[1] == NULL)
4246 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
4247 stream->profile == GST_RTSP_PROFILE_SAVPF)
4248 caps = gst_caps_new_empty_simple ("application/x-srtcp");
4250 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4251 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
4252 gst_caps_unref (caps);
4254 /* take ownership */
4255 gst_object_ref_sink (stream->udpsrc[1]);
4257 if (src->multi_iface != NULL)
4258 g_object_set (G_OBJECT (stream->udpsrc[1]), "multicast-iface",
4259 src->multi_iface, NULL);
4261 gst_element_set_state (stream->udpsrc[1], GST_STATE_READY);
4268 GST_DEBUG_OBJECT (src, "no UDP source element found");
4273 GST_DEBUG_OBJECT (src, "no destination found");
4278 GST_DEBUG_OBJECT (src, "no ports found");
4283 /* configure the remainder of the UDP ports */
4285 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
4286 GstRTSPTransport * transport, GstPad ** outpad)
4288 /* we manage the UDP elements now. For unicast, the UDP sources where
4289 * allocated in the stream when we suggested a transport. */
4290 if (stream->udpsrc[0]) {
4293 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
4294 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
4296 GST_DEBUG_OBJECT (src, "setting up UDP source");
4298 /* configure a timeout on the UDP port. When the timeout message is
4299 * posted, we assume UDP transport is not possible. We reconnect using TCP
4301 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
4302 src->udp_timeout * 1000, NULL);
4304 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt)))
4305 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
4307 /* get output pad of the UDP source. */
4308 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
4310 /* save it so we can unblock */
4311 stream->blockedpad = *outpad;
4313 /* configure pad block on the pad. As soon as there is dataflow on the
4314 * UDP source, we know that UDP is not blocked by a firewall and we can
4315 * configure all the streams to let the application autoplug decoders. */
4317 gst_pad_add_probe (stream->blockedpad,
4318 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
4319 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocked, src, NULL);
4321 gst_pad_add_probe (stream->blockedpad,
4322 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM, udpsrc_probe_cb,
4323 &(stream->segment_seqnum[0]), NULL);
4325 if (stream->channelpad[0]) {
4326 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
4327 /* configure for UDP delivery, we need to connect the UDP pads to
4328 * the session plugin. */
4329 gst_pad_link_full (*outpad, stream->channelpad[0],
4330 GST_PAD_LINK_CHECK_NOTHING);
4331 gst_object_unref (*outpad);
4333 /* we connected to pad-added signal to get pads from the manager */
4335 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
4340 if (stream->udpsrc[1]) {
4343 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
4344 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
4346 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
4347 stream->profile == GST_RTSP_PROFILE_SAVPF)
4348 caps = gst_caps_new_empty_simple ("application/x-srtcp");
4350 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4351 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
4352 gst_caps_unref (caps);
4354 if (stream->channelpad[1]) {
4357 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
4359 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
4360 gst_pad_add_probe (pad,
4361 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM, udpsrc_probe_cb,
4362 &(stream->segment_seqnum[1]), NULL);
4363 gst_pad_link_full (pad, stream->channelpad[1],
4364 GST_PAD_LINK_CHECK_NOTHING);
4365 gst_object_unref (pad);
4367 /* leave unlinked */
4373 /* configure the UDP sink back to the server for status reports */
4375 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
4376 GstRTSPStream * stream, GstRTSPTransport * transport)
4379 gint rtp_port, rtcp_port;
4380 gboolean do_rtp, do_rtcp;
4381 const gchar *destination;
4386 /* get transport info */
4387 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
4388 &rtp_port, &rtcp_port, &ttl);
4390 /* see what we need to do */
4391 do_rtp = (rtp_port != -1);
4392 /* it's possible that the server does not want us to send RTCP in which case
4394 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
4396 /* we need a destination when we have RTP or RTCP ports */
4397 if (destination == NULL && (do_rtp || do_rtcp))
4398 goto no_destination;
4400 /* try to construct the fakesrc to the RTP port of the server to open up any
4401 * NAT firewalls or, if backchannel, construct an appsrc */
4403 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
4406 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
4407 stream->udpsink[0] =
4408 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
4410 if (stream->udpsink[0] == NULL)
4411 goto no_sink_element;
4413 /* don't join multicast group, we will have the source socket do that */
4414 /* no sync or async state changes needed */
4415 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
4416 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
4418 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
4420 if (stream->udpsrc[0]) {
4421 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
4422 * so that NAT firewalls will open a hole for us */
4423 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
4427 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
4428 /* configure socket and make sure udpsink does not close it when shutting
4429 * down, it belongs to udpsrc after all. */
4430 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
4431 "close-socket", FALSE, NULL);
4432 g_object_unref (socket);
4435 if (stream->is_backchannel) {
4436 /* appsrc is for the app to shovel data using push-backchannel-buffer */
4437 stream->rtpsrc = gst_element_factory_make ("appsrc", NULL);
4438 if (stream->rtpsrc == NULL)
4439 goto no_appsrc_element;
4441 /* interal use only, don't emit signals */
4442 g_object_set (G_OBJECT (stream->rtpsrc), "emit-signals", TRUE,
4443 "is-live", TRUE, NULL);
4445 /* the source for the dummy packets to open up NAT */
4446 stream->rtpsrc = gst_element_factory_make ("fakesrc", NULL);
4447 if (stream->rtpsrc == NULL)
4448 goto no_fakesrc_element;
4450 /* random data in 5 buffers, a size of 200 bytes should be fine */
4451 g_object_set (G_OBJECT (stream->rtpsrc), "filltype", 3, "num-buffers", 5,
4452 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
4455 /* keep everything locked */
4456 gst_element_set_locked_state (stream->udpsink[0], TRUE);
4457 gst_element_set_locked_state (stream->rtpsrc, TRUE);
4459 gst_object_ref (stream->udpsink[0]);
4460 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
4461 gst_object_ref (stream->rtpsrc);
4462 gst_bin_add (GST_BIN_CAST (src), stream->rtpsrc);
4464 gst_element_link_pads_full (stream->rtpsrc, "src", stream->udpsink[0],
4465 "sink", GST_PAD_LINK_CHECK_NOTHING);
4468 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
4471 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
4472 stream->udpsink[1] =
4473 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
4475 if (stream->udpsink[1] == NULL)
4476 goto no_sink_element;
4478 /* don't join multicast group, we will have the source socket do that */
4479 /* no sync or async state changes needed */
4480 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
4481 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
4483 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
4485 if (stream->udpsrc[1]) {
4486 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
4487 * because some servers check the port number of where it sends RTCP to identify
4488 * the RTCP packets it receives */
4489 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
4493 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
4494 /* configure socket and make sure udpsink does not close it when shutting
4495 * down, it belongs to udpsrc after all. */
4496 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
4497 "close-socket", FALSE, NULL);
4498 g_object_unref (socket);
4501 /* we keep this playing always */
4502 gst_element_set_locked_state (stream->udpsink[1], TRUE);
4503 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
4505 gst_object_ref (stream->udpsink[1]);
4506 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
4508 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
4510 /* get session RTCP pad */
4511 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
4512 pad = gst_element_get_request_pad (src->manager, name);
4517 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
4518 gst_object_unref (pad);
4527 GST_ERROR_OBJECT (src, "no destination address specified");
4532 GST_ERROR_OBJECT (src, "no UDP sink element found");
4537 GST_ERROR_OBJECT (src, "no appsrc element found");
4542 GST_ERROR_OBJECT (src, "no fakesrc element found");
4547 GST_ERROR_OBJECT (src, "failed to create socket");
4552 /* sets up all elements needed for streaming over the specified transport.
4553 * Does not yet expose the element pads, this will be done when there is actuall
4554 * dataflow detected, which might never happen when UDP is blocked in a
4555 * firewall, for example.
4558 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
4559 GstRTSPTransport * transport)
4562 GstPad *outpad = NULL;
4563 GstPadTemplate *template;
4565 const gchar *media_type;
4568 src = stream->parent;
4570 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
4572 /* get the proper media type for this stream now */
4573 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
4574 goto unknown_transport;
4576 goto unknown_transport;
4578 /* configure the final media type */
4579 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
4581 len = stream->ptmap->len;
4582 for (i = 0; i < len; i++) {
4584 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
4586 if (item->caps == NULL)
4589 s = gst_caps_get_structure (item->caps, 0);
4590 gst_structure_set_name (s, media_type);
4591 /* set ssrc if known */
4592 if (transport->ssrc)
4593 gst_structure_set (s, "ssrc", G_TYPE_UINT, transport->ssrc, NULL);
4596 /* try to get and configure a manager, channelpad[0-1] will be configured with
4597 * the pads for the manager, or NULL when no manager is needed. */
4598 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
4601 switch (transport->lower_transport) {
4602 case GST_RTSP_LOWER_TRANS_TCP:
4603 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
4604 goto transport_failed;
4606 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
4607 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
4608 goto transport_failed;
4609 /* fallthrough, the rest is the same for UDP and MCAST */
4610 case GST_RTSP_LOWER_TRANS_UDP:
4611 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
4612 goto transport_failed;
4613 /* configure udpsinks back to the server for RTCP messages, for the
4614 * dummy RTP messages to open NAT, and for the backchannel */
4615 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
4616 goto transport_failed;
4619 goto unknown_transport;
4622 /* using backchannel and no manager, hence no srcpad for this stream */
4623 if (outpad && stream->is_backchannel) {
4624 add_backchannel_fakesink (src, stream, outpad);
4625 gst_object_unref (outpad);
4626 } else if (outpad) {
4627 GST_DEBUG_OBJECT (src, "creating ghostpad for stream %p", stream);
4629 gst_pad_use_fixed_caps (outpad);
4631 /* create ghostpad, don't add just yet, this will be done when we activate
4633 name = g_strdup_printf ("stream_%u", stream->id);
4634 template = gst_static_pad_template_get (&rtptemplate);
4635 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
4636 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
4637 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
4638 gst_object_unref (template);
4641 gst_object_unref (outpad);
4643 /* mark pad as ok */
4644 stream->last_ret = GST_FLOW_OK;
4651 GST_WARNING_OBJECT (src, "failed to configure transport");
4656 GST_WARNING_OBJECT (src, "unknown transport");
4661 GST_WARNING_OBJECT (src, "cannot get a session manager");
4666 /* send a couple of dummy random packets on the receiver RTP port to the server,
4667 * this should make a firewall think we initiated the data transfer and
4668 * hopefully allow packets to go from the sender port to our RTP receiver port */
4670 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
4674 if (src->nat_method != GST_RTSP_NAT_DUMMY)
4677 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4678 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4680 if (!stream->rtpsrc || !stream->udpsink[0])
4683 if (stream->is_backchannel)
4684 GST_DEBUG_OBJECT (src, "starting backchannel stream %p", stream);
4686 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
4688 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
4689 gst_element_set_state (stream->rtpsrc, GST_STATE_NULL);
4690 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
4691 gst_element_set_state (stream->rtpsrc, GST_STATE_PLAYING);
4696 /* Adds the source pads of all configured streams to the element.
4697 * This code is performed when we detected dataflow.
4699 * We detect dataflow from either the _loop function or with pad probes on the
4703 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
4707 GST_DEBUG_OBJECT (src, "activating streams");
4709 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4710 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4712 if (stream->udpsrc[0]) {
4713 /* remove timeout, we are streaming now and timeouts will be handled by
4714 * the session manager and jitter buffer */
4715 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
4717 if (stream->srcpad) {
4718 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
4719 gst_pad_set_active (stream->srcpad, TRUE);
4721 /* if we don't have a session manager, set the caps now. If we have a
4722 * session, we will get a notification of the pad and the caps. */
4723 if (!src->manager) {
4726 caps = stream_get_caps_for_pt (stream, stream->default_pt);
4727 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
4728 gst_pad_set_caps (stream->srcpad, caps);
4731 if (!stream->added) {
4732 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
4733 if (stream->is_backchannel)
4734 add_backchannel_fakesink (src, stream, stream->srcpad);
4736 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
4737 stream->added = TRUE;
4742 /* unblock all pads */
4743 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4744 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4746 if (stream->blockid) {
4747 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
4748 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
4749 stream->blockid = 0;
4757 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
4758 gboolean reset_manager)
4761 guint64 start, stop;
4762 gdouble play_speed, play_scale;
4764 GST_DEBUG_OBJECT (src, "configuring stream caps");
4766 start = segment->position;
4767 stop = segment->duration;
4768 play_speed = segment->rate;
4769 play_scale = segment->applied_rate;
4771 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4772 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4778 len = stream->ptmap->len;
4779 for (j = 0; j < len; j++) {
4781 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
4783 if (item->caps == NULL)
4786 caps = gst_caps_make_writable (item->caps);
4788 if (stream->timebase != -1)
4789 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
4790 (guint) stream->timebase, NULL);
4791 if (stream->seqbase != -1)
4792 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
4793 (guint) stream->seqbase, NULL);
4794 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
4796 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
4797 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
4798 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
4801 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
4804 if (item->pt == stream->default_pt) {
4805 if (stream->udpsrc[0])
4806 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
4807 stream->need_caps = TRUE;
4811 if (reset_manager && src->manager) {
4812 GST_DEBUG_OBJECT (src, "clear session");
4813 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
4817 static GstFlowReturn
4818 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
4823 /* store the value */
4824 stream->last_ret = ret;
4826 /* if it's success we can return the value right away */
4827 if (ret == GST_FLOW_OK)
4830 /* any other error that is not-linked can be returned right
4832 if (ret != GST_FLOW_NOT_LINKED)
4835 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4836 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4837 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4839 ret = ostream->last_ret;
4840 /* some other return value (must be SUCCESS but we can return
4841 * other values as well) */
4842 if (ret != GST_FLOW_NOT_LINKED)
4845 /* if we get here, all other pads were unlinked and we return
4846 * NOT_LINKED then */
4852 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
4855 gboolean res = TRUE;
4857 /* only streams that have a connection to the outside world */
4861 if (stream->udpsrc[0]) {
4862 GstEvent *sent_event;
4864 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
4865 sent_event = gst_event_new_eos ();
4866 gst_event_set_seqnum (sent_event, stream->segment_seqnum[0]);
4868 sent_event = gst_event_ref (event);
4871 res = gst_element_send_event (stream->udpsrc[0], sent_event);
4872 } else if (stream->channelpad[0]) {
4873 gst_event_ref (event);
4874 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4875 res = gst_pad_push_event (stream->channelpad[0], event);
4877 res = gst_pad_send_event (stream->channelpad[0], event);
4880 if (stream->udpsrc[1]) {
4881 GstEvent *sent_event;
4883 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
4884 sent_event = gst_event_new_eos ();
4885 gst_event_set_seqnum (sent_event, stream->segment_seqnum[1]);
4887 sent_event = gst_event_ref (event);
4890 res &= gst_element_send_event (stream->udpsrc[1], sent_event);
4891 } else if (stream->channelpad[1]) {
4892 gst_event_ref (event);
4893 if (GST_PAD_IS_SRC (stream->channelpad[1]))
4894 res &= gst_pad_push_event (stream->channelpad[1], event);
4896 res &= gst_pad_send_event (stream->channelpad[1], event);
4900 gst_event_unref (event);
4906 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
4909 gboolean res = TRUE;
4911 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4912 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4914 gst_event_ref (event);
4915 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
4917 gst_event_unref (event);
4923 accept_certificate_cb (GTlsConnection * conn, GTlsCertificate * peer_cert,
4924 GTlsCertificateFlags errors, gpointer user_data)
4926 GstRTSPSrc *src = user_data;
4927 gboolean accept = FALSE;
4929 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ACCEPT_CERTIFICATE], 0, conn,
4930 peer_cert, errors, &accept);
4935 static GstRTSPResult
4936 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4940 GstRTSPMessage response;
4941 gboolean retry = FALSE;
4942 memset (&response, 0, sizeof (response));
4943 gst_rtsp_message_init (&response);
4945 if (info->connection == NULL) {
4946 if (info->url == NULL) {
4947 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
4948 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
4951 /* create connection */
4952 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
4953 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
4954 goto could_not_create;
4957 gst_rtspsrc_setup_auth (src, &response);
4960 g_free (info->url_str);
4961 info->url_str = gst_rtsp_url_get_request_uri (info->url);
4963 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
4965 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
4966 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
4967 src->tls_validation_flags))
4968 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
4970 if (src->tls_database)
4971 gst_rtsp_connection_set_tls_database (info->connection,
4974 if (src->tls_interaction)
4975 gst_rtsp_connection_set_tls_interaction (info->connection,
4976 src->tls_interaction);
4977 gst_rtsp_connection_set_accept_certificate_func (info->connection,
4978 accept_certificate_cb, src, NULL);
4981 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
4982 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
4984 if (src->proxy_host) {
4985 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
4987 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
4992 if (!info->connected) {
4995 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
4996 ("Connecting to %s", info->location));
4997 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
4998 res = gst_rtsp_connection_connect_with_response (info->connection,
4999 src->ptcp_timeout, &response);
5001 if (response.type == GST_RTSP_MESSAGE_HTTP_RESPONSE &&
5002 response.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
5003 gst_rtsp_conninfo_close (src, info, TRUE);
5007 retry = FALSE; // we should not retry more than once
5012 if (res == GST_RTSP_OK)
5013 info->connected = TRUE;
5015 goto could_not_connect;
5017 } while (!info->connected && retry);
5019 gst_rtsp_message_unset (&response);
5025 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
5026 gst_rtsp_message_unset (&response);
5031 gchar *str = gst_rtsp_strresult (res);
5032 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
5034 gst_rtsp_message_unset (&response);
5039 gchar *str = gst_rtsp_strresult (res);
5040 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
5042 gst_rtsp_message_unset (&response);
5047 static GstRTSPResult
5048 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
5051 GST_RTSP_STATE_LOCK (src);
5052 if (info->connected) {
5053 GST_DEBUG_OBJECT (src, "closing connection...");
5054 gst_rtsp_connection_close (info->connection);
5055 info->connected = FALSE;
5057 if (free && info->connection) {
5058 /* free connection */
5059 GST_DEBUG_OBJECT (src, "freeing connection...");
5060 gst_rtsp_connection_free (info->connection);
5061 info->connection = NULL;
5062 info->flushing = FALSE;
5064 GST_RTSP_STATE_UNLOCK (src);
5068 static GstRTSPResult
5069 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
5074 GST_DEBUG_OBJECT (src, "reconnecting connection...");
5075 gst_rtsp_conninfo_close (src, info, FALSE);
5076 res = gst_rtsp_conninfo_connect (src, info, async);
5082 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
5086 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
5087 GST_RTSP_STATE_LOCK (src);
5088 if (src->conninfo.connection && src->conninfo.flushing != flush) {
5089 GST_DEBUG_OBJECT (src, "connection flush");
5090 gst_rtsp_connection_flush (src->conninfo.connection, flush);
5091 src->conninfo.flushing = flush;
5093 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5094 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5095 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
5096 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
5097 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
5098 stream->conninfo.flushing = flush;
5101 GST_RTSP_STATE_UNLOCK (src);
5104 static GstRTSPResult
5105 gst_rtspsrc_init_request (GstRTSPSrc * src, GstRTSPMessage * msg,
5106 GstRTSPMethod method, const gchar * uri)
5110 res = gst_rtsp_message_init_request (msg, method, uri);
5114 /* set user-agent */
5115 if (src->user_agent)
5116 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_USER_AGENT, src->user_agent);
5121 /* FIXME, handle server request, reply with OK, for now */
5122 static GstRTSPResult
5123 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
5124 GstRTSPMessage * request)
5126 GstRTSPMessage response = { 0 };
5129 GST_DEBUG_OBJECT (src, "got server request message");
5131 DEBUG_RTSP (src, request);
5133 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
5135 if (res == GST_RTSP_ENOTIMPL) {
5136 /* default implementation, send OK */
5137 GST_DEBUG_OBJECT (src, "prepare OK reply");
5139 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
5144 /* let app parse and reply */
5145 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
5146 0, request, &response);
5148 DEBUG_RTSP (src, &response);
5150 res = gst_rtspsrc_connection_send (src, conninfo, &response, NULL);
5154 gst_rtsp_message_unset (&response);
5155 } else if (res == GST_RTSP_EEOF)
5163 gst_rtsp_message_unset (&response);
5168 /* send server keep-alive */
5169 static GstRTSPResult
5170 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
5172 GstRTSPMessage request = { 0 };
5174 GstRTSPMethod method;
5175 const gchar *control;
5177 if (src->do_rtsp_keep_alive == FALSE) {
5178 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
5179 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
5183 GST_DEBUG_OBJECT (src, "creating server keep-alive");
5185 /* find a method to use for keep-alive */
5186 if (src->methods & GST_RTSP_GET_PARAMETER)
5187 method = GST_RTSP_GET_PARAMETER;
5189 method = GST_RTSP_OPTIONS;
5191 control = get_aggregate_control (src);
5192 if (control == NULL)
5195 res = gst_rtspsrc_init_request (src, &request, method, control);
5199 request.type_data.request.version = src->version;
5201 res = gst_rtspsrc_connection_send (src, &src->conninfo, &request, NULL);
5205 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
5206 gst_rtsp_message_unset (&request);
5213 GST_WARNING_OBJECT (src, "no control url to send keepalive");
5218 gchar *str = gst_rtsp_strresult (res);
5220 gst_rtsp_message_unset (&request);
5221 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
5222 ("Could not send keep-alive. (%s)", str));
5228 static GstFlowReturn
5229 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
5231 GstFlowReturn ret = GST_FLOW_OK;
5233 GstRTSPStream *stream;
5234 GstPad *outpad = NULL;
5240 channel = message->type_data.data.channel;
5242 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
5244 goto unknown_stream;
5246 if (channel == stream->channel[0]) {
5247 outpad = stream->channelpad[0];
5249 } else if (channel == stream->channel[1]) {
5250 outpad = stream->channelpad[1];
5256 /* take a look at the body to figure out what we have */
5257 gst_rtsp_message_get_body (message, &data, &size);
5259 goto invalid_length;
5261 /* channels are not correct on some servers, do extra check */
5262 if (data[1] >= 200 && data[1] <= 204) {
5263 /* hmm RTCP message switch to the RTCP pad of the same stream. */
5264 outpad = stream->channelpad[1];
5268 /* we have no clue what this is, just ignore then. */
5270 goto unknown_stream;
5272 /* take the message body for further processing */
5273 gst_rtsp_message_steal_body (message, &data, &size);
5275 /* strip the trailing \0 */
5278 buf = gst_buffer_new ();
5279 gst_buffer_append_memory (buf,
5280 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
5282 /* don't need message anymore */
5283 gst_rtsp_message_unset (message);
5285 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
5288 if (src->need_activate) {
5294 guint group_id = gst_util_group_id_next ();
5296 /* generate an SHA256 sum of the URI */
5297 cs = g_checksum_new (G_CHECKSUM_SHA256);
5298 uri = src->conninfo.location;
5299 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
5301 for (streams = src->streams; streams; streams = g_list_next (streams)) {
5302 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
5306 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
5307 event = gst_event_new_stream_start (stream_id);
5308 gst_event_set_group_id (event, group_id);
5311 gst_rtspsrc_stream_push_event (src, ostream, event);
5313 if ((caps = stream_get_caps_for_pt (ostream, ostream->default_pt))) {
5314 /* only streams that have a connection to the outside world */
5315 if (ostream->setup) {
5316 if (ostream->udpsrc[0]) {
5317 gst_element_send_event (ostream->udpsrc[0],
5318 gst_event_new_caps (caps));
5319 } else if (ostream->channelpad[0]) {
5320 if (GST_PAD_IS_SRC (ostream->channelpad[0]))
5321 gst_pad_push_event (ostream->channelpad[0],
5322 gst_event_new_caps (caps));
5324 gst_pad_send_event (ostream->channelpad[0],
5325 gst_event_new_caps (caps));
5327 ostream->need_caps = FALSE;
5329 if (ostream->profile == GST_RTSP_PROFILE_SAVP ||
5330 ostream->profile == GST_RTSP_PROFILE_SAVPF)
5331 caps = gst_caps_new_empty_simple ("application/x-srtcp");
5333 caps = gst_caps_new_empty_simple ("application/x-rtcp");
5335 if (ostream->udpsrc[1]) {
5336 gst_element_send_event (ostream->udpsrc[1],
5337 gst_event_new_caps (caps));
5338 } else if (ostream->channelpad[1]) {
5339 if (GST_PAD_IS_SRC (ostream->channelpad[1]))
5340 gst_pad_push_event (ostream->channelpad[1],
5341 gst_event_new_caps (caps));
5343 gst_pad_send_event (ostream->channelpad[1],
5344 gst_event_new_caps (caps));
5347 gst_caps_unref (caps);
5351 g_checksum_free (cs);
5353 gst_rtspsrc_activate_streams (src);
5354 src->need_activate = FALSE;
5355 src->need_segment = TRUE;
5358 if (src->base_time == -1) {
5359 /* Take current running_time. This timestamp will be put on
5360 * the first buffer of each stream because we are a live source and so we
5361 * timestamp with the running_time. When we are dealing with TCP, we also
5362 * only timestamp the first buffer (using the DISCONT flag) because a server
5363 * typically bursts data, for which we don't want to compensate by speeding
5364 * up the media. The other timestamps will be interpollated from this one
5365 * using the RTP timestamps. */
5366 GST_OBJECT_LOCK (src);
5367 if (GST_ELEMENT_CLOCK (src)) {
5369 GstClockTime base_time;
5371 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
5372 base_time = GST_ELEMENT_CAST (src)->base_time;
5374 src->base_time = now - base_time;
5376 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
5377 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
5379 GST_OBJECT_UNLOCK (src);
5382 /* If needed send a new segment, don't forget we are live and buffer are
5383 * timestamped with running time */
5384 if (src->need_segment) {
5386 src->need_segment = FALSE;
5387 gst_segment_init (&segment, GST_FORMAT_TIME);
5388 gst_rtspsrc_push_event (src, gst_event_new_segment (&segment));
5391 if (stream->need_caps) {
5394 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt))) {
5395 /* only streams that have a connection to the outside world */
5396 if (stream->setup) {
5397 /* Only need to update the TCP caps here, UDP is already handled */
5398 if (stream->channelpad[0]) {
5399 if (GST_PAD_IS_SRC (stream->channelpad[0]))
5400 gst_pad_push_event (stream->channelpad[0],
5401 gst_event_new_caps (caps));
5403 gst_pad_send_event (stream->channelpad[0],
5404 gst_event_new_caps (caps));
5406 stream->need_caps = FALSE;
5410 stream->need_caps = FALSE;
5413 if (stream->discont && !is_rtcp) {
5414 /* mark first RTP buffer as discont */
5415 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
5416 stream->discont = FALSE;
5417 /* first buffer gets the timestamp, other buffers are not timestamped and
5418 * their presentation time will be interpollated from the rtp timestamps. */
5419 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
5420 GST_TIME_ARGS (src->base_time));
5422 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
5425 /* chain to the peer pad */
5426 if (GST_PAD_IS_SINK (outpad))
5427 ret = gst_pad_chain (outpad, buf);
5429 ret = gst_pad_push (outpad, buf);
5432 /* combine all stream flows for the data transport */
5433 ret = gst_rtspsrc_combine_flows (src, stream, ret);
5440 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
5441 gst_rtsp_message_unset (message);
5446 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5447 ("Short message received, ignoring."));
5448 gst_rtsp_message_unset (message);
5453 static GstFlowReturn
5454 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
5456 GstRTSPMessage message = { 0 };
5458 GstFlowReturn ret = GST_FLOW_OK;
5459 GTimeVal tv_timeout;
5462 /* get the next timeout interval */
5463 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
5465 /* see if the timeout period expired */
5466 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
5467 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
5468 /* send keep-alive, only act on interrupt, a warning will be posted for
5470 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5472 /* get new timeout */
5473 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
5476 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
5477 tv_timeout.tv_sec, tv_timeout.tv_usec);
5479 /* protect the connection with the connection lock so that we can see when
5480 * we are finished doing server communication */
5482 gst_rtspsrc_connection_receive (src, &src->conninfo,
5483 &message, src->ptcp_timeout);
5487 GST_DEBUG_OBJECT (src, "we received a server message");
5489 case GST_RTSP_EINTR:
5490 /* we got interrupted this means we need to stop */
5492 case GST_RTSP_ETIMEOUT:
5493 /* no reply, send keep alive */
5494 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
5495 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5499 /* go EOS when the server closed the connection */
5505 switch (message.type) {
5506 case GST_RTSP_MESSAGE_REQUEST:
5507 /* server sends us a request message, handle it */
5508 res = gst_rtspsrc_handle_request (src, &src->conninfo, &message);
5509 if (res == GST_RTSP_EEOF)
5512 goto handle_request_failed;
5514 case GST_RTSP_MESSAGE_RESPONSE:
5515 /* we ignore response messages */
5516 GST_DEBUG_OBJECT (src, "ignoring response message");
5517 DEBUG_RTSP (src, &message);
5519 case GST_RTSP_MESSAGE_DATA:
5520 GST_DEBUG_OBJECT (src, "got data message");
5521 ret = gst_rtspsrc_handle_data (src, &message);
5522 if (ret != GST_FLOW_OK)
5523 goto handle_data_failed;
5526 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5531 g_assert_not_reached ();
5536 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5537 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5538 ("The server closed the connection."));
5539 src->conninfo.connected = FALSE;
5540 gst_rtsp_message_unset (&message);
5541 return GST_FLOW_EOS;
5545 gst_rtsp_message_unset (&message);
5546 GST_DEBUG_OBJECT (src, "got interrupted");
5547 return GST_FLOW_FLUSHING;
5551 gchar *str = gst_rtsp_strresult (res);
5553 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5554 ("Could not receive message. (%s)", str));
5557 gst_rtsp_message_unset (&message);
5558 return GST_FLOW_ERROR;
5560 handle_request_failed:
5562 gchar *str = gst_rtsp_strresult (res);
5564 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5565 ("Could not handle server message. (%s)", str));
5567 gst_rtsp_message_unset (&message);
5568 return GST_FLOW_ERROR;
5572 GST_DEBUG_OBJECT (src, "could no handle data message");
5577 static GstFlowReturn
5578 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
5581 GstRTSPMessage message = { 0 };
5585 GTimeVal tv_timeout;
5587 /* get the next timeout interval */
5588 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
5590 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
5591 (gint) tv_timeout.tv_sec);
5593 gst_rtsp_message_unset (&message);
5595 /* we should continue reading the TCP socket because the server might
5596 * send us requests. When the session timeout expires, we need to send a
5597 * keep-alive request to keep the session open. */
5598 res = gst_rtspsrc_connection_receive (src, &src->conninfo,
5599 &message, &tv_timeout);
5603 GST_DEBUG_OBJECT (src, "we received a server message");
5605 case GST_RTSP_EINTR:
5606 /* we got interrupted, see what we have to do */
5608 case GST_RTSP_ETIMEOUT:
5609 /* send keep-alive, ignore the result, a warning will be posted. */
5610 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
5611 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5615 /* server closed the connection. not very fatal for UDP, reconnect and
5616 * see what happens. */
5617 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5618 ("The server closed the connection."));
5619 if (src->udp_reconnect) {
5621 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
5628 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
5630 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5631 ("Unhandled return value %d.", res));
5635 switch (message.type) {
5636 case GST_RTSP_MESSAGE_REQUEST:
5637 /* server sends us a request message, handle it */
5638 res = gst_rtspsrc_handle_request (src, &src->conninfo, &message);
5639 if (res == GST_RTSP_EEOF)
5642 goto handle_request_failed;
5644 case GST_RTSP_MESSAGE_RESPONSE:
5645 /* we ignore response and data messages */
5646 GST_DEBUG_OBJECT (src, "ignoring response message");
5647 DEBUG_RTSP (src, &message);
5648 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
5649 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
5650 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
5651 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
5652 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5659 case GST_RTSP_MESSAGE_DATA:
5660 /* we ignore response and data messages */
5661 GST_DEBUG_OBJECT (src, "ignoring data message");
5664 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5669 g_assert_not_reached ();
5671 /* we get here when the connection got interrupted */
5674 gst_rtsp_message_unset (&message);
5675 GST_DEBUG_OBJECT (src, "got interrupted");
5676 return GST_FLOW_FLUSHING;
5680 gchar *str = gst_rtsp_strresult (res);
5683 src->conninfo.connected = FALSE;
5684 if (res != GST_RTSP_EINTR) {
5685 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
5686 ("Could not connect to server. (%s)", str));
5688 ret = GST_FLOW_ERROR;
5690 ret = GST_FLOW_FLUSHING;
5696 gchar *str = gst_rtsp_strresult (res);
5698 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5699 ("Could not receive message. (%s)", str));
5701 return GST_FLOW_ERROR;
5703 handle_request_failed:
5705 gchar *str = gst_rtsp_strresult (res);
5708 gst_rtsp_message_unset (&message);
5709 if (res != GST_RTSP_EINTR) {
5710 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5711 ("Could not handle server message. (%s)", str));
5713 ret = GST_FLOW_ERROR;
5715 ret = GST_FLOW_FLUSHING;
5721 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5722 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5723 ("The server closed the connection."));
5724 src->conninfo.connected = FALSE;
5725 gst_rtsp_message_unset (&message);
5726 return GST_FLOW_EOS;
5730 static GstRTSPResult
5731 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
5733 GstRTSPResult res = GST_RTSP_OK;
5736 GST_DEBUG_OBJECT (src, "doing reconnect");
5738 GST_OBJECT_LOCK (src);
5739 /* only restart when the pads were not yet activated, else we were
5740 * streaming over UDP */
5741 restart = src->need_activate;
5742 GST_OBJECT_UNLOCK (src);
5744 /* no need to restart, we're done */
5748 /* we can try only TCP now */
5749 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
5751 /* close and cleanup our state */
5752 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
5755 /* see if we have TCP left to try. Also don't try TCP when we were configured
5757 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
5760 /* We post a warning message now to inform the user
5761 * that nothing happened. It's most likely a firewall thing. */
5762 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5763 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5764 "firewall is blocking it. Retrying using a tcp connection.",
5765 gst_guint64_to_gdouble (src->udp_timeout) / 1000000.0));
5767 /* open new connection using tcp */
5768 if (gst_rtspsrc_open (src, async) < 0)
5771 /* start playback */
5772 if (gst_rtspsrc_play (src, &src->segment, async, NULL) < 0)
5781 src->cur_protocols = 0;
5782 /* no transport possible, post an error and stop */
5783 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5784 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5785 "firewall is blocking it. No other protocols to try.",
5786 gst_guint64_to_gdouble (src->udp_timeout) / 1000000.0));
5787 return GST_RTSP_ERROR;
5791 GST_DEBUG_OBJECT (src, "open failed");
5796 GST_DEBUG_OBJECT (src, "play failed");
5802 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
5806 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
5809 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
5812 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
5814 case CMD_GET_PARAMETER:
5815 GST_ELEMENT_PROGRESS (src, START, "request",
5816 ("Sending GET_PARAMETER request"));
5818 case CMD_SET_PARAMETER:
5819 GST_ELEMENT_PROGRESS (src, START, "request",
5820 ("Sending SET_PARAMETER request"));
5823 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
5831 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
5835 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
5838 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
5841 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
5843 case CMD_GET_PARAMETER:
5844 GST_ELEMENT_PROGRESS (src, COMPLETE, "request",
5845 ("Sent GET_PARAMETER request"));
5847 case CMD_SET_PARAMETER:
5848 GST_ELEMENT_PROGRESS (src, COMPLETE, "request",
5849 ("Sent SET_PARAMETER request"));
5852 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
5860 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
5864 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
5867 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
5870 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
5872 case CMD_GET_PARAMETER:
5873 GST_ELEMENT_PROGRESS (src, CANCELED, "request",
5874 ("GET_PARAMETER canceled"));
5876 case CMD_SET_PARAMETER:
5877 GST_ELEMENT_PROGRESS (src, CANCELED, "request",
5878 ("SET_PARAMETER canceled"));
5881 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
5889 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
5893 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
5896 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
5899 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
5901 case CMD_GET_PARAMETER:
5902 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("GET_PARAMETER failed"));
5904 case CMD_SET_PARAMETER:
5905 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("SET_PARAMETER failed"));
5908 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
5916 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
5918 if (ret == GST_RTSP_OK)
5919 gst_rtspsrc_loop_complete_cmd (src, cmd);
5920 else if (ret == GST_RTSP_EINTR)
5921 gst_rtspsrc_loop_cancel_cmd (src, cmd);
5923 gst_rtspsrc_loop_error_cmd (src, cmd);
5927 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
5930 gboolean flushed = FALSE;
5932 /* start new request */
5933 gst_rtspsrc_loop_start_cmd (src, cmd);
5935 GST_DEBUG_OBJECT (src, "sending cmd %s", cmd_to_string (cmd));
5937 GST_OBJECT_LOCK (src);
5938 old = src->pending_cmd;
5940 if (old == CMD_RECONNECT) {
5941 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
5942 cmd = CMD_RECONNECT;
5943 } else if (old == CMD_CLOSE) {
5944 /* our CMD_CLOSE might have interrutped CMD_LOOP. gst_rtspsrc_loop
5945 * will send a CMD_WAIT which would cancel our pending CMD_CLOSE (if
5946 * still pending). We just avoid it here by making sure CMD_CLOSE is
5947 * still the pending command. */
5948 GST_DEBUG_OBJECT (src, "ignore, we were closing");
5950 } else if (old == CMD_SET_PARAMETER) {
5951 GST_DEBUG_OBJECT (src, "ignore, we have a pending %s", cmd_to_string (old));
5952 cmd = CMD_SET_PARAMETER;
5953 } else if (old == CMD_GET_PARAMETER) {
5954 GST_DEBUG_OBJECT (src, "ignore, we have a pending %s", cmd_to_string (old));
5955 cmd = CMD_GET_PARAMETER;
5956 } else if (old != CMD_WAIT) {
5957 src->pending_cmd = CMD_WAIT;
5958 GST_OBJECT_UNLOCK (src);
5959 /* cancel previous request */
5960 GST_DEBUG_OBJECT (src, "cancel previous request %s", cmd_to_string (old));
5961 gst_rtspsrc_loop_cancel_cmd (src, old);
5962 GST_OBJECT_LOCK (src);
5964 src->pending_cmd = cmd;
5965 /* interrupt if allowed */
5966 if (src->busy_cmd & mask) {
5967 GST_DEBUG_OBJECT (src, "connection flush busy %s",
5968 cmd_to_string (src->busy_cmd));
5969 gst_rtspsrc_connection_flush (src, TRUE);
5972 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %s",
5973 cmd_to_string (src->busy_cmd));
5976 gst_task_start (src->task);
5977 GST_OBJECT_UNLOCK (src);
5983 gst_rtspsrc_loop_send_cmd_and_wait (GstRTSPSrc * src, gint cmd, gint mask,
5984 GstClockTime timeout)
5986 gboolean flushed = gst_rtspsrc_loop_send_cmd (src, cmd, mask);
5989 gint64 end_time = g_get_monotonic_time () + (timeout / 1000);
5990 GST_OBJECT_LOCK (src);
5991 while (src->pending_cmd == cmd || src->busy_cmd == cmd) {
5992 if (!g_cond_wait_until (&src->cmd_cond, GST_OBJECT_GET_LOCK (src),
5994 GST_WARNING_OBJECT (src,
5995 "Timed out waiting for TEARDOWN to be processed.");
5996 break; /* timeout passed */
5999 GST_OBJECT_UNLOCK (src);
6005 gst_rtspsrc_loop (GstRTSPSrc * src)
6009 if (!src->conninfo.connection || !src->conninfo.connected)
6012 if (src->interleaved)
6013 ret = gst_rtspsrc_loop_interleaved (src);
6015 ret = gst_rtspsrc_loop_udp (src);
6017 if (ret != GST_FLOW_OK)
6025 GST_WARNING_OBJECT (src, "we are not connected");
6026 ret = GST_FLOW_FLUSHING;
6031 const gchar *reason = gst_flow_get_name (ret);
6033 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
6034 src->running = FALSE;
6035 if (ret == GST_FLOW_EOS) {
6036 /* perform EOS logic */
6037 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
6038 gst_element_post_message (GST_ELEMENT_CAST (src),
6039 gst_message_new_segment_done (GST_OBJECT_CAST (src),
6040 src->segment.format, src->segment.position));
6041 gst_rtspsrc_push_event (src,
6042 gst_event_new_segment_done (src->segment.format,
6043 src->segment.position));
6045 gst_rtspsrc_push_event (src, gst_event_new_eos ());
6047 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
6048 /* for fatal errors we post an error message, post the error before the
6049 * EOS so the app knows about the error first. */
6050 GST_ELEMENT_FLOW_ERROR (src, ret);
6051 gst_rtspsrc_push_event (src, gst_event_new_eos ());
6053 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
6058 #ifndef GST_DISABLE_GST_DEBUG
6059 static const gchar *
6060 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
6064 while (method != 0) {
6081 /* Parse a WWW-Authenticate Response header and determine the
6082 * available authentication methods
6084 * This code should also cope with the fact that each WWW-Authenticate
6085 * header can contain multiple challenge methods + tokens
6087 * At the moment, for Basic auth, we just do a minimal check and don't
6088 * even parse out the realm */
6090 gst_rtspsrc_parse_auth_hdr (GstRTSPMessage * response,
6091 GstRTSPAuthMethod * methods, GstRTSPConnection * conn, gboolean * stale)
6093 GstRTSPAuthCredential **credentials, **credential;
6095 g_return_if_fail (response != NULL);
6096 g_return_if_fail (methods != NULL);
6097 g_return_if_fail (stale != NULL);
6100 gst_rtsp_message_parse_auth_credentials (response,
6101 GST_RTSP_HDR_WWW_AUTHENTICATE);
6105 credential = credentials;
6106 while (*credential) {
6107 if ((*credential)->scheme == GST_RTSP_AUTH_BASIC) {
6108 *methods |= GST_RTSP_AUTH_BASIC;
6109 } else if ((*credential)->scheme == GST_RTSP_AUTH_DIGEST) {
6110 GstRTSPAuthParam **param = (*credential)->params;
6112 *methods |= GST_RTSP_AUTH_DIGEST;
6114 gst_rtsp_connection_clear_auth_params (conn);
6118 if (strcmp ((*param)->name, "stale") == 0
6119 && g_ascii_strcasecmp ((*param)->value, "TRUE") == 0)
6121 gst_rtsp_connection_set_auth_param (conn, (*param)->name,
6130 gst_rtsp_auth_credentials_free (credentials);
6134 * gst_rtspsrc_setup_auth:
6135 * @src: the rtsp source
6137 * Configure a username and password and auth method on the
6138 * connection object based on a response we received from the
6141 * Currently, this requires that a username and password were supplied
6142 * in the uri. In the future, they may be requested on demand by sending
6143 * a message up the bus.
6145 * Returns: TRUE if authentication information could be set up correctly.
6148 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
6152 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
6153 GstRTSPAuthMethod method;
6154 GstRTSPResult auth_result;
6156 GstRTSPConnection *conn;
6157 gboolean stale = FALSE;
6159 conn = src->conninfo.connection;
6161 /* Identify the available auth methods and see if any are supported */
6162 gst_rtspsrc_parse_auth_hdr (response, &avail_methods, conn, &stale);
6164 if (avail_methods == GST_RTSP_AUTH_NONE)
6165 goto no_auth_available;
6167 /* For digest auth, if the response indicates that the session
6168 * data are stale, we just update them in the connection object and
6169 * return TRUE to retry the request */
6171 src->tried_url_auth = FALSE;
6173 url = gst_rtsp_connection_get_url (conn);
6175 /* Do we have username and password available? */
6176 if (url != NULL && !src->tried_url_auth && url->user != NULL
6177 && url->passwd != NULL) {
6180 src->tried_url_auth = TRUE;
6181 GST_DEBUG_OBJECT (src,
6182 "Attempting authentication using credentials from the URL");
6184 user = src->user_id;
6185 pass = src->user_pw;
6186 GST_DEBUG_OBJECT (src,
6187 "Attempting authentication using credentials from the properties");
6190 /* FIXME: If the url didn't contain username and password or we tried them
6191 * already, request a username and passwd from the application via some kind
6192 * of credentials request message */
6194 /* If we don't have a username and passwd at this point, bail out. */
6195 if (user == NULL || pass == NULL)
6198 /* Try to configure for each available authentication method, strongest to
6200 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
6201 /* Check if this method is available on the server */
6202 if ((method & avail_methods) == 0)
6205 /* Pass the credentials to the connection to try on the next request */
6206 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
6207 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
6208 * ignore it and end up retrying later */
6209 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
6210 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
6211 gst_rtsp_auth_method_to_string (method));
6216 if (method == GST_RTSP_AUTH_NONE)
6217 goto no_auth_available;
6223 /* Output an error indicating that we couldn't connect because there were
6224 * no supported authentication protocols */
6225 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
6226 ("No supported authentication protocol was found"));
6231 /* We don't fire an error message, we just return FALSE and let the
6232 * normal NOT_AUTHORIZED error be propagated */
6237 static GstRTSPResult
6238 gst_rtsp_src_receive_response (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
6239 GstRTSPMessage * response, GstRTSPStatusCode * code)
6241 GstRTSPStatusCode thecode;
6242 gchar *content_base = NULL;
6243 GstRTSPResult res = gst_rtspsrc_connection_receive (src, conninfo,
6244 response, src->ptcp_timeout);
6249 DEBUG_RTSP (src, response);
6251 switch (response->type) {
6252 case GST_RTSP_MESSAGE_REQUEST:
6253 res = gst_rtspsrc_handle_request (src, conninfo, response);
6254 if (res == GST_RTSP_EEOF)
6257 goto handle_request_failed;
6259 /* Not a response, receive next message */
6260 return gst_rtsp_src_receive_response (src, conninfo, response, code);
6261 case GST_RTSP_MESSAGE_RESPONSE:
6262 /* ok, a response is good */
6263 GST_DEBUG_OBJECT (src, "received response message");
6265 case GST_RTSP_MESSAGE_DATA:
6266 /* get next response */
6267 GST_DEBUG_OBJECT (src, "handle data response message");
6268 gst_rtspsrc_handle_data (src, response);
6270 /* Not a response, receive next message */
6271 return gst_rtsp_src_receive_response (src, conninfo, response, code);
6273 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
6276 /* Not a response, receive next message */
6277 return gst_rtsp_src_receive_response (src, conninfo, response, code);
6280 thecode = response->type_data.response.code;
6282 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
6284 /* if the caller wanted the result code, we store it. */
6288 /* If the request didn't succeed, bail out before doing any more */
6289 if (thecode != GST_RTSP_STS_OK)
6292 /* store new content base if any */
6293 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
6296 g_free (src->content_base);
6297 src->content_base = g_strdup (content_base);
6307 return GST_RTSP_EEOF;
6310 gchar *str = gst_rtsp_strresult (res);
6312 if (res != GST_RTSP_EINTR) {
6313 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6314 ("Could not receive message. (%s)", str));
6316 GST_WARNING_OBJECT (src, "receive interrupted");
6324 handle_request_failed:
6326 /* ERROR was posted */
6327 gst_rtsp_message_unset (response);
6332 GST_DEBUG_OBJECT (src, "we got an eof from the server");
6333 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
6334 ("The server closed the connection."));
6335 gst_rtsp_message_unset (response);
6341 static GstRTSPResult
6342 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
6343 GstRTSPMessage * request, GstRTSPMessage * response,
6344 GstRTSPStatusCode * code)
6348 gboolean allow_send = TRUE;
6351 if (!src->short_header)
6352 gst_rtsp_ext_list_before_send (src->extensions, request);
6354 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_BEFORE_SEND], 0,
6355 request, &allow_send);
6357 GST_DEBUG_OBJECT (src, "skipping message, disabled by signal");
6361 GST_DEBUG_OBJECT (src, "sending message");
6363 DEBUG_RTSP (src, request);
6365 res = gst_rtspsrc_connection_send (src, conninfo, request, src->ptcp_timeout);
6369 gst_rtsp_connection_reset_timeout (conninfo->connection);
6373 res = gst_rtsp_src_receive_response (src, conninfo, response, code);
6374 if (res == GST_RTSP_EEOF) {
6375 GST_WARNING_OBJECT (src, "server closed connection");
6376 /* only try once after reconnect, then fallthrough and error out */
6377 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
6379 /* if reconnect succeeds, try again */
6380 if ((res = gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) == 0)
6384 gst_rtsp_ext_list_after_send (src->extensions, request, response);
6390 gchar *str = gst_rtsp_strresult (res);
6392 if (res != GST_RTSP_EINTR) {
6393 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6394 ("Could not send message. (%s)", str));
6396 GST_WARNING_OBJECT (src, "send interrupted");
6405 * @src: the rtsp source
6406 * @conninfo: the connection information to send on
6407 * @request: must point to a valid request
6408 * @response: must point to an empty #GstRTSPMessage
6409 * @code: an optional code result
6410 * @versions: List of versions to try, setting it back onto the @request message
6411 * if not set, `src->version` will be used as RTSP version.
6413 * send @request and retrieve the response in @response. optionally @code can be
6414 * non-NULL in which case it will contain the status code of the response.
6416 * If This function returns #GST_RTSP_OK, @response will contain a valid response
6417 * message that should be cleaned with gst_rtsp_message_unset() after usage.
6419 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
6420 * @response message) if the response code was not 200 (OK).
6422 * If the attempt results in an authentication failure, then this will attempt
6423 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
6426 * Returns: #GST_RTSP_OK if the processing was successful.
6428 static GstRTSPResult
6429 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
6430 GstRTSPMessage * request, GstRTSPMessage * response,
6431 GstRTSPStatusCode * code, GstRTSPVersion * versions)
6433 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
6434 GstRTSPResult res = GST_RTSP_ERROR;
6437 GstRTSPMethod method = GST_RTSP_INVALID;
6438 gint version_retry = 0;
6444 /* make sure we don't loop forever */
6448 /* save method so we can disable it when the server complains */
6449 method = request->type_data.request.method;
6452 request->type_data.request.version = src->version;
6455 gst_rtspsrc_try_send (src, conninfo, request, response,
6460 case GST_RTSP_STS_UNAUTHORIZED:
6461 case GST_RTSP_STS_NOT_FOUND:
6462 if (gst_rtspsrc_setup_auth (src, response)) {
6463 /* Try the request/response again after configuring the auth info
6468 case GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED:
6469 GST_INFO_OBJECT (src, "Version %s not supported by the server",
6470 versions ? gst_rtsp_version_as_text (versions[version_retry]) :
6472 if (versions && versions[version_retry] != GST_RTSP_VERSION_INVALID) {
6473 GST_INFO_OBJECT (src, "Unsupported version %s => trying %s",
6474 gst_rtsp_version_as_text (request->type_data.request.version),
6475 gst_rtsp_version_as_text (versions[version_retry]));
6476 request->type_data.request.version = versions[version_retry];
6485 } while (retry == TRUE);
6487 /* If the user requested the code, let them handle errors, otherwise
6488 * post an error below */
6491 else if (int_code != GST_RTSP_STS_OK)
6492 goto error_response;
6499 GST_DEBUG_OBJECT (src, "got error %d", res);
6504 res = GST_RTSP_ERROR;
6506 switch (response->type_data.response.code) {
6507 case GST_RTSP_STS_NOT_FOUND:
6508 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, NOT_FOUND,
6511 case GST_RTSP_STS_UNAUTHORIZED:
6512 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, NOT_AUTHORIZED,
6515 case GST_RTSP_STS_MOVED_PERMANENTLY:
6516 case GST_RTSP_STS_MOVE_TEMPORARILY:
6518 gchar *new_location;
6519 GstRTSPLowerTrans transports;
6521 GST_DEBUG_OBJECT (src, "got redirection");
6522 /* if we don't have a Location Header, we must error */
6523 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
6524 &new_location, 0) < 0)
6527 /* When we receive a redirect result, we go back to the INIT state after
6528 * parsing the new URI. The caller should do the needed steps to issue
6529 * a new setup when it detects this state change. */
6530 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
6532 /* save current transports */
6533 if (src->conninfo.url)
6534 transports = src->conninfo.url->transports;
6536 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
6538 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
6540 /* set old transports */
6541 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
6542 src->conninfo.url->transports = transports;
6544 src->need_redirect = TRUE;
6548 case GST_RTSP_STS_NOT_ACCEPTABLE:
6549 case GST_RTSP_STS_NOT_IMPLEMENTED:
6550 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
6551 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
6552 gst_rtsp_method_as_text (method));
6553 src->methods &= ~method;
6557 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, READ,
6561 /* if we return ERROR we should unset the response ourselves */
6562 if (res == GST_RTSP_ERROR)
6563 gst_rtsp_message_unset (response);
6569 static GstRTSPResult
6570 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
6571 GstRTSPMessage * response, GstRTSPSrc * src)
6573 return gst_rtspsrc_send (src, &src->conninfo, request, response, NULL, NULL);
6577 /* parse the response and collect all the supported methods. We need this
6578 * information so that we don't try to send an unsupported request to the
6582 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
6584 GstRTSPHeaderField field;
6588 /* reset supported methods */
6591 /* Try Allow Header first */
6592 field = GST_RTSP_HDR_ALLOW;
6595 gst_rtsp_message_get_header (response, field, &respoptions, indx);
6599 src->methods |= gst_rtsp_options_from_text (respoptions);
6605 field = GST_RTSP_HDR_PUBLIC;
6608 gst_rtsp_message_get_header (response, field, &respoptions, indx);
6612 src->methods |= gst_rtsp_options_from_text (respoptions);
6617 if (src->methods == 0) {
6618 /* neither Allow nor Public are required, assume the server supports
6619 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
6621 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
6622 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
6624 /* always assume PLAY, FIXME, extensions should be able to override
6626 src->methods |= GST_RTSP_PLAY;
6627 /* also assume it will support Range */
6628 src->seekable = G_MAXFLOAT;
6630 /* we need describe and setup */
6631 if (!(src->methods & GST_RTSP_DESCRIBE))
6633 if (!(src->methods & GST_RTSP_SETUP))
6641 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
6642 ("Server does not support DESCRIBE."));
6647 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
6648 ("Server does not support SETUP."));
6653 /* masks to be kept in sync with the hardcoded protocol order of preference
6655 static const guint protocol_masks[] = {
6656 GST_RTSP_LOWER_TRANS_UDP,
6657 GST_RTSP_LOWER_TRANS_UDP_MCAST,
6658 GST_RTSP_LOWER_TRANS_TCP,
6662 static GstRTSPResult
6663 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
6664 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
6668 gboolean add_udp_str;
6673 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
6678 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
6680 /* extension listed transports, use those */
6681 if (*transports != NULL)
6684 /* it's the default */
6685 add_udp_str = FALSE;
6687 /* the default RTSP transports */
6688 result = g_string_new ("RTP");
6691 case GST_RTSP_PROFILE_AVP:
6692 g_string_append (result, "/AVP");
6694 case GST_RTSP_PROFILE_SAVP:
6695 g_string_append (result, "/SAVP");
6697 case GST_RTSP_PROFILE_AVPF:
6698 g_string_append (result, "/AVPF");
6700 case GST_RTSP_PROFILE_SAVPF:
6701 g_string_append (result, "/SAVPF");
6707 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
6708 GST_DEBUG_OBJECT (src, "adding UDP unicast");
6710 g_string_append (result, "/UDP");
6711 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
6712 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
6713 GST_DEBUG_OBJECT (src, "adding UDP multicast");
6714 /* we don't have to allocate any UDP ports yet, if the selected transport
6715 * turns out to be multicast we can create them and join the multicast
6716 * group indicated in the transport reply */
6718 g_string_append (result, "/UDP");
6719 g_string_append (result, ";multicast");
6720 if (src->next_port_num != 0) {
6721 if (src->client_port_range.max > 0 &&
6722 src->next_port_num >= src->client_port_range.max)
6725 g_string_append_printf (result, ";client_port=%d-%d",
6726 src->next_port_num, src->next_port_num + 1);
6728 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
6729 GST_DEBUG_OBJECT (src, "adding TCP");
6731 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
6733 *transports = g_string_free (result, FALSE);
6735 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
6742 GST_ERROR ("extension gave error %d", res);
6747 GST_ERROR ("no more ports available");
6748 return GST_RTSP_ERROR;
6752 static GstRTSPResult
6753 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
6754 gint orig_rtpport, gint orig_rtcpport)
6757 gint nr_udp, nr_int;
6759 gint rtpport = 0, rtcpport = 0;
6762 src = stream->parent;
6764 /* find number of placeholders first */
6765 if (strstr (*transports, "%%i2"))
6767 else if (strstr (*transports, "%%i1"))
6772 if (strstr (*transports, "%%u2"))
6774 else if (strstr (*transports, "%%u1"))
6779 if (nr_udp == 0 && nr_int == 0)
6783 if (!orig_rtpport || !orig_rtcpport) {
6784 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
6787 rtpport = orig_rtpport;
6788 rtcpport = orig_rtcpport;
6792 str = g_string_new ("");
6794 while ((next = strstr (p, "%%"))) {
6795 g_string_append_len (str, p, next - p);
6796 if (next[2] == 'u') {
6798 g_string_append_printf (str, "%d", rtpport);
6799 else if (next[3] == '2')
6800 g_string_append_printf (str, "%d", rtcpport);
6802 if (next[2] == 'i') {
6804 g_string_append_printf (str, "%d", src->free_channel);
6805 else if (next[3] == '2')
6806 g_string_append_printf (str, "%d", src->free_channel + 1);
6812 if (src->version >= GST_RTSP_VERSION_2_0)
6813 src->free_channel += 2;
6815 /* append final part */
6816 g_string_append (str, p);
6818 g_free (*transports);
6819 *transports = g_string_free (str, FALSE);
6827 GST_ERROR ("failed to allocate udp ports");
6828 return GST_RTSP_ERROR;
6833 signal_get_srtcp_params (GstRTSPSrc * src, GstRTSPStream * stream)
6835 GstCaps *caps = NULL;
6837 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY], 0,
6841 GST_DEBUG_OBJECT (src, "SRTP parameters received");
6847 default_srtcp_params (void)
6854 guint data_size = GST_ROUND_UP_4 (KEY_SIZE);
6856 /* create a random key */
6857 key_data = g_malloc (data_size);
6858 for (i = 0; i < data_size; i += 4)
6859 GST_WRITE_UINT32_BE (key_data + i, g_random_int ());
6861 buf = gst_buffer_new_wrapped (key_data, KEY_SIZE);
6863 caps = gst_caps_new_simple ("application/x-srtcp",
6864 "srtp-key", GST_TYPE_BUFFER, buf,
6865 "srtp-cipher", G_TYPE_STRING, "aes-128-icm",
6866 "srtp-auth", G_TYPE_STRING, "hmac-sha1-80",
6867 "srtcp-cipher", G_TYPE_STRING, "aes-128-icm",
6868 "srtcp-auth", G_TYPE_STRING, "hmac-sha1-80", NULL);
6870 gst_buffer_unref (buf);
6876 gst_rtspsrc_stream_make_keymgmt (GstRTSPSrc * src, GstRTSPStream * stream)
6878 gchar *base64, *result = NULL;
6879 GstMIKEYMessage *mikey_msg;
6881 stream->srtcpparams = signal_get_srtcp_params (src, stream);
6882 if (stream->srtcpparams == NULL)
6883 stream->srtcpparams = default_srtcp_params ();
6885 mikey_msg = gst_mikey_message_new_from_caps (stream->srtcpparams);
6887 /* add policy '0' for our SSRC */
6888 gst_mikey_message_add_cs_srtp (mikey_msg, 0, stream->send_ssrc, 0);
6890 base64 = gst_mikey_message_base64_encode (mikey_msg);
6891 gst_mikey_message_unref (mikey_msg);
6894 result = gst_sdp_make_keymgmt (stream->conninfo.location, base64);
6902 static GstRTSPResult
6903 gst_rtsp_src_setup_stream_from_response (GstRTSPSrc * src,
6904 GstRTSPStream * stream, GstRTSPMessage * response,
6905 GstRTSPLowerTrans * protocols, gint retry, gint * rtpport, gint * rtcpport)
6907 gchar *resptrans = NULL;
6908 GstRTSPTransport transport = { 0 };
6910 gst_rtsp_message_get_header (response, GST_RTSP_HDR_TRANSPORT, &resptrans, 0);
6912 gst_rtspsrc_stream_free_udp (stream);
6916 /* parse transport, go to next stream on parse error */
6917 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
6918 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
6919 return GST_RTSP_ELAST;
6922 /* update allowed transports for other streams. once the transport of
6923 * one stream has been determined, we make sure that all other streams
6924 * are configured in the same way */
6925 switch (transport.lower_transport) {
6926 case GST_RTSP_LOWER_TRANS_TCP:
6927 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
6929 *protocols = GST_RTSP_LOWER_TRANS_TCP;
6930 src->interleaved = TRUE;
6931 if (src->version < GST_RTSP_VERSION_2_0) {
6932 /* update free channels */
6933 src->free_channel = MAX (transport.interleaved.min, src->free_channel);
6934 src->free_channel = MAX (transport.interleaved.max, src->free_channel);
6935 src->free_channel++;
6938 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
6939 /* only allow multicast for other streams */
6940 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
6942 *protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
6943 /* if the server selected our ports, increment our counters so that
6944 * we select a new port later */
6945 if (src->next_port_num == transport.port.min &&
6946 src->next_port_num + 1 == transport.port.max) {
6947 src->next_port_num += 2;
6950 case GST_RTSP_LOWER_TRANS_UDP:
6951 /* only allow unicast for other streams */
6952 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
6954 *protocols = GST_RTSP_LOWER_TRANS_UDP;
6957 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
6958 transport.lower_transport);
6962 if (!src->interleaved || !retry) {
6963 /* now configure the stream with the selected transport */
6964 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
6965 GST_DEBUG_OBJECT (src,
6966 "could not configure stream %p transport, skipping stream", stream);
6968 } else if (stream->udpsrc[0] && stream->udpsrc[1] && rtpport && rtcpport) {
6969 /* retain the first allocated UDP port pair */
6970 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", rtpport, NULL);
6971 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", rtcpport, NULL);
6974 /* we need to activate at least one stream when we detect activity */
6975 src->need_activate = TRUE;
6977 /* stream is setup now */
6978 stream->setup = TRUE;
6979 stream->waiting_setup_response = FALSE;
6981 if (src->version >= GST_RTSP_VERSION_2_0) {
6982 gchar *prop, *media_properties;
6986 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_MEDIA_PROPERTIES,
6987 &media_properties, 0) != GST_RTSP_OK) {
6988 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6989 ("Error: No MEDIA_PROPERTY header in a SETUP request in RTSP 2.0"
6990 " - this header is mandatory."));
6992 gst_rtsp_message_unset (response);
6993 return GST_RTSP_ERROR;
6996 props = g_strsplit (media_properties, ",", -2);
6997 for (i = 0; props[i]; i++) {
7000 while (*prop == ' ')
7003 if (strstr (prop, "Random-Access")) {
7004 gchar **random_seekable_val = g_strsplit (prop, "=", 2);
7006 if (!random_seekable_val[1])
7007 src->seekable = G_MAXFLOAT;
7009 src->seekable = g_ascii_strtod (random_seekable_val[1], NULL);
7011 g_strfreev (random_seekable_val);
7012 } else if (!g_strcmp0 (prop, "No-Seeking")) {
7013 src->seekable = -1.0;
7014 } else if (!g_strcmp0 (prop, "Beginning-Only")) {
7015 src->seekable = 0.0;
7023 /* clean up our transport struct */
7024 gst_rtsp_transport_init (&transport);
7025 /* clean up used RTSP messages */
7026 gst_rtsp_message_unset (response);
7032 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7033 ("Server did not select transport."));
7035 gst_rtsp_message_unset (response);
7036 return GST_RTSP_ERROR;
7040 static GstRTSPResult
7041 gst_rtspsrc_setup_streams_end (GstRTSPSrc * src, gboolean async)
7044 GstRTSPConnInfo *conninfo;
7046 g_assert (src->version >= GST_RTSP_VERSION_2_0);
7048 conninfo = &src->conninfo;
7049 for (tmp = src->streams; tmp; tmp = tmp->next) {
7050 GstRTSPStream *stream = (GstRTSPStream *) tmp->data;
7051 GstRTSPMessage response = { 0, };
7053 if (!stream->waiting_setup_response)
7056 if (!src->conninfo.connection)
7057 conninfo = &((GstRTSPStream *) tmp->data)->conninfo;
7059 gst_rtsp_src_receive_response (src, conninfo, &response, NULL);
7061 gst_rtsp_src_setup_stream_from_response (src, stream,
7062 &response, NULL, 0, NULL, NULL);
7068 /* Perform the SETUP request for all the streams.
7070 * We ask the server for a specific transport, which initially includes all the
7071 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
7072 * two local UDP ports that we send to the server.
7074 * Once the server replied with a transport, we configure the other streams
7075 * with the same transport.
7077 * In case setup request are not pipelined, this function will also configure the
7078 * stream for the selected transport, * which basically means creating the pipeline.
7079 * Otherwise, the first stream is setup right away from the reply and a
7080 * CMD_FINALIZE_SETUP command is set for the stream pipelines to happen on the
7081 * remaining streams from the RTSP thread.
7083 static GstRTSPResult
7084 gst_rtspsrc_setup_streams_start (GstRTSPSrc * src, gboolean async)
7087 GstRTSPResult res = GST_RTSP_ERROR;
7088 GstRTSPMessage request = { 0 };
7089 GstRTSPMessage response = { 0 };
7090 GstRTSPStream *stream = NULL;
7091 GstRTSPLowerTrans protocols;
7092 GstRTSPStatusCode code;
7093 gboolean unsupported_real = FALSE;
7094 gint rtpport, rtcpport;
7097 gchar *pipelined_request_id = NULL;
7099 if (src->conninfo.connection) {
7100 url = gst_rtsp_connection_get_url (src->conninfo.connection);
7101 /* we initially allow all configured lower transports. based on the URL
7102 * transports and the replies from the server we narrow them down. */
7103 protocols = url->transports & src->cur_protocols;
7106 protocols = src->cur_protocols;
7112 /* reset some state */
7113 src->free_channel = 0;
7114 src->interleaved = FALSE;
7115 src->need_activate = FALSE;
7116 /* keep track of next port number, 0 is random */
7117 src->next_port_num = src->client_port_range.min;
7118 rtpport = rtcpport = 0;
7120 if (G_UNLIKELY (src->streams == NULL))
7123 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7124 GstRTSPConnInfo *conninfo;
7131 stream = (GstRTSPStream *) walk->data;
7133 caps = stream_get_caps_for_pt (stream, stream->default_pt);
7135 GST_WARNING_OBJECT (src, "skipping stream %p, no caps", stream);
7139 if (stream->skipped) {
7140 GST_DEBUG_OBJECT (src, "skipping stream %p", stream);
7144 /* see if we need to configure this stream */
7145 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
7146 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
7151 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
7152 stream->id, caps, &selected);
7154 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
7158 /* merge/overwrite global caps */
7163 s = gst_caps_get_structure (caps, 0);
7165 num = gst_structure_n_fields (src->props);
7166 for (j = 0; j < num; j++) {
7170 name = gst_structure_nth_field_name (src->props, j);
7171 val = gst_structure_get_value (src->props, name);
7172 gst_structure_set_value (s, name, val);
7174 GST_DEBUG_OBJECT (src, "copied %s", name);
7178 /* skip setup if we have no URL for it */
7179 if (stream->conninfo.location == NULL) {
7180 GST_WARNING_OBJECT (src, "skipping stream %p, no setup", stream);
7184 if (src->conninfo.connection == NULL) {
7185 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
7186 GST_WARNING_OBJECT (src, "skipping stream %p, failed to connect",
7190 conninfo = &stream->conninfo;
7192 conninfo = &src->conninfo;
7194 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
7195 stream->conninfo.location);
7197 /* if we have a multicast connection, only suggest multicast from now on */
7198 if (stream->is_multicast)
7199 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
7202 /* first selectable protocol */
7203 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
7205 if (!protocol_masks[mask])
7209 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
7210 protocol_masks[mask]);
7211 /* create a string with first transport in line */
7213 res = gst_rtspsrc_create_transports_string (src,
7214 protocols & protocol_masks[mask], stream->profile, &transports);
7215 if (res < 0 || transports == NULL)
7216 goto setup_transport_failed;
7218 if (strlen (transports) == 0) {
7219 g_free (transports);
7220 GST_DEBUG_OBJECT (src, "no transports found");
7225 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
7227 /* replace placeholders with real values, this function will optionally
7228 * allocate UDP ports and other info needed to execute the setup request */
7229 res = gst_rtspsrc_prepare_transports (stream, &transports,
7230 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
7232 g_free (transports);
7233 goto setup_transport_failed;
7236 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
7237 /* create SETUP request */
7239 gst_rtspsrc_init_request (src, &request, GST_RTSP_SETUP,
7240 stream->conninfo.location);
7242 g_free (transports);
7243 goto create_request_failed;
7246 if (src->version >= GST_RTSP_VERSION_2_0) {
7247 if (!pipelined_request_id)
7248 pipelined_request_id = g_strdup_printf ("%d",
7249 g_random_int_range (0, G_MAXINT32));
7251 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_PIPELINED_REQUESTS,
7252 pipelined_request_id);
7253 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT_RANGES,
7254 "npt, clock, smpte, clock");
7257 /* select transport */
7258 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
7260 if (stream->is_backchannel && src->backchannel == BACKCHANNEL_ONVIF)
7261 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
7262 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
7265 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
7266 stream->profile == GST_RTSP_PROFILE_SAVPF) {
7267 hval = gst_rtspsrc_stream_make_keymgmt (src, stream);
7268 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
7271 /* if the user wants a non default RTP packet size we add the blocksize
7273 if (src->rtp_blocksize > 0) {
7274 hval = g_strdup_printf ("%d", src->rtp_blocksize);
7275 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
7279 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
7282 /* handle the code ourselves */
7284 gst_rtspsrc_send (src, conninfo, &request,
7285 pipelined_request_id ? NULL : &response, &code, NULL);
7290 case GST_RTSP_STS_OK:
7292 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
7293 gst_rtsp_message_unset (&request);
7294 gst_rtsp_message_unset (&response);
7295 /* cleanup of leftover transport */
7296 gst_rtspsrc_stream_free_udp (stream);
7297 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
7298 * we might be in this case */
7299 if (stream->container && rtpport && rtcpport && !retry) {
7300 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
7305 /* this transport did not go down well, but we may have others to try
7306 * that we did not send yet, try those and only give up then
7307 * but not without checking for lost cause/extension so we can
7308 * post a nicer/more useful error message later */
7309 if (!unsupported_real)
7310 unsupported_real = stream->is_real;
7311 /* select next available protocol, give up on this stream if none */
7313 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
7315 if (!protocol_masks[mask] || unsupported_real)
7320 /* cleanup of leftover transport and move to the next stream */
7321 gst_rtspsrc_stream_free_udp (stream);
7322 goto response_error;
7326 if (!pipelined_request_id) {
7327 /* parse response transport */
7328 res = gst_rtsp_src_setup_stream_from_response (src, stream,
7329 &response, &protocols, retry, &rtpport, &rtcpport);
7331 case GST_RTSP_ERROR:
7333 case GST_RTSP_ELAST:
7339 stream->waiting_setup_response = TRUE;
7340 /* we need to activate at least one stream when we detect activity */
7341 src->need_activate = TRUE;
7348 GstRTSPStream *sskip;
7350 skip = g_list_next (skip);
7354 sskip = (GstRTSPStream *) skip->data;
7356 /* skip all streams with the same control url */
7357 if (g_str_equal (stream->conninfo.location, sskip->conninfo.location)) {
7358 GST_DEBUG_OBJECT (src, "found stream %p with same control %s",
7359 sskip, sskip->conninfo.location);
7360 sskip->skipped = TRUE;
7364 gst_rtsp_message_unset (&request);
7367 if (pipelined_request_id) {
7368 gst_rtspsrc_setup_streams_end (src, TRUE);
7371 /* store the transport protocol that was configured */
7372 src->cur_protocols = protocols;
7374 gst_rtsp_ext_list_stream_select (src->extensions, url);
7376 if (pipelined_request_id)
7377 g_free (pipelined_request_id);
7379 /* if there is nothing to activate, error out */
7380 if (!src->need_activate)
7381 goto nothing_to_activate;
7388 /* no transport possible, post an error and stop */
7389 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
7390 ("Could not connect to server, no protocols left"));
7391 return GST_RTSP_ERROR;
7395 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7396 ("SDP contains no streams"));
7397 return GST_RTSP_ERROR;
7399 create_request_failed:
7401 gchar *str = gst_rtsp_strresult (res);
7403 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7404 ("Could not create request. (%s)", str));
7408 setup_transport_failed:
7410 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7411 ("Could not setup transport."));
7412 res = GST_RTSP_ERROR;
7417 const gchar *str = gst_rtsp_status_as_text (code);
7419 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7420 ("Error (%d): %s", code, GST_STR_NULL (str)));
7421 res = GST_RTSP_ERROR;
7426 gchar *str = gst_rtsp_strresult (res);
7428 if (res != GST_RTSP_EINTR) {
7429 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7430 ("Could not send message. (%s)", str));
7432 GST_WARNING_OBJECT (src, "send interrupted");
7437 nothing_to_activate:
7439 /* none of the available error codes is really right .. */
7440 if (unsupported_real) {
7441 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
7442 (_("No supported stream was found. You might need to install a "
7443 "GStreamer RTSP extension plugin for Real media streams.")),
7446 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
7447 (_("No supported stream was found. You might need to allow "
7448 "more transport protocols or may otherwise be missing "
7449 "the right GStreamer RTSP extension plugin.")), (NULL));
7451 return GST_RTSP_ERROR;
7455 if (pipelined_request_id)
7456 g_free (pipelined_request_id);
7457 gst_rtsp_message_unset (&request);
7458 gst_rtsp_message_unset (&response);
7464 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
7465 GstSegment * segment)
7468 GstRTSPTimeRange *therange;
7471 gst_rtsp_range_free (src->range);
7473 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
7474 GST_DEBUG_OBJECT (src, "parsed range %s", range);
7475 src->range = therange;
7477 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
7479 gst_segment_init (segment, GST_FORMAT_TIME);
7483 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
7484 therange->min.type, therange->min.seconds, therange->max.type,
7485 therange->max.seconds);
7487 if (therange->min.type == GST_RTSP_TIME_NOW)
7489 else if (therange->min.type == GST_RTSP_TIME_END)
7492 seconds = therange->min.seconds * GST_SECOND;
7494 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
7495 GST_TIME_ARGS (seconds));
7497 /* we need to start playback without clipping from the position reported by
7499 segment->start = seconds;
7500 segment->position = seconds;
7502 if (therange->max.type == GST_RTSP_TIME_NOW)
7504 else if (therange->max.type == GST_RTSP_TIME_END)
7507 seconds = therange->max.seconds * GST_SECOND;
7509 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
7510 GST_TIME_ARGS (seconds));
7512 /* live (WMS) server might send overflowed large max as its idea of infinity,
7513 * compensate to prevent problems later on */
7514 if (seconds != -1 && seconds < 0) {
7516 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
7519 /* live (WMS) might send min == max, which is not worth recording */
7520 if (segment->duration == -1 && seconds == segment->start)
7523 /* don't change duration with unknown value, we might have a valid value
7524 * there that we want to keep. */
7526 segment->duration = seconds;
7531 /* Parse clock profived by the server with following syntax:
7533 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
7536 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
7538 gboolean res = FALSE;
7540 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
7541 gchar **fields = NULL, **parts = NULL;
7542 gchar *remote_ip, *str;
7544 GstClockTime base_time;
7547 fields = g_strsplit (gstclock, " ", 0);
7549 /* wrapped clock, not very interesting for now */
7550 if (fields[1] == NULL)
7553 /* remote IP address and port */
7554 if ((str = fields[2]) == NULL)
7557 parts = g_strsplit (str, ":", 0);
7559 if ((remote_ip = parts[0]) == NULL)
7562 if ((str = parts[1]) == NULL)
7570 if ((str = fields[3]) == NULL)
7573 base_time = g_ascii_strtoull (str, NULL, 10);
7576 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
7579 if (src->provided_clock)
7580 gst_object_unref (src->provided_clock);
7581 src->provided_clock = netclock;
7583 gst_element_post_message (GST_ELEMENT_CAST (src),
7584 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
7585 src->provided_clock, TRUE));
7589 g_strfreev (fields);
7595 /* must be called with the RTSP state lock */
7596 static GstRTSPResult
7597 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
7603 /* prepare global stream caps properties */
7605 gst_structure_remove_all_fields (src->props);
7607 src->props = gst_structure_new_empty ("RTSPProperties");
7609 DEBUG_SDP (src, sdp);
7611 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
7613 /* let the app inspect and change the SDP */
7614 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
7616 gst_segment_init (&src->segment, GST_FORMAT_TIME);
7618 /* parse range for duration reporting. */
7623 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
7627 /* keep track of the range and configure it in the segment */
7628 if (gst_rtspsrc_parse_range (src, range, &src->segment))
7632 /* parse clock information. This is GStreamer specific, a server can tell the
7633 * client what clock it is using and wrap that in a network clock. The
7634 * advantage of that is that we can slave to it. */
7636 const gchar *gstclock;
7639 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
7640 if (gstclock == NULL)
7643 /* parse the clock and expose it in the provide_clock method */
7644 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
7648 /* try to find a global control attribute. Note that a '*' means that we should
7649 * do aggregate control with the current url (so we don't do anything and
7650 * leave the current connection as is) */
7652 const gchar *control;
7655 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
7656 if (control == NULL)
7659 /* only take fully qualified urls */
7660 if (g_str_has_prefix (control, "rtsp://"))
7664 g_free (src->conninfo.location);
7665 src->conninfo.location = g_strdup (control);
7666 /* make a connection for this, if there was a connection already, nothing
7668 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
7669 GST_ERROR_OBJECT (src, "could not connect");
7672 /* we need to keep the control url separate from the connection url because
7673 * the rules for constructing the media control url need it */
7674 g_free (src->control);
7675 src->control = g_strdup (control);
7678 /* create streams */
7679 n_streams = gst_sdp_message_medias_len (sdp);
7680 for (i = 0; i < n_streams; i++) {
7681 gst_rtspsrc_create_stream (src, sdp, i, n_streams);
7684 src->state = GST_RTSP_STATE_INIT;
7687 if ((res = gst_rtspsrc_setup_streams_start (src, async)) < 0)
7690 /* reset our state */
7691 src->need_range = TRUE;
7694 src->state = GST_RTSP_STATE_READY;
7701 GST_ERROR_OBJECT (src, "setup failed");
7702 gst_rtspsrc_cleanup (src);
7707 static GstRTSPResult
7708 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
7712 GstRTSPMessage request = { 0 };
7713 GstRTSPMessage response = { 0 };
7716 gchar *respcont = NULL;
7717 GstRTSPVersion versions[] =
7718 { GST_RTSP_VERSION_2_0, GST_RTSP_VERSION_INVALID };
7720 src->version = src->default_version;
7721 if (src->default_version == GST_RTSP_VERSION_2_0) {
7722 versions[0] = GST_RTSP_VERSION_1_0;
7726 src->need_redirect = FALSE;
7728 /* can't continue without a valid url */
7729 if (G_UNLIKELY (src->conninfo.url == NULL)) {
7730 res = GST_RTSP_EINVAL;
7733 src->tried_url_auth = FALSE;
7735 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
7736 goto connect_failed;
7738 /* create OPTIONS */
7739 GST_DEBUG_OBJECT (src, "create options... (%s)", async ? "async" : "sync");
7741 gst_rtspsrc_init_request (src, &request, GST_RTSP_OPTIONS,
7742 src->conninfo.url_str);
7744 goto create_request_failed;
7747 request.type_data.request.version = src->version;
7748 GST_DEBUG_OBJECT (src, "send options...");
7751 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
7754 gst_rtspsrc_send (src, &src->conninfo, &request, &response,
7755 NULL, versions)) < 0) {
7759 src->version = request.type_data.request.version;
7760 GST_INFO_OBJECT (src, "Now using version: %s",
7761 gst_rtsp_version_as_text (src->version));
7764 if (!gst_rtspsrc_parse_methods (src, &response))
7767 /* create DESCRIBE */
7768 GST_DEBUG_OBJECT (src, "create describe...");
7770 gst_rtspsrc_init_request (src, &request, GST_RTSP_DESCRIBE,
7771 src->conninfo.url_str);
7773 goto create_request_failed;
7775 /* we only accept SDP for now */
7776 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
7779 if (src->backchannel == BACKCHANNEL_ONVIF)
7780 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
7781 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
7782 /* TODO: Handle the case when backchannel is unsupported and goto restart */
7785 GST_DEBUG_OBJECT (src, "send describe...");
7788 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
7791 gst_rtspsrc_send (src, &src->conninfo, &request, &response,
7795 /* we only perform redirect for describe and play, currently */
7796 if (src->need_redirect) {
7797 /* close connection, we don't have to send a TEARDOWN yet, ignore the
7799 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7801 gst_rtsp_message_unset (&request);
7802 gst_rtsp_message_unset (&response);
7808 /* it could be that the DESCRIBE method was not implemented */
7809 if (!(src->methods & GST_RTSP_DESCRIBE))
7812 /* check if reply is SDP */
7813 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
7815 /* could not be set but since the request returned OK, we assume it
7816 * was SDP, else check it. */
7818 const gchar *props = strchr (respcont, ';');
7821 gchar *mimetype = g_strndup (respcont, props - respcont);
7823 mimetype = g_strstrip (mimetype);
7824 if (g_ascii_strcasecmp (mimetype, "application/sdp") != 0) {
7826 goto wrong_content_type;
7829 /* TODO: Check for charset property and do conversions of all messages if
7830 * needed. Some servers actually send that property */
7833 } else if (g_ascii_strcasecmp (respcont, "application/sdp") != 0) {
7834 goto wrong_content_type;
7838 /* get message body and parse as SDP */
7839 gst_rtsp_message_get_body (&response, &data, &size);
7840 if (data == NULL || size == 0)
7843 GST_DEBUG_OBJECT (src, "parse SDP...");
7844 gst_sdp_message_new (sdp);
7845 gst_sdp_message_parse_buffer (data, size, *sdp);
7847 /* clean up any messages */
7848 gst_rtsp_message_unset (&request);
7849 gst_rtsp_message_unset (&response);
7856 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
7857 ("No valid RTSP URL was provided"));
7862 gchar *str = gst_rtsp_strresult (res);
7864 if (res != GST_RTSP_EINTR) {
7865 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
7866 ("Failed to connect. (%s)", str));
7868 GST_WARNING_OBJECT (src, "connect interrupted");
7873 create_request_failed:
7875 gchar *str = gst_rtsp_strresult (res);
7877 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7878 ("Could not create request. (%s)", str));
7884 /* Don't post a message - the rtsp_send method will have
7885 * taken care of it because we passed NULL for the response code */
7890 /* error was posted */
7891 res = GST_RTSP_ERROR;
7896 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7897 ("Server does not support SDP, got %s.", respcont));
7898 res = GST_RTSP_ERROR;
7903 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7904 ("Server can not provide an SDP."));
7905 res = GST_RTSP_ERROR;
7910 if (src->conninfo.connection) {
7911 GST_DEBUG_OBJECT (src, "free connection");
7912 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7914 gst_rtsp_message_unset (&request);
7915 gst_rtsp_message_unset (&response);
7920 static GstRTSPResult
7921 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
7926 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
7928 if (src->sdp == NULL) {
7929 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
7933 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
7938 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
7945 GST_WARNING_OBJECT (src, "can't get sdp");
7946 src->open_error = TRUE;
7951 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
7952 src->open_error = TRUE;
7957 static GstRTSPResult
7958 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
7960 GstRTSPMessage request = { 0 };
7961 GstRTSPMessage response = { 0 };
7962 GstRTSPResult res = GST_RTSP_OK;
7964 const gchar *control;
7966 GST_DEBUG_OBJECT (src, "TEARDOWN...");
7968 gst_rtspsrc_set_state (src, GST_STATE_READY);
7970 if (src->state < GST_RTSP_STATE_READY) {
7971 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
7978 /* construct a control url */
7979 control = get_aggregate_control (src);
7981 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
7984 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7985 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7986 const gchar *setup_url;
7987 GstRTSPConnInfo *info;
7989 /* try aggregate control first but do non-aggregate control otherwise */
7991 setup_url = control;
7992 else if ((setup_url = stream->conninfo.location) == NULL)
7995 if (src->conninfo.connection) {
7996 info = &src->conninfo;
7997 } else if (stream->conninfo.connection) {
7998 info = &stream->conninfo;
8002 if (!info->connected)
8007 gst_rtspsrc_init_request (src, &request, GST_RTSP_TEARDOWN, setup_url);
8008 GST_LOG_OBJECT (src, "Teardown on %s", setup_url);
8010 goto create_request_failed;
8012 if (stream->is_backchannel && src->backchannel == BACKCHANNEL_ONVIF)
8013 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
8014 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
8017 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
8020 gst_rtspsrc_send (src, info, &request, &response, NULL, NULL)) < 0)
8023 /* FIXME, parse result? */
8024 gst_rtsp_message_unset (&request);
8025 gst_rtsp_message_unset (&response);
8028 /* early exit when we did aggregate control */
8034 /* close connections */
8035 GST_DEBUG_OBJECT (src, "closing connection...");
8036 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
8037 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8038 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8039 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
8043 gst_rtspsrc_cleanup (src);
8045 src->state = GST_RTSP_STATE_INVALID;
8048 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
8053 create_request_failed:
8055 gchar *str = gst_rtsp_strresult (res);
8057 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
8058 ("Could not create request. (%s)", str));
8064 gchar *str = gst_rtsp_strresult (res);
8066 gst_rtsp_message_unset (&request);
8067 if (res != GST_RTSP_EINTR) {
8068 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
8069 ("Could not send message. (%s)", str));
8071 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
8078 GST_DEBUG_OBJECT (src,
8079 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
8084 /* RTP-Info is of the format:
8086 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
8088 * rtptime corresponds to the timestamp for the NPT time given in the header
8089 * seqbase corresponds to the next sequence number we received. This number
8090 * indicates the first seqnum after the seek and should be used to discard
8091 * packets that are from before the seek.
8094 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
8099 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
8101 infos = g_strsplit (rtpinfo, ",", 0);
8102 for (i = 0; infos[i]; i++) {
8104 GstRTSPStream *stream;
8108 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
8110 /* init values, types of seqbase and timebase are bigger than needed so we
8111 * can store -1 as uninitialized values */
8116 /* parse url, find stream for url.
8117 * parse seq and rtptime. The seq number should be configured in the rtp
8118 * depayloader or session manager to detect gaps. Same for the rtptime, it
8119 * should be used to create an initial time newsegment. */
8120 fields = g_strsplit (infos[i], ";", 0);
8121 for (j = 0; fields[j]; j++) {
8122 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
8123 /* remove leading whitespace */
8124 fields[j] = g_strchug (fields[j]);
8125 if (g_str_has_prefix (fields[j], "url=")) {
8126 /* get the url and the stream */
8128 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
8129 } else if (g_str_has_prefix (fields[j], "seq=")) {
8130 seqbase = atoi (fields[j] + 4);
8131 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
8132 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
8135 g_strfreev (fields);
8136 /* now we need to store the values for the caps of the stream */
8137 if (stream != NULL) {
8138 GST_DEBUG_OBJECT (src,
8139 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
8140 stream, seqbase, timebase);
8142 /* we have a stream, configure detected params */
8143 stream->seqbase = seqbase;
8144 stream->timebase = timebase;
8153 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
8158 interval = strtoul (rtcp, NULL, 10);
8159 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
8164 interval *= GST_MSECOND;
8166 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8167 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8169 /* already (optionally) retrieved this when configuring manager */
8170 if (stream->session) {
8171 GObject *rtpsession = stream->session;
8173 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
8175 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
8179 /* now it happens that (Xenon) server sending this may also provide bogus
8180 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
8181 * and just use RTP-Info to sync */
8183 GObjectClass *klass;
8185 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
8186 if (g_object_class_find_property (klass, "rtcp-sync")) {
8187 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
8188 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
8194 gst_rtspsrc_get_float (const gchar * dstr)
8196 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
8198 /* canonicalise floating point string so we can handle float strings
8199 * in the form "24.930" or "24,930" irrespective of the current locale */
8200 g_strlcpy (s, dstr, sizeof (s));
8201 g_strdelimit (s, ",", '.');
8202 return g_ascii_strtod (s, NULL);
8206 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
8208 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
8210 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
8211 g_strlcpy (val_str, "now", sizeof (val_str));
8213 if (segment->position == 0) {
8214 g_strlcpy (val_str, "0", sizeof (val_str));
8216 g_ascii_dtostr (val_str, sizeof (val_str),
8217 ((gdouble) segment->position) / GST_SECOND);
8220 return g_strdup_printf ("npt=%s-", val_str);
8224 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
8228 stream->timebase = -1;
8229 stream->seqbase = -1;
8231 len = stream->ptmap->len;
8232 for (i = 0; i < len; i++) {
8233 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
8236 if (item->caps == NULL)
8239 item->caps = gst_caps_make_writable (item->caps);
8240 s = gst_caps_get_structure (item->caps, 0);
8241 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
8242 if (item->pt == stream->default_pt && stream->udpsrc[0])
8243 g_object_set (stream->udpsrc[0], "caps", item->caps, NULL);
8245 stream->need_caps = TRUE;
8248 static GstRTSPResult
8249 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
8251 GstRTSPResult res = GST_RTSP_OK;
8253 if (src->state < GST_RTSP_STATE_READY) {
8254 res = GST_RTSP_ERROR;
8255 if (src->open_error) {
8256 GST_DEBUG_OBJECT (src, "the stream was in error");
8260 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
8262 if ((res = gst_rtspsrc_open (src, async)) < 0) {
8263 GST_DEBUG_OBJECT (src, "failed to open stream");
8272 static GstRTSPResult
8273 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async,
8274 const gchar * seek_style)
8276 GstRTSPMessage request = { 0 };
8277 GstRTSPMessage response = { 0 };
8278 GstRTSPResult res = GST_RTSP_OK;
8282 const gchar *control;
8284 GST_DEBUG_OBJECT (src, "PLAY...");
8287 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
8290 if (!(src->methods & GST_RTSP_PLAY))
8293 if (src->state == GST_RTSP_STATE_PLAYING)
8296 if (!src->conninfo.connection || !src->conninfo.connected)
8299 /* send some dummy packets before we activate the receive in the
8301 gst_rtspsrc_send_dummy_packets (src);
8303 /* require new SR packets */
8305 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
8307 /* construct a control url */
8308 control = get_aggregate_control (src);
8310 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8311 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8312 const gchar *setup_url;
8313 GstRTSPConnInfo *conninfo;
8315 /* try aggregate control first but do non-aggregate control otherwise */
8317 setup_url = control;
8318 else if ((setup_url = stream->conninfo.location) == NULL)
8321 if (src->conninfo.connection) {
8322 conninfo = &src->conninfo;
8323 } else if (stream->conninfo.connection) {
8324 conninfo = &stream->conninfo;
8330 res = gst_rtspsrc_init_request (src, &request, GST_RTSP_PLAY, setup_url);
8332 goto create_request_failed;
8334 if (src->need_range && src->seekable >= 0.0) {
8335 hval = gen_range_header (src, segment);
8337 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
8339 /* store the newsegment event so it can be sent from the streaming thread. */
8340 src->need_segment = TRUE;
8343 if (segment->rate != 1.0) {
8344 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
8346 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
8348 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
8350 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
8354 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SEEK_STYLE,
8357 /* when we have an ONVIF audio backchannel, the PLAY request must have the
8358 * Require: header when doing either aggregate or non-aggregate control */
8359 if (src->backchannel == BACKCHANNEL_ONVIF &&
8360 (control || stream->is_backchannel))
8361 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
8362 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
8365 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
8368 gst_rtspsrc_send (src, conninfo, &request, &response, NULL, NULL))
8372 if (src->need_redirect) {
8373 GST_DEBUG_OBJECT (src,
8374 "redirect: tearing down and restarting with new url");
8375 /* teardown and restart with new url */
8376 gst_rtspsrc_close (src, TRUE, FALSE);
8377 /* reset protocols to force re-negotiation with redirected url */
8378 src->cur_protocols = src->protocols;
8379 gst_rtsp_message_unset (&request);
8380 gst_rtsp_message_unset (&response);
8384 /* seek may have silently failed as it is not supported */
8385 if (!(src->methods & GST_RTSP_PLAY)) {
8386 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
8388 if (src->version >= GST_RTSP_VERSION_2_0 && src->seekable >= 0.0) {
8389 GST_WARNING_OBJECT (src, "Server declared stream as seekable but"
8390 " playing with range failed... Ignoring information.");
8392 /* obviously it is supported as we made it here */
8393 src->methods |= GST_RTSP_PLAY;
8394 src->seekable = -1.0;
8395 /* but there is nothing to parse in the response,
8396 * so convey we have no idea and not to expect anything particular */
8397 clear_rtp_base (src, stream);
8401 /* need to do for all streams */
8402 for (run = src->streams; run; run = g_list_next (run))
8403 clear_rtp_base (src, (GstRTSPStream *) run->data);
8405 /* NOTE the above also disables npt based eos detection */
8406 /* and below forces position to 0,
8407 * which is visible feedback we lost the plot */
8408 segment->start = segment->position = src->last_pos;
8411 gst_rtsp_message_unset (&request);
8413 /* parse RTP npt field. This is the current position in the stream (Normal
8414 * Play Time) and should be put in the NEWSEGMENT position field. */
8415 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
8417 gst_rtspsrc_parse_range (src, hval, segment);
8419 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
8420 segment->rate = 1.0;
8422 /* parse Speed header. This is the intended playback rate of the stream
8423 * and should be put in the NEWSEGMENT rate field. */
8424 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
8425 0) == GST_RTSP_OK) {
8426 segment->rate = gst_rtspsrc_get_float (hval);
8427 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
8428 &hval, 0) == GST_RTSP_OK) {
8429 segment->rate = gst_rtspsrc_get_float (hval);
8432 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
8433 * for the RTP packets. If this is not present, we assume all starts from 0...
8434 * This is info for the RTP session manager that we pass to it in caps. */
8436 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
8437 &hval, hval_idx++) == GST_RTSP_OK)
8438 gst_rtspsrc_parse_rtpinfo (src, hval);
8440 /* some servers indicate RTCP parameters in PLAY response,
8441 * rather than properly in SDP */
8442 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
8443 &hval, 0) == GST_RTSP_OK)
8444 gst_rtspsrc_handle_rtcp_interval (src, hval);
8446 gst_rtsp_message_unset (&response);
8448 /* early exit when we did aggregate control */
8452 /* configure the caps of the streams after we parsed all headers. Only reset
8453 * the manager object when we set a new Range header (we did a seek) */
8454 gst_rtspsrc_configure_caps (src, segment, src->need_range);
8456 /* set to PLAYING after we have configured the caps, otherwise we
8457 * might end up calling request_key (with SRTP) while caps are still
8458 * being configured. */
8459 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
8461 /* set again when needed */
8462 src->need_range = FALSE;
8464 src->running = TRUE;
8465 src->base_time = -1;
8466 src->state = GST_RTSP_STATE_PLAYING;
8469 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
8470 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8471 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8472 stream->discont = TRUE;
8477 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
8484 GST_WARNING_OBJECT (src, "failed to open stream");
8489 GST_WARNING_OBJECT (src, "PLAY is not supported");
8494 GST_WARNING_OBJECT (src, "we were already PLAYING");
8497 create_request_failed:
8499 gchar *str = gst_rtsp_strresult (res);
8501 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
8502 ("Could not create request. (%s)", str));
8508 gchar *str = gst_rtsp_strresult (res);
8510 gst_rtsp_message_unset (&request);
8511 if (res != GST_RTSP_EINTR) {
8512 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
8513 ("Could not send message. (%s)", str));
8515 GST_WARNING_OBJECT (src, "PLAY interrupted");
8522 static GstRTSPResult
8523 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
8525 GstRTSPResult res = GST_RTSP_OK;
8526 GstRTSPMessage request = { 0 };
8527 GstRTSPMessage response = { 0 };
8529 const gchar *control;
8531 GST_DEBUG_OBJECT (src, "PAUSE...");
8533 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
8536 if (!(src->methods & GST_RTSP_PAUSE))
8539 if (src->state == GST_RTSP_STATE_READY)
8542 if (!src->conninfo.connection || !src->conninfo.connected)
8545 /* construct a control url */
8546 control = get_aggregate_control (src);
8548 /* loop over the streams. We might exit the loop early when we could do an
8549 * aggregate control */
8550 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8551 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8552 GstRTSPConnInfo *conninfo;
8553 const gchar *setup_url;
8555 /* try aggregate control first but do non-aggregate control otherwise */
8557 setup_url = control;
8558 else if ((setup_url = stream->conninfo.location) == NULL)
8561 if (src->conninfo.connection) {
8562 conninfo = &src->conninfo;
8563 } else if (stream->conninfo.connection) {
8564 conninfo = &stream->conninfo;
8570 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
8571 ("Sending PAUSE request"));
8574 gst_rtspsrc_init_request (src, &request, GST_RTSP_PAUSE,
8576 goto create_request_failed;
8578 /* when we have an ONVIF audio backchannel, the PAUSE request must have the
8579 * Require: header when doing either aggregate or non-aggregate control */
8580 if (src->backchannel == BACKCHANNEL_ONVIF &&
8581 (control || stream->is_backchannel))
8582 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
8583 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
8586 gst_rtspsrc_send (src, conninfo, &request, &response, NULL,
8590 gst_rtsp_message_unset (&request);
8591 gst_rtsp_message_unset (&response);
8593 /* exit early when we did agregate control */
8598 /* change element states now */
8599 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
8602 src->state = GST_RTSP_STATE_READY;
8606 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
8613 GST_DEBUG_OBJECT (src, "failed to open stream");
8618 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
8623 GST_DEBUG_OBJECT (src, "we were already PAUSED");
8626 create_request_failed:
8628 gchar *str = gst_rtsp_strresult (res);
8630 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
8631 ("Could not create request. (%s)", str));
8637 gchar *str = gst_rtsp_strresult (res);
8639 gst_rtsp_message_unset (&request);
8640 if (res != GST_RTSP_EINTR) {
8641 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
8642 ("Could not send message. (%s)", str));
8644 GST_WARNING_OBJECT (src, "PAUSE interrupted");
8652 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
8654 GstRTSPSrc *rtspsrc;
8656 rtspsrc = GST_RTSPSRC (bin);
8658 switch (GST_MESSAGE_TYPE (message)) {
8659 case GST_MESSAGE_EOS:
8660 gst_message_unref (message);
8662 case GST_MESSAGE_ELEMENT:
8664 const GstStructure *s = gst_message_get_structure (message);
8666 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
8667 gboolean ignore_timeout;
8669 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
8671 GST_OBJECT_LOCK (rtspsrc);
8672 ignore_timeout = rtspsrc->ignore_timeout;
8673 rtspsrc->ignore_timeout = TRUE;
8674 GST_OBJECT_UNLOCK (rtspsrc);
8676 /* we only act on the first udp timeout message, others are irrelevant
8677 * and can be ignored. */
8678 if (!ignore_timeout)
8679 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
8681 gst_message_unref (message);
8684 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8687 case GST_MESSAGE_ERROR:
8690 GstRTSPStream *stream;
8693 udpsrc = GST_MESSAGE_SRC (message);
8695 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
8696 GST_ELEMENT_NAME (udpsrc));
8698 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
8702 /* we ignore the RTCP udpsrc */
8703 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
8706 /* if we get error messages from the udp sources, that's not a problem as
8707 * long as not all of them error out. We also don't really know what the
8708 * problem is, the message does not give enough detail... */
8709 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
8710 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
8711 if (ret != GST_FLOW_OK)
8715 gst_message_unref (message);
8719 /* fatal but not our message, forward */
8720 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8725 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8731 /* the thread where everything happens */
8733 gst_rtspsrc_thread (GstRTSPSrc * src)
8736 ParameterRequest *req = NULL;
8738 GST_OBJECT_LOCK (src);
8739 cmd = src->pending_cmd;
8740 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
8741 || cmd == CMD_LOOP || cmd == CMD_OPEN || cmd == CMD_GET_PARAMETER
8742 || cmd == CMD_SET_PARAMETER) {
8743 if (g_queue_is_empty (&src->set_get_param_q)) {
8744 src->pending_cmd = CMD_LOOP;
8746 ParameterRequest *next_req;
8747 req = g_queue_pop_head (&src->set_get_param_q);
8748 next_req = g_queue_peek_head (&src->set_get_param_q);
8749 src->pending_cmd = next_req ? next_req->cmd : CMD_LOOP;
8752 src->pending_cmd = CMD_WAIT;
8753 GST_DEBUG_OBJECT (src, "got command %s", cmd_to_string (cmd));
8755 /* we got the message command, so ensure communication is possible again */
8756 gst_rtspsrc_connection_flush (src, FALSE);
8758 src->busy_cmd = cmd;
8759 GST_OBJECT_UNLOCK (src);
8763 gst_rtspsrc_open (src, TRUE);
8766 gst_rtspsrc_play (src, &src->segment, TRUE, NULL);
8769 gst_rtspsrc_pause (src, TRUE);
8772 gst_rtspsrc_close (src, TRUE, FALSE);
8774 case CMD_GET_PARAMETER:
8775 gst_rtspsrc_get_parameter (src, req);
8777 case CMD_SET_PARAMETER:
8778 gst_rtspsrc_set_parameter (src, req);
8781 gst_rtspsrc_loop (src);
8784 gst_rtspsrc_reconnect (src, FALSE);
8790 GST_OBJECT_LOCK (src);
8791 /* No more cmds, wake any waiters */
8792 g_cond_broadcast (&src->cmd_cond);
8793 /* and go back to sleep */
8794 if (src->pending_cmd == CMD_WAIT) {
8796 gst_task_pause (src->task);
8799 src->busy_cmd = CMD_WAIT;
8800 GST_OBJECT_UNLOCK (src);
8804 gst_rtspsrc_start (GstRTSPSrc * src)
8806 GST_DEBUG_OBJECT (src, "starting");
8808 GST_OBJECT_LOCK (src);
8810 src->pending_cmd = CMD_WAIT;
8812 if (src->task == NULL) {
8813 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
8814 if (src->task == NULL)
8817 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
8819 GST_OBJECT_UNLOCK (src);
8826 GST_OBJECT_UNLOCK (src);
8827 GST_ERROR_OBJECT (src, "failed to create task");
8833 gst_rtspsrc_stop (GstRTSPSrc * src)
8837 GST_DEBUG_OBJECT (src, "stopping");
8839 /* also cancels pending task */
8840 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
8842 GST_OBJECT_LOCK (src);
8843 if ((task = src->task)) {
8845 GST_OBJECT_UNLOCK (src);
8847 gst_task_stop (task);
8849 /* make sure it is not running */
8850 GST_RTSP_STREAM_LOCK (src);
8851 GST_RTSP_STREAM_UNLOCK (src);
8853 /* now wait for the task to finish */
8854 gst_task_join (task);
8856 /* and free the task */
8857 gst_object_unref (GST_OBJECT (task));
8859 GST_OBJECT_LOCK (src);
8861 GST_OBJECT_UNLOCK (src);
8863 /* ensure synchronously all is closed and clean */
8864 gst_rtspsrc_close (src, FALSE, TRUE);
8869 static GstStateChangeReturn
8870 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
8872 GstRTSPSrc *rtspsrc;
8873 GstStateChangeReturn ret;
8875 rtspsrc = GST_RTSPSRC (element);
8877 switch (transition) {
8878 case GST_STATE_CHANGE_NULL_TO_READY:
8879 if (!gst_rtspsrc_start (rtspsrc))
8882 case GST_STATE_CHANGE_READY_TO_PAUSED:
8883 /* init some state */
8884 rtspsrc->cur_protocols = rtspsrc->protocols;
8885 /* first attempt, don't ignore timeouts */
8886 rtspsrc->ignore_timeout = FALSE;
8887 rtspsrc->open_error = FALSE;
8888 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
8890 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
8891 set_manager_buffer_mode (rtspsrc);
8893 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
8894 /* unblock the tcp tasks and make the loop waiting */
8895 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
8896 /* make sure it is waiting before we send PAUSE or PLAY below */
8897 GST_RTSP_STREAM_LOCK (rtspsrc);
8898 GST_RTSP_STREAM_UNLOCK (rtspsrc);
8901 case GST_STATE_CHANGE_PAUSED_TO_READY:
8907 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
8908 if (ret == GST_STATE_CHANGE_FAILURE)
8911 switch (transition) {
8912 case GST_STATE_CHANGE_NULL_TO_READY:
8913 ret = GST_STATE_CHANGE_SUCCESS;
8915 case GST_STATE_CHANGE_READY_TO_PAUSED:
8916 ret = GST_STATE_CHANGE_NO_PREROLL;
8918 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
8919 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
8920 ret = GST_STATE_CHANGE_SUCCESS;
8922 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
8923 /* send pause request and keep the idle task around */
8924 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
8925 ret = GST_STATE_CHANGE_NO_PREROLL;
8927 case GST_STATE_CHANGE_PAUSED_TO_READY:
8928 gst_rtspsrc_loop_send_cmd_and_wait (rtspsrc, CMD_CLOSE, CMD_ALL,
8929 rtspsrc->teardown_timeout);
8930 ret = GST_STATE_CHANGE_SUCCESS;
8932 case GST_STATE_CHANGE_READY_TO_NULL:
8933 gst_rtspsrc_stop (rtspsrc);
8934 ret = GST_STATE_CHANGE_SUCCESS;
8937 /* Otherwise it's success, we don't want to return spurious
8938 * NO_PREROLL or ASYNC from internal elements as we care for
8939 * state changes ourselves here
8941 * This is to catch PAUSED->PAUSED and PLAYING->PLAYING transitions.
8943 if (GST_STATE_TRANSITION_NEXT (transition) == GST_STATE_PAUSED)
8944 ret = GST_STATE_CHANGE_NO_PREROLL;
8946 ret = GST_STATE_CHANGE_SUCCESS;
8955 GST_DEBUG_OBJECT (rtspsrc, "start failed");
8956 return GST_STATE_CHANGE_FAILURE;
8961 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
8964 GstRTSPSrc *rtspsrc;
8966 rtspsrc = GST_RTSPSRC (element);
8968 if (GST_EVENT_IS_DOWNSTREAM (event)) {
8969 res = gst_rtspsrc_push_event (rtspsrc, event);
8971 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
8978 /*** GSTURIHANDLER INTERFACE *************************************************/
8981 gst_rtspsrc_uri_get_type (GType type)
8986 static const gchar *const *
8987 gst_rtspsrc_uri_get_protocols (GType type)
8989 static const gchar *protocols[] =
8990 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
8991 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
8998 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
9000 GstRTSPSrc *src = GST_RTSPSRC (handler);
9002 /* FIXME: make thread-safe */
9003 return g_strdup (src->conninfo.location);
9007 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
9013 GstRTSPUrl *newurl = NULL;
9014 GstSDPMessage *sdp = NULL;
9016 src = GST_RTSPSRC (handler);
9018 /* same URI, we're fine */
9019 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
9022 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
9023 sres = gst_sdp_message_new (&sdp);
9027 GST_DEBUG_OBJECT (src, "parsing SDP message");
9028 sres = gst_sdp_message_parse_uri (uri, sdp);
9033 GST_DEBUG_OBJECT (src, "parsing URI");
9034 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
9038 /* if worked, free previous and store new url object along with the original
9040 GST_DEBUG_OBJECT (src, "configuring URI");
9041 g_free (src->conninfo.location);
9042 src->conninfo.location = g_strdup (uri);
9043 gst_rtsp_url_free (src->conninfo.url);
9044 src->conninfo.url = newurl;
9045 g_free (src->conninfo.url_str);
9047 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
9049 src->conninfo.url_str = NULL;
9052 gst_sdp_message_free (src->sdp);
9054 src->from_sdp = sdp != NULL;
9056 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
9057 GST_DEBUG_OBJECT (src, "request uri is: %s",
9058 GST_STR_NULL (src->conninfo.url_str));
9065 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
9070 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", sres);
9071 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
9072 "Could not create SDP");
9077 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", sres,
9078 GST_STR_NULL (uri));
9079 gst_sdp_message_free (sdp);
9080 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
9086 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
9087 GST_STR_NULL (uri), res);
9088 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
9089 "Invalid RTSP URI");
9095 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
9097 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
9099 iface->get_type = gst_rtspsrc_uri_get_type;
9100 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
9101 iface->get_uri = gst_rtspsrc_uri_get_uri;
9102 iface->set_uri = gst_rtspsrc_uri_set_uri;
9106 /* send GET_PARAMETER */
9107 static GstRTSPResult
9108 gst_rtspsrc_get_parameter (GstRTSPSrc * src, ParameterRequest * req)
9110 GstRTSPMessage request = { 0 };
9111 GstRTSPMessage response = { 0 };
9113 GstRTSPStatusCode code = GST_RTSP_STS_OK;
9114 const gchar *control;
9115 gchar *recv_body = NULL;
9116 guint recv_body_len;
9118 GST_DEBUG_OBJECT (src, "creating server get_parameter");
9120 if ((res = gst_rtspsrc_ensure_open (src, FALSE)) < 0)
9123 control = get_aggregate_control (src);
9124 if (control == NULL)
9127 if (!(src->methods & GST_RTSP_GET_PARAMETER))
9130 gst_rtspsrc_connection_flush (src, FALSE);
9132 res = gst_rtsp_message_init_request (&request, GST_RTSP_GET_PARAMETER,
9135 goto create_request_failed;
9137 res = gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CONTENT_TYPE,
9138 req->content_type == NULL ? "text/parameters" : req->content_type);
9140 goto add_content_hdr_failed;
9142 if (req->body && req->body->len) {
9144 gst_rtsp_message_set_body (&request, (guint8 *) req->body->str,
9147 goto set_body_failed;
9150 if ((res = gst_rtspsrc_send (src, &src->conninfo,
9151 &request, &response, &code, NULL)) < 0)
9154 res = gst_rtsp_message_get_body (&response, (guint8 **) & recv_body,
9157 goto get_body_failed;
9161 gst_promise_reply (req->promise,
9162 gst_structure_new ("get-parameter-reply",
9163 "rtsp-result", G_TYPE_INT, res,
9164 "rtsp-code", G_TYPE_INT, code,
9165 "rtsp-reason", G_TYPE_STRING, gst_rtsp_status_as_text (code),
9166 "body", G_TYPE_STRING, GST_STR_NULL (recv_body), NULL));
9167 free_param_data (req);
9170 gst_rtsp_message_unset (&request);
9171 gst_rtsp_message_unset (&response);
9179 GST_DEBUG_OBJECT (src, "failed to open stream");
9184 GST_DEBUG_OBJECT (src, "no control url to send GET_PARAMETER");
9185 res = GST_RTSP_ERROR;
9190 GST_DEBUG_OBJECT (src, "GET_PARAMETER is not supported");
9191 res = GST_RTSP_ERROR;
9194 create_request_failed:
9196 GST_DEBUG_OBJECT (src, "could not create GET_PARAMETER request");
9199 add_content_hdr_failed:
9201 GST_DEBUG_OBJECT (src, "could not add content header");
9206 GST_DEBUG_OBJECT (src, "could not set body");
9211 gchar *str = gst_rtsp_strresult (res);
9213 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
9214 ("Could not send get-parameter. (%s)", str));
9220 GST_DEBUG_OBJECT (src, "could not get body");
9225 /* send SET_PARAMETER */
9226 static GstRTSPResult
9227 gst_rtspsrc_set_parameter (GstRTSPSrc * src, ParameterRequest * req)
9229 GstRTSPMessage request = { 0 };
9230 GstRTSPMessage response = { 0 };
9231 GstRTSPResult res = GST_RTSP_OK;
9232 GstRTSPStatusCode code = GST_RTSP_STS_OK;
9233 const gchar *control;
9235 GST_DEBUG_OBJECT (src, "creating server set_parameter");
9237 if ((res = gst_rtspsrc_ensure_open (src, FALSE)) < 0)
9240 control = get_aggregate_control (src);
9241 if (control == NULL)
9244 if (!(src->methods & GST_RTSP_SET_PARAMETER))
9247 gst_rtspsrc_connection_flush (src, FALSE);
9250 gst_rtsp_message_init_request (&request, GST_RTSP_SET_PARAMETER, control);
9254 res = gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CONTENT_TYPE,
9255 req->content_type == NULL ? "text/parameters" : req->content_type);
9257 goto add_content_hdr_failed;
9259 if (req->body && req->body->len) {
9261 gst_rtsp_message_set_body (&request, (guint8 *) req->body->str,
9265 goto set_body_failed;
9268 if ((res = gst_rtspsrc_send (src, &src->conninfo,
9269 &request, &response, &code, NULL)) < 0)
9274 gst_promise_reply (req->promise, gst_structure_new ("set-parameter-reply",
9275 "rtsp-result", G_TYPE_INT, res,
9276 "rtsp-code", G_TYPE_INT, code,
9277 "rtsp-reason", G_TYPE_STRING, gst_rtsp_status_as_text (code),
9279 free_param_data (req);
9281 gst_rtsp_message_unset (&request);
9282 gst_rtsp_message_unset (&response);
9290 GST_DEBUG_OBJECT (src, "failed to open stream");
9295 GST_DEBUG_OBJECT (src, "no control url to send SET_PARAMETER");
9296 res = GST_RTSP_ERROR;
9301 GST_DEBUG_OBJECT (src, "SET_PARAMETER is not supported");
9302 res = GST_RTSP_ERROR;
9305 add_content_hdr_failed:
9307 GST_DEBUG_OBJECT (src, "could not add content header");
9312 GST_DEBUG_OBJECT (src, "could not set body");
9317 gchar *str = gst_rtsp_strresult (res);
9319 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
9320 ("Could not send set-parameter. (%s)", str));
9326 typedef struct _RTSPKeyValue
9328 GstRTSPHeaderField field;
9330 gchar *custom_key; /* custom header string (field is INVALID then) */
9334 key_value_foreach (GArray * array, GFunc func, gpointer user_data)
9338 g_return_if_fail (array != NULL);
9340 for (i = 0; i < array->len; i++) {
9341 (*func) (&g_array_index (array, RTSPKeyValue, i), user_data);
9346 dump_key_value (gpointer data, gpointer user_data G_GNUC_UNUSED)
9348 RTSPKeyValue *key_value = (RTSPKeyValue *) data;
9349 GstRTSPSrc *src = GST_RTSPSRC (user_data);
9350 const gchar *key_string;
9352 if (key_value->custom_key != NULL)
9353 key_string = key_value->custom_key;
9355 key_string = gst_rtsp_header_as_text (key_value->field);
9357 GST_LOG_OBJECT (src, " key: '%s', value: '%s'", key_string,
9362 gst_rtspsrc_print_rtsp_message (GstRTSPSrc * src, const GstRTSPMessage * msg)
9366 GString *body_string = NULL;
9368 g_return_if_fail (src != NULL);
9369 g_return_if_fail (msg != NULL);
9371 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) < GST_LEVEL_LOG)
9374 GST_LOG_OBJECT (src, "--------------------------------------------");
9375 switch (msg->type) {
9376 case GST_RTSP_MESSAGE_REQUEST:
9377 GST_LOG_OBJECT (src, "RTSP request message %p", msg);
9378 GST_LOG_OBJECT (src, " request line:");
9379 GST_LOG_OBJECT (src, " method: '%s'",
9380 gst_rtsp_method_as_text (msg->type_data.request.method));
9381 GST_LOG_OBJECT (src, " uri: '%s'", msg->type_data.request.uri);
9382 GST_LOG_OBJECT (src, " version: '%s'",
9383 gst_rtsp_version_as_text (msg->type_data.request.version));
9384 GST_LOG_OBJECT (src, " headers:");
9385 key_value_foreach (msg->hdr_fields, dump_key_value, src);
9386 GST_LOG_OBJECT (src, " body:");
9387 gst_rtsp_message_get_body (msg, &data, &size);
9389 body_string = g_string_new_len ((const gchar *) data, size);
9390 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
9391 g_string_free (body_string, TRUE);
9395 case GST_RTSP_MESSAGE_RESPONSE:
9396 GST_LOG_OBJECT (src, "RTSP response message %p", msg);
9397 GST_LOG_OBJECT (src, " status line:");
9398 GST_LOG_OBJECT (src, " code: '%d'", msg->type_data.response.code);
9399 GST_LOG_OBJECT (src, " reason: '%s'", msg->type_data.response.reason);
9400 GST_LOG_OBJECT (src, " version: '%s",
9401 gst_rtsp_version_as_text (msg->type_data.response.version));
9402 GST_LOG_OBJECT (src, " headers:");
9403 key_value_foreach (msg->hdr_fields, dump_key_value, src);
9404 gst_rtsp_message_get_body (msg, &data, &size);
9405 GST_LOG_OBJECT (src, " body: length %d", size);
9407 body_string = g_string_new_len ((const gchar *) data, size);
9408 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
9409 g_string_free (body_string, TRUE);
9413 case GST_RTSP_MESSAGE_HTTP_REQUEST:
9414 GST_LOG_OBJECT (src, "HTTP request message %p", msg);
9415 GST_LOG_OBJECT (src, " request line:");
9416 GST_LOG_OBJECT (src, " method: '%s'",
9417 gst_rtsp_method_as_text (msg->type_data.request.method));
9418 GST_LOG_OBJECT (src, " uri: '%s'", msg->type_data.request.uri);
9419 GST_LOG_OBJECT (src, " version: '%s'",
9420 gst_rtsp_version_as_text (msg->type_data.request.version));
9421 GST_LOG_OBJECT (src, " headers:");
9422 key_value_foreach (msg->hdr_fields, dump_key_value, src);
9423 GST_LOG_OBJECT (src, " body:");
9424 gst_rtsp_message_get_body (msg, &data, &size);
9426 body_string = g_string_new_len ((const gchar *) data, size);
9427 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
9428 g_string_free (body_string, TRUE);
9432 case GST_RTSP_MESSAGE_HTTP_RESPONSE:
9433 GST_LOG_OBJECT (src, "HTTP response message %p", msg);
9434 GST_LOG_OBJECT (src, " status line:");
9435 GST_LOG_OBJECT (src, " code: '%d'", msg->type_data.response.code);
9436 GST_LOG_OBJECT (src, " reason: '%s'", msg->type_data.response.reason);
9437 GST_LOG_OBJECT (src, " version: '%s'",
9438 gst_rtsp_version_as_text (msg->type_data.response.version));
9439 GST_LOG_OBJECT (src, " headers:");
9440 key_value_foreach (msg->hdr_fields, dump_key_value, src);
9441 gst_rtsp_message_get_body (msg, &data, &size);
9442 GST_LOG_OBJECT (src, " body: length %d", size);
9444 body_string = g_string_new_len ((const gchar *) data, size);
9445 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
9446 g_string_free (body_string, TRUE);
9450 case GST_RTSP_MESSAGE_DATA:
9451 GST_LOG_OBJECT (src, "RTSP data message %p", msg);
9452 GST_LOG_OBJECT (src, " channel: '%d'", msg->type_data.data.channel);
9453 GST_LOG_OBJECT (src, " size: '%d'", msg->body_size);
9454 gst_rtsp_message_get_body (msg, &data, &size);
9456 body_string = g_string_new_len ((const gchar *) data, size);
9457 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
9458 g_string_free (body_string, TRUE);
9463 GST_LOG_OBJECT (src, "unsupported message type %d", msg->type);
9466 GST_LOG_OBJECT (src, "--------------------------------------------");
9470 gst_rtspsrc_print_sdp_media (GstRTSPSrc * src, GstSDPMedia * media)
9472 GST_LOG_OBJECT (src, " media: '%s'", GST_STR_NULL (media->media));
9473 GST_LOG_OBJECT (src, " port: '%u'", media->port);
9474 GST_LOG_OBJECT (src, " num_ports: '%u'", media->num_ports);
9475 GST_LOG_OBJECT (src, " proto: '%s'", GST_STR_NULL (media->proto));
9476 if (media->fmts && media->fmts->len > 0) {
9479 GST_LOG_OBJECT (src, " formats:");
9480 for (i = 0; i < media->fmts->len; i++) {
9481 GST_LOG_OBJECT (src, " format '%s'", g_array_index (media->fmts,
9485 GST_LOG_OBJECT (src, " information: '%s'",
9486 GST_STR_NULL (media->information));
9487 if (media->connections && media->connections->len > 0) {
9490 GST_LOG_OBJECT (src, " connections:");
9491 for (i = 0; i < media->connections->len; i++) {
9492 GstSDPConnection *conn =
9493 &g_array_index (media->connections, GstSDPConnection, i);
9495 GST_LOG_OBJECT (src, " nettype: '%s'",
9496 GST_STR_NULL (conn->nettype));
9497 GST_LOG_OBJECT (src, " addrtype: '%s'",
9498 GST_STR_NULL (conn->addrtype));
9499 GST_LOG_OBJECT (src, " address: '%s'",
9500 GST_STR_NULL (conn->address));
9501 GST_LOG_OBJECT (src, " ttl: '%u'", conn->ttl);
9502 GST_LOG_OBJECT (src, " addr_number: '%u'", conn->addr_number);
9505 if (media->bandwidths && media->bandwidths->len > 0) {
9508 GST_LOG_OBJECT (src, " bandwidths:");
9509 for (i = 0; i < media->bandwidths->len; i++) {
9510 GstSDPBandwidth *bw =
9511 &g_array_index (media->bandwidths, GstSDPBandwidth, i);
9513 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (bw->bwtype));
9514 GST_LOG_OBJECT (src, " bandwidth: '%u'", bw->bandwidth);
9517 GST_LOG_OBJECT (src, " key:");
9518 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (media->key.type));
9519 GST_LOG_OBJECT (src, " data: '%s'", GST_STR_NULL (media->key.data));
9520 if (media->attributes && media->attributes->len > 0) {
9523 GST_LOG_OBJECT (src, " attributes:");
9524 for (i = 0; i < media->attributes->len; i++) {
9525 GstSDPAttribute *attr =
9526 &g_array_index (media->attributes, GstSDPAttribute, i);
9528 GST_LOG_OBJECT (src, " attribute '%s' : '%s'", attr->key, attr->value);
9534 gst_rtspsrc_print_sdp_message (GstRTSPSrc * src, const GstSDPMessage * msg)
9536 g_return_if_fail (src != NULL);
9537 g_return_if_fail (msg != NULL);
9539 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) < GST_LEVEL_LOG)
9542 GST_LOG_OBJECT (src, "--------------------------------------------");
9543 GST_LOG_OBJECT (src, "sdp packet %p:", msg);
9544 GST_LOG_OBJECT (src, " version: '%s'", GST_STR_NULL (msg->version));
9545 GST_LOG_OBJECT (src, " origin:");
9546 GST_LOG_OBJECT (src, " username: '%s'",
9547 GST_STR_NULL (msg->origin.username));
9548 GST_LOG_OBJECT (src, " sess_id: '%s'",
9549 GST_STR_NULL (msg->origin.sess_id));
9550 GST_LOG_OBJECT (src, " sess_version: '%s'",
9551 GST_STR_NULL (msg->origin.sess_version));
9552 GST_LOG_OBJECT (src, " nettype: '%s'",
9553 GST_STR_NULL (msg->origin.nettype));
9554 GST_LOG_OBJECT (src, " addrtype: '%s'",
9555 GST_STR_NULL (msg->origin.addrtype));
9556 GST_LOG_OBJECT (src, " addr: '%s'", GST_STR_NULL (msg->origin.addr));
9557 GST_LOG_OBJECT (src, " session_name: '%s'",
9558 GST_STR_NULL (msg->session_name));
9559 GST_LOG_OBJECT (src, " information: '%s'", GST_STR_NULL (msg->information));
9560 GST_LOG_OBJECT (src, " uri: '%s'", GST_STR_NULL (msg->uri));
9562 if (msg->emails && msg->emails->len > 0) {
9565 GST_LOG_OBJECT (src, " emails:");
9566 for (i = 0; i < msg->emails->len; i++) {
9567 GST_LOG_OBJECT (src, " email '%s'", g_array_index (msg->emails, gchar *,
9571 if (msg->phones && msg->phones->len > 0) {
9574 GST_LOG_OBJECT (src, " phones:");
9575 for (i = 0; i < msg->phones->len; i++) {
9576 GST_LOG_OBJECT (src, " phone '%s'", g_array_index (msg->phones, gchar *,
9580 GST_LOG_OBJECT (src, " connection:");
9581 GST_LOG_OBJECT (src, " nettype: '%s'",
9582 GST_STR_NULL (msg->connection.nettype));
9583 GST_LOG_OBJECT (src, " addrtype: '%s'",
9584 GST_STR_NULL (msg->connection.addrtype));
9585 GST_LOG_OBJECT (src, " address: '%s'",
9586 GST_STR_NULL (msg->connection.address));
9587 GST_LOG_OBJECT (src, " ttl: '%u'", msg->connection.ttl);
9588 GST_LOG_OBJECT (src, " addr_number: '%u'", msg->connection.addr_number);
9589 if (msg->bandwidths && msg->bandwidths->len > 0) {
9592 GST_LOG_OBJECT (src, " bandwidths:");
9593 for (i = 0; i < msg->bandwidths->len; i++) {
9594 GstSDPBandwidth *bw =
9595 &g_array_index (msg->bandwidths, GstSDPBandwidth, i);
9597 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (bw->bwtype));
9598 GST_LOG_OBJECT (src, " bandwidth: '%u'", bw->bandwidth);
9601 GST_LOG_OBJECT (src, " key:");
9602 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (msg->key.type));
9603 GST_LOG_OBJECT (src, " data: '%s'", GST_STR_NULL (msg->key.data));
9604 if (msg->attributes && msg->attributes->len > 0) {
9607 GST_LOG_OBJECT (src, " attributes:");
9608 for (i = 0; i < msg->attributes->len; i++) {
9609 GstSDPAttribute *attr =
9610 &g_array_index (msg->attributes, GstSDPAttribute, i);
9612 GST_LOG_OBJECT (src, " attribute '%s' : '%s'", attr->key, attr->value);
9615 if (msg->medias && msg->medias->len > 0) {
9618 GST_LOG_OBJECT (src, " medias:");
9619 for (i = 0; i < msg->medias->len; i++) {
9620 GST_LOG_OBJECT (src, " media %u:", i);
9621 gst_rtspsrc_print_sdp_media (src, &g_array_index (msg->medias,
9625 GST_LOG_OBJECT (src, "--------------------------------------------");