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 if (gst_object_has_as_parent (GST_OBJECT (stream->udpsrc[i]),
2286 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
2287 gst_object_unref (stream->udpsrc[i]);
2289 if (stream->channelpad[i])
2290 gst_object_unref (stream->channelpad[i]);
2292 if (stream->udpsink[i]) {
2293 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
2294 if (gst_object_has_as_parent (GST_OBJECT (stream->udpsink[i]),
2296 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
2297 gst_object_unref (stream->udpsink[i]);
2300 if (stream->rtpsrc) {
2301 gst_element_set_state (stream->rtpsrc, GST_STATE_NULL);
2302 gst_bin_remove (GST_BIN_CAST (src), stream->rtpsrc);
2303 gst_object_unref (stream->rtpsrc);
2305 if (stream->srcpad) {
2306 gst_pad_set_active (stream->srcpad, FALSE);
2308 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2310 if (stream->srtpenc)
2311 gst_object_unref (stream->srtpenc);
2312 if (stream->srtpdec)
2313 gst_object_unref (stream->srtpdec);
2314 if (stream->srtcpparams)
2315 gst_caps_unref (stream->srtcpparams);
2317 gst_mikey_message_unref (stream->mikey);
2318 if (stream->rtcppad)
2319 gst_object_unref (stream->rtcppad);
2320 if (stream->session)
2321 g_object_unref (stream->session);
2322 if (stream->rtx_pt_map)
2323 gst_structure_free (stream->rtx_pt_map);
2325 g_mutex_clear (&stream->conninfo.send_lock);
2326 g_mutex_clear (&stream->conninfo.recv_lock);
2332 gst_rtspsrc_cleanup (GstRTSPSrc * src)
2336 GST_DEBUG_OBJECT (src, "cleanup");
2338 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2339 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2341 gst_rtspsrc_stream_free (src, stream);
2343 g_list_free (src->streams);
2344 src->streams = NULL;
2346 if (src->manager_sig_id) {
2347 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
2348 src->manager_sig_id = 0;
2350 gst_element_set_state (src->manager, GST_STATE_NULL);
2351 gst_bin_remove (GST_BIN_CAST (src), src->manager);
2352 src->manager = NULL;
2355 gst_structure_free (src->props);
2358 g_free (src->content_base);
2359 src->content_base = NULL;
2361 g_free (src->control);
2362 src->control = NULL;
2365 gst_rtsp_range_free (src->range);
2368 /* don't clear the SDP when it was used in the url */
2369 if (src->sdp && !src->from_sdp) {
2370 gst_sdp_message_free (src->sdp);
2374 src->need_segment = FALSE;
2376 if (src->provided_clock) {
2377 gst_object_unref (src->provided_clock);
2378 src->provided_clock = NULL;
2381 /* free parameter requests queue */
2382 if (!g_queue_is_empty (&src->set_get_param_q))
2383 g_queue_free_full (&src->set_get_param_q, free_param_queue);
2388 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
2389 gint * rtpport, gint * rtcpport)
2392 GstStateChangeReturn ret;
2393 GstElement *udpsrc0, *udpsrc1;
2394 gint tmp_rtp, tmp_rtcp;
2398 src = stream->parent;
2404 /* Start at next port */
2405 tmp_rtp = src->next_port_num;
2407 if (stream->is_ipv6)
2408 host = "udp://[::0]";
2410 host = "udp://0.0.0.0";
2412 /* try to allocate 2 UDP ports, the RTP port should be an even
2413 * number and the RTCP port should be the next (uneven) port */
2416 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
2417 tmp_rtp >= src->client_port_range.max)
2420 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2421 if (udpsrc0 == NULL)
2422 goto no_udp_protocol;
2423 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
2425 if (src->udp_buffer_size != 0)
2426 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
2429 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
2430 if (ret == GST_STATE_CHANGE_FAILURE) {
2432 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
2435 if (++count > src->retry)
2438 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2439 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2440 gst_object_unref (udpsrc0);
2443 GST_DEBUG_OBJECT (src, "retry %d", count);
2446 goto no_udp_protocol;
2449 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
2450 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
2452 /* check if port is even */
2453 if ((tmp_rtp & 0x01) != 0) {
2454 /* port not even, close and allocate another */
2455 if (++count > src->retry)
2458 GST_DEBUG_OBJECT (src, "RTP port not even");
2460 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2461 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2462 gst_object_unref (udpsrc0);
2465 GST_DEBUG_OBJECT (src, "retry %d", count);
2470 /* allocate port+1 for RTCP now */
2471 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2472 if (udpsrc1 == NULL)
2473 goto no_udp_rtcp_protocol;
2476 tmp_rtcp = tmp_rtp + 1;
2477 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
2480 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
2482 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
2483 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
2484 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
2485 if (ret == GST_STATE_CHANGE_FAILURE) {
2486 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
2488 if (++count > src->retry)
2491 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2492 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2493 gst_object_unref (udpsrc0);
2496 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
2497 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2498 gst_object_unref (udpsrc1);
2502 GST_DEBUG_OBJECT (src, "retry %d", count);
2506 /* all fine, do port check */
2507 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
2508 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
2510 /* this should not happen... */
2511 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
2514 /* we keep these elements, we configure all in configure_transport when the
2515 * server told us to really use the UDP ports. */
2516 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
2517 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
2518 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
2519 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
2521 /* keep track of next available port number when we have a range
2523 if (src->next_port_num != 0)
2524 src->next_port_num = tmp_rtcp + 1;
2531 GST_DEBUG_OBJECT (src, "could not get UDP source");
2536 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
2540 no_udp_rtcp_protocol:
2542 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
2547 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
2548 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
2554 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2555 gst_object_unref (udpsrc0);
2558 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2559 gst_object_unref (udpsrc1);
2566 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
2571 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
2573 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2574 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2577 for (i = 0; i < 2; i++) {
2578 if (stream->udpsrc[i])
2579 gst_element_set_state (stream->udpsrc[i], state);
2585 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing,
2593 event = gst_event_new_flush_start ();
2594 gst_event_set_seqnum (event, seqnum);
2595 GST_DEBUG_OBJECT (src, "start flush");
2597 state = GST_STATE_PAUSED;
2599 event = gst_event_new_flush_stop (FALSE);
2600 gst_event_set_seqnum (event, seqnum);
2601 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2604 state = GST_STATE_PLAYING;
2606 state = GST_STATE_PAUSED;
2608 gst_rtspsrc_push_event (src, event);
2609 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2610 gst_rtspsrc_set_state (src, state);
2613 static GstRTSPResult
2614 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
2615 GstRTSPMessage * message, GTimeVal * timeout)
2619 if (conninfo->connection) {
2620 g_mutex_lock (&conninfo->send_lock);
2621 ret = gst_rtsp_connection_send (conninfo->connection, message, timeout);
2622 g_mutex_unlock (&conninfo->send_lock);
2624 ret = GST_RTSP_ERROR;
2630 static GstRTSPResult
2631 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
2632 GstRTSPMessage * message, GTimeVal * timeout)
2636 if (conninfo->connection) {
2637 g_mutex_lock (&conninfo->recv_lock);
2638 ret = gst_rtsp_connection_receive (conninfo->connection, message, timeout);
2639 g_mutex_unlock (&conninfo->recv_lock);
2641 ret = GST_RTSP_ERROR;
2648 gst_rtspsrc_get_position (GstRTSPSrc * src)
2653 query = gst_query_new_position (GST_FORMAT_TIME);
2654 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2655 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2656 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2660 if (stream->srcpad) {
2661 if (gst_pad_query (stream->srcpad, query)) {
2662 gst_query_parse_position (query, &fmt, &pos);
2663 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2664 GST_TIME_ARGS (pos));
2665 src->last_pos = pos;
2675 gst_query_unref (query);
2679 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2684 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2686 gboolean flush, skip;
2689 GstSegment seeksegment = { 0, };
2691 const gchar *seek_style = NULL;
2693 GST_DEBUG_OBJECT (src, "doing seek with event %" GST_PTR_FORMAT, event);
2695 gst_event_parse_seek (event, &rate, &format, &flags,
2696 &cur_type, &cur, &stop_type, &stop);
2698 /* no negative rates yet */
2702 /* we need TIME format */
2703 if (format != src->segment.format)
2706 /* Check if we are not at all seekable */
2707 if (src->seekable == -1.0)
2710 /* Additional seeking-to-beginning-only check */
2711 if (src->seekable == 0.0 && cur != 0)
2714 if (flags & GST_SEEK_FLAG_SEGMENT)
2715 goto invalid_segment_flag;
2717 /* get flush flag */
2718 flush = flags & GST_SEEK_FLAG_FLUSH;
2719 skip = flags & GST_SEEK_FLAG_SKIP;
2721 /* now we need to make sure the streaming thread is stopped. We do this by
2722 * either sending a FLUSH_START event downstream which will cause the
2723 * streaming thread to stop with a WRONG_STATE.
2724 * For a non-flushing seek we simply pause the task, which will happen as soon
2725 * as it completes one iteration (and thus might block when the sink is
2726 * blocking in preroll). */
2728 GST_DEBUG_OBJECT (src, "starting flush");
2729 gst_rtspsrc_flush (src, TRUE, FALSE, gst_event_get_seqnum (event));
2732 gst_task_pause (src->task);
2736 /* we should now be able to grab the streaming thread because we stopped it
2737 * with the above flush/pause code */
2738 GST_RTSP_STREAM_LOCK (src);
2740 GST_DEBUG_OBJECT (src, "stopped streaming");
2742 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2743 gst_rtspsrc_connection_flush (src, FALSE);
2745 /* copy segment, we need this because we still need the old
2746 * segment when we close the current segment. */
2747 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2749 /* configure the seek parameters in the seeksegment. We will then have the
2750 * right values in the segment to perform the seek */
2751 GST_DEBUG_OBJECT (src, "configuring seek");
2752 gst_segment_do_seek (&seeksegment, rate, format, flags,
2753 cur_type, cur, stop_type, stop, &update);
2755 /* figure out the last position we need to play. If it's configured (stop !=
2756 * -1), use that, else we play until the total duration of the file */
2757 if ((stop = seeksegment.stop) == -1)
2758 stop = seeksegment.duration;
2760 /* if we were playing, pause first */
2761 playing = (src->state == GST_RTSP_STATE_PLAYING);
2763 /* obtain current position in case seek fails */
2764 gst_rtspsrc_get_position (src);
2765 gst_rtspsrc_pause (src, FALSE);
2769 src->state = GST_RTSP_STATE_SEEKING;
2771 /* PLAY will add the range header now. */
2772 src->need_range = TRUE;
2774 /* prepare for streaming again */
2776 /* if we started flush, we stop now */
2777 GST_DEBUG_OBJECT (src, "stopping flush");
2778 gst_rtspsrc_flush (src, FALSE, playing, gst_event_get_seqnum (event));
2781 /* now we did the seek and can activate the new segment values */
2782 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2784 /* if we're doing a segment seek, post a SEGMENT_START message */
2785 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2786 gst_element_post_message (GST_ELEMENT_CAST (src),
2787 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2788 src->segment.format, src->segment.position));
2791 /* now create the newsegment */
2792 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2793 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2796 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2797 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2798 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2799 stream->discont = TRUE;
2802 /* and continue playing if needed */
2803 GST_OBJECT_LOCK (src);
2804 playing = (GST_STATE_PENDING (src) == GST_STATE_VOID_PENDING
2805 && GST_STATE (src) == GST_STATE_PLAYING)
2806 || (GST_STATE_PENDING (src) == GST_STATE_PLAYING);
2807 GST_OBJECT_UNLOCK (src);
2809 if (src->version >= GST_RTSP_VERSION_2_0) {
2810 if (flags & GST_SEEK_FLAG_ACCURATE)
2812 else if (flags & GST_SEEK_FLAG_KEY_UNIT)
2813 seek_style = "CoRAP";
2814 else if (flags & GST_SEEK_FLAG_KEY_UNIT
2815 && flags & GST_SEEK_FLAG_SNAP_BEFORE)
2816 seek_style = "First-Prior";
2817 else if (flags & GST_SEEK_FLAG_KEY_UNIT && flags & GST_SEEK_FLAG_SNAP_AFTER)
2818 seek_style = "Next";
2822 gst_rtspsrc_play (src, &seeksegment, FALSE, seek_style);
2824 GST_RTSP_STREAM_UNLOCK (src);
2831 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2836 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2841 GST_DEBUG_OBJECT (src, "stream is not seekable");
2844 invalid_segment_flag:
2846 GST_WARNING_OBJECT (src, "Segment seeks not supported");
2852 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2856 gboolean res = TRUE;
2859 src = GST_RTSPSRC_CAST (parent);
2861 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2862 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2864 switch (GST_EVENT_TYPE (event)) {
2865 case GST_EVENT_SEEK:
2866 res = gst_rtspsrc_perform_seek (src, event);
2870 case GST_EVENT_NAVIGATION:
2871 case GST_EVENT_LATENCY:
2879 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2880 res = gst_pad_send_event (target, event);
2881 gst_object_unref (target);
2883 gst_event_unref (event);
2886 gst_event_unref (event);
2893 gst_rtspsrc_handle_src_sink_event (GstPad * pad, GstObject * parent,
2896 GstRTSPStream *stream;
2898 stream = gst_pad_get_element_private (pad);
2900 switch (GST_EVENT_TYPE (event)) {
2901 case GST_EVENT_STREAM_START:{
2902 const gchar *upstream_id;
2905 gst_event_parse_stream_start (event, &upstream_id);
2906 stream_id = g_strdup_printf ("%s/%s", upstream_id, stream->stream_id);
2908 gst_event_unref (event);
2909 event = gst_event_new_stream_start (stream_id);
2917 return gst_pad_push_event (stream->srcpad, event);
2920 /* this is the final event function we receive on the internal source pad when
2921 * we deal with TCP connections */
2923 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2928 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2930 switch (GST_EVENT_TYPE (event)) {
2931 case GST_EVENT_SEEK:
2933 case GST_EVENT_NAVIGATION:
2934 case GST_EVENT_LATENCY:
2936 gst_event_unref (event);
2943 /* this is the final query function we receive on the internal source pad when
2944 * we deal with TCP connections */
2946 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2950 gboolean res = TRUE;
2952 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2954 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2955 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2957 switch (GST_QUERY_TYPE (query)) {
2958 case GST_QUERY_POSITION:
2963 case GST_QUERY_DURATION:
2967 gst_query_parse_duration (query, &format, NULL);
2970 case GST_FORMAT_TIME:
2971 gst_query_set_duration (query, format, src->segment.duration);
2979 case GST_QUERY_LATENCY:
2981 /* we are live with a min latency of 0 and unlimited max latency, this
2982 * result will be updated by the session manager if there is any. */
2983 gst_query_set_latency (query, TRUE, 0, -1);
2993 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2995 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2999 gboolean res = FALSE;
3001 src = GST_RTSPSRC_CAST (parent);
3003 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
3004 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3006 switch (GST_QUERY_TYPE (query)) {
3007 case GST_QUERY_DURATION:
3011 gst_query_parse_duration (query, &format, NULL);
3014 case GST_FORMAT_TIME:
3015 gst_query_set_duration (query, format, src->segment.duration);
3023 case GST_QUERY_SEEKING:
3027 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
3028 if (format == GST_FORMAT_TIME) {
3030 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
3031 GstClockTime start = 0, duration = src->segment.duration;
3033 /* seeking without duration is unlikely */
3034 seekable = seekable && src->seekable >= 0.0 && src->segment.duration &&
3035 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
3038 if (src->seekable > 0.0) {
3039 start = src->last_pos - src->seekable * GST_SECOND;
3041 /* src->seekable == 0 means that we can only seek to 0 */
3047 GST_LOG_OBJECT (src, "seekable : %d", seekable);
3049 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, start,
3059 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
3061 gst_query_set_uri (query, uri);
3069 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
3071 /* forward the query to the proxy target pad */
3073 res = gst_pad_query (target, query);
3074 gst_object_unref (target);
3083 /* callback for RTCP messages to be sent to the server when operating in TCP
3085 static GstFlowReturn
3086 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
3089 GstRTSPStream *stream;
3090 GstFlowReturn res = GST_FLOW_OK;
3095 GstRTSPMessage message = { 0 };
3096 GstRTSPConnInfo *conninfo;
3098 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
3099 src = stream->parent;
3101 gst_buffer_map (buffer, &map, GST_MAP_READ);
3105 gst_rtsp_message_init_data (&message, stream->channel[1]);
3107 /* lend the body data to the message */
3108 gst_rtsp_message_take_body (&message, data, size);
3110 if (stream->conninfo.connection)
3111 conninfo = &stream->conninfo;
3113 conninfo = &src->conninfo;
3115 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
3116 ret = gst_rtspsrc_connection_send (src, conninfo, &message, NULL);
3117 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
3119 /* and steal it away again because we will free it when unreffing the
3121 gst_rtsp_message_steal_body (&message, &data, &size);
3122 gst_rtsp_message_unset (&message);
3124 gst_buffer_unmap (buffer, &map);
3125 gst_buffer_unref (buffer);
3130 static GstFlowReturn
3131 gst_rtspsrc_push_backchannel_buffer (GstRTSPSrc * src, guint id,
3134 GstFlowReturn res = GST_FLOW_OK;
3135 GstRTSPStream *stream;
3137 if (!src->conninfo.connected || src->state != GST_RTSP_STATE_PLAYING)
3140 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
3141 if (stream == NULL) {
3142 GST_ERROR_OBJECT (src, "no stream with id %u", id);
3146 if (src->interleaved) {
3152 GstRTSPMessage message = { 0 };
3153 GstRTSPConnInfo *conninfo;
3155 buffer = gst_sample_get_buffer (sample);
3157 gst_buffer_map (buffer, &map, GST_MAP_READ);
3161 gst_rtsp_message_init_data (&message, stream->channel[0]);
3163 /* lend the body data to the message */
3164 gst_rtsp_message_take_body (&message, data, size);
3166 if (stream->conninfo.connection)
3167 conninfo = &stream->conninfo;
3169 conninfo = &src->conninfo;
3171 GST_DEBUG_OBJECT (src, "sending %u bytes backchannel RTP", size);
3172 ret = gst_rtspsrc_connection_send (src, conninfo, &message, NULL);
3173 GST_DEBUG_OBJECT (src, "sent backchannel RTP, %d", ret);
3175 /* and steal it away again because we will free it when unreffing the
3177 gst_rtsp_message_steal_body (&message, &data, &size);
3178 gst_rtsp_message_unset (&message);
3180 gst_buffer_unmap (buffer, &map);
3184 g_signal_emit_by_name (stream->rtpsrc, "push-sample", sample, &res);
3185 GST_DEBUG_OBJECT (src, "sent backchannel RTP sample %p: %s", sample,
3186 gst_flow_get_name (res));
3190 gst_sample_unref (sample);
3195 static GstPadProbeReturn
3196 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3198 GstRTSPSrc *src = user_data;
3200 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
3201 GST_DEBUG_PAD_NAME (pad));
3203 /* activate the streams */
3204 GST_OBJECT_LOCK (src);
3205 if (!src->need_activate)
3208 src->need_activate = FALSE;
3209 GST_OBJECT_UNLOCK (src);
3211 gst_rtspsrc_activate_streams (src);
3213 return GST_PAD_PROBE_OK;
3217 GST_OBJECT_UNLOCK (src);
3218 return GST_PAD_PROBE_OK;
3222 static GstPadProbeReturn
3223 udpsrc_probe_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3225 guint32 *segment_seqnum = user_data;
3227 switch (GST_EVENT_TYPE (info->data)) {
3228 case GST_EVENT_SEGMENT:
3229 if (!gst_event_is_writable (info->data))
3230 info->data = gst_event_make_writable (info->data);
3232 *segment_seqnum = gst_event_get_seqnum (info->data);
3237 return GST_PAD_PROBE_OK;
3241 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
3243 GstPad *gpad = GST_PAD_CAST (user_data);
3245 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
3246 gst_pad_store_sticky_event (gpad, *event);
3252 add_backchannel_fakesink (GstRTSPSrc * src, GstRTSPStream * stream,
3256 GstElement *fakesink;
3258 fakesink = gst_element_factory_make ("fakesink", NULL);
3259 if (fakesink == NULL) {
3260 GST_ERROR_OBJECT (src, "no fakesink");
3264 sinkpad = gst_element_get_static_pad (fakesink, "sink");
3266 GST_DEBUG_OBJECT (src, "backchannel stream %p, hooking fakesink", stream);
3268 gst_bin_add (GST_BIN_CAST (src), fakesink);
3269 if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK) {
3270 GST_WARNING_OBJECT (src, "could not link to fakesink");
3274 gst_object_unref (sinkpad);
3276 gst_element_sync_state_with_parent (fakesink);
3280 /* this callback is called when the session manager generated a new src pad with
3281 * payloaded RTP packets. We simply ghost the pad here. */
3283 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
3286 GstPadTemplate *template;
3289 GstRTSPStream *stream;
3291 GstPad *internal_src;
3293 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
3295 GST_RTSP_STATE_LOCK (src);
3297 name = gst_object_get_name (GST_OBJECT_CAST (pad));
3298 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
3299 goto unknown_stream;
3301 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
3303 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
3305 goto unknown_stream;
3308 stream->ssrc = ssrc;
3310 /* we'll add it later see below */
3311 stream->added = TRUE;
3313 /* check if we added all streams */
3315 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
3316 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
3318 GST_DEBUG_OBJECT (src, "stream %p, container %d, added %d, setup %d",
3319 ostream, ostream->container, ostream->added, ostream->setup);
3321 /* if we find a stream for which we did a setup that is not added, we
3322 * need to wait some more */
3323 if (ostream->setup && !ostream->added) {
3328 GST_RTSP_STATE_UNLOCK (src);
3330 /* create a new pad we will use to stream to */
3331 template = gst_static_pad_template_get (&rtptemplate);
3332 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
3333 gst_object_unref (template);
3336 /* We intercept and modify the stream start event */
3338 GST_PAD (gst_proxy_pad_get_internal (GST_PROXY_PAD (stream->srcpad)));
3339 gst_pad_set_element_private (internal_src, stream);
3340 gst_pad_set_event_function (internal_src, gst_rtspsrc_handle_src_sink_event);
3341 gst_object_unref (internal_src);
3343 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3344 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3345 gst_pad_set_active (stream->srcpad, TRUE);
3346 gst_pad_sticky_events_foreach (pad, copy_sticky_events, stream->srcpad);
3348 /* don't add the srcpad if this is a sendonly stream */
3349 if (stream->is_backchannel)
3350 add_backchannel_fakesink (src, stream, stream->srcpad);
3352 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3355 GST_DEBUG_OBJECT (src, "We added all streams");
3356 /* when we get here, all stream are added and we can fire the no-more-pads
3358 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
3366 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
3367 GST_RTSP_STATE_UNLOCK (src);
3374 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
3378 len = stream->ptmap->len;
3379 for (i = 0; i < len; i++) {
3380 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3388 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
3390 GstRTSPStream *stream;
3393 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
3395 GST_RTSP_STATE_LOCK (src);
3396 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
3398 goto unknown_stream;
3400 if ((caps = stream_get_caps_for_pt (stream, pt)))
3401 gst_caps_ref (caps);
3402 GST_RTSP_STATE_UNLOCK (src);
3408 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
3409 GST_RTSP_STATE_UNLOCK (src);
3415 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
3417 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
3423 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
3429 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
3435 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
3437 GstRTSPSrc *src = stream->parent;
3440 g_object_get (source, "ssrc", &ssrc, NULL);
3442 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
3443 ssrc, stream->ssrc, stream->id);
3445 if (ssrc == stream->ssrc)
3446 gst_rtspsrc_do_stream_eos (src, stream);
3450 on_timeout_common (GObject * session, GObject * source, GstRTSPStream * stream)
3452 GstRTSPSrc *src = stream->parent;
3455 g_object_get (source, "ssrc", &ssrc, NULL);
3457 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
3458 ssrc, stream->ssrc, stream->id);
3460 if (ssrc == stream->ssrc)
3461 gst_rtspsrc_do_stream_eos (src, stream);
3465 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
3467 GstRTSPSrc *src = stream->parent;
3469 /* timeout, post element message */
3470 gst_element_post_message (GST_ELEMENT_CAST (src),
3471 gst_message_new_element (GST_OBJECT_CAST (src),
3472 gst_structure_new ("GstRTSPSrcTimeout",
3473 "cause", G_TYPE_ENUM, GST_RTSP_SRC_TIMEOUT_CAUSE_RTCP,
3474 "stream-number", G_TYPE_INT, stream->id, "ssrc", G_TYPE_UINT,
3475 stream->ssrc, NULL)));
3477 on_timeout_common (session, source, stream);
3481 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
3483 GstRTSPStream *stream;
3485 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
3487 /* get stream for session */
3488 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
3490 gst_rtspsrc_do_stream_eos (src, stream);
3495 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
3497 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
3502 set_manager_buffer_mode (GstRTSPSrc * src)
3504 GObjectClass *klass;
3506 if (src->manager == NULL)
3509 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3511 if (!g_object_class_find_property (klass, "buffer-mode"))
3514 if (src->buffer_mode != BUFFER_MODE_AUTO) {
3515 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
3520 GST_DEBUG_OBJECT (src,
3521 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
3523 if (src->provided_clock) {
3524 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
3526 if (clock == src->provided_clock) {
3527 GST_DEBUG_OBJECT (src, "selected synced");
3528 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
3531 gst_object_unref (clock);
3536 /* Otherwise fall-through and use another buffer mode */
3538 gst_object_unref (clock);
3541 GST_DEBUG_OBJECT (src, "auto buffering mode");
3542 if (src->use_buffering) {
3543 GST_DEBUG_OBJECT (src, "selected buffer");
3544 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
3546 GST_DEBUG_OBJECT (src, "selected slave");
3547 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
3552 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
3556 GstMIKEYMessage *msg = stream->mikey;
3558 GST_DEBUG ("request key SSRC %u", ssrc);
3560 caps = gst_caps_ref (stream_get_caps_for_pt (stream, stream->default_pt));
3561 caps = gst_caps_make_writable (caps);
3563 /* parse crypto sessions and look for the SSRC rollover counter */
3564 msg = stream->mikey;
3565 for (i = 0; msg && i < gst_mikey_message_get_n_cs (msg); i++) {
3566 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
3568 if (ssrc == map->ssrc) {
3569 gst_caps_set_simple (caps, "roc", G_TYPE_UINT, map->roc, NULL);
3578 request_rtp_decoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
3580 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3581 if (stream->id != session)
3584 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3585 stream->profile != GST_RTSP_PROFILE_SAVPF)
3588 if (stream->srtpdec == NULL) {
3591 name = g_strdup_printf ("srtpdec_%u", session);
3592 stream->srtpdec = gst_element_factory_make ("srtpdec", name);
3595 if (stream->srtpdec == NULL) {
3596 GST_ELEMENT_ERROR (stream->parent, CORE, MISSING_PLUGIN, (NULL),
3597 ("no srtpdec element present!"));
3600 g_signal_connect (stream->srtpdec, "request-key",
3601 (GCallback) request_key, stream);
3603 return gst_object_ref (stream->srtpdec);
3607 request_rtcp_encoder (GstElement * rtpbin, guint session,
3608 GstRTSPStream * stream)
3613 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3614 if (stream->id != session)
3617 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3618 stream->profile != GST_RTSP_PROFILE_SAVPF)
3621 if (stream->srtpenc == NULL) {
3624 name = g_strdup_printf ("srtpenc_%u", session);
3625 stream->srtpenc = gst_element_factory_make ("srtpenc", name);
3628 if (stream->srtpenc == NULL) {
3629 GST_ELEMENT_ERROR (stream->parent, CORE, MISSING_PLUGIN, (NULL),
3630 ("no srtpenc element present!"));
3634 /* get RTCP crypto parameters from caps */
3635 s = gst_caps_get_structure (stream->srtcpparams, 0);
3639 GType ciphertype, authtype;
3640 GValue rtcp_cipher = G_VALUE_INIT, rtcp_auth = G_VALUE_INIT;
3642 ciphertype = g_type_from_name ("GstSrtpCipherType");
3643 authtype = g_type_from_name ("GstSrtpAuthType");
3644 g_value_init (&rtcp_cipher, ciphertype);
3645 g_value_init (&rtcp_auth, authtype);
3647 str = gst_structure_get_string (s, "srtcp-cipher");
3648 gst_value_deserialize (&rtcp_cipher, str);
3649 str = gst_structure_get_string (s, "srtcp-auth");
3650 gst_value_deserialize (&rtcp_auth, str);
3651 gst_structure_get (s, "srtp-key", GST_TYPE_BUFFER, &buf, NULL);
3653 g_object_set_property (G_OBJECT (stream->srtpenc), "rtp-cipher",
3655 g_object_set_property (G_OBJECT (stream->srtpenc), "rtp-auth",
3657 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-cipher",
3659 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-auth",
3661 g_object_set (stream->srtpenc, "key", buf, NULL);
3663 g_value_unset (&rtcp_cipher);
3664 g_value_unset (&rtcp_auth);
3665 gst_buffer_unref (buf);
3668 name = g_strdup_printf ("rtcp_sink_%d", session);
3669 pad = gst_element_get_request_pad (stream->srtpenc, name);
3671 gst_object_unref (pad);
3673 return gst_object_ref (stream->srtpenc);
3677 request_aux_receiver (GstElement * rtpbin, guint sessid, GstRTSPSrc * src)
3679 GstElement *rtx, *bin;
3682 GstRTSPStream *stream;
3684 stream = find_stream (src, &sessid, (gpointer) find_stream_by_id);
3686 GST_WARNING_OBJECT (src, "Stream %u not found", sessid);
3690 GST_INFO_OBJECT (src, "creating retransmision receiver for session %u "
3691 "with map %" GST_PTR_FORMAT, sessid, stream->rtx_pt_map);
3692 bin = gst_bin_new (NULL);
3693 rtx = gst_element_factory_make ("rtprtxreceive", NULL);
3694 g_object_set (rtx, "payload-type-map", stream->rtx_pt_map, NULL);
3695 gst_bin_add (GST_BIN (bin), rtx);
3697 pad = gst_element_get_static_pad (rtx, "src");
3698 name = g_strdup_printf ("src_%u", sessid);
3699 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3701 gst_object_unref (pad);
3703 pad = gst_element_get_static_pad (rtx, "sink");
3704 name = g_strdup_printf ("sink_%u", sessid);
3705 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3707 gst_object_unref (pad);
3713 add_retransmission (GstRTSPSrc * src, GstRTSPTransport * transport)
3717 gboolean do_retransmission = FALSE;
3719 if (transport->trans != GST_RTSP_TRANS_RTP)
3721 if (transport->profile != GST_RTSP_PROFILE_AVPF &&
3722 transport->profile != GST_RTSP_PROFILE_SAVPF)
3725 signal_id = g_signal_lookup ("request-aux-receiver",
3726 G_OBJECT_TYPE (src->manager));
3727 /* there's already something connected */
3728 if (g_signal_handler_find (src->manager, G_SIGNAL_MATCH_ID, signal_id, 0,
3729 NULL, NULL, NULL) != 0) {
3730 GST_DEBUG_OBJECT (src, "Not adding RTX AUX element as "
3731 "\"request-aux-receiver\" signal is "
3732 "already used by the application");
3736 /* build the retransmission payload type map */
3737 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3738 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3739 gboolean do_retransmission_stream = FALSE;
3742 if (stream->rtx_pt_map)
3743 gst_structure_free (stream->rtx_pt_map);
3744 stream->rtx_pt_map = gst_structure_new_empty ("application/x-rtp-pt-map");
3746 for (i = 0; i < stream->ptmap->len; i++) {
3747 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3748 GstStructure *s = gst_caps_get_structure (item->caps, 0);
3749 const gchar *encoding;
3751 /* we only care about RTX streams */
3752 if ((encoding = gst_structure_get_string (s, "encoding-name"))
3753 && g_strcmp0 (encoding, "RTX") == 0) {
3754 const gchar *stream_pt_s;
3757 if (gst_structure_get_int (s, "payload", &rtx_pt)
3758 && (stream_pt_s = gst_structure_get_string (s, "apt"))) {
3761 gst_structure_set (stream->rtx_pt_map, stream_pt_s, G_TYPE_UINT,
3763 do_retransmission_stream = TRUE;
3769 if (do_retransmission_stream) {
3770 GST_DEBUG_OBJECT (src, "built retransmission payload map for stream "
3771 "id %i: %" GST_PTR_FORMAT, stream->id, stream->rtx_pt_map);
3772 do_retransmission = TRUE;
3774 GST_DEBUG_OBJECT (src, "no retransmission payload map for stream "
3775 "id %i", stream->id);
3776 gst_structure_free (stream->rtx_pt_map);
3777 stream->rtx_pt_map = NULL;
3781 if (do_retransmission) {
3782 GST_DEBUG_OBJECT (src, "Enabling retransmissions");
3784 g_object_set (src->manager, "do-retransmission", TRUE, NULL);
3786 /* enable RFC4588 retransmission handling by setting rtprtxreceive
3787 * as the "aux" element of rtpbin */
3788 g_signal_connect (src->manager, "request-aux-receiver",
3789 (GCallback) request_aux_receiver, src);
3791 GST_DEBUG_OBJECT (src,
3792 "Not enabling retransmissions as no stream had a retransmission payload map");
3796 /* try to get and configure a manager */
3798 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
3799 GstRTSPTransport * transport)
3801 const gchar *manager;
3803 GstStateChangeReturn ret;
3805 /* find a manager */
3806 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
3810 GST_DEBUG_OBJECT (src, "using manager %s", manager);
3812 /* configure the manager */
3813 if (src->manager == NULL) {
3814 GObjectClass *klass;
3816 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
3818 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
3822 goto use_no_manager;
3824 if (!(src->manager = gst_element_factory_make (manager, "manager")))
3825 goto manager_failed;
3828 /* we manage this element */
3829 gst_element_set_locked_state (src->manager, TRUE);
3830 gst_bin_add (GST_BIN_CAST (src), src->manager);
3832 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
3833 if (ret == GST_STATE_CHANGE_FAILURE)
3834 goto start_manager_failure;
3836 g_object_set (src->manager, "latency", src->latency, NULL);
3838 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3840 if (g_object_class_find_property (klass, "ntp-sync")) {
3841 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
3844 if (g_object_class_find_property (klass, "rfc7273-sync")) {
3845 g_object_set (src->manager, "rfc7273-sync", src->rfc7273_sync, NULL);
3848 if (src->use_pipeline_clock) {
3849 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
3850 g_object_set (src->manager, "use-pipeline-clock", TRUE, NULL);
3853 if (g_object_class_find_property (klass, "ntp-time-source")) {
3854 g_object_set (src->manager, "ntp-time-source", src->ntp_time_source,
3859 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
3860 g_object_set (src->manager, "sdes", src->sdes, NULL);
3863 if (g_object_class_find_property (klass, "drop-on-latency")) {
3864 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
3868 if (g_object_class_find_property (klass, "max-rtcp-rtp-time-diff")) {
3869 g_object_set (src->manager, "max-rtcp-rtp-time-diff",
3870 src->max_rtcp_rtp_time_diff, NULL);
3873 if (g_object_class_find_property (klass, "max-ts-offset-adjustment")) {
3874 g_object_set (src->manager, "max-ts-offset-adjustment",
3875 src->max_ts_offset_adjustment, NULL);
3878 if (g_object_class_find_property (klass, "max-ts-offset")) {
3879 gint64 max_ts_offset;
3881 /* setting max-ts-offset in the manager has side effects so only do it
3882 * if the value differs */
3883 g_object_get (src->manager, "max-ts-offset", &max_ts_offset, NULL);
3884 if (max_ts_offset != src->max_ts_offset) {
3885 g_object_set (src->manager, "max-ts-offset", src->max_ts_offset,
3890 /* buffer mode pauses are handled by adding offsets to buffer times,
3891 * but some depayloaders may have a hard time syncing output times
3892 * with such input times, e.g. container ones, most notably ASF */
3893 /* TODO alternatives are having an event that indicates these shifts,
3894 * or having rtsp extensions provide suggestion on buffer mode */
3895 /* valid duration implies not likely live pipeline,
3896 * so slaving in jitterbuffer does not make much sense
3897 * (and might mess things up due to bursts) */
3898 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
3899 src->segment.duration && stream->container) {
3900 src->use_buffering = TRUE;
3902 src->use_buffering = FALSE;
3905 set_manager_buffer_mode (src);
3907 /* connect to signals */
3908 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
3910 src->manager_sig_id =
3911 g_signal_connect (src->manager, "pad-added",
3912 (GCallback) new_manager_pad, src);
3913 src->manager_ptmap_id =
3914 g_signal_connect (src->manager, "request-pt-map",
3915 (GCallback) request_pt_map, src);
3917 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
3920 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
3923 if (src->do_retransmission)
3924 add_retransmission (src, transport);
3926 g_signal_connect (src->manager, "request-rtp-decoder",
3927 (GCallback) request_rtp_decoder, stream);
3928 g_signal_connect (src->manager, "request-rtcp-decoder",
3929 (GCallback) request_rtp_decoder, stream);
3930 g_signal_connect (src->manager, "request-rtcp-encoder",
3931 (GCallback) request_rtcp_encoder, stream);
3933 /* we stream directly to the manager, get some pads. Each RTSP stream goes
3934 * into a separate RTP session. */
3935 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
3936 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
3938 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
3939 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
3942 /* now configure the bandwidth in the manager */
3943 if (g_signal_lookup ("get-internal-session",
3944 G_OBJECT_TYPE (src->manager)) != 0) {
3945 GObject *rtpsession;
3947 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
3950 GstRTPProfile rtp_profile;
3952 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
3954 stream->session = rtpsession;
3956 if (stream->as_bandwidth != -1) {
3957 GST_INFO_OBJECT (src, "setting AS: %f",
3958 (gdouble) (stream->as_bandwidth * 1000));
3959 g_object_set (rtpsession, "bandwidth",
3960 (gdouble) (stream->as_bandwidth * 1000), NULL);
3962 if (stream->rr_bandwidth != -1) {
3963 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
3964 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
3967 if (stream->rs_bandwidth != -1) {
3968 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
3969 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
3973 switch (stream->profile) {
3974 case GST_RTSP_PROFILE_AVPF:
3975 rtp_profile = GST_RTP_PROFILE_AVPF;
3977 case GST_RTSP_PROFILE_SAVP:
3978 rtp_profile = GST_RTP_PROFILE_SAVP;
3980 case GST_RTSP_PROFILE_SAVPF:
3981 rtp_profile = GST_RTP_PROFILE_SAVPF;
3983 case GST_RTSP_PROFILE_AVP:
3985 rtp_profile = GST_RTP_PROFILE_AVP;
3989 g_object_set (rtpsession, "rtp-profile", rtp_profile, NULL);
3991 g_object_set (rtpsession, "probation", src->probation, NULL);
3993 g_object_set (rtpsession, "internal-ssrc", stream->send_ssrc, NULL);
3995 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
3997 g_signal_connect (rtpsession, "on-bye-timeout",
3998 (GCallback) on_timeout_common, stream);
3999 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
4001 g_signal_connect (rtpsession, "on-ssrc-active",
4002 (GCallback) on_ssrc_active, stream);
4013 GST_DEBUG_OBJECT (src, "cannot get a session manager");
4018 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
4021 start_manager_failure:
4023 GST_DEBUG_OBJECT (src, "could not start session manager");
4028 /* free the UDP sources allocated when negotiating a transport.
4029 * This function is called when the server negotiated to a transport where the
4030 * UDP sources are not needed anymore, such as TCP or multicast. */
4032 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
4036 for (i = 0; i < 2; i++) {
4037 if (stream->udpsrc[i]) {
4038 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
4039 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
4040 gst_object_unref (stream->udpsrc[i]);
4041 stream->udpsrc[i] = NULL;
4046 /* for TCP, create pads to send and receive data to and from the manager and to
4047 * intercept various events and queries
4050 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
4051 GstRTSPTransport * transport, GstPad ** outpad)
4054 GstPadTemplate *template;
4055 GstPad *pad0, *pad1;
4057 /* configure for interleaved delivery, nothing needs to be done
4058 * here, the loop function will call the chain functions of the
4059 * session manager. */
4060 stream->channel[0] = transport->interleaved.min;
4061 stream->channel[1] = transport->interleaved.max;
4062 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
4063 stream->channel[0], stream->channel[1]);
4065 /* we can remove the allocated UDP ports now */
4066 gst_rtspsrc_stream_free_udp (stream);
4068 /* no session manager, send data to srcpad directly */
4069 if (!stream->channelpad[0]) {
4070 GST_DEBUG_OBJECT (src, "no manager, creating pad");
4072 /* create a new pad we will use to stream to */
4073 name = g_strdup_printf ("stream_%u", stream->id);
4074 template = gst_static_pad_template_get (&rtptemplate);
4075 stream->channelpad[0] = gst_pad_new_from_template (template, name);
4076 gst_object_unref (template);
4079 /* set caps and activate */
4080 gst_pad_use_fixed_caps (stream->channelpad[0]);
4081 gst_pad_set_active (stream->channelpad[0], TRUE);
4083 *outpad = gst_object_ref (stream->channelpad[0]);
4085 GST_DEBUG_OBJECT (src, "using manager source pad");
4087 template = gst_static_pad_template_get (&anysrctemplate);
4089 /* allocate pads for sending the channel data into the manager */
4090 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
4091 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
4092 gst_object_unref (stream->channelpad[0]);
4093 stream->channelpad[0] = pad0;
4094 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
4095 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
4096 gst_pad_set_element_private (pad0, src);
4097 gst_pad_set_active (pad0, TRUE);
4099 if (stream->channelpad[1]) {
4100 /* if we have a sinkpad for the other channel, create a pad and link to the
4102 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
4103 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
4104 gst_pad_link_full (pad1, stream->channelpad[1],
4105 GST_PAD_LINK_CHECK_NOTHING);
4106 gst_object_unref (stream->channelpad[1]);
4107 stream->channelpad[1] = pad1;
4108 gst_pad_set_active (pad1, TRUE);
4110 gst_object_unref (template);
4112 /* setup RTCP transport back to the server if we have to. */
4113 if (src->manager && src->do_rtcp) {
4116 template = gst_static_pad_template_get (&anysinktemplate);
4118 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
4119 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
4120 gst_pad_set_element_private (stream->rtcppad, stream);
4121 gst_pad_set_active (stream->rtcppad, TRUE);
4123 /* get session RTCP pad */
4124 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
4125 pad = gst_element_get_request_pad (src->manager, name);
4130 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
4131 gst_object_unref (pad);
4134 gst_object_unref (template);
4140 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
4141 GstRTSPTransport * transport, const gchar ** destination, gint * min,
4142 gint * max, guint * ttl)
4144 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
4146 if (!(*destination = transport->destination))
4147 *destination = stream->destination;
4150 /* transport first */
4151 *min = transport->port.min;
4152 *max = transport->port.max;
4153 if (*min == -1 && *max == -1) {
4154 /* then try from SDP */
4155 if (stream->port != 0) {
4156 *min = stream->port;
4157 *max = stream->port + 1;
4163 if (!(*ttl = transport->ttl))
4168 /* first take the source, then the endpoint to figure out where to send
4170 if (!(*destination = transport->source)) {
4171 if (src->conninfo.connection)
4172 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
4173 else if (stream->conninfo.connection)
4175 gst_rtsp_connection_get_ip (stream->conninfo.connection);
4179 /* for unicast we only expect the ports here */
4180 *min = transport->server_port.min;
4181 *max = transport->server_port.max;
4186 /* For multicast create UDP sources and join the multicast group. */
4188 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
4189 GstRTSPTransport * transport, GstPad ** outpad)
4192 const gchar *destination;
4195 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
4197 /* we can remove the allocated UDP ports now */
4198 gst_rtspsrc_stream_free_udp (stream);
4200 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
4203 /* we need a destination now */
4204 if (destination == NULL)
4205 goto no_destination;
4207 /* we really need ports now or we won't be able to receive anything at all */
4208 if (min == -1 && max == -1)
4211 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
4212 destination, min, max);
4214 /* creating UDP source for RTP */
4216 uri = g_strdup_printf ("udp://%s:%d", destination, min);
4218 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
4220 if (stream->udpsrc[0] == NULL)
4223 /* take ownership */
4224 gst_object_ref_sink (stream->udpsrc[0]);
4226 if (src->udp_buffer_size != 0)
4227 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
4228 src->udp_buffer_size, NULL);
4230 if (src->multi_iface != NULL)
4231 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
4232 src->multi_iface, NULL);
4235 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
4236 gst_element_set_state (stream->udpsrc[0], GST_STATE_READY);
4239 /* creating another UDP source for RTCP */
4243 uri = g_strdup_printf ("udp://%s:%d", destination, max);
4245 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
4247 if (stream->udpsrc[1] == NULL)
4250 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
4251 stream->profile == GST_RTSP_PROFILE_SAVPF)
4252 caps = gst_caps_new_empty_simple ("application/x-srtcp");
4254 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4255 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
4256 gst_caps_unref (caps);
4258 /* take ownership */
4259 gst_object_ref_sink (stream->udpsrc[1]);
4261 if (src->multi_iface != NULL)
4262 g_object_set (G_OBJECT (stream->udpsrc[1]), "multicast-iface",
4263 src->multi_iface, NULL);
4265 gst_element_set_state (stream->udpsrc[1], GST_STATE_READY);
4272 GST_DEBUG_OBJECT (src, "no UDP source element found");
4277 GST_DEBUG_OBJECT (src, "no destination found");
4282 GST_DEBUG_OBJECT (src, "no ports found");
4287 /* configure the remainder of the UDP ports */
4289 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
4290 GstRTSPTransport * transport, GstPad ** outpad)
4292 /* we manage the UDP elements now. For unicast, the UDP sources where
4293 * allocated in the stream when we suggested a transport. */
4294 if (stream->udpsrc[0]) {
4297 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
4298 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
4300 GST_DEBUG_OBJECT (src, "setting up UDP source");
4302 /* configure a timeout on the UDP port. When the timeout message is
4303 * posted, we assume UDP transport is not possible. We reconnect using TCP
4305 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
4306 src->udp_timeout * 1000, NULL);
4308 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt)))
4309 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
4311 /* get output pad of the UDP source. */
4312 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
4314 /* save it so we can unblock */
4315 stream->blockedpad = *outpad;
4317 /* configure pad block on the pad. As soon as there is dataflow on the
4318 * UDP source, we know that UDP is not blocked by a firewall and we can
4319 * configure all the streams to let the application autoplug decoders. */
4321 gst_pad_add_probe (stream->blockedpad,
4322 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
4323 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocked, src, NULL);
4325 gst_pad_add_probe (stream->blockedpad,
4326 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM, udpsrc_probe_cb,
4327 &(stream->segment_seqnum[0]), NULL);
4329 if (stream->channelpad[0]) {
4330 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
4331 /* configure for UDP delivery, we need to connect the UDP pads to
4332 * the session plugin. */
4333 gst_pad_link_full (*outpad, stream->channelpad[0],
4334 GST_PAD_LINK_CHECK_NOTHING);
4335 gst_object_unref (*outpad);
4337 /* we connected to pad-added signal to get pads from the manager */
4339 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
4344 if (stream->udpsrc[1]) {
4347 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
4348 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
4350 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
4351 stream->profile == GST_RTSP_PROFILE_SAVPF)
4352 caps = gst_caps_new_empty_simple ("application/x-srtcp");
4354 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4355 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
4356 gst_caps_unref (caps);
4358 if (stream->channelpad[1]) {
4361 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
4363 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
4364 gst_pad_add_probe (pad,
4365 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM, udpsrc_probe_cb,
4366 &(stream->segment_seqnum[1]), NULL);
4367 gst_pad_link_full (pad, stream->channelpad[1],
4368 GST_PAD_LINK_CHECK_NOTHING);
4369 gst_object_unref (pad);
4371 /* leave unlinked */
4377 /* configure the UDP sink back to the server for status reports */
4379 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
4380 GstRTSPStream * stream, GstRTSPTransport * transport)
4383 gint rtp_port, rtcp_port;
4384 gboolean do_rtp, do_rtcp;
4385 const gchar *destination;
4390 /* get transport info */
4391 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
4392 &rtp_port, &rtcp_port, &ttl);
4394 /* see what we need to do */
4395 do_rtp = (rtp_port != -1);
4396 /* it's possible that the server does not want us to send RTCP in which case
4398 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
4400 /* we need a destination when we have RTP or RTCP ports */
4401 if (destination == NULL && (do_rtp || do_rtcp))
4402 goto no_destination;
4404 /* try to construct the fakesrc to the RTP port of the server to open up any
4405 * NAT firewalls or, if backchannel, construct an appsrc */
4407 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
4410 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
4411 stream->udpsink[0] =
4412 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
4414 if (stream->udpsink[0] == NULL)
4415 goto no_sink_element;
4417 /* don't join multicast group, we will have the source socket do that */
4418 /* no sync or async state changes needed */
4419 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
4420 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
4422 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
4424 if (stream->udpsrc[0]) {
4425 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
4426 * so that NAT firewalls will open a hole for us */
4427 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
4431 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
4432 /* configure socket and make sure udpsink does not close it when shutting
4433 * down, it belongs to udpsrc after all. */
4434 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
4435 "close-socket", FALSE, NULL);
4436 g_object_unref (socket);
4439 if (stream->is_backchannel) {
4440 /* appsrc is for the app to shovel data using push-backchannel-buffer */
4441 stream->rtpsrc = gst_element_factory_make ("appsrc", NULL);
4442 if (stream->rtpsrc == NULL)
4443 goto no_appsrc_element;
4445 /* interal use only, don't emit signals */
4446 g_object_set (G_OBJECT (stream->rtpsrc), "emit-signals", TRUE,
4447 "is-live", TRUE, NULL);
4449 /* the source for the dummy packets to open up NAT */
4450 stream->rtpsrc = gst_element_factory_make ("fakesrc", NULL);
4451 if (stream->rtpsrc == NULL)
4452 goto no_fakesrc_element;
4454 /* random data in 5 buffers, a size of 200 bytes should be fine */
4455 g_object_set (G_OBJECT (stream->rtpsrc), "filltype", 3, "num-buffers", 5,
4456 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
4459 /* keep everything locked */
4460 gst_element_set_locked_state (stream->udpsink[0], TRUE);
4461 gst_element_set_locked_state (stream->rtpsrc, TRUE);
4463 gst_object_ref (stream->udpsink[0]);
4464 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
4465 gst_object_ref (stream->rtpsrc);
4466 gst_bin_add (GST_BIN_CAST (src), stream->rtpsrc);
4468 gst_element_link_pads_full (stream->rtpsrc, "src", stream->udpsink[0],
4469 "sink", GST_PAD_LINK_CHECK_NOTHING);
4472 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
4475 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
4476 stream->udpsink[1] =
4477 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
4479 if (stream->udpsink[1] == NULL)
4480 goto no_sink_element;
4482 /* don't join multicast group, we will have the source socket do that */
4483 /* no sync or async state changes needed */
4484 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
4485 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
4487 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
4489 if (stream->udpsrc[1]) {
4490 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
4491 * because some servers check the port number of where it sends RTCP to identify
4492 * the RTCP packets it receives */
4493 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
4497 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
4498 /* configure socket and make sure udpsink does not close it when shutting
4499 * down, it belongs to udpsrc after all. */
4500 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
4501 "close-socket", FALSE, NULL);
4502 g_object_unref (socket);
4505 /* we keep this playing always */
4506 gst_element_set_locked_state (stream->udpsink[1], TRUE);
4507 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
4509 gst_object_ref (stream->udpsink[1]);
4510 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
4512 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
4514 /* get session RTCP pad */
4515 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
4516 pad = gst_element_get_request_pad (src->manager, name);
4521 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
4522 gst_object_unref (pad);
4531 GST_ERROR_OBJECT (src, "no destination address specified");
4536 GST_ERROR_OBJECT (src, "no UDP sink element found");
4541 GST_ERROR_OBJECT (src, "no appsrc element found");
4546 GST_ERROR_OBJECT (src, "no fakesrc element found");
4551 GST_ERROR_OBJECT (src, "failed to create socket");
4556 /* sets up all elements needed for streaming over the specified transport.
4557 * Does not yet expose the element pads, this will be done when there is actuall
4558 * dataflow detected, which might never happen when UDP is blocked in a
4559 * firewall, for example.
4562 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
4563 GstRTSPTransport * transport)
4566 GstPad *outpad = NULL;
4567 GstPadTemplate *template;
4569 const gchar *media_type;
4572 src = stream->parent;
4574 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
4576 /* get the proper media type for this stream now */
4577 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
4578 goto unknown_transport;
4580 goto unknown_transport;
4582 /* configure the final media type */
4583 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
4585 len = stream->ptmap->len;
4586 for (i = 0; i < len; i++) {
4588 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
4590 if (item->caps == NULL)
4593 s = gst_caps_get_structure (item->caps, 0);
4594 gst_structure_set_name (s, media_type);
4595 /* set ssrc if known */
4596 if (transport->ssrc)
4597 gst_structure_set (s, "ssrc", G_TYPE_UINT, transport->ssrc, NULL);
4600 /* try to get and configure a manager, channelpad[0-1] will be configured with
4601 * the pads for the manager, or NULL when no manager is needed. */
4602 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
4605 switch (transport->lower_transport) {
4606 case GST_RTSP_LOWER_TRANS_TCP:
4607 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
4608 goto transport_failed;
4610 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
4611 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
4612 goto transport_failed;
4613 /* fallthrough, the rest is the same for UDP and MCAST */
4614 case GST_RTSP_LOWER_TRANS_UDP:
4615 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
4616 goto transport_failed;
4617 /* configure udpsinks back to the server for RTCP messages, for the
4618 * dummy RTP messages to open NAT, and for the backchannel */
4619 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
4620 goto transport_failed;
4623 goto unknown_transport;
4626 /* using backchannel and no manager, hence no srcpad for this stream */
4627 if (outpad && stream->is_backchannel) {
4628 add_backchannel_fakesink (src, stream, outpad);
4629 gst_object_unref (outpad);
4630 } else if (outpad) {
4631 GST_DEBUG_OBJECT (src, "creating ghostpad for stream %p", stream);
4633 gst_pad_use_fixed_caps (outpad);
4635 /* create ghostpad, don't add just yet, this will be done when we activate
4637 name = g_strdup_printf ("stream_%u", stream->id);
4638 template = gst_static_pad_template_get (&rtptemplate);
4639 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
4640 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
4641 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
4642 gst_object_unref (template);
4645 gst_object_unref (outpad);
4647 /* mark pad as ok */
4648 stream->last_ret = GST_FLOW_OK;
4655 GST_WARNING_OBJECT (src, "failed to configure transport");
4660 GST_WARNING_OBJECT (src, "unknown transport");
4665 GST_WARNING_OBJECT (src, "cannot get a session manager");
4670 /* send a couple of dummy random packets on the receiver RTP port to the server,
4671 * this should make a firewall think we initiated the data transfer and
4672 * hopefully allow packets to go from the sender port to our RTP receiver port */
4674 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
4678 if (src->nat_method != GST_RTSP_NAT_DUMMY)
4681 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4682 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4684 if (!stream->rtpsrc || !stream->udpsink[0])
4687 if (stream->is_backchannel)
4688 GST_DEBUG_OBJECT (src, "starting backchannel stream %p", stream);
4690 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
4692 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
4693 gst_element_set_state (stream->rtpsrc, GST_STATE_NULL);
4694 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
4695 gst_element_set_state (stream->rtpsrc, GST_STATE_PLAYING);
4700 /* Adds the source pads of all configured streams to the element.
4701 * This code is performed when we detected dataflow.
4703 * We detect dataflow from either the _loop function or with pad probes on the
4707 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
4711 GST_DEBUG_OBJECT (src, "activating streams");
4713 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4714 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4716 if (stream->udpsrc[0]) {
4717 /* remove timeout, we are streaming now and timeouts will be handled by
4718 * the session manager and jitter buffer */
4719 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
4721 if (stream->srcpad) {
4722 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
4723 gst_pad_set_active (stream->srcpad, TRUE);
4725 /* if we don't have a session manager, set the caps now. If we have a
4726 * session, we will get a notification of the pad and the caps. */
4727 if (!src->manager) {
4730 caps = stream_get_caps_for_pt (stream, stream->default_pt);
4731 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
4732 gst_pad_set_caps (stream->srcpad, caps);
4735 if (!stream->added) {
4736 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
4737 if (stream->is_backchannel)
4738 add_backchannel_fakesink (src, stream, stream->srcpad);
4740 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
4741 stream->added = TRUE;
4746 /* unblock all pads */
4747 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4748 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4750 if (stream->blockid) {
4751 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
4752 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
4753 stream->blockid = 0;
4761 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
4762 gboolean reset_manager)
4765 guint64 start, stop;
4766 gdouble play_speed, play_scale;
4768 GST_DEBUG_OBJECT (src, "configuring stream caps");
4770 start = segment->position;
4771 stop = segment->duration;
4772 play_speed = segment->rate;
4773 play_scale = segment->applied_rate;
4775 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4776 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4782 len = stream->ptmap->len;
4783 for (j = 0; j < len; j++) {
4785 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
4787 if (item->caps == NULL)
4790 caps = gst_caps_make_writable (item->caps);
4792 if (stream->timebase != -1)
4793 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
4794 (guint) stream->timebase, NULL);
4795 if (stream->seqbase != -1)
4796 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
4797 (guint) stream->seqbase, NULL);
4798 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
4800 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
4801 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
4802 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
4805 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
4808 if (item->pt == stream->default_pt) {
4809 if (stream->udpsrc[0])
4810 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
4811 stream->need_caps = TRUE;
4815 if (reset_manager && src->manager) {
4816 GST_DEBUG_OBJECT (src, "clear session");
4817 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
4821 static GstFlowReturn
4822 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
4827 /* store the value */
4828 stream->last_ret = ret;
4830 /* if it's success we can return the value right away */
4831 if (ret == GST_FLOW_OK)
4834 /* any other error that is not-linked can be returned right
4836 if (ret != GST_FLOW_NOT_LINKED)
4839 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4840 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4841 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4843 ret = ostream->last_ret;
4844 /* some other return value (must be SUCCESS but we can return
4845 * other values as well) */
4846 if (ret != GST_FLOW_NOT_LINKED)
4849 /* if we get here, all other pads were unlinked and we return
4850 * NOT_LINKED then */
4856 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
4859 gboolean res = TRUE;
4861 /* only streams that have a connection to the outside world */
4865 if (stream->udpsrc[0]) {
4866 GstEvent *sent_event;
4868 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
4869 sent_event = gst_event_new_eos ();
4870 gst_event_set_seqnum (sent_event, stream->segment_seqnum[0]);
4872 sent_event = gst_event_ref (event);
4875 res = gst_element_send_event (stream->udpsrc[0], sent_event);
4876 } else if (stream->channelpad[0]) {
4877 gst_event_ref (event);
4878 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4879 res = gst_pad_push_event (stream->channelpad[0], event);
4881 res = gst_pad_send_event (stream->channelpad[0], event);
4884 if (stream->udpsrc[1]) {
4885 GstEvent *sent_event;
4887 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
4888 sent_event = gst_event_new_eos ();
4889 if (stream->segment_seqnum[1] != GST_SEQNUM_INVALID) {
4890 gst_event_set_seqnum (sent_event, stream->segment_seqnum[1]);
4893 sent_event = gst_event_ref (event);
4896 res &= gst_element_send_event (stream->udpsrc[1], sent_event);
4897 } else if (stream->channelpad[1]) {
4898 gst_event_ref (event);
4899 if (GST_PAD_IS_SRC (stream->channelpad[1]))
4900 res &= gst_pad_push_event (stream->channelpad[1], event);
4902 res &= gst_pad_send_event (stream->channelpad[1], event);
4906 gst_event_unref (event);
4912 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
4915 gboolean res = TRUE;
4917 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4918 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4920 gst_event_ref (event);
4921 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
4923 gst_event_unref (event);
4929 accept_certificate_cb (GTlsConnection * conn, GTlsCertificate * peer_cert,
4930 GTlsCertificateFlags errors, gpointer user_data)
4932 GstRTSPSrc *src = user_data;
4933 gboolean accept = FALSE;
4935 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ACCEPT_CERTIFICATE], 0, conn,
4936 peer_cert, errors, &accept);
4941 static GstRTSPResult
4942 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4946 GstRTSPMessage response;
4947 gboolean retry = FALSE;
4948 memset (&response, 0, sizeof (response));
4949 gst_rtsp_message_init (&response);
4951 if (info->connection == NULL) {
4952 if (info->url == NULL) {
4953 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
4954 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
4957 /* create connection */
4958 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
4959 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
4960 goto could_not_create;
4963 gst_rtspsrc_setup_auth (src, &response);
4966 g_free (info->url_str);
4967 info->url_str = gst_rtsp_url_get_request_uri (info->url);
4969 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
4971 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
4972 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
4973 src->tls_validation_flags))
4974 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
4976 if (src->tls_database)
4977 gst_rtsp_connection_set_tls_database (info->connection,
4980 if (src->tls_interaction)
4981 gst_rtsp_connection_set_tls_interaction (info->connection,
4982 src->tls_interaction);
4983 gst_rtsp_connection_set_accept_certificate_func (info->connection,
4984 accept_certificate_cb, src, NULL);
4987 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
4988 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
4990 if (src->proxy_host) {
4991 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
4993 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
4998 if (!info->connected) {
5001 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
5002 ("Connecting to %s", info->location));
5003 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
5004 res = gst_rtsp_connection_connect_with_response (info->connection,
5005 src->ptcp_timeout, &response);
5007 if (response.type == GST_RTSP_MESSAGE_HTTP_RESPONSE &&
5008 response.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
5009 gst_rtsp_conninfo_close (src, info, TRUE);
5013 retry = FALSE; // we should not retry more than once
5018 if (res == GST_RTSP_OK)
5019 info->connected = TRUE;
5021 goto could_not_connect;
5023 } while (!info->connected && retry);
5025 gst_rtsp_message_unset (&response);
5031 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
5032 gst_rtsp_message_unset (&response);
5037 gchar *str = gst_rtsp_strresult (res);
5038 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
5040 gst_rtsp_message_unset (&response);
5045 gchar *str = gst_rtsp_strresult (res);
5046 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
5048 gst_rtsp_message_unset (&response);
5053 static GstRTSPResult
5054 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
5057 GST_RTSP_STATE_LOCK (src);
5058 if (info->connected) {
5059 GST_DEBUG_OBJECT (src, "closing connection...");
5060 gst_rtsp_connection_close (info->connection);
5061 info->connected = FALSE;
5063 if (free && info->connection) {
5064 /* free connection */
5065 GST_DEBUG_OBJECT (src, "freeing connection...");
5066 gst_rtsp_connection_free (info->connection);
5067 info->connection = NULL;
5068 info->flushing = FALSE;
5070 GST_RTSP_STATE_UNLOCK (src);
5074 static GstRTSPResult
5075 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
5080 GST_DEBUG_OBJECT (src, "reconnecting connection...");
5081 gst_rtsp_conninfo_close (src, info, FALSE);
5082 res = gst_rtsp_conninfo_connect (src, info, async);
5088 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
5092 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
5093 GST_RTSP_STATE_LOCK (src);
5094 if (src->conninfo.connection && src->conninfo.flushing != flush) {
5095 GST_DEBUG_OBJECT (src, "connection flush");
5096 gst_rtsp_connection_flush (src->conninfo.connection, flush);
5097 src->conninfo.flushing = flush;
5099 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5100 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5101 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
5102 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
5103 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
5104 stream->conninfo.flushing = flush;
5107 GST_RTSP_STATE_UNLOCK (src);
5110 static GstRTSPResult
5111 gst_rtspsrc_init_request (GstRTSPSrc * src, GstRTSPMessage * msg,
5112 GstRTSPMethod method, const gchar * uri)
5116 res = gst_rtsp_message_init_request (msg, method, uri);
5120 /* set user-agent */
5121 if (src->user_agent)
5122 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_USER_AGENT, src->user_agent);
5127 /* FIXME, handle server request, reply with OK, for now */
5128 static GstRTSPResult
5129 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
5130 GstRTSPMessage * request)
5132 GstRTSPMessage response = { 0 };
5135 GST_DEBUG_OBJECT (src, "got server request message");
5137 DEBUG_RTSP (src, request);
5139 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
5141 if (res == GST_RTSP_ENOTIMPL) {
5142 /* default implementation, send OK */
5143 GST_DEBUG_OBJECT (src, "prepare OK reply");
5145 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
5150 /* let app parse and reply */
5151 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
5152 0, request, &response);
5154 DEBUG_RTSP (src, &response);
5156 res = gst_rtspsrc_connection_send (src, conninfo, &response, NULL);
5160 gst_rtsp_message_unset (&response);
5161 } else if (res == GST_RTSP_EEOF)
5169 gst_rtsp_message_unset (&response);
5174 /* send server keep-alive */
5175 static GstRTSPResult
5176 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
5178 GstRTSPMessage request = { 0 };
5180 GstRTSPMethod method;
5181 const gchar *control;
5183 if (src->do_rtsp_keep_alive == FALSE) {
5184 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
5185 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
5189 GST_DEBUG_OBJECT (src, "creating server keep-alive");
5191 /* find a method to use for keep-alive */
5192 if (src->methods & GST_RTSP_GET_PARAMETER)
5193 method = GST_RTSP_GET_PARAMETER;
5195 method = GST_RTSP_OPTIONS;
5197 control = get_aggregate_control (src);
5198 if (control == NULL)
5201 res = gst_rtspsrc_init_request (src, &request, method, control);
5205 request.type_data.request.version = src->version;
5207 res = gst_rtspsrc_connection_send (src, &src->conninfo, &request, NULL);
5211 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
5212 gst_rtsp_message_unset (&request);
5219 GST_WARNING_OBJECT (src, "no control url to send keepalive");
5224 gchar *str = gst_rtsp_strresult (res);
5226 gst_rtsp_message_unset (&request);
5227 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
5228 ("Could not send keep-alive. (%s)", str));
5234 static GstFlowReturn
5235 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
5237 GstFlowReturn ret = GST_FLOW_OK;
5239 GstRTSPStream *stream;
5240 GstPad *outpad = NULL;
5246 channel = message->type_data.data.channel;
5248 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
5250 goto unknown_stream;
5252 if (channel == stream->channel[0]) {
5253 outpad = stream->channelpad[0];
5255 } else if (channel == stream->channel[1]) {
5256 outpad = stream->channelpad[1];
5262 /* take a look at the body to figure out what we have */
5263 gst_rtsp_message_get_body (message, &data, &size);
5265 goto invalid_length;
5267 /* channels are not correct on some servers, do extra check */
5268 if (data[1] >= 200 && data[1] <= 204) {
5269 /* hmm RTCP message switch to the RTCP pad of the same stream. */
5270 outpad = stream->channelpad[1];
5274 /* we have no clue what this is, just ignore then. */
5276 goto unknown_stream;
5278 /* take the message body for further processing */
5279 gst_rtsp_message_steal_body (message, &data, &size);
5281 /* strip the trailing \0 */
5284 buf = gst_buffer_new ();
5285 gst_buffer_append_memory (buf,
5286 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
5288 /* don't need message anymore */
5289 gst_rtsp_message_unset (message);
5291 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
5294 if (src->need_activate) {
5300 guint group_id = gst_util_group_id_next ();
5302 /* generate an SHA256 sum of the URI */
5303 cs = g_checksum_new (G_CHECKSUM_SHA256);
5304 uri = src->conninfo.location;
5305 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
5307 for (streams = src->streams; streams; streams = g_list_next (streams)) {
5308 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
5312 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
5313 event = gst_event_new_stream_start (stream_id);
5314 gst_event_set_group_id (event, group_id);
5317 gst_rtspsrc_stream_push_event (src, ostream, event);
5319 if ((caps = stream_get_caps_for_pt (ostream, ostream->default_pt))) {
5320 /* only streams that have a connection to the outside world */
5321 if (ostream->setup) {
5322 if (ostream->udpsrc[0]) {
5323 gst_element_send_event (ostream->udpsrc[0],
5324 gst_event_new_caps (caps));
5325 } else if (ostream->channelpad[0]) {
5326 if (GST_PAD_IS_SRC (ostream->channelpad[0]))
5327 gst_pad_push_event (ostream->channelpad[0],
5328 gst_event_new_caps (caps));
5330 gst_pad_send_event (ostream->channelpad[0],
5331 gst_event_new_caps (caps));
5333 ostream->need_caps = FALSE;
5335 if (ostream->profile == GST_RTSP_PROFILE_SAVP ||
5336 ostream->profile == GST_RTSP_PROFILE_SAVPF)
5337 caps = gst_caps_new_empty_simple ("application/x-srtcp");
5339 caps = gst_caps_new_empty_simple ("application/x-rtcp");
5341 if (ostream->udpsrc[1]) {
5342 gst_element_send_event (ostream->udpsrc[1],
5343 gst_event_new_caps (caps));
5344 } else if (ostream->channelpad[1]) {
5345 if (GST_PAD_IS_SRC (ostream->channelpad[1]))
5346 gst_pad_push_event (ostream->channelpad[1],
5347 gst_event_new_caps (caps));
5349 gst_pad_send_event (ostream->channelpad[1],
5350 gst_event_new_caps (caps));
5353 gst_caps_unref (caps);
5357 g_checksum_free (cs);
5359 gst_rtspsrc_activate_streams (src);
5360 src->need_activate = FALSE;
5361 src->need_segment = TRUE;
5364 if (src->base_time == -1) {
5365 /* Take current running_time. This timestamp will be put on
5366 * the first buffer of each stream because we are a live source and so we
5367 * timestamp with the running_time. When we are dealing with TCP, we also
5368 * only timestamp the first buffer (using the DISCONT flag) because a server
5369 * typically bursts data, for which we don't want to compensate by speeding
5370 * up the media. The other timestamps will be interpollated from this one
5371 * using the RTP timestamps. */
5372 GST_OBJECT_LOCK (src);
5373 if (GST_ELEMENT_CLOCK (src)) {
5375 GstClockTime base_time;
5377 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
5378 base_time = GST_ELEMENT_CAST (src)->base_time;
5380 src->base_time = now - base_time;
5382 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
5383 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
5385 GST_OBJECT_UNLOCK (src);
5388 /* If needed send a new segment, don't forget we are live and buffer are
5389 * timestamped with running time */
5390 if (src->need_segment) {
5392 src->need_segment = FALSE;
5393 gst_segment_init (&segment, GST_FORMAT_TIME);
5394 gst_rtspsrc_push_event (src, gst_event_new_segment (&segment));
5397 if (stream->need_caps) {
5400 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt))) {
5401 /* only streams that have a connection to the outside world */
5402 if (stream->setup) {
5403 /* Only need to update the TCP caps here, UDP is already handled */
5404 if (stream->channelpad[0]) {
5405 if (GST_PAD_IS_SRC (stream->channelpad[0]))
5406 gst_pad_push_event (stream->channelpad[0],
5407 gst_event_new_caps (caps));
5409 gst_pad_send_event (stream->channelpad[0],
5410 gst_event_new_caps (caps));
5412 stream->need_caps = FALSE;
5416 stream->need_caps = FALSE;
5419 if (stream->discont && !is_rtcp) {
5420 /* mark first RTP buffer as discont */
5421 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
5422 stream->discont = FALSE;
5423 /* first buffer gets the timestamp, other buffers are not timestamped and
5424 * their presentation time will be interpollated from the rtp timestamps. */
5425 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
5426 GST_TIME_ARGS (src->base_time));
5428 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
5431 /* chain to the peer pad */
5432 if (GST_PAD_IS_SINK (outpad))
5433 ret = gst_pad_chain (outpad, buf);
5435 ret = gst_pad_push (outpad, buf);
5438 /* combine all stream flows for the data transport */
5439 ret = gst_rtspsrc_combine_flows (src, stream, ret);
5446 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
5447 gst_rtsp_message_unset (message);
5452 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5453 ("Short message received, ignoring."));
5454 gst_rtsp_message_unset (message);
5459 static GstFlowReturn
5460 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
5462 GstRTSPMessage message = { 0 };
5464 GstFlowReturn ret = GST_FLOW_OK;
5465 GTimeVal tv_timeout;
5468 /* get the next timeout interval */
5469 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
5471 /* see if the timeout period expired */
5472 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
5473 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
5474 /* send keep-alive, only act on interrupt, a warning will be posted for
5476 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5478 /* get new timeout */
5479 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
5482 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
5483 tv_timeout.tv_sec, tv_timeout.tv_usec);
5485 /* protect the connection with the connection lock so that we can see when
5486 * we are finished doing server communication */
5488 gst_rtspsrc_connection_receive (src, &src->conninfo,
5489 &message, src->ptcp_timeout);
5493 GST_DEBUG_OBJECT (src, "we received a server message");
5495 case GST_RTSP_EINTR:
5496 /* we got interrupted this means we need to stop */
5498 case GST_RTSP_ETIMEOUT:
5499 /* no reply, send keep alive */
5500 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
5501 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5505 /* go EOS when the server closed the connection */
5511 switch (message.type) {
5512 case GST_RTSP_MESSAGE_REQUEST:
5513 /* server sends us a request message, handle it */
5514 res = gst_rtspsrc_handle_request (src, &src->conninfo, &message);
5515 if (res == GST_RTSP_EEOF)
5518 goto handle_request_failed;
5520 case GST_RTSP_MESSAGE_RESPONSE:
5521 /* we ignore response messages */
5522 GST_DEBUG_OBJECT (src, "ignoring response message");
5523 DEBUG_RTSP (src, &message);
5525 case GST_RTSP_MESSAGE_DATA:
5526 GST_DEBUG_OBJECT (src, "got data message");
5527 ret = gst_rtspsrc_handle_data (src, &message);
5528 if (ret != GST_FLOW_OK)
5529 goto handle_data_failed;
5532 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5537 g_assert_not_reached ();
5542 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5543 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5544 ("The server closed the connection."));
5545 src->conninfo.connected = FALSE;
5546 gst_rtsp_message_unset (&message);
5547 return GST_FLOW_EOS;
5551 gst_rtsp_message_unset (&message);
5552 GST_DEBUG_OBJECT (src, "got interrupted");
5553 return GST_FLOW_FLUSHING;
5557 gchar *str = gst_rtsp_strresult (res);
5559 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5560 ("Could not receive message. (%s)", str));
5563 gst_rtsp_message_unset (&message);
5564 return GST_FLOW_ERROR;
5566 handle_request_failed:
5568 gchar *str = gst_rtsp_strresult (res);
5570 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5571 ("Could not handle server message. (%s)", str));
5573 gst_rtsp_message_unset (&message);
5574 return GST_FLOW_ERROR;
5578 GST_DEBUG_OBJECT (src, "could no handle data message");
5583 static GstFlowReturn
5584 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
5587 GstRTSPMessage message = { 0 };
5591 GTimeVal tv_timeout;
5593 /* get the next timeout interval */
5594 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
5596 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
5597 (gint) tv_timeout.tv_sec);
5599 gst_rtsp_message_unset (&message);
5601 /* we should continue reading the TCP socket because the server might
5602 * send us requests. When the session timeout expires, we need to send a
5603 * keep-alive request to keep the session open. */
5604 res = gst_rtspsrc_connection_receive (src, &src->conninfo,
5605 &message, &tv_timeout);
5609 GST_DEBUG_OBJECT (src, "we received a server message");
5611 case GST_RTSP_EINTR:
5612 /* we got interrupted, see what we have to do */
5614 case GST_RTSP_ETIMEOUT:
5615 /* send keep-alive, ignore the result, a warning will be posted. */
5616 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
5617 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5621 /* server closed the connection. not very fatal for UDP, reconnect and
5622 * see what happens. */
5623 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5624 ("The server closed the connection."));
5625 if (src->udp_reconnect) {
5627 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
5634 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
5636 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5637 ("Unhandled return value %d.", res));
5641 switch (message.type) {
5642 case GST_RTSP_MESSAGE_REQUEST:
5643 /* server sends us a request message, handle it */
5644 res = gst_rtspsrc_handle_request (src, &src->conninfo, &message);
5645 if (res == GST_RTSP_EEOF)
5648 goto handle_request_failed;
5650 case GST_RTSP_MESSAGE_RESPONSE:
5651 /* we ignore response and data messages */
5652 GST_DEBUG_OBJECT (src, "ignoring response message");
5653 DEBUG_RTSP (src, &message);
5654 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
5655 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
5656 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
5657 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
5658 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5665 case GST_RTSP_MESSAGE_DATA:
5666 /* we ignore response and data messages */
5667 GST_DEBUG_OBJECT (src, "ignoring data message");
5670 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5675 g_assert_not_reached ();
5677 /* we get here when the connection got interrupted */
5680 gst_rtsp_message_unset (&message);
5681 GST_DEBUG_OBJECT (src, "got interrupted");
5682 return GST_FLOW_FLUSHING;
5686 gchar *str = gst_rtsp_strresult (res);
5689 src->conninfo.connected = FALSE;
5690 if (res != GST_RTSP_EINTR) {
5691 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
5692 ("Could not connect to server. (%s)", str));
5694 ret = GST_FLOW_ERROR;
5696 ret = GST_FLOW_FLUSHING;
5702 gchar *str = gst_rtsp_strresult (res);
5704 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5705 ("Could not receive message. (%s)", str));
5707 return GST_FLOW_ERROR;
5709 handle_request_failed:
5711 gchar *str = gst_rtsp_strresult (res);
5714 gst_rtsp_message_unset (&message);
5715 if (res != GST_RTSP_EINTR) {
5716 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5717 ("Could not handle server message. (%s)", str));
5719 ret = GST_FLOW_ERROR;
5721 ret = GST_FLOW_FLUSHING;
5727 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5728 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5729 ("The server closed the connection."));
5730 src->conninfo.connected = FALSE;
5731 gst_rtsp_message_unset (&message);
5732 return GST_FLOW_EOS;
5736 static GstRTSPResult
5737 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
5739 GstRTSPResult res = GST_RTSP_OK;
5742 GST_DEBUG_OBJECT (src, "doing reconnect");
5744 GST_OBJECT_LOCK (src);
5745 /* only restart when the pads were not yet activated, else we were
5746 * streaming over UDP */
5747 restart = src->need_activate;
5748 GST_OBJECT_UNLOCK (src);
5750 /* no need to restart, we're done */
5754 /* we can try only TCP now */
5755 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
5757 /* close and cleanup our state */
5758 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
5761 /* see if we have TCP left to try. Also don't try TCP when we were configured
5763 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
5766 /* We post a warning message now to inform the user
5767 * that nothing happened. It's most likely a firewall thing. */
5768 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5769 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5770 "firewall is blocking it. Retrying using a tcp connection.",
5771 gst_guint64_to_gdouble (src->udp_timeout) / 1000000.0));
5773 /* open new connection using tcp */
5774 if (gst_rtspsrc_open (src, async) < 0)
5777 /* start playback */
5778 if (gst_rtspsrc_play (src, &src->segment, async, NULL) < 0)
5787 src->cur_protocols = 0;
5788 /* no transport possible, post an error and stop */
5789 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5790 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5791 "firewall is blocking it. No other protocols to try.",
5792 gst_guint64_to_gdouble (src->udp_timeout) / 1000000.0));
5793 return GST_RTSP_ERROR;
5797 GST_DEBUG_OBJECT (src, "open failed");
5802 GST_DEBUG_OBJECT (src, "play failed");
5808 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
5812 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
5815 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
5818 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
5820 case CMD_GET_PARAMETER:
5821 GST_ELEMENT_PROGRESS (src, START, "request",
5822 ("Sending GET_PARAMETER request"));
5824 case CMD_SET_PARAMETER:
5825 GST_ELEMENT_PROGRESS (src, START, "request",
5826 ("Sending SET_PARAMETER request"));
5829 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
5837 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
5841 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
5844 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
5847 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
5849 case CMD_GET_PARAMETER:
5850 GST_ELEMENT_PROGRESS (src, COMPLETE, "request",
5851 ("Sent GET_PARAMETER request"));
5853 case CMD_SET_PARAMETER:
5854 GST_ELEMENT_PROGRESS (src, COMPLETE, "request",
5855 ("Sent SET_PARAMETER request"));
5858 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
5866 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
5870 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
5873 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
5876 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
5878 case CMD_GET_PARAMETER:
5879 GST_ELEMENT_PROGRESS (src, CANCELED, "request",
5880 ("GET_PARAMETER canceled"));
5882 case CMD_SET_PARAMETER:
5883 GST_ELEMENT_PROGRESS (src, CANCELED, "request",
5884 ("SET_PARAMETER canceled"));
5887 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
5895 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
5899 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
5902 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
5905 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
5907 case CMD_GET_PARAMETER:
5908 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("GET_PARAMETER failed"));
5910 case CMD_SET_PARAMETER:
5911 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("SET_PARAMETER failed"));
5914 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
5922 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
5924 if (ret == GST_RTSP_OK)
5925 gst_rtspsrc_loop_complete_cmd (src, cmd);
5926 else if (ret == GST_RTSP_EINTR)
5927 gst_rtspsrc_loop_cancel_cmd (src, cmd);
5929 gst_rtspsrc_loop_error_cmd (src, cmd);
5933 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
5936 gboolean flushed = FALSE;
5938 /* start new request */
5939 gst_rtspsrc_loop_start_cmd (src, cmd);
5941 GST_DEBUG_OBJECT (src, "sending cmd %s", cmd_to_string (cmd));
5943 GST_OBJECT_LOCK (src);
5944 old = src->pending_cmd;
5946 if (old == CMD_RECONNECT) {
5947 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
5948 cmd = CMD_RECONNECT;
5949 } else if (old == CMD_CLOSE) {
5950 /* our CMD_CLOSE might have interrutped CMD_LOOP. gst_rtspsrc_loop
5951 * will send a CMD_WAIT which would cancel our pending CMD_CLOSE (if
5952 * still pending). We just avoid it here by making sure CMD_CLOSE is
5953 * still the pending command. */
5954 GST_DEBUG_OBJECT (src, "ignore, we were closing");
5956 } else if (old == CMD_SET_PARAMETER) {
5957 GST_DEBUG_OBJECT (src, "ignore, we have a pending %s", cmd_to_string (old));
5958 cmd = CMD_SET_PARAMETER;
5959 } else if (old == CMD_GET_PARAMETER) {
5960 GST_DEBUG_OBJECT (src, "ignore, we have a pending %s", cmd_to_string (old));
5961 cmd = CMD_GET_PARAMETER;
5962 } else if (old != CMD_WAIT) {
5963 src->pending_cmd = CMD_WAIT;
5964 GST_OBJECT_UNLOCK (src);
5965 /* cancel previous request */
5966 GST_DEBUG_OBJECT (src, "cancel previous request %s", cmd_to_string (old));
5967 gst_rtspsrc_loop_cancel_cmd (src, old);
5968 GST_OBJECT_LOCK (src);
5970 src->pending_cmd = cmd;
5971 /* interrupt if allowed */
5972 if (src->busy_cmd & mask) {
5973 GST_DEBUG_OBJECT (src, "connection flush busy %s",
5974 cmd_to_string (src->busy_cmd));
5975 gst_rtspsrc_connection_flush (src, TRUE);
5978 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %s",
5979 cmd_to_string (src->busy_cmd));
5982 gst_task_start (src->task);
5983 GST_OBJECT_UNLOCK (src);
5989 gst_rtspsrc_loop_send_cmd_and_wait (GstRTSPSrc * src, gint cmd, gint mask,
5990 GstClockTime timeout)
5992 gboolean flushed = gst_rtspsrc_loop_send_cmd (src, cmd, mask);
5995 gint64 end_time = g_get_monotonic_time () + (timeout / 1000);
5996 GST_OBJECT_LOCK (src);
5997 while (src->pending_cmd == cmd || src->busy_cmd == cmd) {
5998 if (!g_cond_wait_until (&src->cmd_cond, GST_OBJECT_GET_LOCK (src),
6000 GST_WARNING_OBJECT (src,
6001 "Timed out waiting for TEARDOWN to be processed.");
6002 break; /* timeout passed */
6005 GST_OBJECT_UNLOCK (src);
6011 gst_rtspsrc_loop (GstRTSPSrc * src)
6015 if (!src->conninfo.connection || !src->conninfo.connected)
6018 if (src->interleaved)
6019 ret = gst_rtspsrc_loop_interleaved (src);
6021 ret = gst_rtspsrc_loop_udp (src);
6023 if (ret != GST_FLOW_OK)
6031 GST_WARNING_OBJECT (src, "we are not connected");
6032 ret = GST_FLOW_FLUSHING;
6037 const gchar *reason = gst_flow_get_name (ret);
6039 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
6040 src->running = FALSE;
6041 if (ret == GST_FLOW_EOS) {
6042 /* perform EOS logic */
6043 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
6044 gst_element_post_message (GST_ELEMENT_CAST (src),
6045 gst_message_new_segment_done (GST_OBJECT_CAST (src),
6046 src->segment.format, src->segment.position));
6047 gst_rtspsrc_push_event (src,
6048 gst_event_new_segment_done (src->segment.format,
6049 src->segment.position));
6051 gst_rtspsrc_push_event (src, gst_event_new_eos ());
6053 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
6054 /* for fatal errors we post an error message, post the error before the
6055 * EOS so the app knows about the error first. */
6056 GST_ELEMENT_FLOW_ERROR (src, ret);
6057 gst_rtspsrc_push_event (src, gst_event_new_eos ());
6059 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
6064 #ifndef GST_DISABLE_GST_DEBUG
6065 static const gchar *
6066 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
6070 while (method != 0) {
6087 /* Parse a WWW-Authenticate Response header and determine the
6088 * available authentication methods
6090 * This code should also cope with the fact that each WWW-Authenticate
6091 * header can contain multiple challenge methods + tokens
6093 * At the moment, for Basic auth, we just do a minimal check and don't
6094 * even parse out the realm */
6096 gst_rtspsrc_parse_auth_hdr (GstRTSPMessage * response,
6097 GstRTSPAuthMethod * methods, GstRTSPConnection * conn, gboolean * stale)
6099 GstRTSPAuthCredential **credentials, **credential;
6101 g_return_if_fail (response != NULL);
6102 g_return_if_fail (methods != NULL);
6103 g_return_if_fail (stale != NULL);
6106 gst_rtsp_message_parse_auth_credentials (response,
6107 GST_RTSP_HDR_WWW_AUTHENTICATE);
6111 credential = credentials;
6112 while (*credential) {
6113 if ((*credential)->scheme == GST_RTSP_AUTH_BASIC) {
6114 *methods |= GST_RTSP_AUTH_BASIC;
6115 } else if ((*credential)->scheme == GST_RTSP_AUTH_DIGEST) {
6116 GstRTSPAuthParam **param = (*credential)->params;
6118 *methods |= GST_RTSP_AUTH_DIGEST;
6120 gst_rtsp_connection_clear_auth_params (conn);
6124 if (strcmp ((*param)->name, "stale") == 0
6125 && g_ascii_strcasecmp ((*param)->value, "TRUE") == 0)
6127 gst_rtsp_connection_set_auth_param (conn, (*param)->name,
6136 gst_rtsp_auth_credentials_free (credentials);
6140 * gst_rtspsrc_setup_auth:
6141 * @src: the rtsp source
6143 * Configure a username and password and auth method on the
6144 * connection object based on a response we received from the
6147 * Currently, this requires that a username and password were supplied
6148 * in the uri. In the future, they may be requested on demand by sending
6149 * a message up the bus.
6151 * Returns: TRUE if authentication information could be set up correctly.
6154 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
6158 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
6159 GstRTSPAuthMethod method;
6160 GstRTSPResult auth_result;
6162 GstRTSPConnection *conn;
6163 gboolean stale = FALSE;
6165 conn = src->conninfo.connection;
6167 /* Identify the available auth methods and see if any are supported */
6168 gst_rtspsrc_parse_auth_hdr (response, &avail_methods, conn, &stale);
6170 if (avail_methods == GST_RTSP_AUTH_NONE)
6171 goto no_auth_available;
6173 /* For digest auth, if the response indicates that the session
6174 * data are stale, we just update them in the connection object and
6175 * return TRUE to retry the request */
6177 src->tried_url_auth = FALSE;
6179 url = gst_rtsp_connection_get_url (conn);
6181 /* Do we have username and password available? */
6182 if (url != NULL && !src->tried_url_auth && url->user != NULL
6183 && url->passwd != NULL) {
6186 src->tried_url_auth = TRUE;
6187 GST_DEBUG_OBJECT (src,
6188 "Attempting authentication using credentials from the URL");
6190 user = src->user_id;
6191 pass = src->user_pw;
6192 GST_DEBUG_OBJECT (src,
6193 "Attempting authentication using credentials from the properties");
6196 /* FIXME: If the url didn't contain username and password or we tried them
6197 * already, request a username and passwd from the application via some kind
6198 * of credentials request message */
6200 /* If we don't have a username and passwd at this point, bail out. */
6201 if (user == NULL || pass == NULL)
6204 /* Try to configure for each available authentication method, strongest to
6206 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
6207 /* Check if this method is available on the server */
6208 if ((method & avail_methods) == 0)
6211 /* Pass the credentials to the connection to try on the next request */
6212 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
6213 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
6214 * ignore it and end up retrying later */
6215 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
6216 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
6217 gst_rtsp_auth_method_to_string (method));
6222 if (method == GST_RTSP_AUTH_NONE)
6223 goto no_auth_available;
6229 /* Output an error indicating that we couldn't connect because there were
6230 * no supported authentication protocols */
6231 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
6232 ("No supported authentication protocol was found"));
6237 /* We don't fire an error message, we just return FALSE and let the
6238 * normal NOT_AUTHORIZED error be propagated */
6243 static GstRTSPResult
6244 gst_rtsp_src_receive_response (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
6245 GstRTSPMessage * response, GstRTSPStatusCode * code)
6247 GstRTSPStatusCode thecode;
6248 gchar *content_base = NULL;
6249 GstRTSPResult res = gst_rtspsrc_connection_receive (src, conninfo,
6250 response, src->ptcp_timeout);
6255 DEBUG_RTSP (src, response);
6257 switch (response->type) {
6258 case GST_RTSP_MESSAGE_REQUEST:
6259 res = gst_rtspsrc_handle_request (src, conninfo, response);
6260 if (res == GST_RTSP_EEOF)
6263 goto handle_request_failed;
6265 /* Not a response, receive next message */
6266 return gst_rtsp_src_receive_response (src, conninfo, response, code);
6267 case GST_RTSP_MESSAGE_RESPONSE:
6268 /* ok, a response is good */
6269 GST_DEBUG_OBJECT (src, "received response message");
6271 case GST_RTSP_MESSAGE_DATA:
6272 /* get next response */
6273 GST_DEBUG_OBJECT (src, "handle data response message");
6274 gst_rtspsrc_handle_data (src, response);
6276 /* Not a response, receive next message */
6277 return gst_rtsp_src_receive_response (src, conninfo, response, code);
6279 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
6282 /* Not a response, receive next message */
6283 return gst_rtsp_src_receive_response (src, conninfo, response, code);
6286 thecode = response->type_data.response.code;
6288 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
6290 /* if the caller wanted the result code, we store it. */
6294 /* If the request didn't succeed, bail out before doing any more */
6295 if (thecode != GST_RTSP_STS_OK)
6298 /* store new content base if any */
6299 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
6302 g_free (src->content_base);
6303 src->content_base = g_strdup (content_base);
6313 return GST_RTSP_EEOF;
6316 gchar *str = gst_rtsp_strresult (res);
6318 if (res != GST_RTSP_EINTR) {
6319 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6320 ("Could not receive message. (%s)", str));
6322 GST_WARNING_OBJECT (src, "receive interrupted");
6330 handle_request_failed:
6332 /* ERROR was posted */
6333 gst_rtsp_message_unset (response);
6338 GST_DEBUG_OBJECT (src, "we got an eof from the server");
6339 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
6340 ("The server closed the connection."));
6341 gst_rtsp_message_unset (response);
6347 static GstRTSPResult
6348 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
6349 GstRTSPMessage * request, GstRTSPMessage * response,
6350 GstRTSPStatusCode * code)
6354 gboolean allow_send = TRUE;
6357 if (!src->short_header)
6358 gst_rtsp_ext_list_before_send (src->extensions, request);
6360 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_BEFORE_SEND], 0,
6361 request, &allow_send);
6363 GST_DEBUG_OBJECT (src, "skipping message, disabled by signal");
6367 GST_DEBUG_OBJECT (src, "sending message");
6369 DEBUG_RTSP (src, request);
6371 res = gst_rtspsrc_connection_send (src, conninfo, request, src->ptcp_timeout);
6375 gst_rtsp_connection_reset_timeout (conninfo->connection);
6379 res = gst_rtsp_src_receive_response (src, conninfo, response, code);
6380 if (res == GST_RTSP_EEOF) {
6381 GST_WARNING_OBJECT (src, "server closed connection");
6382 /* only try once after reconnect, then fallthrough and error out */
6383 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
6385 /* if reconnect succeeds, try again */
6386 if ((res = gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) == 0)
6390 gst_rtsp_ext_list_after_send (src->extensions, request, response);
6396 gchar *str = gst_rtsp_strresult (res);
6398 if (res != GST_RTSP_EINTR) {
6399 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6400 ("Could not send message. (%s)", str));
6402 GST_WARNING_OBJECT (src, "send interrupted");
6411 * @src: the rtsp source
6412 * @conninfo: the connection information to send on
6413 * @request: must point to a valid request
6414 * @response: must point to an empty #GstRTSPMessage
6415 * @code: an optional code result
6416 * @versions: List of versions to try, setting it back onto the @request message
6417 * if not set, `src->version` will be used as RTSP version.
6419 * send @request and retrieve the response in @response. optionally @code can be
6420 * non-NULL in which case it will contain the status code of the response.
6422 * If This function returns #GST_RTSP_OK, @response will contain a valid response
6423 * message that should be cleaned with gst_rtsp_message_unset() after usage.
6425 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
6426 * @response message) if the response code was not 200 (OK).
6428 * If the attempt results in an authentication failure, then this will attempt
6429 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
6432 * Returns: #GST_RTSP_OK if the processing was successful.
6434 static GstRTSPResult
6435 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
6436 GstRTSPMessage * request, GstRTSPMessage * response,
6437 GstRTSPStatusCode * code, GstRTSPVersion * versions)
6439 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
6440 GstRTSPResult res = GST_RTSP_ERROR;
6443 GstRTSPMethod method = GST_RTSP_INVALID;
6444 gint version_retry = 0;
6450 /* make sure we don't loop forever */
6454 /* save method so we can disable it when the server complains */
6455 method = request->type_data.request.method;
6458 request->type_data.request.version = src->version;
6461 gst_rtspsrc_try_send (src, conninfo, request, response,
6466 case GST_RTSP_STS_UNAUTHORIZED:
6467 case GST_RTSP_STS_NOT_FOUND:
6468 if (gst_rtspsrc_setup_auth (src, response)) {
6469 /* Try the request/response again after configuring the auth info
6474 case GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED:
6475 GST_INFO_OBJECT (src, "Version %s not supported by the server",
6476 versions ? gst_rtsp_version_as_text (versions[version_retry]) :
6478 if (versions && versions[version_retry] != GST_RTSP_VERSION_INVALID) {
6479 GST_INFO_OBJECT (src, "Unsupported version %s => trying %s",
6480 gst_rtsp_version_as_text (request->type_data.request.version),
6481 gst_rtsp_version_as_text (versions[version_retry]));
6482 request->type_data.request.version = versions[version_retry];
6491 } while (retry == TRUE);
6493 /* If the user requested the code, let them handle errors, otherwise
6494 * post an error below */
6497 else if (int_code != GST_RTSP_STS_OK)
6498 goto error_response;
6505 GST_DEBUG_OBJECT (src, "got error %d", res);
6510 res = GST_RTSP_ERROR;
6512 switch (response->type_data.response.code) {
6513 case GST_RTSP_STS_NOT_FOUND:
6514 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, NOT_FOUND,
6517 case GST_RTSP_STS_UNAUTHORIZED:
6518 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, NOT_AUTHORIZED,
6521 case GST_RTSP_STS_MOVED_PERMANENTLY:
6522 case GST_RTSP_STS_MOVE_TEMPORARILY:
6524 gchar *new_location;
6525 GstRTSPLowerTrans transports;
6527 GST_DEBUG_OBJECT (src, "got redirection");
6528 /* if we don't have a Location Header, we must error */
6529 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
6530 &new_location, 0) < 0)
6533 /* When we receive a redirect result, we go back to the INIT state after
6534 * parsing the new URI. The caller should do the needed steps to issue
6535 * a new setup when it detects this state change. */
6536 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
6538 /* save current transports */
6539 if (src->conninfo.url)
6540 transports = src->conninfo.url->transports;
6542 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
6544 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
6546 /* set old transports */
6547 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
6548 src->conninfo.url->transports = transports;
6550 src->need_redirect = TRUE;
6554 case GST_RTSP_STS_NOT_ACCEPTABLE:
6555 case GST_RTSP_STS_NOT_IMPLEMENTED:
6556 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
6557 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
6558 gst_rtsp_method_as_text (method));
6559 src->methods &= ~method;
6563 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, READ,
6567 /* if we return ERROR we should unset the response ourselves */
6568 if (res == GST_RTSP_ERROR)
6569 gst_rtsp_message_unset (response);
6575 static GstRTSPResult
6576 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
6577 GstRTSPMessage * response, GstRTSPSrc * src)
6579 return gst_rtspsrc_send (src, &src->conninfo, request, response, NULL, NULL);
6583 /* parse the response and collect all the supported methods. We need this
6584 * information so that we don't try to send an unsupported request to the
6588 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
6590 GstRTSPHeaderField field;
6594 /* reset supported methods */
6597 /* Try Allow Header first */
6598 field = GST_RTSP_HDR_ALLOW;
6601 gst_rtsp_message_get_header (response, field, &respoptions, indx);
6605 src->methods |= gst_rtsp_options_from_text (respoptions);
6611 field = GST_RTSP_HDR_PUBLIC;
6614 gst_rtsp_message_get_header (response, field, &respoptions, indx);
6618 src->methods |= gst_rtsp_options_from_text (respoptions);
6623 if (src->methods == 0) {
6624 /* neither Allow nor Public are required, assume the server supports
6625 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
6627 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
6628 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
6630 /* always assume PLAY, FIXME, extensions should be able to override
6632 src->methods |= GST_RTSP_PLAY;
6633 /* also assume it will support Range */
6634 src->seekable = G_MAXFLOAT;
6636 /* we need describe and setup */
6637 if (!(src->methods & GST_RTSP_DESCRIBE))
6639 if (!(src->methods & GST_RTSP_SETUP))
6647 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
6648 ("Server does not support DESCRIBE."));
6653 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
6654 ("Server does not support SETUP."));
6659 /* masks to be kept in sync with the hardcoded protocol order of preference
6661 static const guint protocol_masks[] = {
6662 GST_RTSP_LOWER_TRANS_UDP,
6663 GST_RTSP_LOWER_TRANS_UDP_MCAST,
6664 GST_RTSP_LOWER_TRANS_TCP,
6668 static GstRTSPResult
6669 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
6670 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
6674 gboolean add_udp_str;
6679 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
6684 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
6686 /* extension listed transports, use those */
6687 if (*transports != NULL)
6690 /* it's the default */
6691 add_udp_str = FALSE;
6693 /* the default RTSP transports */
6694 result = g_string_new ("RTP");
6697 case GST_RTSP_PROFILE_AVP:
6698 g_string_append (result, "/AVP");
6700 case GST_RTSP_PROFILE_SAVP:
6701 g_string_append (result, "/SAVP");
6703 case GST_RTSP_PROFILE_AVPF:
6704 g_string_append (result, "/AVPF");
6706 case GST_RTSP_PROFILE_SAVPF:
6707 g_string_append (result, "/SAVPF");
6713 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
6714 GST_DEBUG_OBJECT (src, "adding UDP unicast");
6716 g_string_append (result, "/UDP");
6717 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
6718 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
6719 GST_DEBUG_OBJECT (src, "adding UDP multicast");
6720 /* we don't have to allocate any UDP ports yet, if the selected transport
6721 * turns out to be multicast we can create them and join the multicast
6722 * group indicated in the transport reply */
6724 g_string_append (result, "/UDP");
6725 g_string_append (result, ";multicast");
6726 if (src->next_port_num != 0) {
6727 if (src->client_port_range.max > 0 &&
6728 src->next_port_num >= src->client_port_range.max)
6731 g_string_append_printf (result, ";client_port=%d-%d",
6732 src->next_port_num, src->next_port_num + 1);
6734 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
6735 GST_DEBUG_OBJECT (src, "adding TCP");
6737 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
6739 *transports = g_string_free (result, FALSE);
6741 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
6748 GST_ERROR ("extension gave error %d", res);
6753 GST_ERROR ("no more ports available");
6754 return GST_RTSP_ERROR;
6758 static GstRTSPResult
6759 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
6760 gint orig_rtpport, gint orig_rtcpport)
6763 gint nr_udp, nr_int;
6765 gint rtpport = 0, rtcpport = 0;
6768 src = stream->parent;
6770 /* find number of placeholders first */
6771 if (strstr (*transports, "%%i2"))
6773 else if (strstr (*transports, "%%i1"))
6778 if (strstr (*transports, "%%u2"))
6780 else if (strstr (*transports, "%%u1"))
6785 if (nr_udp == 0 && nr_int == 0)
6789 if (!orig_rtpport || !orig_rtcpport) {
6790 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
6793 rtpport = orig_rtpport;
6794 rtcpport = orig_rtcpport;
6798 str = g_string_new ("");
6800 while ((next = strstr (p, "%%"))) {
6801 g_string_append_len (str, p, next - p);
6802 if (next[2] == 'u') {
6804 g_string_append_printf (str, "%d", rtpport);
6805 else if (next[3] == '2')
6806 g_string_append_printf (str, "%d", rtcpport);
6808 if (next[2] == 'i') {
6810 g_string_append_printf (str, "%d", src->free_channel);
6811 else if (next[3] == '2')
6812 g_string_append_printf (str, "%d", src->free_channel + 1);
6818 if (src->version >= GST_RTSP_VERSION_2_0)
6819 src->free_channel += 2;
6821 /* append final part */
6822 g_string_append (str, p);
6824 g_free (*transports);
6825 *transports = g_string_free (str, FALSE);
6833 GST_ERROR ("failed to allocate udp ports");
6834 return GST_RTSP_ERROR;
6839 signal_get_srtcp_params (GstRTSPSrc * src, GstRTSPStream * stream)
6841 GstCaps *caps = NULL;
6843 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY], 0,
6847 GST_DEBUG_OBJECT (src, "SRTP parameters received");
6853 default_srtcp_params (void)
6860 guint data_size = GST_ROUND_UP_4 (KEY_SIZE);
6862 /* create a random key */
6863 key_data = g_malloc (data_size);
6864 for (i = 0; i < data_size; i += 4)
6865 GST_WRITE_UINT32_BE (key_data + i, g_random_int ());
6867 buf = gst_buffer_new_wrapped (key_data, KEY_SIZE);
6869 caps = gst_caps_new_simple ("application/x-srtcp",
6870 "srtp-key", GST_TYPE_BUFFER, buf,
6871 "srtp-cipher", G_TYPE_STRING, "aes-128-icm",
6872 "srtp-auth", G_TYPE_STRING, "hmac-sha1-80",
6873 "srtcp-cipher", G_TYPE_STRING, "aes-128-icm",
6874 "srtcp-auth", G_TYPE_STRING, "hmac-sha1-80", NULL);
6876 gst_buffer_unref (buf);
6882 gst_rtspsrc_stream_make_keymgmt (GstRTSPSrc * src, GstRTSPStream * stream)
6884 gchar *base64, *result = NULL;
6885 GstMIKEYMessage *mikey_msg;
6887 stream->srtcpparams = signal_get_srtcp_params (src, stream);
6888 if (stream->srtcpparams == NULL)
6889 stream->srtcpparams = default_srtcp_params ();
6891 mikey_msg = gst_mikey_message_new_from_caps (stream->srtcpparams);
6893 /* add policy '0' for our SSRC */
6894 gst_mikey_message_add_cs_srtp (mikey_msg, 0, stream->send_ssrc, 0);
6896 base64 = gst_mikey_message_base64_encode (mikey_msg);
6897 gst_mikey_message_unref (mikey_msg);
6900 result = gst_sdp_make_keymgmt (stream->conninfo.location, base64);
6908 static GstRTSPResult
6909 gst_rtsp_src_setup_stream_from_response (GstRTSPSrc * src,
6910 GstRTSPStream * stream, GstRTSPMessage * response,
6911 GstRTSPLowerTrans * protocols, gint retry, gint * rtpport, gint * rtcpport)
6913 gchar *resptrans = NULL;
6914 GstRTSPTransport transport = { 0 };
6916 gst_rtsp_message_get_header (response, GST_RTSP_HDR_TRANSPORT, &resptrans, 0);
6918 gst_rtspsrc_stream_free_udp (stream);
6922 /* parse transport, go to next stream on parse error */
6923 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
6924 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
6925 return GST_RTSP_ELAST;
6928 /* update allowed transports for other streams. once the transport of
6929 * one stream has been determined, we make sure that all other streams
6930 * are configured in the same way */
6931 switch (transport.lower_transport) {
6932 case GST_RTSP_LOWER_TRANS_TCP:
6933 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
6935 *protocols = GST_RTSP_LOWER_TRANS_TCP;
6936 src->interleaved = TRUE;
6937 if (src->version < GST_RTSP_VERSION_2_0) {
6938 /* update free channels */
6939 src->free_channel = MAX (transport.interleaved.min, src->free_channel);
6940 src->free_channel = MAX (transport.interleaved.max, src->free_channel);
6941 src->free_channel++;
6944 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
6945 /* only allow multicast for other streams */
6946 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
6948 *protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
6949 /* if the server selected our ports, increment our counters so that
6950 * we select a new port later */
6951 if (src->next_port_num == transport.port.min &&
6952 src->next_port_num + 1 == transport.port.max) {
6953 src->next_port_num += 2;
6956 case GST_RTSP_LOWER_TRANS_UDP:
6957 /* only allow unicast for other streams */
6958 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
6960 *protocols = GST_RTSP_LOWER_TRANS_UDP;
6963 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
6964 transport.lower_transport);
6968 if (!src->interleaved || !retry) {
6969 /* now configure the stream with the selected transport */
6970 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
6971 GST_DEBUG_OBJECT (src,
6972 "could not configure stream %p transport, skipping stream", stream);
6974 } else if (stream->udpsrc[0] && stream->udpsrc[1] && rtpport && rtcpport) {
6975 /* retain the first allocated UDP port pair */
6976 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", rtpport, NULL);
6977 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", rtcpport, NULL);
6980 /* we need to activate at least one stream when we detect activity */
6981 src->need_activate = TRUE;
6983 /* stream is setup now */
6984 stream->setup = TRUE;
6985 stream->waiting_setup_response = FALSE;
6987 if (src->version >= GST_RTSP_VERSION_2_0) {
6988 gchar *prop, *media_properties;
6992 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_MEDIA_PROPERTIES,
6993 &media_properties, 0) != GST_RTSP_OK) {
6994 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6995 ("Error: No MEDIA_PROPERTY header in a SETUP request in RTSP 2.0"
6996 " - this header is mandatory."));
6998 gst_rtsp_message_unset (response);
6999 return GST_RTSP_ERROR;
7002 props = g_strsplit (media_properties, ",", -2);
7003 for (i = 0; props[i]; i++) {
7006 while (*prop == ' ')
7009 if (strstr (prop, "Random-Access")) {
7010 gchar **random_seekable_val = g_strsplit (prop, "=", 2);
7012 if (!random_seekable_val[1])
7013 src->seekable = G_MAXFLOAT;
7015 src->seekable = g_ascii_strtod (random_seekable_val[1], NULL);
7017 g_strfreev (random_seekable_val);
7018 } else if (!g_strcmp0 (prop, "No-Seeking")) {
7019 src->seekable = -1.0;
7020 } else if (!g_strcmp0 (prop, "Beginning-Only")) {
7021 src->seekable = 0.0;
7029 /* clean up our transport struct */
7030 gst_rtsp_transport_init (&transport);
7031 /* clean up used RTSP messages */
7032 gst_rtsp_message_unset (response);
7038 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7039 ("Server did not select transport."));
7041 gst_rtsp_message_unset (response);
7042 return GST_RTSP_ERROR;
7046 static GstRTSPResult
7047 gst_rtspsrc_setup_streams_end (GstRTSPSrc * src, gboolean async)
7050 GstRTSPConnInfo *conninfo;
7052 g_assert (src->version >= GST_RTSP_VERSION_2_0);
7054 conninfo = &src->conninfo;
7055 for (tmp = src->streams; tmp; tmp = tmp->next) {
7056 GstRTSPStream *stream = (GstRTSPStream *) tmp->data;
7057 GstRTSPMessage response = { 0, };
7059 if (!stream->waiting_setup_response)
7062 if (!src->conninfo.connection)
7063 conninfo = &((GstRTSPStream *) tmp->data)->conninfo;
7065 gst_rtsp_src_receive_response (src, conninfo, &response, NULL);
7067 gst_rtsp_src_setup_stream_from_response (src, stream,
7068 &response, NULL, 0, NULL, NULL);
7074 /* Perform the SETUP request for all the streams.
7076 * We ask the server for a specific transport, which initially includes all the
7077 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
7078 * two local UDP ports that we send to the server.
7080 * Once the server replied with a transport, we configure the other streams
7081 * with the same transport.
7083 * In case setup request are not pipelined, this function will also configure the
7084 * stream for the selected transport, * which basically means creating the pipeline.
7085 * Otherwise, the first stream is setup right away from the reply and a
7086 * CMD_FINALIZE_SETUP command is set for the stream pipelines to happen on the
7087 * remaining streams from the RTSP thread.
7089 static GstRTSPResult
7090 gst_rtspsrc_setup_streams_start (GstRTSPSrc * src, gboolean async)
7093 GstRTSPResult res = GST_RTSP_ERROR;
7094 GstRTSPMessage request = { 0 };
7095 GstRTSPMessage response = { 0 };
7096 GstRTSPStream *stream = NULL;
7097 GstRTSPLowerTrans protocols;
7098 GstRTSPStatusCode code;
7099 gboolean unsupported_real = FALSE;
7100 gint rtpport, rtcpport;
7103 gchar *pipelined_request_id = NULL;
7105 if (src->conninfo.connection) {
7106 url = gst_rtsp_connection_get_url (src->conninfo.connection);
7107 /* we initially allow all configured lower transports. based on the URL
7108 * transports and the replies from the server we narrow them down. */
7109 protocols = url->transports & src->cur_protocols;
7112 protocols = src->cur_protocols;
7118 /* reset some state */
7119 src->free_channel = 0;
7120 src->interleaved = FALSE;
7121 src->need_activate = FALSE;
7122 /* keep track of next port number, 0 is random */
7123 src->next_port_num = src->client_port_range.min;
7124 rtpport = rtcpport = 0;
7126 if (G_UNLIKELY (src->streams == NULL))
7129 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7130 GstRTSPConnInfo *conninfo;
7137 stream = (GstRTSPStream *) walk->data;
7139 caps = stream_get_caps_for_pt (stream, stream->default_pt);
7141 GST_WARNING_OBJECT (src, "skipping stream %p, no caps", stream);
7145 if (stream->skipped) {
7146 GST_DEBUG_OBJECT (src, "skipping stream %p", stream);
7150 /* see if we need to configure this stream */
7151 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
7152 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
7157 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
7158 stream->id, caps, &selected);
7160 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
7164 /* merge/overwrite global caps */
7169 s = gst_caps_get_structure (caps, 0);
7171 num = gst_structure_n_fields (src->props);
7172 for (j = 0; j < num; j++) {
7176 name = gst_structure_nth_field_name (src->props, j);
7177 val = gst_structure_get_value (src->props, name);
7178 gst_structure_set_value (s, name, val);
7180 GST_DEBUG_OBJECT (src, "copied %s", name);
7184 /* skip setup if we have no URL for it */
7185 if (stream->conninfo.location == NULL) {
7186 GST_WARNING_OBJECT (src, "skipping stream %p, no setup", stream);
7190 if (src->conninfo.connection == NULL) {
7191 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
7192 GST_WARNING_OBJECT (src, "skipping stream %p, failed to connect",
7196 conninfo = &stream->conninfo;
7198 conninfo = &src->conninfo;
7200 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
7201 stream->conninfo.location);
7203 /* if we have a multicast connection, only suggest multicast from now on */
7204 if (stream->is_multicast)
7205 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
7208 /* first selectable protocol */
7209 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
7211 if (!protocol_masks[mask])
7215 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
7216 protocol_masks[mask]);
7217 /* create a string with first transport in line */
7219 res = gst_rtspsrc_create_transports_string (src,
7220 protocols & protocol_masks[mask], stream->profile, &transports);
7221 if (res < 0 || transports == NULL)
7222 goto setup_transport_failed;
7224 if (strlen (transports) == 0) {
7225 g_free (transports);
7226 GST_DEBUG_OBJECT (src, "no transports found");
7231 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
7233 /* replace placeholders with real values, this function will optionally
7234 * allocate UDP ports and other info needed to execute the setup request */
7235 res = gst_rtspsrc_prepare_transports (stream, &transports,
7236 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
7238 g_free (transports);
7239 goto setup_transport_failed;
7242 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
7243 /* create SETUP request */
7245 gst_rtspsrc_init_request (src, &request, GST_RTSP_SETUP,
7246 stream->conninfo.location);
7248 g_free (transports);
7249 goto create_request_failed;
7252 if (src->version >= GST_RTSP_VERSION_2_0) {
7253 if (!pipelined_request_id)
7254 pipelined_request_id = g_strdup_printf ("%d",
7255 g_random_int_range (0, G_MAXINT32));
7257 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_PIPELINED_REQUESTS,
7258 pipelined_request_id);
7259 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT_RANGES,
7260 "npt, clock, smpte, clock");
7263 /* select transport */
7264 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
7266 if (stream->is_backchannel && src->backchannel == BACKCHANNEL_ONVIF)
7267 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
7268 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
7271 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
7272 stream->profile == GST_RTSP_PROFILE_SAVPF) {
7273 hval = gst_rtspsrc_stream_make_keymgmt (src, stream);
7274 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
7277 /* if the user wants a non default RTP packet size we add the blocksize
7279 if (src->rtp_blocksize > 0) {
7280 hval = g_strdup_printf ("%d", src->rtp_blocksize);
7281 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
7285 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
7288 /* handle the code ourselves */
7290 gst_rtspsrc_send (src, conninfo, &request,
7291 pipelined_request_id ? NULL : &response, &code, NULL);
7296 case GST_RTSP_STS_OK:
7298 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
7299 gst_rtsp_message_unset (&request);
7300 gst_rtsp_message_unset (&response);
7301 /* cleanup of leftover transport */
7302 gst_rtspsrc_stream_free_udp (stream);
7303 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
7304 * we might be in this case */
7305 if (stream->container && rtpport && rtcpport && !retry) {
7306 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
7311 /* this transport did not go down well, but we may have others to try
7312 * that we did not send yet, try those and only give up then
7313 * but not without checking for lost cause/extension so we can
7314 * post a nicer/more useful error message later */
7315 if (!unsupported_real)
7316 unsupported_real = stream->is_real;
7317 /* select next available protocol, give up on this stream if none */
7319 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
7321 if (!protocol_masks[mask] || unsupported_real)
7326 /* cleanup of leftover transport and move to the next stream */
7327 gst_rtspsrc_stream_free_udp (stream);
7328 goto response_error;
7332 if (!pipelined_request_id) {
7333 /* parse response transport */
7334 res = gst_rtsp_src_setup_stream_from_response (src, stream,
7335 &response, &protocols, retry, &rtpport, &rtcpport);
7337 case GST_RTSP_ERROR:
7339 case GST_RTSP_ELAST:
7345 stream->waiting_setup_response = TRUE;
7346 /* we need to activate at least one stream when we detect activity */
7347 src->need_activate = TRUE;
7354 GstRTSPStream *sskip;
7356 skip = g_list_next (skip);
7360 sskip = (GstRTSPStream *) skip->data;
7362 /* skip all streams with the same control url */
7363 if (g_str_equal (stream->conninfo.location, sskip->conninfo.location)) {
7364 GST_DEBUG_OBJECT (src, "found stream %p with same control %s",
7365 sskip, sskip->conninfo.location);
7366 sskip->skipped = TRUE;
7370 gst_rtsp_message_unset (&request);
7373 if (pipelined_request_id) {
7374 gst_rtspsrc_setup_streams_end (src, TRUE);
7377 /* store the transport protocol that was configured */
7378 src->cur_protocols = protocols;
7380 gst_rtsp_ext_list_stream_select (src->extensions, url);
7382 if (pipelined_request_id)
7383 g_free (pipelined_request_id);
7385 /* if there is nothing to activate, error out */
7386 if (!src->need_activate)
7387 goto nothing_to_activate;
7394 /* no transport possible, post an error and stop */
7395 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
7396 ("Could not connect to server, no protocols left"));
7397 return GST_RTSP_ERROR;
7401 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7402 ("SDP contains no streams"));
7403 return GST_RTSP_ERROR;
7405 create_request_failed:
7407 gchar *str = gst_rtsp_strresult (res);
7409 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7410 ("Could not create request. (%s)", str));
7414 setup_transport_failed:
7416 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7417 ("Could not setup transport."));
7418 res = GST_RTSP_ERROR;
7423 const gchar *str = gst_rtsp_status_as_text (code);
7425 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7426 ("Error (%d): %s", code, GST_STR_NULL (str)));
7427 res = GST_RTSP_ERROR;
7432 gchar *str = gst_rtsp_strresult (res);
7434 if (res != GST_RTSP_EINTR) {
7435 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7436 ("Could not send message. (%s)", str));
7438 GST_WARNING_OBJECT (src, "send interrupted");
7443 nothing_to_activate:
7445 /* none of the available error codes is really right .. */
7446 if (unsupported_real) {
7447 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
7448 (_("No supported stream was found. You might need to install a "
7449 "GStreamer RTSP extension plugin for Real media streams.")),
7452 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
7453 (_("No supported stream was found. You might need to allow "
7454 "more transport protocols or may otherwise be missing "
7455 "the right GStreamer RTSP extension plugin.")), (NULL));
7457 return GST_RTSP_ERROR;
7461 if (pipelined_request_id)
7462 g_free (pipelined_request_id);
7463 gst_rtsp_message_unset (&request);
7464 gst_rtsp_message_unset (&response);
7470 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
7471 GstSegment * segment)
7474 GstRTSPTimeRange *therange;
7477 gst_rtsp_range_free (src->range);
7479 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
7480 GST_DEBUG_OBJECT (src, "parsed range %s", range);
7481 src->range = therange;
7483 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
7485 gst_segment_init (segment, GST_FORMAT_TIME);
7489 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
7490 therange->min.type, therange->min.seconds, therange->max.type,
7491 therange->max.seconds);
7493 if (therange->min.type == GST_RTSP_TIME_NOW)
7495 else if (therange->min.type == GST_RTSP_TIME_END)
7498 seconds = therange->min.seconds * GST_SECOND;
7500 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
7501 GST_TIME_ARGS (seconds));
7503 /* we need to start playback without clipping from the position reported by
7505 segment->start = seconds;
7506 segment->position = seconds;
7508 if (therange->max.type == GST_RTSP_TIME_NOW)
7510 else if (therange->max.type == GST_RTSP_TIME_END)
7513 seconds = therange->max.seconds * GST_SECOND;
7515 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
7516 GST_TIME_ARGS (seconds));
7518 /* live (WMS) server might send overflowed large max as its idea of infinity,
7519 * compensate to prevent problems later on */
7520 if (seconds != -1 && seconds < 0) {
7522 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
7525 /* live (WMS) might send min == max, which is not worth recording */
7526 if (segment->duration == -1 && seconds == segment->start)
7529 /* don't change duration with unknown value, we might have a valid value
7530 * there that we want to keep. */
7532 segment->duration = seconds;
7537 /* Parse clock profived by the server with following syntax:
7539 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
7542 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
7544 gboolean res = FALSE;
7546 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
7547 gchar **fields = NULL, **parts = NULL;
7548 gchar *remote_ip, *str;
7550 GstClockTime base_time;
7553 fields = g_strsplit (gstclock, " ", 0);
7555 /* wrapped clock, not very interesting for now */
7556 if (fields[1] == NULL)
7559 /* remote IP address and port */
7560 if ((str = fields[2]) == NULL)
7563 parts = g_strsplit (str, ":", 0);
7565 if ((remote_ip = parts[0]) == NULL)
7568 if ((str = parts[1]) == NULL)
7576 if ((str = fields[3]) == NULL)
7579 base_time = g_ascii_strtoull (str, NULL, 10);
7582 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
7585 if (src->provided_clock)
7586 gst_object_unref (src->provided_clock);
7587 src->provided_clock = netclock;
7589 gst_element_post_message (GST_ELEMENT_CAST (src),
7590 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
7591 src->provided_clock, TRUE));
7595 g_strfreev (fields);
7601 /* must be called with the RTSP state lock */
7602 static GstRTSPResult
7603 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
7609 /* prepare global stream caps properties */
7611 gst_structure_remove_all_fields (src->props);
7613 src->props = gst_structure_new_empty ("RTSPProperties");
7615 DEBUG_SDP (src, sdp);
7617 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
7619 /* let the app inspect and change the SDP */
7620 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
7622 gst_segment_init (&src->segment, GST_FORMAT_TIME);
7624 /* parse range for duration reporting. */
7629 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
7633 /* keep track of the range and configure it in the segment */
7634 if (gst_rtspsrc_parse_range (src, range, &src->segment))
7638 /* parse clock information. This is GStreamer specific, a server can tell the
7639 * client what clock it is using and wrap that in a network clock. The
7640 * advantage of that is that we can slave to it. */
7642 const gchar *gstclock;
7645 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
7646 if (gstclock == NULL)
7649 /* parse the clock and expose it in the provide_clock method */
7650 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
7654 /* try to find a global control attribute. Note that a '*' means that we should
7655 * do aggregate control with the current url (so we don't do anything and
7656 * leave the current connection as is) */
7658 const gchar *control;
7661 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
7662 if (control == NULL)
7665 /* only take fully qualified urls */
7666 if (g_str_has_prefix (control, "rtsp://"))
7670 g_free (src->conninfo.location);
7671 src->conninfo.location = g_strdup (control);
7672 /* make a connection for this, if there was a connection already, nothing
7674 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
7675 GST_ERROR_OBJECT (src, "could not connect");
7678 /* we need to keep the control url separate from the connection url because
7679 * the rules for constructing the media control url need it */
7680 g_free (src->control);
7681 src->control = g_strdup (control);
7684 /* create streams */
7685 n_streams = gst_sdp_message_medias_len (sdp);
7686 for (i = 0; i < n_streams; i++) {
7687 gst_rtspsrc_create_stream (src, sdp, i, n_streams);
7690 src->state = GST_RTSP_STATE_INIT;
7693 if ((res = gst_rtspsrc_setup_streams_start (src, async)) < 0)
7696 /* reset our state */
7697 src->need_range = TRUE;
7700 src->state = GST_RTSP_STATE_READY;
7707 GST_ERROR_OBJECT (src, "setup failed");
7708 gst_rtspsrc_cleanup (src);
7713 static GstRTSPResult
7714 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
7718 GstRTSPMessage request = { 0 };
7719 GstRTSPMessage response = { 0 };
7722 gchar *respcont = NULL;
7723 GstRTSPVersion versions[] =
7724 { GST_RTSP_VERSION_2_0, GST_RTSP_VERSION_INVALID };
7726 src->version = src->default_version;
7727 if (src->default_version == GST_RTSP_VERSION_2_0) {
7728 versions[0] = GST_RTSP_VERSION_1_0;
7732 src->need_redirect = FALSE;
7734 /* can't continue without a valid url */
7735 if (G_UNLIKELY (src->conninfo.url == NULL)) {
7736 res = GST_RTSP_EINVAL;
7739 src->tried_url_auth = FALSE;
7741 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
7742 goto connect_failed;
7744 /* create OPTIONS */
7745 GST_DEBUG_OBJECT (src, "create options... (%s)", async ? "async" : "sync");
7747 gst_rtspsrc_init_request (src, &request, GST_RTSP_OPTIONS,
7748 src->conninfo.url_str);
7750 goto create_request_failed;
7753 request.type_data.request.version = src->version;
7754 GST_DEBUG_OBJECT (src, "send options...");
7757 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
7760 gst_rtspsrc_send (src, &src->conninfo, &request, &response,
7761 NULL, versions)) < 0) {
7765 src->version = request.type_data.request.version;
7766 GST_INFO_OBJECT (src, "Now using version: %s",
7767 gst_rtsp_version_as_text (src->version));
7770 if (!gst_rtspsrc_parse_methods (src, &response))
7773 /* create DESCRIBE */
7774 GST_DEBUG_OBJECT (src, "create describe...");
7776 gst_rtspsrc_init_request (src, &request, GST_RTSP_DESCRIBE,
7777 src->conninfo.url_str);
7779 goto create_request_failed;
7781 /* we only accept SDP for now */
7782 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
7785 if (src->backchannel == BACKCHANNEL_ONVIF)
7786 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
7787 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
7788 /* TODO: Handle the case when backchannel is unsupported and goto restart */
7791 GST_DEBUG_OBJECT (src, "send describe...");
7794 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
7797 gst_rtspsrc_send (src, &src->conninfo, &request, &response,
7801 /* we only perform redirect for describe and play, currently */
7802 if (src->need_redirect) {
7803 /* close connection, we don't have to send a TEARDOWN yet, ignore the
7805 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7807 gst_rtsp_message_unset (&request);
7808 gst_rtsp_message_unset (&response);
7814 /* it could be that the DESCRIBE method was not implemented */
7815 if (!(src->methods & GST_RTSP_DESCRIBE))
7818 /* check if reply is SDP */
7819 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
7821 /* could not be set but since the request returned OK, we assume it
7822 * was SDP, else check it. */
7824 const gchar *props = strchr (respcont, ';');
7827 gchar *mimetype = g_strndup (respcont, props - respcont);
7829 mimetype = g_strstrip (mimetype);
7830 if (g_ascii_strcasecmp (mimetype, "application/sdp") != 0) {
7832 goto wrong_content_type;
7835 /* TODO: Check for charset property and do conversions of all messages if
7836 * needed. Some servers actually send that property */
7839 } else if (g_ascii_strcasecmp (respcont, "application/sdp") != 0) {
7840 goto wrong_content_type;
7844 /* get message body and parse as SDP */
7845 gst_rtsp_message_get_body (&response, &data, &size);
7846 if (data == NULL || size == 0)
7849 GST_DEBUG_OBJECT (src, "parse SDP...");
7850 gst_sdp_message_new (sdp);
7851 gst_sdp_message_parse_buffer (data, size, *sdp);
7853 /* clean up any messages */
7854 gst_rtsp_message_unset (&request);
7855 gst_rtsp_message_unset (&response);
7862 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
7863 ("No valid RTSP URL was provided"));
7868 gchar *str = gst_rtsp_strresult (res);
7870 if (res != GST_RTSP_EINTR) {
7871 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
7872 ("Failed to connect. (%s)", str));
7874 GST_WARNING_OBJECT (src, "connect interrupted");
7879 create_request_failed:
7881 gchar *str = gst_rtsp_strresult (res);
7883 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7884 ("Could not create request. (%s)", str));
7890 /* Don't post a message - the rtsp_send method will have
7891 * taken care of it because we passed NULL for the response code */
7896 /* error was posted */
7897 res = GST_RTSP_ERROR;
7902 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7903 ("Server does not support SDP, got %s.", respcont));
7904 res = GST_RTSP_ERROR;
7909 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7910 ("Server can not provide an SDP."));
7911 res = GST_RTSP_ERROR;
7916 if (src->conninfo.connection) {
7917 GST_DEBUG_OBJECT (src, "free connection");
7918 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7920 gst_rtsp_message_unset (&request);
7921 gst_rtsp_message_unset (&response);
7926 static GstRTSPResult
7927 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
7932 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
7934 if (src->sdp == NULL) {
7935 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
7939 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
7944 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
7951 GST_WARNING_OBJECT (src, "can't get sdp");
7952 src->open_error = TRUE;
7957 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
7958 src->open_error = TRUE;
7963 static GstRTSPResult
7964 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
7966 GstRTSPMessage request = { 0 };
7967 GstRTSPMessage response = { 0 };
7968 GstRTSPResult res = GST_RTSP_OK;
7970 const gchar *control;
7972 GST_DEBUG_OBJECT (src, "TEARDOWN...");
7974 gst_rtspsrc_set_state (src, GST_STATE_READY);
7976 if (src->state < GST_RTSP_STATE_READY) {
7977 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
7984 /* construct a control url */
7985 control = get_aggregate_control (src);
7987 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
7990 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7991 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7992 const gchar *setup_url;
7993 GstRTSPConnInfo *info;
7995 /* try aggregate control first but do non-aggregate control otherwise */
7997 setup_url = control;
7998 else if ((setup_url = stream->conninfo.location) == NULL)
8001 if (src->conninfo.connection) {
8002 info = &src->conninfo;
8003 } else if (stream->conninfo.connection) {
8004 info = &stream->conninfo;
8008 if (!info->connected)
8013 gst_rtspsrc_init_request (src, &request, GST_RTSP_TEARDOWN, setup_url);
8014 GST_LOG_OBJECT (src, "Teardown on %s", setup_url);
8016 goto create_request_failed;
8018 if (stream->is_backchannel && src->backchannel == BACKCHANNEL_ONVIF)
8019 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
8020 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
8023 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
8026 gst_rtspsrc_send (src, info, &request, &response, NULL, NULL)) < 0)
8029 /* FIXME, parse result? */
8030 gst_rtsp_message_unset (&request);
8031 gst_rtsp_message_unset (&response);
8034 /* early exit when we did aggregate control */
8040 /* close connections */
8041 GST_DEBUG_OBJECT (src, "closing connection...");
8042 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
8043 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8044 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8045 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
8049 gst_rtspsrc_cleanup (src);
8051 src->state = GST_RTSP_STATE_INVALID;
8054 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
8059 create_request_failed:
8061 gchar *str = gst_rtsp_strresult (res);
8063 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
8064 ("Could not create request. (%s)", str));
8070 gchar *str = gst_rtsp_strresult (res);
8072 gst_rtsp_message_unset (&request);
8073 if (res != GST_RTSP_EINTR) {
8074 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
8075 ("Could not send message. (%s)", str));
8077 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
8084 GST_DEBUG_OBJECT (src,
8085 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
8090 /* RTP-Info is of the format:
8092 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
8094 * rtptime corresponds to the timestamp for the NPT time given in the header
8095 * seqbase corresponds to the next sequence number we received. This number
8096 * indicates the first seqnum after the seek and should be used to discard
8097 * packets that are from before the seek.
8100 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
8105 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
8107 infos = g_strsplit (rtpinfo, ",", 0);
8108 for (i = 0; infos[i]; i++) {
8110 GstRTSPStream *stream;
8114 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
8116 /* init values, types of seqbase and timebase are bigger than needed so we
8117 * can store -1 as uninitialized values */
8122 /* parse url, find stream for url.
8123 * parse seq and rtptime. The seq number should be configured in the rtp
8124 * depayloader or session manager to detect gaps. Same for the rtptime, it
8125 * should be used to create an initial time newsegment. */
8126 fields = g_strsplit (infos[i], ";", 0);
8127 for (j = 0; fields[j]; j++) {
8128 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
8129 /* remove leading whitespace */
8130 fields[j] = g_strchug (fields[j]);
8131 if (g_str_has_prefix (fields[j], "url=")) {
8132 /* get the url and the stream */
8134 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
8135 } else if (g_str_has_prefix (fields[j], "seq=")) {
8136 seqbase = atoi (fields[j] + 4);
8137 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
8138 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
8141 g_strfreev (fields);
8142 /* now we need to store the values for the caps of the stream */
8143 if (stream != NULL) {
8144 GST_DEBUG_OBJECT (src,
8145 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
8146 stream, seqbase, timebase);
8148 /* we have a stream, configure detected params */
8149 stream->seqbase = seqbase;
8150 stream->timebase = timebase;
8159 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
8164 interval = strtoul (rtcp, NULL, 10);
8165 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
8170 interval *= GST_MSECOND;
8172 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8173 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8175 /* already (optionally) retrieved this when configuring manager */
8176 if (stream->session) {
8177 GObject *rtpsession = stream->session;
8179 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
8181 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
8185 /* now it happens that (Xenon) server sending this may also provide bogus
8186 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
8187 * and just use RTP-Info to sync */
8189 GObjectClass *klass;
8191 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
8192 if (g_object_class_find_property (klass, "rtcp-sync")) {
8193 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
8194 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
8200 gst_rtspsrc_get_float (const gchar * dstr)
8202 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
8204 /* canonicalise floating point string so we can handle float strings
8205 * in the form "24.930" or "24,930" irrespective of the current locale */
8206 g_strlcpy (s, dstr, sizeof (s));
8207 g_strdelimit (s, ",", '.');
8208 return g_ascii_strtod (s, NULL);
8212 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
8214 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
8216 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
8217 g_strlcpy (val_str, "now", sizeof (val_str));
8219 if (segment->position == 0) {
8220 g_strlcpy (val_str, "0", sizeof (val_str));
8222 g_ascii_dtostr (val_str, sizeof (val_str),
8223 ((gdouble) segment->position) / GST_SECOND);
8226 return g_strdup_printf ("npt=%s-", val_str);
8230 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
8234 stream->timebase = -1;
8235 stream->seqbase = -1;
8237 len = stream->ptmap->len;
8238 for (i = 0; i < len; i++) {
8239 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
8242 if (item->caps == NULL)
8245 item->caps = gst_caps_make_writable (item->caps);
8246 s = gst_caps_get_structure (item->caps, 0);
8247 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
8248 if (item->pt == stream->default_pt && stream->udpsrc[0])
8249 g_object_set (stream->udpsrc[0], "caps", item->caps, NULL);
8251 stream->need_caps = TRUE;
8254 static GstRTSPResult
8255 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
8257 GstRTSPResult res = GST_RTSP_OK;
8259 if (src->state < GST_RTSP_STATE_READY) {
8260 res = GST_RTSP_ERROR;
8261 if (src->open_error) {
8262 GST_DEBUG_OBJECT (src, "the stream was in error");
8266 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
8268 if ((res = gst_rtspsrc_open (src, async)) < 0) {
8269 GST_DEBUG_OBJECT (src, "failed to open stream");
8278 static GstRTSPResult
8279 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async,
8280 const gchar * seek_style)
8282 GstRTSPMessage request = { 0 };
8283 GstRTSPMessage response = { 0 };
8284 GstRTSPResult res = GST_RTSP_OK;
8288 const gchar *control;
8290 GST_DEBUG_OBJECT (src, "PLAY...");
8293 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
8296 if (!(src->methods & GST_RTSP_PLAY))
8299 if (src->state == GST_RTSP_STATE_PLAYING)
8302 if (!src->conninfo.connection || !src->conninfo.connected)
8305 /* send some dummy packets before we activate the receive in the
8307 gst_rtspsrc_send_dummy_packets (src);
8309 /* require new SR packets */
8311 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
8313 /* construct a control url */
8314 control = get_aggregate_control (src);
8316 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8317 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8318 const gchar *setup_url;
8319 GstRTSPConnInfo *conninfo;
8321 /* try aggregate control first but do non-aggregate control otherwise */
8323 setup_url = control;
8324 else if ((setup_url = stream->conninfo.location) == NULL)
8327 if (src->conninfo.connection) {
8328 conninfo = &src->conninfo;
8329 } else if (stream->conninfo.connection) {
8330 conninfo = &stream->conninfo;
8336 res = gst_rtspsrc_init_request (src, &request, GST_RTSP_PLAY, setup_url);
8338 goto create_request_failed;
8340 if (src->need_range && src->seekable >= 0.0) {
8341 hval = gen_range_header (src, segment);
8343 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
8345 /* store the newsegment event so it can be sent from the streaming thread. */
8346 src->need_segment = TRUE;
8349 if (segment->rate != 1.0) {
8350 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
8352 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
8354 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
8356 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
8360 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SEEK_STYLE,
8363 /* when we have an ONVIF audio backchannel, the PLAY request must have the
8364 * Require: header when doing either aggregate or non-aggregate control */
8365 if (src->backchannel == BACKCHANNEL_ONVIF &&
8366 (control || stream->is_backchannel))
8367 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
8368 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
8371 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
8374 gst_rtspsrc_send (src, conninfo, &request, &response, NULL, NULL))
8378 if (src->need_redirect) {
8379 GST_DEBUG_OBJECT (src,
8380 "redirect: tearing down and restarting with new url");
8381 /* teardown and restart with new url */
8382 gst_rtspsrc_close (src, TRUE, FALSE);
8383 /* reset protocols to force re-negotiation with redirected url */
8384 src->cur_protocols = src->protocols;
8385 gst_rtsp_message_unset (&request);
8386 gst_rtsp_message_unset (&response);
8390 /* seek may have silently failed as it is not supported */
8391 if (!(src->methods & GST_RTSP_PLAY)) {
8392 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
8394 if (src->version >= GST_RTSP_VERSION_2_0 && src->seekable >= 0.0) {
8395 GST_WARNING_OBJECT (src, "Server declared stream as seekable but"
8396 " playing with range failed... Ignoring information.");
8398 /* obviously it is supported as we made it here */
8399 src->methods |= GST_RTSP_PLAY;
8400 src->seekable = -1.0;
8401 /* but there is nothing to parse in the response,
8402 * so convey we have no idea and not to expect anything particular */
8403 clear_rtp_base (src, stream);
8407 /* need to do for all streams */
8408 for (run = src->streams; run; run = g_list_next (run))
8409 clear_rtp_base (src, (GstRTSPStream *) run->data);
8411 /* NOTE the above also disables npt based eos detection */
8412 /* and below forces position to 0,
8413 * which is visible feedback we lost the plot */
8414 segment->start = segment->position = src->last_pos;
8417 gst_rtsp_message_unset (&request);
8419 /* parse RTP npt field. This is the current position in the stream (Normal
8420 * Play Time) and should be put in the NEWSEGMENT position field. */
8421 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
8423 gst_rtspsrc_parse_range (src, hval, segment);
8425 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
8426 segment->rate = 1.0;
8428 /* parse Speed header. This is the intended playback rate of the stream
8429 * and should be put in the NEWSEGMENT rate field. */
8430 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
8431 0) == GST_RTSP_OK) {
8432 segment->rate = gst_rtspsrc_get_float (hval);
8433 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
8434 &hval, 0) == GST_RTSP_OK) {
8435 segment->rate = gst_rtspsrc_get_float (hval);
8438 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
8439 * for the RTP packets. If this is not present, we assume all starts from 0...
8440 * This is info for the RTP session manager that we pass to it in caps. */
8442 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
8443 &hval, hval_idx++) == GST_RTSP_OK)
8444 gst_rtspsrc_parse_rtpinfo (src, hval);
8446 /* some servers indicate RTCP parameters in PLAY response,
8447 * rather than properly in SDP */
8448 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
8449 &hval, 0) == GST_RTSP_OK)
8450 gst_rtspsrc_handle_rtcp_interval (src, hval);
8452 gst_rtsp_message_unset (&response);
8454 /* early exit when we did aggregate control */
8458 /* configure the caps of the streams after we parsed all headers. Only reset
8459 * the manager object when we set a new Range header (we did a seek) */
8460 gst_rtspsrc_configure_caps (src, segment, src->need_range);
8462 /* set to PLAYING after we have configured the caps, otherwise we
8463 * might end up calling request_key (with SRTP) while caps are still
8464 * being configured. */
8465 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
8467 /* set again when needed */
8468 src->need_range = FALSE;
8470 src->running = TRUE;
8471 src->base_time = -1;
8472 src->state = GST_RTSP_STATE_PLAYING;
8475 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
8476 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8477 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8478 stream->discont = TRUE;
8483 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
8490 GST_WARNING_OBJECT (src, "failed to open stream");
8495 GST_WARNING_OBJECT (src, "PLAY is not supported");
8500 GST_WARNING_OBJECT (src, "we were already PLAYING");
8503 create_request_failed:
8505 gchar *str = gst_rtsp_strresult (res);
8507 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
8508 ("Could not create request. (%s)", str));
8514 gchar *str = gst_rtsp_strresult (res);
8516 gst_rtsp_message_unset (&request);
8517 if (res != GST_RTSP_EINTR) {
8518 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
8519 ("Could not send message. (%s)", str));
8521 GST_WARNING_OBJECT (src, "PLAY interrupted");
8528 static GstRTSPResult
8529 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
8531 GstRTSPResult res = GST_RTSP_OK;
8532 GstRTSPMessage request = { 0 };
8533 GstRTSPMessage response = { 0 };
8535 const gchar *control;
8537 GST_DEBUG_OBJECT (src, "PAUSE...");
8539 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
8542 if (!(src->methods & GST_RTSP_PAUSE))
8545 if (src->state == GST_RTSP_STATE_READY)
8548 if (!src->conninfo.connection || !src->conninfo.connected)
8551 /* construct a control url */
8552 control = get_aggregate_control (src);
8554 /* loop over the streams. We might exit the loop early when we could do an
8555 * aggregate control */
8556 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8557 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8558 GstRTSPConnInfo *conninfo;
8559 const gchar *setup_url;
8561 /* try aggregate control first but do non-aggregate control otherwise */
8563 setup_url = control;
8564 else if ((setup_url = stream->conninfo.location) == NULL)
8567 if (src->conninfo.connection) {
8568 conninfo = &src->conninfo;
8569 } else if (stream->conninfo.connection) {
8570 conninfo = &stream->conninfo;
8576 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
8577 ("Sending PAUSE request"));
8580 gst_rtspsrc_init_request (src, &request, GST_RTSP_PAUSE,
8582 goto create_request_failed;
8584 /* when we have an ONVIF audio backchannel, the PAUSE request must have the
8585 * Require: header when doing either aggregate or non-aggregate control */
8586 if (src->backchannel == BACKCHANNEL_ONVIF &&
8587 (control || stream->is_backchannel))
8588 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
8589 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
8592 gst_rtspsrc_send (src, conninfo, &request, &response, NULL,
8596 gst_rtsp_message_unset (&request);
8597 gst_rtsp_message_unset (&response);
8599 /* exit early when we did agregate control */
8604 /* change element states now */
8605 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
8608 src->state = GST_RTSP_STATE_READY;
8612 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
8619 GST_DEBUG_OBJECT (src, "failed to open stream");
8624 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
8629 GST_DEBUG_OBJECT (src, "we were already PAUSED");
8632 create_request_failed:
8634 gchar *str = gst_rtsp_strresult (res);
8636 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
8637 ("Could not create request. (%s)", str));
8643 gchar *str = gst_rtsp_strresult (res);
8645 gst_rtsp_message_unset (&request);
8646 if (res != GST_RTSP_EINTR) {
8647 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
8648 ("Could not send message. (%s)", str));
8650 GST_WARNING_OBJECT (src, "PAUSE interrupted");
8658 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
8660 GstRTSPSrc *rtspsrc;
8662 rtspsrc = GST_RTSPSRC (bin);
8664 switch (GST_MESSAGE_TYPE (message)) {
8665 case GST_MESSAGE_EOS:
8666 gst_message_unref (message);
8668 case GST_MESSAGE_ELEMENT:
8670 const GstStructure *s = gst_message_get_structure (message);
8672 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
8673 gboolean ignore_timeout;
8675 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
8677 GST_OBJECT_LOCK (rtspsrc);
8678 ignore_timeout = rtspsrc->ignore_timeout;
8679 rtspsrc->ignore_timeout = TRUE;
8680 GST_OBJECT_UNLOCK (rtspsrc);
8682 /* we only act on the first udp timeout message, others are irrelevant
8683 * and can be ignored. */
8684 if (!ignore_timeout)
8685 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
8687 gst_message_unref (message);
8690 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8693 case GST_MESSAGE_ERROR:
8696 GstRTSPStream *stream;
8699 udpsrc = GST_MESSAGE_SRC (message);
8701 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
8702 GST_ELEMENT_NAME (udpsrc));
8704 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
8708 /* we ignore the RTCP udpsrc */
8709 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
8712 /* if we get error messages from the udp sources, that's not a problem as
8713 * long as not all of them error out. We also don't really know what the
8714 * problem is, the message does not give enough detail... */
8715 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
8716 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
8717 if (ret != GST_FLOW_OK)
8721 gst_message_unref (message);
8725 /* fatal but not our message, forward */
8726 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8731 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8737 /* the thread where everything happens */
8739 gst_rtspsrc_thread (GstRTSPSrc * src)
8742 ParameterRequest *req = NULL;
8744 GST_OBJECT_LOCK (src);
8745 cmd = src->pending_cmd;
8746 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
8747 || cmd == CMD_LOOP || cmd == CMD_OPEN || cmd == CMD_GET_PARAMETER
8748 || cmd == CMD_SET_PARAMETER) {
8749 if (g_queue_is_empty (&src->set_get_param_q)) {
8750 src->pending_cmd = CMD_LOOP;
8752 ParameterRequest *next_req;
8753 req = g_queue_pop_head (&src->set_get_param_q);
8754 next_req = g_queue_peek_head (&src->set_get_param_q);
8755 src->pending_cmd = next_req ? next_req->cmd : CMD_LOOP;
8758 src->pending_cmd = CMD_WAIT;
8759 GST_DEBUG_OBJECT (src, "got command %s", cmd_to_string (cmd));
8761 /* we got the message command, so ensure communication is possible again */
8762 gst_rtspsrc_connection_flush (src, FALSE);
8764 src->busy_cmd = cmd;
8765 GST_OBJECT_UNLOCK (src);
8769 gst_rtspsrc_open (src, TRUE);
8772 gst_rtspsrc_play (src, &src->segment, TRUE, NULL);
8775 gst_rtspsrc_pause (src, TRUE);
8778 gst_rtspsrc_close (src, TRUE, FALSE);
8780 case CMD_GET_PARAMETER:
8781 gst_rtspsrc_get_parameter (src, req);
8783 case CMD_SET_PARAMETER:
8784 gst_rtspsrc_set_parameter (src, req);
8787 gst_rtspsrc_loop (src);
8790 gst_rtspsrc_reconnect (src, FALSE);
8796 GST_OBJECT_LOCK (src);
8797 /* No more cmds, wake any waiters */
8798 g_cond_broadcast (&src->cmd_cond);
8799 /* and go back to sleep */
8800 if (src->pending_cmd == CMD_WAIT) {
8802 gst_task_pause (src->task);
8805 src->busy_cmd = CMD_WAIT;
8806 GST_OBJECT_UNLOCK (src);
8810 gst_rtspsrc_start (GstRTSPSrc * src)
8812 GST_DEBUG_OBJECT (src, "starting");
8814 GST_OBJECT_LOCK (src);
8816 src->pending_cmd = CMD_WAIT;
8818 if (src->task == NULL) {
8819 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
8820 if (src->task == NULL)
8823 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
8825 GST_OBJECT_UNLOCK (src);
8832 GST_OBJECT_UNLOCK (src);
8833 GST_ERROR_OBJECT (src, "failed to create task");
8839 gst_rtspsrc_stop (GstRTSPSrc * src)
8843 GST_DEBUG_OBJECT (src, "stopping");
8845 /* also cancels pending task */
8846 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
8848 GST_OBJECT_LOCK (src);
8849 if ((task = src->task)) {
8851 GST_OBJECT_UNLOCK (src);
8853 gst_task_stop (task);
8855 /* make sure it is not running */
8856 GST_RTSP_STREAM_LOCK (src);
8857 GST_RTSP_STREAM_UNLOCK (src);
8859 /* now wait for the task to finish */
8860 gst_task_join (task);
8862 /* and free the task */
8863 gst_object_unref (GST_OBJECT (task));
8865 GST_OBJECT_LOCK (src);
8867 GST_OBJECT_UNLOCK (src);
8869 /* ensure synchronously all is closed and clean */
8870 gst_rtspsrc_close (src, FALSE, TRUE);
8875 static GstStateChangeReturn
8876 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
8878 GstRTSPSrc *rtspsrc;
8879 GstStateChangeReturn ret;
8881 rtspsrc = GST_RTSPSRC (element);
8883 switch (transition) {
8884 case GST_STATE_CHANGE_NULL_TO_READY:
8885 if (!gst_rtspsrc_start (rtspsrc))
8888 case GST_STATE_CHANGE_READY_TO_PAUSED:
8889 /* init some state */
8890 rtspsrc->cur_protocols = rtspsrc->protocols;
8891 /* first attempt, don't ignore timeouts */
8892 rtspsrc->ignore_timeout = FALSE;
8893 rtspsrc->open_error = FALSE;
8894 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
8896 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
8897 set_manager_buffer_mode (rtspsrc);
8899 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
8900 /* unblock the tcp tasks and make the loop waiting */
8901 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
8902 /* make sure it is waiting before we send PAUSE or PLAY below */
8903 GST_RTSP_STREAM_LOCK (rtspsrc);
8904 GST_RTSP_STREAM_UNLOCK (rtspsrc);
8907 case GST_STATE_CHANGE_PAUSED_TO_READY:
8913 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
8914 if (ret == GST_STATE_CHANGE_FAILURE)
8917 switch (transition) {
8918 case GST_STATE_CHANGE_NULL_TO_READY:
8919 ret = GST_STATE_CHANGE_SUCCESS;
8921 case GST_STATE_CHANGE_READY_TO_PAUSED:
8922 ret = GST_STATE_CHANGE_NO_PREROLL;
8924 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
8925 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
8926 ret = GST_STATE_CHANGE_SUCCESS;
8928 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
8929 /* send pause request and keep the idle task around */
8930 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
8931 ret = GST_STATE_CHANGE_NO_PREROLL;
8933 case GST_STATE_CHANGE_PAUSED_TO_READY:
8934 gst_rtspsrc_loop_send_cmd_and_wait (rtspsrc, CMD_CLOSE, CMD_ALL,
8935 rtspsrc->teardown_timeout);
8936 ret = GST_STATE_CHANGE_SUCCESS;
8938 case GST_STATE_CHANGE_READY_TO_NULL:
8939 gst_rtspsrc_stop (rtspsrc);
8940 ret = GST_STATE_CHANGE_SUCCESS;
8943 /* Otherwise it's success, we don't want to return spurious
8944 * NO_PREROLL or ASYNC from internal elements as we care for
8945 * state changes ourselves here
8947 * This is to catch PAUSED->PAUSED and PLAYING->PLAYING transitions.
8949 if (GST_STATE_TRANSITION_NEXT (transition) == GST_STATE_PAUSED)
8950 ret = GST_STATE_CHANGE_NO_PREROLL;
8952 ret = GST_STATE_CHANGE_SUCCESS;
8961 GST_DEBUG_OBJECT (rtspsrc, "start failed");
8962 return GST_STATE_CHANGE_FAILURE;
8967 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
8970 GstRTSPSrc *rtspsrc;
8972 rtspsrc = GST_RTSPSRC (element);
8974 if (GST_EVENT_IS_DOWNSTREAM (event)) {
8975 res = gst_rtspsrc_push_event (rtspsrc, event);
8977 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
8984 /*** GSTURIHANDLER INTERFACE *************************************************/
8987 gst_rtspsrc_uri_get_type (GType type)
8992 static const gchar *const *
8993 gst_rtspsrc_uri_get_protocols (GType type)
8995 static const gchar *protocols[] =
8996 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
8997 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
9004 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
9006 GstRTSPSrc *src = GST_RTSPSRC (handler);
9008 /* FIXME: make thread-safe */
9009 return g_strdup (src->conninfo.location);
9013 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
9019 GstRTSPUrl *newurl = NULL;
9020 GstSDPMessage *sdp = NULL;
9022 src = GST_RTSPSRC (handler);
9024 /* same URI, we're fine */
9025 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
9028 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
9029 sres = gst_sdp_message_new (&sdp);
9033 GST_DEBUG_OBJECT (src, "parsing SDP message");
9034 sres = gst_sdp_message_parse_uri (uri, sdp);
9039 GST_DEBUG_OBJECT (src, "parsing URI");
9040 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
9044 /* if worked, free previous and store new url object along with the original
9046 GST_DEBUG_OBJECT (src, "configuring URI");
9047 g_free (src->conninfo.location);
9048 src->conninfo.location = g_strdup (uri);
9049 gst_rtsp_url_free (src->conninfo.url);
9050 src->conninfo.url = newurl;
9051 g_free (src->conninfo.url_str);
9053 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
9055 src->conninfo.url_str = NULL;
9058 gst_sdp_message_free (src->sdp);
9060 src->from_sdp = sdp != NULL;
9062 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
9063 GST_DEBUG_OBJECT (src, "request uri is: %s",
9064 GST_STR_NULL (src->conninfo.url_str));
9071 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
9076 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", sres);
9077 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
9078 "Could not create SDP");
9083 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", sres,
9084 GST_STR_NULL (uri));
9085 gst_sdp_message_free (sdp);
9086 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
9092 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
9093 GST_STR_NULL (uri), res);
9094 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
9095 "Invalid RTSP URI");
9101 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
9103 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
9105 iface->get_type = gst_rtspsrc_uri_get_type;
9106 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
9107 iface->get_uri = gst_rtspsrc_uri_get_uri;
9108 iface->set_uri = gst_rtspsrc_uri_set_uri;
9112 /* send GET_PARAMETER */
9113 static GstRTSPResult
9114 gst_rtspsrc_get_parameter (GstRTSPSrc * src, ParameterRequest * req)
9116 GstRTSPMessage request = { 0 };
9117 GstRTSPMessage response = { 0 };
9119 GstRTSPStatusCode code = GST_RTSP_STS_OK;
9120 const gchar *control;
9121 gchar *recv_body = NULL;
9122 guint recv_body_len;
9124 GST_DEBUG_OBJECT (src, "creating server get_parameter");
9126 if ((res = gst_rtspsrc_ensure_open (src, FALSE)) < 0)
9129 control = get_aggregate_control (src);
9130 if (control == NULL)
9133 if (!(src->methods & GST_RTSP_GET_PARAMETER))
9136 gst_rtspsrc_connection_flush (src, FALSE);
9138 res = gst_rtsp_message_init_request (&request, GST_RTSP_GET_PARAMETER,
9141 goto create_request_failed;
9143 res = gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CONTENT_TYPE,
9144 req->content_type == NULL ? "text/parameters" : req->content_type);
9146 goto add_content_hdr_failed;
9148 if (req->body && req->body->len) {
9150 gst_rtsp_message_set_body (&request, (guint8 *) req->body->str,
9153 goto set_body_failed;
9156 if ((res = gst_rtspsrc_send (src, &src->conninfo,
9157 &request, &response, &code, NULL)) < 0)
9160 res = gst_rtsp_message_get_body (&response, (guint8 **) & recv_body,
9163 goto get_body_failed;
9167 gst_promise_reply (req->promise,
9168 gst_structure_new ("get-parameter-reply",
9169 "rtsp-result", G_TYPE_INT, res,
9170 "rtsp-code", G_TYPE_INT, code,
9171 "rtsp-reason", G_TYPE_STRING, gst_rtsp_status_as_text (code),
9172 "body", G_TYPE_STRING, GST_STR_NULL (recv_body), NULL));
9173 free_param_data (req);
9176 gst_rtsp_message_unset (&request);
9177 gst_rtsp_message_unset (&response);
9185 GST_DEBUG_OBJECT (src, "failed to open stream");
9190 GST_DEBUG_OBJECT (src, "no control url to send GET_PARAMETER");
9191 res = GST_RTSP_ERROR;
9196 GST_DEBUG_OBJECT (src, "GET_PARAMETER is not supported");
9197 res = GST_RTSP_ERROR;
9200 create_request_failed:
9202 GST_DEBUG_OBJECT (src, "could not create GET_PARAMETER request");
9205 add_content_hdr_failed:
9207 GST_DEBUG_OBJECT (src, "could not add content header");
9212 GST_DEBUG_OBJECT (src, "could not set body");
9217 gchar *str = gst_rtsp_strresult (res);
9219 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
9220 ("Could not send get-parameter. (%s)", str));
9226 GST_DEBUG_OBJECT (src, "could not get body");
9231 /* send SET_PARAMETER */
9232 static GstRTSPResult
9233 gst_rtspsrc_set_parameter (GstRTSPSrc * src, ParameterRequest * req)
9235 GstRTSPMessage request = { 0 };
9236 GstRTSPMessage response = { 0 };
9237 GstRTSPResult res = GST_RTSP_OK;
9238 GstRTSPStatusCode code = GST_RTSP_STS_OK;
9239 const gchar *control;
9241 GST_DEBUG_OBJECT (src, "creating server set_parameter");
9243 if ((res = gst_rtspsrc_ensure_open (src, FALSE)) < 0)
9246 control = get_aggregate_control (src);
9247 if (control == NULL)
9250 if (!(src->methods & GST_RTSP_SET_PARAMETER))
9253 gst_rtspsrc_connection_flush (src, FALSE);
9256 gst_rtsp_message_init_request (&request, GST_RTSP_SET_PARAMETER, control);
9260 res = gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CONTENT_TYPE,
9261 req->content_type == NULL ? "text/parameters" : req->content_type);
9263 goto add_content_hdr_failed;
9265 if (req->body && req->body->len) {
9267 gst_rtsp_message_set_body (&request, (guint8 *) req->body->str,
9271 goto set_body_failed;
9274 if ((res = gst_rtspsrc_send (src, &src->conninfo,
9275 &request, &response, &code, NULL)) < 0)
9280 gst_promise_reply (req->promise, gst_structure_new ("set-parameter-reply",
9281 "rtsp-result", G_TYPE_INT, res,
9282 "rtsp-code", G_TYPE_INT, code,
9283 "rtsp-reason", G_TYPE_STRING, gst_rtsp_status_as_text (code),
9285 free_param_data (req);
9287 gst_rtsp_message_unset (&request);
9288 gst_rtsp_message_unset (&response);
9296 GST_DEBUG_OBJECT (src, "failed to open stream");
9301 GST_DEBUG_OBJECT (src, "no control url to send SET_PARAMETER");
9302 res = GST_RTSP_ERROR;
9307 GST_DEBUG_OBJECT (src, "SET_PARAMETER is not supported");
9308 res = GST_RTSP_ERROR;
9311 add_content_hdr_failed:
9313 GST_DEBUG_OBJECT (src, "could not add content header");
9318 GST_DEBUG_OBJECT (src, "could not set body");
9323 gchar *str = gst_rtsp_strresult (res);
9325 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
9326 ("Could not send set-parameter. (%s)", str));
9332 typedef struct _RTSPKeyValue
9334 GstRTSPHeaderField field;
9336 gchar *custom_key; /* custom header string (field is INVALID then) */
9340 key_value_foreach (GArray * array, GFunc func, gpointer user_data)
9344 g_return_if_fail (array != NULL);
9346 for (i = 0; i < array->len; i++) {
9347 (*func) (&g_array_index (array, RTSPKeyValue, i), user_data);
9352 dump_key_value (gpointer data, gpointer user_data G_GNUC_UNUSED)
9354 RTSPKeyValue *key_value = (RTSPKeyValue *) data;
9355 GstRTSPSrc *src = GST_RTSPSRC (user_data);
9356 const gchar *key_string;
9358 if (key_value->custom_key != NULL)
9359 key_string = key_value->custom_key;
9361 key_string = gst_rtsp_header_as_text (key_value->field);
9363 GST_LOG_OBJECT (src, " key: '%s', value: '%s'", key_string,
9368 gst_rtspsrc_print_rtsp_message (GstRTSPSrc * src, const GstRTSPMessage * msg)
9372 GString *body_string = NULL;
9374 g_return_if_fail (src != NULL);
9375 g_return_if_fail (msg != NULL);
9377 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) < GST_LEVEL_LOG)
9380 GST_LOG_OBJECT (src, "--------------------------------------------");
9381 switch (msg->type) {
9382 case GST_RTSP_MESSAGE_REQUEST:
9383 GST_LOG_OBJECT (src, "RTSP request message %p", msg);
9384 GST_LOG_OBJECT (src, " request line:");
9385 GST_LOG_OBJECT (src, " method: '%s'",
9386 gst_rtsp_method_as_text (msg->type_data.request.method));
9387 GST_LOG_OBJECT (src, " uri: '%s'", msg->type_data.request.uri);
9388 GST_LOG_OBJECT (src, " version: '%s'",
9389 gst_rtsp_version_as_text (msg->type_data.request.version));
9390 GST_LOG_OBJECT (src, " headers:");
9391 key_value_foreach (msg->hdr_fields, dump_key_value, src);
9392 GST_LOG_OBJECT (src, " body:");
9393 gst_rtsp_message_get_body (msg, &data, &size);
9395 body_string = g_string_new_len ((const gchar *) data, size);
9396 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
9397 g_string_free (body_string, TRUE);
9401 case GST_RTSP_MESSAGE_RESPONSE:
9402 GST_LOG_OBJECT (src, "RTSP response message %p", msg);
9403 GST_LOG_OBJECT (src, " status line:");
9404 GST_LOG_OBJECT (src, " code: '%d'", msg->type_data.response.code);
9405 GST_LOG_OBJECT (src, " reason: '%s'", msg->type_data.response.reason);
9406 GST_LOG_OBJECT (src, " version: '%s",
9407 gst_rtsp_version_as_text (msg->type_data.response.version));
9408 GST_LOG_OBJECT (src, " headers:");
9409 key_value_foreach (msg->hdr_fields, dump_key_value, src);
9410 gst_rtsp_message_get_body (msg, &data, &size);
9411 GST_LOG_OBJECT (src, " body: length %d", size);
9413 body_string = g_string_new_len ((const gchar *) data, size);
9414 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
9415 g_string_free (body_string, TRUE);
9419 case GST_RTSP_MESSAGE_HTTP_REQUEST:
9420 GST_LOG_OBJECT (src, "HTTP request message %p", msg);
9421 GST_LOG_OBJECT (src, " request line:");
9422 GST_LOG_OBJECT (src, " method: '%s'",
9423 gst_rtsp_method_as_text (msg->type_data.request.method));
9424 GST_LOG_OBJECT (src, " uri: '%s'", msg->type_data.request.uri);
9425 GST_LOG_OBJECT (src, " version: '%s'",
9426 gst_rtsp_version_as_text (msg->type_data.request.version));
9427 GST_LOG_OBJECT (src, " headers:");
9428 key_value_foreach (msg->hdr_fields, dump_key_value, src);
9429 GST_LOG_OBJECT (src, " body:");
9430 gst_rtsp_message_get_body (msg, &data, &size);
9432 body_string = g_string_new_len ((const gchar *) data, size);
9433 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
9434 g_string_free (body_string, TRUE);
9438 case GST_RTSP_MESSAGE_HTTP_RESPONSE:
9439 GST_LOG_OBJECT (src, "HTTP response message %p", msg);
9440 GST_LOG_OBJECT (src, " status line:");
9441 GST_LOG_OBJECT (src, " code: '%d'", msg->type_data.response.code);
9442 GST_LOG_OBJECT (src, " reason: '%s'", msg->type_data.response.reason);
9443 GST_LOG_OBJECT (src, " version: '%s'",
9444 gst_rtsp_version_as_text (msg->type_data.response.version));
9445 GST_LOG_OBJECT (src, " headers:");
9446 key_value_foreach (msg->hdr_fields, dump_key_value, src);
9447 gst_rtsp_message_get_body (msg, &data, &size);
9448 GST_LOG_OBJECT (src, " body: length %d", size);
9450 body_string = g_string_new_len ((const gchar *) data, size);
9451 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
9452 g_string_free (body_string, TRUE);
9456 case GST_RTSP_MESSAGE_DATA:
9457 GST_LOG_OBJECT (src, "RTSP data message %p", msg);
9458 GST_LOG_OBJECT (src, " channel: '%d'", msg->type_data.data.channel);
9459 GST_LOG_OBJECT (src, " size: '%d'", msg->body_size);
9460 gst_rtsp_message_get_body (msg, &data, &size);
9462 body_string = g_string_new_len ((const gchar *) data, size);
9463 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
9464 g_string_free (body_string, TRUE);
9469 GST_LOG_OBJECT (src, "unsupported message type %d", msg->type);
9472 GST_LOG_OBJECT (src, "--------------------------------------------");
9476 gst_rtspsrc_print_sdp_media (GstRTSPSrc * src, GstSDPMedia * media)
9478 GST_LOG_OBJECT (src, " media: '%s'", GST_STR_NULL (media->media));
9479 GST_LOG_OBJECT (src, " port: '%u'", media->port);
9480 GST_LOG_OBJECT (src, " num_ports: '%u'", media->num_ports);
9481 GST_LOG_OBJECT (src, " proto: '%s'", GST_STR_NULL (media->proto));
9482 if (media->fmts && media->fmts->len > 0) {
9485 GST_LOG_OBJECT (src, " formats:");
9486 for (i = 0; i < media->fmts->len; i++) {
9487 GST_LOG_OBJECT (src, " format '%s'", g_array_index (media->fmts,
9491 GST_LOG_OBJECT (src, " information: '%s'",
9492 GST_STR_NULL (media->information));
9493 if (media->connections && media->connections->len > 0) {
9496 GST_LOG_OBJECT (src, " connections:");
9497 for (i = 0; i < media->connections->len; i++) {
9498 GstSDPConnection *conn =
9499 &g_array_index (media->connections, GstSDPConnection, i);
9501 GST_LOG_OBJECT (src, " nettype: '%s'",
9502 GST_STR_NULL (conn->nettype));
9503 GST_LOG_OBJECT (src, " addrtype: '%s'",
9504 GST_STR_NULL (conn->addrtype));
9505 GST_LOG_OBJECT (src, " address: '%s'",
9506 GST_STR_NULL (conn->address));
9507 GST_LOG_OBJECT (src, " ttl: '%u'", conn->ttl);
9508 GST_LOG_OBJECT (src, " addr_number: '%u'", conn->addr_number);
9511 if (media->bandwidths && media->bandwidths->len > 0) {
9514 GST_LOG_OBJECT (src, " bandwidths:");
9515 for (i = 0; i < media->bandwidths->len; i++) {
9516 GstSDPBandwidth *bw =
9517 &g_array_index (media->bandwidths, GstSDPBandwidth, i);
9519 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (bw->bwtype));
9520 GST_LOG_OBJECT (src, " bandwidth: '%u'", bw->bandwidth);
9523 GST_LOG_OBJECT (src, " key:");
9524 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (media->key.type));
9525 GST_LOG_OBJECT (src, " data: '%s'", GST_STR_NULL (media->key.data));
9526 if (media->attributes && media->attributes->len > 0) {
9529 GST_LOG_OBJECT (src, " attributes:");
9530 for (i = 0; i < media->attributes->len; i++) {
9531 GstSDPAttribute *attr =
9532 &g_array_index (media->attributes, GstSDPAttribute, i);
9534 GST_LOG_OBJECT (src, " attribute '%s' : '%s'", attr->key, attr->value);
9540 gst_rtspsrc_print_sdp_message (GstRTSPSrc * src, const GstSDPMessage * msg)
9542 g_return_if_fail (src != NULL);
9543 g_return_if_fail (msg != NULL);
9545 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) < GST_LEVEL_LOG)
9548 GST_LOG_OBJECT (src, "--------------------------------------------");
9549 GST_LOG_OBJECT (src, "sdp packet %p:", msg);
9550 GST_LOG_OBJECT (src, " version: '%s'", GST_STR_NULL (msg->version));
9551 GST_LOG_OBJECT (src, " origin:");
9552 GST_LOG_OBJECT (src, " username: '%s'",
9553 GST_STR_NULL (msg->origin.username));
9554 GST_LOG_OBJECT (src, " sess_id: '%s'",
9555 GST_STR_NULL (msg->origin.sess_id));
9556 GST_LOG_OBJECT (src, " sess_version: '%s'",
9557 GST_STR_NULL (msg->origin.sess_version));
9558 GST_LOG_OBJECT (src, " nettype: '%s'",
9559 GST_STR_NULL (msg->origin.nettype));
9560 GST_LOG_OBJECT (src, " addrtype: '%s'",
9561 GST_STR_NULL (msg->origin.addrtype));
9562 GST_LOG_OBJECT (src, " addr: '%s'", GST_STR_NULL (msg->origin.addr));
9563 GST_LOG_OBJECT (src, " session_name: '%s'",
9564 GST_STR_NULL (msg->session_name));
9565 GST_LOG_OBJECT (src, " information: '%s'", GST_STR_NULL (msg->information));
9566 GST_LOG_OBJECT (src, " uri: '%s'", GST_STR_NULL (msg->uri));
9568 if (msg->emails && msg->emails->len > 0) {
9571 GST_LOG_OBJECT (src, " emails:");
9572 for (i = 0; i < msg->emails->len; i++) {
9573 GST_LOG_OBJECT (src, " email '%s'", g_array_index (msg->emails, gchar *,
9577 if (msg->phones && msg->phones->len > 0) {
9580 GST_LOG_OBJECT (src, " phones:");
9581 for (i = 0; i < msg->phones->len; i++) {
9582 GST_LOG_OBJECT (src, " phone '%s'", g_array_index (msg->phones, gchar *,
9586 GST_LOG_OBJECT (src, " connection:");
9587 GST_LOG_OBJECT (src, " nettype: '%s'",
9588 GST_STR_NULL (msg->connection.nettype));
9589 GST_LOG_OBJECT (src, " addrtype: '%s'",
9590 GST_STR_NULL (msg->connection.addrtype));
9591 GST_LOG_OBJECT (src, " address: '%s'",
9592 GST_STR_NULL (msg->connection.address));
9593 GST_LOG_OBJECT (src, " ttl: '%u'", msg->connection.ttl);
9594 GST_LOG_OBJECT (src, " addr_number: '%u'", msg->connection.addr_number);
9595 if (msg->bandwidths && msg->bandwidths->len > 0) {
9598 GST_LOG_OBJECT (src, " bandwidths:");
9599 for (i = 0; i < msg->bandwidths->len; i++) {
9600 GstSDPBandwidth *bw =
9601 &g_array_index (msg->bandwidths, GstSDPBandwidth, i);
9603 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (bw->bwtype));
9604 GST_LOG_OBJECT (src, " bandwidth: '%u'", bw->bandwidth);
9607 GST_LOG_OBJECT (src, " key:");
9608 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (msg->key.type));
9609 GST_LOG_OBJECT (src, " data: '%s'", GST_STR_NULL (msg->key.data));
9610 if (msg->attributes && msg->attributes->len > 0) {
9613 GST_LOG_OBJECT (src, " attributes:");
9614 for (i = 0; i < msg->attributes->len; i++) {
9615 GstSDPAttribute *attr =
9616 &g_array_index (msg->attributes, GstSDPAttribute, i);
9618 GST_LOG_OBJECT (src, " attribute '%s' : '%s'", attr->key, attr->value);
9621 if (msg->medias && msg->medias->len > 0) {
9624 GST_LOG_OBJECT (src, " medias:");
9625 for (i = 0; i < msg->medias->len; i++) {
9626 GST_LOG_OBJECT (src, " media %u:", i);
9627 gst_rtspsrc_print_sdp_media (src, &g_array_index (msg->medias,
9631 GST_LOG_OBJECT (src, "--------------------------------------------");