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
88 * NOTE: rtspsrc will send a PAUSE command to the server if you set the
89 * element to the PAUSED state, and will send a PLAY command if you set it to
92 * Unfortunately, going to the NULL state involves going through PAUSED, so
93 * rtspsrc does not know the difference and will send a PAUSE when you wanted
94 * a TEARDOWN. The workaround is to hook into the `before-send` signal and
95 * return FALSE in this case.
104 #endif /* HAVE_UNISTD_H */
110 #include <gst/net/gstnet.h>
111 #include <gst/sdp/gstsdpmessage.h>
112 #include <gst/sdp/gstmikey.h>
113 #include <gst/rtp/rtp.h>
115 #include <glib/gi18n-lib.h>
117 #include "gstrtspelements.h"
118 #include "gstrtspsrc.h"
120 GST_DEBUG_CATEGORY_STATIC (rtspsrc_debug);
121 #define GST_CAT_DEFAULT (rtspsrc_debug)
123 static GstStaticPadTemplate rtptemplate = GST_STATIC_PAD_TEMPLATE ("stream_%u",
126 GST_STATIC_CAPS ("application/x-rtp; application/x-rdt"));
128 /* templates used internally */
129 static GstStaticPadTemplate anysrctemplate =
130 GST_STATIC_PAD_TEMPLATE ("internalsrc_%u",
133 GST_STATIC_CAPS_ANY);
135 static GstStaticPadTemplate anysinktemplate =
136 GST_STATIC_PAD_TEMPLATE ("internalsink_%u",
139 GST_STATIC_CAPS_ANY);
143 SIGNAL_HANDLE_REQUEST,
145 SIGNAL_SELECT_STREAM,
147 SIGNAL_REQUEST_RTCP_KEY,
148 SIGNAL_ACCEPT_CERTIFICATE,
150 SIGNAL_PUSH_BACKCHANNEL_BUFFER,
151 SIGNAL_GET_PARAMETER,
152 SIGNAL_GET_PARAMETERS,
153 SIGNAL_SET_PARAMETER,
154 SIGNAL_PUSH_BACKCHANNEL_SAMPLE,
158 enum _GstRtspSrcRtcpSyncMode
165 #define GST_TYPE_RTSP_SRC_TIMEOUT_CAUSE (gst_rtsp_src_timeout_cause_get_type())
167 gst_rtsp_src_timeout_cause_get_type (void)
169 static GType timeout_cause_type = 0;
170 static const GEnumValue timeout_causes[] = {
171 {GST_RTSP_SRC_TIMEOUT_CAUSE_RTCP, "timeout triggered by RTCP", "RTCP"},
175 if (!timeout_cause_type) {
177 g_enum_register_static ("GstRTSPSrcTimeoutCause", timeout_causes);
179 return timeout_cause_type;
182 enum _GstRtspSrcBufferMode
191 #define GST_TYPE_RTSP_SRC_BUFFER_MODE (gst_rtsp_src_buffer_mode_get_type())
193 gst_rtsp_src_buffer_mode_get_type (void)
195 static GType buffer_mode_type = 0;
196 static const GEnumValue buffer_modes[] = {
197 {BUFFER_MODE_NONE, "Only use RTP timestamps", "none"},
198 {BUFFER_MODE_SLAVE, "Slave receiver to sender clock", "slave"},
199 {BUFFER_MODE_BUFFER, "Do low/high watermark buffering", "buffer"},
200 {BUFFER_MODE_AUTO, "Choose mode depending on stream live", "auto"},
201 {BUFFER_MODE_SYNCED, "Synchronized sender and receiver clocks", "synced"},
205 if (!buffer_mode_type) {
207 g_enum_register_static ("GstRTSPSrcBufferMode", buffer_modes);
209 return buffer_mode_type;
212 enum _GstRtspSrcNtpTimeSource
215 NTP_TIME_SOURCE_UNIX,
216 NTP_TIME_SOURCE_RUNNING_TIME,
217 NTP_TIME_SOURCE_CLOCK_TIME
220 #define DEBUG_RTSP(__self,msg) gst_rtspsrc_print_rtsp_message (__self, msg)
221 #define DEBUG_SDP(__self,msg) gst_rtspsrc_print_sdp_message (__self, msg)
223 #define GST_TYPE_RTSP_SRC_NTP_TIME_SOURCE (gst_rtsp_src_ntp_time_source_get_type())
225 gst_rtsp_src_ntp_time_source_get_type (void)
227 static GType ntp_time_source_type = 0;
228 static const GEnumValue ntp_time_source_values[] = {
229 {NTP_TIME_SOURCE_NTP, "NTP time based on realtime clock", "ntp"},
230 {NTP_TIME_SOURCE_UNIX, "UNIX time based on realtime clock", "unix"},
231 {NTP_TIME_SOURCE_RUNNING_TIME,
232 "Running time based on pipeline clock",
234 {NTP_TIME_SOURCE_CLOCK_TIME, "Pipeline clock time", "clock-time"},
238 if (!ntp_time_source_type) {
239 ntp_time_source_type =
240 g_enum_register_static ("GstRTSPSrcNtpTimeSource",
241 ntp_time_source_values);
243 return ntp_time_source_type;
246 enum _GstRtspBackchannel
252 #define GST_TYPE_RTSP_BACKCHANNEL (gst_rtsp_backchannel_get_type())
254 gst_rtsp_backchannel_get_type (void)
256 static GType backchannel_type = 0;
257 static const GEnumValue backchannel_values[] = {
258 {BACKCHANNEL_NONE, "No backchannel", "none"},
259 {BACKCHANNEL_ONVIF, "ONVIF audio backchannel", "onvif"},
263 if (G_UNLIKELY (backchannel_type == 0)) {
265 g_enum_register_static ("GstRTSPBackchannel", backchannel_values);
267 return backchannel_type;
270 #define BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL "www.onvif.org/ver20/backchannel"
272 #define DEFAULT_LOCATION NULL
273 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
274 #define DEFAULT_DEBUG FALSE
275 #define DEFAULT_RETRY 20
276 #define DEFAULT_TIMEOUT 5000000
277 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
278 #define DEFAULT_TCP_TIMEOUT 20000000
279 #define DEFAULT_LATENCY_MS 2000
280 #define DEFAULT_DROP_ON_LATENCY FALSE
281 #define DEFAULT_CONNECTION_SPEED 0
282 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
283 #define DEFAULT_DO_RTCP TRUE
284 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
285 #define DEFAULT_PROXY NULL
286 #define DEFAULT_RTP_BLOCKSIZE 0
287 #define DEFAULT_USER_ID NULL
288 #define DEFAULT_USER_PW NULL
289 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
290 #define DEFAULT_PORT_RANGE NULL
291 #define DEFAULT_SHORT_HEADER FALSE
292 #define DEFAULT_PROBATION 2
293 #define DEFAULT_UDP_RECONNECT TRUE
294 #define DEFAULT_MULTICAST_IFACE NULL
295 #define DEFAULT_NTP_SYNC FALSE
296 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
297 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
298 #define DEFAULT_TLS_DATABASE NULL
299 #define DEFAULT_TLS_INTERACTION NULL
300 #define DEFAULT_DO_RETRANSMISSION TRUE
301 #define DEFAULT_NTP_TIME_SOURCE NTP_TIME_SOURCE_NTP
302 #define DEFAULT_USER_AGENT "GStreamer/" PACKAGE_VERSION
303 #define DEFAULT_MAX_RTCP_RTP_TIME_DIFF 1000
304 #define DEFAULT_RFC7273_SYNC FALSE
305 #define DEFAULT_ADD_REFERENCE_TIMESTAMP_META FALSE
306 #define DEFAULT_MAX_TS_OFFSET_ADJUSTMENT G_GUINT64_CONSTANT(0)
307 #define DEFAULT_MAX_TS_OFFSET G_GINT64_CONSTANT(3000000000)
308 #define DEFAULT_VERSION GST_RTSP_VERSION_1_0
309 #define DEFAULT_BACKCHANNEL GST_RTSP_BACKCHANNEL_NONE
310 #define DEFAULT_TEARDOWN_TIMEOUT (100 * GST_MSECOND)
311 #define DEFAULT_ONVIF_MODE FALSE
312 #define DEFAULT_ONVIF_RATE_CONTROL TRUE
313 #define DEFAULT_IS_LIVE TRUE
314 #define DEFAULT_IGNORE_X_SERVER_REPLY FALSE
326 PROP_DROP_ON_LATENCY,
327 PROP_CONNECTION_SPEED,
330 PROP_DO_RTSP_KEEP_ALIVE,
339 PROP_UDP_BUFFER_SIZE,
343 PROP_MULTICAST_IFACE,
345 PROP_USE_PIPELINE_CLOCK,
347 PROP_TLS_VALIDATION_FLAGS,
349 PROP_TLS_INTERACTION,
350 PROP_DO_RETRANSMISSION,
351 PROP_NTP_TIME_SOURCE,
353 PROP_MAX_RTCP_RTP_TIME_DIFF,
355 PROP_ADD_REFERENCE_TIMESTAMP_META,
356 PROP_MAX_TS_OFFSET_ADJUSTMENT,
358 PROP_DEFAULT_VERSION,
360 PROP_TEARDOWN_TIMEOUT,
362 PROP_ONVIF_RATE_CONTROL,
364 PROP_IGNORE_X_SERVER_REPLY
367 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
369 gst_rtsp_nat_method_get_type (void)
371 static GType rtsp_nat_method_type = 0;
372 static const GEnumValue rtsp_nat_method[] = {
373 {GST_RTSP_NAT_NONE, "None", "none"},
374 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
378 if (!rtsp_nat_method_type) {
379 rtsp_nat_method_type =
380 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
382 return rtsp_nat_method_type;
385 #define RTSP_SRC_RESPONSE_ERROR(src, response_msg, err_cat, err_code, error_message) \
387 GST_ELEMENT_ERROR_WITH_DETAILS((src), err_cat, err_code, ("%s", error_message), \
388 ("%s (%d)", (response_msg)->type_data.response.reason, (response_msg)->type_data.response.code), \
389 ("rtsp-status-code", G_TYPE_UINT, (response_msg)->type_data.response.code, \
390 "rtsp-status-reason", G_TYPE_STRING, GST_STR_NULL((response_msg)->type_data.response.reason), NULL)); \
393 typedef struct _ParameterRequest
401 static void gst_rtspsrc_finalize (GObject * object);
403 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
404 const GValue * value, GParamSpec * pspec);
405 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
406 GValue * value, GParamSpec * pspec);
408 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
410 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
411 gpointer iface_data);
413 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
414 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
416 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
417 GstStateChange transition);
418 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
419 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
421 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
422 GstRTSPMessage * response);
424 static gboolean gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
426 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
427 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
429 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
430 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
431 gboolean async, const gchar * seek_style);
432 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
433 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
434 gboolean only_close);
436 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
437 const gchar * uri, GError ** error);
438 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
440 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
441 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
442 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
443 GstRTSPStream * stream, GstEvent * event);
444 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
445 static void gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush);
446 static GstRTSPResult gst_rtsp_conninfo_close (GstRTSPSrc * src,
447 GstRTSPConnInfo * info, gboolean free);
449 gst_rtspsrc_print_rtsp_message (GstRTSPSrc * src, const GstRTSPMessage * msg);
451 gst_rtspsrc_print_sdp_message (GstRTSPSrc * src, const GstSDPMessage * msg);
454 gst_rtspsrc_get_parameter (GstRTSPSrc * src, ParameterRequest * req);
457 gst_rtspsrc_set_parameter (GstRTSPSrc * src, ParameterRequest * req);
459 static gboolean get_parameter (GstRTSPSrc * src, const gchar * parameter,
460 const gchar * content_type, GstPromise * promise);
462 static gboolean get_parameters (GstRTSPSrc * src, gchar ** parameters,
463 const gchar * content_type, GstPromise * promise);
465 static gboolean set_parameter (GstRTSPSrc * src, const gchar * name,
466 const gchar * value, const gchar * content_type, GstPromise * promise);
468 static GstFlowReturn gst_rtspsrc_push_backchannel_buffer (GstRTSPSrc * src,
469 guint id, GstSample * sample);
471 static GstFlowReturn gst_rtspsrc_push_backchannel_sample (GstRTSPSrc * src,
472 guint id, GstSample * sample);
480 /* commands we send to out loop to notify it of events */
481 #define CMD_OPEN (1 << 0)
482 #define CMD_PLAY (1 << 1)
483 #define CMD_PAUSE (1 << 2)
484 #define CMD_CLOSE (1 << 3)
485 #define CMD_WAIT (1 << 4)
486 #define CMD_RECONNECT (1 << 5)
487 #define CMD_LOOP (1 << 6)
488 #define CMD_GET_PARAMETER (1 << 7)
489 #define CMD_SET_PARAMETER (1 << 8)
491 /* mask for all commands */
492 #define CMD_ALL ((CMD_SET_PARAMETER << 1) - 1)
494 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
496 gchar *__txt = _gst_element_error_printf text; \
497 gst_element_post_message (GST_ELEMENT_CAST (el), \
498 gst_message_new_progress (GST_OBJECT_CAST (el), \
499 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
503 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
505 #define gst_rtspsrc_parent_class parent_class
506 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
507 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
508 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtspsrc, "rtspsrc", GST_RANK_NONE,
509 GST_TYPE_RTSPSRC, rtsp_element_init (plugin));
511 #ifndef GST_DISABLE_GST_DEBUG
512 static inline const char *
513 cmd_to_string (guint cmd)
530 case CMD_GET_PARAMETER:
531 return "GET_PARAMETER";
532 case CMD_SET_PARAMETER:
533 return "SET_PARAMETER";
541 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
543 GST_DEBUG_OBJECT (src, "default handler");
548 select_stream_accum (GSignalInvocationHint * ihint,
549 GValue * return_accu, const GValue * handler_return, gpointer data)
553 myboolean = g_value_get_boolean (handler_return);
554 GST_DEBUG ("accum %d", myboolean);
555 g_value_set_boolean (return_accu, myboolean);
557 /* stop emission if FALSE */
562 default_before_send (GstRTSPSrc * src, GstRTSPMessage * msg)
564 GST_DEBUG_OBJECT (src, "default handler");
569 before_send_accum (GSignalInvocationHint * ihint,
570 GValue * return_accu, const GValue * handler_return, gpointer data)
574 myboolean = g_value_get_boolean (handler_return);
575 g_value_set_boolean (return_accu, myboolean);
577 /* prevent send if FALSE */
582 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
584 GObjectClass *gobject_class;
585 GstElementClass *gstelement_class;
586 GstBinClass *gstbin_class;
588 gobject_class = (GObjectClass *) klass;
589 gstelement_class = (GstElementClass *) klass;
590 gstbin_class = (GstBinClass *) klass;
592 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
594 gobject_class->set_property = gst_rtspsrc_set_property;
595 gobject_class->get_property = gst_rtspsrc_get_property;
597 gobject_class->finalize = gst_rtspsrc_finalize;
599 g_object_class_install_property (gobject_class, PROP_LOCATION,
600 g_param_spec_string ("location", "RTSP Location",
601 "Location of the RTSP url to read",
602 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
604 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
605 g_param_spec_flags ("protocols", "Protocols",
606 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
607 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
609 g_object_class_install_property (gobject_class, PROP_DEBUG,
610 g_param_spec_boolean ("debug", "Debug",
611 "Dump request and response messages to stdout"
612 "(DEPRECATED: Printed all RTSP message to gstreamer log as 'log' level)",
614 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
616 g_object_class_install_property (gobject_class, PROP_RETRY,
617 g_param_spec_uint ("retry", "Retry",
618 "Max number of retries when allocating RTP ports.",
619 0, G_MAXUINT16, DEFAULT_RETRY,
620 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
622 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
623 g_param_spec_uint64 ("timeout", "Timeout",
624 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
625 0, G_MAXUINT64, DEFAULT_TIMEOUT,
626 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
628 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
629 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
630 "Fail after timeout microseconds on TCP connections (0 = disabled)",
631 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
632 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
634 g_object_class_install_property (gobject_class, PROP_LATENCY,
635 g_param_spec_uint ("latency", "Buffer latency in ms",
636 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
637 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
639 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
640 g_param_spec_boolean ("drop-on-latency",
641 "Drop buffers when maximum latency is reached",
642 "Tells the jitterbuffer to never exceed the given latency in size",
643 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
645 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
646 g_param_spec_uint64 ("connection-speed", "Connection Speed",
647 "Network connection speed in kbps (0 = unknown)",
648 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
649 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
651 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
652 g_param_spec_enum ("nat-method", "NAT Method",
653 "Method to use for traversing firewalls and NAT",
654 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
655 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
658 * GstRTSPSrc:do-rtcp:
660 * Enable RTCP support. Some old server don't like RTCP and then this property
661 * needs to be set to FALSE.
663 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
664 g_param_spec_boolean ("do-rtcp", "Do RTCP",
665 "Send RTCP packets, disable for old incompatible server.",
666 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
669 * GstRTSPSrc:do-rtsp-keep-alive:
671 * Enable RTSP keep alive support. Some old server don't like RTSP
672 * keep alive and then this property needs to be set to FALSE.
674 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
675 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
676 "Send RTSP keep alive packets, disable for old incompatible server.",
677 DEFAULT_DO_RTSP_KEEP_ALIVE,
678 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
683 * Set the proxy parameters. This has to be a string of the format
684 * [http://][user:passwd@]host[:port].
686 g_object_class_install_property (gobject_class, PROP_PROXY,
687 g_param_spec_string ("proxy", "Proxy",
688 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
689 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
691 * GstRTSPSrc:proxy-id:
693 * Sets the proxy URI user id for authentication. If the URI set via the
694 * "proxy" property contains a user-id already, that will take precedence.
698 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
699 g_param_spec_string ("proxy-id", "proxy-id",
700 "HTTP proxy URI user id for authentication", "",
701 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
703 * GstRTSPSrc:proxy-pw:
705 * Sets the proxy URI password for authentication. If the URI set via the
706 * "proxy" property contains a password already, that will take precedence.
710 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
711 g_param_spec_string ("proxy-pw", "proxy-pw",
712 "HTTP proxy URI user password for authentication", "",
713 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
716 * GstRTSPSrc:rtp-blocksize:
718 * RTP package size to suggest to server.
720 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
721 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
722 "RTP package size to suggest to server (0 = disabled)",
723 0, 65536, DEFAULT_RTP_BLOCKSIZE,
724 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
726 g_object_class_install_property (gobject_class,
728 g_param_spec_string ("user-id", "user-id",
729 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
730 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
731 g_object_class_install_property (gobject_class, PROP_USER_PW,
732 g_param_spec_string ("user-pw", "user-pw",
733 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
734 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
737 * GstRTSPSrc:buffer-mode:
739 * Control the buffering and timestamping mode used by the jitterbuffer.
741 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
742 g_param_spec_enum ("buffer-mode", "Buffer Mode",
743 "Control the buffering algorithm in use",
744 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
745 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
748 * GstRTSPSrc:port-range:
750 * Configure the client port numbers that can be used to receive RTP and
753 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
754 g_param_spec_string ("port-range", "Port range",
755 "Client port range that can be used to receive RTP and RTCP data, "
756 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
757 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
760 * GstRTSPSrc:udp-buffer-size:
762 * Size of the kernel UDP receive buffer in bytes.
764 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
765 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
766 "Size of the kernel UDP receive buffer in bytes, 0=default",
767 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
768 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
771 * GstRTSPSrc:short-header:
773 * Only send the basic RTSP headers for broken encoders.
775 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
776 g_param_spec_boolean ("short-header", "Short Header",
777 "Only send the basic RTSP headers for broken encoders",
778 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
780 g_object_class_install_property (gobject_class, PROP_PROBATION,
781 g_param_spec_uint ("probation", "Number of probations",
782 "Consecutive packet sequence numbers to accept the source",
783 0, G_MAXUINT, DEFAULT_PROBATION,
784 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
786 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
787 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
788 "Reconnect to the server if RTSP connection is closed when doing UDP",
789 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
791 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
792 g_param_spec_string ("multicast-iface", "Multicast Interface",
793 "The network interface on which to join the multicast group",
794 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
796 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
797 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
798 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
799 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
801 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
802 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
803 "Use the pipeline running-time to set the NTP time in the RTCP SR messages"
804 "(DEPRECATED: Use ntp-time-source property)",
805 DEFAULT_USE_PIPELINE_CLOCK,
806 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
808 g_object_class_install_property (gobject_class, PROP_SDES,
809 g_param_spec_boxed ("sdes", "SDES",
810 "The SDES items of this session",
811 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
814 * GstRTSPSrc::tls-validation-flags:
816 * TLS certificate validation flags used to validate server
819 * GLib guarantees that if certificate verification fails, at least one
820 * error will be set, but it does not guarantee that all possible errors
821 * will be set. Accordingly, you may not safely decide to ignore any
822 * particular type of error.
824 * For example, it would be incorrect to mask %G_TLS_CERTIFICATE_EXPIRED if
825 * you want to allow expired certificates, because this could potentially be
826 * the only error flag set even if other problems exist with the
831 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
832 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
833 "TLS certificate validation flags used to validate the server certificate",
834 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
835 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
838 * GstRTSPSrc::tls-database:
840 * TLS database with anchor certificate authorities used to validate
841 * the server certificate.
845 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
846 g_param_spec_object ("tls-database", "TLS database",
847 "TLS database with anchor certificate authorities used to validate the server certificate",
848 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
851 * GstRTSPSrc::tls-interaction:
853 * A #GTlsInteraction object to be used when the connection or certificate
854 * database need to interact with the user. This will be used to prompt the
855 * user for passwords where necessary.
859 g_object_class_install_property (gobject_class, PROP_TLS_INTERACTION,
860 g_param_spec_object ("tls-interaction", "TLS interaction",
861 "A GTlsInteraction object to prompt the user for password or certificate",
862 G_TYPE_TLS_INTERACTION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
865 * GstRTSPSrc::do-retransmission:
867 * Attempt to ask the server to retransmit lost packets according to RFC4588.
869 * Note: currently only works with SSRC-multiplexed retransmission streams
873 g_object_class_install_property (gobject_class, PROP_DO_RETRANSMISSION,
874 g_param_spec_boolean ("do-retransmission", "Retransmission",
875 "Ask the server to retransmit lost packets",
876 DEFAULT_DO_RETRANSMISSION,
877 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
880 * GstRTSPSrc::ntp-time-source:
882 * allows to select the time source that should be used
883 * for the NTP time in RTCP packets
887 g_object_class_install_property (gobject_class, PROP_NTP_TIME_SOURCE,
888 g_param_spec_enum ("ntp-time-source", "NTP Time Source",
889 "NTP time source for RTCP packets",
890 GST_TYPE_RTSP_SRC_NTP_TIME_SOURCE, DEFAULT_NTP_TIME_SOURCE,
891 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
894 * GstRTSPSrc::user-agent:
896 * The string to set in the User-Agent header.
900 g_object_class_install_property (gobject_class, PROP_USER_AGENT,
901 g_param_spec_string ("user-agent", "User Agent",
902 "The User-Agent string to send to the server",
903 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
905 g_object_class_install_property (gobject_class, PROP_MAX_RTCP_RTP_TIME_DIFF,
906 g_param_spec_int ("max-rtcp-rtp-time-diff", "Max RTCP RTP Time Diff",
907 "Maximum amount of time in ms that the RTP time in RTCP SRs "
908 "is allowed to be ahead (-1 disabled)", -1, G_MAXINT,
909 DEFAULT_MAX_RTCP_RTP_TIME_DIFF,
910 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
912 g_object_class_install_property (gobject_class, PROP_RFC7273_SYNC,
913 g_param_spec_boolean ("rfc7273-sync", "Sync on RFC7273 clock",
914 "Synchronize received streams to the RFC7273 clock "
915 "(requires clock and offset to be provided)", DEFAULT_RFC7273_SYNC,
916 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
919 * GstRTSPSrc:add-reference-timestamp-meta:
921 * When syncing to a RFC7273 clock, add #GstReferenceTimestampMeta
922 * to buffers with the original reconstructed reference clock timestamp.
926 g_object_class_install_property (gobject_class,
927 PROP_ADD_REFERENCE_TIMESTAMP_META,
928 g_param_spec_boolean ("add-reference-timestamp-meta",
929 "Add Reference Timestamp Meta",
930 "Add Reference Timestamp Meta to buffers with the original clock timestamp "
931 "before any adjustments when syncing to an RFC7273 clock.",
932 DEFAULT_ADD_REFERENCE_TIMESTAMP_META,
933 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
936 * GstRTSPSrc:default-rtsp-version:
938 * The preferred RTSP version to use while negotiating the version with the server.
942 g_object_class_install_property (gobject_class, PROP_DEFAULT_VERSION,
943 g_param_spec_enum ("default-rtsp-version",
944 "The RTSP version to try first",
945 "The RTSP version that should be tried first when negotiating version.",
946 GST_TYPE_RTSP_VERSION, DEFAULT_VERSION,
947 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
950 * GstRTSPSrc:max-ts-offset-adjustment:
952 * Syncing time stamps to NTP time adds a time offset. This parameter
953 * specifies the maximum number of nanoseconds per frame that this time offset
954 * may be adjusted with. This is used to avoid sudden large changes to time
957 g_object_class_install_property (gobject_class, PROP_MAX_TS_OFFSET_ADJUSTMENT,
958 g_param_spec_uint64 ("max-ts-offset-adjustment",
959 "Max Timestamp Offset Adjustment",
960 "The maximum number of nanoseconds per frame that time stamp offsets "
961 "may be adjusted (0 = no limit).", 0, G_MAXUINT64,
962 DEFAULT_MAX_TS_OFFSET_ADJUSTMENT, G_PARAM_READWRITE |
963 G_PARAM_STATIC_STRINGS));
966 * GstRTSPSrc:max-ts-offset:
968 * Used to set an upper limit of how large a time offset may be. This
969 * is used to protect against unrealistic values as a result of either
970 * client,server or clock issues.
972 g_object_class_install_property (gobject_class, PROP_MAX_TS_OFFSET,
973 g_param_spec_int64 ("max-ts-offset", "Max TS Offset",
974 "The maximum absolute value of the time offset in (nanoseconds). "
975 "Note, if the ntp-sync parameter is set the default value is "
976 "changed to 0 (no limit)", 0, G_MAXINT64, DEFAULT_MAX_TS_OFFSET,
977 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
980 * GstRTSPSrc:backchannel
982 * Select a type of backchannel to setup with the RTSP server.
983 * Default value is "none". Allowed values are "none" and "onvif".
987 g_object_class_install_property (gobject_class, PROP_BACKCHANNEL,
988 g_param_spec_enum ("backchannel", "Backchannel type",
989 "The type of backchannel to setup. Default is 'none'.",
990 GST_TYPE_RTSP_BACKCHANNEL, BACKCHANNEL_NONE,
991 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
994 * GstRTSPSrc:teardown-timeout
996 * When transitioning PAUSED-READY, allow up to timeout (in nanoseconds)
997 * delay in order to send teardown (0 = disabled)
1001 g_object_class_install_property (gobject_class, PROP_TEARDOWN_TIMEOUT,
1002 g_param_spec_uint64 ("teardown-timeout", "Teardown Timeout",
1003 "When transitioning PAUSED-READY, allow up to timeout (in nanoseconds) "
1004 "delay in order to send teardown (0 = disabled)",
1005 0, G_MAXUINT64, DEFAULT_TEARDOWN_TIMEOUT,
1006 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1009 * GstRTSPSrc:onvif-mode
1011 * Act as an ONVIF client. When set to %TRUE:
1013 * - seeks will be interpreted as nanoseconds since prime epoch (1900-01-01)
1015 * - #GstRTSPSrc:onvif-rate-control can be used to request that the server sends
1016 * data as fast as it can
1018 * - TCP is picked as the transport protocol
1020 * - Trickmode flags in seek events are transformed into the appropriate ONVIF
1025 g_object_class_install_property (gobject_class, PROP_ONVIF_MODE,
1026 g_param_spec_boolean ("onvif-mode", "Onvif Mode",
1027 "Act as an ONVIF client",
1028 DEFAULT_ONVIF_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1031 * GstRTSPSrc:onvif-rate-control
1033 * When in onvif-mode, whether to set Rate-Control to yes or no. When set
1034 * to %FALSE, the server will deliver data as fast as the client can consume
1039 g_object_class_install_property (gobject_class, PROP_ONVIF_RATE_CONTROL,
1040 g_param_spec_boolean ("onvif-rate-control", "Onvif Rate Control",
1041 "When in onvif-mode, whether to set Rate-Control to yes or no",
1042 DEFAULT_ONVIF_RATE_CONTROL,
1043 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1046 * GstRTSPSrc:is-live
1048 * Whether to act as a live source. This is useful in combination with
1049 * #GstRTSPSrc:onvif-rate-control set to %FALSE and usage of the TCP
1050 * protocol. In that situation, data delivery rate can be entirely
1051 * controlled from the client side, enabling features such as frame
1052 * stepping and instantaneous rate changes.
1056 g_object_class_install_property (gobject_class, PROP_IS_LIVE,
1057 g_param_spec_boolean ("is-live", "Is live",
1058 "Whether to act as a live source",
1059 DEFAULT_IS_LIVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1062 * GstRTSPSrc:ignore-x-server-reply
1064 * When connecting to an RTSP server in tunneled mode (HTTP) the server
1065 * usually replies with an x-server-ip-address header. This contains the
1066 * address of the intended streaming server. However some servers return an
1067 * "invalid" address. Here follows two examples when it might happen.
1069 * 1. A server uses Apache combined with a separate RTSP process to handle
1070 * HTTPS requests on port 443. In this case Apache handles TLS and
1071 * connects to the local RTSP server, which results in a local
1072 * address 127.0.0.1 or ::1 in the header reply. This address is
1073 * returned to the actual RTSP client in the header. The client will
1074 * receive this address and try to connect to it and fail.
1076 * 2. The client uses an IPv6 link local address with a specified scope id
1077 * fe80::aaaa:bbbb:cccc:dddd%eth0 and connects via HTTP on port 80.
1078 * The RTSP server receives the connection and returns the address
1079 * in the x-server-ip-address header. The client will receive this
1080 * address and try to connect to it "as is" without the scope id and
1083 * In the case of streaming data from RTSP servers like 1 and 2, it's
1084 * useful to have the option to simply ignore the x-server-ip-address
1085 * header reply and continue using the original address.
1089 g_object_class_install_property (gobject_class, PROP_IGNORE_X_SERVER_REPLY,
1090 g_param_spec_boolean ("ignore-x-server-reply",
1091 "Ignore x-server-ip-address",
1092 "Whether to ignore the x-server-ip-address server header reply",
1093 DEFAULT_IGNORE_X_SERVER_REPLY,
1094 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1097 * GstRTSPSrc::handle-request:
1098 * @rtspsrc: a #GstRTSPSrc
1099 * @request: a #GstRTSPMessage
1100 * @response: a #GstRTSPMessage
1102 * Handle a server request in @request and prepare @response.
1104 * This signal is called from the streaming thread, you should therefore not
1105 * do any state changes on @rtspsrc because this might deadlock. If you want
1106 * to modify the state as a result of this signal, post a
1107 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
1108 * in some other way.
1112 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
1113 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
1114 0, NULL, NULL, NULL, G_TYPE_NONE, 2,
1115 GST_TYPE_RTSP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE,
1116 GST_TYPE_RTSP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
1119 * GstRTSPSrc::on-sdp:
1120 * @rtspsrc: a #GstRTSPSrc
1121 * @sdp: a #GstSDPMessage
1123 * Emitted when the client has retrieved the SDP and before it configures the
1124 * streams in the SDP. @sdp can be inspected and modified.
1126 * This signal is called from the streaming thread, you should therefore not
1127 * do any state changes on @rtspsrc because this might deadlock. If you want
1128 * to modify the state as a result of this signal, post a
1129 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
1130 * in some other way.
1134 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
1135 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
1136 0, NULL, NULL, NULL, G_TYPE_NONE, 1,
1137 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
1140 * GstRTSPSrc::select-stream:
1141 * @rtspsrc: a #GstRTSPSrc
1142 * @num: the stream number
1143 * @caps: the stream caps
1145 * Emitted before the client decides to configure the stream @num with
1148 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
1153 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
1154 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
1156 (GCallback) default_select_stream, select_stream_accum, NULL, NULL,
1157 G_TYPE_BOOLEAN, 2, G_TYPE_UINT, GST_TYPE_CAPS);
1159 * GstRTSPSrc::new-manager:
1160 * @rtspsrc: a #GstRTSPSrc
1161 * @manager: a #GstElement
1163 * Emitted after a new manager (like rtpbin) was created and the default
1164 * properties were configured.
1168 gst_rtspsrc_signals[SIGNAL_NEW_MANAGER] =
1169 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
1170 0, 0, NULL, NULL, NULL, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
1173 * GstRTSPSrc::request-rtcp-key:
1174 * @rtspsrc: a #GstRTSPSrc
1175 * @num: the stream number
1177 * Signal emitted to get the crypto parameters relevant to the RTCP
1178 * stream. User should provide the key and the RTCP encryption ciphers
1179 * and authentication, and return them wrapped in a GstCaps.
1183 gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY] =
1184 g_signal_new ("request-rtcp-key", G_TYPE_FROM_CLASS (klass),
1185 0, 0, NULL, NULL, NULL, GST_TYPE_CAPS, 1, G_TYPE_UINT);
1188 * GstRTSPSrc::accept-certificate:
1189 * @rtspsrc: a #GstRTSPSrc
1190 * @peer_cert: the peer's #GTlsCertificate
1191 * @errors: the problems with @peer_cert
1192 * @user_data: user data set when the signal handler was connected.
1194 * This will directly map to #GTlsConnection 's "accept-certificate"
1195 * signal and be performed after the default checks of #GstRTSPConnection
1196 * (checking against the #GTlsDatabase with the given #GTlsCertificateFlags)
1197 * have failed. If no #GTlsDatabase is set on this connection, only this
1198 * signal will be emitted.
1202 gst_rtspsrc_signals[SIGNAL_ACCEPT_CERTIFICATE] =
1203 g_signal_new ("accept-certificate", G_TYPE_FROM_CLASS (klass),
1204 G_SIGNAL_RUN_LAST, 0, g_signal_accumulator_true_handled, NULL, NULL,
1205 G_TYPE_BOOLEAN, 3, G_TYPE_TLS_CONNECTION, G_TYPE_TLS_CERTIFICATE,
1206 G_TYPE_TLS_CERTIFICATE_FLAGS);
1209 * GstRTSPSrc::before-send:
1210 * @rtspsrc: a #GstRTSPSrc
1211 * @num: the stream number
1213 * Emitted before each RTSP request is sent, in order to allow
1214 * the application to modify send parameters or to skip the message entirely.
1215 * This can be used, for example, to work with ONVIF Profile G servers,
1216 * which need a different/additional range, rate-control, and intra/x
1219 * Returns: %TRUE when the command should be sent, %FALSE when the
1220 * command should be dropped.
1224 gst_rtspsrc_signals[SIGNAL_BEFORE_SEND] =
1225 g_signal_new_class_handler ("before-send", G_TYPE_FROM_CLASS (klass),
1227 (GCallback) default_before_send, before_send_accum, NULL, NULL,
1228 G_TYPE_BOOLEAN, 1, GST_TYPE_RTSP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
1231 * GstRTSPSrc::push-backchannel-buffer:
1232 * @rtspsrc: a #GstRTSPSrc
1233 * @id: stream ID where the sample should be sent
1234 * @sample: RTP sample to send back
1236 * Deprecated: 1.22: Use action signal GstRTSPSrc::push-backchannel-sample instead.
1237 * IMPORTANT: Please note that this signal decrements the reference count
1238 * of sample internally! So it cannot be used from other
1239 * language bindings in general.
1242 gst_rtspsrc_signals[SIGNAL_PUSH_BACKCHANNEL_BUFFER] =
1243 g_signal_new ("push-backchannel-buffer", G_TYPE_FROM_CLASS (klass),
1244 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION | G_SIGNAL_DEPRECATED,
1245 G_STRUCT_OFFSET (GstRTSPSrcClass, push_backchannel_buffer), NULL, NULL,
1246 NULL, GST_TYPE_FLOW_RETURN, 2, G_TYPE_UINT, GST_TYPE_SAMPLE);
1249 * GstRTSPSrc::push-backchannel-sample:
1250 * @rtspsrc: a #GstRTSPSrc
1251 * @id: stream ID where the sample should be sent
1252 * @sample: RTP sample to send back
1256 gst_rtspsrc_signals[SIGNAL_PUSH_BACKCHANNEL_SAMPLE] =
1257 g_signal_new ("push-backchannel-sample", G_TYPE_FROM_CLASS (klass),
1258 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION | G_SIGNAL_DEPRECATED,
1259 G_STRUCT_OFFSET (GstRTSPSrcClass, push_backchannel_buffer), NULL, NULL,
1260 NULL, GST_TYPE_FLOW_RETURN, 2, G_TYPE_UINT, GST_TYPE_SAMPLE);
1263 * GstRTSPSrc::get-parameter:
1264 * @rtspsrc: a #GstRTSPSrc
1265 * @parameter: the parameter name
1266 * @parameter: the content type
1267 * @parameter: a pointer to #GstPromise
1269 * Handle the GET_PARAMETER signal.
1271 * Returns: %TRUE when the command could be issued, %FALSE otherwise
1274 gst_rtspsrc_signals[SIGNAL_GET_PARAMETER] =
1275 g_signal_new ("get-parameter", G_TYPE_FROM_CLASS (klass),
1276 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRTSPSrcClass,
1277 get_parameter), NULL, NULL, NULL,
1278 G_TYPE_BOOLEAN, 3, G_TYPE_STRING, G_TYPE_STRING, GST_TYPE_PROMISE);
1281 * GstRTSPSrc::get-parameters:
1282 * @rtspsrc: a #GstRTSPSrc
1283 * @parameter: a NULL-terminated array of parameters
1284 * @parameter: the content type
1285 * @parameter: a pointer to #GstPromise
1287 * Handle the GET_PARAMETERS signal.
1289 * Returns: %TRUE when the command could be issued, %FALSE otherwise
1292 gst_rtspsrc_signals[SIGNAL_GET_PARAMETERS] =
1293 g_signal_new ("get-parameters", G_TYPE_FROM_CLASS (klass),
1294 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRTSPSrcClass,
1295 get_parameters), NULL, NULL, NULL,
1296 G_TYPE_BOOLEAN, 3, G_TYPE_STRV, G_TYPE_STRING, GST_TYPE_PROMISE);
1299 * GstRTSPSrc::set-parameter:
1300 * @rtspsrc: a #GstRTSPSrc
1301 * @parameter: the parameter name
1302 * @parameter: the parameter value
1303 * @parameter: the content type
1304 * @parameter: a pointer to #GstPromise
1306 * Handle the SET_PARAMETER signal.
1308 * Returns: %TRUE when the command could be issued, %FALSE otherwise
1311 gst_rtspsrc_signals[SIGNAL_SET_PARAMETER] =
1312 g_signal_new ("set-parameter", G_TYPE_FROM_CLASS (klass),
1313 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRTSPSrcClass,
1314 set_parameter), NULL, NULL, NULL, G_TYPE_BOOLEAN, 4, G_TYPE_STRING,
1315 G_TYPE_STRING, G_TYPE_STRING, GST_TYPE_PROMISE);
1317 gstelement_class->send_event = gst_rtspsrc_send_event;
1318 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
1319 gstelement_class->change_state = gst_rtspsrc_change_state;
1321 gst_element_class_add_static_pad_template (gstelement_class, &rtptemplate);
1323 gst_element_class_set_static_metadata (gstelement_class,
1324 "RTSP packet receiver", "Source/Network",
1325 "Receive data over the network via RTSP (RFC 2326)",
1326 "Wim Taymans <wim@fluendo.com>, "
1327 "Thijs Vermeir <thijs.vermeir@barco.com>, "
1328 "Lutz Mueller <lutz@topfrose.de>");
1330 gstbin_class->handle_message = gst_rtspsrc_handle_message;
1332 klass->push_backchannel_buffer = gst_rtspsrc_push_backchannel_buffer;
1333 klass->push_backchannel_sample = gst_rtspsrc_push_backchannel_sample;
1334 klass->get_parameter = GST_DEBUG_FUNCPTR (get_parameter);
1335 klass->get_parameters = GST_DEBUG_FUNCPTR (get_parameters);
1336 klass->set_parameter = GST_DEBUG_FUNCPTR (set_parameter);
1338 gst_rtsp_ext_list_init ();
1340 gst_type_mark_as_plugin_api (GST_TYPE_RTSP_SRC_TIMEOUT_CAUSE, 0);
1341 gst_type_mark_as_plugin_api (GST_TYPE_RTSP_SRC_BUFFER_MODE, 0);
1342 gst_type_mark_as_plugin_api (GST_TYPE_RTSP_SRC_NTP_TIME_SOURCE, 0);
1343 gst_type_mark_as_plugin_api (GST_TYPE_RTSP_BACKCHANNEL, 0);
1344 gst_type_mark_as_plugin_api (GST_TYPE_RTSP_NAT_METHOD, 0);
1348 validate_set_get_parameter_name (const gchar * parameter_name)
1350 gchar *ptr = (gchar *) parameter_name;
1353 /* Don't allow '\r', '\n', \'t', ' ' etc in the parameter name */
1354 if (g_ascii_isspace (*ptr) || g_ascii_iscntrl (*ptr)) {
1355 GST_DEBUG ("invalid parameter name '%s'", parameter_name);
1364 validate_set_get_parameters (gchar ** parameter_names)
1366 while (*parameter_names) {
1367 if (!validate_set_get_parameter_name (*parameter_names)) {
1376 get_parameter (GstRTSPSrc * src, const gchar * parameter,
1377 const gchar * content_type, GstPromise * promise)
1379 gchar *parameters[] = { (gchar *) parameter, NULL };
1381 GST_LOG_OBJECT (src, "get_parameter: %s", GST_STR_NULL (parameter));
1383 if (parameter == NULL || parameter[0] == '\0' || promise == NULL) {
1384 GST_DEBUG ("invalid input");
1388 return get_parameters (src, parameters, content_type, promise);
1392 get_parameters (GstRTSPSrc * src, gchar ** parameters,
1393 const gchar * content_type, GstPromise * promise)
1395 ParameterRequest *req;
1397 GST_LOG_OBJECT (src, "get_parameters: %d", g_strv_length (parameters));
1399 if (parameters == NULL || promise == NULL) {
1400 GST_DEBUG ("invalid input");
1404 if (src->state == GST_RTSP_STATE_INVALID) {
1405 GST_DEBUG ("invalid state");
1409 if (!validate_set_get_parameters (parameters)) {
1413 req = g_new0 (ParameterRequest, 1);
1414 req->promise = gst_promise_ref (promise);
1415 req->cmd = CMD_GET_PARAMETER;
1416 /* Set the request body according to RFC 2326 or RFC 7826 */
1417 req->body = g_string_new (NULL);
1418 while (*parameters) {
1419 g_string_append_printf (req->body, "%s:\r\n", *parameters);
1423 req->content_type = g_strdup (content_type);
1425 GST_OBJECT_LOCK (src);
1426 g_queue_push_tail (&src->set_get_param_q, req);
1427 GST_OBJECT_UNLOCK (src);
1429 gst_rtspsrc_loop_send_cmd (src, CMD_GET_PARAMETER, CMD_LOOP);
1435 set_parameter (GstRTSPSrc * src, const gchar * name, const gchar * value,
1436 const gchar * content_type, GstPromise * promise)
1438 ParameterRequest *req;
1440 GST_LOG_OBJECT (src, "set_parameter: %s: %s", GST_STR_NULL (name),
1441 GST_STR_NULL (value));
1443 if (name == NULL || name[0] == '\0' || value == NULL || promise == NULL) {
1444 GST_DEBUG ("invalid input");
1448 if (src->state == GST_RTSP_STATE_INVALID) {
1449 GST_DEBUG ("invalid state");
1453 if (!validate_set_get_parameter_name (name)) {
1457 req = g_new0 (ParameterRequest, 1);
1458 req->cmd = CMD_SET_PARAMETER;
1459 req->promise = gst_promise_ref (promise);
1460 req->body = g_string_new (NULL);
1461 /* Set the request body according to RFC 2326 or RFC 7826 */
1462 g_string_append_printf (req->body, "%s: %s\r\n", name, value);
1464 req->content_type = g_strdup (content_type);
1466 GST_OBJECT_LOCK (src);
1467 g_queue_push_tail (&src->set_get_param_q, req);
1468 GST_OBJECT_UNLOCK (src);
1470 gst_rtspsrc_loop_send_cmd (src, CMD_SET_PARAMETER, CMD_LOOP);
1476 gst_rtspsrc_init (GstRTSPSrc * src)
1478 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
1479 src->protocols = DEFAULT_PROTOCOLS;
1480 src->debug = DEFAULT_DEBUG;
1481 src->retry = DEFAULT_RETRY;
1482 src->udp_timeout = DEFAULT_TIMEOUT;
1483 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
1484 src->latency = DEFAULT_LATENCY_MS;
1485 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
1486 src->connection_speed = DEFAULT_CONNECTION_SPEED;
1487 src->nat_method = DEFAULT_NAT_METHOD;
1488 src->do_rtcp = DEFAULT_DO_RTCP;
1489 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
1490 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
1491 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
1492 src->user_id = g_strdup (DEFAULT_USER_ID);
1493 src->user_pw = g_strdup (DEFAULT_USER_PW);
1494 src->buffer_mode = DEFAULT_BUFFER_MODE;
1495 src->client_port_range.min = 0;
1496 src->client_port_range.max = 0;
1497 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
1498 src->short_header = DEFAULT_SHORT_HEADER;
1499 src->probation = DEFAULT_PROBATION;
1500 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
1501 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1502 src->ntp_sync = DEFAULT_NTP_SYNC;
1503 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
1505 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
1506 src->tls_database = DEFAULT_TLS_DATABASE;
1507 src->tls_interaction = DEFAULT_TLS_INTERACTION;
1508 src->do_retransmission = DEFAULT_DO_RETRANSMISSION;
1509 src->ntp_time_source = DEFAULT_NTP_TIME_SOURCE;
1510 src->user_agent = g_strdup (DEFAULT_USER_AGENT);
1511 src->max_rtcp_rtp_time_diff = DEFAULT_MAX_RTCP_RTP_TIME_DIFF;
1512 src->rfc7273_sync = DEFAULT_RFC7273_SYNC;
1513 src->add_reference_timestamp_meta = DEFAULT_ADD_REFERENCE_TIMESTAMP_META;
1514 src->max_ts_offset_adjustment = DEFAULT_MAX_TS_OFFSET_ADJUSTMENT;
1515 src->max_ts_offset = DEFAULT_MAX_TS_OFFSET;
1516 src->max_ts_offset_is_set = FALSE;
1517 src->default_version = DEFAULT_VERSION;
1518 src->version = GST_RTSP_VERSION_INVALID;
1519 src->teardown_timeout = DEFAULT_TEARDOWN_TIMEOUT;
1520 src->onvif_mode = DEFAULT_ONVIF_MODE;
1521 src->onvif_rate_control = DEFAULT_ONVIF_RATE_CONTROL;
1522 src->is_live = DEFAULT_IS_LIVE;
1523 src->seek_seqnum = GST_SEQNUM_INVALID;
1524 src->group_id = GST_GROUP_ID_INVALID;
1526 /* get a list of all extensions */
1527 src->extensions = gst_rtsp_ext_list_get ();
1529 /* connect to send signal */
1530 gst_rtsp_ext_list_connect (src->extensions, "send",
1531 (GCallback) gst_rtspsrc_send_cb, src);
1533 /* protects the streaming thread in interleaved mode or the polling
1534 * thread in UDP mode. */
1535 g_rec_mutex_init (&src->stream_rec_lock);
1537 /* protects our state changes from multiple invocations */
1538 g_rec_mutex_init (&src->state_rec_lock);
1540 g_queue_init (&src->set_get_param_q);
1542 src->state = GST_RTSP_STATE_INVALID;
1544 g_mutex_init (&src->conninfo.send_lock);
1545 g_mutex_init (&src->conninfo.recv_lock);
1546 g_cond_init (&src->cmd_cond);
1548 g_mutex_init (&src->group_lock);
1550 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
1551 gst_bin_set_suppressed_flags (GST_BIN (src),
1552 GST_ELEMENT_FLAG_SOURCE | GST_ELEMENT_FLAG_SINK);
1556 free_param_data (ParameterRequest * req)
1558 gst_promise_unref (req->promise);
1560 g_string_free (req->body, TRUE);
1561 g_free (req->content_type);
1566 gst_rtspsrc_finalize (GObject * object)
1568 GstRTSPSrc *rtspsrc;
1570 rtspsrc = GST_RTSPSRC (object);
1572 gst_rtsp_ext_list_free (rtspsrc->extensions);
1573 g_free (rtspsrc->conninfo.location);
1574 gst_rtsp_url_free (rtspsrc->conninfo.url);
1575 g_free (rtspsrc->conninfo.url_str);
1576 g_free (rtspsrc->user_id);
1577 g_free (rtspsrc->user_pw);
1578 g_free (rtspsrc->multi_iface);
1579 g_free (rtspsrc->user_agent);
1582 gst_sdp_message_free (rtspsrc->sdp);
1583 rtspsrc->sdp = NULL;
1585 if (rtspsrc->provided_clock)
1586 gst_object_unref (rtspsrc->provided_clock);
1589 gst_structure_free (rtspsrc->sdes);
1591 if (rtspsrc->tls_database)
1592 g_object_unref (rtspsrc->tls_database);
1594 if (rtspsrc->tls_interaction)
1595 g_object_unref (rtspsrc->tls_interaction);
1598 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
1599 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
1601 g_mutex_clear (&rtspsrc->conninfo.send_lock);
1602 g_mutex_clear (&rtspsrc->conninfo.recv_lock);
1603 g_cond_clear (&rtspsrc->cmd_cond);
1605 g_mutex_clear (&rtspsrc->group_lock);
1607 G_OBJECT_CLASS (parent_class)->finalize (object);
1611 gst_rtspsrc_provide_clock (GstElement * element)
1613 GstRTSPSrc *src = GST_RTSPSRC (element);
1616 if ((clock = src->provided_clock) != NULL)
1617 return gst_object_ref (clock);
1619 return GST_ELEMENT_CLASS (parent_class)->provide_clock (element);
1622 /* a proxy string of the format [user:passwd@]host[:port] */
1624 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
1626 gchar *p, *at, *col;
1628 g_free (rtsp->proxy_user);
1629 rtsp->proxy_user = NULL;
1630 g_free (rtsp->proxy_passwd);
1631 rtsp->proxy_passwd = NULL;
1632 g_free (rtsp->proxy_host);
1633 rtsp->proxy_host = NULL;
1634 rtsp->proxy_port = 0;
1636 p = (gchar *) proxy;
1641 /* we allow http:// in front but ignore it */
1642 if (g_str_has_prefix (p, "http://"))
1645 at = strchr (p, '@');
1647 /* look for user:passwd */
1648 col = strchr (proxy, ':');
1649 if (col == NULL || col > at)
1652 rtsp->proxy_user = g_strndup (p, col - p);
1654 rtsp->proxy_passwd = g_strndup (col, at - col);
1659 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
1660 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
1661 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
1662 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
1663 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
1664 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
1665 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
1668 col = strchr (p, ':');
1671 /* everything before the colon is the hostname */
1672 rtsp->proxy_host = g_strndup (p, col - p);
1674 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
1676 rtsp->proxy_host = g_strdup (p);
1677 rtsp->proxy_port = 8080;
1683 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
1685 rtspsrc->tcp_timeout = timeout;
1689 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
1692 GstRTSPSrc *rtspsrc;
1694 rtspsrc = GST_RTSPSRC (object);
1698 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
1699 g_value_get_string (value), NULL);
1701 case PROP_PROTOCOLS:
1702 rtspsrc->protocols = g_value_get_flags (value);
1705 rtspsrc->debug = g_value_get_boolean (value);
1708 rtspsrc->retry = g_value_get_uint (value);
1711 rtspsrc->udp_timeout = g_value_get_uint64 (value);
1713 case PROP_TCP_TIMEOUT:
1714 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
1717 rtspsrc->latency = g_value_get_uint (value);
1719 case PROP_DROP_ON_LATENCY:
1720 rtspsrc->drop_on_latency = g_value_get_boolean (value);
1722 case PROP_CONNECTION_SPEED:
1723 rtspsrc->connection_speed = g_value_get_uint64 (value);
1725 case PROP_NAT_METHOD:
1726 rtspsrc->nat_method = g_value_get_enum (value);
1729 rtspsrc->do_rtcp = g_value_get_boolean (value);
1731 case PROP_DO_RTSP_KEEP_ALIVE:
1732 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
1735 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
1738 g_free (rtspsrc->prop_proxy_id);
1739 rtspsrc->prop_proxy_id = g_value_dup_string (value);
1742 g_free (rtspsrc->prop_proxy_pw);
1743 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
1745 case PROP_RTP_BLOCKSIZE:
1746 rtspsrc->rtp_blocksize = g_value_get_uint (value);
1749 g_free (rtspsrc->user_id);
1750 rtspsrc->user_id = g_value_dup_string (value);
1753 g_free (rtspsrc->user_pw);
1754 rtspsrc->user_pw = g_value_dup_string (value);
1756 case PROP_BUFFER_MODE:
1757 rtspsrc->buffer_mode = g_value_get_enum (value);
1759 case PROP_PORT_RANGE:
1763 str = g_value_get_string (value);
1764 if (str == NULL || sscanf (str, "%u-%u", &rtspsrc->client_port_range.min,
1765 &rtspsrc->client_port_range.max) != 2) {
1766 rtspsrc->client_port_range.min = 0;
1767 rtspsrc->client_port_range.max = 0;
1771 case PROP_UDP_BUFFER_SIZE:
1772 rtspsrc->udp_buffer_size = g_value_get_int (value);
1774 case PROP_SHORT_HEADER:
1775 rtspsrc->short_header = g_value_get_boolean (value);
1777 case PROP_PROBATION:
1778 rtspsrc->probation = g_value_get_uint (value);
1780 case PROP_UDP_RECONNECT:
1781 rtspsrc->udp_reconnect = g_value_get_boolean (value);
1783 case PROP_MULTICAST_IFACE:
1784 g_free (rtspsrc->multi_iface);
1786 if (g_value_get_string (value) == NULL)
1787 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1789 rtspsrc->multi_iface = g_value_dup_string (value);
1792 rtspsrc->ntp_sync = g_value_get_boolean (value);
1793 /* The default value of max_ts_offset depends on ntp_sync. If user
1794 * hasn't set it then change default value */
1795 if (!rtspsrc->max_ts_offset_is_set) {
1796 if (rtspsrc->ntp_sync) {
1797 rtspsrc->max_ts_offset = 0;
1799 rtspsrc->max_ts_offset = DEFAULT_MAX_TS_OFFSET;
1803 case PROP_USE_PIPELINE_CLOCK:
1804 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
1807 rtspsrc->sdes = g_value_dup_boxed (value);
1809 case PROP_TLS_VALIDATION_FLAGS:
1810 rtspsrc->tls_validation_flags = g_value_get_flags (value);
1812 case PROP_TLS_DATABASE:
1813 g_clear_object (&rtspsrc->tls_database);
1814 rtspsrc->tls_database = g_value_dup_object (value);
1816 case PROP_TLS_INTERACTION:
1817 g_clear_object (&rtspsrc->tls_interaction);
1818 rtspsrc->tls_interaction = g_value_dup_object (value);
1820 case PROP_DO_RETRANSMISSION:
1821 rtspsrc->do_retransmission = g_value_get_boolean (value);
1823 case PROP_NTP_TIME_SOURCE:
1824 rtspsrc->ntp_time_source = g_value_get_enum (value);
1826 case PROP_USER_AGENT:
1827 g_free (rtspsrc->user_agent);
1828 rtspsrc->user_agent = g_value_dup_string (value);
1830 case PROP_MAX_RTCP_RTP_TIME_DIFF:
1831 rtspsrc->max_rtcp_rtp_time_diff = g_value_get_int (value);
1833 case PROP_RFC7273_SYNC:
1834 rtspsrc->rfc7273_sync = g_value_get_boolean (value);
1836 case PROP_ADD_REFERENCE_TIMESTAMP_META:
1837 rtspsrc->add_reference_timestamp_meta = g_value_get_boolean (value);
1839 case PROP_MAX_TS_OFFSET_ADJUSTMENT:
1840 rtspsrc->max_ts_offset_adjustment = g_value_get_uint64 (value);
1842 case PROP_MAX_TS_OFFSET:
1843 rtspsrc->max_ts_offset = g_value_get_int64 (value);
1844 rtspsrc->max_ts_offset_is_set = TRUE;
1846 case PROP_DEFAULT_VERSION:
1847 rtspsrc->default_version = g_value_get_enum (value);
1849 case PROP_BACKCHANNEL:
1850 rtspsrc->backchannel = g_value_get_enum (value);
1852 case PROP_TEARDOWN_TIMEOUT:
1853 rtspsrc->teardown_timeout = g_value_get_uint64 (value);
1855 case PROP_ONVIF_MODE:
1856 rtspsrc->onvif_mode = g_value_get_boolean (value);
1858 case PROP_ONVIF_RATE_CONTROL:
1859 rtspsrc->onvif_rate_control = g_value_get_boolean (value);
1862 rtspsrc->is_live = g_value_get_boolean (value);
1864 case PROP_IGNORE_X_SERVER_REPLY:
1865 rtspsrc->ignore_x_server_reply = g_value_get_boolean (value);
1868 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1874 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
1877 GstRTSPSrc *rtspsrc;
1879 rtspsrc = GST_RTSPSRC (object);
1883 g_value_set_string (value, rtspsrc->conninfo.location);
1885 case PROP_PROTOCOLS:
1886 g_value_set_flags (value, rtspsrc->protocols);
1889 g_value_set_boolean (value, rtspsrc->debug);
1892 g_value_set_uint (value, rtspsrc->retry);
1895 g_value_set_uint64 (value, rtspsrc->udp_timeout);
1897 case PROP_TCP_TIMEOUT:
1898 g_value_set_uint64 (value, rtspsrc->tcp_timeout);
1901 g_value_set_uint (value, rtspsrc->latency);
1903 case PROP_DROP_ON_LATENCY:
1904 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1906 case PROP_CONNECTION_SPEED:
1907 g_value_set_uint64 (value, rtspsrc->connection_speed);
1909 case PROP_NAT_METHOD:
1910 g_value_set_enum (value, rtspsrc->nat_method);
1913 g_value_set_boolean (value, rtspsrc->do_rtcp);
1915 case PROP_DO_RTSP_KEEP_ALIVE:
1916 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1922 if (rtspsrc->proxy_host) {
1924 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1928 g_value_take_string (value, str);
1932 g_value_set_string (value, rtspsrc->prop_proxy_id);
1935 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1937 case PROP_RTP_BLOCKSIZE:
1938 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1941 g_value_set_string (value, rtspsrc->user_id);
1944 g_value_set_string (value, rtspsrc->user_pw);
1946 case PROP_BUFFER_MODE:
1947 g_value_set_enum (value, rtspsrc->buffer_mode);
1949 case PROP_PORT_RANGE:
1953 if (rtspsrc->client_port_range.min != 0) {
1954 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1955 rtspsrc->client_port_range.max);
1959 g_value_take_string (value, str);
1962 case PROP_UDP_BUFFER_SIZE:
1963 g_value_set_int (value, rtspsrc->udp_buffer_size);
1965 case PROP_SHORT_HEADER:
1966 g_value_set_boolean (value, rtspsrc->short_header);
1968 case PROP_PROBATION:
1969 g_value_set_uint (value, rtspsrc->probation);
1971 case PROP_UDP_RECONNECT:
1972 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1974 case PROP_MULTICAST_IFACE:
1975 g_value_set_string (value, rtspsrc->multi_iface);
1978 g_value_set_boolean (value, rtspsrc->ntp_sync);
1980 case PROP_USE_PIPELINE_CLOCK:
1981 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1984 g_value_set_boxed (value, rtspsrc->sdes);
1986 case PROP_TLS_VALIDATION_FLAGS:
1987 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1989 case PROP_TLS_DATABASE:
1990 g_value_set_object (value, rtspsrc->tls_database);
1992 case PROP_TLS_INTERACTION:
1993 g_value_set_object (value, rtspsrc->tls_interaction);
1995 case PROP_DO_RETRANSMISSION:
1996 g_value_set_boolean (value, rtspsrc->do_retransmission);
1998 case PROP_NTP_TIME_SOURCE:
1999 g_value_set_enum (value, rtspsrc->ntp_time_source);
2001 case PROP_USER_AGENT:
2002 g_value_set_string (value, rtspsrc->user_agent);
2004 case PROP_MAX_RTCP_RTP_TIME_DIFF:
2005 g_value_set_int (value, rtspsrc->max_rtcp_rtp_time_diff);
2007 case PROP_RFC7273_SYNC:
2008 g_value_set_boolean (value, rtspsrc->rfc7273_sync);
2010 case PROP_ADD_REFERENCE_TIMESTAMP_META:
2011 g_value_set_boolean (value, rtspsrc->add_reference_timestamp_meta);
2013 case PROP_MAX_TS_OFFSET_ADJUSTMENT:
2014 g_value_set_uint64 (value, rtspsrc->max_ts_offset_adjustment);
2016 case PROP_MAX_TS_OFFSET:
2017 g_value_set_int64 (value, rtspsrc->max_ts_offset);
2019 case PROP_DEFAULT_VERSION:
2020 g_value_set_enum (value, rtspsrc->default_version);
2022 case PROP_BACKCHANNEL:
2023 g_value_set_enum (value, rtspsrc->backchannel);
2025 case PROP_TEARDOWN_TIMEOUT:
2026 g_value_set_uint64 (value, rtspsrc->teardown_timeout);
2028 case PROP_ONVIF_MODE:
2029 g_value_set_boolean (value, rtspsrc->onvif_mode);
2031 case PROP_ONVIF_RATE_CONTROL:
2032 g_value_set_boolean (value, rtspsrc->onvif_rate_control);
2035 g_value_set_boolean (value, rtspsrc->is_live);
2037 case PROP_IGNORE_X_SERVER_REPLY:
2038 g_value_set_boolean (value, rtspsrc->ignore_x_server_reply);
2041 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2047 find_stream_by_id (GstRTSPStream * stream, gint * id)
2049 if (stream->id == *id)
2056 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
2058 /* ignore unconfigured channels here (e.g., those that
2059 * were explicitly skipped during SETUP) */
2060 if ((stream->channelpad[0] != NULL) &&
2061 (stream->channel[0] == *channel || stream->channel[1] == *channel))
2068 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
2070 GstElement *src = (GstElement *) a;
2072 if (stream->udpsrc[0] == src)
2074 if (stream->udpsrc[1] == src)
2081 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
2083 if (stream->conninfo.location) {
2084 /* check qualified setup_url */
2085 if (!strcmp (stream->conninfo.location, (gchar *) a))
2088 if (stream->control_url) {
2089 /* check original control_url */
2090 if (!strcmp (stream->control_url, (gchar *) a))
2093 /* check if qualified setup_url ends with string */
2094 if (g_str_has_suffix (stream->control_url, (gchar *) a))
2101 static GstRTSPStream *
2102 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
2106 /* find and get stream */
2107 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
2108 return (GstRTSPStream *) lstream->data;
2113 static const GstSDPBandwidth *
2114 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
2115 const GstSDPMedia * media, const gchar * type)
2119 /* first look in the media specific section */
2120 len = gst_sdp_media_bandwidths_len (media);
2121 for (i = 0; i < len; i++) {
2122 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
2124 if (strcmp (bw->bwtype, type) == 0)
2127 /* then look in the message specific section */
2128 len = gst_sdp_message_bandwidths_len (sdp);
2129 for (i = 0; i < len; i++) {
2130 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
2132 if (strcmp (bw->bwtype, type) == 0)
2139 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
2140 const GstSDPMedia * media, GstRTSPStream * stream)
2142 const GstSDPBandwidth *bw;
2144 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
2145 stream->as_bandwidth = bw->bandwidth;
2147 stream->as_bandwidth = -1;
2149 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
2150 stream->rr_bandwidth = bw->bandwidth;
2152 stream->rr_bandwidth = -1;
2154 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
2155 stream->rs_bandwidth = bw->bandwidth;
2157 stream->rs_bandwidth = -1;
2161 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
2162 const GstSDPConnection * conn)
2164 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
2167 if (conn->addrtype == NULL)
2170 /* check for IPV6 */
2171 if (strcmp (conn->addrtype, "IP4") == 0)
2172 stream->is_ipv6 = FALSE;
2173 else if (strcmp (conn->addrtype, "IP6") == 0)
2174 stream->is_ipv6 = TRUE;
2179 g_free (stream->destination);
2180 stream->destination = g_strdup (conn->address);
2182 /* check for multicast */
2183 stream->is_multicast =
2184 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
2186 stream->ttl = conn->ttl;
2189 /* Go over the connections for a stream.
2190 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
2192 * - If we are dealing with a localhost address, we disable multicast
2195 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
2196 const GstSDPMedia * media, GstRTSPStream * stream)
2198 const GstSDPConnection *conn;
2201 /* first look in the media specific section */
2202 len = gst_sdp_media_connections_len (media);
2203 for (i = 0; i < len; i++) {
2204 conn = gst_sdp_media_get_connection (media, i);
2206 gst_rtspsrc_do_stream_connection (src, stream, conn);
2208 /* then look in the message specific section */
2209 if ((conn = gst_sdp_message_get_connection (sdp))) {
2210 gst_rtspsrc_do_stream_connection (src, stream, conn);
2215 make_stream_id (GstRTSPStream * stream, const GstSDPMedia * media)
2218 g_strdup_printf ("%s:%d:%d:%s:%d", media->media, media->port,
2219 media->num_ports, media->proto, stream->default_pt);
2221 g_strcanon (stream_id, G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS, ':');
2226 /* m=<media> <UDP port> RTP/AVP <payload>
2229 gst_rtspsrc_collect_payloads (GstRTSPSrc * src, const GstSDPMessage * sdp,
2230 const GstSDPMedia * media, GstRTSPStream * stream)
2234 GstCaps *global_caps;
2237 proto = gst_sdp_media_get_proto (media);
2241 if (g_str_equal (proto, "RTP/AVP"))
2242 stream->profile = GST_RTSP_PROFILE_AVP;
2243 else if (g_str_equal (proto, "RTP/SAVP"))
2244 stream->profile = GST_RTSP_PROFILE_SAVP;
2245 else if (g_str_equal (proto, "RTP/AVPF"))
2246 stream->profile = GST_RTSP_PROFILE_AVPF;
2247 else if (g_str_equal (proto, "RTP/SAVPF"))
2248 stream->profile = GST_RTSP_PROFILE_SAVPF;
2252 if (gst_sdp_media_get_attribute_val (media, "sendonly") != NULL &&
2253 /* We want to setup caps for streams configured as backchannel */
2254 !stream->is_backchannel && src->backchannel != BACKCHANNEL_NONE)
2255 goto sendonly_media;
2257 /* Parse global SDP attributes once */
2258 global_caps = gst_caps_new_empty_simple ("application/x-unknown");
2259 GST_DEBUG ("mapping sdp session level attributes to caps");
2260 gst_sdp_message_attributes_to_caps (sdp, global_caps);
2261 GST_DEBUG ("mapping sdp media level attributes to caps");
2262 gst_sdp_media_attributes_to_caps (media, global_caps);
2264 /* Keep a copy of the SDP key management */
2265 gst_sdp_media_parse_keymgmt (media, &stream->mikey);
2266 if (stream->mikey == NULL)
2267 gst_sdp_message_parse_keymgmt (sdp, &stream->mikey);
2269 len = gst_sdp_media_formats_len (media);
2270 for (i = 0; i < len; i++) {
2272 GstCaps *caps, *outcaps;
2277 pt = atoi (gst_sdp_media_get_format (media, i));
2279 GST_DEBUG_OBJECT (src, " looking at %d pt: %d", i, pt);
2282 caps = gst_sdp_media_get_caps_from_media (media, pt);
2284 GST_WARNING_OBJECT (src, " skipping pt %d without caps", pt);
2288 /* do some tweaks */
2289 s = gst_caps_get_structure (caps, 0);
2290 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
2291 stream->is_real = (strstr (enc, "-REAL") != NULL);
2292 if (strcmp (enc, "X-ASF-PF") == 0)
2293 stream->container = TRUE;
2296 /* Merge in global caps */
2297 /* Intersect will merge in missing fields to the current caps */
2298 outcaps = gst_caps_intersect (caps, global_caps);
2299 gst_caps_unref (caps);
2301 /* the first pt will be the default */
2302 if (stream->ptmap->len == 0)
2303 stream->default_pt = pt;
2306 item.caps = outcaps;
2308 g_array_append_val (stream->ptmap, item);
2311 stream->stream_id = make_stream_id (stream, media);
2313 gst_caps_unref (global_caps);
2318 GST_ERROR_OBJECT (src, "can't find proto in media");
2323 GST_ERROR_OBJECT (src, "unknown proto in media: '%s'", proto);
2328 GST_DEBUG_OBJECT (src, "sendonly media ignored, no backchannel");
2333 static const gchar *
2334 get_aggregate_control (GstRTSPSrc * src)
2339 base = src->control;
2340 else if (src->content_base)
2341 base = src->content_base;
2342 else if (src->conninfo.url_str)
2343 base = src->conninfo.url_str;
2351 clear_ptmap_item (PtMapItem * item)
2354 gst_caps_unref (item->caps);
2357 static GstRTSPStream *
2358 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx,
2361 GstRTSPStream *stream;
2362 const gchar *control_path;
2363 const GstSDPMedia *media;
2365 /* get media, should not return NULL */
2366 media = gst_sdp_message_get_media (sdp, idx);
2370 stream = g_new0 (GstRTSPStream, 1);
2371 stream->parent = src;
2372 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
2374 stream->last_ret = GST_FLOW_NOT_LINKED;
2375 stream->added = FALSE;
2376 stream->setup = FALSE;
2377 stream->skipped = FALSE;
2379 stream->eos = FALSE;
2380 stream->discont = TRUE;
2381 stream->seqbase = -1;
2382 stream->timebase = -1;
2383 stream->send_ssrc = g_random_int ();
2384 stream->profile = GST_RTSP_PROFILE_AVP;
2385 stream->ptmap = g_array_new (FALSE, FALSE, sizeof (PtMapItem));
2386 stream->mikey = NULL;
2387 stream->stream_id = NULL;
2388 stream->is_backchannel = FALSE;
2389 g_mutex_init (&stream->conninfo.send_lock);
2390 g_mutex_init (&stream->conninfo.recv_lock);
2391 g_array_set_clear_func (stream->ptmap, (GDestroyNotify) clear_ptmap_item);
2393 /* stream is sendonly and onvif backchannel is requested */
2394 if (gst_sdp_media_get_attribute_val (media, "sendonly") != NULL &&
2395 src->backchannel != BACKCHANNEL_NONE)
2396 stream->is_backchannel = TRUE;
2398 /* collect bandwidth information for this steam. FIXME, configure in the RTP
2399 * session manager to scale RTCP. */
2400 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
2402 /* collect connection info */
2403 gst_rtspsrc_collect_connections (src, sdp, media, stream);
2405 /* make the payload type map */
2406 gst_rtspsrc_collect_payloads (src, sdp, media, stream);
2408 /* collect port number */
2409 stream->port = gst_sdp_media_get_port (media);
2411 /* get control url to construct the setup url. The setup url is used to
2412 * configure the transport of the stream and is used to identity the stream in
2413 * the RTP-Info header field returned from PLAY. */
2414 control_path = gst_sdp_media_get_attribute_val (media, "control");
2415 if (control_path == NULL)
2416 control_path = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
2418 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
2419 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
2420 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
2421 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_path));
2423 /* RFC 2326, C.3: missing control_path permitted in case of a single stream */
2424 if (control_path == NULL && n_streams == 1) {
2428 if (control_path != NULL) {
2429 stream->control_url = g_strdup (control_path);
2430 /* Build a fully qualified url using the content_base if any or by prefixing
2431 * the original request.
2432 * If the control_path starts with a non rtsp: protocol we will most
2433 * likely build a URL that the server will fail to understand, this is ok,
2434 * we will fail then. */
2435 if (g_str_has_prefix (control_path, "rtsp://"))
2436 stream->conninfo.location = g_strdup (control_path);
2440 base = get_aggregate_control (src);
2441 if (g_strcmp0 (control_path, "*") == 0)
2442 control_path = g_strdup (base);
2444 stream->conninfo.location = gst_uri_join_strings (base, control_path);
2447 GST_DEBUG_OBJECT (src, " setup: %s",
2448 GST_STR_NULL (stream->conninfo.location));
2450 /* we keep track of all streams */
2451 src->streams = g_list_append (src->streams, stream);
2459 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
2463 GST_DEBUG_OBJECT (src, "free stream %p", stream);
2465 g_array_free (stream->ptmap, TRUE);
2467 g_free (stream->destination);
2468 g_free (stream->control_url);
2469 g_free (stream->conninfo.location);
2470 g_free (stream->stream_id);
2472 for (i = 0; i < 2; i++) {
2473 if (stream->udpsrc[i]) {
2474 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
2475 if (gst_object_has_as_parent (GST_OBJECT (stream->udpsrc[i]),
2477 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
2478 gst_object_unref (stream->udpsrc[i]);
2480 if (stream->channelpad[i])
2481 gst_object_unref (stream->channelpad[i]);
2483 if (stream->udpsink[i]) {
2484 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
2485 if (gst_object_has_as_parent (GST_OBJECT (stream->udpsink[i]),
2487 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
2488 gst_object_unref (stream->udpsink[i]);
2491 if (stream->rtpsrc) {
2492 gst_element_set_state (stream->rtpsrc, GST_STATE_NULL);
2493 gst_bin_remove (GST_BIN_CAST (src), stream->rtpsrc);
2494 gst_object_unref (stream->rtpsrc);
2496 if (stream->srcpad) {
2497 gst_pad_set_active (stream->srcpad, FALSE);
2499 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2501 if (stream->srtpenc)
2502 gst_object_unref (stream->srtpenc);
2503 if (stream->srtpdec)
2504 gst_object_unref (stream->srtpdec);
2505 if (stream->srtcpparams)
2506 gst_caps_unref (stream->srtcpparams);
2508 gst_mikey_message_unref (stream->mikey);
2509 if (stream->rtcppad)
2510 gst_object_unref (stream->rtcppad);
2511 if (stream->session)
2512 g_object_unref (stream->session);
2513 if (stream->rtx_pt_map)
2514 gst_structure_free (stream->rtx_pt_map);
2516 g_mutex_clear (&stream->conninfo.send_lock);
2517 g_mutex_clear (&stream->conninfo.recv_lock);
2523 gst_rtspsrc_cleanup (GstRTSPSrc * src)
2526 ParameterRequest *req;
2528 GST_DEBUG_OBJECT (src, "cleanup");
2530 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2531 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2533 gst_rtspsrc_stream_free (src, stream);
2535 g_list_free (src->streams);
2536 src->streams = NULL;
2538 if (src->manager_sig_id) {
2539 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
2540 src->manager_sig_id = 0;
2542 gst_element_set_state (src->manager, GST_STATE_NULL);
2543 gst_bin_remove (GST_BIN_CAST (src), src->manager);
2544 src->manager = NULL;
2547 gst_structure_free (src->props);
2550 g_free (src->content_base);
2551 src->content_base = NULL;
2553 g_free (src->control);
2554 src->control = NULL;
2557 gst_rtsp_range_free (src->range);
2560 /* don't clear the SDP when it was used in the url */
2561 if (src->sdp && !src->from_sdp) {
2562 gst_sdp_message_free (src->sdp);
2566 src->need_segment = FALSE;
2567 src->clip_out_segment = FALSE;
2569 if (src->provided_clock) {
2570 gst_object_unref (src->provided_clock);
2571 src->provided_clock = NULL;
2574 GST_OBJECT_LOCK (src);
2575 /* free parameter requests queue */
2576 while ((req = g_queue_pop_head (&src->set_get_param_q))) {
2577 gst_promise_expire (req->promise);
2578 free_param_data (req);
2580 GST_OBJECT_UNLOCK (src);
2585 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
2586 gint * rtpport, gint * rtcpport)
2589 GstStateChangeReturn ret;
2590 GstElement *udpsrc0, *udpsrc1;
2591 gint tmp_rtp, tmp_rtcp;
2595 src = stream->parent;
2601 /* Start at next port */
2602 tmp_rtp = src->next_port_num;
2604 if (stream->is_ipv6)
2605 host = "udp://[::0]";
2607 host = "udp://0.0.0.0";
2609 /* try to allocate 2 UDP ports, the RTP port should be an even
2610 * number and the RTCP port should be the next (uneven) port */
2613 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
2614 tmp_rtp >= src->client_port_range.max)
2617 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2618 if (udpsrc0 == NULL)
2619 goto no_udp_protocol;
2620 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
2622 if (src->udp_buffer_size != 0)
2623 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
2626 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
2627 if (ret == GST_STATE_CHANGE_FAILURE) {
2629 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
2632 if (++count > src->retry)
2635 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2636 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2637 gst_object_unref (udpsrc0);
2640 GST_DEBUG_OBJECT (src, "retry %d", count);
2643 goto no_udp_protocol;
2646 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
2647 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
2649 /* check if port is even */
2650 if ((tmp_rtp & 0x01) != 0) {
2651 /* port not even, close and allocate another */
2652 if (++count > src->retry)
2655 GST_DEBUG_OBJECT (src, "RTP port not even");
2657 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2658 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2659 gst_object_unref (udpsrc0);
2662 GST_DEBUG_OBJECT (src, "retry %d", count);
2667 /* allocate port+1 for RTCP now */
2668 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2669 if (udpsrc1 == NULL)
2670 goto no_udp_rtcp_protocol;
2673 tmp_rtcp = tmp_rtp + 1;
2674 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
2677 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
2679 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
2680 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
2681 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
2682 if (ret == GST_STATE_CHANGE_FAILURE) {
2683 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
2685 if (++count > src->retry)
2688 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2689 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2690 gst_object_unref (udpsrc0);
2693 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
2694 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2695 gst_object_unref (udpsrc1);
2699 GST_DEBUG_OBJECT (src, "retry %d", count);
2703 /* all fine, do port check */
2704 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
2705 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
2707 /* this should not happen... */
2708 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
2711 /* we keep these elements, we configure all in configure_transport when the
2712 * server told us to really use the UDP ports. */
2713 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
2714 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
2715 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
2716 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
2718 /* keep track of next available port number when we have a range
2720 if (src->next_port_num != 0)
2721 src->next_port_num = tmp_rtcp + 1;
2728 GST_DEBUG_OBJECT (src, "could not get UDP source");
2733 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
2737 no_udp_rtcp_protocol:
2739 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
2744 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
2745 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
2751 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2752 gst_object_unref (udpsrc0);
2755 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2756 gst_object_unref (udpsrc1);
2763 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
2768 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
2770 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2771 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2774 for (i = 0; i < 2; i++) {
2775 if (stream->udpsrc[i])
2776 gst_element_set_state (stream->udpsrc[i], state);
2782 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
2789 event = gst_event_new_flush_start ();
2790 GST_DEBUG_OBJECT (src, "start flush");
2792 state = GST_STATE_PAUSED;
2794 event = gst_event_new_flush_stop (TRUE);
2795 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2798 state = GST_STATE_PLAYING;
2800 state = GST_STATE_PAUSED;
2802 gst_rtspsrc_push_event (src, event);
2803 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2804 gst_rtspsrc_set_state (src, state);
2807 static GstRTSPResult
2808 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
2809 GstRTSPMessage * message, gint64 timeout)
2813 if (conninfo->connection) {
2814 g_mutex_lock (&conninfo->send_lock);
2816 gst_rtsp_connection_send_usec (conninfo->connection, message, timeout);
2817 g_mutex_unlock (&conninfo->send_lock);
2819 ret = GST_RTSP_ERROR;
2825 static GstRTSPResult
2826 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
2827 GstRTSPMessage * message, gint64 timeout)
2831 if (conninfo->connection) {
2832 g_mutex_lock (&conninfo->recv_lock);
2833 ret = gst_rtsp_connection_receive_usec (conninfo->connection, message,
2835 g_mutex_unlock (&conninfo->recv_lock);
2837 ret = GST_RTSP_ERROR;
2844 gst_rtspsrc_get_position (GstRTSPSrc * src)
2849 query = gst_query_new_position (GST_FORMAT_TIME);
2850 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2851 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2852 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2856 if (stream->srcpad) {
2857 if (gst_pad_query (stream->srcpad, query)) {
2858 gst_query_parse_position (query, &fmt, &pos);
2859 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2860 GST_TIME_ARGS (pos));
2861 src->last_pos = pos;
2871 gst_query_unref (query);
2875 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2880 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type = GST_SEEK_TYPE_NONE;
2882 gboolean flush, server_side_trickmode;
2885 GstSegment seeksegment = { 0, };
2887 const gchar *seek_style = NULL;
2888 gboolean rate_change_only = FALSE;
2889 gboolean rate_change_same_direction = FALSE;
2891 GST_DEBUG_OBJECT (src, "doing seek with event %" GST_PTR_FORMAT, event);
2893 gst_event_parse_seek (event, &rate, &format, &flags,
2894 &cur_type, &cur, &stop_type, &stop);
2895 rate_change_only = cur_type == GST_SEEK_TYPE_NONE
2896 && stop_type == GST_SEEK_TYPE_NONE;
2898 /* we need TIME format */
2899 if (format != src->segment.format)
2902 /* Check if we are not at all seekable */
2903 if (src->seekable == -1.0)
2906 /* Additional seeking-to-beginning-only check */
2907 if (src->seekable == 0.0 && cur != 0)
2910 if (flags & GST_SEEK_FLAG_SEGMENT)
2911 goto invalid_segment_flag;
2913 /* get flush flag */
2914 flush = flags & GST_SEEK_FLAG_FLUSH;
2915 server_side_trickmode = flags & GST_SEEK_FLAG_TRICKMODE;
2917 gst_event_parse_seek_trickmode_interval (event, &src->trickmode_interval);
2919 /* now we need to make sure the streaming thread is stopped. We do this by
2920 * either sending a FLUSH_START event downstream which will cause the
2921 * streaming thread to stop with a WRONG_STATE.
2922 * For a non-flushing seek we simply pause the task, which will happen as soon
2923 * as it completes one iteration (and thus might block when the sink is
2924 * blocking in preroll). */
2926 GST_DEBUG_OBJECT (src, "starting flush");
2927 gst_rtspsrc_flush (src, TRUE, FALSE);
2930 gst_task_pause (src->task);
2934 /* we should now be able to grab the streaming thread because we stopped it
2935 * with the above flush/pause code */
2936 GST_RTSP_STREAM_LOCK (src);
2938 GST_DEBUG_OBJECT (src, "stopped streaming");
2940 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2941 gst_rtspsrc_connection_flush (src, FALSE);
2943 /* copy segment, we need this because we still need the old
2944 * segment when we close the current segment. */
2945 seeksegment = src->segment;
2947 /* configure the seek parameters in the seeksegment. We will then have the
2948 * right values in the segment to perform the seek */
2949 GST_DEBUG_OBJECT (src, "configuring seek");
2950 rate_change_same_direction = (rate * seeksegment.rate) > 0;
2951 gst_segment_do_seek (&seeksegment, rate, format, flags,
2952 cur_type, cur, stop_type, stop, &update);
2954 /* if we were playing, pause first */
2955 playing = (src->state == GST_RTSP_STATE_PLAYING);
2957 /* obtain current position in case seek fails */
2958 gst_rtspsrc_get_position (src);
2959 gst_rtspsrc_pause (src, FALSE);
2961 src->server_side_trickmode = server_side_trickmode;
2963 src->state = GST_RTSP_STATE_SEEKING;
2965 /* PLAY will add the range header now. */
2966 src->need_range = TRUE;
2968 /* If an accurate seek was requested, we want to clip the segment we
2969 * output in ONVIF mode to the requested bounds */
2970 src->clip_out_segment = ! !(flags & GST_SEEK_FLAG_ACCURATE);
2971 src->seek_seqnum = gst_event_get_seqnum (event);
2973 /* prepare for streaming again */
2975 /* if we started flush, we stop now */
2976 GST_DEBUG_OBJECT (src, "stopping flush");
2977 gst_rtspsrc_flush (src, FALSE, playing);
2980 /* now we did the seek and can activate the new segment values */
2981 src->segment = seeksegment;
2983 /* if we're doing a segment seek, post a SEGMENT_START message */
2984 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2985 gst_element_post_message (GST_ELEMENT_CAST (src),
2986 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2987 src->segment.format, src->segment.position));
2990 /* mark discont when needed */
2991 if (!(rate_change_only && rate_change_same_direction)) {
2992 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2993 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2994 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2995 stream->discont = TRUE;
2999 /* and continue playing if needed. If we are not acting as a live source,
3000 * then only the RTSP PLAYING state, set earlier, matters. */
3001 GST_OBJECT_LOCK (src);
3003 playing = (GST_STATE_PENDING (src) == GST_STATE_VOID_PENDING
3004 && GST_STATE (src) == GST_STATE_PLAYING)
3005 || (GST_STATE_PENDING (src) == GST_STATE_PLAYING);
3007 GST_OBJECT_UNLOCK (src);
3009 if (src->version >= GST_RTSP_VERSION_2_0) {
3010 if (flags & GST_SEEK_FLAG_ACCURATE)
3012 else if (flags & GST_SEEK_FLAG_KEY_UNIT)
3013 seek_style = "CoRAP";
3014 else if (flags & GST_SEEK_FLAG_KEY_UNIT
3015 && flags & GST_SEEK_FLAG_SNAP_BEFORE)
3016 seek_style = "First-Prior";
3017 else if (flags & GST_SEEK_FLAG_KEY_UNIT && flags & GST_SEEK_FLAG_SNAP_AFTER)
3018 seek_style = "Next";
3022 gst_rtspsrc_play (src, &seeksegment, FALSE, seek_style);
3024 GST_RTSP_STREAM_UNLOCK (src);
3031 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
3036 GST_DEBUG_OBJECT (src, "stream is not seekable");
3039 invalid_segment_flag:
3041 GST_WARNING_OBJECT (src, "Segment seeks not supported");
3047 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
3051 gboolean res = TRUE;
3054 src = GST_RTSPSRC_CAST (parent);
3056 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
3057 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
3059 switch (GST_EVENT_TYPE (event)) {
3060 case GST_EVENT_SEEK:
3062 guint32 seqnum = gst_event_get_seqnum (event);
3063 if (seqnum == src->seek_seqnum) {
3064 GST_LOG_OBJECT (pad, "Drop duplicated SEEK event seqnum %"
3065 G_GUINT32_FORMAT, seqnum);
3067 res = gst_rtspsrc_perform_seek (src, event);
3073 case GST_EVENT_NAVIGATION:
3074 case GST_EVENT_LATENCY:
3082 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
3083 res = gst_pad_send_event (target, event);
3084 gst_object_unref (target);
3086 gst_event_unref (event);
3089 gst_event_unref (event);
3096 gst_rtspsrc_stream_start_event_add_group_id (GstRTSPSrc * src, GstEvent * event)
3098 g_mutex_lock (&src->group_lock);
3100 if (src->group_id == GST_GROUP_ID_INVALID)
3101 src->group_id = gst_util_group_id_next ();
3103 g_mutex_unlock (&src->group_lock);
3105 gst_event_set_group_id (event, src->group_id);
3109 gst_rtspsrc_update_src_event (GstRTSPSrc * self, GstRTSPStream * stream,
3112 switch (GST_EVENT_TYPE (event)) {
3113 case GST_EVENT_STREAM_START:{
3118 cs = g_checksum_new (G_CHECKSUM_SHA256);
3119 uri = self->conninfo.location;
3120 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
3123 g_strdup_printf ("%s/%s", g_checksum_get_string (cs),
3126 g_checksum_free (cs);
3127 gst_event_unref (event);
3128 event = gst_event_new_stream_start (stream_id);
3129 gst_rtspsrc_stream_start_event_add_group_id (self, event);
3132 gst_event_set_seqnum (event, self->seek_seqnum);
3136 event = gst_event_make_writable (event);
3137 gst_event_set_seqnum (event, self->seek_seqnum);
3145 gst_rtspsrc_handle_src_sink_event (GstPad * pad, GstObject * parent,
3148 GstRTSPStream *stream;
3149 GstRTSPSrc *self = GST_RTSPSRC (GST_OBJECT_PARENT (parent));
3151 stream = gst_pad_get_element_private (pad);
3153 event = gst_rtspsrc_update_src_event (self, stream, event);
3155 return gst_pad_push_event (stream->srcpad, event);
3158 /* this is the final event function we receive on the internal source pad when
3159 * we deal with TCP connections */
3161 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
3166 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
3168 switch (GST_EVENT_TYPE (event)) {
3169 case GST_EVENT_SEEK:
3171 case GST_EVENT_NAVIGATION:
3172 case GST_EVENT_LATENCY:
3174 gst_event_unref (event);
3181 /* this is the final query function we receive on the internal source pad when
3182 * we deal with TCP connections */
3184 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
3188 gboolean res = FALSE;
3190 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
3192 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
3193 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3195 switch (GST_QUERY_TYPE (query)) {
3196 case GST_QUERY_POSITION:
3201 case GST_QUERY_DURATION:
3205 gst_query_parse_duration (query, &format, NULL);
3208 case GST_FORMAT_TIME:
3209 gst_query_set_duration (query, format, src->segment.duration);
3217 case GST_QUERY_LATENCY:
3219 /* we are live with a min latency of 0 and unlimited max latency, this
3220 * result will be updated by the session manager if there is any. */
3221 gst_query_set_latency (query, src->is_live, 0, -1);
3232 /* this query is executed on the ghost source pad exposed on rtspsrc. */
3234 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
3238 gboolean res = FALSE;
3240 src = GST_RTSPSRC_CAST (parent);
3242 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
3243 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3245 switch (GST_QUERY_TYPE (query)) {
3246 case GST_QUERY_DURATION:
3250 gst_query_parse_duration (query, &format, NULL);
3253 case GST_FORMAT_TIME:
3254 gst_query_set_duration (query, format, src->segment.duration);
3262 case GST_QUERY_SEEKING:
3266 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
3267 if (format == GST_FORMAT_TIME) {
3268 gboolean seekable = TRUE;
3269 GstClockTime start = 0, duration = src->segment.duration;
3271 /* seeking without duration is unlikely */
3272 seekable = seekable && src->seekable >= 0.0 && src->segment.duration &&
3273 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
3276 if (src->seekable > 0.0) {
3277 start = src->last_pos - src->seekable * GST_SECOND;
3279 /* src->seekable == 0 means that we can only seek to 0 */
3285 GST_LOG_OBJECT (src, "seekable: %d, duration: %" GST_TIME_FORMAT
3286 ", src->seekable: %f", seekable,
3287 GST_TIME_ARGS (src->segment.duration), src->seekable);
3289 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, start,
3299 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
3301 gst_query_set_uri (query, uri);
3309 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
3311 /* forward the query to the proxy target pad */
3313 res = gst_pad_query (target, query);
3314 gst_object_unref (target);
3323 /* callback for RTCP messages to be sent to the server when operating in TCP
3325 static GstFlowReturn
3326 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
3329 GstRTSPStream *stream;
3330 GstFlowReturn res = GST_FLOW_OK;
3332 GstRTSPMessage message = { 0 };
3333 GstRTSPConnInfo *conninfo;
3335 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
3336 src = stream->parent;
3338 gst_rtsp_message_init_data (&message, stream->channel[1]);
3340 /* lend the body data to the message */
3341 gst_rtsp_message_set_body_buffer (&message, buffer);
3343 if (stream->conninfo.connection)
3344 conninfo = &stream->conninfo;
3346 conninfo = &src->conninfo;
3348 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP",
3349 (guint) gst_buffer_get_size (buffer));
3350 ret = gst_rtspsrc_connection_send (src, conninfo, &message, 0);
3351 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
3353 gst_rtsp_message_unset (&message);
3355 gst_buffer_unref (buffer);
3360 static GstFlowReturn
3361 gst_rtspsrc_push_backchannel_buffer (GstRTSPSrc * src, guint id,
3366 res = gst_rtspsrc_push_backchannel_sample (src, id, sample);
3368 gst_sample_unref (sample);
3373 static GstFlowReturn
3374 gst_rtspsrc_push_backchannel_sample (GstRTSPSrc * src, guint id,
3377 GstFlowReturn res = GST_FLOW_OK;
3378 GstRTSPStream *stream;
3380 if (!src->conninfo.connected || src->state != GST_RTSP_STATE_PLAYING)
3383 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
3384 if (stream == NULL) {
3385 GST_ERROR_OBJECT (src, "no stream with id %u", id);
3389 if (src->interleaved) {
3392 GstRTSPMessage message = { 0 };
3393 GstRTSPConnInfo *conninfo;
3395 buffer = gst_sample_get_buffer (sample);
3397 gst_rtsp_message_init_data (&message, stream->channel[0]);
3399 /* lend the body data to the message */
3400 gst_rtsp_message_set_body_buffer (&message, buffer);
3402 if (stream->conninfo.connection)
3403 conninfo = &stream->conninfo;
3405 conninfo = &src->conninfo;
3407 GST_DEBUG_OBJECT (src, "sending %u bytes backchannel RTP",
3408 (guint) gst_buffer_get_size (buffer));
3409 ret = gst_rtspsrc_connection_send (src, conninfo, &message, 0);
3410 GST_DEBUG_OBJECT (src, "sent backchannel RTP, %d", ret);
3412 gst_rtsp_message_unset (&message);
3416 g_signal_emit_by_name (stream->rtpsrc, "push-sample", sample, &res);
3417 GST_DEBUG_OBJECT (src, "sent backchannel RTP sample %p: %s", sample,
3418 gst_flow_get_name (res));
3425 static GstPadProbeReturn
3426 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3428 GstRTSPSrc *src = user_data;
3430 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
3431 GST_DEBUG_PAD_NAME (pad));
3433 /* activate the streams */
3434 GST_OBJECT_LOCK (src);
3435 if (!src->need_activate)
3438 src->need_activate = FALSE;
3439 GST_OBJECT_UNLOCK (src);
3441 gst_rtspsrc_activate_streams (src);
3443 return GST_PAD_PROBE_OK;
3447 GST_OBJECT_UNLOCK (src);
3448 return GST_PAD_PROBE_OK;
3452 static GstPadProbeReturn
3453 udpsrc_probe_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3455 guint32 *segment_seqnum = user_data;
3457 switch (GST_EVENT_TYPE (info->data)) {
3458 case GST_EVENT_SEGMENT:
3459 *segment_seqnum = gst_event_get_seqnum (info->data);
3465 return GST_PAD_PROBE_OK;
3471 GstRTSPStream *stream;
3472 } CopyStickyEventsData;
3475 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
3477 CopyStickyEventsData *data = user_data;
3478 GstEvent *new_event;
3480 GST_DEBUG_OBJECT (data->stream->srcpad, "send sticky event %" GST_PTR_FORMAT,
3483 gst_rtspsrc_update_src_event (data->src, data->stream,
3484 gst_event_ref (*event));
3485 gst_pad_store_sticky_event (data->stream->srcpad, new_event);
3486 gst_event_unref (new_event);
3492 add_backchannel_fakesink (GstRTSPSrc * src, GstRTSPStream * stream,
3496 GstElement *fakesink;
3498 fakesink = gst_element_factory_make ("fakesink", NULL);
3499 if (fakesink == NULL) {
3500 GST_ERROR_OBJECT (src, "no fakesink");
3504 sinkpad = gst_element_get_static_pad (fakesink, "sink");
3506 GST_DEBUG_OBJECT (src, "backchannel stream %p, hooking fakesink", stream);
3508 gst_bin_add (GST_BIN_CAST (src), fakesink);
3509 if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK) {
3510 GST_WARNING_OBJECT (src, "could not link to fakesink");
3514 gst_object_unref (sinkpad);
3516 gst_element_sync_state_with_parent (fakesink);
3520 /* this callback is called when the session manager generated a new src pad with
3521 * payloaded RTP packets. We simply ghost the pad here. */
3523 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
3526 GstPadTemplate *template;
3529 GstRTSPStream *stream;
3531 GstPad *internal_src;
3532 CopyStickyEventsData copy_sticky_events_data;
3534 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
3536 GST_RTSP_STATE_LOCK (src);
3538 name = gst_object_get_name (GST_OBJECT_CAST (pad));
3539 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
3540 goto unknown_stream;
3542 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
3544 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
3546 goto unknown_stream;
3549 stream->ssrc = ssrc;
3551 /* we'll add it later see below */
3552 stream->added = TRUE;
3554 /* check if we added all streams */
3556 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
3557 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
3559 GST_DEBUG_OBJECT (src, "stream %p, container %d, added %d, setup %d",
3560 ostream, ostream->container, ostream->added, ostream->setup);
3562 /* if we find a stream for which we did a setup that is not added, we
3563 * need to wait some more */
3564 if (ostream->setup && !ostream->added) {
3569 GST_RTSP_STATE_UNLOCK (src);
3571 /* create a new pad we will use to stream to */
3572 template = gst_static_pad_template_get (&rtptemplate);
3573 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
3574 gst_object_unref (template);
3577 /* We intercept and modify the stream start event */
3579 GST_PAD (gst_proxy_pad_get_internal (GST_PROXY_PAD (stream->srcpad)));
3580 gst_pad_set_element_private (internal_src, stream);
3581 gst_pad_set_event_function (internal_src, gst_rtspsrc_handle_src_sink_event);
3583 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3584 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3585 gst_pad_set_active (stream->srcpad, TRUE);
3587 copy_sticky_events_data.src = src;
3588 copy_sticky_events_data.stream = stream;
3589 gst_pad_sticky_events_foreach (pad, copy_sticky_events,
3590 ©_sticky_events_data);
3592 gst_object_unref (internal_src);
3594 /* don't add the srcpad if this is a sendonly stream */
3595 if (stream->is_backchannel)
3596 add_backchannel_fakesink (src, stream, stream->srcpad);
3598 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3601 GST_DEBUG_OBJECT (src, "We added all streams");
3602 /* when we get here, all stream are added and we can fire the no-more-pads
3604 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
3612 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
3613 GST_RTSP_STATE_UNLOCK (src);
3620 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
3624 len = stream->ptmap->len;
3625 for (i = 0; i < len; i++) {
3626 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3634 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
3636 GstRTSPStream *stream;
3639 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
3641 GST_RTSP_STATE_LOCK (src);
3642 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
3644 goto unknown_stream;
3646 if ((caps = stream_get_caps_for_pt (stream, pt)))
3647 gst_caps_ref (caps);
3648 GST_RTSP_STATE_UNLOCK (src);
3654 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
3655 GST_RTSP_STATE_UNLOCK (src);
3661 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
3663 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
3665 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
3669 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
3671 GstRTSPSrc *src = stream->parent;
3674 g_object_get (source, "ssrc", &ssrc, NULL);
3676 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
3677 ssrc, stream->ssrc, stream->id);
3679 if (ssrc == stream->ssrc)
3680 gst_rtspsrc_do_stream_eos (src, stream);
3684 on_timeout_common (GObject * session, GObject * source, GstRTSPStream * stream)
3686 GstRTSPSrc *src = stream->parent;
3689 g_object_get (source, "ssrc", &ssrc, NULL);
3691 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
3692 ssrc, stream->ssrc, stream->id);
3694 if (ssrc == stream->ssrc) {
3696 gboolean all_eos = TRUE;
3698 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
3701 /* Only EOS all streams at once if they're all EOS. Otherwise it is
3702 * possible for timed out streams to reappear at a later time time: they
3703 * might just be inactive currently.
3706 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3707 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3709 /* Skip streams that were not set up at all */
3720 GST_DEBUG_OBJECT (src, "sending EOS on all streams");
3721 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3722 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3723 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
3730 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
3732 GstRTSPSrc *src = stream->parent;
3734 /* timeout, post element message */
3735 gst_element_post_message (GST_ELEMENT_CAST (src),
3736 gst_message_new_element (GST_OBJECT_CAST (src),
3737 gst_structure_new ("GstRTSPSrcTimeout", "cause",
3738 GST_TYPE_RTSP_SRC_TIMEOUT_CAUSE, GST_RTSP_SRC_TIMEOUT_CAUSE_RTCP,
3739 "stream-number", G_TYPE_INT, stream->id, "ssrc", G_TYPE_UINT,
3740 stream->ssrc, NULL)));
3742 /* In non-live mode, timeouts can occur if we are PAUSED, this doesn't mean
3743 * the stream is EOS, it may simply be blocked */
3744 if (src->is_live || !src->interleaved)
3745 on_timeout_common (session, source, stream);
3749 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
3751 GstRTSPStream *stream;
3753 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
3755 /* get stream for session */
3756 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
3758 gst_rtspsrc_do_stream_eos (src, stream);
3763 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
3765 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
3768 stream->eos = FALSE;
3772 set_manager_buffer_mode (GstRTSPSrc * src)
3774 GObjectClass *klass;
3776 if (src->manager == NULL)
3779 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3781 if (!g_object_class_find_property (klass, "buffer-mode"))
3784 if (src->buffer_mode != BUFFER_MODE_AUTO) {
3785 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
3790 GST_DEBUG_OBJECT (src,
3791 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
3793 if (src->provided_clock) {
3794 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
3796 if (clock == src->provided_clock) {
3797 GST_DEBUG_OBJECT (src, "selected synced");
3798 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
3801 gst_object_unref (clock);
3806 /* Otherwise fall-through and use another buffer mode */
3808 gst_object_unref (clock);
3811 GST_DEBUG_OBJECT (src, "auto buffering mode");
3812 if (src->use_buffering) {
3813 GST_DEBUG_OBJECT (src, "selected buffer");
3814 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
3816 GST_DEBUG_OBJECT (src, "selected slave");
3817 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
3822 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
3826 GstMIKEYMessage *msg = stream->mikey;
3828 GST_DEBUG ("request key SSRC %u", ssrc);
3830 caps = gst_caps_ref (stream_get_caps_for_pt (stream, stream->default_pt));
3831 caps = gst_caps_make_writable (caps);
3833 /* parse crypto sessions and look for the SSRC rollover counter */
3834 msg = stream->mikey;
3835 for (i = 0; msg && i < gst_mikey_message_get_n_cs (msg); i++) {
3836 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
3838 if (ssrc == map->ssrc) {
3839 gst_caps_set_simple (caps, "roc", G_TYPE_UINT, map->roc, NULL);
3848 request_rtp_decoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
3850 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3851 if (stream->id != session)
3854 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3855 stream->profile != GST_RTSP_PROFILE_SAVPF)
3858 if (stream->srtpdec == NULL) {
3861 name = g_strdup_printf ("srtpdec_%u", session);
3862 stream->srtpdec = gst_element_factory_make ("srtpdec", name);
3865 if (stream->srtpdec == NULL) {
3866 GST_ELEMENT_ERROR (stream->parent, CORE, MISSING_PLUGIN, (NULL),
3867 ("no srtpdec element present!"));
3870 g_signal_connect (stream->srtpdec, "request-key",
3871 (GCallback) request_key, stream);
3873 return gst_object_ref (stream->srtpdec);
3877 request_rtcp_encoder (GstElement * rtpbin, guint session,
3878 GstRTSPStream * stream)
3883 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3884 if (stream->id != session)
3887 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3888 stream->profile != GST_RTSP_PROFILE_SAVPF)
3891 if (stream->srtpenc == NULL) {
3894 name = g_strdup_printf ("srtpenc_%u", session);
3895 stream->srtpenc = gst_element_factory_make ("srtpenc", name);
3898 if (stream->srtpenc == NULL) {
3899 GST_ELEMENT_ERROR (stream->parent, CORE, MISSING_PLUGIN, (NULL),
3900 ("no srtpenc element present!"));
3904 /* get RTCP crypto parameters from caps */
3905 s = gst_caps_get_structure (stream->srtcpparams, 0);
3909 GType ciphertype, authtype;
3910 GValue rtcp_cipher = G_VALUE_INIT, rtcp_auth = G_VALUE_INIT;
3912 ciphertype = g_type_from_name ("GstSrtpCipherType");
3913 authtype = g_type_from_name ("GstSrtpAuthType");
3914 g_value_init (&rtcp_cipher, ciphertype);
3915 g_value_init (&rtcp_auth, authtype);
3917 str = gst_structure_get_string (s, "srtcp-cipher");
3918 gst_value_deserialize (&rtcp_cipher, str);
3919 str = gst_structure_get_string (s, "srtcp-auth");
3920 gst_value_deserialize (&rtcp_auth, str);
3921 gst_structure_get (s, "srtp-key", GST_TYPE_BUFFER, &buf, NULL);
3923 g_object_set_property (G_OBJECT (stream->srtpenc), "rtp-cipher",
3925 g_object_set_property (G_OBJECT (stream->srtpenc), "rtp-auth",
3927 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-cipher",
3929 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-auth",
3931 g_object_set (stream->srtpenc, "key", buf, NULL);
3933 g_value_unset (&rtcp_cipher);
3934 g_value_unset (&rtcp_auth);
3935 gst_buffer_unref (buf);
3938 name = g_strdup_printf ("rtcp_sink_%d", session);
3939 pad = gst_element_request_pad_simple (stream->srtpenc, name);
3941 gst_object_unref (pad);
3943 return gst_object_ref (stream->srtpenc);
3947 request_aux_receiver (GstElement * rtpbin, guint sessid, GstRTSPSrc * src)
3949 GstElement *rtx, *bin;
3952 GstRTSPStream *stream;
3954 stream = find_stream (src, &sessid, (gpointer) find_stream_by_id);
3956 GST_WARNING_OBJECT (src, "Stream %u not found", sessid);
3960 GST_INFO_OBJECT (src, "creating retransmision receiver for session %u "
3961 "with map %" GST_PTR_FORMAT, sessid, stream->rtx_pt_map);
3962 bin = gst_bin_new (NULL);
3963 rtx = gst_element_factory_make ("rtprtxreceive", NULL);
3964 g_object_set (rtx, "payload-type-map", stream->rtx_pt_map, NULL);
3965 gst_bin_add (GST_BIN (bin), rtx);
3967 pad = gst_element_get_static_pad (rtx, "src");
3968 name = g_strdup_printf ("src_%u", sessid);
3969 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3971 gst_object_unref (pad);
3973 pad = gst_element_get_static_pad (rtx, "sink");
3974 name = g_strdup_printf ("sink_%u", sessid);
3975 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3977 gst_object_unref (pad);
3983 add_retransmission (GstRTSPSrc * src, GstRTSPTransport * transport)
3987 gboolean do_retransmission = FALSE;
3989 if (transport->trans != GST_RTSP_TRANS_RTP)
3991 if (transport->profile != GST_RTSP_PROFILE_AVPF &&
3992 transport->profile != GST_RTSP_PROFILE_SAVPF)
3995 signal_id = g_signal_lookup ("request-aux-receiver",
3996 G_OBJECT_TYPE (src->manager));
3997 /* there's already something connected */
3998 if (g_signal_handler_find (src->manager, G_SIGNAL_MATCH_ID, signal_id, 0,
3999 NULL, NULL, NULL) != 0) {
4000 GST_DEBUG_OBJECT (src, "Not adding RTX AUX element as "
4001 "\"request-aux-receiver\" signal is "
4002 "already used by the application");
4006 /* build the retransmission payload type map */
4007 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4008 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4009 gboolean do_retransmission_stream = FALSE;
4012 if (stream->rtx_pt_map)
4013 gst_structure_free (stream->rtx_pt_map);
4014 stream->rtx_pt_map = gst_structure_new_empty ("application/x-rtp-pt-map");
4016 for (i = 0; i < stream->ptmap->len; i++) {
4017 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
4018 GstStructure *s = gst_caps_get_structure (item->caps, 0);
4019 const gchar *encoding;
4021 /* we only care about RTX streams */
4022 if ((encoding = gst_structure_get_string (s, "encoding-name"))
4023 && g_strcmp0 (encoding, "RTX") == 0) {
4024 const gchar *stream_pt_s;
4027 if (gst_structure_get_int (s, "payload", &rtx_pt)
4028 && (stream_pt_s = gst_structure_get_string (s, "apt"))) {
4031 gst_structure_set (stream->rtx_pt_map, stream_pt_s, G_TYPE_UINT,
4033 do_retransmission_stream = TRUE;
4039 if (do_retransmission_stream) {
4040 GST_DEBUG_OBJECT (src, "built retransmission payload map for stream "
4041 "id %i: %" GST_PTR_FORMAT, stream->id, stream->rtx_pt_map);
4042 do_retransmission = TRUE;
4044 GST_DEBUG_OBJECT (src, "no retransmission payload map for stream "
4045 "id %i", stream->id);
4046 gst_structure_free (stream->rtx_pt_map);
4047 stream->rtx_pt_map = NULL;
4051 if (do_retransmission) {
4052 GST_DEBUG_OBJECT (src, "Enabling retransmissions");
4054 g_object_set (src->manager, "do-retransmission", TRUE, NULL);
4056 /* enable RFC4588 retransmission handling by setting rtprtxreceive
4057 * as the "aux" element of rtpbin */
4058 g_signal_connect (src->manager, "request-aux-receiver",
4059 (GCallback) request_aux_receiver, src);
4061 GST_DEBUG_OBJECT (src,
4062 "Not enabling retransmissions as no stream had a retransmission payload map");
4066 /* try to get and configure a manager */
4068 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
4069 GstRTSPTransport * transport)
4071 const gchar *manager;
4073 GstStateChangeReturn ret;
4076 goto use_no_manager;
4078 /* find a manager */
4079 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
4083 GST_DEBUG_OBJECT (src, "using manager %s", manager);
4085 /* configure the manager */
4086 if (src->manager == NULL) {
4087 GObjectClass *klass;
4089 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
4091 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
4095 goto use_no_manager;
4097 if (!(src->manager = gst_element_factory_make (manager, "manager")))
4098 goto manager_failed;
4101 /* we manage this element */
4102 gst_element_set_locked_state (src->manager, TRUE);
4103 gst_bin_add (GST_BIN_CAST (src), src->manager);
4105 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
4106 if (ret == GST_STATE_CHANGE_FAILURE)
4107 goto start_manager_failure;
4109 g_object_set (src->manager, "latency", src->latency, NULL);
4111 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
4113 if (g_object_class_find_property (klass, "ntp-sync")) {
4114 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
4117 if (g_object_class_find_property (klass, "rfc7273-sync")) {
4118 g_object_set (src->manager, "rfc7273-sync", src->rfc7273_sync, NULL);
4121 if (g_object_class_find_property (klass, "add-reference-timestamp-meta")) {
4122 g_object_set (src->manager, "add-reference-timestamp-meta",
4123 src->add_reference_timestamp_meta, NULL);
4126 if (src->use_pipeline_clock) {
4127 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
4128 g_object_set (src->manager, "use-pipeline-clock", TRUE, NULL);
4131 if (g_object_class_find_property (klass, "ntp-time-source")) {
4132 g_object_set (src->manager, "ntp-time-source", src->ntp_time_source,
4137 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
4138 g_object_set (src->manager, "sdes", src->sdes, NULL);
4141 if (g_object_class_find_property (klass, "drop-on-latency")) {
4142 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
4146 if (g_object_class_find_property (klass, "max-rtcp-rtp-time-diff")) {
4147 g_object_set (src->manager, "max-rtcp-rtp-time-diff",
4148 src->max_rtcp_rtp_time_diff, NULL);
4151 if (g_object_class_find_property (klass, "max-ts-offset-adjustment")) {
4152 g_object_set (src->manager, "max-ts-offset-adjustment",
4153 src->max_ts_offset_adjustment, NULL);
4156 if (g_object_class_find_property (klass, "max-ts-offset")) {
4157 gint64 max_ts_offset;
4159 /* setting max-ts-offset in the manager has side effects so only do it
4160 * if the value differs */
4161 g_object_get (src->manager, "max-ts-offset", &max_ts_offset, NULL);
4162 if (max_ts_offset != src->max_ts_offset) {
4163 g_object_set (src->manager, "max-ts-offset", src->max_ts_offset,
4168 /* buffer mode pauses are handled by adding offsets to buffer times,
4169 * but some depayloaders may have a hard time syncing output times
4170 * with such input times, e.g. container ones, most notably ASF */
4171 /* TODO alternatives are having an event that indicates these shifts,
4172 * or having rtsp extensions provide suggestion on buffer mode */
4173 /* valid duration implies not likely live pipeline,
4174 * so slaving in jitterbuffer does not make much sense
4175 * (and might mess things up due to bursts) */
4176 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
4177 src->segment.duration && stream->container) {
4178 src->use_buffering = TRUE;
4180 src->use_buffering = FALSE;
4183 set_manager_buffer_mode (src);
4185 /* connect to signals */
4186 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
4188 src->manager_sig_id =
4189 g_signal_connect (src->manager, "pad-added",
4190 (GCallback) new_manager_pad, src);
4191 src->manager_ptmap_id =
4192 g_signal_connect (src->manager, "request-pt-map",
4193 (GCallback) request_pt_map, src);
4195 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
4198 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
4201 if (src->do_retransmission)
4202 add_retransmission (src, transport);
4204 g_signal_connect (src->manager, "request-rtp-decoder",
4205 (GCallback) request_rtp_decoder, stream);
4206 g_signal_connect (src->manager, "request-rtcp-decoder",
4207 (GCallback) request_rtp_decoder, stream);
4208 g_signal_connect (src->manager, "request-rtcp-encoder",
4209 (GCallback) request_rtcp_encoder, stream);
4211 /* we stream directly to the manager, get some pads. Each RTSP stream goes
4212 * into a separate RTP session. */
4213 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
4214 stream->channelpad[0] = gst_element_request_pad_simple (src->manager, name);
4216 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
4217 stream->channelpad[1] = gst_element_request_pad_simple (src->manager, name);
4220 /* now configure the bandwidth in the manager */
4221 if (g_signal_lookup ("get-internal-session",
4222 G_OBJECT_TYPE (src->manager)) != 0) {
4223 GObject *rtpsession;
4225 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
4228 GstRTPProfile rtp_profile;
4230 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
4232 stream->session = rtpsession;
4234 if (stream->as_bandwidth != -1) {
4235 GST_INFO_OBJECT (src, "setting AS: %f",
4236 (gdouble) (stream->as_bandwidth * 1000));
4237 g_object_set (rtpsession, "bandwidth",
4238 (gdouble) (stream->as_bandwidth * 1000), NULL);
4240 if (stream->rr_bandwidth != -1) {
4241 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
4242 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
4245 if (stream->rs_bandwidth != -1) {
4246 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
4247 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
4251 switch (stream->profile) {
4252 case GST_RTSP_PROFILE_AVPF:
4253 rtp_profile = GST_RTP_PROFILE_AVPF;
4255 case GST_RTSP_PROFILE_SAVP:
4256 rtp_profile = GST_RTP_PROFILE_SAVP;
4258 case GST_RTSP_PROFILE_SAVPF:
4259 rtp_profile = GST_RTP_PROFILE_SAVPF;
4261 case GST_RTSP_PROFILE_AVP:
4263 rtp_profile = GST_RTP_PROFILE_AVP;
4267 g_object_set (rtpsession, "rtp-profile", rtp_profile, NULL);
4269 g_object_set (rtpsession, "probation", src->probation, NULL);
4271 g_object_set (rtpsession, "internal-ssrc", stream->send_ssrc, NULL);
4273 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
4275 g_signal_connect (rtpsession, "on-bye-timeout",
4276 (GCallback) on_timeout_common, stream);
4277 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
4279 g_signal_connect (rtpsession, "on-ssrc-active",
4280 (GCallback) on_ssrc_active, stream);
4291 GST_DEBUG_OBJECT (src, "cannot get a session manager");
4296 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
4299 start_manager_failure:
4301 GST_DEBUG_OBJECT (src, "could not start session manager");
4306 /* free the UDP sources allocated when negotiating a transport.
4307 * This function is called when the server negotiated to a transport where the
4308 * UDP sources are not needed anymore, such as TCP or multicast. */
4310 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
4314 for (i = 0; i < 2; i++) {
4315 if (stream->udpsrc[i]) {
4316 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
4317 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
4318 gst_object_unref (stream->udpsrc[i]);
4319 stream->udpsrc[i] = NULL;
4324 /* for TCP, create pads to send and receive data to and from the manager and to
4325 * intercept various events and queries
4328 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
4329 GstRTSPTransport * transport, GstPad ** outpad)
4332 GstPadTemplate *template;
4333 GstPad *pad0, *pad1;
4335 /* configure for interleaved delivery, nothing needs to be done
4336 * here, the loop function will call the chain functions of the
4337 * session manager. */
4338 stream->channel[0] = transport->interleaved.min;
4339 stream->channel[1] = transport->interleaved.max;
4340 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
4341 stream->channel[0], stream->channel[1]);
4343 /* we can remove the allocated UDP ports now */
4344 gst_rtspsrc_stream_free_udp (stream);
4346 /* no session manager, send data to srcpad directly */
4347 if (!stream->channelpad[0]) {
4348 GST_DEBUG_OBJECT (src, "no manager, creating pad");
4350 /* create a new pad we will use to stream to */
4351 name = g_strdup_printf ("stream_%u", stream->id);
4352 template = gst_static_pad_template_get (&rtptemplate);
4353 stream->channelpad[0] = gst_pad_new_from_template (template, name);
4354 gst_object_unref (template);
4357 /* set caps and activate */
4358 gst_pad_use_fixed_caps (stream->channelpad[0]);
4359 gst_pad_set_active (stream->channelpad[0], TRUE);
4361 *outpad = gst_object_ref (stream->channelpad[0]);
4363 GST_DEBUG_OBJECT (src, "using manager source pad");
4365 template = gst_static_pad_template_get (&anysrctemplate);
4367 /* allocate pads for sending the channel data into the manager */
4368 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
4369 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
4370 gst_object_unref (stream->channelpad[0]);
4371 stream->channelpad[0] = pad0;
4372 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
4373 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
4374 gst_pad_set_element_private (pad0, src);
4375 gst_pad_set_active (pad0, TRUE);
4377 if (stream->channelpad[1]) {
4378 /* if we have a sinkpad for the other channel, create a pad and link to the
4380 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
4381 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
4382 gst_pad_link_full (pad1, stream->channelpad[1],
4383 GST_PAD_LINK_CHECK_NOTHING);
4384 gst_object_unref (stream->channelpad[1]);
4385 stream->channelpad[1] = pad1;
4386 gst_pad_set_active (pad1, TRUE);
4388 gst_object_unref (template);
4390 /* setup RTCP transport back to the server if we have to. */
4391 if (src->manager && src->do_rtcp) {
4394 template = gst_static_pad_template_get (&anysinktemplate);
4396 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
4397 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
4398 gst_pad_set_element_private (stream->rtcppad, stream);
4399 gst_pad_set_active (stream->rtcppad, TRUE);
4401 /* get session RTCP pad */
4402 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
4403 pad = gst_element_request_pad_simple (src->manager, name);
4408 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
4409 gst_object_unref (pad);
4412 gst_object_unref (template);
4418 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
4419 GstRTSPTransport * transport, const gchar ** destination, gint * min,
4420 gint * max, guint * ttl)
4422 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
4424 if (!(*destination = transport->destination))
4425 *destination = stream->destination;
4428 /* transport first */
4429 *min = transport->port.min;
4430 *max = transport->port.max;
4431 if (*min == -1 && *max == -1) {
4432 /* then try from SDP */
4433 if (stream->port != 0) {
4434 *min = stream->port;
4435 *max = stream->port + 1;
4441 if (!(*ttl = transport->ttl))
4446 /* first take the source, then the endpoint to figure out where to send
4448 if (!(*destination = transport->source)) {
4449 if (src->conninfo.connection)
4450 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
4451 else if (stream->conninfo.connection)
4453 gst_rtsp_connection_get_ip (stream->conninfo.connection);
4457 /* for unicast we only expect the ports here */
4458 *min = transport->server_port.min;
4459 *max = transport->server_port.max;
4465 element_make_from_addr (const GstURIType type, const char *addr_s,
4466 int port, const char *name, GError ** error)
4469 GstElement *element = NULL;
4472 addr = g_inet_address_new_from_string (addr_s);
4474 /* Address is a hostname, not an IP address */
4475 uri = g_strdup_printf ("udp://%s:%i", addr_s, port);
4477 switch (g_inet_address_get_family (addr)) {
4478 case G_SOCKET_FAMILY_IPV6:
4479 uri = g_strdup_printf ("udp://[%s]:%i", addr_s, port);
4481 case G_SOCKET_FAMILY_INVALID:
4482 GST_ERROR ("Unknown family type for %s", addr_s);
4484 case G_SOCKET_FAMILY_UNIX:
4485 GST_ERROR ("Unexpected family type UNIX for %s", addr_s);
4487 case G_SOCKET_FAMILY_IPV4:
4488 uri = g_strdup_printf ("udp://%s:%i", addr_s, port);
4493 element = gst_element_make_from_uri (type, uri, name, error);
4495 g_clear_object (&addr);
4500 /* For multicast create UDP sources and join the multicast group. */
4502 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
4503 GstRTSPTransport * transport, GstPad ** outpad)
4505 const gchar *destination;
4508 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
4510 /* we can remove the allocated UDP ports now */
4511 gst_rtspsrc_stream_free_udp (stream);
4513 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
4516 /* we need a destination now */
4517 if (destination == NULL)
4518 goto no_destination;
4520 /* we really need ports now or we won't be able to receive anything at all */
4521 if (min == -1 && max == -1)
4524 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
4525 destination, min, max);
4527 /* creating UDP source for RTP */
4530 element_make_from_addr (GST_URI_SRC, destination, min, NULL, NULL);
4531 if (stream->udpsrc[0] == NULL)
4534 /* take ownership */
4535 gst_object_ref_sink (stream->udpsrc[0]);
4537 if (src->udp_buffer_size != 0)
4538 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
4539 src->udp_buffer_size, NULL);
4541 if (src->multi_iface != NULL)
4542 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
4543 src->multi_iface, NULL);
4546 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
4547 gst_element_set_state (stream->udpsrc[0], GST_STATE_READY);
4550 /* creating another UDP source for RTCP */
4555 element_make_from_addr (GST_URI_SRC, destination, max, NULL, NULL);
4556 if (stream->udpsrc[1] == NULL)
4559 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
4560 stream->profile == GST_RTSP_PROFILE_SAVPF)
4561 caps = gst_caps_new_empty_simple ("application/x-srtcp");
4563 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4564 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
4565 gst_caps_unref (caps);
4567 /* take ownership */
4568 gst_object_ref_sink (stream->udpsrc[1]);
4570 if (src->multi_iface != NULL)
4571 g_object_set (G_OBJECT (stream->udpsrc[1]), "multicast-iface",
4572 src->multi_iface, NULL);
4574 gst_element_set_state (stream->udpsrc[1], GST_STATE_READY);
4581 GST_DEBUG_OBJECT (src, "no UDP source element found");
4586 GST_DEBUG_OBJECT (src, "no destination found");
4591 GST_DEBUG_OBJECT (src, "no ports found");
4596 /* configure the remainder of the UDP ports */
4598 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
4599 GstRTSPTransport * transport, GstPad ** outpad)
4601 /* we manage the UDP elements now. For unicast, the UDP sources where
4602 * allocated in the stream when we suggested a transport. */
4603 if (stream->udpsrc[0]) {
4606 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
4607 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
4609 GST_DEBUG_OBJECT (src, "setting up UDP source");
4611 /* configure a timeout on the UDP port. When the timeout message is
4612 * posted, we assume UDP transport is not possible. We reconnect using TCP
4614 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
4615 src->udp_timeout * 1000, NULL);
4617 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt)))
4618 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
4620 /* get output pad of the UDP source. */
4621 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
4623 /* save it so we can unblock */
4624 stream->blockedpad = *outpad;
4626 /* configure pad block on the pad. As soon as there is dataflow on the
4627 * UDP source, we know that UDP is not blocked by a firewall and we can
4628 * configure all the streams to let the application autoplug decoders. */
4630 gst_pad_add_probe (stream->blockedpad,
4631 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
4632 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocked, src, NULL);
4634 gst_pad_add_probe (stream->blockedpad,
4635 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM, udpsrc_probe_cb,
4636 &(stream->segment_seqnum[0]), NULL);
4638 if (stream->channelpad[0]) {
4639 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
4640 /* configure for UDP delivery, we need to connect the UDP pads to
4641 * the session plugin. */
4642 gst_pad_link_full (*outpad, stream->channelpad[0],
4643 GST_PAD_LINK_CHECK_NOTHING);
4644 gst_object_unref (*outpad);
4646 /* we connected to pad-added signal to get pads from the manager */
4648 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
4653 if (stream->udpsrc[1]) {
4656 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
4657 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
4659 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
4660 stream->profile == GST_RTSP_PROFILE_SAVPF)
4661 caps = gst_caps_new_empty_simple ("application/x-srtcp");
4663 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4664 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
4665 gst_caps_unref (caps);
4667 if (stream->channelpad[1]) {
4670 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
4672 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
4673 gst_pad_add_probe (pad,
4674 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM, udpsrc_probe_cb,
4675 &(stream->segment_seqnum[1]), NULL);
4676 gst_pad_link_full (pad, stream->channelpad[1],
4677 GST_PAD_LINK_CHECK_NOTHING);
4678 gst_object_unref (pad);
4680 /* leave unlinked */
4686 /* configure the UDP sink back to the server for status reports */
4688 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
4689 GstRTSPStream * stream, GstRTSPTransport * transport)
4692 gint rtp_port, rtcp_port;
4693 gboolean do_rtp, do_rtcp;
4694 const gchar *destination;
4699 /* get transport info */
4700 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
4701 &rtp_port, &rtcp_port, &ttl);
4703 /* see what we need to do */
4704 do_rtp = (rtp_port != -1);
4705 /* it's possible that the server does not want us to send RTCP in which case
4707 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
4709 /* we need a destination when we have RTP or RTCP ports */
4710 if (destination == NULL && (do_rtp || do_rtcp))
4711 goto no_destination;
4713 /* try to construct the fakesrc to the RTP port of the server to open up any
4714 * NAT firewalls or, if backchannel, construct an appsrc */
4716 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
4719 stream->udpsink[0] = element_make_from_addr (GST_URI_SINK, destination,
4720 rtp_port, NULL, NULL);
4721 if (stream->udpsink[0] == NULL)
4722 goto no_sink_element;
4724 /* don't join multicast group, we will have the source socket do that */
4725 /* no sync or async state changes needed */
4726 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
4727 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
4729 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
4731 if (stream->udpsrc[0]) {
4732 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
4733 * so that NAT firewalls will open a hole for us */
4734 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
4738 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
4739 /* configure socket and make sure udpsink does not close it when shutting
4740 * down, it belongs to udpsrc after all. */
4741 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
4742 "close-socket", FALSE, NULL);
4743 g_object_unref (socket);
4746 if (stream->is_backchannel) {
4747 /* appsrc is for the app to shovel data using push-backchannel-buffer */
4748 stream->rtpsrc = gst_element_factory_make ("appsrc", NULL);
4749 if (stream->rtpsrc == NULL)
4750 goto no_appsrc_element;
4752 /* interal use only, don't emit signals */
4753 g_object_set (G_OBJECT (stream->rtpsrc), "emit-signals", TRUE,
4754 "is-live", TRUE, NULL);
4756 /* the source for the dummy packets to open up NAT */
4757 stream->rtpsrc = gst_element_factory_make ("fakesrc", NULL);
4758 if (stream->rtpsrc == NULL)
4759 goto no_fakesrc_element;
4761 /* random data in 5 buffers, a size of 200 bytes should be fine */
4762 g_object_set (G_OBJECT (stream->rtpsrc), "filltype", 3, "num-buffers", 5,
4763 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
4766 /* keep everything locked */
4767 gst_element_set_locked_state (stream->udpsink[0], TRUE);
4768 gst_element_set_locked_state (stream->rtpsrc, TRUE);
4770 gst_object_ref (stream->udpsink[0]);
4771 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
4772 gst_object_ref (stream->rtpsrc);
4773 gst_bin_add (GST_BIN_CAST (src), stream->rtpsrc);
4775 gst_element_link_pads_full (stream->rtpsrc, "src", stream->udpsink[0],
4776 "sink", GST_PAD_LINK_CHECK_NOTHING);
4779 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
4782 stream->udpsink[1] = element_make_from_addr (GST_URI_SINK, destination,
4783 rtcp_port, NULL, NULL);
4784 if (stream->udpsink[1] == NULL)
4785 goto no_sink_element;
4787 /* don't join multicast group, we will have the source socket do that */
4788 /* no sync or async state changes needed */
4789 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
4790 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
4792 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
4794 if (stream->udpsrc[1]) {
4795 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
4796 * because some servers check the port number of where it sends RTCP to identify
4797 * the RTCP packets it receives */
4798 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
4802 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
4803 /* configure socket and make sure udpsink does not close it when shutting
4804 * down, it belongs to udpsrc after all. */
4805 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
4806 "close-socket", FALSE, NULL);
4807 g_object_unref (socket);
4810 /* we keep this playing always */
4811 gst_element_set_locked_state (stream->udpsink[1], TRUE);
4812 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
4814 gst_object_ref (stream->udpsink[1]);
4815 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
4817 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
4819 /* get session RTCP pad */
4820 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
4821 pad = gst_element_request_pad_simple (src->manager, name);
4826 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
4827 gst_object_unref (pad);
4836 GST_ERROR_OBJECT (src, "no destination address specified");
4841 GST_ERROR_OBJECT (src, "no UDP sink element found");
4846 GST_ERROR_OBJECT (src, "no appsrc element found");
4851 GST_ERROR_OBJECT (src, "no fakesrc element found");
4856 GST_ERROR_OBJECT (src, "failed to create socket");
4861 /* sets up all elements needed for streaming over the specified transport.
4862 * Does not yet expose the element pads, this will be done when there is actuall
4863 * dataflow detected, which might never happen when UDP is blocked in a
4864 * firewall, for example.
4867 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
4868 GstRTSPTransport * transport)
4871 GstPad *outpad = NULL;
4872 GstPadTemplate *template;
4874 const gchar *media_type;
4877 src = stream->parent;
4879 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
4881 /* get the proper media type for this stream now */
4882 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
4883 goto unknown_transport;
4885 goto unknown_transport;
4887 /* configure the final media type */
4888 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
4890 len = stream->ptmap->len;
4891 for (i = 0; i < len; i++) {
4893 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
4895 if (item->caps == NULL)
4898 s = gst_caps_get_structure (item->caps, 0);
4899 gst_structure_set_name (s, media_type);
4900 /* set ssrc if known */
4901 if (transport->ssrc)
4902 gst_structure_set (s, "ssrc", G_TYPE_UINT, transport->ssrc, NULL);
4905 /* try to get and configure a manager, channelpad[0-1] will be configured with
4906 * the pads for the manager, or NULL when no manager is needed. */
4907 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
4910 switch (transport->lower_transport) {
4911 case GST_RTSP_LOWER_TRANS_TCP:
4912 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
4913 goto transport_failed;
4915 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
4916 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
4917 goto transport_failed;
4918 /* fallthrough, the rest is the same for UDP and MCAST */
4919 case GST_RTSP_LOWER_TRANS_UDP:
4920 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
4921 goto transport_failed;
4922 /* configure udpsinks back to the server for RTCP messages, for the
4923 * dummy RTP messages to open NAT, and for the backchannel */
4924 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
4925 goto transport_failed;
4928 goto unknown_transport;
4931 /* using backchannel and no manager, hence no srcpad for this stream */
4932 if (outpad && stream->is_backchannel) {
4933 add_backchannel_fakesink (src, stream, outpad);
4934 gst_object_unref (outpad);
4935 } else if (outpad) {
4936 GstPad *internal_src;
4938 GST_DEBUG_OBJECT (src, "creating ghostpad for stream %p", stream);
4940 gst_pad_use_fixed_caps (outpad);
4942 /* create ghostpad, don't add just yet, this will be done when we activate
4944 name = g_strdup_printf ("stream_%u", stream->id);
4945 template = gst_static_pad_template_get (&rtptemplate);
4946 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
4947 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
4948 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
4949 gst_object_unref (template);
4952 /* We intercept and modify the stream start event */
4954 GST_PAD (gst_proxy_pad_get_internal (GST_PROXY_PAD (stream->srcpad)));
4955 gst_pad_set_element_private (internal_src, stream);
4956 gst_pad_set_event_function (internal_src,
4957 gst_rtspsrc_handle_src_sink_event);
4958 gst_object_unref (internal_src);
4960 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
4961 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
4963 gst_object_unref (outpad);
4965 /* mark pad as ok */
4966 stream->last_ret = GST_FLOW_OK;
4973 GST_WARNING_OBJECT (src, "failed to configure transport");
4978 GST_WARNING_OBJECT (src, "unknown transport");
4983 GST_WARNING_OBJECT (src, "cannot get a session manager");
4988 /* send a couple of dummy random packets on the receiver RTP port to the server,
4989 * this should make a firewall think we initiated the data transfer and
4990 * hopefully allow packets to go from the sender port to our RTP receiver port */
4992 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
4996 if (src->nat_method != GST_RTSP_NAT_DUMMY)
4999 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5000 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5002 if (!stream->rtpsrc || !stream->udpsink[0])
5005 if (stream->is_backchannel)
5006 GST_DEBUG_OBJECT (src, "starting backchannel stream %p", stream);
5008 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
5010 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
5011 gst_element_set_state (stream->rtpsrc, GST_STATE_NULL);
5012 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
5013 gst_element_set_state (stream->rtpsrc, GST_STATE_PLAYING);
5018 /* Adds the source pads of all configured streams to the element.
5019 * This code is performed when we detected dataflow.
5021 * We detect dataflow from either the _loop function or with pad probes on the
5025 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
5029 GST_DEBUG_OBJECT (src, "activating streams");
5031 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5032 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5034 if (stream->udpsrc[0]) {
5035 /* remove timeout, we are streaming now and timeouts will be handled by
5036 * the session manager and jitter buffer */
5037 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
5039 if (stream->srcpad) {
5040 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
5041 gst_pad_set_active (stream->srcpad, TRUE);
5043 /* if we don't have a session manager, set the caps now. If we have a
5044 * session, we will get a notification of the pad and the caps. */
5045 if (!src->manager) {
5048 caps = stream_get_caps_for_pt (stream, stream->default_pt);
5049 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
5050 gst_pad_set_caps (stream->srcpad, caps);
5053 if (!stream->added) {
5054 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
5055 if (stream->is_backchannel)
5056 add_backchannel_fakesink (src, stream, stream->srcpad);
5058 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
5059 stream->added = TRUE;
5064 /* unblock all pads */
5065 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5066 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5068 if (stream->blockid) {
5069 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
5070 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
5071 stream->blockid = 0;
5079 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
5080 gboolean reset_manager)
5083 guint64 start, stop;
5084 gdouble play_speed, play_scale;
5086 GST_DEBUG_OBJECT (src, "configuring stream caps");
5088 start = segment->rate > 0.0 ? segment->start : segment->stop;
5089 stop = segment->rate > 0.0 ? segment->stop : segment->start;
5090 play_speed = segment->rate;
5091 play_scale = segment->applied_rate;
5093 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5094 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5100 len = stream->ptmap->len;
5101 for (j = 0; j < len; j++) {
5103 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
5105 if (item->caps == NULL)
5108 caps = gst_caps_make_writable (item->caps);
5110 if (stream->timebase != -1)
5111 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
5112 (guint) stream->timebase, NULL);
5113 if (stream->seqbase != -1)
5114 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
5115 (guint) stream->seqbase, NULL);
5116 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
5118 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
5119 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
5120 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
5121 gst_caps_set_simple (caps, "onvif-mode", G_TYPE_BOOLEAN, src->onvif_mode,
5125 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
5128 if (item->pt == stream->default_pt) {
5129 if (stream->udpsrc[0])
5130 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
5131 stream->need_caps = TRUE;
5135 if (reset_manager && src->manager) {
5136 GST_DEBUG_OBJECT (src, "clear session");
5137 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
5141 static GstFlowReturn
5142 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
5147 /* store the value */
5148 stream->last_ret = ret;
5150 /* if it's success we can return the value right away */
5151 if (ret == GST_FLOW_OK)
5154 /* any other error that is not-linked can be returned right
5156 if (ret != GST_FLOW_NOT_LINKED)
5159 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
5160 for (streams = src->streams; streams; streams = g_list_next (streams)) {
5161 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
5163 ret = ostream->last_ret;
5164 /* some other return value (must be SUCCESS but we can return
5165 * other values as well) */
5166 if (ret != GST_FLOW_NOT_LINKED)
5169 /* if we get here, all other pads were unlinked and we return
5170 * NOT_LINKED then */
5176 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
5179 gboolean res = TRUE;
5181 /* only streams that have a connection to the outside world */
5185 switch (GST_EVENT_TYPE (event)) {
5189 case GST_EVENT_FLUSH_STOP:
5190 stream->eos = FALSE;
5196 if (stream->udpsrc[0]) {
5197 GstEvent *sent_event;
5199 if (stream->segment_seqnum[0] != GST_SEQNUM_INVALID) {
5200 sent_event = gst_event_copy (event);
5201 gst_event_set_seqnum (sent_event, stream->segment_seqnum[0]);
5203 sent_event = gst_event_ref (event);
5206 res = gst_element_send_event (stream->udpsrc[0], sent_event);
5207 } else if (stream->channelpad[0]) {
5208 GstEvent *sent_event;
5210 sent_event = gst_event_copy (event);
5211 gst_event_set_seqnum (sent_event, src->seek_seqnum);
5213 if (GST_PAD_IS_SRC (stream->channelpad[0]))
5214 res = gst_pad_push_event (stream->channelpad[0], sent_event);
5216 res = gst_pad_send_event (stream->channelpad[0], sent_event);
5219 if (stream->udpsrc[1]) {
5220 GstEvent *sent_event;
5222 if (stream->segment_seqnum[1] != GST_SEQNUM_INVALID) {
5223 sent_event = gst_event_copy (event);
5224 gst_event_set_seqnum (sent_event, stream->segment_seqnum[1]);
5226 sent_event = gst_event_ref (event);
5229 res &= gst_element_send_event (stream->udpsrc[1], sent_event);
5230 } else if (stream->channelpad[1]) {
5231 GstEvent *sent_event;
5233 sent_event = gst_event_copy (event);
5234 gst_event_set_seqnum (sent_event, src->seek_seqnum);
5236 if (GST_PAD_IS_SRC (stream->channelpad[1]))
5237 res &= gst_pad_push_event (stream->channelpad[1], sent_event);
5239 res &= gst_pad_send_event (stream->channelpad[1], sent_event);
5243 gst_event_unref (event);
5249 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
5252 gboolean res = TRUE;
5254 for (streams = src->streams; streams; streams = g_list_next (streams)) {
5255 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
5257 gst_event_ref (event);
5258 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
5260 gst_event_unref (event);
5266 accept_certificate_cb (GTlsConnection * conn, GTlsCertificate * peer_cert,
5267 GTlsCertificateFlags errors, gpointer user_data)
5269 GstRTSPSrc *src = user_data;
5270 gboolean accept = FALSE;
5272 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ACCEPT_CERTIFICATE], 0, conn,
5273 peer_cert, errors, &accept);
5278 static GstRTSPResult
5279 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
5283 GstRTSPMessage response;
5284 gboolean retry = FALSE;
5285 memset (&response, 0, sizeof (response));
5286 gst_rtsp_message_init (&response);
5288 if (info->connection == NULL) {
5289 if (info->url == NULL) {
5290 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
5291 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
5294 /* create connection */
5295 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
5296 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
5297 goto could_not_create;
5300 gst_rtspsrc_setup_auth (src, &response);
5303 g_free (info->url_str);
5304 info->url_str = gst_rtsp_url_get_request_uri (info->url);
5306 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
5308 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
5309 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
5310 src->tls_validation_flags))
5311 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
5313 if (src->tls_database)
5314 gst_rtsp_connection_set_tls_database (info->connection,
5317 if (src->tls_interaction)
5318 gst_rtsp_connection_set_tls_interaction (info->connection,
5319 src->tls_interaction);
5320 gst_rtsp_connection_set_accept_certificate_func (info->connection,
5321 accept_certificate_cb, src, NULL);
5324 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP) {
5325 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
5326 gst_rtsp_connection_set_ignore_x_server_reply (info->connection,
5327 src->ignore_x_server_reply);
5330 if (src->proxy_host) {
5331 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
5333 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
5338 if (!info->connected) {
5341 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
5342 ("Connecting to %s", info->location));
5343 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
5344 res = gst_rtsp_connection_connect_with_response_usec (info->connection,
5345 src->tcp_timeout, &response);
5347 if (response.type == GST_RTSP_MESSAGE_HTTP_RESPONSE &&
5348 response.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
5349 gst_rtsp_conninfo_close (src, info, TRUE);
5353 retry = FALSE; // we should not retry more than once
5358 if (res == GST_RTSP_OK)
5359 info->connected = TRUE;
5361 goto could_not_connect;
5363 } while (!info->connected && retry);
5365 gst_rtsp_message_unset (&response);
5371 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
5372 gst_rtsp_message_unset (&response);
5377 gchar *str = gst_rtsp_strresult (res);
5378 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
5380 gst_rtsp_message_unset (&response);
5385 gchar *str = gst_rtsp_strresult (res);
5386 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
5388 gst_rtsp_message_unset (&response);
5393 static GstRTSPResult
5394 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
5397 GST_RTSP_STATE_LOCK (src);
5398 if (info->connected) {
5399 GST_DEBUG_OBJECT (src, "closing connection...");
5400 gst_rtsp_connection_close (info->connection);
5401 info->connected = FALSE;
5403 if (free && info->connection) {
5404 /* free connection */
5405 GST_DEBUG_OBJECT (src, "freeing connection...");
5406 gst_rtsp_connection_free (info->connection);
5407 info->connection = NULL;
5408 info->flushing = FALSE;
5410 GST_RTSP_STATE_UNLOCK (src);
5414 static GstRTSPResult
5415 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
5420 GST_DEBUG_OBJECT (src, "reconnecting connection...");
5421 gst_rtsp_conninfo_close (src, info, FALSE);
5422 res = gst_rtsp_conninfo_connect (src, info, async);
5428 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
5432 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
5433 GST_RTSP_STATE_LOCK (src);
5434 if (src->conninfo.connection && src->conninfo.flushing != flush) {
5435 GST_DEBUG_OBJECT (src, "connection flush");
5436 gst_rtsp_connection_flush (src->conninfo.connection, flush);
5437 src->conninfo.flushing = flush;
5439 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5440 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5441 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
5442 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
5443 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
5444 stream->conninfo.flushing = flush;
5447 GST_RTSP_STATE_UNLOCK (src);
5450 static GstRTSPResult
5451 gst_rtspsrc_init_request (GstRTSPSrc * src, GstRTSPMessage * msg,
5452 GstRTSPMethod method, const gchar * uri)
5456 res = gst_rtsp_message_init_request (msg, method, uri);
5460 /* set user-agent */
5461 if (src->user_agent)
5462 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_USER_AGENT, src->user_agent);
5467 /* FIXME, handle server request, reply with OK, for now */
5468 static GstRTSPResult
5469 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
5470 GstRTSPMessage * request)
5472 GstRTSPMessage response = { 0 };
5475 GST_DEBUG_OBJECT (src, "got server request message");
5477 DEBUG_RTSP (src, request);
5479 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
5481 if (res == GST_RTSP_ENOTIMPL) {
5482 /* default implementation, send OK */
5483 GST_DEBUG_OBJECT (src, "prepare OK reply");
5485 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
5490 /* let app parse and reply */
5491 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
5492 0, request, &response);
5494 DEBUG_RTSP (src, &response);
5496 res = gst_rtspsrc_connection_send (src, conninfo, &response, 0);
5500 gst_rtsp_message_unset (&response);
5501 } else if (res == GST_RTSP_EEOF)
5509 gst_rtsp_message_unset (&response);
5514 /* send server keep-alive */
5515 static GstRTSPResult
5516 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
5518 GstRTSPMessage request = { 0 };
5520 GstRTSPMethod method;
5521 const gchar *control;
5523 if (src->do_rtsp_keep_alive == FALSE) {
5524 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
5525 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
5529 GST_DEBUG_OBJECT (src, "creating server keep-alive");
5531 /* find a method to use for keep-alive */
5532 if (src->methods & GST_RTSP_GET_PARAMETER)
5533 method = GST_RTSP_GET_PARAMETER;
5535 method = GST_RTSP_OPTIONS;
5537 control = get_aggregate_control (src);
5538 if (control == NULL)
5541 res = gst_rtspsrc_init_request (src, &request, method, control);
5545 request.type_data.request.version = src->version;
5547 res = gst_rtspsrc_connection_send (src, &src->conninfo, &request, 0);
5551 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
5552 gst_rtsp_message_unset (&request);
5559 GST_WARNING_OBJECT (src, "no control url to send keepalive");
5564 gchar *str = gst_rtsp_strresult (res);
5566 gst_rtsp_message_unset (&request);
5567 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
5568 ("Could not send keep-alive. (%s)", str));
5574 static GstFlowReturn
5575 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
5577 GstFlowReturn ret = GST_FLOW_OK;
5579 GstRTSPStream *stream;
5580 GstPad *outpad = NULL;
5586 channel = message->type_data.data.channel;
5588 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
5590 goto unknown_stream;
5592 if (channel == stream->channel[0]) {
5593 outpad = stream->channelpad[0];
5595 } else if (channel == stream->channel[1]) {
5596 outpad = stream->channelpad[1];
5602 /* take a look at the body to figure out what we have */
5603 gst_rtsp_message_get_body (message, &data, &size);
5605 goto invalid_length;
5607 /* channels are not correct on some servers, do extra check */
5608 if (data[1] >= 200 && data[1] <= 204) {
5609 /* hmm RTCP message switch to the RTCP pad of the same stream. */
5610 outpad = stream->channelpad[1];
5614 /* we have no clue what this is, just ignore then. */
5616 goto unknown_stream;
5618 /* take the message body for further processing */
5619 gst_rtsp_message_steal_body (message, &data, &size);
5621 /* strip the trailing \0 */
5624 buf = gst_buffer_new ();
5625 gst_buffer_append_memory (buf,
5626 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
5628 /* don't need message anymore */
5629 gst_rtsp_message_unset (message);
5631 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
5634 if (src->need_activate) {
5641 /* generate an SHA256 sum of the URI */
5642 cs = g_checksum_new (G_CHECKSUM_SHA256);
5643 uri = src->conninfo.location;
5644 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
5646 for (streams = src->streams; streams; streams = g_list_next (streams)) {
5647 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
5650 /* Activate in advance so that the stream-start event is registered */
5651 if (stream->srcpad) {
5652 gst_pad_set_active (stream->srcpad, TRUE);
5656 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
5658 event = gst_event_new_stream_start (stream_id);
5660 gst_rtspsrc_stream_start_event_add_group_id (src, event);
5663 gst_rtspsrc_stream_push_event (src, ostream, event);
5665 if ((caps = stream_get_caps_for_pt (ostream, ostream->default_pt))) {
5666 /* only streams that have a connection to the outside world */
5667 if (ostream->setup) {
5668 if (ostream->udpsrc[0]) {
5669 gst_element_send_event (ostream->udpsrc[0],
5670 gst_event_new_caps (caps));
5671 } else if (ostream->channelpad[0]) {
5672 if (GST_PAD_IS_SRC (ostream->channelpad[0]))
5673 gst_pad_push_event (ostream->channelpad[0],
5674 gst_event_new_caps (caps));
5676 gst_pad_send_event (ostream->channelpad[0],
5677 gst_event_new_caps (caps));
5679 ostream->need_caps = FALSE;
5681 if (ostream->profile == GST_RTSP_PROFILE_SAVP ||
5682 ostream->profile == GST_RTSP_PROFILE_SAVPF)
5683 caps = gst_caps_new_empty_simple ("application/x-srtcp");
5685 caps = gst_caps_new_empty_simple ("application/x-rtcp");
5687 if (ostream->udpsrc[1]) {
5688 gst_element_send_event (ostream->udpsrc[1],
5689 gst_event_new_caps (caps));
5690 } else if (ostream->channelpad[1]) {
5691 if (GST_PAD_IS_SRC (ostream->channelpad[1]))
5692 gst_pad_push_event (ostream->channelpad[1],
5693 gst_event_new_caps (caps));
5695 gst_pad_send_event (ostream->channelpad[1],
5696 gst_event_new_caps (caps));
5699 gst_caps_unref (caps);
5703 g_checksum_free (cs);
5705 gst_rtspsrc_activate_streams (src);
5706 src->need_activate = FALSE;
5707 src->need_segment = TRUE;
5710 if (src->base_time == -1) {
5711 /* Take current running_time. This timestamp will be put on
5712 * the first buffer of each stream because we are a live source and so we
5713 * timestamp with the running_time. When we are dealing with TCP, we also
5714 * only timestamp the first buffer (using the DISCONT flag) because a server
5715 * typically bursts data, for which we don't want to compensate by speeding
5716 * up the media. The other timestamps will be interpollated from this one
5717 * using the RTP timestamps. */
5718 GST_OBJECT_LOCK (src);
5719 if (GST_ELEMENT_CLOCK (src)) {
5721 GstClockTime base_time;
5723 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
5724 base_time = GST_ELEMENT_CAST (src)->base_time;
5726 src->base_time = now - base_time;
5728 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
5729 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
5731 GST_OBJECT_UNLOCK (src);
5734 /* If needed send a new segment, don't forget we are live and buffer are
5735 * timestamped with running time */
5736 if (src->need_segment) {
5737 src->need_segment = FALSE;
5738 if (src->onvif_mode) {
5739 gst_rtspsrc_push_event (src, gst_event_new_segment (&src->out_segment));
5743 gst_segment_init (&segment, GST_FORMAT_TIME);
5744 gst_rtspsrc_push_event (src, gst_event_new_segment (&segment));
5748 if (stream->need_caps) {
5751 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt))) {
5752 /* only streams that have a connection to the outside world */
5753 if (stream->setup) {
5754 /* Only need to update the TCP caps here, UDP is already handled */
5755 if (stream->channelpad[0]) {
5756 if (GST_PAD_IS_SRC (stream->channelpad[0]))
5757 gst_pad_push_event (stream->channelpad[0],
5758 gst_event_new_caps (caps));
5760 gst_pad_send_event (stream->channelpad[0],
5761 gst_event_new_caps (caps));
5763 stream->need_caps = FALSE;
5767 stream->need_caps = FALSE;
5770 if (stream->discont && !is_rtcp) {
5771 /* mark first RTP buffer as discont */
5772 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
5773 stream->discont = FALSE;
5774 /* first buffer gets the timestamp, other buffers are not timestamped and
5775 * their presentation time will be interpollated from the rtp timestamps. */
5776 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
5777 GST_TIME_ARGS (src->base_time));
5779 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
5782 /* chain to the peer pad */
5783 if (GST_PAD_IS_SINK (outpad))
5784 ret = gst_pad_chain (outpad, buf);
5786 ret = gst_pad_push (outpad, buf);
5789 /* combine all stream flows for the data transport */
5790 ret = gst_rtspsrc_combine_flows (src, stream, ret);
5797 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
5798 gst_rtsp_message_unset (message);
5803 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5804 ("Short message received, ignoring."));
5805 gst_rtsp_message_unset (message);
5810 static GstFlowReturn
5811 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
5813 GstRTSPMessage message = { 0 };
5815 GstFlowReturn ret = GST_FLOW_OK;
5818 gst_rtsp_message_unset (&message);
5820 if (src->conninfo.flushing) {
5821 /* do not attempt to receive if flushing */
5822 res = GST_RTSP_EINTR;
5824 /* protect the connection with the connection lock so that we can see when
5825 * we are finished doing server communication */
5826 res = gst_rtspsrc_connection_receive (src, &src->conninfo, &message,
5832 GST_DEBUG_OBJECT (src, "we received a server message");
5834 case GST_RTSP_EINTR:
5835 /* we got interrupted this means we need to stop */
5837 case GST_RTSP_ETIMEOUT:
5838 /* no reply, send keep alive */
5839 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
5840 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5844 /* go EOS when the server closed the connection */
5850 switch (message.type) {
5851 case GST_RTSP_MESSAGE_REQUEST:
5852 /* server sends us a request message, handle it */
5853 res = gst_rtspsrc_handle_request (src, &src->conninfo, &message);
5854 if (res == GST_RTSP_EEOF)
5857 goto handle_request_failed;
5859 case GST_RTSP_MESSAGE_RESPONSE:
5860 /* we ignore response messages */
5861 GST_DEBUG_OBJECT (src, "ignoring response message");
5862 DEBUG_RTSP (src, &message);
5864 case GST_RTSP_MESSAGE_DATA:
5865 GST_DEBUG_OBJECT (src, "got data message");
5866 ret = gst_rtspsrc_handle_data (src, &message);
5867 if (ret != GST_FLOW_OK)
5868 goto handle_data_failed;
5871 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5876 g_assert_not_reached ();
5881 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5882 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5883 ("The server closed the connection."));
5884 src->conninfo.connected = FALSE;
5885 gst_rtsp_message_unset (&message);
5886 return GST_FLOW_EOS;
5890 gst_rtsp_message_unset (&message);
5891 GST_DEBUG_OBJECT (src, "got interrupted");
5892 return GST_FLOW_FLUSHING;
5896 gchar *str = gst_rtsp_strresult (res);
5898 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5899 ("Could not receive message. (%s)", str));
5902 gst_rtsp_message_unset (&message);
5903 return GST_FLOW_ERROR;
5905 handle_request_failed:
5907 gchar *str = gst_rtsp_strresult (res);
5909 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5910 ("Could not handle server message. (%s)", str));
5912 gst_rtsp_message_unset (&message);
5913 return GST_FLOW_ERROR;
5917 GST_DEBUG_OBJECT (src, "could no handle data message");
5922 static GstFlowReturn
5923 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
5926 GstRTSPMessage message = { 0 };
5932 /* get the next timeout interval */
5933 timeout = gst_rtsp_connection_next_timeout_usec (src->conninfo.connection);
5935 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
5936 (gint) timeout / G_USEC_PER_SEC);
5938 gst_rtsp_message_unset (&message);
5940 /* we should continue reading the TCP socket because the server might
5941 * send us requests. When the session timeout expires, we need to send a
5942 * keep-alive request to keep the session open. */
5943 if (src->conninfo.flushing) {
5944 /* do not attempt to receive if flushing */
5945 res = GST_RTSP_EINTR;
5947 res = gst_rtspsrc_connection_receive (src, &src->conninfo, &message,
5953 GST_DEBUG_OBJECT (src, "we received a server message");
5955 case GST_RTSP_EINTR:
5956 /* we got interrupted, see what we have to do */
5958 case GST_RTSP_ETIMEOUT:
5959 /* send keep-alive, ignore the result, a warning will be posted. */
5960 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
5961 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5965 /* server closed the connection. not very fatal for UDP, reconnect and
5966 * see what happens. */
5967 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5968 ("The server closed the connection."));
5969 if (src->udp_reconnect) {
5971 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
5978 GST_DEBUG_OBJECT (src, "An ethernet problem occurred.");
5980 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5981 ("Unhandled return value %d.", res));
5985 switch (message.type) {
5986 case GST_RTSP_MESSAGE_REQUEST:
5987 /* server sends us a request message, handle it */
5988 res = gst_rtspsrc_handle_request (src, &src->conninfo, &message);
5989 if (res == GST_RTSP_EEOF)
5992 goto handle_request_failed;
5994 case GST_RTSP_MESSAGE_RESPONSE:
5995 /* we ignore response and data messages */
5996 GST_DEBUG_OBJECT (src, "ignoring response message");
5997 DEBUG_RTSP (src, &message);
5998 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
5999 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
6000 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
6001 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
6002 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
6009 case GST_RTSP_MESSAGE_DATA:
6010 /* we ignore response and data messages */
6011 GST_DEBUG_OBJECT (src, "ignoring data message");
6014 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
6019 g_assert_not_reached ();
6021 /* we get here when the connection got interrupted */
6024 gst_rtsp_message_unset (&message);
6025 GST_DEBUG_OBJECT (src, "got interrupted");
6026 return GST_FLOW_FLUSHING;
6030 gchar *str = gst_rtsp_strresult (res);
6033 src->conninfo.connected = FALSE;
6034 if (res != GST_RTSP_EINTR) {
6035 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
6036 ("Could not connect to server. (%s)", str));
6038 ret = GST_FLOW_ERROR;
6040 ret = GST_FLOW_FLUSHING;
6046 gchar *str = gst_rtsp_strresult (res);
6048 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6049 ("Could not receive message. (%s)", str));
6051 return GST_FLOW_ERROR;
6053 handle_request_failed:
6055 gchar *str = gst_rtsp_strresult (res);
6058 gst_rtsp_message_unset (&message);
6059 if (res != GST_RTSP_EINTR) {
6060 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6061 ("Could not handle server message. (%s)", str));
6063 ret = GST_FLOW_ERROR;
6065 ret = GST_FLOW_FLUSHING;
6071 GST_DEBUG_OBJECT (src, "we got an eof from the server");
6072 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
6073 ("The server closed the connection."));
6074 src->conninfo.connected = FALSE;
6075 gst_rtsp_message_unset (&message);
6076 return GST_FLOW_EOS;
6080 static GstRTSPResult
6081 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
6083 GstRTSPResult res = GST_RTSP_OK;
6086 GST_DEBUG_OBJECT (src, "doing reconnect");
6088 GST_OBJECT_LOCK (src);
6089 /* only restart when the pads were not yet activated, else we were
6090 * streaming over UDP */
6091 restart = src->need_activate;
6092 GST_OBJECT_UNLOCK (src);
6094 /* no need to restart, we're done */
6098 /* we can try only TCP now */
6099 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
6101 /* close and cleanup our state */
6102 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
6105 /* see if we have TCP left to try. Also don't try TCP when we were configured
6107 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
6110 /* We post a warning message now to inform the user
6111 * that nothing happened. It's most likely a firewall thing. */
6112 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
6113 ("Could not receive any UDP packets for %.4f seconds, maybe your "
6114 "firewall is blocking it. Retrying using a tcp connection.",
6115 gst_guint64_to_gdouble (src->udp_timeout) / 1000000.0));
6117 /* open new connection using tcp */
6118 if (gst_rtspsrc_open (src, async) < 0)
6121 /* start playback */
6122 if (gst_rtspsrc_play (src, &src->segment, async, NULL) < 0)
6131 src->cur_protocols = 0;
6132 /* no transport possible, post an error and stop */
6133 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6134 ("Could not receive any UDP packets for %.4f seconds, maybe your "
6135 "firewall is blocking it. No other protocols to try.",
6136 gst_guint64_to_gdouble (src->udp_timeout) / 1000000.0));
6137 return GST_RTSP_ERROR;
6141 GST_DEBUG_OBJECT (src, "open failed");
6146 GST_DEBUG_OBJECT (src, "play failed");
6152 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
6156 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
6159 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
6162 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
6164 case CMD_GET_PARAMETER:
6165 GST_ELEMENT_PROGRESS (src, START, "request",
6166 ("Sending GET_PARAMETER request"));
6168 case CMD_SET_PARAMETER:
6169 GST_ELEMENT_PROGRESS (src, START, "request",
6170 ("Sending SET_PARAMETER request"));
6173 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
6181 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
6185 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
6188 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
6191 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
6193 case CMD_GET_PARAMETER:
6194 GST_ELEMENT_PROGRESS (src, COMPLETE, "request",
6195 ("Sent GET_PARAMETER request"));
6197 case CMD_SET_PARAMETER:
6198 GST_ELEMENT_PROGRESS (src, COMPLETE, "request",
6199 ("Sent SET_PARAMETER request"));
6202 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
6210 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
6214 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
6217 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
6220 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
6222 case CMD_GET_PARAMETER:
6223 GST_ELEMENT_PROGRESS (src, CANCELED, "request",
6224 ("GET_PARAMETER canceled"));
6226 case CMD_SET_PARAMETER:
6227 GST_ELEMENT_PROGRESS (src, CANCELED, "request",
6228 ("SET_PARAMETER canceled"));
6231 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
6239 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
6243 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
6246 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
6249 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
6251 case CMD_GET_PARAMETER:
6252 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("GET_PARAMETER failed"));
6254 case CMD_SET_PARAMETER:
6255 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("SET_PARAMETER failed"));
6258 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
6266 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
6268 if (ret == GST_RTSP_OK)
6269 gst_rtspsrc_loop_complete_cmd (src, cmd);
6270 else if (ret == GST_RTSP_EINTR)
6271 gst_rtspsrc_loop_cancel_cmd (src, cmd);
6273 gst_rtspsrc_loop_error_cmd (src, cmd);
6277 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
6280 gboolean flushed = FALSE;
6282 /* start new request */
6283 gst_rtspsrc_loop_start_cmd (src, cmd);
6285 GST_DEBUG_OBJECT (src, "sending cmd %s", cmd_to_string (cmd));
6287 GST_OBJECT_LOCK (src);
6288 old = src->pending_cmd;
6290 if (old == CMD_RECONNECT) {
6291 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
6292 cmd = CMD_RECONNECT;
6293 } else if (old == CMD_CLOSE) {
6294 /* our CMD_CLOSE might have interrutped CMD_LOOP. gst_rtspsrc_loop
6295 * will send a CMD_WAIT which would cancel our pending CMD_CLOSE (if
6296 * still pending). We just avoid it here by making sure CMD_CLOSE is
6297 * still the pending command. */
6298 GST_DEBUG_OBJECT (src, "ignore, we were closing");
6300 } else if (old == CMD_SET_PARAMETER) {
6301 GST_DEBUG_OBJECT (src, "ignore, we have a pending %s", cmd_to_string (old));
6302 cmd = CMD_SET_PARAMETER;
6303 } else if (old == CMD_GET_PARAMETER) {
6304 GST_DEBUG_OBJECT (src, "ignore, we have a pending %s", cmd_to_string (old));
6305 cmd = CMD_GET_PARAMETER;
6306 } else if (old != CMD_WAIT) {
6307 src->pending_cmd = CMD_WAIT;
6308 GST_OBJECT_UNLOCK (src);
6309 /* cancel previous request */
6310 GST_DEBUG_OBJECT (src, "cancel previous request %s", cmd_to_string (old));
6311 gst_rtspsrc_loop_cancel_cmd (src, old);
6312 GST_OBJECT_LOCK (src);
6314 src->pending_cmd = cmd;
6315 /* interrupt if allowed */
6316 if (src->busy_cmd & mask) {
6317 GST_DEBUG_OBJECT (src, "connection flush busy %s",
6318 cmd_to_string (src->busy_cmd));
6319 gst_rtspsrc_connection_flush (src, TRUE);
6322 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %s",
6323 cmd_to_string (src->busy_cmd));
6326 gst_task_start (src->task);
6327 GST_OBJECT_UNLOCK (src);
6333 gst_rtspsrc_loop_send_cmd_and_wait (GstRTSPSrc * src, gint cmd, gint mask,
6334 GstClockTime timeout)
6336 gboolean flushed = gst_rtspsrc_loop_send_cmd (src, cmd, mask);
6339 gint64 end_time = g_get_monotonic_time () + (timeout / 1000);
6340 GST_OBJECT_LOCK (src);
6341 while (src->pending_cmd == cmd || src->busy_cmd == cmd) {
6342 if (!g_cond_wait_until (&src->cmd_cond, GST_OBJECT_GET_LOCK (src),
6344 GST_WARNING_OBJECT (src,
6345 "Timed out waiting for TEARDOWN to be processed.");
6346 break; /* timeout passed */
6349 GST_OBJECT_UNLOCK (src);
6355 gst_rtspsrc_loop (GstRTSPSrc * src)
6359 if (!src->conninfo.connection || !src->conninfo.connected)
6362 if (src->interleaved)
6363 ret = gst_rtspsrc_loop_interleaved (src);
6365 ret = gst_rtspsrc_loop_udp (src);
6367 if (ret != GST_FLOW_OK)
6375 GST_WARNING_OBJECT (src, "we are not connected");
6376 ret = GST_FLOW_FLUSHING;
6381 const gchar *reason = gst_flow_get_name (ret);
6383 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
6384 src->running = FALSE;
6385 if (ret == GST_FLOW_EOS) {
6386 /* perform EOS logic */
6387 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
6388 gst_element_post_message (GST_ELEMENT_CAST (src),
6389 gst_message_new_segment_done (GST_OBJECT_CAST (src),
6390 src->segment.format, src->segment.position));
6391 gst_rtspsrc_push_event (src,
6392 gst_event_new_segment_done (src->segment.format,
6393 src->segment.position));
6395 gst_rtspsrc_push_event (src, gst_event_new_eos ());
6397 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
6398 /* for fatal errors we post an error message, post the error before the
6399 * EOS so the app knows about the error first. */
6400 GST_ELEMENT_FLOW_ERROR (src, ret);
6401 gst_rtspsrc_push_event (src, gst_event_new_eos ());
6403 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
6408 #ifndef GST_DISABLE_GST_DEBUG
6409 static const gchar *
6410 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
6414 while (method != 0) {
6431 /* Parse a WWW-Authenticate Response header and determine the
6432 * available authentication methods
6434 * This code should also cope with the fact that each WWW-Authenticate
6435 * header can contain multiple challenge methods + tokens
6437 * At the moment, for Basic auth, we just do a minimal check and don't
6438 * even parse out the realm */
6440 gst_rtspsrc_parse_auth_hdr (GstRTSPMessage * response,
6441 GstRTSPAuthMethod * methods, GstRTSPConnection * conn, gboolean * stale)
6443 GstRTSPAuthCredential **credentials, **credential;
6445 g_return_if_fail (response != NULL);
6446 g_return_if_fail (methods != NULL);
6447 g_return_if_fail (stale != NULL);
6450 gst_rtsp_message_parse_auth_credentials (response,
6451 GST_RTSP_HDR_WWW_AUTHENTICATE);
6455 credential = credentials;
6456 while (*credential) {
6457 if ((*credential)->scheme == GST_RTSP_AUTH_BASIC) {
6458 *methods |= GST_RTSP_AUTH_BASIC;
6459 } else if ((*credential)->scheme == GST_RTSP_AUTH_DIGEST) {
6460 GstRTSPAuthParam **param = (*credential)->params;
6462 *methods |= GST_RTSP_AUTH_DIGEST;
6464 gst_rtsp_connection_clear_auth_params (conn);
6468 if (strcmp ((*param)->name, "stale") == 0
6469 && g_ascii_strcasecmp ((*param)->value, "TRUE") == 0)
6471 gst_rtsp_connection_set_auth_param (conn, (*param)->name,
6480 gst_rtsp_auth_credentials_free (credentials);
6484 * gst_rtspsrc_setup_auth:
6485 * @src: the rtsp source
6487 * Configure a username and password and auth method on the
6488 * connection object based on a response we received from the
6491 * Currently, this requires that a username and password were supplied
6492 * in the uri. In the future, they may be requested on demand by sending
6493 * a message up the bus.
6495 * Returns: TRUE if authentication information could be set up correctly.
6498 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
6502 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
6503 GstRTSPAuthMethod method;
6504 GstRTSPResult auth_result;
6506 GstRTSPConnection *conn;
6507 gboolean stale = FALSE;
6509 conn = src->conninfo.connection;
6511 /* Identify the available auth methods and see if any are supported */
6512 gst_rtspsrc_parse_auth_hdr (response, &avail_methods, conn, &stale);
6514 if (avail_methods == GST_RTSP_AUTH_NONE)
6515 goto no_auth_available;
6517 /* For digest auth, if the response indicates that the session
6518 * data are stale, we just update them in the connection object and
6519 * return TRUE to retry the request */
6521 src->tried_url_auth = FALSE;
6523 url = gst_rtsp_connection_get_url (conn);
6525 /* Do we have username and password available? */
6526 if (url != NULL && !src->tried_url_auth && url->user != NULL
6527 && url->passwd != NULL) {
6530 src->tried_url_auth = TRUE;
6531 GST_DEBUG_OBJECT (src,
6532 "Attempting authentication using credentials from the URL");
6534 user = src->user_id;
6535 pass = src->user_pw;
6536 GST_DEBUG_OBJECT (src,
6537 "Attempting authentication using credentials from the properties");
6540 /* FIXME: If the url didn't contain username and password or we tried them
6541 * already, request a username and passwd from the application via some kind
6542 * of credentials request message */
6544 /* If we don't have a username and passwd at this point, bail out. */
6545 if (user == NULL || pass == NULL)
6548 /* Try to configure for each available authentication method, strongest to
6550 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
6551 /* Check if this method is available on the server */
6552 if ((method & avail_methods) == 0)
6555 /* Pass the credentials to the connection to try on the next request */
6556 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
6557 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
6558 * ignore it and end up retrying later */
6559 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
6560 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
6561 gst_rtsp_auth_method_to_string (method));
6566 if (method == GST_RTSP_AUTH_NONE)
6567 goto no_auth_available;
6573 /* Output an error indicating that we couldn't connect because there were
6574 * no supported authentication protocols */
6575 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
6576 ("No supported authentication protocol was found"));
6581 /* We don't fire an error message, we just return FALSE and let the
6582 * normal NOT_AUTHORIZED error be propagated */
6587 static GstRTSPResult
6588 gst_rtsp_src_receive_response (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
6589 GstRTSPMessage * response, GstRTSPStatusCode * code)
6591 GstRTSPStatusCode thecode;
6592 gchar *content_base = NULL;
6596 if (conninfo->flushing) {
6597 /* do not attempt to receive if flushing */
6598 res = GST_RTSP_EINTR;
6600 res = gst_rtspsrc_connection_receive (src, conninfo, response,
6607 DEBUG_RTSP (src, response);
6609 switch (response->type) {
6610 case GST_RTSP_MESSAGE_REQUEST:
6611 res = gst_rtspsrc_handle_request (src, conninfo, response);
6612 if (res == GST_RTSP_EEOF)
6615 goto handle_request_failed;
6617 /* Not a response, receive next message */
6619 case GST_RTSP_MESSAGE_RESPONSE:
6620 /* ok, a response is good */
6621 GST_DEBUG_OBJECT (src, "received response message");
6623 case GST_RTSP_MESSAGE_DATA:
6624 /* get next response */
6625 GST_DEBUG_OBJECT (src, "handle data response message");
6626 gst_rtspsrc_handle_data (src, response);
6628 /* Not a response, receive next message */
6631 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
6634 /* Not a response, receive next message */
6638 thecode = response->type_data.response.code;
6640 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
6642 /* if the caller wanted the result code, we store it. */
6646 /* If the request didn't succeed, bail out before doing any more */
6647 if (thecode != GST_RTSP_STS_OK)
6650 /* store new content base if any */
6651 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
6654 g_free (src->content_base);
6655 src->content_base = g_strdup (content_base);
6665 return GST_RTSP_EEOF;
6668 gchar *str = gst_rtsp_strresult (res);
6670 if (res != GST_RTSP_EINTR) {
6671 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6672 ("Could not receive message. (%s)", str));
6674 GST_WARNING_OBJECT (src, "receive interrupted");
6682 handle_request_failed:
6684 /* ERROR was posted */
6685 gst_rtsp_message_unset (response);
6690 GST_DEBUG_OBJECT (src, "we got an eof from the server");
6691 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
6692 ("The server closed the connection."));
6693 gst_rtsp_message_unset (response);
6699 static GstRTSPResult
6700 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
6701 GstRTSPMessage * request, GstRTSPMessage * response,
6702 GstRTSPStatusCode * code)
6706 gboolean allow_send = TRUE;
6709 if (!src->short_header)
6710 gst_rtsp_ext_list_before_send (src->extensions, request);
6712 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_BEFORE_SEND], 0,
6713 request, &allow_send);
6715 GST_DEBUG_OBJECT (src, "skipping message, disabled by signal");
6719 GST_DEBUG_OBJECT (src, "sending message");
6721 DEBUG_RTSP (src, request);
6723 res = gst_rtspsrc_connection_send (src, conninfo, request, src->tcp_timeout);
6727 gst_rtsp_connection_reset_timeout (conninfo->connection);
6731 res = gst_rtsp_src_receive_response (src, conninfo, response, code);
6732 if (res == GST_RTSP_EEOF) {
6733 GST_WARNING_OBJECT (src, "server closed connection");
6734 /* only try once after reconnect, then fallthrough and error out */
6735 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
6737 /* if reconnect succeeds, try again */
6738 if ((res = gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) == 0)
6746 gst_rtsp_ext_list_after_send (src->extensions, request, response);
6752 gchar *str = gst_rtsp_strresult (res);
6754 if (res != GST_RTSP_EINTR) {
6755 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6756 ("Could not send message. (%s)", str));
6758 GST_WARNING_OBJECT (src, "send interrupted");
6766 gchar *str = gst_rtsp_strresult (res);
6768 if (res != GST_RTSP_EINTR) {
6769 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6770 ("Could not receive message. (%s)", str));
6772 GST_WARNING_OBJECT (src, "receive interrupted");
6781 * @src: the rtsp source
6782 * @conninfo: the connection information to send on
6783 * @request: must point to a valid request
6784 * @response: must point to an empty #GstRTSPMessage
6785 * @code: an optional code result
6786 * @versions: List of versions to try, setting it back onto the @request message
6787 * if not set, `src->version` will be used as RTSP version.
6789 * send @request and retrieve the response in @response. optionally @code can be
6790 * non-NULL in which case it will contain the status code of the response.
6792 * If This function returns #GST_RTSP_OK, @response will contain a valid response
6793 * message that should be cleaned with gst_rtsp_message_unset() after usage.
6795 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
6796 * @response message) if the response code was not 200 (OK).
6798 * If the attempt results in an authentication failure, then this will attempt
6799 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
6802 * Returns: #GST_RTSP_OK if the processing was successful.
6804 static GstRTSPResult
6805 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
6806 GstRTSPMessage * request, GstRTSPMessage * response,
6807 GstRTSPStatusCode * code, GstRTSPVersion * versions)
6809 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
6810 GstRTSPResult res = GST_RTSP_ERROR;
6813 GstRTSPMethod method = GST_RTSP_INVALID;
6814 gint version_retry = 0;
6820 /* make sure we don't loop forever */
6824 /* save method so we can disable it when the server complains */
6825 method = request->type_data.request.method;
6828 request->type_data.request.version = src->version;
6831 gst_rtspsrc_try_send (src, conninfo, request, response,
6836 case GST_RTSP_STS_UNAUTHORIZED:
6837 case GST_RTSP_STS_NOT_FOUND:
6838 if (gst_rtspsrc_setup_auth (src, response)) {
6839 /* Try the request/response again after configuring the auth info
6844 case GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED:
6845 GST_INFO_OBJECT (src, "Version %s not supported by the server",
6846 versions ? gst_rtsp_version_as_text (versions[version_retry]) :
6848 if (versions && versions[version_retry] != GST_RTSP_VERSION_INVALID) {
6849 GST_INFO_OBJECT (src, "Unsupported version %s => trying %s",
6850 gst_rtsp_version_as_text (request->type_data.request.version),
6851 gst_rtsp_version_as_text (versions[version_retry]));
6852 request->type_data.request.version = versions[version_retry];
6861 } while (retry == TRUE);
6863 /* If the user requested the code, let them handle errors, otherwise
6864 * post an error below */
6867 else if (int_code != GST_RTSP_STS_OK)
6868 goto error_response;
6875 GST_DEBUG_OBJECT (src, "got error %d", res);
6880 res = GST_RTSP_ERROR;
6882 switch (response->type_data.response.code) {
6883 case GST_RTSP_STS_NOT_FOUND:
6884 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, NOT_FOUND,
6887 case GST_RTSP_STS_UNAUTHORIZED:
6888 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, NOT_AUTHORIZED,
6891 case GST_RTSP_STS_MOVED_PERMANENTLY:
6892 case GST_RTSP_STS_MOVE_TEMPORARILY:
6894 gchar *new_location;
6895 GstRTSPLowerTrans transports;
6897 GST_DEBUG_OBJECT (src, "got redirection");
6898 /* if we don't have a Location Header, we must error */
6899 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
6900 &new_location, 0) < 0)
6903 /* When we receive a redirect result, we go back to the INIT state after
6904 * parsing the new URI. The caller should do the needed steps to issue
6905 * a new setup when it detects this state change. */
6906 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
6908 /* save current transports */
6909 if (src->conninfo.url)
6910 transports = src->conninfo.url->transports;
6912 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
6914 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
6916 /* set old transports */
6917 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
6918 src->conninfo.url->transports = transports;
6920 src->need_redirect = TRUE;
6924 case GST_RTSP_STS_NOT_ACCEPTABLE:
6925 case GST_RTSP_STS_NOT_IMPLEMENTED:
6926 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
6927 /* Some cameras (e.g. HikVision DS-2CD2732F-IS) return "551
6928 * Option not supported" when a command is sent that is not implemented
6929 * (e.g. PAUSE). Instead; it should return "501 Not Implemented".
6931 * This is wrong, as previously, the camera did announce support
6932 * for PAUSE in the OPTIONS.
6934 * In this case, handle the 551 as if it was 501 to avoid throwing
6935 * errors to application level. */
6936 case GST_RTSP_STS_OPTION_NOT_SUPPORTED:
6937 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
6938 gst_rtsp_method_as_text (method));
6939 src->methods &= ~method;
6943 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, READ,
6947 /* if we return ERROR we should unset the response ourselves */
6948 if (res == GST_RTSP_ERROR)
6949 gst_rtsp_message_unset (response);
6955 static GstRTSPResult
6956 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
6957 GstRTSPMessage * response, GstRTSPSrc * src)
6959 return gst_rtspsrc_send (src, &src->conninfo, request, response, NULL, NULL);
6963 /* parse the response and collect all the supported methods. We need this
6964 * information so that we don't try to send an unsupported request to the
6968 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
6970 GstRTSPHeaderField field;
6974 /* reset supported methods */
6977 /* Try Allow Header first */
6978 field = GST_RTSP_HDR_ALLOW;
6981 gst_rtsp_message_get_header (response, field, &respoptions, indx);
6985 src->methods |= gst_rtsp_options_from_text (respoptions);
6991 field = GST_RTSP_HDR_PUBLIC;
6994 gst_rtsp_message_get_header (response, field, &respoptions, indx);
6998 src->methods |= gst_rtsp_options_from_text (respoptions);
7003 if (src->methods == 0) {
7004 /* neither Allow nor Public are required, assume the server supports
7005 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
7007 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
7008 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
7010 /* always assume PLAY, FIXME, extensions should be able to override
7012 src->methods |= GST_RTSP_PLAY;
7013 /* also assume it will support Range */
7014 src->seekable = G_MAXFLOAT;
7016 /* we need describe and setup */
7017 if (!(src->methods & GST_RTSP_DESCRIBE))
7019 if (!(src->methods & GST_RTSP_SETUP))
7027 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
7028 ("Server does not support DESCRIBE."));
7033 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
7034 ("Server does not support SETUP."));
7039 /* masks to be kept in sync with the hardcoded protocol order of preference
7041 static const guint protocol_masks[] = {
7042 GST_RTSP_LOWER_TRANS_UDP,
7043 GST_RTSP_LOWER_TRANS_UDP_MCAST,
7044 GST_RTSP_LOWER_TRANS_TCP,
7048 static GstRTSPResult
7049 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
7050 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
7054 gboolean add_udp_str;
7059 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
7064 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
7066 /* extension listed transports, use those */
7067 if (*transports != NULL)
7070 /* it's the default */
7071 add_udp_str = FALSE;
7073 /* the default RTSP transports */
7074 result = g_string_new ("RTP");
7077 case GST_RTSP_PROFILE_AVP:
7078 g_string_append (result, "/AVP");
7080 case GST_RTSP_PROFILE_SAVP:
7081 g_string_append (result, "/SAVP");
7083 case GST_RTSP_PROFILE_AVPF:
7084 g_string_append (result, "/AVPF");
7086 case GST_RTSP_PROFILE_SAVPF:
7087 g_string_append (result, "/SAVPF");
7093 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
7094 GST_DEBUG_OBJECT (src, "adding UDP unicast");
7096 g_string_append (result, "/UDP");
7097 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
7098 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
7099 GST_DEBUG_OBJECT (src, "adding UDP multicast");
7100 /* we don't have to allocate any UDP ports yet, if the selected transport
7101 * turns out to be multicast we can create them and join the multicast
7102 * group indicated in the transport reply */
7104 g_string_append (result, "/UDP");
7105 g_string_append (result, ";multicast");
7106 if (src->next_port_num != 0) {
7107 if (src->client_port_range.max > 0 &&
7108 src->next_port_num >= src->client_port_range.max)
7111 g_string_append_printf (result, ";client_port=%d-%d",
7112 src->next_port_num, src->next_port_num + 1);
7114 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
7115 GST_DEBUG_OBJECT (src, "adding TCP");
7117 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
7119 *transports = g_string_free (result, FALSE);
7121 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
7128 GST_ERROR ("extension gave error %d", res);
7133 GST_ERROR ("no more ports available");
7134 return GST_RTSP_ERROR;
7138 static GstRTSPResult
7139 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
7140 gint orig_rtpport, gint orig_rtcpport)
7143 gint nr_udp, nr_int;
7145 gint rtpport = 0, rtcpport = 0;
7148 src = stream->parent;
7150 /* find number of placeholders first */
7151 if (strstr (*transports, "%%i2"))
7153 else if (strstr (*transports, "%%i1"))
7158 if (strstr (*transports, "%%u2"))
7160 else if (strstr (*transports, "%%u1"))
7165 if (nr_udp == 0 && nr_int == 0)
7169 if (!orig_rtpport || !orig_rtcpport) {
7170 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
7173 rtpport = orig_rtpport;
7174 rtcpport = orig_rtcpport;
7178 str = g_string_new ("");
7180 while ((next = strstr (p, "%%"))) {
7181 g_string_append_len (str, p, next - p);
7182 if (next[2] == 'u') {
7184 g_string_append_printf (str, "%d", rtpport);
7185 else if (next[3] == '2')
7186 g_string_append_printf (str, "%d", rtcpport);
7188 if (next[2] == 'i') {
7190 g_string_append_printf (str, "%d", src->free_channel);
7191 else if (next[3] == '2')
7192 g_string_append_printf (str, "%d", src->free_channel + 1);
7198 if (src->version >= GST_RTSP_VERSION_2_0)
7199 src->free_channel += 2;
7201 /* append final part */
7202 g_string_append (str, p);
7204 g_free (*transports);
7205 *transports = g_string_free (str, FALSE);
7213 GST_ERROR ("failed to allocate udp ports");
7214 return GST_RTSP_ERROR;
7219 signal_get_srtcp_params (GstRTSPSrc * src, GstRTSPStream * stream)
7221 GstCaps *caps = NULL;
7223 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY], 0,
7227 GST_DEBUG_OBJECT (src, "SRTP parameters received");
7233 default_srtcp_params (void)
7240 guint data_size = GST_ROUND_UP_4 (KEY_SIZE);
7242 /* create a random key */
7243 key_data = g_malloc (data_size);
7244 for (i = 0; i < data_size; i += 4)
7245 GST_WRITE_UINT32_BE (key_data + i, g_random_int ());
7247 buf = gst_buffer_new_wrapped (key_data, KEY_SIZE);
7249 caps = gst_caps_new_simple ("application/x-srtcp",
7250 "srtp-key", GST_TYPE_BUFFER, buf,
7251 "srtp-cipher", G_TYPE_STRING, "aes-128-icm",
7252 "srtp-auth", G_TYPE_STRING, "hmac-sha1-80",
7253 "srtcp-cipher", G_TYPE_STRING, "aes-128-icm",
7254 "srtcp-auth", G_TYPE_STRING, "hmac-sha1-80", NULL);
7256 gst_buffer_unref (buf);
7262 gst_rtspsrc_stream_make_keymgmt (GstRTSPSrc * src, GstRTSPStream * stream)
7264 gchar *base64, *result = NULL;
7265 GstMIKEYMessage *mikey_msg;
7267 stream->srtcpparams = signal_get_srtcp_params (src, stream);
7268 if (stream->srtcpparams == NULL)
7269 stream->srtcpparams = default_srtcp_params ();
7271 mikey_msg = gst_mikey_message_new_from_caps (stream->srtcpparams);
7273 /* add policy '0' for our SSRC */
7274 gst_mikey_message_add_cs_srtp (mikey_msg, 0, stream->send_ssrc, 0);
7276 base64 = gst_mikey_message_base64_encode (mikey_msg);
7277 gst_mikey_message_unref (mikey_msg);
7280 result = gst_sdp_make_keymgmt (stream->conninfo.location, base64);
7288 static GstRTSPResult
7289 gst_rtsp_src_setup_stream_from_response (GstRTSPSrc * src,
7290 GstRTSPStream * stream, GstRTSPMessage * response,
7291 GstRTSPLowerTrans * protocols, gint retry, gint * rtpport, gint * rtcpport)
7293 gchar *resptrans = NULL;
7294 GstRTSPTransport transport = { 0 };
7296 gst_rtsp_message_get_header (response, GST_RTSP_HDR_TRANSPORT, &resptrans, 0);
7298 gst_rtspsrc_stream_free_udp (stream);
7302 /* parse transport, go to next stream on parse error */
7303 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
7304 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
7305 return GST_RTSP_ELAST;
7308 /* update allowed transports for other streams. once the transport of
7309 * one stream has been determined, we make sure that all other streams
7310 * are configured in the same way */
7311 switch (transport.lower_transport) {
7312 case GST_RTSP_LOWER_TRANS_TCP:
7313 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
7315 *protocols = GST_RTSP_LOWER_TRANS_TCP;
7316 src->interleaved = TRUE;
7317 if (src->version < GST_RTSP_VERSION_2_0) {
7318 /* update free channels */
7319 src->free_channel = MAX (transport.interleaved.min, src->free_channel);
7320 src->free_channel = MAX (transport.interleaved.max, src->free_channel);
7321 src->free_channel++;
7324 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
7325 /* only allow multicast for other streams */
7326 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
7328 *protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
7329 /* if the server selected our ports, increment our counters so that
7330 * we select a new port later */
7331 if (src->next_port_num == transport.port.min &&
7332 src->next_port_num + 1 == transport.port.max) {
7333 src->next_port_num += 2;
7336 case GST_RTSP_LOWER_TRANS_UDP:
7337 /* only allow unicast for other streams */
7338 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
7340 *protocols = GST_RTSP_LOWER_TRANS_UDP;
7343 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
7344 transport.lower_transport);
7348 if (!src->interleaved || !retry) {
7349 /* now configure the stream with the selected transport */
7350 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
7351 GST_DEBUG_OBJECT (src,
7352 "could not configure stream %p transport, skipping stream", stream);
7354 } else if (stream->udpsrc[0] && stream->udpsrc[1] && rtpport && rtcpport) {
7355 /* retain the first allocated UDP port pair */
7356 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", rtpport, NULL);
7357 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", rtcpport, NULL);
7360 /* we need to activate at least one stream when we detect activity */
7361 src->need_activate = TRUE;
7363 /* stream is setup now */
7364 stream->setup = TRUE;
7365 stream->waiting_setup_response = FALSE;
7367 if (src->version >= GST_RTSP_VERSION_2_0) {
7368 gchar *prop, *media_properties;
7372 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_MEDIA_PROPERTIES,
7373 &media_properties, 0) != GST_RTSP_OK) {
7374 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7375 ("Error: No MEDIA_PROPERTY header in a SETUP request in RTSP 2.0"
7376 " - this header is mandatory."));
7378 gst_rtsp_message_unset (response);
7379 return GST_RTSP_ERROR;
7382 props = g_strsplit (media_properties, ",", -2);
7383 for (i = 0; props[i]; i++) {
7386 while (*prop == ' ')
7389 if (strstr (prop, "Random-Access")) {
7390 gchar **random_seekable_val = g_strsplit (prop, "=", 2);
7392 if (!random_seekable_val[1])
7393 src->seekable = G_MAXFLOAT;
7395 src->seekable = g_ascii_strtod (random_seekable_val[1], NULL);
7397 g_strfreev (random_seekable_val);
7398 } else if (!g_strcmp0 (prop, "No-Seeking")) {
7399 src->seekable = -1.0;
7400 } else if (!g_strcmp0 (prop, "Beginning-Only")) {
7401 src->seekable = 0.0;
7409 /* clean up our transport struct */
7410 gst_rtsp_transport_init (&transport);
7411 /* clean up used RTSP messages */
7412 gst_rtsp_message_unset (response);
7418 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7419 ("Server did not select transport."));
7421 gst_rtsp_message_unset (response);
7422 return GST_RTSP_ERROR;
7426 static GstRTSPResult
7427 gst_rtspsrc_setup_streams_end (GstRTSPSrc * src, gboolean async)
7430 GstRTSPConnInfo *conninfo;
7432 g_assert (src->version >= GST_RTSP_VERSION_2_0);
7434 conninfo = &src->conninfo;
7435 for (tmp = src->streams; tmp; tmp = tmp->next) {
7436 GstRTSPStream *stream = (GstRTSPStream *) tmp->data;
7437 GstRTSPMessage response = { 0, };
7439 if (!stream->waiting_setup_response)
7442 if (!src->conninfo.connection)
7443 conninfo = &((GstRTSPStream *) tmp->data)->conninfo;
7445 gst_rtsp_src_receive_response (src, conninfo, &response, NULL);
7447 gst_rtsp_src_setup_stream_from_response (src, stream,
7448 &response, NULL, 0, NULL, NULL);
7454 /* Perform the SETUP request for all the streams.
7456 * We ask the server for a specific transport, which initially includes all the
7457 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
7458 * two local UDP ports that we send to the server.
7460 * Once the server replied with a transport, we configure the other streams
7461 * with the same transport.
7463 * In case setup request are not pipelined, this function will also configure the
7464 * stream for the selected transport, * which basically means creating the pipeline.
7465 * Otherwise, the first stream is setup right away from the reply and a
7466 * CMD_FINALIZE_SETUP command is set for the stream pipelines to happen on the
7467 * remaining streams from the RTSP thread.
7469 static GstRTSPResult
7470 gst_rtspsrc_setup_streams_start (GstRTSPSrc * src, gboolean async)
7473 GstRTSPResult res = GST_RTSP_ERROR;
7474 GstRTSPMessage request = { 0 };
7475 GstRTSPMessage response = { 0 };
7476 GstRTSPStream *stream = NULL;
7477 GstRTSPLowerTrans protocols;
7478 GstRTSPStatusCode code;
7479 gboolean unsupported_real = FALSE;
7480 gint rtpport, rtcpport;
7483 gchar *pipelined_request_id = NULL;
7485 if (src->conninfo.connection) {
7486 url = gst_rtsp_connection_get_url (src->conninfo.connection);
7487 /* we initially allow all configured lower transports. based on the URL
7488 * transports and the replies from the server we narrow them down. */
7489 protocols = url->transports & src->cur_protocols;
7492 protocols = src->cur_protocols;
7495 /* In ONVIF mode, we only want to try TCP transport */
7496 if (src->onvif_mode && (protocols & GST_RTSP_LOWER_TRANS_TCP))
7497 protocols = GST_RTSP_LOWER_TRANS_TCP;
7502 /* reset some state */
7503 src->free_channel = 0;
7504 src->interleaved = FALSE;
7505 src->need_activate = FALSE;
7506 /* keep track of next port number, 0 is random */
7507 src->next_port_num = src->client_port_range.min;
7508 rtpport = rtcpport = 0;
7510 if (G_UNLIKELY (src->streams == NULL))
7513 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7514 GstRTSPConnInfo *conninfo;
7517 gboolean tried_non_compliant_url = FALSE;
7522 stream = (GstRTSPStream *) walk->data;
7524 caps = stream_get_caps_for_pt (stream, stream->default_pt);
7526 GST_WARNING_OBJECT (src, "skipping stream %p, no caps", stream);
7530 if (stream->skipped) {
7531 GST_DEBUG_OBJECT (src, "skipping stream %p", stream);
7535 /* see if we need to configure this stream */
7536 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
7537 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
7542 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
7543 stream->id, caps, &selected);
7545 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
7549 /* merge/overwrite global caps */
7554 s = gst_caps_get_structure (caps, 0);
7556 num = gst_structure_n_fields (src->props);
7557 for (j = 0; j < num; j++) {
7561 name = gst_structure_nth_field_name (src->props, j);
7562 val = gst_structure_get_value (src->props, name);
7563 gst_structure_set_value (s, name, val);
7565 GST_DEBUG_OBJECT (src, "copied %s", name);
7569 /* skip setup if we have no URL for it */
7570 if (stream->conninfo.location == NULL) {
7571 GST_WARNING_OBJECT (src, "skipping stream %p, no setup", stream);
7575 if (src->conninfo.connection == NULL) {
7576 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
7577 GST_WARNING_OBJECT (src, "skipping stream %p, failed to connect",
7581 conninfo = &stream->conninfo;
7583 conninfo = &src->conninfo;
7585 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
7586 stream->conninfo.location);
7588 /* if we have a multicast connection, only suggest multicast from now on */
7589 if (stream->is_multicast)
7590 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
7593 /* first selectable protocol */
7594 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
7596 if (!protocol_masks[mask])
7600 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
7601 protocol_masks[mask]);
7602 /* create a string with first transport in line */
7604 res = gst_rtspsrc_create_transports_string (src,
7605 protocols & protocol_masks[mask], stream->profile, &transports);
7606 if (res < 0 || transports == NULL)
7607 goto setup_transport_failed;
7609 if (strlen (transports) == 0) {
7610 g_free (transports);
7611 GST_DEBUG_OBJECT (src, "no transports found");
7616 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
7618 /* replace placeholders with real values, this function will optionally
7619 * allocate UDP ports and other info needed to execute the setup request */
7620 res = gst_rtspsrc_prepare_transports (stream, &transports,
7621 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
7623 g_free (transports);
7624 goto setup_transport_failed;
7627 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
7628 /* create SETUP request */
7630 gst_rtspsrc_init_request (src, &request, GST_RTSP_SETUP,
7631 stream->conninfo.location);
7633 g_free (transports);
7634 goto create_request_failed;
7637 if (src->version >= GST_RTSP_VERSION_2_0) {
7638 if (!pipelined_request_id)
7639 pipelined_request_id = g_strdup_printf ("%d",
7640 g_random_int_range (0, G_MAXINT32));
7642 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_PIPELINED_REQUESTS,
7643 pipelined_request_id);
7644 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT_RANGES,
7645 "npt, clock, smpte, clock");
7648 /* select transport */
7649 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
7651 if (stream->is_backchannel && src->backchannel == BACKCHANNEL_ONVIF)
7652 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
7653 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
7656 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
7657 stream->profile == GST_RTSP_PROFILE_SAVPF) {
7658 hval = gst_rtspsrc_stream_make_keymgmt (src, stream);
7659 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
7662 /* if the user wants a non default RTP packet size we add the blocksize
7664 if (src->rtp_blocksize > 0) {
7665 hval = g_strdup_printf ("%d", src->rtp_blocksize);
7666 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
7670 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
7673 /* handle the code ourselves */
7675 gst_rtspsrc_send (src, conninfo, &request,
7676 pipelined_request_id ? NULL : &response, &code, NULL);
7681 case GST_RTSP_STS_OK:
7683 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
7684 gst_rtsp_message_unset (&request);
7685 gst_rtsp_message_unset (&response);
7686 /* cleanup of leftover transport */
7687 gst_rtspsrc_stream_free_udp (stream);
7688 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
7689 * we might be in this case */
7690 if (stream->container && rtpport && rtcpport && !retry) {
7691 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
7696 /* this transport did not go down well, but we may have others to try
7697 * that we did not send yet, try those and only give up then
7698 * but not without checking for lost cause/extension so we can
7699 * post a nicer/more useful error message later */
7700 if (!unsupported_real)
7701 unsupported_real = stream->is_real;
7702 /* select next available protocol, give up on this stream if none */
7704 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
7706 if (!protocol_masks[mask] || unsupported_real)
7710 case GST_RTSP_STS_BAD_REQUEST:
7711 case GST_RTSP_STS_NOT_FOUND:
7712 /* There are various non-compliant servers that don't require control
7713 * URLs that are not resolved correctly but instead are just appended.
7715 * https://gitlab.freedesktop.org/gstreamer/gst-plugins-good/-/issues/922
7716 * https://gitlab.freedesktop.org/gstreamer/gstreamer/-/issues/1447
7718 if (!tried_non_compliant_url && stream->control_url
7719 && !g_str_has_prefix (stream->control_url, "rtsp://")) {
7722 gst_rtsp_message_unset (&request);
7723 gst_rtsp_message_unset (&response);
7724 gst_rtspsrc_stream_free_udp (stream);
7726 g_free (stream->conninfo.location);
7727 base = get_aggregate_control (src);
7729 /* Make sure to not accumulate too many `/` */
7730 if ((g_str_has_suffix (base, "/")
7731 && !g_str_has_suffix (stream->control_url, "/"))
7732 || (!g_str_has_suffix (base, "/")
7733 && g_str_has_suffix (stream->control_url, "/"))
7735 stream->conninfo.location =
7736 g_strconcat (base, stream->control_url, NULL);
7737 else if (g_str_has_suffix (base, "/")
7738 && g_str_has_suffix (stream->control_url, "/"))
7739 stream->conninfo.location =
7740 g_strconcat (base, stream->control_url + 1, NULL);
7742 stream->conninfo.location =
7743 g_strconcat (base, "/", stream->control_url, NULL);
7745 tried_non_compliant_url = TRUE;
7752 /* cleanup of leftover transport and move to the next stream */
7753 gst_rtspsrc_stream_free_udp (stream);
7754 goto response_error;
7758 if (!pipelined_request_id) {
7759 /* parse response transport */
7760 res = gst_rtsp_src_setup_stream_from_response (src, stream,
7761 &response, &protocols, retry, &rtpport, &rtcpport);
7763 case GST_RTSP_ERROR:
7765 case GST_RTSP_ELAST:
7771 stream->waiting_setup_response = TRUE;
7772 /* we need to activate at least one stream when we detect activity */
7773 src->need_activate = TRUE;
7780 GstRTSPStream *sskip;
7782 skip = g_list_next (skip);
7786 sskip = (GstRTSPStream *) skip->data;
7788 /* skip all streams with the same control url */
7789 if (g_str_equal (stream->conninfo.location, sskip->conninfo.location)) {
7790 GST_DEBUG_OBJECT (src, "found stream %p with same control %s",
7791 sskip, sskip->conninfo.location);
7792 sskip->skipped = TRUE;
7796 gst_rtsp_message_unset (&request);
7799 if (pipelined_request_id) {
7800 gst_rtspsrc_setup_streams_end (src, TRUE);
7803 /* store the transport protocol that was configured */
7804 src->cur_protocols = protocols;
7806 gst_rtsp_ext_list_stream_select (src->extensions, url);
7808 if (pipelined_request_id)
7809 g_free (pipelined_request_id);
7811 /* if there is nothing to activate, error out */
7812 if (!src->need_activate)
7813 goto nothing_to_activate;
7820 /* no transport possible, post an error and stop */
7821 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
7822 ("Could not connect to server, no protocols left"));
7823 return GST_RTSP_ERROR;
7827 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7828 ("SDP contains no streams"));
7829 return GST_RTSP_ERROR;
7831 create_request_failed:
7833 gchar *str = gst_rtsp_strresult (res);
7835 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7836 ("Could not create request. (%s)", str));
7840 setup_transport_failed:
7842 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7843 ("Could not setup transport."));
7844 res = GST_RTSP_ERROR;
7849 const gchar *str = gst_rtsp_status_as_text (code);
7851 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7852 ("Error (%d): %s", code, GST_STR_NULL (str)));
7853 res = GST_RTSP_ERROR;
7858 gchar *str = gst_rtsp_strresult (res);
7860 if (res != GST_RTSP_EINTR) {
7861 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7862 ("Could not send message. (%s)", str));
7864 GST_WARNING_OBJECT (src, "send interrupted");
7869 nothing_to_activate:
7871 /* none of the available error codes is really right .. */
7872 if (unsupported_real) {
7873 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
7874 (_("No supported stream was found. You might need to install a "
7875 "GStreamer RTSP extension plugin for Real media streams.")),
7878 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
7879 (_("No supported stream was found. You might need to allow "
7880 "more transport protocols or may otherwise be missing "
7881 "the right GStreamer RTSP extension plugin.")), (NULL));
7883 return GST_RTSP_ERROR;
7887 if (pipelined_request_id)
7888 g_free (pipelined_request_id);
7889 gst_rtsp_message_unset (&request);
7890 gst_rtsp_message_unset (&response);
7896 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
7897 GstSegment * segment, gboolean update_duration)
7899 GstClockTime begin_seconds, end_seconds;
7901 GstRTSPTimeRange *therange;
7904 gst_rtsp_range_free (src->range);
7906 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
7907 GST_DEBUG_OBJECT (src, "parsed range %s", range);
7908 src->range = therange;
7910 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
7912 gst_segment_init (segment, GST_FORMAT_TIME);
7916 gst_rtsp_range_get_times (therange, &begin_seconds, &end_seconds);
7918 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
7919 therange->min.type, therange->min.seconds, therange->max.type,
7920 therange->max.seconds);
7922 if (therange->min.type == GST_RTSP_TIME_NOW)
7924 else if (therange->min.type == GST_RTSP_TIME_END)
7927 seconds = begin_seconds;
7929 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
7930 GST_TIME_ARGS (seconds));
7932 /* we need to start playback without clipping from the position reported by
7934 if (segment->rate > 0.0)
7935 segment->start = seconds;
7937 segment->stop = seconds;
7939 segment->position = seconds;
7941 if (therange->max.type == GST_RTSP_TIME_NOW)
7943 else if (therange->max.type == GST_RTSP_TIME_END)
7946 seconds = end_seconds;
7948 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
7949 GST_TIME_ARGS (seconds));
7951 /* live (WMS) server might send overflowed large max as its idea of infinity,
7952 * compensate to prevent problems later on */
7953 if (seconds != -1 && seconds < 0) {
7955 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
7958 /* live (WMS) might send min == max, which is not worth recording */
7959 if (segment->duration == -1 && seconds == begin_seconds)
7962 /* don't change duration with unknown value, we might have a valid value
7963 * there that we want to keep. Also, the total duration of the stream
7964 * can only be determined from the response to a DESCRIBE request, not
7965 * from a PLAY request where we might have requested a custom range, so
7966 * don't update duration in that case */
7967 if (update_duration && seconds != -1) {
7968 segment->duration = seconds;
7969 GST_DEBUG_OBJECT (src, "set duration from range as %" GST_TIME_FORMAT,
7970 GST_TIME_ARGS (seconds));
7972 GST_DEBUG_OBJECT (src, "not updating existing duration %" GST_TIME_FORMAT
7973 " from range %" GST_TIME_FORMAT, GST_TIME_ARGS (segment->duration),
7974 GST_TIME_ARGS (seconds));
7977 if (segment->rate > 0.0)
7978 segment->stop = seconds;
7980 segment->start = seconds;
7985 /* Parse clock profived by the server with following syntax:
7987 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
7990 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
7992 gboolean res = FALSE;
7994 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
7995 gchar **fields = NULL, **parts = NULL;
7996 gchar *remote_ip, *str;
7998 GstClockTime base_time;
8001 fields = g_strsplit (gstclock, " ", 0);
8003 /* wrapped clock, not very interesting for now */
8004 if (fields[1] == NULL)
8007 /* remote IP address and port */
8008 if ((str = fields[2]) == NULL)
8011 parts = g_strsplit (str, ":", 0);
8013 if ((remote_ip = parts[0]) == NULL)
8016 if ((str = parts[1]) == NULL)
8024 if ((str = fields[3]) == NULL)
8027 base_time = g_ascii_strtoull (str, NULL, 10);
8030 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
8033 if (src->provided_clock)
8034 gst_object_unref (src->provided_clock);
8035 src->provided_clock = netclock;
8037 gst_element_post_message (GST_ELEMENT_CAST (src),
8038 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
8039 src->provided_clock, TRUE));
8043 g_strfreev (fields);
8049 /* must be called with the RTSP state lock */
8050 static GstRTSPResult
8051 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
8057 /* prepare global stream caps properties */
8059 gst_structure_remove_all_fields (src->props);
8061 src->props = gst_structure_new_empty ("RTSPProperties");
8063 DEBUG_SDP (src, sdp);
8065 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
8067 /* let the app inspect and change the SDP */
8068 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
8070 gst_segment_init (&src->segment, GST_FORMAT_TIME);
8072 /* parse range for duration reporting. */
8077 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
8081 /* keep track of the range and configure it in the segment */
8082 if (gst_rtspsrc_parse_range (src, range, &src->segment, TRUE))
8086 /* parse clock information. This is GStreamer specific, a server can tell the
8087 * client what clock it is using and wrap that in a network clock. The
8088 * advantage of that is that we can slave to it. */
8090 const gchar *gstclock;
8093 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
8094 if (gstclock == NULL)
8097 /* parse the clock and expose it in the provide_clock method */
8098 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
8102 /* try to find a global control attribute. Note that a '*' means that we should
8103 * do aggregate control with the current url (so we don't do anything and
8104 * leave the current connection as is) */
8106 const gchar *control;
8109 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
8110 if (control == NULL)
8113 /* only take fully qualified urls */
8114 if (g_str_has_prefix (control, "rtsp://"))
8118 g_free (src->conninfo.location);
8119 src->conninfo.location = g_strdup (control);
8120 /* make a connection for this, if there was a connection already, nothing
8122 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
8123 GST_ERROR_OBJECT (src, "could not connect");
8126 /* we need to keep the control url separate from the connection url because
8127 * the rules for constructing the media control url need it */
8128 g_free (src->control);
8129 src->control = g_strdup (control);
8132 /* create streams */
8133 n_streams = gst_sdp_message_medias_len (sdp);
8134 for (i = 0; i < n_streams; i++) {
8135 gst_rtspsrc_create_stream (src, sdp, i, n_streams);
8138 src->state = GST_RTSP_STATE_INIT;
8141 if ((res = gst_rtspsrc_setup_streams_start (src, async)) < 0)
8144 /* reset our state */
8145 src->need_range = TRUE;
8146 src->server_side_trickmode = FALSE;
8147 src->trickmode_interval = 0;
8149 src->state = GST_RTSP_STATE_READY;
8156 GST_ERROR_OBJECT (src, "setup failed");
8157 gst_rtspsrc_cleanup (src);
8162 static GstRTSPResult
8163 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
8167 GstRTSPMessage request = { 0 };
8168 GstRTSPMessage response = { 0 };
8171 gchar *respcont = NULL;
8172 GstRTSPVersion versions[] =
8173 { GST_RTSP_VERSION_2_0, GST_RTSP_VERSION_INVALID };
8175 src->version = src->default_version;
8176 if (src->default_version == GST_RTSP_VERSION_2_0) {
8177 versions[0] = GST_RTSP_VERSION_1_0;
8181 src->need_redirect = FALSE;
8183 /* can't continue without a valid url */
8184 if (G_UNLIKELY (src->conninfo.url == NULL)) {
8185 res = GST_RTSP_EINVAL;
8188 src->tried_url_auth = FALSE;
8190 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
8191 goto connect_failed;
8193 /* create OPTIONS */
8194 GST_DEBUG_OBJECT (src, "create options... (%s)", async ? "async" : "sync");
8196 gst_rtspsrc_init_request (src, &request, GST_RTSP_OPTIONS,
8197 src->conninfo.url_str);
8199 goto create_request_failed;
8202 request.type_data.request.version = src->version;
8203 GST_DEBUG_OBJECT (src, "send options...");
8206 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
8209 gst_rtspsrc_send (src, &src->conninfo, &request, &response,
8210 NULL, versions)) < 0) {
8214 src->version = request.type_data.request.version;
8215 GST_INFO_OBJECT (src, "Now using version: %s",
8216 gst_rtsp_version_as_text (src->version));
8219 if (!gst_rtspsrc_parse_methods (src, &response))
8222 /* create DESCRIBE */
8223 GST_DEBUG_OBJECT (src, "create describe...");
8225 gst_rtspsrc_init_request (src, &request, GST_RTSP_DESCRIBE,
8226 src->conninfo.url_str);
8228 goto create_request_failed;
8230 /* we only accept SDP for now */
8231 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
8234 if (src->backchannel == BACKCHANNEL_ONVIF)
8235 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
8236 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
8237 /* TODO: Handle the case when backchannel is unsupported and goto restart */
8240 GST_DEBUG_OBJECT (src, "send describe...");
8243 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
8246 gst_rtspsrc_send (src, &src->conninfo, &request, &response,
8250 /* we only perform redirect for describe and play, currently */
8251 if (src->need_redirect) {
8252 /* close connection, we don't have to send a TEARDOWN yet, ignore the
8254 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
8256 gst_rtsp_message_unset (&request);
8257 gst_rtsp_message_unset (&response);
8263 /* it could be that the DESCRIBE method was not implemented */
8264 if (!(src->methods & GST_RTSP_DESCRIBE))
8267 /* check if reply is SDP */
8268 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
8270 /* could not be set but since the request returned OK, we assume it
8271 * was SDP, else check it. */
8273 const gchar *props = strchr (respcont, ';');
8276 gchar *mimetype = g_strndup (respcont, props - respcont);
8278 mimetype = g_strstrip (mimetype);
8279 if (g_ascii_strcasecmp (mimetype, "application/sdp") != 0) {
8281 goto wrong_content_type;
8284 /* TODO: Check for charset property and do conversions of all messages if
8285 * needed. Some servers actually send that property */
8288 } else if (g_ascii_strcasecmp (respcont, "application/sdp") != 0) {
8289 goto wrong_content_type;
8293 /* get message body and parse as SDP */
8294 gst_rtsp_message_get_body (&response, &data, &size);
8295 if (data == NULL || size == 0)
8298 GST_DEBUG_OBJECT (src, "parse SDP...");
8299 gst_sdp_message_new (sdp);
8300 gst_sdp_message_parse_buffer (data, size, *sdp);
8302 /* clean up any messages */
8303 gst_rtsp_message_unset (&request);
8304 gst_rtsp_message_unset (&response);
8311 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
8312 ("No valid RTSP URL was provided"));
8317 gchar *str = gst_rtsp_strresult (res);
8319 if (res != GST_RTSP_EINTR) {
8320 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
8321 ("Failed to connect. (%s)", str));
8323 GST_WARNING_OBJECT (src, "connect interrupted");
8328 create_request_failed:
8330 gchar *str = gst_rtsp_strresult (res);
8332 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
8333 ("Could not create request. (%s)", str));
8339 /* Don't post a message - the rtsp_send method will have
8340 * taken care of it because we passed NULL for the response code */
8345 /* error was posted */
8346 res = GST_RTSP_ERROR;
8351 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
8352 ("Server does not support SDP, got %s.", respcont));
8353 res = GST_RTSP_ERROR;
8358 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
8359 ("Server can not provide an SDP."));
8360 res = GST_RTSP_ERROR;
8365 if (src->conninfo.connection) {
8366 GST_DEBUG_OBJECT (src, "free connection");
8367 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
8369 gst_rtsp_message_unset (&request);
8370 gst_rtsp_message_unset (&response);
8375 static GstRTSPResult
8376 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
8381 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
8383 if (src->sdp == NULL) {
8384 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
8388 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
8391 if (src->initial_seek) {
8392 if (!gst_rtspsrc_perform_seek (src, src->initial_seek))
8393 goto initial_seek_failed;
8394 gst_event_replace (&src->initial_seek, NULL);
8399 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
8406 GST_WARNING_OBJECT (src, "can't get sdp");
8407 src->open_error = TRUE;
8412 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
8413 src->open_error = TRUE;
8416 initial_seek_failed:
8418 GST_WARNING_OBJECT (src, "Failed to perform initial seek");
8419 ret = GST_RTSP_ERROR;
8420 src->open_error = TRUE;
8425 static GstRTSPResult
8426 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
8428 GstRTSPMessage request = { 0 };
8429 GstRTSPMessage response = { 0 };
8430 GstRTSPResult res = GST_RTSP_OK;
8432 const gchar *control;
8434 GST_DEBUG_OBJECT (src, "TEARDOWN...");
8436 gst_rtspsrc_set_state (src, GST_STATE_READY);
8438 if (src->state < GST_RTSP_STATE_READY) {
8439 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
8446 /* construct a control url */
8447 control = get_aggregate_control (src);
8449 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
8452 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8453 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8454 const gchar *setup_url;
8455 GstRTSPConnInfo *info;
8457 /* try aggregate control first but do non-aggregate control otherwise */
8459 setup_url = control;
8460 else if ((setup_url = stream->conninfo.location) == NULL)
8463 if (src->conninfo.connection) {
8464 info = &src->conninfo;
8465 } else if (stream->conninfo.connection) {
8466 info = &stream->conninfo;
8470 if (!info->connected)
8475 gst_rtspsrc_init_request (src, &request, GST_RTSP_TEARDOWN, setup_url);
8476 GST_LOG_OBJECT (src, "Teardown on %s", setup_url);
8478 goto create_request_failed;
8480 if (stream->is_backchannel && src->backchannel == BACKCHANNEL_ONVIF)
8481 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
8482 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
8485 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
8488 gst_rtspsrc_send (src, info, &request, &response, NULL, NULL)) < 0)
8491 /* FIXME, parse result? */
8492 gst_rtsp_message_unset (&request);
8493 gst_rtsp_message_unset (&response);
8496 /* early exit when we did aggregate control */
8502 /* close connections */
8503 GST_DEBUG_OBJECT (src, "closing connection...");
8504 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
8505 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8506 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8507 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
8511 gst_rtspsrc_cleanup (src);
8513 src->state = GST_RTSP_STATE_INVALID;
8516 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
8521 create_request_failed:
8523 gchar *str = gst_rtsp_strresult (res);
8525 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
8526 ("Could not create request. (%s)", str));
8532 gchar *str = gst_rtsp_strresult (res);
8534 gst_rtsp_message_unset (&request);
8535 if (res != GST_RTSP_EINTR) {
8536 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
8537 ("Could not send message. (%s)", str));
8539 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
8546 GST_DEBUG_OBJECT (src,
8547 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
8552 /* RTP-Info is of the format:
8554 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
8556 * rtptime corresponds to the timestamp for the NPT time given in the header
8557 * seqbase corresponds to the next sequence number we received. This number
8558 * indicates the first seqnum after the seek and should be used to discard
8559 * packets that are from before the seek.
8562 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
8567 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
8569 infos = g_strsplit (rtpinfo, ",", 0);
8570 for (i = 0; infos[i]; i++) {
8572 GstRTSPStream *stream;
8576 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
8578 /* init values, types of seqbase and timebase are bigger than needed so we
8579 * can store -1 as uninitialized values */
8584 /* parse url, find stream for url.
8585 * parse seq and rtptime. The seq number should be configured in the rtp
8586 * depayloader or session manager to detect gaps. Same for the rtptime, it
8587 * should be used to create an initial time newsegment. */
8588 fields = g_strsplit (infos[i], ";", 0);
8589 for (j = 0; fields[j]; j++) {
8590 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
8591 /* remove leading whitespace */
8592 fields[j] = g_strchug (fields[j]);
8593 if (g_str_has_prefix (fields[j], "url=")) {
8594 /* get the url and the stream */
8596 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
8597 } else if (g_str_has_prefix (fields[j], "seq=")) {
8598 seqbase = atoi (fields[j] + 4);
8599 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
8600 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
8603 g_strfreev (fields);
8604 /* now we need to store the values for the caps of the stream */
8605 if (stream != NULL) {
8606 GST_DEBUG_OBJECT (src,
8607 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
8608 stream, seqbase, timebase);
8610 /* we have a stream, configure detected params */
8611 stream->seqbase = seqbase;
8612 stream->timebase = timebase;
8621 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
8626 interval = strtoul (rtcp, NULL, 10);
8627 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
8632 interval *= GST_MSECOND;
8634 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8635 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8637 /* already (optionally) retrieved this when configuring manager */
8638 if (stream->session) {
8639 GObject *rtpsession = stream->session;
8641 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
8643 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
8647 /* now it happens that (Xenon) server sending this may also provide bogus
8648 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
8649 * and just use RTP-Info to sync */
8651 GObjectClass *klass;
8653 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
8654 if (g_object_class_find_property (klass, "rtcp-sync")) {
8655 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
8656 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
8662 gst_rtspsrc_get_float (const gchar * dstr)
8664 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
8666 /* canonicalise floating point string so we can handle float strings
8667 * in the form "24.930" or "24,930" irrespective of the current locale */
8668 g_strlcpy (s, dstr, sizeof (s));
8669 g_strdelimit (s, ",", '.');
8670 return g_ascii_strtod (s, NULL);
8674 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
8676 GstRTSPTimeRange range = { 0, };
8677 gdouble begin_seconds, end_seconds;
8679 if (segment->rate > 0) {
8680 begin_seconds = (gdouble) segment->start / GST_SECOND;
8681 end_seconds = (gdouble) segment->stop / GST_SECOND;
8683 begin_seconds = (gdouble) segment->stop / GST_SECOND;
8684 end_seconds = (gdouble) segment->start / GST_SECOND;
8687 if (src->onvif_mode) {
8688 GDateTime *prime_epoch, *datetime;
8690 range.unit = GST_RTSP_RANGE_CLOCK;
8692 prime_epoch = g_date_time_new_utc (1900, 1, 1, 0, 0, 0);
8694 datetime = g_date_time_add_seconds (prime_epoch, begin_seconds);
8696 range.min.type = GST_RTSP_TIME_UTC;
8697 range.min2.year = g_date_time_get_year (datetime);
8698 range.min2.month = g_date_time_get_month (datetime);
8699 range.min2.day = g_date_time_get_day_of_month (datetime);
8701 g_date_time_get_seconds (datetime) +
8702 g_date_time_get_minute (datetime) * 60 +
8703 g_date_time_get_hour (datetime) * 60 * 60;
8705 g_date_time_unref (datetime);
8707 datetime = g_date_time_add_seconds (prime_epoch, end_seconds);
8709 range.max.type = GST_RTSP_TIME_UTC;
8710 range.max2.year = g_date_time_get_year (datetime);
8711 range.max2.month = g_date_time_get_month (datetime);
8712 range.max2.day = g_date_time_get_day_of_month (datetime);
8714 g_date_time_get_seconds (datetime) +
8715 g_date_time_get_minute (datetime) * 60 +
8716 g_date_time_get_hour (datetime) * 60 * 60;
8718 g_date_time_unref (datetime);
8719 g_date_time_unref (prime_epoch);
8721 range.unit = GST_RTSP_RANGE_NPT;
8723 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
8724 range.min.type = GST_RTSP_TIME_NOW;
8726 range.min.type = GST_RTSP_TIME_SECONDS;
8727 range.min.seconds = begin_seconds;
8730 if (src->range && src->range->max.type == GST_RTSP_TIME_END) {
8731 range.max.type = GST_RTSP_TIME_END;
8733 range.max.type = GST_RTSP_TIME_SECONDS;
8734 range.max.seconds = end_seconds;
8738 /* Don't set end bounds when not required to */
8739 if (!GST_CLOCK_TIME_IS_VALID (segment->stop)) {
8740 if (segment->rate > 0)
8741 range.max.type = GST_RTSP_TIME_END;
8743 range.min.type = GST_RTSP_TIME_END;
8746 return gst_rtsp_range_to_string (&range);
8750 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
8754 stream->timebase = -1;
8755 stream->seqbase = -1;
8757 len = stream->ptmap->len;
8758 for (i = 0; i < len; i++) {
8759 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
8762 if (item->caps == NULL)
8765 item->caps = gst_caps_make_writable (item->caps);
8766 s = gst_caps_get_structure (item->caps, 0);
8767 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
8768 if (item->pt == stream->default_pt && stream->udpsrc[0])
8769 g_object_set (stream->udpsrc[0], "caps", item->caps, NULL);
8771 stream->need_caps = TRUE;
8774 static GstRTSPResult
8775 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
8777 GstRTSPResult res = GST_RTSP_OK;
8779 if (src->state < GST_RTSP_STATE_READY) {
8780 res = GST_RTSP_ERROR;
8781 if (src->open_error) {
8782 GST_DEBUG_OBJECT (src, "the stream was in error");
8786 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
8788 if ((res = gst_rtspsrc_open (src, async)) < 0) {
8789 GST_DEBUG_OBJECT (src, "failed to open stream");
8798 static GstRTSPResult
8799 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async,
8800 const gchar * seek_style)
8802 GstRTSPMessage request = { 0 };
8803 GstRTSPMessage response = { 0 };
8804 GstRTSPResult res = GST_RTSP_OK;
8808 const gchar *control;
8809 GstSegment requested;
8811 GST_DEBUG_OBJECT (src, "PLAY...");
8814 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
8817 if (!(src->methods & GST_RTSP_PLAY))
8820 if (src->state == GST_RTSP_STATE_PLAYING)
8823 if (!src->conninfo.connection || !src->conninfo.connected)
8826 requested = *segment;
8828 /* send some dummy packets before we activate the receive in the
8830 gst_rtspsrc_send_dummy_packets (src);
8832 /* require new SR packets */
8834 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
8836 /* construct a control url */
8837 control = get_aggregate_control (src);
8839 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8840 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8841 const gchar *setup_url;
8842 GstRTSPConnInfo *conninfo;
8844 /* try aggregate control first but do non-aggregate control otherwise */
8846 setup_url = control;
8847 else if ((setup_url = stream->conninfo.location) == NULL)
8850 if (src->conninfo.connection) {
8851 conninfo = &src->conninfo;
8852 } else if (stream->conninfo.connection) {
8853 conninfo = &stream->conninfo;
8859 res = gst_rtspsrc_init_request (src, &request, GST_RTSP_PLAY, setup_url);
8861 goto create_request_failed;
8863 if (src->need_range && src->seekable >= 0.0) {
8864 hval = gen_range_header (src, segment);
8866 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
8868 /* store the newsegment event so it can be sent from the streaming thread. */
8869 src->need_segment = TRUE;
8872 if (segment->rate != 1.0) {
8873 gchar scale_val[G_ASCII_DTOSTR_BUF_SIZE];
8874 gchar speed_val[G_ASCII_DTOSTR_BUF_SIZE];
8876 if (src->server_side_trickmode) {
8877 g_ascii_dtostr (scale_val, sizeof (scale_val), segment->rate);
8878 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, scale_val);
8879 } else if (segment->rate < 0.0) {
8880 g_ascii_dtostr (scale_val, sizeof (scale_val), -1.0);
8881 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, scale_val);
8883 if (ABS (segment->rate) != 1.0) {
8884 g_ascii_dtostr (speed_val, sizeof (speed_val), ABS (segment->rate));
8885 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, speed_val);
8888 g_ascii_dtostr (speed_val, sizeof (speed_val), segment->rate);
8889 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, speed_val);
8893 if (src->onvif_mode) {
8894 if (segment->flags & GST_SEEK_FLAG_TRICKMODE_KEY_UNITS) {
8897 if (src->trickmode_interval)
8899 g_strdup_printf ("intra/%" G_GUINT64_FORMAT,
8900 src->trickmode_interval / GST_MSECOND);
8902 hval = g_strdup ("intra");
8904 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_FRAMES, hval);
8907 } else if (segment->flags & GST_SEEK_FLAG_TRICKMODE_FORWARD_PREDICTED) {
8908 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_FRAMES,
8914 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SEEK_STYLE,
8917 /* when we have an ONVIF audio backchannel, the PLAY request must have the
8918 * Require: header when doing either aggregate or non-aggregate control */
8919 if (src->backchannel == BACKCHANNEL_ONVIF &&
8920 (control || stream->is_backchannel))
8921 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
8922 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
8924 if (src->onvif_mode) {
8925 if (src->onvif_rate_control)
8926 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_RATE_CONTROL,
8929 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_RATE_CONTROL, "no");
8933 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
8936 gst_rtspsrc_send (src, conninfo, &request, &response, NULL, NULL))
8940 if (src->need_redirect) {
8941 GST_DEBUG_OBJECT (src,
8942 "redirect: tearing down and restarting with new url");
8943 /* teardown and restart with new url */
8944 gst_rtspsrc_close (src, TRUE, FALSE);
8945 /* reset protocols to force re-negotiation with redirected url */
8946 src->cur_protocols = src->protocols;
8947 gst_rtsp_message_unset (&request);
8948 gst_rtsp_message_unset (&response);
8952 /* seek may have silently failed as it is not supported */
8953 if (!(src->methods & GST_RTSP_PLAY)) {
8954 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
8956 if (src->version >= GST_RTSP_VERSION_2_0 && src->seekable >= 0.0) {
8957 GST_WARNING_OBJECT (src, "Server declared stream as seekable but"
8958 " playing with range failed... Ignoring information.");
8960 /* obviously it is supported as we made it here */
8961 src->methods |= GST_RTSP_PLAY;
8962 src->seekable = -1.0;
8963 /* but there is nothing to parse in the response,
8964 * so convey we have no idea and not to expect anything particular */
8965 clear_rtp_base (src, stream);
8969 /* need to do for all streams */
8970 for (run = src->streams; run; run = g_list_next (run))
8971 clear_rtp_base (src, (GstRTSPStream *) run->data);
8973 /* NOTE the above also disables npt based eos detection */
8974 /* and below forces position to 0,
8975 * which is visible feedback we lost the plot */
8976 segment->start = segment->position = src->last_pos;
8979 gst_rtsp_message_unset (&request);
8981 /* parse RTP npt field. This is the current position in the stream (Normal
8982 * Play Time) and should be put in the NEWSEGMENT position field. */
8983 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
8985 gst_rtspsrc_parse_range (src, hval, segment, FALSE);
8987 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
8988 segment->rate = 1.0;
8990 /* parse Speed header. This is the intended playback rate of the stream
8991 * and should be put in the NEWSEGMENT rate field. */
8992 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
8993 0) == GST_RTSP_OK) {
8994 segment->rate = gst_rtspsrc_get_float (hval);
8995 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
8996 &hval, 0) == GST_RTSP_OK) {
8997 segment->rate = gst_rtspsrc_get_float (hval);
9000 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
9001 * for the RTP packets. If this is not present, we assume all starts from 0...
9002 * This is info for the RTP session manager that we pass to it in caps. */
9004 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
9005 &hval, hval_idx++) == GST_RTSP_OK)
9006 gst_rtspsrc_parse_rtpinfo (src, hval);
9008 /* some servers indicate RTCP parameters in PLAY response,
9009 * rather than properly in SDP */
9010 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
9011 &hval, 0) == GST_RTSP_OK)
9012 gst_rtspsrc_handle_rtcp_interval (src, hval);
9014 gst_rtsp_message_unset (&response);
9016 /* early exit when we did aggregate control */
9021 src->out_segment = *segment;
9023 if (src->clip_out_segment) {
9024 /* Only clip the output segment when the server has answered with valid
9025 * values, we cannot know otherwise whether the requested bounds were
9027 if (GST_CLOCK_TIME_IS_VALID (src->segment.start) &&
9028 GST_CLOCK_TIME_IS_VALID (requested.start))
9029 src->out_segment.start = MAX (src->out_segment.start, requested.start);
9030 if (GST_CLOCK_TIME_IS_VALID (src->segment.stop) &&
9031 GST_CLOCK_TIME_IS_VALID (requested.stop))
9032 src->out_segment.stop = MIN (src->out_segment.stop, requested.stop);
9035 /* configure the caps of the streams after we parsed all headers. Only reset
9036 * the manager object when we set a new Range header (we did a seek) */
9037 gst_rtspsrc_configure_caps (src, segment, src->need_range);
9039 /* set to PLAYING after we have configured the caps, otherwise we
9040 * might end up calling request_key (with SRTP) while caps are still
9041 * being configured. */
9042 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
9044 /* set again when needed */
9045 src->need_range = FALSE;
9047 src->running = TRUE;
9048 src->base_time = -1;
9049 src->state = GST_RTSP_STATE_PLAYING;
9052 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
9053 for (walk = src->streams; walk; walk = g_list_next (walk)) {
9054 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
9055 stream->discont = TRUE;
9060 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
9067 GST_WARNING_OBJECT (src, "failed to open stream");
9072 GST_WARNING_OBJECT (src, "PLAY is not supported");
9077 GST_WARNING_OBJECT (src, "we were already PLAYING");
9080 create_request_failed:
9082 gchar *str = gst_rtsp_strresult (res);
9084 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
9085 ("Could not create request. (%s)", str));
9091 gchar *str = gst_rtsp_strresult (res);
9093 gst_rtsp_message_unset (&request);
9094 if (res != GST_RTSP_EINTR) {
9095 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
9096 ("Could not send message. (%s)", str));
9098 GST_WARNING_OBJECT (src, "PLAY interrupted");
9105 static GstRTSPResult
9106 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
9108 GstRTSPResult res = GST_RTSP_OK;
9109 GstRTSPMessage request = { 0 };
9110 GstRTSPMessage response = { 0 };
9112 const gchar *control;
9114 GST_DEBUG_OBJECT (src, "PAUSE...");
9116 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
9119 if (!(src->methods & GST_RTSP_PAUSE))
9122 if (src->state == GST_RTSP_STATE_READY)
9125 if (!src->conninfo.connection || !src->conninfo.connected)
9128 /* construct a control url */
9129 control = get_aggregate_control (src);
9131 /* loop over the streams. We might exit the loop early when we could do an
9132 * aggregate control */
9133 for (walk = src->streams; walk; walk = g_list_next (walk)) {
9134 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
9135 GstRTSPConnInfo *conninfo;
9136 const gchar *setup_url;
9138 /* try aggregate control first but do non-aggregate control otherwise */
9140 setup_url = control;
9141 else if ((setup_url = stream->conninfo.location) == NULL)
9144 if (src->conninfo.connection) {
9145 conninfo = &src->conninfo;
9146 } else if (stream->conninfo.connection) {
9147 conninfo = &stream->conninfo;
9153 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
9154 ("Sending PAUSE request"));
9157 gst_rtspsrc_init_request (src, &request, GST_RTSP_PAUSE,
9159 goto create_request_failed;
9161 /* when we have an ONVIF audio backchannel, the PAUSE request must have the
9162 * Require: header when doing either aggregate or non-aggregate control */
9163 if (src->backchannel == BACKCHANNEL_ONVIF &&
9164 (control || stream->is_backchannel))
9165 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
9166 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
9169 gst_rtspsrc_send (src, conninfo, &request, &response, NULL,
9173 gst_rtsp_message_unset (&request);
9174 gst_rtsp_message_unset (&response);
9176 /* exit early when we did aggregate control */
9181 /* change element states now */
9182 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
9185 src->state = GST_RTSP_STATE_READY;
9189 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
9196 GST_DEBUG_OBJECT (src, "failed to open stream");
9201 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
9206 GST_DEBUG_OBJECT (src, "we were already PAUSED");
9209 create_request_failed:
9211 gchar *str = gst_rtsp_strresult (res);
9213 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
9214 ("Could not create request. (%s)", str));
9220 gchar *str = gst_rtsp_strresult (res);
9222 gst_rtsp_message_unset (&request);
9223 if (res != GST_RTSP_EINTR) {
9224 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
9225 ("Could not send message. (%s)", str));
9227 GST_WARNING_OBJECT (src, "PAUSE interrupted");
9235 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
9237 GstRTSPSrc *rtspsrc;
9239 rtspsrc = GST_RTSPSRC (bin);
9241 switch (GST_MESSAGE_TYPE (message)) {
9242 case GST_MESSAGE_STREAM_START:
9243 case GST_MESSAGE_EOS:
9244 gst_message_unref (message);
9246 case GST_MESSAGE_ELEMENT:
9248 const GstStructure *s = gst_message_get_structure (message);
9250 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
9251 gboolean ignore_timeout;
9253 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
9255 GST_OBJECT_LOCK (rtspsrc);
9256 ignore_timeout = rtspsrc->ignore_timeout;
9257 rtspsrc->ignore_timeout = TRUE;
9258 GST_OBJECT_UNLOCK (rtspsrc);
9260 /* we only act on the first udp timeout message, others are irrelevant
9261 * and can be ignored. */
9262 if (!ignore_timeout)
9263 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
9265 gst_message_unref (message);
9268 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
9271 case GST_MESSAGE_ERROR:
9274 GstRTSPStream *stream;
9277 udpsrc = GST_MESSAGE_SRC (message);
9279 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
9280 GST_ELEMENT_NAME (udpsrc));
9282 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
9286 /* we ignore the RTCP udpsrc */
9287 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
9290 /* if we get error messages from the udp sources, that's not a problem as
9291 * long as not all of them error out. We also don't really know what the
9292 * problem is, the message does not give enough detail... */
9293 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
9294 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
9295 if (ret != GST_FLOW_OK)
9299 gst_message_unref (message);
9303 /* fatal but not our message, forward */
9304 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
9309 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
9315 /* the thread where everything happens */
9317 gst_rtspsrc_thread (GstRTSPSrc * src)
9320 ParameterRequest *req = NULL;
9322 GST_OBJECT_LOCK (src);
9323 cmd = src->pending_cmd;
9324 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
9325 || cmd == CMD_LOOP || cmd == CMD_OPEN || cmd == CMD_GET_PARAMETER
9326 || cmd == CMD_SET_PARAMETER) {
9327 if (g_queue_is_empty (&src->set_get_param_q)) {
9328 src->pending_cmd = CMD_LOOP;
9330 ParameterRequest *next_req;
9331 if (cmd == CMD_GET_PARAMETER || cmd == CMD_SET_PARAMETER) {
9332 req = g_queue_pop_head (&src->set_get_param_q);
9334 next_req = g_queue_peek_head (&src->set_get_param_q);
9335 src->pending_cmd = next_req ? next_req->cmd : CMD_LOOP;
9338 src->pending_cmd = CMD_WAIT;
9339 GST_DEBUG_OBJECT (src, "got command %s", cmd_to_string (cmd));
9341 /* we got the message command, so ensure communication is possible again */
9342 gst_rtspsrc_connection_flush (src, FALSE);
9344 src->busy_cmd = cmd;
9345 GST_OBJECT_UNLOCK (src);
9349 gst_rtspsrc_open (src, TRUE);
9352 gst_rtspsrc_play (src, &src->segment, TRUE, NULL);
9355 gst_rtspsrc_pause (src, TRUE);
9358 gst_rtspsrc_close (src, TRUE, FALSE);
9360 case CMD_GET_PARAMETER:
9361 gst_rtspsrc_get_parameter (src, req);
9363 case CMD_SET_PARAMETER:
9364 gst_rtspsrc_set_parameter (src, req);
9367 gst_rtspsrc_loop (src);
9370 gst_rtspsrc_reconnect (src, FALSE);
9376 GST_OBJECT_LOCK (src);
9377 /* No more cmds, wake any waiters */
9378 g_cond_broadcast (&src->cmd_cond);
9379 /* and go back to sleep */
9380 if (src->pending_cmd == CMD_WAIT) {
9382 gst_task_pause (src->task);
9385 src->busy_cmd = CMD_WAIT;
9386 GST_OBJECT_UNLOCK (src);
9390 gst_rtspsrc_start (GstRTSPSrc * src)
9392 GST_DEBUG_OBJECT (src, "starting");
9394 GST_OBJECT_LOCK (src);
9396 src->pending_cmd = CMD_WAIT;
9398 if (src->task == NULL) {
9399 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
9400 if (src->task == NULL)
9403 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
9405 GST_OBJECT_UNLOCK (src);
9412 GST_OBJECT_UNLOCK (src);
9413 GST_ERROR_OBJECT (src, "failed to create task");
9419 gst_rtspsrc_stop (GstRTSPSrc * src)
9423 GST_DEBUG_OBJECT (src, "stopping");
9425 /* also cancels pending task */
9426 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
9428 GST_OBJECT_LOCK (src);
9429 if ((task = src->task)) {
9431 GST_OBJECT_UNLOCK (src);
9433 gst_task_stop (task);
9435 /* make sure it is not running */
9436 GST_RTSP_STREAM_LOCK (src);
9437 GST_RTSP_STREAM_UNLOCK (src);
9439 /* now wait for the task to finish */
9440 gst_task_join (task);
9442 /* and free the task */
9443 gst_object_unref (GST_OBJECT (task));
9445 GST_OBJECT_LOCK (src);
9447 GST_OBJECT_UNLOCK (src);
9449 /* ensure synchronously all is closed and clean */
9450 gst_rtspsrc_close (src, FALSE, TRUE);
9455 static GstStateChangeReturn
9456 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
9458 GstRTSPSrc *rtspsrc;
9459 GstStateChangeReturn ret;
9461 rtspsrc = GST_RTSPSRC (element);
9463 switch (transition) {
9464 case GST_STATE_CHANGE_NULL_TO_READY:
9465 if (!gst_rtspsrc_start (rtspsrc))
9468 case GST_STATE_CHANGE_READY_TO_PAUSED:
9469 rtspsrc->seek_seqnum = gst_util_seqnum_next ();
9470 /* init some state */
9471 rtspsrc->cur_protocols = rtspsrc->protocols;
9472 /* first attempt, don't ignore timeouts */
9473 rtspsrc->ignore_timeout = FALSE;
9474 rtspsrc->open_error = FALSE;
9475 if (rtspsrc->is_live)
9476 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
9478 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
9480 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
9481 set_manager_buffer_mode (rtspsrc);
9483 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
9484 if (rtspsrc->is_live) {
9485 /* unblock the tcp tasks and make the loop waiting */
9486 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
9487 /* make sure it is waiting before we send PAUSE or PLAY below */
9488 GST_RTSP_STREAM_LOCK (rtspsrc);
9489 GST_RTSP_STREAM_UNLOCK (rtspsrc);
9493 case GST_STATE_CHANGE_PAUSED_TO_READY:
9494 rtspsrc->group_id = GST_GROUP_ID_INVALID;
9500 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
9501 if (ret == GST_STATE_CHANGE_FAILURE)
9504 switch (transition) {
9505 case GST_STATE_CHANGE_NULL_TO_READY:
9506 ret = GST_STATE_CHANGE_SUCCESS;
9508 case GST_STATE_CHANGE_READY_TO_PAUSED:
9509 if (rtspsrc->is_live)
9510 ret = GST_STATE_CHANGE_NO_PREROLL;
9512 ret = GST_STATE_CHANGE_SUCCESS;
9514 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
9515 if (rtspsrc->is_live)
9516 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
9517 ret = GST_STATE_CHANGE_SUCCESS;
9519 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
9520 if (rtspsrc->is_live) {
9521 /* send pause request and keep the idle task around */
9522 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
9524 ret = GST_STATE_CHANGE_SUCCESS;
9526 case GST_STATE_CHANGE_PAUSED_TO_READY:
9527 rtspsrc->seek_seqnum = GST_SEQNUM_INVALID;
9528 gst_rtspsrc_loop_send_cmd_and_wait (rtspsrc, CMD_CLOSE, CMD_ALL,
9529 rtspsrc->teardown_timeout);
9530 ret = GST_STATE_CHANGE_SUCCESS;
9532 case GST_STATE_CHANGE_READY_TO_NULL:
9533 gst_rtspsrc_stop (rtspsrc);
9534 ret = GST_STATE_CHANGE_SUCCESS;
9537 /* Otherwise it's success, we don't want to return spurious
9538 * NO_PREROLL or ASYNC from internal elements as we care for
9539 * state changes ourselves here
9541 * This is to catch PAUSED->PAUSED and PLAYING->PLAYING transitions.
9543 if (GST_STATE_TRANSITION_NEXT (transition) == GST_STATE_PAUSED)
9544 ret = GST_STATE_CHANGE_NO_PREROLL;
9546 ret = GST_STATE_CHANGE_SUCCESS;
9555 GST_DEBUG_OBJECT (rtspsrc, "start failed");
9556 return GST_STATE_CHANGE_FAILURE;
9561 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
9564 GstRTSPSrc *rtspsrc;
9566 rtspsrc = GST_RTSPSRC (element);
9568 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK) {
9569 if (rtspsrc->state >= GST_RTSP_STATE_READY) {
9570 res = gst_rtspsrc_perform_seek (rtspsrc, event);
9571 gst_event_unref (event);
9573 /* Store for later use */
9575 rtspsrc->initial_seek = event;
9577 } else if (GST_EVENT_IS_DOWNSTREAM (event)) {
9578 res = gst_rtspsrc_push_event (rtspsrc, event);
9580 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
9587 /*** GSTURIHANDLER INTERFACE *************************************************/
9590 gst_rtspsrc_uri_get_type (GType type)
9595 static const gchar *const *
9596 gst_rtspsrc_uri_get_protocols (GType type)
9598 static const gchar *protocols[] =
9599 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
9600 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
9607 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
9609 GstRTSPSrc *src = GST_RTSPSRC (handler);
9611 /* FIXME: make thread-safe */
9612 return g_strdup (src->conninfo.location);
9616 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
9622 GstRTSPUrl *newurl = NULL;
9623 GstSDPMessage *sdp = NULL;
9625 src = GST_RTSPSRC (handler);
9627 /* same URI, we're fine */
9628 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
9631 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
9632 sres = gst_sdp_message_new (&sdp);
9636 GST_DEBUG_OBJECT (src, "parsing SDP message");
9637 sres = gst_sdp_message_parse_uri (uri, sdp);
9642 GST_DEBUG_OBJECT (src, "parsing URI");
9643 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
9647 /* if worked, free previous and store new url object along with the original
9649 GST_DEBUG_OBJECT (src, "configuring URI");
9650 g_free (src->conninfo.location);
9651 src->conninfo.location = g_strdup (uri);
9652 gst_rtsp_url_free (src->conninfo.url);
9653 src->conninfo.url = newurl;
9654 g_free (src->conninfo.url_str);
9656 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
9658 src->conninfo.url_str = NULL;
9661 gst_sdp_message_free (src->sdp);
9663 src->from_sdp = sdp != NULL;
9665 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
9666 GST_DEBUG_OBJECT (src, "request uri is: %s",
9667 GST_STR_NULL (src->conninfo.url_str));
9674 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
9679 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", sres);
9680 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
9681 "Could not create SDP");
9686 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", sres,
9687 GST_STR_NULL (uri));
9688 gst_sdp_message_free (sdp);
9689 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
9695 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
9696 GST_STR_NULL (uri), res);
9697 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
9698 "Invalid RTSP URI");
9704 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
9706 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
9708 iface->get_type = gst_rtspsrc_uri_get_type;
9709 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
9710 iface->get_uri = gst_rtspsrc_uri_get_uri;
9711 iface->set_uri = gst_rtspsrc_uri_set_uri;
9715 /* send GET_PARAMETER */
9716 static GstRTSPResult
9717 gst_rtspsrc_get_parameter (GstRTSPSrc * src, ParameterRequest * req)
9719 GstRTSPMessage request = { 0 };
9720 GstRTSPMessage response = { 0 };
9722 GstRTSPStatusCode code = GST_RTSP_STS_OK;
9723 const gchar *control;
9724 gchar *recv_body = NULL;
9725 guint recv_body_len;
9727 GST_DEBUG_OBJECT (src, "creating server get_parameter");
9731 if ((res = gst_rtspsrc_ensure_open (src, FALSE)) < 0)
9734 control = get_aggregate_control (src);
9735 if (control == NULL)
9738 if (!(src->methods & GST_RTSP_GET_PARAMETER))
9741 gst_rtspsrc_connection_flush (src, FALSE);
9743 res = gst_rtsp_message_init_request (&request, GST_RTSP_GET_PARAMETER,
9746 goto create_request_failed;
9748 res = gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CONTENT_TYPE,
9749 req->content_type == NULL ? "text/parameters" : req->content_type);
9751 goto add_content_hdr_failed;
9753 if (req->body && req->body->len) {
9755 gst_rtsp_message_set_body (&request, (guint8 *) req->body->str,
9758 goto set_body_failed;
9761 if ((res = gst_rtspsrc_send (src, &src->conninfo,
9762 &request, &response, &code, NULL)) < 0)
9765 res = gst_rtsp_message_get_body (&response, (guint8 **) & recv_body,
9768 goto get_body_failed;
9772 gst_promise_reply (req->promise,
9773 gst_structure_new ("get-parameter-reply",
9774 "rtsp-result", G_TYPE_INT, res,
9775 "rtsp-code", G_TYPE_INT, code,
9776 "rtsp-reason", G_TYPE_STRING, gst_rtsp_status_as_text (code),
9777 "body", G_TYPE_STRING, GST_STR_NULL (recv_body), NULL));
9778 free_param_data (req);
9781 gst_rtsp_message_unset (&request);
9782 gst_rtsp_message_unset (&response);
9790 GST_DEBUG_OBJECT (src, "failed to open stream");
9795 GST_DEBUG_OBJECT (src, "no control url to send GET_PARAMETER");
9796 res = GST_RTSP_ERROR;
9801 GST_DEBUG_OBJECT (src, "GET_PARAMETER is not supported");
9802 res = GST_RTSP_ERROR;
9805 create_request_failed:
9807 GST_DEBUG_OBJECT (src, "could not create GET_PARAMETER request");
9810 add_content_hdr_failed:
9812 GST_DEBUG_OBJECT (src, "could not add content header");
9817 GST_DEBUG_OBJECT (src, "could not set body");
9822 gchar *str = gst_rtsp_strresult (res);
9824 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
9825 ("Could not send get-parameter. (%s)", str));
9831 GST_DEBUG_OBJECT (src, "could not get body");
9836 /* send SET_PARAMETER */
9837 static GstRTSPResult
9838 gst_rtspsrc_set_parameter (GstRTSPSrc * src, ParameterRequest * req)
9840 GstRTSPMessage request = { 0 };
9841 GstRTSPMessage response = { 0 };
9842 GstRTSPResult res = GST_RTSP_OK;
9843 GstRTSPStatusCode code = GST_RTSP_STS_OK;
9844 const gchar *control;
9846 GST_DEBUG_OBJECT (src, "creating server set_parameter");
9850 if ((res = gst_rtspsrc_ensure_open (src, FALSE)) < 0)
9853 control = get_aggregate_control (src);
9854 if (control == NULL)
9857 if (!(src->methods & GST_RTSP_SET_PARAMETER))
9860 gst_rtspsrc_connection_flush (src, FALSE);
9863 gst_rtsp_message_init_request (&request, GST_RTSP_SET_PARAMETER, control);
9867 res = gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CONTENT_TYPE,
9868 req->content_type == NULL ? "text/parameters" : req->content_type);
9870 goto add_content_hdr_failed;
9872 if (req->body && req->body->len) {
9874 gst_rtsp_message_set_body (&request, (guint8 *) req->body->str,
9878 goto set_body_failed;
9881 if ((res = gst_rtspsrc_send (src, &src->conninfo,
9882 &request, &response, &code, NULL)) < 0)
9887 gst_promise_reply (req->promise, gst_structure_new ("set-parameter-reply",
9888 "rtsp-result", G_TYPE_INT, res,
9889 "rtsp-code", G_TYPE_INT, code,
9890 "rtsp-reason", G_TYPE_STRING, gst_rtsp_status_as_text (code),
9892 free_param_data (req);
9894 gst_rtsp_message_unset (&request);
9895 gst_rtsp_message_unset (&response);
9903 GST_DEBUG_OBJECT (src, "failed to open stream");
9908 GST_DEBUG_OBJECT (src, "no control url to send SET_PARAMETER");
9909 res = GST_RTSP_ERROR;
9914 GST_DEBUG_OBJECT (src, "SET_PARAMETER is not supported");
9915 res = GST_RTSP_ERROR;
9918 add_content_hdr_failed:
9920 GST_DEBUG_OBJECT (src, "could not add content header");
9925 GST_DEBUG_OBJECT (src, "could not set body");
9930 gchar *str = gst_rtsp_strresult (res);
9932 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
9933 ("Could not send set-parameter. (%s)", str));
9939 typedef struct _RTSPKeyValue
9941 GstRTSPHeaderField field;
9943 gchar *custom_key; /* custom header string (field is INVALID then) */
9947 key_value_foreach (GArray * array, GFunc func, gpointer user_data)
9951 g_return_if_fail (array != NULL);
9953 for (i = 0; i < array->len; i++) {
9954 (*func) (&g_array_index (array, RTSPKeyValue, i), user_data);
9959 dump_key_value (gpointer data, gpointer user_data G_GNUC_UNUSED)
9961 RTSPKeyValue *key_value = (RTSPKeyValue *) data;
9962 GstRTSPSrc *src = GST_RTSPSRC (user_data);
9963 const gchar *key_string;
9965 if (key_value->custom_key != NULL)
9966 key_string = key_value->custom_key;
9968 key_string = gst_rtsp_header_as_text (key_value->field);
9970 GST_LOG_OBJECT (src, " key: '%s', value: '%s'", key_string,
9975 gst_rtspsrc_print_rtsp_message (GstRTSPSrc * src, const GstRTSPMessage * msg)
9979 GString *body_string = NULL;
9981 g_return_if_fail (src != NULL);
9982 g_return_if_fail (msg != NULL);
9984 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) < GST_LEVEL_LOG)
9987 GST_LOG_OBJECT (src, "--------------------------------------------");
9988 switch (msg->type) {
9989 case GST_RTSP_MESSAGE_REQUEST:
9990 GST_LOG_OBJECT (src, "RTSP request message %p", msg);
9991 GST_LOG_OBJECT (src, " request line:");
9992 GST_LOG_OBJECT (src, " method: '%s'",
9993 gst_rtsp_method_as_text (msg->type_data.request.method));
9994 GST_LOG_OBJECT (src, " uri: '%s'", msg->type_data.request.uri);
9995 GST_LOG_OBJECT (src, " version: '%s'",
9996 gst_rtsp_version_as_text (msg->type_data.request.version));
9997 GST_LOG_OBJECT (src, " headers:");
9998 key_value_foreach (msg->hdr_fields, dump_key_value, src);
9999 GST_LOG_OBJECT (src, " body:");
10000 gst_rtsp_message_get_body (msg, &data, &size);
10002 body_string = g_string_new_len ((const gchar *) data, size);
10003 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
10004 g_string_free (body_string, TRUE);
10005 body_string = NULL;
10008 case GST_RTSP_MESSAGE_RESPONSE:
10009 GST_LOG_OBJECT (src, "RTSP response message %p", msg);
10010 GST_LOG_OBJECT (src, " status line:");
10011 GST_LOG_OBJECT (src, " code: '%d'", msg->type_data.response.code);
10012 GST_LOG_OBJECT (src, " reason: '%s'", msg->type_data.response.reason);
10013 GST_LOG_OBJECT (src, " version: '%s",
10014 gst_rtsp_version_as_text (msg->type_data.response.version));
10015 GST_LOG_OBJECT (src, " headers:");
10016 key_value_foreach (msg->hdr_fields, dump_key_value, src);
10017 gst_rtsp_message_get_body (msg, &data, &size);
10018 GST_LOG_OBJECT (src, " body: length %d", size);
10020 body_string = g_string_new_len ((const gchar *) data, size);
10021 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
10022 g_string_free (body_string, TRUE);
10023 body_string = NULL;
10026 case GST_RTSP_MESSAGE_HTTP_REQUEST:
10027 GST_LOG_OBJECT (src, "HTTP request message %p", msg);
10028 GST_LOG_OBJECT (src, " request line:");
10029 GST_LOG_OBJECT (src, " method: '%s'",
10030 gst_rtsp_method_as_text (msg->type_data.request.method));
10031 GST_LOG_OBJECT (src, " uri: '%s'", msg->type_data.request.uri);
10032 GST_LOG_OBJECT (src, " version: '%s'",
10033 gst_rtsp_version_as_text (msg->type_data.request.version));
10034 GST_LOG_OBJECT (src, " headers:");
10035 key_value_foreach (msg->hdr_fields, dump_key_value, src);
10036 GST_LOG_OBJECT (src, " body:");
10037 gst_rtsp_message_get_body (msg, &data, &size);
10039 body_string = g_string_new_len ((const gchar *) data, size);
10040 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
10041 g_string_free (body_string, TRUE);
10042 body_string = NULL;
10045 case GST_RTSP_MESSAGE_HTTP_RESPONSE:
10046 GST_LOG_OBJECT (src, "HTTP response message %p", msg);
10047 GST_LOG_OBJECT (src, " status line:");
10048 GST_LOG_OBJECT (src, " code: '%d'", msg->type_data.response.code);
10049 GST_LOG_OBJECT (src, " reason: '%s'", msg->type_data.response.reason);
10050 GST_LOG_OBJECT (src, " version: '%s'",
10051 gst_rtsp_version_as_text (msg->type_data.response.version));
10052 GST_LOG_OBJECT (src, " headers:");
10053 key_value_foreach (msg->hdr_fields, dump_key_value, src);
10054 gst_rtsp_message_get_body (msg, &data, &size);
10055 GST_LOG_OBJECT (src, " body: length %d", size);
10057 body_string = g_string_new_len ((const gchar *) data, size);
10058 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
10059 g_string_free (body_string, TRUE);
10060 body_string = NULL;
10063 case GST_RTSP_MESSAGE_DATA:
10064 GST_LOG_OBJECT (src, "RTSP data message %p", msg);
10065 GST_LOG_OBJECT (src, " channel: '%d'", msg->type_data.data.channel);
10066 GST_LOG_OBJECT (src, " size: '%d'", msg->body_size);
10067 gst_rtsp_message_get_body (msg, &data, &size);
10069 body_string = g_string_new_len ((const gchar *) data, size);
10070 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
10071 g_string_free (body_string, TRUE);
10072 body_string = NULL;
10076 GST_LOG_OBJECT (src, "unsupported message type %d", msg->type);
10079 GST_LOG_OBJECT (src, "--------------------------------------------");
10083 gst_rtspsrc_print_sdp_media (GstRTSPSrc * src, GstSDPMedia * media)
10085 GST_LOG_OBJECT (src, " media: '%s'", GST_STR_NULL (media->media));
10086 GST_LOG_OBJECT (src, " port: '%u'", media->port);
10087 GST_LOG_OBJECT (src, " num_ports: '%u'", media->num_ports);
10088 GST_LOG_OBJECT (src, " proto: '%s'", GST_STR_NULL (media->proto));
10089 if (media->fmts && media->fmts->len > 0) {
10092 GST_LOG_OBJECT (src, " formats:");
10093 for (i = 0; i < media->fmts->len; i++) {
10094 GST_LOG_OBJECT (src, " format '%s'", g_array_index (media->fmts,
10098 GST_LOG_OBJECT (src, " information: '%s'",
10099 GST_STR_NULL (media->information));
10100 if (media->connections && media->connections->len > 0) {
10103 GST_LOG_OBJECT (src, " connections:");
10104 for (i = 0; i < media->connections->len; i++) {
10105 GstSDPConnection *conn =
10106 &g_array_index (media->connections, GstSDPConnection, i);
10108 GST_LOG_OBJECT (src, " nettype: '%s'",
10109 GST_STR_NULL (conn->nettype));
10110 GST_LOG_OBJECT (src, " addrtype: '%s'",
10111 GST_STR_NULL (conn->addrtype));
10112 GST_LOG_OBJECT (src, " address: '%s'",
10113 GST_STR_NULL (conn->address));
10114 GST_LOG_OBJECT (src, " ttl: '%u'", conn->ttl);
10115 GST_LOG_OBJECT (src, " addr_number: '%u'", conn->addr_number);
10118 if (media->bandwidths && media->bandwidths->len > 0) {
10121 GST_LOG_OBJECT (src, " bandwidths:");
10122 for (i = 0; i < media->bandwidths->len; i++) {
10123 GstSDPBandwidth *bw =
10124 &g_array_index (media->bandwidths, GstSDPBandwidth, i);
10126 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (bw->bwtype));
10127 GST_LOG_OBJECT (src, " bandwidth: '%u'", bw->bandwidth);
10130 GST_LOG_OBJECT (src, " key:");
10131 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (media->key.type));
10132 GST_LOG_OBJECT (src, " data: '%s'", GST_STR_NULL (media->key.data));
10133 if (media->attributes && media->attributes->len > 0) {
10136 GST_LOG_OBJECT (src, " attributes:");
10137 for (i = 0; i < media->attributes->len; i++) {
10138 GstSDPAttribute *attr =
10139 &g_array_index (media->attributes, GstSDPAttribute, i);
10141 GST_LOG_OBJECT (src, " attribute '%s' : '%s'", attr->key, attr->value);
10147 gst_rtspsrc_print_sdp_message (GstRTSPSrc * src, const GstSDPMessage * msg)
10149 g_return_if_fail (src != NULL);
10150 g_return_if_fail (msg != NULL);
10152 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) < GST_LEVEL_LOG)
10155 GST_LOG_OBJECT (src, "--------------------------------------------");
10156 GST_LOG_OBJECT (src, "sdp packet %p:", msg);
10157 GST_LOG_OBJECT (src, " version: '%s'", GST_STR_NULL (msg->version));
10158 GST_LOG_OBJECT (src, " origin:");
10159 GST_LOG_OBJECT (src, " username: '%s'",
10160 GST_STR_NULL (msg->origin.username));
10161 GST_LOG_OBJECT (src, " sess_id: '%s'",
10162 GST_STR_NULL (msg->origin.sess_id));
10163 GST_LOG_OBJECT (src, " sess_version: '%s'",
10164 GST_STR_NULL (msg->origin.sess_version));
10165 GST_LOG_OBJECT (src, " nettype: '%s'",
10166 GST_STR_NULL (msg->origin.nettype));
10167 GST_LOG_OBJECT (src, " addrtype: '%s'",
10168 GST_STR_NULL (msg->origin.addrtype));
10169 GST_LOG_OBJECT (src, " addr: '%s'", GST_STR_NULL (msg->origin.addr));
10170 GST_LOG_OBJECT (src, " session_name: '%s'",
10171 GST_STR_NULL (msg->session_name));
10172 GST_LOG_OBJECT (src, " information: '%s'", GST_STR_NULL (msg->information));
10173 GST_LOG_OBJECT (src, " uri: '%s'", GST_STR_NULL (msg->uri));
10175 if (msg->emails && msg->emails->len > 0) {
10178 GST_LOG_OBJECT (src, " emails:");
10179 for (i = 0; i < msg->emails->len; i++) {
10180 GST_LOG_OBJECT (src, " email '%s'", g_array_index (msg->emails, gchar *,
10184 if (msg->phones && msg->phones->len > 0) {
10187 GST_LOG_OBJECT (src, " phones:");
10188 for (i = 0; i < msg->phones->len; i++) {
10189 GST_LOG_OBJECT (src, " phone '%s'", g_array_index (msg->phones, gchar *,
10193 GST_LOG_OBJECT (src, " connection:");
10194 GST_LOG_OBJECT (src, " nettype: '%s'",
10195 GST_STR_NULL (msg->connection.nettype));
10196 GST_LOG_OBJECT (src, " addrtype: '%s'",
10197 GST_STR_NULL (msg->connection.addrtype));
10198 GST_LOG_OBJECT (src, " address: '%s'",
10199 GST_STR_NULL (msg->connection.address));
10200 GST_LOG_OBJECT (src, " ttl: '%u'", msg->connection.ttl);
10201 GST_LOG_OBJECT (src, " addr_number: '%u'", msg->connection.addr_number);
10202 if (msg->bandwidths && msg->bandwidths->len > 0) {
10205 GST_LOG_OBJECT (src, " bandwidths:");
10206 for (i = 0; i < msg->bandwidths->len; i++) {
10207 GstSDPBandwidth *bw =
10208 &g_array_index (msg->bandwidths, GstSDPBandwidth, i);
10210 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (bw->bwtype));
10211 GST_LOG_OBJECT (src, " bandwidth: '%u'", bw->bandwidth);
10214 GST_LOG_OBJECT (src, " key:");
10215 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (msg->key.type));
10216 GST_LOG_OBJECT (src, " data: '%s'", GST_STR_NULL (msg->key.data));
10217 if (msg->attributes && msg->attributes->len > 0) {
10220 GST_LOG_OBJECT (src, " attributes:");
10221 for (i = 0; i < msg->attributes->len; i++) {
10222 GstSDPAttribute *attr =
10223 &g_array_index (msg->attributes, GstSDPAttribute, i);
10225 GST_LOG_OBJECT (src, " attribute '%s' : '%s'", attr->key, attr->value);
10228 if (msg->medias && msg->medias->len > 0) {
10231 GST_LOG_OBJECT (src, " medias:");
10232 for (i = 0; i < msg->medias->len; i++) {
10233 GST_LOG_OBJECT (src, " media %u:", i);
10234 gst_rtspsrc_print_sdp_media (src, &g_array_index (msg->medias,
10238 GST_LOG_OBJECT (src, "--------------------------------------------");