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,
2790 event = gst_event_new_flush_start ();
2791 gst_event_set_seqnum (event, seqnum);
2792 GST_DEBUG_OBJECT (src, "start flush");
2794 state = GST_STATE_PAUSED;
2796 event = gst_event_new_flush_stop (TRUE);
2797 gst_event_set_seqnum (event, seqnum);
2798 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2801 state = GST_STATE_PLAYING;
2803 state = GST_STATE_PAUSED;
2805 gst_rtspsrc_push_event (src, event);
2806 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2807 gst_rtspsrc_set_state (src, state);
2810 static GstRTSPResult
2811 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
2812 GstRTSPMessage * message, gint64 timeout)
2816 if (conninfo->connection) {
2817 g_mutex_lock (&conninfo->send_lock);
2819 gst_rtsp_connection_send_usec (conninfo->connection, message, timeout);
2820 g_mutex_unlock (&conninfo->send_lock);
2822 ret = GST_RTSP_ERROR;
2828 static GstRTSPResult
2829 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
2830 GstRTSPMessage * message, gint64 timeout)
2834 if (conninfo->connection) {
2835 g_mutex_lock (&conninfo->recv_lock);
2836 ret = gst_rtsp_connection_receive_usec (conninfo->connection, message,
2838 g_mutex_unlock (&conninfo->recv_lock);
2840 ret = GST_RTSP_ERROR;
2847 gst_rtspsrc_get_position (GstRTSPSrc * src)
2852 query = gst_query_new_position (GST_FORMAT_TIME);
2853 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2854 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2855 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2859 if (stream->srcpad) {
2860 if (gst_pad_query (stream->srcpad, query)) {
2861 gst_query_parse_position (query, &fmt, &pos);
2862 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2863 GST_TIME_ARGS (pos));
2864 src->last_pos = pos;
2874 gst_query_unref (query);
2878 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2883 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type = GST_SEEK_TYPE_NONE;
2885 gboolean flush, server_side_trickmode;
2888 GstSegment seeksegment = { 0, };
2890 const gchar *seek_style = NULL;
2891 gboolean rate_change_only = FALSE;
2892 gboolean rate_change_same_direction = FALSE;
2894 GST_DEBUG_OBJECT (src, "doing seek with event %" GST_PTR_FORMAT, event);
2896 gst_event_parse_seek (event, &rate, &format, &flags,
2897 &cur_type, &cur, &stop_type, &stop);
2898 rate_change_only = cur_type == GST_SEEK_TYPE_NONE
2899 && stop_type == GST_SEEK_TYPE_NONE;
2901 /* we need TIME format */
2902 if (format != src->segment.format)
2905 /* Check if we are not at all seekable */
2906 if (src->seekable == -1.0)
2909 /* Additional seeking-to-beginning-only check */
2910 if (src->seekable == 0.0 && cur != 0)
2913 if (flags & GST_SEEK_FLAG_SEGMENT)
2914 goto invalid_segment_flag;
2916 /* get flush flag */
2917 flush = flags & GST_SEEK_FLAG_FLUSH;
2918 server_side_trickmode = flags & GST_SEEK_FLAG_TRICKMODE;
2920 gst_event_parse_seek_trickmode_interval (event, &src->trickmode_interval);
2922 /* now we need to make sure the streaming thread is stopped. We do this by
2923 * either sending a FLUSH_START event downstream which will cause the
2924 * streaming thread to stop with a WRONG_STATE.
2925 * For a non-flushing seek we simply pause the task, which will happen as soon
2926 * as it completes one iteration (and thus might block when the sink is
2927 * blocking in preroll). */
2929 GST_DEBUG_OBJECT (src, "starting flush");
2930 gst_rtspsrc_flush (src, TRUE, FALSE, gst_event_get_seqnum (event));
2933 gst_task_pause (src->task);
2937 /* we should now be able to grab the streaming thread because we stopped it
2938 * with the above flush/pause code */
2939 GST_RTSP_STREAM_LOCK (src);
2941 GST_DEBUG_OBJECT (src, "stopped streaming");
2943 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2944 gst_rtspsrc_connection_flush (src, FALSE);
2946 /* copy segment, we need this because we still need the old
2947 * segment when we close the current segment. */
2948 seeksegment = src->segment;
2950 /* configure the seek parameters in the seeksegment. We will then have the
2951 * right values in the segment to perform the seek */
2952 GST_DEBUG_OBJECT (src, "configuring seek");
2953 rate_change_same_direction = (rate * seeksegment.rate) > 0;
2954 gst_segment_do_seek (&seeksegment, rate, format, flags,
2955 cur_type, cur, stop_type, stop, &update);
2957 /* if we were playing, pause first */
2958 playing = (src->state == GST_RTSP_STATE_PLAYING);
2960 /* obtain current position in case seek fails */
2961 gst_rtspsrc_get_position (src);
2962 gst_rtspsrc_pause (src, FALSE);
2964 src->server_side_trickmode = server_side_trickmode;
2966 src->state = GST_RTSP_STATE_SEEKING;
2968 /* PLAY will add the range header now. */
2969 src->need_range = TRUE;
2971 /* If an accurate seek was requested, we want to clip the segment we
2972 * output in ONVIF mode to the requested bounds */
2973 src->clip_out_segment = ! !(flags & GST_SEEK_FLAG_ACCURATE);
2974 src->seek_seqnum = gst_event_get_seqnum (event);
2976 /* prepare for streaming again */
2978 /* if we started flush, we stop now */
2979 GST_DEBUG_OBJECT (src, "stopping flush");
2980 gst_rtspsrc_flush (src, FALSE, playing, gst_event_get_seqnum (event));
2983 /* now we did the seek and can activate the new segment values */
2984 src->segment = seeksegment;
2986 /* if we're doing a segment seek, post a SEGMENT_START message */
2987 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2988 gst_element_post_message (GST_ELEMENT_CAST (src),
2989 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2990 src->segment.format, src->segment.position));
2993 /* mark discont when needed */
2994 if (!(rate_change_only && rate_change_same_direction)) {
2995 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2996 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2997 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2998 stream->discont = TRUE;
3002 /* and continue playing if needed. If we are not acting as a live source,
3003 * then only the RTSP PLAYING state, set earlier, matters. */
3004 GST_OBJECT_LOCK (src);
3006 playing = (GST_STATE_PENDING (src) == GST_STATE_VOID_PENDING
3007 && GST_STATE (src) == GST_STATE_PLAYING)
3008 || (GST_STATE_PENDING (src) == GST_STATE_PLAYING);
3010 GST_OBJECT_UNLOCK (src);
3012 if (src->version >= GST_RTSP_VERSION_2_0) {
3013 if (flags & GST_SEEK_FLAG_ACCURATE)
3015 else if (flags & GST_SEEK_FLAG_KEY_UNIT)
3016 seek_style = "CoRAP";
3017 else if (flags & GST_SEEK_FLAG_KEY_UNIT
3018 && flags & GST_SEEK_FLAG_SNAP_BEFORE)
3019 seek_style = "First-Prior";
3020 else if (flags & GST_SEEK_FLAG_KEY_UNIT && flags & GST_SEEK_FLAG_SNAP_AFTER)
3021 seek_style = "Next";
3025 gst_rtspsrc_play (src, &seeksegment, FALSE, seek_style);
3027 GST_RTSP_STREAM_UNLOCK (src);
3034 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
3039 GST_DEBUG_OBJECT (src, "stream is not seekable");
3042 invalid_segment_flag:
3044 GST_WARNING_OBJECT (src, "Segment seeks not supported");
3050 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
3054 gboolean res = TRUE;
3057 src = GST_RTSPSRC_CAST (parent);
3059 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
3060 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
3062 switch (GST_EVENT_TYPE (event)) {
3063 case GST_EVENT_SEEK:
3065 guint32 seqnum = gst_event_get_seqnum (event);
3066 if (seqnum == src->seek_seqnum) {
3067 GST_LOG_OBJECT (pad, "Drop duplicated SEEK event seqnum %"
3068 G_GUINT32_FORMAT, seqnum);
3070 res = gst_rtspsrc_perform_seek (src, event);
3076 case GST_EVENT_NAVIGATION:
3077 case GST_EVENT_LATENCY:
3085 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
3086 res = gst_pad_send_event (target, event);
3087 gst_object_unref (target);
3089 gst_event_unref (event);
3092 gst_event_unref (event);
3099 gst_rtspsrc_stream_start_event_add_group_id (GstRTSPSrc * src, GstEvent * event)
3101 g_mutex_lock (&src->group_lock);
3103 if (src->group_id == GST_GROUP_ID_INVALID)
3104 src->group_id = gst_util_group_id_next ();
3106 g_mutex_unlock (&src->group_lock);
3108 gst_event_set_group_id (event, src->group_id);
3112 gst_rtspsrc_update_src_event (GstRTSPSrc * self, GstRTSPStream * stream,
3115 switch (GST_EVENT_TYPE (event)) {
3116 case GST_EVENT_STREAM_START:{
3121 cs = g_checksum_new (G_CHECKSUM_SHA256);
3122 uri = self->conninfo.location;
3123 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
3126 g_strdup_printf ("%s/%s", g_checksum_get_string (cs),
3129 g_checksum_free (cs);
3130 gst_event_unref (event);
3131 event = gst_event_new_stream_start (stream_id);
3132 gst_rtspsrc_stream_start_event_add_group_id (self, event);
3136 case GST_EVENT_SEGMENT:
3137 if (self->seek_seqnum != GST_SEQNUM_INVALID)
3138 GST_EVENT_SEQNUM (event) = self->seek_seqnum;
3148 gst_rtspsrc_handle_src_sink_event (GstPad * pad, GstObject * parent,
3151 GstRTSPStream *stream;
3152 GstRTSPSrc *self = GST_RTSPSRC (GST_OBJECT_PARENT (parent));
3154 stream = gst_pad_get_element_private (pad);
3156 event = gst_rtspsrc_update_src_event (self, stream, event);
3158 return gst_pad_push_event (stream->srcpad, event);
3161 /* this is the final event function we receive on the internal source pad when
3162 * we deal with TCP connections */
3164 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
3169 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
3171 switch (GST_EVENT_TYPE (event)) {
3172 case GST_EVENT_SEEK:
3174 case GST_EVENT_NAVIGATION:
3175 case GST_EVENT_LATENCY:
3177 gst_event_unref (event);
3184 /* this is the final query function we receive on the internal source pad when
3185 * we deal with TCP connections */
3187 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
3191 gboolean res = FALSE;
3193 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
3195 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
3196 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3198 switch (GST_QUERY_TYPE (query)) {
3199 case GST_QUERY_POSITION:
3204 case GST_QUERY_DURATION:
3208 gst_query_parse_duration (query, &format, NULL);
3211 case GST_FORMAT_TIME:
3212 gst_query_set_duration (query, format, src->segment.duration);
3220 case GST_QUERY_LATENCY:
3222 /* we are live with a min latency of 0 and unlimited max latency, this
3223 * result will be updated by the session manager if there is any. */
3224 gst_query_set_latency (query, src->is_live, 0, -1);
3235 /* this query is executed on the ghost source pad exposed on rtspsrc. */
3237 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
3241 gboolean res = FALSE;
3243 src = GST_RTSPSRC_CAST (parent);
3245 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
3246 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3248 switch (GST_QUERY_TYPE (query)) {
3249 case GST_QUERY_DURATION:
3253 gst_query_parse_duration (query, &format, NULL);
3256 case GST_FORMAT_TIME:
3257 gst_query_set_duration (query, format, src->segment.duration);
3265 case GST_QUERY_SEEKING:
3269 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
3270 if (format == GST_FORMAT_TIME) {
3271 gboolean seekable = TRUE;
3272 GstClockTime start = 0, duration = src->segment.duration;
3274 /* seeking without duration is unlikely */
3275 seekable = seekable && src->seekable >= 0.0 && src->segment.duration &&
3276 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
3279 if (src->seekable > 0.0) {
3280 start = src->last_pos - src->seekable * GST_SECOND;
3282 /* src->seekable == 0 means that we can only seek to 0 */
3288 GST_LOG_OBJECT (src, "seekable: %d, duration: %" GST_TIME_FORMAT
3289 ", src->seekable: %f", seekable,
3290 GST_TIME_ARGS (src->segment.duration), src->seekable);
3292 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, start,
3302 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
3304 gst_query_set_uri (query, uri);
3312 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
3314 /* forward the query to the proxy target pad */
3316 res = gst_pad_query (target, query);
3317 gst_object_unref (target);
3326 /* callback for RTCP messages to be sent to the server when operating in TCP
3328 static GstFlowReturn
3329 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
3332 GstRTSPStream *stream;
3333 GstFlowReturn res = GST_FLOW_OK;
3335 GstRTSPMessage message = { 0 };
3336 GstRTSPConnInfo *conninfo;
3338 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
3339 src = stream->parent;
3341 gst_rtsp_message_init_data (&message, stream->channel[1]);
3343 /* lend the body data to the message */
3344 gst_rtsp_message_set_body_buffer (&message, buffer);
3346 if (stream->conninfo.connection)
3347 conninfo = &stream->conninfo;
3349 conninfo = &src->conninfo;
3351 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP",
3352 (guint) gst_buffer_get_size (buffer));
3353 ret = gst_rtspsrc_connection_send (src, conninfo, &message, 0);
3354 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
3356 gst_rtsp_message_unset (&message);
3358 gst_buffer_unref (buffer);
3363 static GstFlowReturn
3364 gst_rtspsrc_push_backchannel_buffer (GstRTSPSrc * src, guint id,
3369 res = gst_rtspsrc_push_backchannel_sample (src, id, sample);
3371 gst_sample_unref (sample);
3376 static GstFlowReturn
3377 gst_rtspsrc_push_backchannel_sample (GstRTSPSrc * src, guint id,
3380 GstFlowReturn res = GST_FLOW_OK;
3381 GstRTSPStream *stream;
3383 if (!src->conninfo.connected || src->state != GST_RTSP_STATE_PLAYING)
3386 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
3387 if (stream == NULL) {
3388 GST_ERROR_OBJECT (src, "no stream with id %u", id);
3392 if (src->interleaved) {
3395 GstRTSPMessage message = { 0 };
3396 GstRTSPConnInfo *conninfo;
3398 buffer = gst_sample_get_buffer (sample);
3400 gst_rtsp_message_init_data (&message, stream->channel[0]);
3402 /* lend the body data to the message */
3403 gst_rtsp_message_set_body_buffer (&message, buffer);
3405 if (stream->conninfo.connection)
3406 conninfo = &stream->conninfo;
3408 conninfo = &src->conninfo;
3410 GST_DEBUG_OBJECT (src, "sending %u bytes backchannel RTP",
3411 (guint) gst_buffer_get_size (buffer));
3412 ret = gst_rtspsrc_connection_send (src, conninfo, &message, 0);
3413 GST_DEBUG_OBJECT (src, "sent backchannel RTP, %d", ret);
3415 gst_rtsp_message_unset (&message);
3419 g_signal_emit_by_name (stream->rtpsrc, "push-sample", sample, &res);
3420 GST_DEBUG_OBJECT (src, "sent backchannel RTP sample %p: %s", sample,
3421 gst_flow_get_name (res));
3428 static GstPadProbeReturn
3429 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3431 GstRTSPSrc *src = user_data;
3433 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
3434 GST_DEBUG_PAD_NAME (pad));
3436 /* activate the streams */
3437 GST_OBJECT_LOCK (src);
3438 if (!src->need_activate)
3441 src->need_activate = FALSE;
3442 GST_OBJECT_UNLOCK (src);
3444 gst_rtspsrc_activate_streams (src);
3446 return GST_PAD_PROBE_OK;
3450 GST_OBJECT_UNLOCK (src);
3451 return GST_PAD_PROBE_OK;
3455 static GstPadProbeReturn
3456 udpsrc_probe_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3458 guint32 *segment_seqnum = user_data;
3460 switch (GST_EVENT_TYPE (info->data)) {
3461 case GST_EVENT_SEGMENT:
3462 *segment_seqnum = gst_event_get_seqnum (info->data);
3468 return GST_PAD_PROBE_OK;
3474 GstRTSPStream *stream;
3475 } CopyStickyEventsData;
3478 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
3480 CopyStickyEventsData *data = user_data;
3481 GstEvent *new_event;
3483 GST_DEBUG_OBJECT (data->stream->srcpad, "send sticky event %" GST_PTR_FORMAT,
3486 gst_rtspsrc_update_src_event (data->src, data->stream,
3487 gst_event_ref (*event));
3488 gst_pad_store_sticky_event (data->stream->srcpad, new_event);
3494 add_backchannel_fakesink (GstRTSPSrc * src, GstRTSPStream * stream,
3498 GstElement *fakesink;
3500 fakesink = gst_element_factory_make ("fakesink", NULL);
3501 if (fakesink == NULL) {
3502 GST_ERROR_OBJECT (src, "no fakesink");
3506 sinkpad = gst_element_get_static_pad (fakesink, "sink");
3508 GST_DEBUG_OBJECT (src, "backchannel stream %p, hooking fakesink", stream);
3510 gst_bin_add (GST_BIN_CAST (src), fakesink);
3511 if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK) {
3512 GST_WARNING_OBJECT (src, "could not link to fakesink");
3516 gst_object_unref (sinkpad);
3518 gst_element_sync_state_with_parent (fakesink);
3522 /* this callback is called when the session manager generated a new src pad with
3523 * payloaded RTP packets. We simply ghost the pad here. */
3525 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
3528 GstPadTemplate *template;
3531 GstRTSPStream *stream;
3533 GstPad *internal_src;
3534 CopyStickyEventsData copy_sticky_events_data;
3536 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
3538 GST_RTSP_STATE_LOCK (src);
3540 name = gst_object_get_name (GST_OBJECT_CAST (pad));
3541 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
3542 goto unknown_stream;
3544 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
3546 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
3548 goto unknown_stream;
3551 stream->ssrc = ssrc;
3553 /* we'll add it later see below */
3554 stream->added = TRUE;
3556 /* check if we added all streams */
3558 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
3559 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
3561 GST_DEBUG_OBJECT (src, "stream %p, container %d, added %d, setup %d",
3562 ostream, ostream->container, ostream->added, ostream->setup);
3564 /* if we find a stream for which we did a setup that is not added, we
3565 * need to wait some more */
3566 if (ostream->setup && !ostream->added) {
3571 GST_RTSP_STATE_UNLOCK (src);
3573 /* create a new pad we will use to stream to */
3574 template = gst_static_pad_template_get (&rtptemplate);
3575 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
3576 gst_object_unref (template);
3579 /* We intercept and modify the stream start event */
3581 GST_PAD (gst_proxy_pad_get_internal (GST_PROXY_PAD (stream->srcpad)));
3582 gst_pad_set_element_private (internal_src, stream);
3583 gst_pad_set_event_function (internal_src, gst_rtspsrc_handle_src_sink_event);
3585 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3586 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3587 gst_pad_set_active (stream->srcpad, TRUE);
3589 copy_sticky_events_data.src = src;
3590 copy_sticky_events_data.stream = stream;
3591 gst_pad_sticky_events_foreach (pad, copy_sticky_events,
3592 ©_sticky_events_data);
3594 gst_object_unref (internal_src);
3596 /* don't add the srcpad if this is a sendonly stream */
3597 if (stream->is_backchannel)
3598 add_backchannel_fakesink (src, stream, stream->srcpad);
3600 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3603 GST_DEBUG_OBJECT (src, "We added all streams");
3604 /* when we get here, all stream are added and we can fire the no-more-pads
3606 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
3614 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
3615 GST_RTSP_STATE_UNLOCK (src);
3622 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
3626 len = stream->ptmap->len;
3627 for (i = 0; i < len; i++) {
3628 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3636 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
3638 GstRTSPStream *stream;
3641 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
3643 GST_RTSP_STATE_LOCK (src);
3644 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
3646 goto unknown_stream;
3648 if ((caps = stream_get_caps_for_pt (stream, pt)))
3649 gst_caps_ref (caps);
3650 GST_RTSP_STATE_UNLOCK (src);
3656 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
3657 GST_RTSP_STATE_UNLOCK (src);
3663 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
3665 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
3667 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
3671 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
3673 GstRTSPSrc *src = stream->parent;
3676 g_object_get (source, "ssrc", &ssrc, NULL);
3678 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
3679 ssrc, stream->ssrc, stream->id);
3681 if (ssrc == stream->ssrc)
3682 gst_rtspsrc_do_stream_eos (src, stream);
3686 on_timeout_common (GObject * session, GObject * source, GstRTSPStream * stream)
3688 GstRTSPSrc *src = stream->parent;
3691 g_object_get (source, "ssrc", &ssrc, NULL);
3693 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
3694 ssrc, stream->ssrc, stream->id);
3696 if (ssrc == stream->ssrc) {
3698 gboolean all_eos = TRUE;
3700 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
3703 /* Only EOS all streams at once if they're all EOS. Otherwise it is
3704 * possible for timed out streams to reappear at a later time time: they
3705 * might just be inactive currently.
3708 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3709 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3711 /* Skip streams that were not set up at all */
3722 GST_DEBUG_OBJECT (src, "sending EOS on all streams");
3723 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3724 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3725 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
3732 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
3734 GstRTSPSrc *src = stream->parent;
3736 /* timeout, post element message */
3737 gst_element_post_message (GST_ELEMENT_CAST (src),
3738 gst_message_new_element (GST_OBJECT_CAST (src),
3739 gst_structure_new ("GstRTSPSrcTimeout", "cause",
3740 GST_TYPE_RTSP_SRC_TIMEOUT_CAUSE, GST_RTSP_SRC_TIMEOUT_CAUSE_RTCP,
3741 "stream-number", G_TYPE_INT, stream->id, "ssrc", G_TYPE_UINT,
3742 stream->ssrc, NULL)));
3744 /* In non-live mode, timeouts can occur if we are PAUSED, this doesn't mean
3745 * the stream is EOS, it may simply be blocked */
3746 if (src->is_live || !src->interleaved)
3747 on_timeout_common (session, source, stream);
3751 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
3753 GstRTSPStream *stream;
3755 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
3757 /* get stream for session */
3758 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
3760 gst_rtspsrc_do_stream_eos (src, stream);
3765 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
3767 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
3770 stream->eos = FALSE;
3774 set_manager_buffer_mode (GstRTSPSrc * src)
3776 GObjectClass *klass;
3778 if (src->manager == NULL)
3781 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3783 if (!g_object_class_find_property (klass, "buffer-mode"))
3786 if (src->buffer_mode != BUFFER_MODE_AUTO) {
3787 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
3792 GST_DEBUG_OBJECT (src,
3793 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
3795 if (src->provided_clock) {
3796 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
3798 if (clock == src->provided_clock) {
3799 GST_DEBUG_OBJECT (src, "selected synced");
3800 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
3803 gst_object_unref (clock);
3808 /* Otherwise fall-through and use another buffer mode */
3810 gst_object_unref (clock);
3813 GST_DEBUG_OBJECT (src, "auto buffering mode");
3814 if (src->use_buffering) {
3815 GST_DEBUG_OBJECT (src, "selected buffer");
3816 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
3818 GST_DEBUG_OBJECT (src, "selected slave");
3819 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
3824 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
3828 GstMIKEYMessage *msg = stream->mikey;
3830 GST_DEBUG ("request key SSRC %u", ssrc);
3832 caps = gst_caps_ref (stream_get_caps_for_pt (stream, stream->default_pt));
3833 caps = gst_caps_make_writable (caps);
3835 /* parse crypto sessions and look for the SSRC rollover counter */
3836 msg = stream->mikey;
3837 for (i = 0; msg && i < gst_mikey_message_get_n_cs (msg); i++) {
3838 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
3840 if (ssrc == map->ssrc) {
3841 gst_caps_set_simple (caps, "roc", G_TYPE_UINT, map->roc, NULL);
3850 request_rtp_decoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
3852 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3853 if (stream->id != session)
3856 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3857 stream->profile != GST_RTSP_PROFILE_SAVPF)
3860 if (stream->srtpdec == NULL) {
3863 name = g_strdup_printf ("srtpdec_%u", session);
3864 stream->srtpdec = gst_element_factory_make ("srtpdec", name);
3867 if (stream->srtpdec == NULL) {
3868 GST_ELEMENT_ERROR (stream->parent, CORE, MISSING_PLUGIN, (NULL),
3869 ("no srtpdec element present!"));
3872 g_signal_connect (stream->srtpdec, "request-key",
3873 (GCallback) request_key, stream);
3875 return gst_object_ref (stream->srtpdec);
3879 request_rtcp_encoder (GstElement * rtpbin, guint session,
3880 GstRTSPStream * stream)
3885 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3886 if (stream->id != session)
3889 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3890 stream->profile != GST_RTSP_PROFILE_SAVPF)
3893 if (stream->srtpenc == NULL) {
3896 name = g_strdup_printf ("srtpenc_%u", session);
3897 stream->srtpenc = gst_element_factory_make ("srtpenc", name);
3900 if (stream->srtpenc == NULL) {
3901 GST_ELEMENT_ERROR (stream->parent, CORE, MISSING_PLUGIN, (NULL),
3902 ("no srtpenc element present!"));
3906 /* get RTCP crypto parameters from caps */
3907 s = gst_caps_get_structure (stream->srtcpparams, 0);
3911 GType ciphertype, authtype;
3912 GValue rtcp_cipher = G_VALUE_INIT, rtcp_auth = G_VALUE_INIT;
3914 ciphertype = g_type_from_name ("GstSrtpCipherType");
3915 authtype = g_type_from_name ("GstSrtpAuthType");
3916 g_value_init (&rtcp_cipher, ciphertype);
3917 g_value_init (&rtcp_auth, authtype);
3919 str = gst_structure_get_string (s, "srtcp-cipher");
3920 gst_value_deserialize (&rtcp_cipher, str);
3921 str = gst_structure_get_string (s, "srtcp-auth");
3922 gst_value_deserialize (&rtcp_auth, str);
3923 gst_structure_get (s, "srtp-key", GST_TYPE_BUFFER, &buf, NULL);
3925 g_object_set_property (G_OBJECT (stream->srtpenc), "rtp-cipher",
3927 g_object_set_property (G_OBJECT (stream->srtpenc), "rtp-auth",
3929 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-cipher",
3931 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-auth",
3933 g_object_set (stream->srtpenc, "key", buf, NULL);
3935 g_value_unset (&rtcp_cipher);
3936 g_value_unset (&rtcp_auth);
3937 gst_buffer_unref (buf);
3940 name = g_strdup_printf ("rtcp_sink_%d", session);
3941 pad = gst_element_request_pad_simple (stream->srtpenc, name);
3943 gst_object_unref (pad);
3945 return gst_object_ref (stream->srtpenc);
3949 request_aux_receiver (GstElement * rtpbin, guint sessid, GstRTSPSrc * src)
3951 GstElement *rtx, *bin;
3954 GstRTSPStream *stream;
3956 stream = find_stream (src, &sessid, (gpointer) find_stream_by_id);
3958 GST_WARNING_OBJECT (src, "Stream %u not found", sessid);
3962 GST_INFO_OBJECT (src, "creating retransmision receiver for session %u "
3963 "with map %" GST_PTR_FORMAT, sessid, stream->rtx_pt_map);
3964 bin = gst_bin_new (NULL);
3965 rtx = gst_element_factory_make ("rtprtxreceive", NULL);
3966 g_object_set (rtx, "payload-type-map", stream->rtx_pt_map, NULL);
3967 gst_bin_add (GST_BIN (bin), rtx);
3969 pad = gst_element_get_static_pad (rtx, "src");
3970 name = g_strdup_printf ("src_%u", sessid);
3971 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3973 gst_object_unref (pad);
3975 pad = gst_element_get_static_pad (rtx, "sink");
3976 name = g_strdup_printf ("sink_%u", sessid);
3977 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3979 gst_object_unref (pad);
3985 add_retransmission (GstRTSPSrc * src, GstRTSPTransport * transport)
3989 gboolean do_retransmission = FALSE;
3991 if (transport->trans != GST_RTSP_TRANS_RTP)
3993 if (transport->profile != GST_RTSP_PROFILE_AVPF &&
3994 transport->profile != GST_RTSP_PROFILE_SAVPF)
3997 signal_id = g_signal_lookup ("request-aux-receiver",
3998 G_OBJECT_TYPE (src->manager));
3999 /* there's already something connected */
4000 if (g_signal_handler_find (src->manager, G_SIGNAL_MATCH_ID, signal_id, 0,
4001 NULL, NULL, NULL) != 0) {
4002 GST_DEBUG_OBJECT (src, "Not adding RTX AUX element as "
4003 "\"request-aux-receiver\" signal is "
4004 "already used by the application");
4008 /* build the retransmission payload type map */
4009 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4010 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4011 gboolean do_retransmission_stream = FALSE;
4014 if (stream->rtx_pt_map)
4015 gst_structure_free (stream->rtx_pt_map);
4016 stream->rtx_pt_map = gst_structure_new_empty ("application/x-rtp-pt-map");
4018 for (i = 0; i < stream->ptmap->len; i++) {
4019 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
4020 GstStructure *s = gst_caps_get_structure (item->caps, 0);
4021 const gchar *encoding;
4023 /* we only care about RTX streams */
4024 if ((encoding = gst_structure_get_string (s, "encoding-name"))
4025 && g_strcmp0 (encoding, "RTX") == 0) {
4026 const gchar *stream_pt_s;
4029 if (gst_structure_get_int (s, "payload", &rtx_pt)
4030 && (stream_pt_s = gst_structure_get_string (s, "apt"))) {
4033 gst_structure_set (stream->rtx_pt_map, stream_pt_s, G_TYPE_UINT,
4035 do_retransmission_stream = TRUE;
4041 if (do_retransmission_stream) {
4042 GST_DEBUG_OBJECT (src, "built retransmission payload map for stream "
4043 "id %i: %" GST_PTR_FORMAT, stream->id, stream->rtx_pt_map);
4044 do_retransmission = TRUE;
4046 GST_DEBUG_OBJECT (src, "no retransmission payload map for stream "
4047 "id %i", stream->id);
4048 gst_structure_free (stream->rtx_pt_map);
4049 stream->rtx_pt_map = NULL;
4053 if (do_retransmission) {
4054 GST_DEBUG_OBJECT (src, "Enabling retransmissions");
4056 g_object_set (src->manager, "do-retransmission", TRUE, NULL);
4058 /* enable RFC4588 retransmission handling by setting rtprtxreceive
4059 * as the "aux" element of rtpbin */
4060 g_signal_connect (src->manager, "request-aux-receiver",
4061 (GCallback) request_aux_receiver, src);
4063 GST_DEBUG_OBJECT (src,
4064 "Not enabling retransmissions as no stream had a retransmission payload map");
4068 /* try to get and configure a manager */
4070 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
4071 GstRTSPTransport * transport)
4073 const gchar *manager;
4075 GstStateChangeReturn ret;
4078 goto use_no_manager;
4080 /* find a manager */
4081 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
4085 GST_DEBUG_OBJECT (src, "using manager %s", manager);
4087 /* configure the manager */
4088 if (src->manager == NULL) {
4089 GObjectClass *klass;
4091 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
4093 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
4097 goto use_no_manager;
4099 if (!(src->manager = gst_element_factory_make (manager, "manager")))
4100 goto manager_failed;
4103 /* we manage this element */
4104 gst_element_set_locked_state (src->manager, TRUE);
4105 gst_bin_add (GST_BIN_CAST (src), src->manager);
4107 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
4108 if (ret == GST_STATE_CHANGE_FAILURE)
4109 goto start_manager_failure;
4111 g_object_set (src->manager, "latency", src->latency, NULL);
4113 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
4115 if (g_object_class_find_property (klass, "ntp-sync")) {
4116 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
4119 if (g_object_class_find_property (klass, "rfc7273-sync")) {
4120 g_object_set (src->manager, "rfc7273-sync", src->rfc7273_sync, NULL);
4123 if (g_object_class_find_property (klass, "add-reference-timestamp-meta")) {
4124 g_object_set (src->manager, "add-reference-timestamp-meta",
4125 src->add_reference_timestamp_meta, NULL);
4128 if (src->use_pipeline_clock) {
4129 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
4130 g_object_set (src->manager, "use-pipeline-clock", TRUE, NULL);
4133 if (g_object_class_find_property (klass, "ntp-time-source")) {
4134 g_object_set (src->manager, "ntp-time-source", src->ntp_time_source,
4139 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
4140 g_object_set (src->manager, "sdes", src->sdes, NULL);
4143 if (g_object_class_find_property (klass, "drop-on-latency")) {
4144 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
4148 if (g_object_class_find_property (klass, "max-rtcp-rtp-time-diff")) {
4149 g_object_set (src->manager, "max-rtcp-rtp-time-diff",
4150 src->max_rtcp_rtp_time_diff, NULL);
4153 if (g_object_class_find_property (klass, "max-ts-offset-adjustment")) {
4154 g_object_set (src->manager, "max-ts-offset-adjustment",
4155 src->max_ts_offset_adjustment, NULL);
4158 if (g_object_class_find_property (klass, "max-ts-offset")) {
4159 gint64 max_ts_offset;
4161 /* setting max-ts-offset in the manager has side effects so only do it
4162 * if the value differs */
4163 g_object_get (src->manager, "max-ts-offset", &max_ts_offset, NULL);
4164 if (max_ts_offset != src->max_ts_offset) {
4165 g_object_set (src->manager, "max-ts-offset", src->max_ts_offset,
4170 /* buffer mode pauses are handled by adding offsets to buffer times,
4171 * but some depayloaders may have a hard time syncing output times
4172 * with such input times, e.g. container ones, most notably ASF */
4173 /* TODO alternatives are having an event that indicates these shifts,
4174 * or having rtsp extensions provide suggestion on buffer mode */
4175 /* valid duration implies not likely live pipeline,
4176 * so slaving in jitterbuffer does not make much sense
4177 * (and might mess things up due to bursts) */
4178 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
4179 src->segment.duration && stream->container) {
4180 src->use_buffering = TRUE;
4182 src->use_buffering = FALSE;
4185 set_manager_buffer_mode (src);
4187 /* connect to signals */
4188 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
4190 src->manager_sig_id =
4191 g_signal_connect (src->manager, "pad-added",
4192 (GCallback) new_manager_pad, src);
4193 src->manager_ptmap_id =
4194 g_signal_connect (src->manager, "request-pt-map",
4195 (GCallback) request_pt_map, src);
4197 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
4200 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
4203 if (src->do_retransmission)
4204 add_retransmission (src, transport);
4206 g_signal_connect (src->manager, "request-rtp-decoder",
4207 (GCallback) request_rtp_decoder, stream);
4208 g_signal_connect (src->manager, "request-rtcp-decoder",
4209 (GCallback) request_rtp_decoder, stream);
4210 g_signal_connect (src->manager, "request-rtcp-encoder",
4211 (GCallback) request_rtcp_encoder, stream);
4213 /* we stream directly to the manager, get some pads. Each RTSP stream goes
4214 * into a separate RTP session. */
4215 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
4216 stream->channelpad[0] = gst_element_request_pad_simple (src->manager, name);
4218 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
4219 stream->channelpad[1] = gst_element_request_pad_simple (src->manager, name);
4222 /* now configure the bandwidth in the manager */
4223 if (g_signal_lookup ("get-internal-session",
4224 G_OBJECT_TYPE (src->manager)) != 0) {
4225 GObject *rtpsession;
4227 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
4230 GstRTPProfile rtp_profile;
4232 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
4234 stream->session = rtpsession;
4236 if (stream->as_bandwidth != -1) {
4237 GST_INFO_OBJECT (src, "setting AS: %f",
4238 (gdouble) (stream->as_bandwidth * 1000));
4239 g_object_set (rtpsession, "bandwidth",
4240 (gdouble) (stream->as_bandwidth * 1000), NULL);
4242 if (stream->rr_bandwidth != -1) {
4243 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
4244 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
4247 if (stream->rs_bandwidth != -1) {
4248 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
4249 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
4253 switch (stream->profile) {
4254 case GST_RTSP_PROFILE_AVPF:
4255 rtp_profile = GST_RTP_PROFILE_AVPF;
4257 case GST_RTSP_PROFILE_SAVP:
4258 rtp_profile = GST_RTP_PROFILE_SAVP;
4260 case GST_RTSP_PROFILE_SAVPF:
4261 rtp_profile = GST_RTP_PROFILE_SAVPF;
4263 case GST_RTSP_PROFILE_AVP:
4265 rtp_profile = GST_RTP_PROFILE_AVP;
4269 g_object_set (rtpsession, "rtp-profile", rtp_profile, NULL);
4271 g_object_set (rtpsession, "probation", src->probation, NULL);
4273 g_object_set (rtpsession, "internal-ssrc", stream->send_ssrc, NULL);
4275 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
4277 g_signal_connect (rtpsession, "on-bye-timeout",
4278 (GCallback) on_timeout_common, stream);
4279 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
4281 g_signal_connect (rtpsession, "on-ssrc-active",
4282 (GCallback) on_ssrc_active, stream);
4293 GST_DEBUG_OBJECT (src, "cannot get a session manager");
4298 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
4301 start_manager_failure:
4303 GST_DEBUG_OBJECT (src, "could not start session manager");
4308 /* free the UDP sources allocated when negotiating a transport.
4309 * This function is called when the server negotiated to a transport where the
4310 * UDP sources are not needed anymore, such as TCP or multicast. */
4312 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
4316 for (i = 0; i < 2; i++) {
4317 if (stream->udpsrc[i]) {
4318 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
4319 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
4320 gst_object_unref (stream->udpsrc[i]);
4321 stream->udpsrc[i] = NULL;
4326 /* for TCP, create pads to send and receive data to and from the manager and to
4327 * intercept various events and queries
4330 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
4331 GstRTSPTransport * transport, GstPad ** outpad)
4334 GstPadTemplate *template;
4335 GstPad *pad0, *pad1;
4337 /* configure for interleaved delivery, nothing needs to be done
4338 * here, the loop function will call the chain functions of the
4339 * session manager. */
4340 stream->channel[0] = transport->interleaved.min;
4341 stream->channel[1] = transport->interleaved.max;
4342 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
4343 stream->channel[0], stream->channel[1]);
4345 /* we can remove the allocated UDP ports now */
4346 gst_rtspsrc_stream_free_udp (stream);
4348 /* no session manager, send data to srcpad directly */
4349 if (!stream->channelpad[0]) {
4350 GST_DEBUG_OBJECT (src, "no manager, creating pad");
4352 /* create a new pad we will use to stream to */
4353 name = g_strdup_printf ("stream_%u", stream->id);
4354 template = gst_static_pad_template_get (&rtptemplate);
4355 stream->channelpad[0] = gst_pad_new_from_template (template, name);
4356 gst_object_unref (template);
4359 /* set caps and activate */
4360 gst_pad_use_fixed_caps (stream->channelpad[0]);
4361 gst_pad_set_active (stream->channelpad[0], TRUE);
4363 *outpad = gst_object_ref (stream->channelpad[0]);
4365 GST_DEBUG_OBJECT (src, "using manager source pad");
4367 template = gst_static_pad_template_get (&anysrctemplate);
4369 /* allocate pads for sending the channel data into the manager */
4370 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
4371 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
4372 gst_object_unref (stream->channelpad[0]);
4373 stream->channelpad[0] = pad0;
4374 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
4375 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
4376 gst_pad_set_element_private (pad0, src);
4377 gst_pad_set_active (pad0, TRUE);
4379 if (stream->channelpad[1]) {
4380 /* if we have a sinkpad for the other channel, create a pad and link to the
4382 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
4383 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
4384 gst_pad_link_full (pad1, stream->channelpad[1],
4385 GST_PAD_LINK_CHECK_NOTHING);
4386 gst_object_unref (stream->channelpad[1]);
4387 stream->channelpad[1] = pad1;
4388 gst_pad_set_active (pad1, TRUE);
4390 gst_object_unref (template);
4392 /* setup RTCP transport back to the server if we have to. */
4393 if (src->manager && src->do_rtcp) {
4396 template = gst_static_pad_template_get (&anysinktemplate);
4398 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
4399 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
4400 gst_pad_set_element_private (stream->rtcppad, stream);
4401 gst_pad_set_active (stream->rtcppad, TRUE);
4403 /* get session RTCP pad */
4404 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
4405 pad = gst_element_request_pad_simple (src->manager, name);
4410 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
4411 gst_object_unref (pad);
4414 gst_object_unref (template);
4420 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
4421 GstRTSPTransport * transport, const gchar ** destination, gint * min,
4422 gint * max, guint * ttl)
4424 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
4426 if (!(*destination = transport->destination))
4427 *destination = stream->destination;
4430 /* transport first */
4431 *min = transport->port.min;
4432 *max = transport->port.max;
4433 if (*min == -1 && *max == -1) {
4434 /* then try from SDP */
4435 if (stream->port != 0) {
4436 *min = stream->port;
4437 *max = stream->port + 1;
4443 if (!(*ttl = transport->ttl))
4448 /* first take the source, then the endpoint to figure out where to send
4450 if (!(*destination = transport->source)) {
4451 if (src->conninfo.connection)
4452 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
4453 else if (stream->conninfo.connection)
4455 gst_rtsp_connection_get_ip (stream->conninfo.connection);
4459 /* for unicast we only expect the ports here */
4460 *min = transport->server_port.min;
4461 *max = transport->server_port.max;
4467 element_make_from_addr (const GstURIType type, const char *addr_s,
4468 int port, const char *name, GError ** error)
4471 GstElement *element = NULL;
4474 addr = g_inet_address_new_from_string (addr_s);
4476 switch (g_inet_address_get_family (addr)) {
4477 case G_SOCKET_FAMILY_IPV6:
4478 uri = g_strdup_printf ("udp://[%s]:%i", addr_s, port);
4480 case G_SOCKET_FAMILY_INVALID:
4481 GST_ERROR ("Unknown family type for %s", addr_s);
4483 case G_SOCKET_FAMILY_UNIX:
4484 GST_ERROR ("Unexpected family type UNIX for %s", addr_s);
4486 case G_SOCKET_FAMILY_IPV4:
4487 uri = g_strdup_printf ("udp://%s:%i", addr_s, port);
4491 element = gst_element_make_from_uri (type, uri, name, error);
4493 g_object_unref (addr);
4498 /* For multicast create UDP sources and join the multicast group. */
4500 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
4501 GstRTSPTransport * transport, GstPad ** outpad)
4503 const gchar *destination;
4506 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
4508 /* we can remove the allocated UDP ports now */
4509 gst_rtspsrc_stream_free_udp (stream);
4511 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
4514 /* we need a destination now */
4515 if (destination == NULL)
4516 goto no_destination;
4518 /* we really need ports now or we won't be able to receive anything at all */
4519 if (min == -1 && max == -1)
4522 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
4523 destination, min, max);
4525 /* creating UDP source for RTP */
4528 element_make_from_addr (GST_URI_SRC, destination, min, NULL, NULL);
4529 if (stream->udpsrc[0] == NULL)
4532 /* take ownership */
4533 gst_object_ref_sink (stream->udpsrc[0]);
4535 if (src->udp_buffer_size != 0)
4536 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
4537 src->udp_buffer_size, NULL);
4539 if (src->multi_iface != NULL)
4540 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
4541 src->multi_iface, NULL);
4544 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
4545 gst_element_set_state (stream->udpsrc[0], GST_STATE_READY);
4548 /* creating another UDP source for RTCP */
4553 element_make_from_addr (GST_URI_SRC, destination, max, NULL, NULL);
4554 if (stream->udpsrc[1] == NULL)
4557 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
4558 stream->profile == GST_RTSP_PROFILE_SAVPF)
4559 caps = gst_caps_new_empty_simple ("application/x-srtcp");
4561 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4562 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
4563 gst_caps_unref (caps);
4565 /* take ownership */
4566 gst_object_ref_sink (stream->udpsrc[1]);
4568 if (src->multi_iface != NULL)
4569 g_object_set (G_OBJECT (stream->udpsrc[1]), "multicast-iface",
4570 src->multi_iface, NULL);
4572 gst_element_set_state (stream->udpsrc[1], GST_STATE_READY);
4579 GST_DEBUG_OBJECT (src, "no UDP source element found");
4584 GST_DEBUG_OBJECT (src, "no destination found");
4589 GST_DEBUG_OBJECT (src, "no ports found");
4594 /* configure the remainder of the UDP ports */
4596 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
4597 GstRTSPTransport * transport, GstPad ** outpad)
4599 /* we manage the UDP elements now. For unicast, the UDP sources where
4600 * allocated in the stream when we suggested a transport. */
4601 if (stream->udpsrc[0]) {
4604 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
4605 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
4607 GST_DEBUG_OBJECT (src, "setting up UDP source");
4609 /* configure a timeout on the UDP port. When the timeout message is
4610 * posted, we assume UDP transport is not possible. We reconnect using TCP
4612 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
4613 src->udp_timeout * 1000, NULL);
4615 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt)))
4616 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
4618 /* get output pad of the UDP source. */
4619 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
4621 /* save it so we can unblock */
4622 stream->blockedpad = *outpad;
4624 /* configure pad block on the pad. As soon as there is dataflow on the
4625 * UDP source, we know that UDP is not blocked by a firewall and we can
4626 * configure all the streams to let the application autoplug decoders. */
4628 gst_pad_add_probe (stream->blockedpad,
4629 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
4630 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocked, src, NULL);
4632 gst_pad_add_probe (stream->blockedpad,
4633 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM, udpsrc_probe_cb,
4634 &(stream->segment_seqnum[0]), NULL);
4636 if (stream->channelpad[0]) {
4637 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
4638 /* configure for UDP delivery, we need to connect the UDP pads to
4639 * the session plugin. */
4640 gst_pad_link_full (*outpad, stream->channelpad[0],
4641 GST_PAD_LINK_CHECK_NOTHING);
4642 gst_object_unref (*outpad);
4644 /* we connected to pad-added signal to get pads from the manager */
4646 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
4651 if (stream->udpsrc[1]) {
4654 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
4655 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
4657 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
4658 stream->profile == GST_RTSP_PROFILE_SAVPF)
4659 caps = gst_caps_new_empty_simple ("application/x-srtcp");
4661 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4662 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
4663 gst_caps_unref (caps);
4665 if (stream->channelpad[1]) {
4668 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
4670 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
4671 gst_pad_add_probe (pad,
4672 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM, udpsrc_probe_cb,
4673 &(stream->segment_seqnum[1]), NULL);
4674 gst_pad_link_full (pad, stream->channelpad[1],
4675 GST_PAD_LINK_CHECK_NOTHING);
4676 gst_object_unref (pad);
4678 /* leave unlinked */
4684 /* configure the UDP sink back to the server for status reports */
4686 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
4687 GstRTSPStream * stream, GstRTSPTransport * transport)
4690 gint rtp_port, rtcp_port;
4691 gboolean do_rtp, do_rtcp;
4692 const gchar *destination;
4697 /* get transport info */
4698 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
4699 &rtp_port, &rtcp_port, &ttl);
4701 /* see what we need to do */
4702 do_rtp = (rtp_port != -1);
4703 /* it's possible that the server does not want us to send RTCP in which case
4705 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
4707 /* we need a destination when we have RTP or RTCP ports */
4708 if (destination == NULL && (do_rtp || do_rtcp))
4709 goto no_destination;
4711 /* try to construct the fakesrc to the RTP port of the server to open up any
4712 * NAT firewalls or, if backchannel, construct an appsrc */
4714 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
4717 stream->udpsink[0] = element_make_from_addr (GST_URI_SINK, destination,
4718 rtp_port, NULL, NULL);
4719 if (stream->udpsink[0] == NULL)
4720 goto no_sink_element;
4722 /* don't join multicast group, we will have the source socket do that */
4723 /* no sync or async state changes needed */
4724 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
4725 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
4727 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
4729 if (stream->udpsrc[0]) {
4730 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
4731 * so that NAT firewalls will open a hole for us */
4732 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
4736 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
4737 /* configure socket and make sure udpsink does not close it when shutting
4738 * down, it belongs to udpsrc after all. */
4739 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
4740 "close-socket", FALSE, NULL);
4741 g_object_unref (socket);
4744 if (stream->is_backchannel) {
4745 /* appsrc is for the app to shovel data using push-backchannel-buffer */
4746 stream->rtpsrc = gst_element_factory_make ("appsrc", NULL);
4747 if (stream->rtpsrc == NULL)
4748 goto no_appsrc_element;
4750 /* interal use only, don't emit signals */
4751 g_object_set (G_OBJECT (stream->rtpsrc), "emit-signals", TRUE,
4752 "is-live", TRUE, NULL);
4754 /* the source for the dummy packets to open up NAT */
4755 stream->rtpsrc = gst_element_factory_make ("fakesrc", NULL);
4756 if (stream->rtpsrc == NULL)
4757 goto no_fakesrc_element;
4759 /* random data in 5 buffers, a size of 200 bytes should be fine */
4760 g_object_set (G_OBJECT (stream->rtpsrc), "filltype", 3, "num-buffers", 5,
4761 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
4764 /* keep everything locked */
4765 gst_element_set_locked_state (stream->udpsink[0], TRUE);
4766 gst_element_set_locked_state (stream->rtpsrc, TRUE);
4768 gst_object_ref (stream->udpsink[0]);
4769 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
4770 gst_object_ref (stream->rtpsrc);
4771 gst_bin_add (GST_BIN_CAST (src), stream->rtpsrc);
4773 gst_element_link_pads_full (stream->rtpsrc, "src", stream->udpsink[0],
4774 "sink", GST_PAD_LINK_CHECK_NOTHING);
4777 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
4780 stream->udpsink[1] = element_make_from_addr (GST_URI_SINK, destination,
4781 rtcp_port, NULL, NULL);
4782 if (stream->udpsink[1] == NULL)
4783 goto no_sink_element;
4785 /* don't join multicast group, we will have the source socket do that */
4786 /* no sync or async state changes needed */
4787 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
4788 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
4790 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
4792 if (stream->udpsrc[1]) {
4793 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
4794 * because some servers check the port number of where it sends RTCP to identify
4795 * the RTCP packets it receives */
4796 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
4800 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
4801 /* configure socket and make sure udpsink does not close it when shutting
4802 * down, it belongs to udpsrc after all. */
4803 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
4804 "close-socket", FALSE, NULL);
4805 g_object_unref (socket);
4808 /* we keep this playing always */
4809 gst_element_set_locked_state (stream->udpsink[1], TRUE);
4810 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
4812 gst_object_ref (stream->udpsink[1]);
4813 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
4815 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
4817 /* get session RTCP pad */
4818 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
4819 pad = gst_element_request_pad_simple (src->manager, name);
4824 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
4825 gst_object_unref (pad);
4834 GST_ERROR_OBJECT (src, "no destination address specified");
4839 GST_ERROR_OBJECT (src, "no UDP sink element found");
4844 GST_ERROR_OBJECT (src, "no appsrc element found");
4849 GST_ERROR_OBJECT (src, "no fakesrc element found");
4854 GST_ERROR_OBJECT (src, "failed to create socket");
4859 /* sets up all elements needed for streaming over the specified transport.
4860 * Does not yet expose the element pads, this will be done when there is actuall
4861 * dataflow detected, which might never happen when UDP is blocked in a
4862 * firewall, for example.
4865 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
4866 GstRTSPTransport * transport)
4869 GstPad *outpad = NULL;
4870 GstPadTemplate *template;
4872 const gchar *media_type;
4875 src = stream->parent;
4877 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
4879 /* get the proper media type for this stream now */
4880 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
4881 goto unknown_transport;
4883 goto unknown_transport;
4885 /* configure the final media type */
4886 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
4888 len = stream->ptmap->len;
4889 for (i = 0; i < len; i++) {
4891 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
4893 if (item->caps == NULL)
4896 s = gst_caps_get_structure (item->caps, 0);
4897 gst_structure_set_name (s, media_type);
4898 /* set ssrc if known */
4899 if (transport->ssrc)
4900 gst_structure_set (s, "ssrc", G_TYPE_UINT, transport->ssrc, NULL);
4903 /* try to get and configure a manager, channelpad[0-1] will be configured with
4904 * the pads for the manager, or NULL when no manager is needed. */
4905 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
4908 switch (transport->lower_transport) {
4909 case GST_RTSP_LOWER_TRANS_TCP:
4910 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
4911 goto transport_failed;
4913 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
4914 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
4915 goto transport_failed;
4916 /* fallthrough, the rest is the same for UDP and MCAST */
4917 case GST_RTSP_LOWER_TRANS_UDP:
4918 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
4919 goto transport_failed;
4920 /* configure udpsinks back to the server for RTCP messages, for the
4921 * dummy RTP messages to open NAT, and for the backchannel */
4922 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
4923 goto transport_failed;
4926 goto unknown_transport;
4929 /* using backchannel and no manager, hence no srcpad for this stream */
4930 if (outpad && stream->is_backchannel) {
4931 add_backchannel_fakesink (src, stream, outpad);
4932 gst_object_unref (outpad);
4933 } else if (outpad) {
4934 GstPad *internal_src;
4936 GST_DEBUG_OBJECT (src, "creating ghostpad for stream %p", stream);
4938 gst_pad_use_fixed_caps (outpad);
4940 /* create ghostpad, don't add just yet, this will be done when we activate
4942 name = g_strdup_printf ("stream_%u", stream->id);
4943 template = gst_static_pad_template_get (&rtptemplate);
4944 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
4945 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
4946 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
4947 gst_object_unref (template);
4950 /* We intercept and modify the stream start event */
4952 GST_PAD (gst_proxy_pad_get_internal (GST_PROXY_PAD (stream->srcpad)));
4953 gst_pad_set_element_private (internal_src, stream);
4954 gst_pad_set_event_function (internal_src,
4955 gst_rtspsrc_handle_src_sink_event);
4956 gst_object_unref (internal_src);
4958 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
4959 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
4961 gst_object_unref (outpad);
4963 /* mark pad as ok */
4964 stream->last_ret = GST_FLOW_OK;
4971 GST_WARNING_OBJECT (src, "failed to configure transport");
4976 GST_WARNING_OBJECT (src, "unknown transport");
4981 GST_WARNING_OBJECT (src, "cannot get a session manager");
4986 /* send a couple of dummy random packets on the receiver RTP port to the server,
4987 * this should make a firewall think we initiated the data transfer and
4988 * hopefully allow packets to go from the sender port to our RTP receiver port */
4990 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
4994 if (src->nat_method != GST_RTSP_NAT_DUMMY)
4997 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4998 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5000 if (!stream->rtpsrc || !stream->udpsink[0])
5003 if (stream->is_backchannel)
5004 GST_DEBUG_OBJECT (src, "starting backchannel stream %p", stream);
5006 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
5008 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
5009 gst_element_set_state (stream->rtpsrc, GST_STATE_NULL);
5010 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
5011 gst_element_set_state (stream->rtpsrc, GST_STATE_PLAYING);
5016 /* Adds the source pads of all configured streams to the element.
5017 * This code is performed when we detected dataflow.
5019 * We detect dataflow from either the _loop function or with pad probes on the
5023 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
5027 GST_DEBUG_OBJECT (src, "activating streams");
5029 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5030 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5032 if (stream->udpsrc[0]) {
5033 /* remove timeout, we are streaming now and timeouts will be handled by
5034 * the session manager and jitter buffer */
5035 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
5037 if (stream->srcpad) {
5038 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
5039 gst_pad_set_active (stream->srcpad, TRUE);
5041 /* if we don't have a session manager, set the caps now. If we have a
5042 * session, we will get a notification of the pad and the caps. */
5043 if (!src->manager) {
5046 caps = stream_get_caps_for_pt (stream, stream->default_pt);
5047 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
5048 gst_pad_set_caps (stream->srcpad, caps);
5051 if (!stream->added) {
5052 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
5053 if (stream->is_backchannel)
5054 add_backchannel_fakesink (src, stream, stream->srcpad);
5056 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
5057 stream->added = TRUE;
5062 /* unblock all pads */
5063 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5064 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5066 if (stream->blockid) {
5067 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
5068 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
5069 stream->blockid = 0;
5077 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
5078 gboolean reset_manager)
5081 guint64 start, stop;
5082 gdouble play_speed, play_scale;
5084 GST_DEBUG_OBJECT (src, "configuring stream caps");
5086 start = segment->rate > 0.0 ? segment->start : segment->stop;
5087 stop = segment->rate > 0.0 ? segment->stop : segment->start;
5088 play_speed = segment->rate;
5089 play_scale = segment->applied_rate;
5091 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5092 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5098 len = stream->ptmap->len;
5099 for (j = 0; j < len; j++) {
5101 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
5103 if (item->caps == NULL)
5106 caps = gst_caps_make_writable (item->caps);
5108 if (stream->timebase != -1)
5109 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
5110 (guint) stream->timebase, NULL);
5111 if (stream->seqbase != -1)
5112 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
5113 (guint) stream->seqbase, NULL);
5114 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
5116 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
5117 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
5118 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
5119 gst_caps_set_simple (caps, "onvif-mode", G_TYPE_BOOLEAN, src->onvif_mode,
5123 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
5126 if (item->pt == stream->default_pt) {
5127 if (stream->udpsrc[0])
5128 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
5129 stream->need_caps = TRUE;
5133 if (reset_manager && src->manager) {
5134 GST_DEBUG_OBJECT (src, "clear session");
5135 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
5139 static GstFlowReturn
5140 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
5145 /* store the value */
5146 stream->last_ret = ret;
5148 /* if it's success we can return the value right away */
5149 if (ret == GST_FLOW_OK)
5152 /* any other error that is not-linked can be returned right
5154 if (ret != GST_FLOW_NOT_LINKED)
5157 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
5158 for (streams = src->streams; streams; streams = g_list_next (streams)) {
5159 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
5161 ret = ostream->last_ret;
5162 /* some other return value (must be SUCCESS but we can return
5163 * other values as well) */
5164 if (ret != GST_FLOW_NOT_LINKED)
5167 /* if we get here, all other pads were unlinked and we return
5168 * NOT_LINKED then */
5174 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
5177 gboolean res = TRUE;
5179 /* only streams that have a connection to the outside world */
5183 switch (GST_EVENT_TYPE (event)) {
5187 case GST_EVENT_FLUSH_STOP:
5188 stream->eos = FALSE;
5194 if (stream->udpsrc[0]) {
5195 GstEvent *sent_event;
5197 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
5198 sent_event = gst_event_new_eos ();
5199 gst_event_set_seqnum (sent_event, stream->segment_seqnum[0]);
5201 sent_event = gst_event_ref (event);
5204 res = gst_element_send_event (stream->udpsrc[0], sent_event);
5205 } else if (stream->channelpad[0]) {
5206 gst_event_ref (event);
5207 if (GST_PAD_IS_SRC (stream->channelpad[0]))
5208 res = gst_pad_push_event (stream->channelpad[0], event);
5210 res = gst_pad_send_event (stream->channelpad[0], event);
5213 if (stream->udpsrc[1]) {
5214 GstEvent *sent_event;
5216 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
5217 sent_event = gst_event_new_eos ();
5218 if (stream->segment_seqnum[1] != GST_SEQNUM_INVALID) {
5219 gst_event_set_seqnum (sent_event, stream->segment_seqnum[1]);
5222 sent_event = gst_event_ref (event);
5225 res &= gst_element_send_event (stream->udpsrc[1], sent_event);
5226 } else if (stream->channelpad[1]) {
5227 gst_event_ref (event);
5228 if (GST_PAD_IS_SRC (stream->channelpad[1]))
5229 res &= gst_pad_push_event (stream->channelpad[1], event);
5231 res &= gst_pad_send_event (stream->channelpad[1], event);
5235 gst_event_unref (event);
5241 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
5244 gboolean res = TRUE;
5246 for (streams = src->streams; streams; streams = g_list_next (streams)) {
5247 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
5249 gst_event_ref (event);
5250 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
5252 gst_event_unref (event);
5258 accept_certificate_cb (GTlsConnection * conn, GTlsCertificate * peer_cert,
5259 GTlsCertificateFlags errors, gpointer user_data)
5261 GstRTSPSrc *src = user_data;
5262 gboolean accept = FALSE;
5264 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ACCEPT_CERTIFICATE], 0, conn,
5265 peer_cert, errors, &accept);
5270 static GstRTSPResult
5271 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
5275 GstRTSPMessage response;
5276 gboolean retry = FALSE;
5277 memset (&response, 0, sizeof (response));
5278 gst_rtsp_message_init (&response);
5280 if (info->connection == NULL) {
5281 if (info->url == NULL) {
5282 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
5283 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
5286 /* create connection */
5287 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
5288 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
5289 goto could_not_create;
5292 gst_rtspsrc_setup_auth (src, &response);
5295 g_free (info->url_str);
5296 info->url_str = gst_rtsp_url_get_request_uri (info->url);
5298 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
5300 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
5301 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
5302 src->tls_validation_flags))
5303 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
5305 if (src->tls_database)
5306 gst_rtsp_connection_set_tls_database (info->connection,
5309 if (src->tls_interaction)
5310 gst_rtsp_connection_set_tls_interaction (info->connection,
5311 src->tls_interaction);
5312 gst_rtsp_connection_set_accept_certificate_func (info->connection,
5313 accept_certificate_cb, src, NULL);
5316 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP) {
5317 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
5318 gst_rtsp_connection_set_ignore_x_server_reply (info->connection,
5319 src->ignore_x_server_reply);
5322 if (src->proxy_host) {
5323 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
5325 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
5330 if (!info->connected) {
5333 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
5334 ("Connecting to %s", info->location));
5335 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
5336 res = gst_rtsp_connection_connect_with_response_usec (info->connection,
5337 src->tcp_timeout, &response);
5339 if (response.type == GST_RTSP_MESSAGE_HTTP_RESPONSE &&
5340 response.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
5341 gst_rtsp_conninfo_close (src, info, TRUE);
5345 retry = FALSE; // we should not retry more than once
5350 if (res == GST_RTSP_OK)
5351 info->connected = TRUE;
5353 goto could_not_connect;
5355 } while (!info->connected && retry);
5357 gst_rtsp_message_unset (&response);
5363 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
5364 gst_rtsp_message_unset (&response);
5369 gchar *str = gst_rtsp_strresult (res);
5370 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
5372 gst_rtsp_message_unset (&response);
5377 gchar *str = gst_rtsp_strresult (res);
5378 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
5380 gst_rtsp_message_unset (&response);
5385 static GstRTSPResult
5386 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
5389 GST_RTSP_STATE_LOCK (src);
5390 if (info->connected) {
5391 GST_DEBUG_OBJECT (src, "closing connection...");
5392 gst_rtsp_connection_close (info->connection);
5393 info->connected = FALSE;
5395 if (free && info->connection) {
5396 /* free connection */
5397 GST_DEBUG_OBJECT (src, "freeing connection...");
5398 gst_rtsp_connection_free (info->connection);
5399 info->connection = NULL;
5400 info->flushing = FALSE;
5402 GST_RTSP_STATE_UNLOCK (src);
5406 static GstRTSPResult
5407 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
5412 GST_DEBUG_OBJECT (src, "reconnecting connection...");
5413 gst_rtsp_conninfo_close (src, info, FALSE);
5414 res = gst_rtsp_conninfo_connect (src, info, async);
5420 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
5424 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
5425 GST_RTSP_STATE_LOCK (src);
5426 if (src->conninfo.connection && src->conninfo.flushing != flush) {
5427 GST_DEBUG_OBJECT (src, "connection flush");
5428 gst_rtsp_connection_flush (src->conninfo.connection, flush);
5429 src->conninfo.flushing = flush;
5431 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5432 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5433 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
5434 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
5435 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
5436 stream->conninfo.flushing = flush;
5439 GST_RTSP_STATE_UNLOCK (src);
5442 static GstRTSPResult
5443 gst_rtspsrc_init_request (GstRTSPSrc * src, GstRTSPMessage * msg,
5444 GstRTSPMethod method, const gchar * uri)
5448 res = gst_rtsp_message_init_request (msg, method, uri);
5452 /* set user-agent */
5453 if (src->user_agent)
5454 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_USER_AGENT, src->user_agent);
5459 /* FIXME, handle server request, reply with OK, for now */
5460 static GstRTSPResult
5461 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
5462 GstRTSPMessage * request)
5464 GstRTSPMessage response = { 0 };
5467 GST_DEBUG_OBJECT (src, "got server request message");
5469 DEBUG_RTSP (src, request);
5471 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
5473 if (res == GST_RTSP_ENOTIMPL) {
5474 /* default implementation, send OK */
5475 GST_DEBUG_OBJECT (src, "prepare OK reply");
5477 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
5482 /* let app parse and reply */
5483 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
5484 0, request, &response);
5486 DEBUG_RTSP (src, &response);
5488 res = gst_rtspsrc_connection_send (src, conninfo, &response, 0);
5492 gst_rtsp_message_unset (&response);
5493 } else if (res == GST_RTSP_EEOF)
5501 gst_rtsp_message_unset (&response);
5506 /* send server keep-alive */
5507 static GstRTSPResult
5508 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
5510 GstRTSPMessage request = { 0 };
5512 GstRTSPMethod method;
5513 const gchar *control;
5515 if (src->do_rtsp_keep_alive == FALSE) {
5516 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
5517 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
5521 GST_DEBUG_OBJECT (src, "creating server keep-alive");
5523 /* find a method to use for keep-alive */
5524 if (src->methods & GST_RTSP_GET_PARAMETER)
5525 method = GST_RTSP_GET_PARAMETER;
5527 method = GST_RTSP_OPTIONS;
5529 control = get_aggregate_control (src);
5530 if (control == NULL)
5533 res = gst_rtspsrc_init_request (src, &request, method, control);
5537 request.type_data.request.version = src->version;
5539 res = gst_rtspsrc_connection_send (src, &src->conninfo, &request, 0);
5543 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
5544 gst_rtsp_message_unset (&request);
5551 GST_WARNING_OBJECT (src, "no control url to send keepalive");
5556 gchar *str = gst_rtsp_strresult (res);
5558 gst_rtsp_message_unset (&request);
5559 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
5560 ("Could not send keep-alive. (%s)", str));
5566 static GstFlowReturn
5567 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
5569 GstFlowReturn ret = GST_FLOW_OK;
5571 GstRTSPStream *stream;
5572 GstPad *outpad = NULL;
5578 channel = message->type_data.data.channel;
5580 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
5582 goto unknown_stream;
5584 if (channel == stream->channel[0]) {
5585 outpad = stream->channelpad[0];
5587 } else if (channel == stream->channel[1]) {
5588 outpad = stream->channelpad[1];
5594 /* take a look at the body to figure out what we have */
5595 gst_rtsp_message_get_body (message, &data, &size);
5597 goto invalid_length;
5599 /* channels are not correct on some servers, do extra check */
5600 if (data[1] >= 200 && data[1] <= 204) {
5601 /* hmm RTCP message switch to the RTCP pad of the same stream. */
5602 outpad = stream->channelpad[1];
5606 /* we have no clue what this is, just ignore then. */
5608 goto unknown_stream;
5610 /* take the message body for further processing */
5611 gst_rtsp_message_steal_body (message, &data, &size);
5613 /* strip the trailing \0 */
5616 buf = gst_buffer_new ();
5617 gst_buffer_append_memory (buf,
5618 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
5620 /* don't need message anymore */
5621 gst_rtsp_message_unset (message);
5623 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
5626 if (src->need_activate) {
5633 /* generate an SHA256 sum of the URI */
5634 cs = g_checksum_new (G_CHECKSUM_SHA256);
5635 uri = src->conninfo.location;
5636 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
5638 for (streams = src->streams; streams; streams = g_list_next (streams)) {
5639 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
5642 /* Activate in advance so that the stream-start event is registered */
5643 if (stream->srcpad) {
5644 gst_pad_set_active (stream->srcpad, TRUE);
5648 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
5650 event = gst_event_new_stream_start (stream_id);
5652 gst_rtspsrc_stream_start_event_add_group_id (src, event);
5655 gst_rtspsrc_stream_push_event (src, ostream, event);
5657 if ((caps = stream_get_caps_for_pt (ostream, ostream->default_pt))) {
5658 /* only streams that have a connection to the outside world */
5659 if (ostream->setup) {
5660 if (ostream->udpsrc[0]) {
5661 gst_element_send_event (ostream->udpsrc[0],
5662 gst_event_new_caps (caps));
5663 } else if (ostream->channelpad[0]) {
5664 if (GST_PAD_IS_SRC (ostream->channelpad[0]))
5665 gst_pad_push_event (ostream->channelpad[0],
5666 gst_event_new_caps (caps));
5668 gst_pad_send_event (ostream->channelpad[0],
5669 gst_event_new_caps (caps));
5671 ostream->need_caps = FALSE;
5673 if (ostream->profile == GST_RTSP_PROFILE_SAVP ||
5674 ostream->profile == GST_RTSP_PROFILE_SAVPF)
5675 caps = gst_caps_new_empty_simple ("application/x-srtcp");
5677 caps = gst_caps_new_empty_simple ("application/x-rtcp");
5679 if (ostream->udpsrc[1]) {
5680 gst_element_send_event (ostream->udpsrc[1],
5681 gst_event_new_caps (caps));
5682 } else if (ostream->channelpad[1]) {
5683 if (GST_PAD_IS_SRC (ostream->channelpad[1]))
5684 gst_pad_push_event (ostream->channelpad[1],
5685 gst_event_new_caps (caps));
5687 gst_pad_send_event (ostream->channelpad[1],
5688 gst_event_new_caps (caps));
5691 gst_caps_unref (caps);
5695 g_checksum_free (cs);
5697 gst_rtspsrc_activate_streams (src);
5698 src->need_activate = FALSE;
5699 src->need_segment = TRUE;
5702 if (src->base_time == -1) {
5703 /* Take current running_time. This timestamp will be put on
5704 * the first buffer of each stream because we are a live source and so we
5705 * timestamp with the running_time. When we are dealing with TCP, we also
5706 * only timestamp the first buffer (using the DISCONT flag) because a server
5707 * typically bursts data, for which we don't want to compensate by speeding
5708 * up the media. The other timestamps will be interpollated from this one
5709 * using the RTP timestamps. */
5710 GST_OBJECT_LOCK (src);
5711 if (GST_ELEMENT_CLOCK (src)) {
5713 GstClockTime base_time;
5715 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
5716 base_time = GST_ELEMENT_CAST (src)->base_time;
5718 src->base_time = now - base_time;
5720 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
5721 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
5723 GST_OBJECT_UNLOCK (src);
5726 /* If needed send a new segment, don't forget we are live and buffer are
5727 * timestamped with running time */
5728 if (src->need_segment) {
5729 src->need_segment = FALSE;
5730 if (src->onvif_mode) {
5731 gst_rtspsrc_push_event (src, gst_event_new_segment (&src->out_segment));
5735 gst_segment_init (&segment, GST_FORMAT_TIME);
5736 gst_rtspsrc_push_event (src, gst_event_new_segment (&segment));
5740 if (stream->need_caps) {
5743 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt))) {
5744 /* only streams that have a connection to the outside world */
5745 if (stream->setup) {
5746 /* Only need to update the TCP caps here, UDP is already handled */
5747 if (stream->channelpad[0]) {
5748 if (GST_PAD_IS_SRC (stream->channelpad[0]))
5749 gst_pad_push_event (stream->channelpad[0],
5750 gst_event_new_caps (caps));
5752 gst_pad_send_event (stream->channelpad[0],
5753 gst_event_new_caps (caps));
5755 stream->need_caps = FALSE;
5759 stream->need_caps = FALSE;
5762 if (stream->discont && !is_rtcp) {
5763 /* mark first RTP buffer as discont */
5764 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
5765 stream->discont = FALSE;
5766 /* first buffer gets the timestamp, other buffers are not timestamped and
5767 * their presentation time will be interpollated from the rtp timestamps. */
5768 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
5769 GST_TIME_ARGS (src->base_time));
5771 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
5774 /* chain to the peer pad */
5775 if (GST_PAD_IS_SINK (outpad))
5776 ret = gst_pad_chain (outpad, buf);
5778 ret = gst_pad_push (outpad, buf);
5781 /* combine all stream flows for the data transport */
5782 ret = gst_rtspsrc_combine_flows (src, stream, ret);
5789 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
5790 gst_rtsp_message_unset (message);
5795 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5796 ("Short message received, ignoring."));
5797 gst_rtsp_message_unset (message);
5802 static GstFlowReturn
5803 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
5805 GstRTSPMessage message = { 0 };
5807 GstFlowReturn ret = GST_FLOW_OK;
5810 gst_rtsp_message_unset (&message);
5812 if (src->conninfo.flushing) {
5813 /* do not attempt to receive if flushing */
5814 res = GST_RTSP_EINTR;
5816 /* protect the connection with the connection lock so that we can see when
5817 * we are finished doing server communication */
5818 res = gst_rtspsrc_connection_receive (src, &src->conninfo, &message,
5824 GST_DEBUG_OBJECT (src, "we received a server message");
5826 case GST_RTSP_EINTR:
5827 /* we got interrupted this means we need to stop */
5829 case GST_RTSP_ETIMEOUT:
5830 /* no reply, send keep alive */
5831 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
5832 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5836 /* go EOS when the server closed the connection */
5842 switch (message.type) {
5843 case GST_RTSP_MESSAGE_REQUEST:
5844 /* server sends us a request message, handle it */
5845 res = gst_rtspsrc_handle_request (src, &src->conninfo, &message);
5846 if (res == GST_RTSP_EEOF)
5849 goto handle_request_failed;
5851 case GST_RTSP_MESSAGE_RESPONSE:
5852 /* we ignore response messages */
5853 GST_DEBUG_OBJECT (src, "ignoring response message");
5854 DEBUG_RTSP (src, &message);
5856 case GST_RTSP_MESSAGE_DATA:
5857 GST_DEBUG_OBJECT (src, "got data message");
5858 ret = gst_rtspsrc_handle_data (src, &message);
5859 if (ret != GST_FLOW_OK)
5860 goto handle_data_failed;
5863 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5868 g_assert_not_reached ();
5873 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5874 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5875 ("The server closed the connection."));
5876 src->conninfo.connected = FALSE;
5877 gst_rtsp_message_unset (&message);
5878 return GST_FLOW_EOS;
5882 gst_rtsp_message_unset (&message);
5883 GST_DEBUG_OBJECT (src, "got interrupted");
5884 return GST_FLOW_FLUSHING;
5888 gchar *str = gst_rtsp_strresult (res);
5890 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5891 ("Could not receive message. (%s)", str));
5894 gst_rtsp_message_unset (&message);
5895 return GST_FLOW_ERROR;
5897 handle_request_failed:
5899 gchar *str = gst_rtsp_strresult (res);
5901 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5902 ("Could not handle server message. (%s)", str));
5904 gst_rtsp_message_unset (&message);
5905 return GST_FLOW_ERROR;
5909 GST_DEBUG_OBJECT (src, "could no handle data message");
5914 static GstFlowReturn
5915 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
5918 GstRTSPMessage message = { 0 };
5924 /* get the next timeout interval */
5925 timeout = gst_rtsp_connection_next_timeout_usec (src->conninfo.connection);
5927 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
5928 (gint) timeout / G_USEC_PER_SEC);
5930 gst_rtsp_message_unset (&message);
5932 /* we should continue reading the TCP socket because the server might
5933 * send us requests. When the session timeout expires, we need to send a
5934 * keep-alive request to keep the session open. */
5935 if (src->conninfo.flushing) {
5936 /* do not attempt to receive if flushing */
5937 res = GST_RTSP_EINTR;
5939 res = gst_rtspsrc_connection_receive (src, &src->conninfo, &message,
5945 GST_DEBUG_OBJECT (src, "we received a server message");
5947 case GST_RTSP_EINTR:
5948 /* we got interrupted, see what we have to do */
5950 case GST_RTSP_ETIMEOUT:
5951 /* send keep-alive, ignore the result, a warning will be posted. */
5952 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
5953 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5957 /* server closed the connection. not very fatal for UDP, reconnect and
5958 * see what happens. */
5959 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5960 ("The server closed the connection."));
5961 if (src->udp_reconnect) {
5963 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
5970 GST_DEBUG_OBJECT (src, "An ethernet problem occurred.");
5972 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5973 ("Unhandled return value %d.", res));
5977 switch (message.type) {
5978 case GST_RTSP_MESSAGE_REQUEST:
5979 /* server sends us a request message, handle it */
5980 res = gst_rtspsrc_handle_request (src, &src->conninfo, &message);
5981 if (res == GST_RTSP_EEOF)
5984 goto handle_request_failed;
5986 case GST_RTSP_MESSAGE_RESPONSE:
5987 /* we ignore response and data messages */
5988 GST_DEBUG_OBJECT (src, "ignoring response message");
5989 DEBUG_RTSP (src, &message);
5990 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
5991 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
5992 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
5993 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
5994 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
6001 case GST_RTSP_MESSAGE_DATA:
6002 /* we ignore response and data messages */
6003 GST_DEBUG_OBJECT (src, "ignoring data message");
6006 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
6011 g_assert_not_reached ();
6013 /* we get here when the connection got interrupted */
6016 gst_rtsp_message_unset (&message);
6017 GST_DEBUG_OBJECT (src, "got interrupted");
6018 return GST_FLOW_FLUSHING;
6022 gchar *str = gst_rtsp_strresult (res);
6025 src->conninfo.connected = FALSE;
6026 if (res != GST_RTSP_EINTR) {
6027 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
6028 ("Could not connect to server. (%s)", str));
6030 ret = GST_FLOW_ERROR;
6032 ret = GST_FLOW_FLUSHING;
6038 gchar *str = gst_rtsp_strresult (res);
6040 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6041 ("Could not receive message. (%s)", str));
6043 return GST_FLOW_ERROR;
6045 handle_request_failed:
6047 gchar *str = gst_rtsp_strresult (res);
6050 gst_rtsp_message_unset (&message);
6051 if (res != GST_RTSP_EINTR) {
6052 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6053 ("Could not handle server message. (%s)", str));
6055 ret = GST_FLOW_ERROR;
6057 ret = GST_FLOW_FLUSHING;
6063 GST_DEBUG_OBJECT (src, "we got an eof from the server");
6064 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
6065 ("The server closed the connection."));
6066 src->conninfo.connected = FALSE;
6067 gst_rtsp_message_unset (&message);
6068 return GST_FLOW_EOS;
6072 static GstRTSPResult
6073 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
6075 GstRTSPResult res = GST_RTSP_OK;
6078 GST_DEBUG_OBJECT (src, "doing reconnect");
6080 GST_OBJECT_LOCK (src);
6081 /* only restart when the pads were not yet activated, else we were
6082 * streaming over UDP */
6083 restart = src->need_activate;
6084 GST_OBJECT_UNLOCK (src);
6086 /* no need to restart, we're done */
6090 /* we can try only TCP now */
6091 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
6093 /* close and cleanup our state */
6094 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
6097 /* see if we have TCP left to try. Also don't try TCP when we were configured
6099 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
6102 /* We post a warning message now to inform the user
6103 * that nothing happened. It's most likely a firewall thing. */
6104 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
6105 ("Could not receive any UDP packets for %.4f seconds, maybe your "
6106 "firewall is blocking it. Retrying using a tcp connection.",
6107 gst_guint64_to_gdouble (src->udp_timeout) / 1000000.0));
6109 /* open new connection using tcp */
6110 if (gst_rtspsrc_open (src, async) < 0)
6113 /* start playback */
6114 if (gst_rtspsrc_play (src, &src->segment, async, NULL) < 0)
6123 src->cur_protocols = 0;
6124 /* no transport possible, post an error and stop */
6125 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6126 ("Could not receive any UDP packets for %.4f seconds, maybe your "
6127 "firewall is blocking it. No other protocols to try.",
6128 gst_guint64_to_gdouble (src->udp_timeout) / 1000000.0));
6129 return GST_RTSP_ERROR;
6133 GST_DEBUG_OBJECT (src, "open failed");
6138 GST_DEBUG_OBJECT (src, "play failed");
6144 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
6148 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
6151 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
6154 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
6156 case CMD_GET_PARAMETER:
6157 GST_ELEMENT_PROGRESS (src, START, "request",
6158 ("Sending GET_PARAMETER request"));
6160 case CMD_SET_PARAMETER:
6161 GST_ELEMENT_PROGRESS (src, START, "request",
6162 ("Sending SET_PARAMETER request"));
6165 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
6173 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
6177 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
6180 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
6183 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
6185 case CMD_GET_PARAMETER:
6186 GST_ELEMENT_PROGRESS (src, COMPLETE, "request",
6187 ("Sent GET_PARAMETER request"));
6189 case CMD_SET_PARAMETER:
6190 GST_ELEMENT_PROGRESS (src, COMPLETE, "request",
6191 ("Sent SET_PARAMETER request"));
6194 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
6202 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
6206 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
6209 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
6212 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
6214 case CMD_GET_PARAMETER:
6215 GST_ELEMENT_PROGRESS (src, CANCELED, "request",
6216 ("GET_PARAMETER canceled"));
6218 case CMD_SET_PARAMETER:
6219 GST_ELEMENT_PROGRESS (src, CANCELED, "request",
6220 ("SET_PARAMETER canceled"));
6223 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
6231 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
6235 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
6238 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
6241 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
6243 case CMD_GET_PARAMETER:
6244 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("GET_PARAMETER failed"));
6246 case CMD_SET_PARAMETER:
6247 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("SET_PARAMETER failed"));
6250 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
6258 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
6260 if (ret == GST_RTSP_OK)
6261 gst_rtspsrc_loop_complete_cmd (src, cmd);
6262 else if (ret == GST_RTSP_EINTR)
6263 gst_rtspsrc_loop_cancel_cmd (src, cmd);
6265 gst_rtspsrc_loop_error_cmd (src, cmd);
6269 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
6272 gboolean flushed = FALSE;
6274 /* start new request */
6275 gst_rtspsrc_loop_start_cmd (src, cmd);
6277 GST_DEBUG_OBJECT (src, "sending cmd %s", cmd_to_string (cmd));
6279 GST_OBJECT_LOCK (src);
6280 old = src->pending_cmd;
6282 if (old == CMD_RECONNECT) {
6283 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
6284 cmd = CMD_RECONNECT;
6285 } else if (old == CMD_CLOSE) {
6286 /* our CMD_CLOSE might have interrutped CMD_LOOP. gst_rtspsrc_loop
6287 * will send a CMD_WAIT which would cancel our pending CMD_CLOSE (if
6288 * still pending). We just avoid it here by making sure CMD_CLOSE is
6289 * still the pending command. */
6290 GST_DEBUG_OBJECT (src, "ignore, we were closing");
6292 } else if (old == CMD_SET_PARAMETER) {
6293 GST_DEBUG_OBJECT (src, "ignore, we have a pending %s", cmd_to_string (old));
6294 cmd = CMD_SET_PARAMETER;
6295 } else if (old == CMD_GET_PARAMETER) {
6296 GST_DEBUG_OBJECT (src, "ignore, we have a pending %s", cmd_to_string (old));
6297 cmd = CMD_GET_PARAMETER;
6298 } else if (old != CMD_WAIT) {
6299 src->pending_cmd = CMD_WAIT;
6300 GST_OBJECT_UNLOCK (src);
6301 /* cancel previous request */
6302 GST_DEBUG_OBJECT (src, "cancel previous request %s", cmd_to_string (old));
6303 gst_rtspsrc_loop_cancel_cmd (src, old);
6304 GST_OBJECT_LOCK (src);
6306 src->pending_cmd = cmd;
6307 /* interrupt if allowed */
6308 if (src->busy_cmd & mask) {
6309 GST_DEBUG_OBJECT (src, "connection flush busy %s",
6310 cmd_to_string (src->busy_cmd));
6311 gst_rtspsrc_connection_flush (src, TRUE);
6314 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %s",
6315 cmd_to_string (src->busy_cmd));
6318 gst_task_start (src->task);
6319 GST_OBJECT_UNLOCK (src);
6325 gst_rtspsrc_loop_send_cmd_and_wait (GstRTSPSrc * src, gint cmd, gint mask,
6326 GstClockTime timeout)
6328 gboolean flushed = gst_rtspsrc_loop_send_cmd (src, cmd, mask);
6331 gint64 end_time = g_get_monotonic_time () + (timeout / 1000);
6332 GST_OBJECT_LOCK (src);
6333 while (src->pending_cmd == cmd || src->busy_cmd == cmd) {
6334 if (!g_cond_wait_until (&src->cmd_cond, GST_OBJECT_GET_LOCK (src),
6336 GST_WARNING_OBJECT (src,
6337 "Timed out waiting for TEARDOWN to be processed.");
6338 break; /* timeout passed */
6341 GST_OBJECT_UNLOCK (src);
6347 gst_rtspsrc_loop (GstRTSPSrc * src)
6351 if (!src->conninfo.connection || !src->conninfo.connected)
6354 if (src->interleaved)
6355 ret = gst_rtspsrc_loop_interleaved (src);
6357 ret = gst_rtspsrc_loop_udp (src);
6359 if (ret != GST_FLOW_OK)
6367 GST_WARNING_OBJECT (src, "we are not connected");
6368 ret = GST_FLOW_FLUSHING;
6373 const gchar *reason = gst_flow_get_name (ret);
6375 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
6376 src->running = FALSE;
6377 if (ret == GST_FLOW_EOS) {
6378 /* perform EOS logic */
6379 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
6380 gst_element_post_message (GST_ELEMENT_CAST (src),
6381 gst_message_new_segment_done (GST_OBJECT_CAST (src),
6382 src->segment.format, src->segment.position));
6383 gst_rtspsrc_push_event (src,
6384 gst_event_new_segment_done (src->segment.format,
6385 src->segment.position));
6387 gst_rtspsrc_push_event (src, gst_event_new_eos ());
6389 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
6390 /* for fatal errors we post an error message, post the error before the
6391 * EOS so the app knows about the error first. */
6392 GST_ELEMENT_FLOW_ERROR (src, ret);
6393 gst_rtspsrc_push_event (src, gst_event_new_eos ());
6395 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
6400 #ifndef GST_DISABLE_GST_DEBUG
6401 static const gchar *
6402 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
6406 while (method != 0) {
6423 /* Parse a WWW-Authenticate Response header and determine the
6424 * available authentication methods
6426 * This code should also cope with the fact that each WWW-Authenticate
6427 * header can contain multiple challenge methods + tokens
6429 * At the moment, for Basic auth, we just do a minimal check and don't
6430 * even parse out the realm */
6432 gst_rtspsrc_parse_auth_hdr (GstRTSPMessage * response,
6433 GstRTSPAuthMethod * methods, GstRTSPConnection * conn, gboolean * stale)
6435 GstRTSPAuthCredential **credentials, **credential;
6437 g_return_if_fail (response != NULL);
6438 g_return_if_fail (methods != NULL);
6439 g_return_if_fail (stale != NULL);
6442 gst_rtsp_message_parse_auth_credentials (response,
6443 GST_RTSP_HDR_WWW_AUTHENTICATE);
6447 credential = credentials;
6448 while (*credential) {
6449 if ((*credential)->scheme == GST_RTSP_AUTH_BASIC) {
6450 *methods |= GST_RTSP_AUTH_BASIC;
6451 } else if ((*credential)->scheme == GST_RTSP_AUTH_DIGEST) {
6452 GstRTSPAuthParam **param = (*credential)->params;
6454 *methods |= GST_RTSP_AUTH_DIGEST;
6456 gst_rtsp_connection_clear_auth_params (conn);
6460 if (strcmp ((*param)->name, "stale") == 0
6461 && g_ascii_strcasecmp ((*param)->value, "TRUE") == 0)
6463 gst_rtsp_connection_set_auth_param (conn, (*param)->name,
6472 gst_rtsp_auth_credentials_free (credentials);
6476 * gst_rtspsrc_setup_auth:
6477 * @src: the rtsp source
6479 * Configure a username and password and auth method on the
6480 * connection object based on a response we received from the
6483 * Currently, this requires that a username and password were supplied
6484 * in the uri. In the future, they may be requested on demand by sending
6485 * a message up the bus.
6487 * Returns: TRUE if authentication information could be set up correctly.
6490 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
6494 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
6495 GstRTSPAuthMethod method;
6496 GstRTSPResult auth_result;
6498 GstRTSPConnection *conn;
6499 gboolean stale = FALSE;
6501 conn = src->conninfo.connection;
6503 /* Identify the available auth methods and see if any are supported */
6504 gst_rtspsrc_parse_auth_hdr (response, &avail_methods, conn, &stale);
6506 if (avail_methods == GST_RTSP_AUTH_NONE)
6507 goto no_auth_available;
6509 /* For digest auth, if the response indicates that the session
6510 * data are stale, we just update them in the connection object and
6511 * return TRUE to retry the request */
6513 src->tried_url_auth = FALSE;
6515 url = gst_rtsp_connection_get_url (conn);
6517 /* Do we have username and password available? */
6518 if (url != NULL && !src->tried_url_auth && url->user != NULL
6519 && url->passwd != NULL) {
6522 src->tried_url_auth = TRUE;
6523 GST_DEBUG_OBJECT (src,
6524 "Attempting authentication using credentials from the URL");
6526 user = src->user_id;
6527 pass = src->user_pw;
6528 GST_DEBUG_OBJECT (src,
6529 "Attempting authentication using credentials from the properties");
6532 /* FIXME: If the url didn't contain username and password or we tried them
6533 * already, request a username and passwd from the application via some kind
6534 * of credentials request message */
6536 /* If we don't have a username and passwd at this point, bail out. */
6537 if (user == NULL || pass == NULL)
6540 /* Try to configure for each available authentication method, strongest to
6542 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
6543 /* Check if this method is available on the server */
6544 if ((method & avail_methods) == 0)
6547 /* Pass the credentials to the connection to try on the next request */
6548 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
6549 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
6550 * ignore it and end up retrying later */
6551 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
6552 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
6553 gst_rtsp_auth_method_to_string (method));
6558 if (method == GST_RTSP_AUTH_NONE)
6559 goto no_auth_available;
6565 /* Output an error indicating that we couldn't connect because there were
6566 * no supported authentication protocols */
6567 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
6568 ("No supported authentication protocol was found"));
6573 /* We don't fire an error message, we just return FALSE and let the
6574 * normal NOT_AUTHORIZED error be propagated */
6579 static GstRTSPResult
6580 gst_rtsp_src_receive_response (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
6581 GstRTSPMessage * response, GstRTSPStatusCode * code)
6583 GstRTSPStatusCode thecode;
6584 gchar *content_base = NULL;
6588 if (conninfo->flushing) {
6589 /* do not attempt to receive if flushing */
6590 res = GST_RTSP_EINTR;
6592 res = gst_rtspsrc_connection_receive (src, conninfo, response,
6599 DEBUG_RTSP (src, response);
6601 switch (response->type) {
6602 case GST_RTSP_MESSAGE_REQUEST:
6603 res = gst_rtspsrc_handle_request (src, conninfo, response);
6604 if (res == GST_RTSP_EEOF)
6607 goto handle_request_failed;
6609 /* Not a response, receive next message */
6611 case GST_RTSP_MESSAGE_RESPONSE:
6612 /* ok, a response is good */
6613 GST_DEBUG_OBJECT (src, "received response message");
6615 case GST_RTSP_MESSAGE_DATA:
6616 /* get next response */
6617 GST_DEBUG_OBJECT (src, "handle data response message");
6618 gst_rtspsrc_handle_data (src, response);
6620 /* Not a response, receive next message */
6623 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
6626 /* Not a response, receive next message */
6630 thecode = response->type_data.response.code;
6632 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
6634 /* if the caller wanted the result code, we store it. */
6638 /* If the request didn't succeed, bail out before doing any more */
6639 if (thecode != GST_RTSP_STS_OK)
6642 /* store new content base if any */
6643 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
6646 g_free (src->content_base);
6647 src->content_base = g_strdup (content_base);
6657 return GST_RTSP_EEOF;
6660 gchar *str = gst_rtsp_strresult (res);
6662 if (res != GST_RTSP_EINTR) {
6663 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6664 ("Could not receive message. (%s)", str));
6666 GST_WARNING_OBJECT (src, "receive interrupted");
6674 handle_request_failed:
6676 /* ERROR was posted */
6677 gst_rtsp_message_unset (response);
6682 GST_DEBUG_OBJECT (src, "we got an eof from the server");
6683 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
6684 ("The server closed the connection."));
6685 gst_rtsp_message_unset (response);
6691 static GstRTSPResult
6692 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
6693 GstRTSPMessage * request, GstRTSPMessage * response,
6694 GstRTSPStatusCode * code)
6698 gboolean allow_send = TRUE;
6701 if (!src->short_header)
6702 gst_rtsp_ext_list_before_send (src->extensions, request);
6704 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_BEFORE_SEND], 0,
6705 request, &allow_send);
6707 GST_DEBUG_OBJECT (src, "skipping message, disabled by signal");
6711 GST_DEBUG_OBJECT (src, "sending message");
6713 DEBUG_RTSP (src, request);
6715 res = gst_rtspsrc_connection_send (src, conninfo, request, src->tcp_timeout);
6719 gst_rtsp_connection_reset_timeout (conninfo->connection);
6723 res = gst_rtsp_src_receive_response (src, conninfo, response, code);
6724 if (res == GST_RTSP_EEOF) {
6725 GST_WARNING_OBJECT (src, "server closed connection");
6726 /* only try once after reconnect, then fallthrough and error out */
6727 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
6729 /* if reconnect succeeds, try again */
6730 if ((res = gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) == 0)
6738 gst_rtsp_ext_list_after_send (src->extensions, request, response);
6744 gchar *str = gst_rtsp_strresult (res);
6746 if (res != GST_RTSP_EINTR) {
6747 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6748 ("Could not send message. (%s)", str));
6750 GST_WARNING_OBJECT (src, "send interrupted");
6758 gchar *str = gst_rtsp_strresult (res);
6760 if (res != GST_RTSP_EINTR) {
6761 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6762 ("Could not receive message. (%s)", str));
6764 GST_WARNING_OBJECT (src, "receive interrupted");
6773 * @src: the rtsp source
6774 * @conninfo: the connection information to send on
6775 * @request: must point to a valid request
6776 * @response: must point to an empty #GstRTSPMessage
6777 * @code: an optional code result
6778 * @versions: List of versions to try, setting it back onto the @request message
6779 * if not set, `src->version` will be used as RTSP version.
6781 * send @request and retrieve the response in @response. optionally @code can be
6782 * non-NULL in which case it will contain the status code of the response.
6784 * If This function returns #GST_RTSP_OK, @response will contain a valid response
6785 * message that should be cleaned with gst_rtsp_message_unset() after usage.
6787 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
6788 * @response message) if the response code was not 200 (OK).
6790 * If the attempt results in an authentication failure, then this will attempt
6791 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
6794 * Returns: #GST_RTSP_OK if the processing was successful.
6796 static GstRTSPResult
6797 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
6798 GstRTSPMessage * request, GstRTSPMessage * response,
6799 GstRTSPStatusCode * code, GstRTSPVersion * versions)
6801 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
6802 GstRTSPResult res = GST_RTSP_ERROR;
6805 GstRTSPMethod method = GST_RTSP_INVALID;
6806 gint version_retry = 0;
6812 /* make sure we don't loop forever */
6816 /* save method so we can disable it when the server complains */
6817 method = request->type_data.request.method;
6820 request->type_data.request.version = src->version;
6823 gst_rtspsrc_try_send (src, conninfo, request, response,
6828 case GST_RTSP_STS_UNAUTHORIZED:
6829 case GST_RTSP_STS_NOT_FOUND:
6830 if (gst_rtspsrc_setup_auth (src, response)) {
6831 /* Try the request/response again after configuring the auth info
6836 case GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED:
6837 GST_INFO_OBJECT (src, "Version %s not supported by the server",
6838 versions ? gst_rtsp_version_as_text (versions[version_retry]) :
6840 if (versions && versions[version_retry] != GST_RTSP_VERSION_INVALID) {
6841 GST_INFO_OBJECT (src, "Unsupported version %s => trying %s",
6842 gst_rtsp_version_as_text (request->type_data.request.version),
6843 gst_rtsp_version_as_text (versions[version_retry]));
6844 request->type_data.request.version = versions[version_retry];
6853 } while (retry == TRUE);
6855 /* If the user requested the code, let them handle errors, otherwise
6856 * post an error below */
6859 else if (int_code != GST_RTSP_STS_OK)
6860 goto error_response;
6867 GST_DEBUG_OBJECT (src, "got error %d", res);
6872 res = GST_RTSP_ERROR;
6874 switch (response->type_data.response.code) {
6875 case GST_RTSP_STS_NOT_FOUND:
6876 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, NOT_FOUND,
6879 case GST_RTSP_STS_UNAUTHORIZED:
6880 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, NOT_AUTHORIZED,
6883 case GST_RTSP_STS_MOVED_PERMANENTLY:
6884 case GST_RTSP_STS_MOVE_TEMPORARILY:
6886 gchar *new_location;
6887 GstRTSPLowerTrans transports;
6889 GST_DEBUG_OBJECT (src, "got redirection");
6890 /* if we don't have a Location Header, we must error */
6891 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
6892 &new_location, 0) < 0)
6895 /* When we receive a redirect result, we go back to the INIT state after
6896 * parsing the new URI. The caller should do the needed steps to issue
6897 * a new setup when it detects this state change. */
6898 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
6900 /* save current transports */
6901 if (src->conninfo.url)
6902 transports = src->conninfo.url->transports;
6904 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
6906 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
6908 /* set old transports */
6909 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
6910 src->conninfo.url->transports = transports;
6912 src->need_redirect = TRUE;
6916 case GST_RTSP_STS_NOT_ACCEPTABLE:
6917 case GST_RTSP_STS_NOT_IMPLEMENTED:
6918 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
6919 /* Some cameras (e.g. HikVision DS-2CD2732F-IS) return "551
6920 * Option not supported" when a command is sent that is not implemented
6921 * (e.g. PAUSE). Instead; it should return "501 Not Implemented".
6923 * This is wrong, as previously, the camera did announce support
6924 * for PAUSE in the OPTIONS.
6926 * In this case, handle the 551 as if it was 501 to avoid throwing
6927 * errors to application level. */
6928 case GST_RTSP_STS_OPTION_NOT_SUPPORTED:
6929 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
6930 gst_rtsp_method_as_text (method));
6931 src->methods &= ~method;
6935 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, READ,
6939 /* if we return ERROR we should unset the response ourselves */
6940 if (res == GST_RTSP_ERROR)
6941 gst_rtsp_message_unset (response);
6947 static GstRTSPResult
6948 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
6949 GstRTSPMessage * response, GstRTSPSrc * src)
6951 return gst_rtspsrc_send (src, &src->conninfo, request, response, NULL, NULL);
6955 /* parse the response and collect all the supported methods. We need this
6956 * information so that we don't try to send an unsupported request to the
6960 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
6962 GstRTSPHeaderField field;
6966 /* reset supported methods */
6969 /* Try Allow Header first */
6970 field = GST_RTSP_HDR_ALLOW;
6973 gst_rtsp_message_get_header (response, field, &respoptions, indx);
6977 src->methods |= gst_rtsp_options_from_text (respoptions);
6983 field = GST_RTSP_HDR_PUBLIC;
6986 gst_rtsp_message_get_header (response, field, &respoptions, indx);
6990 src->methods |= gst_rtsp_options_from_text (respoptions);
6995 if (src->methods == 0) {
6996 /* neither Allow nor Public are required, assume the server supports
6997 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
6999 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
7000 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
7002 /* always assume PLAY, FIXME, extensions should be able to override
7004 src->methods |= GST_RTSP_PLAY;
7005 /* also assume it will support Range */
7006 src->seekable = G_MAXFLOAT;
7008 /* we need describe and setup */
7009 if (!(src->methods & GST_RTSP_DESCRIBE))
7011 if (!(src->methods & GST_RTSP_SETUP))
7019 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
7020 ("Server does not support DESCRIBE."));
7025 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
7026 ("Server does not support SETUP."));
7031 /* masks to be kept in sync with the hardcoded protocol order of preference
7033 static const guint protocol_masks[] = {
7034 GST_RTSP_LOWER_TRANS_UDP,
7035 GST_RTSP_LOWER_TRANS_UDP_MCAST,
7036 GST_RTSP_LOWER_TRANS_TCP,
7040 static GstRTSPResult
7041 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
7042 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
7046 gboolean add_udp_str;
7051 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
7056 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
7058 /* extension listed transports, use those */
7059 if (*transports != NULL)
7062 /* it's the default */
7063 add_udp_str = FALSE;
7065 /* the default RTSP transports */
7066 result = g_string_new ("RTP");
7069 case GST_RTSP_PROFILE_AVP:
7070 g_string_append (result, "/AVP");
7072 case GST_RTSP_PROFILE_SAVP:
7073 g_string_append (result, "/SAVP");
7075 case GST_RTSP_PROFILE_AVPF:
7076 g_string_append (result, "/AVPF");
7078 case GST_RTSP_PROFILE_SAVPF:
7079 g_string_append (result, "/SAVPF");
7085 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
7086 GST_DEBUG_OBJECT (src, "adding UDP unicast");
7088 g_string_append (result, "/UDP");
7089 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
7090 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
7091 GST_DEBUG_OBJECT (src, "adding UDP multicast");
7092 /* we don't have to allocate any UDP ports yet, if the selected transport
7093 * turns out to be multicast we can create them and join the multicast
7094 * group indicated in the transport reply */
7096 g_string_append (result, "/UDP");
7097 g_string_append (result, ";multicast");
7098 if (src->next_port_num != 0) {
7099 if (src->client_port_range.max > 0 &&
7100 src->next_port_num >= src->client_port_range.max)
7103 g_string_append_printf (result, ";client_port=%d-%d",
7104 src->next_port_num, src->next_port_num + 1);
7106 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
7107 GST_DEBUG_OBJECT (src, "adding TCP");
7109 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
7111 *transports = g_string_free (result, FALSE);
7113 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
7120 GST_ERROR ("extension gave error %d", res);
7125 GST_ERROR ("no more ports available");
7126 return GST_RTSP_ERROR;
7130 static GstRTSPResult
7131 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
7132 gint orig_rtpport, gint orig_rtcpport)
7135 gint nr_udp, nr_int;
7137 gint rtpport = 0, rtcpport = 0;
7140 src = stream->parent;
7142 /* find number of placeholders first */
7143 if (strstr (*transports, "%%i2"))
7145 else if (strstr (*transports, "%%i1"))
7150 if (strstr (*transports, "%%u2"))
7152 else if (strstr (*transports, "%%u1"))
7157 if (nr_udp == 0 && nr_int == 0)
7161 if (!orig_rtpport || !orig_rtcpport) {
7162 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
7165 rtpport = orig_rtpport;
7166 rtcpport = orig_rtcpport;
7170 str = g_string_new ("");
7172 while ((next = strstr (p, "%%"))) {
7173 g_string_append_len (str, p, next - p);
7174 if (next[2] == 'u') {
7176 g_string_append_printf (str, "%d", rtpport);
7177 else if (next[3] == '2')
7178 g_string_append_printf (str, "%d", rtcpport);
7180 if (next[2] == 'i') {
7182 g_string_append_printf (str, "%d", src->free_channel);
7183 else if (next[3] == '2')
7184 g_string_append_printf (str, "%d", src->free_channel + 1);
7190 if (src->version >= GST_RTSP_VERSION_2_0)
7191 src->free_channel += 2;
7193 /* append final part */
7194 g_string_append (str, p);
7196 g_free (*transports);
7197 *transports = g_string_free (str, FALSE);
7205 GST_ERROR ("failed to allocate udp ports");
7206 return GST_RTSP_ERROR;
7211 signal_get_srtcp_params (GstRTSPSrc * src, GstRTSPStream * stream)
7213 GstCaps *caps = NULL;
7215 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY], 0,
7219 GST_DEBUG_OBJECT (src, "SRTP parameters received");
7225 default_srtcp_params (void)
7232 guint data_size = GST_ROUND_UP_4 (KEY_SIZE);
7234 /* create a random key */
7235 key_data = g_malloc (data_size);
7236 for (i = 0; i < data_size; i += 4)
7237 GST_WRITE_UINT32_BE (key_data + i, g_random_int ());
7239 buf = gst_buffer_new_wrapped (key_data, KEY_SIZE);
7241 caps = gst_caps_new_simple ("application/x-srtcp",
7242 "srtp-key", GST_TYPE_BUFFER, buf,
7243 "srtp-cipher", G_TYPE_STRING, "aes-128-icm",
7244 "srtp-auth", G_TYPE_STRING, "hmac-sha1-80",
7245 "srtcp-cipher", G_TYPE_STRING, "aes-128-icm",
7246 "srtcp-auth", G_TYPE_STRING, "hmac-sha1-80", NULL);
7248 gst_buffer_unref (buf);
7254 gst_rtspsrc_stream_make_keymgmt (GstRTSPSrc * src, GstRTSPStream * stream)
7256 gchar *base64, *result = NULL;
7257 GstMIKEYMessage *mikey_msg;
7259 stream->srtcpparams = signal_get_srtcp_params (src, stream);
7260 if (stream->srtcpparams == NULL)
7261 stream->srtcpparams = default_srtcp_params ();
7263 mikey_msg = gst_mikey_message_new_from_caps (stream->srtcpparams);
7265 /* add policy '0' for our SSRC */
7266 gst_mikey_message_add_cs_srtp (mikey_msg, 0, stream->send_ssrc, 0);
7268 base64 = gst_mikey_message_base64_encode (mikey_msg);
7269 gst_mikey_message_unref (mikey_msg);
7272 result = gst_sdp_make_keymgmt (stream->conninfo.location, base64);
7280 static GstRTSPResult
7281 gst_rtsp_src_setup_stream_from_response (GstRTSPSrc * src,
7282 GstRTSPStream * stream, GstRTSPMessage * response,
7283 GstRTSPLowerTrans * protocols, gint retry, gint * rtpport, gint * rtcpport)
7285 gchar *resptrans = NULL;
7286 GstRTSPTransport transport = { 0 };
7288 gst_rtsp_message_get_header (response, GST_RTSP_HDR_TRANSPORT, &resptrans, 0);
7290 gst_rtspsrc_stream_free_udp (stream);
7294 /* parse transport, go to next stream on parse error */
7295 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
7296 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
7297 return GST_RTSP_ELAST;
7300 /* update allowed transports for other streams. once the transport of
7301 * one stream has been determined, we make sure that all other streams
7302 * are configured in the same way */
7303 switch (transport.lower_transport) {
7304 case GST_RTSP_LOWER_TRANS_TCP:
7305 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
7307 *protocols = GST_RTSP_LOWER_TRANS_TCP;
7308 src->interleaved = TRUE;
7309 if (src->version < GST_RTSP_VERSION_2_0) {
7310 /* update free channels */
7311 src->free_channel = MAX (transport.interleaved.min, src->free_channel);
7312 src->free_channel = MAX (transport.interleaved.max, src->free_channel);
7313 src->free_channel++;
7316 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
7317 /* only allow multicast for other streams */
7318 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
7320 *protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
7321 /* if the server selected our ports, increment our counters so that
7322 * we select a new port later */
7323 if (src->next_port_num == transport.port.min &&
7324 src->next_port_num + 1 == transport.port.max) {
7325 src->next_port_num += 2;
7328 case GST_RTSP_LOWER_TRANS_UDP:
7329 /* only allow unicast for other streams */
7330 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
7332 *protocols = GST_RTSP_LOWER_TRANS_UDP;
7335 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
7336 transport.lower_transport);
7340 if (!src->interleaved || !retry) {
7341 /* now configure the stream with the selected transport */
7342 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
7343 GST_DEBUG_OBJECT (src,
7344 "could not configure stream %p transport, skipping stream", stream);
7346 } else if (stream->udpsrc[0] && stream->udpsrc[1] && rtpport && rtcpport) {
7347 /* retain the first allocated UDP port pair */
7348 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", rtpport, NULL);
7349 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", rtcpport, NULL);
7352 /* we need to activate at least one stream when we detect activity */
7353 src->need_activate = TRUE;
7355 /* stream is setup now */
7356 stream->setup = TRUE;
7357 stream->waiting_setup_response = FALSE;
7359 if (src->version >= GST_RTSP_VERSION_2_0) {
7360 gchar *prop, *media_properties;
7364 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_MEDIA_PROPERTIES,
7365 &media_properties, 0) != GST_RTSP_OK) {
7366 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7367 ("Error: No MEDIA_PROPERTY header in a SETUP request in RTSP 2.0"
7368 " - this header is mandatory."));
7370 gst_rtsp_message_unset (response);
7371 return GST_RTSP_ERROR;
7374 props = g_strsplit (media_properties, ",", -2);
7375 for (i = 0; props[i]; i++) {
7378 while (*prop == ' ')
7381 if (strstr (prop, "Random-Access")) {
7382 gchar **random_seekable_val = g_strsplit (prop, "=", 2);
7384 if (!random_seekable_val[1])
7385 src->seekable = G_MAXFLOAT;
7387 src->seekable = g_ascii_strtod (random_seekable_val[1], NULL);
7389 g_strfreev (random_seekable_val);
7390 } else if (!g_strcmp0 (prop, "No-Seeking")) {
7391 src->seekable = -1.0;
7392 } else if (!g_strcmp0 (prop, "Beginning-Only")) {
7393 src->seekable = 0.0;
7401 /* clean up our transport struct */
7402 gst_rtsp_transport_init (&transport);
7403 /* clean up used RTSP messages */
7404 gst_rtsp_message_unset (response);
7410 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7411 ("Server did not select transport."));
7413 gst_rtsp_message_unset (response);
7414 return GST_RTSP_ERROR;
7418 static GstRTSPResult
7419 gst_rtspsrc_setup_streams_end (GstRTSPSrc * src, gboolean async)
7422 GstRTSPConnInfo *conninfo;
7424 g_assert (src->version >= GST_RTSP_VERSION_2_0);
7426 conninfo = &src->conninfo;
7427 for (tmp = src->streams; tmp; tmp = tmp->next) {
7428 GstRTSPStream *stream = (GstRTSPStream *) tmp->data;
7429 GstRTSPMessage response = { 0, };
7431 if (!stream->waiting_setup_response)
7434 if (!src->conninfo.connection)
7435 conninfo = &((GstRTSPStream *) tmp->data)->conninfo;
7437 gst_rtsp_src_receive_response (src, conninfo, &response, NULL);
7439 gst_rtsp_src_setup_stream_from_response (src, stream,
7440 &response, NULL, 0, NULL, NULL);
7446 /* Perform the SETUP request for all the streams.
7448 * We ask the server for a specific transport, which initially includes all the
7449 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
7450 * two local UDP ports that we send to the server.
7452 * Once the server replied with a transport, we configure the other streams
7453 * with the same transport.
7455 * In case setup request are not pipelined, this function will also configure the
7456 * stream for the selected transport, * which basically means creating the pipeline.
7457 * Otherwise, the first stream is setup right away from the reply and a
7458 * CMD_FINALIZE_SETUP command is set for the stream pipelines to happen on the
7459 * remaining streams from the RTSP thread.
7461 static GstRTSPResult
7462 gst_rtspsrc_setup_streams_start (GstRTSPSrc * src, gboolean async)
7465 GstRTSPResult res = GST_RTSP_ERROR;
7466 GstRTSPMessage request = { 0 };
7467 GstRTSPMessage response = { 0 };
7468 GstRTSPStream *stream = NULL;
7469 GstRTSPLowerTrans protocols;
7470 GstRTSPStatusCode code;
7471 gboolean unsupported_real = FALSE;
7472 gint rtpport, rtcpport;
7475 gchar *pipelined_request_id = NULL;
7477 if (src->conninfo.connection) {
7478 url = gst_rtsp_connection_get_url (src->conninfo.connection);
7479 /* we initially allow all configured lower transports. based on the URL
7480 * transports and the replies from the server we narrow them down. */
7481 protocols = url->transports & src->cur_protocols;
7484 protocols = src->cur_protocols;
7487 /* In ONVIF mode, we only want to try TCP transport */
7488 if (src->onvif_mode && (protocols & GST_RTSP_LOWER_TRANS_TCP))
7489 protocols = GST_RTSP_LOWER_TRANS_TCP;
7494 /* reset some state */
7495 src->free_channel = 0;
7496 src->interleaved = FALSE;
7497 src->need_activate = FALSE;
7498 /* keep track of next port number, 0 is random */
7499 src->next_port_num = src->client_port_range.min;
7500 rtpport = rtcpport = 0;
7502 if (G_UNLIKELY (src->streams == NULL))
7505 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7506 GstRTSPConnInfo *conninfo;
7509 gboolean tried_non_compliant_url = FALSE;
7514 stream = (GstRTSPStream *) walk->data;
7516 caps = stream_get_caps_for_pt (stream, stream->default_pt);
7518 GST_WARNING_OBJECT (src, "skipping stream %p, no caps", stream);
7522 if (stream->skipped) {
7523 GST_DEBUG_OBJECT (src, "skipping stream %p", stream);
7527 /* see if we need to configure this stream */
7528 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
7529 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
7534 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
7535 stream->id, caps, &selected);
7537 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
7541 /* merge/overwrite global caps */
7546 s = gst_caps_get_structure (caps, 0);
7548 num = gst_structure_n_fields (src->props);
7549 for (j = 0; j < num; j++) {
7553 name = gst_structure_nth_field_name (src->props, j);
7554 val = gst_structure_get_value (src->props, name);
7555 gst_structure_set_value (s, name, val);
7557 GST_DEBUG_OBJECT (src, "copied %s", name);
7561 /* skip setup if we have no URL for it */
7562 if (stream->conninfo.location == NULL) {
7563 GST_WARNING_OBJECT (src, "skipping stream %p, no setup", stream);
7567 if (src->conninfo.connection == NULL) {
7568 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
7569 GST_WARNING_OBJECT (src, "skipping stream %p, failed to connect",
7573 conninfo = &stream->conninfo;
7575 conninfo = &src->conninfo;
7577 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
7578 stream->conninfo.location);
7580 /* if we have a multicast connection, only suggest multicast from now on */
7581 if (stream->is_multicast)
7582 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
7585 /* first selectable protocol */
7586 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
7588 if (!protocol_masks[mask])
7592 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
7593 protocol_masks[mask]);
7594 /* create a string with first transport in line */
7596 res = gst_rtspsrc_create_transports_string (src,
7597 protocols & protocol_masks[mask], stream->profile, &transports);
7598 if (res < 0 || transports == NULL)
7599 goto setup_transport_failed;
7601 if (strlen (transports) == 0) {
7602 g_free (transports);
7603 GST_DEBUG_OBJECT (src, "no transports found");
7608 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
7610 /* replace placeholders with real values, this function will optionally
7611 * allocate UDP ports and other info needed to execute the setup request */
7612 res = gst_rtspsrc_prepare_transports (stream, &transports,
7613 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
7615 g_free (transports);
7616 goto setup_transport_failed;
7619 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
7620 /* create SETUP request */
7622 gst_rtspsrc_init_request (src, &request, GST_RTSP_SETUP,
7623 stream->conninfo.location);
7625 g_free (transports);
7626 goto create_request_failed;
7629 if (src->version >= GST_RTSP_VERSION_2_0) {
7630 if (!pipelined_request_id)
7631 pipelined_request_id = g_strdup_printf ("%d",
7632 g_random_int_range (0, G_MAXINT32));
7634 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_PIPELINED_REQUESTS,
7635 pipelined_request_id);
7636 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT_RANGES,
7637 "npt, clock, smpte, clock");
7640 /* select transport */
7641 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
7643 if (stream->is_backchannel && src->backchannel == BACKCHANNEL_ONVIF)
7644 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
7645 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
7648 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
7649 stream->profile == GST_RTSP_PROFILE_SAVPF) {
7650 hval = gst_rtspsrc_stream_make_keymgmt (src, stream);
7651 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
7654 /* if the user wants a non default RTP packet size we add the blocksize
7656 if (src->rtp_blocksize > 0) {
7657 hval = g_strdup_printf ("%d", src->rtp_blocksize);
7658 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
7662 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
7665 /* handle the code ourselves */
7667 gst_rtspsrc_send (src, conninfo, &request,
7668 pipelined_request_id ? NULL : &response, &code, NULL);
7673 case GST_RTSP_STS_OK:
7675 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
7676 gst_rtsp_message_unset (&request);
7677 gst_rtsp_message_unset (&response);
7678 /* cleanup of leftover transport */
7679 gst_rtspsrc_stream_free_udp (stream);
7680 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
7681 * we might be in this case */
7682 if (stream->container && rtpport && rtcpport && !retry) {
7683 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
7688 /* this transport did not go down well, but we may have others to try
7689 * that we did not send yet, try those and only give up then
7690 * but not without checking for lost cause/extension so we can
7691 * post a nicer/more useful error message later */
7692 if (!unsupported_real)
7693 unsupported_real = stream->is_real;
7694 /* select next available protocol, give up on this stream if none */
7696 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
7698 if (!protocol_masks[mask] || unsupported_real)
7702 case GST_RTSP_STS_BAD_REQUEST:
7703 case GST_RTSP_STS_NOT_FOUND:
7704 /* There are various non-compliant servers that don't require control
7705 * URLs that are not resolved correctly but instead are just appended.
7707 * https://gitlab.freedesktop.org/gstreamer/gst-plugins-good/-/issues/922
7708 * https://gitlab.freedesktop.org/gstreamer/gstreamer/-/issues/1447
7710 if (!tried_non_compliant_url && stream->control_url
7711 && !g_str_has_prefix (stream->control_url, "rtsp://")) {
7714 gst_rtsp_message_unset (&request);
7715 gst_rtsp_message_unset (&response);
7716 gst_rtspsrc_stream_free_udp (stream);
7718 g_free (stream->conninfo.location);
7719 base = get_aggregate_control (src);
7721 /* Make sure to not accumulate too many `/` */
7722 if ((g_str_has_suffix (base, "/")
7723 && !g_str_has_suffix (stream->control_url, "/"))
7724 || (!g_str_has_suffix (base, "/")
7725 && g_str_has_suffix (stream->control_url, "/"))
7727 stream->conninfo.location =
7728 g_strconcat (base, stream->control_url, NULL);
7729 else if (g_str_has_suffix (base, "/")
7730 && g_str_has_suffix (stream->control_url, "/"))
7731 stream->conninfo.location =
7732 g_strconcat (base, stream->control_url + 1, NULL);
7734 stream->conninfo.location =
7735 g_strconcat (base, "/", stream->control_url, NULL);
7737 tried_non_compliant_url = TRUE;
7744 /* cleanup of leftover transport and move to the next stream */
7745 gst_rtspsrc_stream_free_udp (stream);
7746 goto response_error;
7750 if (!pipelined_request_id) {
7751 /* parse response transport */
7752 res = gst_rtsp_src_setup_stream_from_response (src, stream,
7753 &response, &protocols, retry, &rtpport, &rtcpport);
7755 case GST_RTSP_ERROR:
7757 case GST_RTSP_ELAST:
7763 stream->waiting_setup_response = TRUE;
7764 /* we need to activate at least one stream when we detect activity */
7765 src->need_activate = TRUE;
7772 GstRTSPStream *sskip;
7774 skip = g_list_next (skip);
7778 sskip = (GstRTSPStream *) skip->data;
7780 /* skip all streams with the same control url */
7781 if (g_str_equal (stream->conninfo.location, sskip->conninfo.location)) {
7782 GST_DEBUG_OBJECT (src, "found stream %p with same control %s",
7783 sskip, sskip->conninfo.location);
7784 sskip->skipped = TRUE;
7788 gst_rtsp_message_unset (&request);
7791 if (pipelined_request_id) {
7792 gst_rtspsrc_setup_streams_end (src, TRUE);
7795 /* store the transport protocol that was configured */
7796 src->cur_protocols = protocols;
7798 gst_rtsp_ext_list_stream_select (src->extensions, url);
7800 if (pipelined_request_id)
7801 g_free (pipelined_request_id);
7803 /* if there is nothing to activate, error out */
7804 if (!src->need_activate)
7805 goto nothing_to_activate;
7812 /* no transport possible, post an error and stop */
7813 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
7814 ("Could not connect to server, no protocols left"));
7815 return GST_RTSP_ERROR;
7819 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7820 ("SDP contains no streams"));
7821 return GST_RTSP_ERROR;
7823 create_request_failed:
7825 gchar *str = gst_rtsp_strresult (res);
7827 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7828 ("Could not create request. (%s)", str));
7832 setup_transport_failed:
7834 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7835 ("Could not setup transport."));
7836 res = GST_RTSP_ERROR;
7841 const gchar *str = gst_rtsp_status_as_text (code);
7843 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7844 ("Error (%d): %s", code, GST_STR_NULL (str)));
7845 res = GST_RTSP_ERROR;
7850 gchar *str = gst_rtsp_strresult (res);
7852 if (res != GST_RTSP_EINTR) {
7853 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7854 ("Could not send message. (%s)", str));
7856 GST_WARNING_OBJECT (src, "send interrupted");
7861 nothing_to_activate:
7863 /* none of the available error codes is really right .. */
7864 if (unsupported_real) {
7865 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
7866 (_("No supported stream was found. You might need to install a "
7867 "GStreamer RTSP extension plugin for Real media streams.")),
7870 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
7871 (_("No supported stream was found. You might need to allow "
7872 "more transport protocols or may otherwise be missing "
7873 "the right GStreamer RTSP extension plugin.")), (NULL));
7875 return GST_RTSP_ERROR;
7879 if (pipelined_request_id)
7880 g_free (pipelined_request_id);
7881 gst_rtsp_message_unset (&request);
7882 gst_rtsp_message_unset (&response);
7888 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
7889 GstSegment * segment, gboolean update_duration)
7891 GstClockTime begin_seconds, end_seconds;
7893 GstRTSPTimeRange *therange;
7896 gst_rtsp_range_free (src->range);
7898 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
7899 GST_DEBUG_OBJECT (src, "parsed range %s", range);
7900 src->range = therange;
7902 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
7904 gst_segment_init (segment, GST_FORMAT_TIME);
7908 gst_rtsp_range_get_times (therange, &begin_seconds, &end_seconds);
7910 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
7911 therange->min.type, therange->min.seconds, therange->max.type,
7912 therange->max.seconds);
7914 if (therange->min.type == GST_RTSP_TIME_NOW)
7916 else if (therange->min.type == GST_RTSP_TIME_END)
7919 seconds = begin_seconds;
7921 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
7922 GST_TIME_ARGS (seconds));
7924 /* we need to start playback without clipping from the position reported by
7926 if (segment->rate > 0.0)
7927 segment->start = seconds;
7929 segment->stop = seconds;
7931 segment->position = seconds;
7933 if (therange->max.type == GST_RTSP_TIME_NOW)
7935 else if (therange->max.type == GST_RTSP_TIME_END)
7938 seconds = end_seconds;
7940 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
7941 GST_TIME_ARGS (seconds));
7943 /* live (WMS) server might send overflowed large max as its idea of infinity,
7944 * compensate to prevent problems later on */
7945 if (seconds != -1 && seconds < 0) {
7947 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
7950 /* live (WMS) might send min == max, which is not worth recording */
7951 if (segment->duration == -1 && seconds == begin_seconds)
7954 /* don't change duration with unknown value, we might have a valid value
7955 * there that we want to keep. Also, the total duration of the stream
7956 * can only be determined from the response to a DESCRIBE request, not
7957 * from a PLAY request where we might have requested a custom range, so
7958 * don't update duration in that case */
7959 if (update_duration && seconds != -1) {
7960 segment->duration = seconds;
7961 GST_DEBUG_OBJECT (src, "set duration from range as %" GST_TIME_FORMAT,
7962 GST_TIME_ARGS (seconds));
7964 GST_DEBUG_OBJECT (src, "not updating existing duration %" GST_TIME_FORMAT
7965 " from range %" GST_TIME_FORMAT, GST_TIME_ARGS (segment->duration),
7966 GST_TIME_ARGS (seconds));
7969 if (segment->rate > 0.0)
7970 segment->stop = seconds;
7972 segment->start = seconds;
7977 /* Parse clock profived by the server with following syntax:
7979 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
7982 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
7984 gboolean res = FALSE;
7986 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
7987 gchar **fields = NULL, **parts = NULL;
7988 gchar *remote_ip, *str;
7990 GstClockTime base_time;
7993 fields = g_strsplit (gstclock, " ", 0);
7995 /* wrapped clock, not very interesting for now */
7996 if (fields[1] == NULL)
7999 /* remote IP address and port */
8000 if ((str = fields[2]) == NULL)
8003 parts = g_strsplit (str, ":", 0);
8005 if ((remote_ip = parts[0]) == NULL)
8008 if ((str = parts[1]) == NULL)
8016 if ((str = fields[3]) == NULL)
8019 base_time = g_ascii_strtoull (str, NULL, 10);
8022 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
8025 if (src->provided_clock)
8026 gst_object_unref (src->provided_clock);
8027 src->provided_clock = netclock;
8029 gst_element_post_message (GST_ELEMENT_CAST (src),
8030 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
8031 src->provided_clock, TRUE));
8035 g_strfreev (fields);
8041 /* must be called with the RTSP state lock */
8042 static GstRTSPResult
8043 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
8049 /* prepare global stream caps properties */
8051 gst_structure_remove_all_fields (src->props);
8053 src->props = gst_structure_new_empty ("RTSPProperties");
8055 DEBUG_SDP (src, sdp);
8057 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
8059 /* let the app inspect and change the SDP */
8060 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
8062 gst_segment_init (&src->segment, GST_FORMAT_TIME);
8064 /* parse range for duration reporting. */
8069 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
8073 /* keep track of the range and configure it in the segment */
8074 if (gst_rtspsrc_parse_range (src, range, &src->segment, TRUE))
8078 /* parse clock information. This is GStreamer specific, a server can tell the
8079 * client what clock it is using and wrap that in a network clock. The
8080 * advantage of that is that we can slave to it. */
8082 const gchar *gstclock;
8085 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
8086 if (gstclock == NULL)
8089 /* parse the clock and expose it in the provide_clock method */
8090 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
8094 /* try to find a global control attribute. Note that a '*' means that we should
8095 * do aggregate control with the current url (so we don't do anything and
8096 * leave the current connection as is) */
8098 const gchar *control;
8101 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
8102 if (control == NULL)
8105 /* only take fully qualified urls */
8106 if (g_str_has_prefix (control, "rtsp://"))
8110 g_free (src->conninfo.location);
8111 src->conninfo.location = g_strdup (control);
8112 /* make a connection for this, if there was a connection already, nothing
8114 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
8115 GST_ERROR_OBJECT (src, "could not connect");
8118 /* we need to keep the control url separate from the connection url because
8119 * the rules for constructing the media control url need it */
8120 g_free (src->control);
8121 src->control = g_strdup (control);
8124 /* create streams */
8125 n_streams = gst_sdp_message_medias_len (sdp);
8126 for (i = 0; i < n_streams; i++) {
8127 gst_rtspsrc_create_stream (src, sdp, i, n_streams);
8130 src->state = GST_RTSP_STATE_INIT;
8133 if ((res = gst_rtspsrc_setup_streams_start (src, async)) < 0)
8136 /* reset our state */
8137 src->need_range = TRUE;
8138 src->server_side_trickmode = FALSE;
8139 src->trickmode_interval = 0;
8141 src->state = GST_RTSP_STATE_READY;
8148 GST_ERROR_OBJECT (src, "setup failed");
8149 gst_rtspsrc_cleanup (src);
8154 static GstRTSPResult
8155 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
8159 GstRTSPMessage request = { 0 };
8160 GstRTSPMessage response = { 0 };
8163 gchar *respcont = NULL;
8164 GstRTSPVersion versions[] =
8165 { GST_RTSP_VERSION_2_0, GST_RTSP_VERSION_INVALID };
8167 src->version = src->default_version;
8168 if (src->default_version == GST_RTSP_VERSION_2_0) {
8169 versions[0] = GST_RTSP_VERSION_1_0;
8173 src->need_redirect = FALSE;
8175 /* can't continue without a valid url */
8176 if (G_UNLIKELY (src->conninfo.url == NULL)) {
8177 res = GST_RTSP_EINVAL;
8180 src->tried_url_auth = FALSE;
8182 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
8183 goto connect_failed;
8185 /* create OPTIONS */
8186 GST_DEBUG_OBJECT (src, "create options... (%s)", async ? "async" : "sync");
8188 gst_rtspsrc_init_request (src, &request, GST_RTSP_OPTIONS,
8189 src->conninfo.url_str);
8191 goto create_request_failed;
8194 request.type_data.request.version = src->version;
8195 GST_DEBUG_OBJECT (src, "send options...");
8198 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
8201 gst_rtspsrc_send (src, &src->conninfo, &request, &response,
8202 NULL, versions)) < 0) {
8206 src->version = request.type_data.request.version;
8207 GST_INFO_OBJECT (src, "Now using version: %s",
8208 gst_rtsp_version_as_text (src->version));
8211 if (!gst_rtspsrc_parse_methods (src, &response))
8214 /* create DESCRIBE */
8215 GST_DEBUG_OBJECT (src, "create describe...");
8217 gst_rtspsrc_init_request (src, &request, GST_RTSP_DESCRIBE,
8218 src->conninfo.url_str);
8220 goto create_request_failed;
8222 /* we only accept SDP for now */
8223 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
8226 if (src->backchannel == BACKCHANNEL_ONVIF)
8227 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
8228 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
8229 /* TODO: Handle the case when backchannel is unsupported and goto restart */
8232 GST_DEBUG_OBJECT (src, "send describe...");
8235 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
8238 gst_rtspsrc_send (src, &src->conninfo, &request, &response,
8242 /* we only perform redirect for describe and play, currently */
8243 if (src->need_redirect) {
8244 /* close connection, we don't have to send a TEARDOWN yet, ignore the
8246 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
8248 gst_rtsp_message_unset (&request);
8249 gst_rtsp_message_unset (&response);
8255 /* it could be that the DESCRIBE method was not implemented */
8256 if (!(src->methods & GST_RTSP_DESCRIBE))
8259 /* check if reply is SDP */
8260 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
8262 /* could not be set but since the request returned OK, we assume it
8263 * was SDP, else check it. */
8265 const gchar *props = strchr (respcont, ';');
8268 gchar *mimetype = g_strndup (respcont, props - respcont);
8270 mimetype = g_strstrip (mimetype);
8271 if (g_ascii_strcasecmp (mimetype, "application/sdp") != 0) {
8273 goto wrong_content_type;
8276 /* TODO: Check for charset property and do conversions of all messages if
8277 * needed. Some servers actually send that property */
8280 } else if (g_ascii_strcasecmp (respcont, "application/sdp") != 0) {
8281 goto wrong_content_type;
8285 /* get message body and parse as SDP */
8286 gst_rtsp_message_get_body (&response, &data, &size);
8287 if (data == NULL || size == 0)
8290 GST_DEBUG_OBJECT (src, "parse SDP...");
8291 gst_sdp_message_new (sdp);
8292 gst_sdp_message_parse_buffer (data, size, *sdp);
8294 /* clean up any messages */
8295 gst_rtsp_message_unset (&request);
8296 gst_rtsp_message_unset (&response);
8303 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
8304 ("No valid RTSP URL was provided"));
8309 gchar *str = gst_rtsp_strresult (res);
8311 if (res != GST_RTSP_EINTR) {
8312 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
8313 ("Failed to connect. (%s)", str));
8315 GST_WARNING_OBJECT (src, "connect interrupted");
8320 create_request_failed:
8322 gchar *str = gst_rtsp_strresult (res);
8324 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
8325 ("Could not create request. (%s)", str));
8331 /* Don't post a message - the rtsp_send method will have
8332 * taken care of it because we passed NULL for the response code */
8337 /* error was posted */
8338 res = GST_RTSP_ERROR;
8343 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
8344 ("Server does not support SDP, got %s.", respcont));
8345 res = GST_RTSP_ERROR;
8350 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
8351 ("Server can not provide an SDP."));
8352 res = GST_RTSP_ERROR;
8357 if (src->conninfo.connection) {
8358 GST_DEBUG_OBJECT (src, "free connection");
8359 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
8361 gst_rtsp_message_unset (&request);
8362 gst_rtsp_message_unset (&response);
8367 static GstRTSPResult
8368 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
8373 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
8375 if (src->sdp == NULL) {
8376 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
8380 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
8383 if (src->initial_seek) {
8384 if (!gst_rtspsrc_perform_seek (src, src->initial_seek))
8385 goto initial_seek_failed;
8386 gst_event_replace (&src->initial_seek, NULL);
8391 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
8398 GST_WARNING_OBJECT (src, "can't get sdp");
8399 src->open_error = TRUE;
8404 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
8405 src->open_error = TRUE;
8408 initial_seek_failed:
8410 GST_WARNING_OBJECT (src, "Failed to perform initial seek");
8411 ret = GST_RTSP_ERROR;
8412 src->open_error = TRUE;
8417 static GstRTSPResult
8418 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
8420 GstRTSPMessage request = { 0 };
8421 GstRTSPMessage response = { 0 };
8422 GstRTSPResult res = GST_RTSP_OK;
8424 const gchar *control;
8426 GST_DEBUG_OBJECT (src, "TEARDOWN...");
8428 gst_rtspsrc_set_state (src, GST_STATE_READY);
8430 if (src->state < GST_RTSP_STATE_READY) {
8431 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
8438 /* construct a control url */
8439 control = get_aggregate_control (src);
8441 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
8444 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8445 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8446 const gchar *setup_url;
8447 GstRTSPConnInfo *info;
8449 /* try aggregate control first but do non-aggregate control otherwise */
8451 setup_url = control;
8452 else if ((setup_url = stream->conninfo.location) == NULL)
8455 if (src->conninfo.connection) {
8456 info = &src->conninfo;
8457 } else if (stream->conninfo.connection) {
8458 info = &stream->conninfo;
8462 if (!info->connected)
8467 gst_rtspsrc_init_request (src, &request, GST_RTSP_TEARDOWN, setup_url);
8468 GST_LOG_OBJECT (src, "Teardown on %s", setup_url);
8470 goto create_request_failed;
8472 if (stream->is_backchannel && src->backchannel == BACKCHANNEL_ONVIF)
8473 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
8474 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
8477 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
8480 gst_rtspsrc_send (src, info, &request, &response, NULL, NULL)) < 0)
8483 /* FIXME, parse result? */
8484 gst_rtsp_message_unset (&request);
8485 gst_rtsp_message_unset (&response);
8488 /* early exit when we did aggregate control */
8494 /* close connections */
8495 GST_DEBUG_OBJECT (src, "closing connection...");
8496 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
8497 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8498 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8499 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
8503 gst_rtspsrc_cleanup (src);
8505 src->state = GST_RTSP_STATE_INVALID;
8508 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
8513 create_request_failed:
8515 gchar *str = gst_rtsp_strresult (res);
8517 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
8518 ("Could not create request. (%s)", str));
8524 gchar *str = gst_rtsp_strresult (res);
8526 gst_rtsp_message_unset (&request);
8527 if (res != GST_RTSP_EINTR) {
8528 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
8529 ("Could not send message. (%s)", str));
8531 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
8538 GST_DEBUG_OBJECT (src,
8539 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
8544 /* RTP-Info is of the format:
8546 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
8548 * rtptime corresponds to the timestamp for the NPT time given in the header
8549 * seqbase corresponds to the next sequence number we received. This number
8550 * indicates the first seqnum after the seek and should be used to discard
8551 * packets that are from before the seek.
8554 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
8559 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
8561 infos = g_strsplit (rtpinfo, ",", 0);
8562 for (i = 0; infos[i]; i++) {
8564 GstRTSPStream *stream;
8568 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
8570 /* init values, types of seqbase and timebase are bigger than needed so we
8571 * can store -1 as uninitialized values */
8576 /* parse url, find stream for url.
8577 * parse seq and rtptime. The seq number should be configured in the rtp
8578 * depayloader or session manager to detect gaps. Same for the rtptime, it
8579 * should be used to create an initial time newsegment. */
8580 fields = g_strsplit (infos[i], ";", 0);
8581 for (j = 0; fields[j]; j++) {
8582 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
8583 /* remove leading whitespace */
8584 fields[j] = g_strchug (fields[j]);
8585 if (g_str_has_prefix (fields[j], "url=")) {
8586 /* get the url and the stream */
8588 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
8589 } else if (g_str_has_prefix (fields[j], "seq=")) {
8590 seqbase = atoi (fields[j] + 4);
8591 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
8592 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
8595 g_strfreev (fields);
8596 /* now we need to store the values for the caps of the stream */
8597 if (stream != NULL) {
8598 GST_DEBUG_OBJECT (src,
8599 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
8600 stream, seqbase, timebase);
8602 /* we have a stream, configure detected params */
8603 stream->seqbase = seqbase;
8604 stream->timebase = timebase;
8613 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
8618 interval = strtoul (rtcp, NULL, 10);
8619 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
8624 interval *= GST_MSECOND;
8626 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8627 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8629 /* already (optionally) retrieved this when configuring manager */
8630 if (stream->session) {
8631 GObject *rtpsession = stream->session;
8633 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
8635 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
8639 /* now it happens that (Xenon) server sending this may also provide bogus
8640 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
8641 * and just use RTP-Info to sync */
8643 GObjectClass *klass;
8645 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
8646 if (g_object_class_find_property (klass, "rtcp-sync")) {
8647 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
8648 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
8654 gst_rtspsrc_get_float (const gchar * dstr)
8656 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
8658 /* canonicalise floating point string so we can handle float strings
8659 * in the form "24.930" or "24,930" irrespective of the current locale */
8660 g_strlcpy (s, dstr, sizeof (s));
8661 g_strdelimit (s, ",", '.');
8662 return g_ascii_strtod (s, NULL);
8666 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
8668 GstRTSPTimeRange range = { 0, };
8669 gdouble begin_seconds, end_seconds;
8671 if (segment->rate > 0) {
8672 begin_seconds = (gdouble) segment->start / GST_SECOND;
8673 end_seconds = (gdouble) segment->stop / GST_SECOND;
8675 begin_seconds = (gdouble) segment->stop / GST_SECOND;
8676 end_seconds = (gdouble) segment->start / GST_SECOND;
8679 if (src->onvif_mode) {
8680 GDateTime *prime_epoch, *datetime;
8682 range.unit = GST_RTSP_RANGE_CLOCK;
8684 prime_epoch = g_date_time_new_utc (1900, 1, 1, 0, 0, 0);
8686 datetime = g_date_time_add_seconds (prime_epoch, begin_seconds);
8688 range.min.type = GST_RTSP_TIME_UTC;
8689 range.min2.year = g_date_time_get_year (datetime);
8690 range.min2.month = g_date_time_get_month (datetime);
8691 range.min2.day = g_date_time_get_day_of_month (datetime);
8693 g_date_time_get_seconds (datetime) +
8694 g_date_time_get_minute (datetime) * 60 +
8695 g_date_time_get_hour (datetime) * 60 * 60;
8697 g_date_time_unref (datetime);
8699 datetime = g_date_time_add_seconds (prime_epoch, end_seconds);
8701 range.max.type = GST_RTSP_TIME_UTC;
8702 range.max2.year = g_date_time_get_year (datetime);
8703 range.max2.month = g_date_time_get_month (datetime);
8704 range.max2.day = g_date_time_get_day_of_month (datetime);
8706 g_date_time_get_seconds (datetime) +
8707 g_date_time_get_minute (datetime) * 60 +
8708 g_date_time_get_hour (datetime) * 60 * 60;
8710 g_date_time_unref (datetime);
8711 g_date_time_unref (prime_epoch);
8713 range.unit = GST_RTSP_RANGE_NPT;
8715 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
8716 range.min.type = GST_RTSP_TIME_NOW;
8718 range.min.type = GST_RTSP_TIME_SECONDS;
8719 range.min.seconds = begin_seconds;
8722 if (src->range && src->range->max.type == GST_RTSP_TIME_END) {
8723 range.max.type = GST_RTSP_TIME_END;
8725 range.max.type = GST_RTSP_TIME_SECONDS;
8726 range.max.seconds = end_seconds;
8730 /* Don't set end bounds when not required to */
8731 if (!GST_CLOCK_TIME_IS_VALID (segment->stop)) {
8732 if (segment->rate > 0)
8733 range.max.type = GST_RTSP_TIME_END;
8735 range.min.type = GST_RTSP_TIME_END;
8738 return gst_rtsp_range_to_string (&range);
8742 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
8746 stream->timebase = -1;
8747 stream->seqbase = -1;
8749 len = stream->ptmap->len;
8750 for (i = 0; i < len; i++) {
8751 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
8754 if (item->caps == NULL)
8757 item->caps = gst_caps_make_writable (item->caps);
8758 s = gst_caps_get_structure (item->caps, 0);
8759 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
8760 if (item->pt == stream->default_pt && stream->udpsrc[0])
8761 g_object_set (stream->udpsrc[0], "caps", item->caps, NULL);
8763 stream->need_caps = TRUE;
8766 static GstRTSPResult
8767 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
8769 GstRTSPResult res = GST_RTSP_OK;
8771 if (src->state < GST_RTSP_STATE_READY) {
8772 res = GST_RTSP_ERROR;
8773 if (src->open_error) {
8774 GST_DEBUG_OBJECT (src, "the stream was in error");
8778 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
8780 if ((res = gst_rtspsrc_open (src, async)) < 0) {
8781 GST_DEBUG_OBJECT (src, "failed to open stream");
8790 static GstRTSPResult
8791 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async,
8792 const gchar * seek_style)
8794 GstRTSPMessage request = { 0 };
8795 GstRTSPMessage response = { 0 };
8796 GstRTSPResult res = GST_RTSP_OK;
8800 const gchar *control;
8801 GstSegment requested;
8803 GST_DEBUG_OBJECT (src, "PLAY...");
8806 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
8809 if (!(src->methods & GST_RTSP_PLAY))
8812 if (src->state == GST_RTSP_STATE_PLAYING)
8815 if (!src->conninfo.connection || !src->conninfo.connected)
8818 requested = *segment;
8820 /* send some dummy packets before we activate the receive in the
8822 gst_rtspsrc_send_dummy_packets (src);
8824 /* require new SR packets */
8826 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
8828 /* construct a control url */
8829 control = get_aggregate_control (src);
8831 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8832 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8833 const gchar *setup_url;
8834 GstRTSPConnInfo *conninfo;
8836 /* try aggregate control first but do non-aggregate control otherwise */
8838 setup_url = control;
8839 else if ((setup_url = stream->conninfo.location) == NULL)
8842 if (src->conninfo.connection) {
8843 conninfo = &src->conninfo;
8844 } else if (stream->conninfo.connection) {
8845 conninfo = &stream->conninfo;
8851 res = gst_rtspsrc_init_request (src, &request, GST_RTSP_PLAY, setup_url);
8853 goto create_request_failed;
8855 if (src->need_range && src->seekable >= 0.0) {
8856 hval = gen_range_header (src, segment);
8858 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
8860 /* store the newsegment event so it can be sent from the streaming thread. */
8861 src->need_segment = TRUE;
8864 if (segment->rate != 1.0) {
8865 gchar scale_val[G_ASCII_DTOSTR_BUF_SIZE];
8866 gchar speed_val[G_ASCII_DTOSTR_BUF_SIZE];
8868 if (src->server_side_trickmode) {
8869 g_ascii_dtostr (scale_val, sizeof (scale_val), segment->rate);
8870 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, scale_val);
8871 } else if (segment->rate < 0.0) {
8872 g_ascii_dtostr (scale_val, sizeof (scale_val), -1.0);
8873 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, scale_val);
8875 if (ABS (segment->rate) != 1.0) {
8876 g_ascii_dtostr (speed_val, sizeof (speed_val), ABS (segment->rate));
8877 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, speed_val);
8880 g_ascii_dtostr (speed_val, sizeof (speed_val), segment->rate);
8881 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, speed_val);
8885 if (src->onvif_mode) {
8886 if (segment->flags & GST_SEEK_FLAG_TRICKMODE_KEY_UNITS) {
8889 if (src->trickmode_interval)
8891 g_strdup_printf ("intra/%" G_GUINT64_FORMAT,
8892 src->trickmode_interval / GST_MSECOND);
8894 hval = g_strdup ("intra");
8896 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_FRAMES, hval);
8899 } else if (segment->flags & GST_SEEK_FLAG_TRICKMODE_FORWARD_PREDICTED) {
8900 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_FRAMES,
8906 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SEEK_STYLE,
8909 /* when we have an ONVIF audio backchannel, the PLAY request must have the
8910 * Require: header when doing either aggregate or non-aggregate control */
8911 if (src->backchannel == BACKCHANNEL_ONVIF &&
8912 (control || stream->is_backchannel))
8913 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
8914 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
8916 if (src->onvif_mode) {
8917 if (src->onvif_rate_control)
8918 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_RATE_CONTROL,
8921 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_RATE_CONTROL, "no");
8925 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
8928 gst_rtspsrc_send (src, conninfo, &request, &response, NULL, NULL))
8932 if (src->need_redirect) {
8933 GST_DEBUG_OBJECT (src,
8934 "redirect: tearing down and restarting with new url");
8935 /* teardown and restart with new url */
8936 gst_rtspsrc_close (src, TRUE, FALSE);
8937 /* reset protocols to force re-negotiation with redirected url */
8938 src->cur_protocols = src->protocols;
8939 gst_rtsp_message_unset (&request);
8940 gst_rtsp_message_unset (&response);
8944 /* seek may have silently failed as it is not supported */
8945 if (!(src->methods & GST_RTSP_PLAY)) {
8946 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
8948 if (src->version >= GST_RTSP_VERSION_2_0 && src->seekable >= 0.0) {
8949 GST_WARNING_OBJECT (src, "Server declared stream as seekable but"
8950 " playing with range failed... Ignoring information.");
8952 /* obviously it is supported as we made it here */
8953 src->methods |= GST_RTSP_PLAY;
8954 src->seekable = -1.0;
8955 /* but there is nothing to parse in the response,
8956 * so convey we have no idea and not to expect anything particular */
8957 clear_rtp_base (src, stream);
8961 /* need to do for all streams */
8962 for (run = src->streams; run; run = g_list_next (run))
8963 clear_rtp_base (src, (GstRTSPStream *) run->data);
8965 /* NOTE the above also disables npt based eos detection */
8966 /* and below forces position to 0,
8967 * which is visible feedback we lost the plot */
8968 segment->start = segment->position = src->last_pos;
8971 gst_rtsp_message_unset (&request);
8973 /* parse RTP npt field. This is the current position in the stream (Normal
8974 * Play Time) and should be put in the NEWSEGMENT position field. */
8975 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
8977 gst_rtspsrc_parse_range (src, hval, segment, FALSE);
8979 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
8980 segment->rate = 1.0;
8982 /* parse Speed header. This is the intended playback rate of the stream
8983 * and should be put in the NEWSEGMENT rate field. */
8984 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
8985 0) == GST_RTSP_OK) {
8986 segment->rate = gst_rtspsrc_get_float (hval);
8987 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
8988 &hval, 0) == GST_RTSP_OK) {
8989 segment->rate = gst_rtspsrc_get_float (hval);
8992 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
8993 * for the RTP packets. If this is not present, we assume all starts from 0...
8994 * This is info for the RTP session manager that we pass to it in caps. */
8996 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
8997 &hval, hval_idx++) == GST_RTSP_OK)
8998 gst_rtspsrc_parse_rtpinfo (src, hval);
9000 /* some servers indicate RTCP parameters in PLAY response,
9001 * rather than properly in SDP */
9002 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
9003 &hval, 0) == GST_RTSP_OK)
9004 gst_rtspsrc_handle_rtcp_interval (src, hval);
9006 gst_rtsp_message_unset (&response);
9008 /* early exit when we did aggregate control */
9013 src->out_segment = *segment;
9015 if (src->clip_out_segment) {
9016 /* Only clip the output segment when the server has answered with valid
9017 * values, we cannot know otherwise whether the requested bounds were
9019 if (GST_CLOCK_TIME_IS_VALID (src->segment.start) &&
9020 GST_CLOCK_TIME_IS_VALID (requested.start))
9021 src->out_segment.start = MAX (src->out_segment.start, requested.start);
9022 if (GST_CLOCK_TIME_IS_VALID (src->segment.stop) &&
9023 GST_CLOCK_TIME_IS_VALID (requested.stop))
9024 src->out_segment.stop = MIN (src->out_segment.stop, requested.stop);
9027 /* configure the caps of the streams after we parsed all headers. Only reset
9028 * the manager object when we set a new Range header (we did a seek) */
9029 gst_rtspsrc_configure_caps (src, segment, src->need_range);
9031 /* set to PLAYING after we have configured the caps, otherwise we
9032 * might end up calling request_key (with SRTP) while caps are still
9033 * being configured. */
9034 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
9036 /* set again when needed */
9037 src->need_range = FALSE;
9039 src->running = TRUE;
9040 src->base_time = -1;
9041 src->state = GST_RTSP_STATE_PLAYING;
9044 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
9045 for (walk = src->streams; walk; walk = g_list_next (walk)) {
9046 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
9047 stream->discont = TRUE;
9052 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
9059 GST_WARNING_OBJECT (src, "failed to open stream");
9064 GST_WARNING_OBJECT (src, "PLAY is not supported");
9069 GST_WARNING_OBJECT (src, "we were already PLAYING");
9072 create_request_failed:
9074 gchar *str = gst_rtsp_strresult (res);
9076 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
9077 ("Could not create request. (%s)", str));
9083 gchar *str = gst_rtsp_strresult (res);
9085 gst_rtsp_message_unset (&request);
9086 if (res != GST_RTSP_EINTR) {
9087 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
9088 ("Could not send message. (%s)", str));
9090 GST_WARNING_OBJECT (src, "PLAY interrupted");
9097 static GstRTSPResult
9098 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
9100 GstRTSPResult res = GST_RTSP_OK;
9101 GstRTSPMessage request = { 0 };
9102 GstRTSPMessage response = { 0 };
9104 const gchar *control;
9106 GST_DEBUG_OBJECT (src, "PAUSE...");
9108 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
9111 if (!(src->methods & GST_RTSP_PAUSE))
9114 if (src->state == GST_RTSP_STATE_READY)
9117 if (!src->conninfo.connection || !src->conninfo.connected)
9120 /* construct a control url */
9121 control = get_aggregate_control (src);
9123 /* loop over the streams. We might exit the loop early when we could do an
9124 * aggregate control */
9125 for (walk = src->streams; walk; walk = g_list_next (walk)) {
9126 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
9127 GstRTSPConnInfo *conninfo;
9128 const gchar *setup_url;
9130 /* try aggregate control first but do non-aggregate control otherwise */
9132 setup_url = control;
9133 else if ((setup_url = stream->conninfo.location) == NULL)
9136 if (src->conninfo.connection) {
9137 conninfo = &src->conninfo;
9138 } else if (stream->conninfo.connection) {
9139 conninfo = &stream->conninfo;
9145 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
9146 ("Sending PAUSE request"));
9149 gst_rtspsrc_init_request (src, &request, GST_RTSP_PAUSE,
9151 goto create_request_failed;
9153 /* when we have an ONVIF audio backchannel, the PAUSE request must have the
9154 * Require: header when doing either aggregate or non-aggregate control */
9155 if (src->backchannel == BACKCHANNEL_ONVIF &&
9156 (control || stream->is_backchannel))
9157 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
9158 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
9161 gst_rtspsrc_send (src, conninfo, &request, &response, NULL,
9165 gst_rtsp_message_unset (&request);
9166 gst_rtsp_message_unset (&response);
9168 /* exit early when we did aggregate control */
9173 /* change element states now */
9174 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
9177 src->state = GST_RTSP_STATE_READY;
9181 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
9188 GST_DEBUG_OBJECT (src, "failed to open stream");
9193 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
9198 GST_DEBUG_OBJECT (src, "we were already PAUSED");
9201 create_request_failed:
9203 gchar *str = gst_rtsp_strresult (res);
9205 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
9206 ("Could not create request. (%s)", str));
9212 gchar *str = gst_rtsp_strresult (res);
9214 gst_rtsp_message_unset (&request);
9215 if (res != GST_RTSP_EINTR) {
9216 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
9217 ("Could not send message. (%s)", str));
9219 GST_WARNING_OBJECT (src, "PAUSE interrupted");
9227 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
9229 GstRTSPSrc *rtspsrc;
9231 rtspsrc = GST_RTSPSRC (bin);
9233 switch (GST_MESSAGE_TYPE (message)) {
9234 case GST_MESSAGE_STREAM_START:
9235 case GST_MESSAGE_EOS:
9236 gst_message_unref (message);
9238 case GST_MESSAGE_ELEMENT:
9240 const GstStructure *s = gst_message_get_structure (message);
9242 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
9243 gboolean ignore_timeout;
9245 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
9247 GST_OBJECT_LOCK (rtspsrc);
9248 ignore_timeout = rtspsrc->ignore_timeout;
9249 rtspsrc->ignore_timeout = TRUE;
9250 GST_OBJECT_UNLOCK (rtspsrc);
9252 /* we only act on the first udp timeout message, others are irrelevant
9253 * and can be ignored. */
9254 if (!ignore_timeout)
9255 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
9257 gst_message_unref (message);
9260 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
9263 case GST_MESSAGE_ERROR:
9266 GstRTSPStream *stream;
9269 udpsrc = GST_MESSAGE_SRC (message);
9271 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
9272 GST_ELEMENT_NAME (udpsrc));
9274 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
9278 /* we ignore the RTCP udpsrc */
9279 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
9282 /* if we get error messages from the udp sources, that's not a problem as
9283 * long as not all of them error out. We also don't really know what the
9284 * problem is, the message does not give enough detail... */
9285 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
9286 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
9287 if (ret != GST_FLOW_OK)
9291 gst_message_unref (message);
9295 /* fatal but not our message, forward */
9296 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
9301 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
9307 /* the thread where everything happens */
9309 gst_rtspsrc_thread (GstRTSPSrc * src)
9312 ParameterRequest *req = NULL;
9314 GST_OBJECT_LOCK (src);
9315 cmd = src->pending_cmd;
9316 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
9317 || cmd == CMD_LOOP || cmd == CMD_OPEN || cmd == CMD_GET_PARAMETER
9318 || cmd == CMD_SET_PARAMETER) {
9319 if (g_queue_is_empty (&src->set_get_param_q)) {
9320 src->pending_cmd = CMD_LOOP;
9322 ParameterRequest *next_req;
9323 if (cmd == CMD_GET_PARAMETER || cmd == CMD_SET_PARAMETER) {
9324 req = g_queue_pop_head (&src->set_get_param_q);
9326 next_req = g_queue_peek_head (&src->set_get_param_q);
9327 src->pending_cmd = next_req ? next_req->cmd : CMD_LOOP;
9330 src->pending_cmd = CMD_WAIT;
9331 GST_DEBUG_OBJECT (src, "got command %s", cmd_to_string (cmd));
9333 /* we got the message command, so ensure communication is possible again */
9334 gst_rtspsrc_connection_flush (src, FALSE);
9336 src->busy_cmd = cmd;
9337 GST_OBJECT_UNLOCK (src);
9341 gst_rtspsrc_open (src, TRUE);
9344 gst_rtspsrc_play (src, &src->segment, TRUE, NULL);
9347 gst_rtspsrc_pause (src, TRUE);
9350 gst_rtspsrc_close (src, TRUE, FALSE);
9352 case CMD_GET_PARAMETER:
9353 gst_rtspsrc_get_parameter (src, req);
9355 case CMD_SET_PARAMETER:
9356 gst_rtspsrc_set_parameter (src, req);
9359 gst_rtspsrc_loop (src);
9362 gst_rtspsrc_reconnect (src, FALSE);
9368 GST_OBJECT_LOCK (src);
9369 /* No more cmds, wake any waiters */
9370 g_cond_broadcast (&src->cmd_cond);
9371 /* and go back to sleep */
9372 if (src->pending_cmd == CMD_WAIT) {
9374 gst_task_pause (src->task);
9377 src->busy_cmd = CMD_WAIT;
9378 GST_OBJECT_UNLOCK (src);
9382 gst_rtspsrc_start (GstRTSPSrc * src)
9384 GST_DEBUG_OBJECT (src, "starting");
9386 GST_OBJECT_LOCK (src);
9388 src->pending_cmd = CMD_WAIT;
9390 if (src->task == NULL) {
9391 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
9392 if (src->task == NULL)
9395 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
9397 GST_OBJECT_UNLOCK (src);
9404 GST_OBJECT_UNLOCK (src);
9405 GST_ERROR_OBJECT (src, "failed to create task");
9411 gst_rtspsrc_stop (GstRTSPSrc * src)
9415 GST_DEBUG_OBJECT (src, "stopping");
9417 /* also cancels pending task */
9418 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
9420 GST_OBJECT_LOCK (src);
9421 if ((task = src->task)) {
9423 GST_OBJECT_UNLOCK (src);
9425 gst_task_stop (task);
9427 /* make sure it is not running */
9428 GST_RTSP_STREAM_LOCK (src);
9429 GST_RTSP_STREAM_UNLOCK (src);
9431 /* now wait for the task to finish */
9432 gst_task_join (task);
9434 /* and free the task */
9435 gst_object_unref (GST_OBJECT (task));
9437 GST_OBJECT_LOCK (src);
9439 GST_OBJECT_UNLOCK (src);
9441 /* ensure synchronously all is closed and clean */
9442 gst_rtspsrc_close (src, FALSE, TRUE);
9447 static GstStateChangeReturn
9448 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
9450 GstRTSPSrc *rtspsrc;
9451 GstStateChangeReturn ret;
9453 rtspsrc = GST_RTSPSRC (element);
9455 switch (transition) {
9456 case GST_STATE_CHANGE_NULL_TO_READY:
9457 if (!gst_rtspsrc_start (rtspsrc))
9460 case GST_STATE_CHANGE_READY_TO_PAUSED:
9461 /* init some state */
9462 rtspsrc->cur_protocols = rtspsrc->protocols;
9463 /* first attempt, don't ignore timeouts */
9464 rtspsrc->ignore_timeout = FALSE;
9465 rtspsrc->open_error = FALSE;
9466 if (rtspsrc->is_live)
9467 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
9469 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
9471 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
9472 set_manager_buffer_mode (rtspsrc);
9474 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
9475 if (rtspsrc->is_live) {
9476 /* unblock the tcp tasks and make the loop waiting */
9477 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
9478 /* make sure it is waiting before we send PAUSE or PLAY below */
9479 GST_RTSP_STREAM_LOCK (rtspsrc);
9480 GST_RTSP_STREAM_UNLOCK (rtspsrc);
9484 case GST_STATE_CHANGE_PAUSED_TO_READY:
9485 rtspsrc->group_id = GST_GROUP_ID_INVALID;
9491 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
9492 if (ret == GST_STATE_CHANGE_FAILURE)
9495 switch (transition) {
9496 case GST_STATE_CHANGE_NULL_TO_READY:
9497 ret = GST_STATE_CHANGE_SUCCESS;
9499 case GST_STATE_CHANGE_READY_TO_PAUSED:
9500 if (rtspsrc->is_live)
9501 ret = GST_STATE_CHANGE_NO_PREROLL;
9503 ret = GST_STATE_CHANGE_SUCCESS;
9505 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
9506 if (rtspsrc->is_live)
9507 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
9508 ret = GST_STATE_CHANGE_SUCCESS;
9510 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
9511 if (rtspsrc->is_live) {
9512 /* send pause request and keep the idle task around */
9513 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
9515 ret = GST_STATE_CHANGE_SUCCESS;
9517 case GST_STATE_CHANGE_PAUSED_TO_READY:
9518 rtspsrc->seek_seqnum = GST_SEQNUM_INVALID;
9519 gst_rtspsrc_loop_send_cmd_and_wait (rtspsrc, CMD_CLOSE, CMD_ALL,
9520 rtspsrc->teardown_timeout);
9521 ret = GST_STATE_CHANGE_SUCCESS;
9523 case GST_STATE_CHANGE_READY_TO_NULL:
9524 gst_rtspsrc_stop (rtspsrc);
9525 ret = GST_STATE_CHANGE_SUCCESS;
9528 /* Otherwise it's success, we don't want to return spurious
9529 * NO_PREROLL or ASYNC from internal elements as we care for
9530 * state changes ourselves here
9532 * This is to catch PAUSED->PAUSED and PLAYING->PLAYING transitions.
9534 if (GST_STATE_TRANSITION_NEXT (transition) == GST_STATE_PAUSED)
9535 ret = GST_STATE_CHANGE_NO_PREROLL;
9537 ret = GST_STATE_CHANGE_SUCCESS;
9546 GST_DEBUG_OBJECT (rtspsrc, "start failed");
9547 return GST_STATE_CHANGE_FAILURE;
9552 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
9555 GstRTSPSrc *rtspsrc;
9557 rtspsrc = GST_RTSPSRC (element);
9559 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK) {
9560 if (rtspsrc->state >= GST_RTSP_STATE_READY) {
9561 res = gst_rtspsrc_perform_seek (rtspsrc, event);
9562 gst_event_unref (event);
9564 /* Store for later use */
9566 rtspsrc->initial_seek = event;
9568 } else if (GST_EVENT_IS_DOWNSTREAM (event)) {
9569 res = gst_rtspsrc_push_event (rtspsrc, event);
9571 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
9578 /*** GSTURIHANDLER INTERFACE *************************************************/
9581 gst_rtspsrc_uri_get_type (GType type)
9586 static const gchar *const *
9587 gst_rtspsrc_uri_get_protocols (GType type)
9589 static const gchar *protocols[] =
9590 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
9591 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
9598 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
9600 GstRTSPSrc *src = GST_RTSPSRC (handler);
9602 /* FIXME: make thread-safe */
9603 return g_strdup (src->conninfo.location);
9607 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
9613 GstRTSPUrl *newurl = NULL;
9614 GstSDPMessage *sdp = NULL;
9616 src = GST_RTSPSRC (handler);
9618 /* same URI, we're fine */
9619 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
9622 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
9623 sres = gst_sdp_message_new (&sdp);
9627 GST_DEBUG_OBJECT (src, "parsing SDP message");
9628 sres = gst_sdp_message_parse_uri (uri, sdp);
9633 GST_DEBUG_OBJECT (src, "parsing URI");
9634 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
9638 /* if worked, free previous and store new url object along with the original
9640 GST_DEBUG_OBJECT (src, "configuring URI");
9641 g_free (src->conninfo.location);
9642 src->conninfo.location = g_strdup (uri);
9643 gst_rtsp_url_free (src->conninfo.url);
9644 src->conninfo.url = newurl;
9645 g_free (src->conninfo.url_str);
9647 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
9649 src->conninfo.url_str = NULL;
9652 gst_sdp_message_free (src->sdp);
9654 src->from_sdp = sdp != NULL;
9656 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
9657 GST_DEBUG_OBJECT (src, "request uri is: %s",
9658 GST_STR_NULL (src->conninfo.url_str));
9665 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
9670 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", sres);
9671 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
9672 "Could not create SDP");
9677 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", sres,
9678 GST_STR_NULL (uri));
9679 gst_sdp_message_free (sdp);
9680 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
9686 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
9687 GST_STR_NULL (uri), res);
9688 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
9689 "Invalid RTSP URI");
9695 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
9697 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
9699 iface->get_type = gst_rtspsrc_uri_get_type;
9700 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
9701 iface->get_uri = gst_rtspsrc_uri_get_uri;
9702 iface->set_uri = gst_rtspsrc_uri_set_uri;
9706 /* send GET_PARAMETER */
9707 static GstRTSPResult
9708 gst_rtspsrc_get_parameter (GstRTSPSrc * src, ParameterRequest * req)
9710 GstRTSPMessage request = { 0 };
9711 GstRTSPMessage response = { 0 };
9713 GstRTSPStatusCode code = GST_RTSP_STS_OK;
9714 const gchar *control;
9715 gchar *recv_body = NULL;
9716 guint recv_body_len;
9718 GST_DEBUG_OBJECT (src, "creating server get_parameter");
9722 if ((res = gst_rtspsrc_ensure_open (src, FALSE)) < 0)
9725 control = get_aggregate_control (src);
9726 if (control == NULL)
9729 if (!(src->methods & GST_RTSP_GET_PARAMETER))
9732 gst_rtspsrc_connection_flush (src, FALSE);
9734 res = gst_rtsp_message_init_request (&request, GST_RTSP_GET_PARAMETER,
9737 goto create_request_failed;
9739 res = gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CONTENT_TYPE,
9740 req->content_type == NULL ? "text/parameters" : req->content_type);
9742 goto add_content_hdr_failed;
9744 if (req->body && req->body->len) {
9746 gst_rtsp_message_set_body (&request, (guint8 *) req->body->str,
9749 goto set_body_failed;
9752 if ((res = gst_rtspsrc_send (src, &src->conninfo,
9753 &request, &response, &code, NULL)) < 0)
9756 res = gst_rtsp_message_get_body (&response, (guint8 **) & recv_body,
9759 goto get_body_failed;
9763 gst_promise_reply (req->promise,
9764 gst_structure_new ("get-parameter-reply",
9765 "rtsp-result", G_TYPE_INT, res,
9766 "rtsp-code", G_TYPE_INT, code,
9767 "rtsp-reason", G_TYPE_STRING, gst_rtsp_status_as_text (code),
9768 "body", G_TYPE_STRING, GST_STR_NULL (recv_body), NULL));
9769 free_param_data (req);
9772 gst_rtsp_message_unset (&request);
9773 gst_rtsp_message_unset (&response);
9781 GST_DEBUG_OBJECT (src, "failed to open stream");
9786 GST_DEBUG_OBJECT (src, "no control url to send GET_PARAMETER");
9787 res = GST_RTSP_ERROR;
9792 GST_DEBUG_OBJECT (src, "GET_PARAMETER is not supported");
9793 res = GST_RTSP_ERROR;
9796 create_request_failed:
9798 GST_DEBUG_OBJECT (src, "could not create GET_PARAMETER request");
9801 add_content_hdr_failed:
9803 GST_DEBUG_OBJECT (src, "could not add content header");
9808 GST_DEBUG_OBJECT (src, "could not set body");
9813 gchar *str = gst_rtsp_strresult (res);
9815 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
9816 ("Could not send get-parameter. (%s)", str));
9822 GST_DEBUG_OBJECT (src, "could not get body");
9827 /* send SET_PARAMETER */
9828 static GstRTSPResult
9829 gst_rtspsrc_set_parameter (GstRTSPSrc * src, ParameterRequest * req)
9831 GstRTSPMessage request = { 0 };
9832 GstRTSPMessage response = { 0 };
9833 GstRTSPResult res = GST_RTSP_OK;
9834 GstRTSPStatusCode code = GST_RTSP_STS_OK;
9835 const gchar *control;
9837 GST_DEBUG_OBJECT (src, "creating server set_parameter");
9841 if ((res = gst_rtspsrc_ensure_open (src, FALSE)) < 0)
9844 control = get_aggregate_control (src);
9845 if (control == NULL)
9848 if (!(src->methods & GST_RTSP_SET_PARAMETER))
9851 gst_rtspsrc_connection_flush (src, FALSE);
9854 gst_rtsp_message_init_request (&request, GST_RTSP_SET_PARAMETER, control);
9858 res = gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CONTENT_TYPE,
9859 req->content_type == NULL ? "text/parameters" : req->content_type);
9861 goto add_content_hdr_failed;
9863 if (req->body && req->body->len) {
9865 gst_rtsp_message_set_body (&request, (guint8 *) req->body->str,
9869 goto set_body_failed;
9872 if ((res = gst_rtspsrc_send (src, &src->conninfo,
9873 &request, &response, &code, NULL)) < 0)
9878 gst_promise_reply (req->promise, gst_structure_new ("set-parameter-reply",
9879 "rtsp-result", G_TYPE_INT, res,
9880 "rtsp-code", G_TYPE_INT, code,
9881 "rtsp-reason", G_TYPE_STRING, gst_rtsp_status_as_text (code),
9883 free_param_data (req);
9885 gst_rtsp_message_unset (&request);
9886 gst_rtsp_message_unset (&response);
9894 GST_DEBUG_OBJECT (src, "failed to open stream");
9899 GST_DEBUG_OBJECT (src, "no control url to send SET_PARAMETER");
9900 res = GST_RTSP_ERROR;
9905 GST_DEBUG_OBJECT (src, "SET_PARAMETER is not supported");
9906 res = GST_RTSP_ERROR;
9909 add_content_hdr_failed:
9911 GST_DEBUG_OBJECT (src, "could not add content header");
9916 GST_DEBUG_OBJECT (src, "could not set body");
9921 gchar *str = gst_rtsp_strresult (res);
9923 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
9924 ("Could not send set-parameter. (%s)", str));
9930 typedef struct _RTSPKeyValue
9932 GstRTSPHeaderField field;
9934 gchar *custom_key; /* custom header string (field is INVALID then) */
9938 key_value_foreach (GArray * array, GFunc func, gpointer user_data)
9942 g_return_if_fail (array != NULL);
9944 for (i = 0; i < array->len; i++) {
9945 (*func) (&g_array_index (array, RTSPKeyValue, i), user_data);
9950 dump_key_value (gpointer data, gpointer user_data G_GNUC_UNUSED)
9952 RTSPKeyValue *key_value = (RTSPKeyValue *) data;
9953 GstRTSPSrc *src = GST_RTSPSRC (user_data);
9954 const gchar *key_string;
9956 if (key_value->custom_key != NULL)
9957 key_string = key_value->custom_key;
9959 key_string = gst_rtsp_header_as_text (key_value->field);
9961 GST_LOG_OBJECT (src, " key: '%s', value: '%s'", key_string,
9966 gst_rtspsrc_print_rtsp_message (GstRTSPSrc * src, const GstRTSPMessage * msg)
9970 GString *body_string = NULL;
9972 g_return_if_fail (src != NULL);
9973 g_return_if_fail (msg != NULL);
9975 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) < GST_LEVEL_LOG)
9978 GST_LOG_OBJECT (src, "--------------------------------------------");
9979 switch (msg->type) {
9980 case GST_RTSP_MESSAGE_REQUEST:
9981 GST_LOG_OBJECT (src, "RTSP request message %p", msg);
9982 GST_LOG_OBJECT (src, " request line:");
9983 GST_LOG_OBJECT (src, " method: '%s'",
9984 gst_rtsp_method_as_text (msg->type_data.request.method));
9985 GST_LOG_OBJECT (src, " uri: '%s'", msg->type_data.request.uri);
9986 GST_LOG_OBJECT (src, " version: '%s'",
9987 gst_rtsp_version_as_text (msg->type_data.request.version));
9988 GST_LOG_OBJECT (src, " headers:");
9989 key_value_foreach (msg->hdr_fields, dump_key_value, src);
9990 GST_LOG_OBJECT (src, " body:");
9991 gst_rtsp_message_get_body (msg, &data, &size);
9993 body_string = g_string_new_len ((const gchar *) data, size);
9994 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
9995 g_string_free (body_string, TRUE);
9999 case GST_RTSP_MESSAGE_RESPONSE:
10000 GST_LOG_OBJECT (src, "RTSP response message %p", msg);
10001 GST_LOG_OBJECT (src, " status line:");
10002 GST_LOG_OBJECT (src, " code: '%d'", msg->type_data.response.code);
10003 GST_LOG_OBJECT (src, " reason: '%s'", msg->type_data.response.reason);
10004 GST_LOG_OBJECT (src, " version: '%s",
10005 gst_rtsp_version_as_text (msg->type_data.response.version));
10006 GST_LOG_OBJECT (src, " headers:");
10007 key_value_foreach (msg->hdr_fields, dump_key_value, src);
10008 gst_rtsp_message_get_body (msg, &data, &size);
10009 GST_LOG_OBJECT (src, " body: length %d", size);
10011 body_string = g_string_new_len ((const gchar *) data, size);
10012 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
10013 g_string_free (body_string, TRUE);
10014 body_string = NULL;
10017 case GST_RTSP_MESSAGE_HTTP_REQUEST:
10018 GST_LOG_OBJECT (src, "HTTP request message %p", msg);
10019 GST_LOG_OBJECT (src, " request line:");
10020 GST_LOG_OBJECT (src, " method: '%s'",
10021 gst_rtsp_method_as_text (msg->type_data.request.method));
10022 GST_LOG_OBJECT (src, " uri: '%s'", msg->type_data.request.uri);
10023 GST_LOG_OBJECT (src, " version: '%s'",
10024 gst_rtsp_version_as_text (msg->type_data.request.version));
10025 GST_LOG_OBJECT (src, " headers:");
10026 key_value_foreach (msg->hdr_fields, dump_key_value, src);
10027 GST_LOG_OBJECT (src, " body:");
10028 gst_rtsp_message_get_body (msg, &data, &size);
10030 body_string = g_string_new_len ((const gchar *) data, size);
10031 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
10032 g_string_free (body_string, TRUE);
10033 body_string = NULL;
10036 case GST_RTSP_MESSAGE_HTTP_RESPONSE:
10037 GST_LOG_OBJECT (src, "HTTP response message %p", msg);
10038 GST_LOG_OBJECT (src, " status line:");
10039 GST_LOG_OBJECT (src, " code: '%d'", msg->type_data.response.code);
10040 GST_LOG_OBJECT (src, " reason: '%s'", msg->type_data.response.reason);
10041 GST_LOG_OBJECT (src, " version: '%s'",
10042 gst_rtsp_version_as_text (msg->type_data.response.version));
10043 GST_LOG_OBJECT (src, " headers:");
10044 key_value_foreach (msg->hdr_fields, dump_key_value, src);
10045 gst_rtsp_message_get_body (msg, &data, &size);
10046 GST_LOG_OBJECT (src, " body: length %d", size);
10048 body_string = g_string_new_len ((const gchar *) data, size);
10049 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
10050 g_string_free (body_string, TRUE);
10051 body_string = NULL;
10054 case GST_RTSP_MESSAGE_DATA:
10055 GST_LOG_OBJECT (src, "RTSP data message %p", msg);
10056 GST_LOG_OBJECT (src, " channel: '%d'", msg->type_data.data.channel);
10057 GST_LOG_OBJECT (src, " size: '%d'", msg->body_size);
10058 gst_rtsp_message_get_body (msg, &data, &size);
10060 body_string = g_string_new_len ((const gchar *) data, size);
10061 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
10062 g_string_free (body_string, TRUE);
10063 body_string = NULL;
10067 GST_LOG_OBJECT (src, "unsupported message type %d", msg->type);
10070 GST_LOG_OBJECT (src, "--------------------------------------------");
10074 gst_rtspsrc_print_sdp_media (GstRTSPSrc * src, GstSDPMedia * media)
10076 GST_LOG_OBJECT (src, " media: '%s'", GST_STR_NULL (media->media));
10077 GST_LOG_OBJECT (src, " port: '%u'", media->port);
10078 GST_LOG_OBJECT (src, " num_ports: '%u'", media->num_ports);
10079 GST_LOG_OBJECT (src, " proto: '%s'", GST_STR_NULL (media->proto));
10080 if (media->fmts && media->fmts->len > 0) {
10083 GST_LOG_OBJECT (src, " formats:");
10084 for (i = 0; i < media->fmts->len; i++) {
10085 GST_LOG_OBJECT (src, " format '%s'", g_array_index (media->fmts,
10089 GST_LOG_OBJECT (src, " information: '%s'",
10090 GST_STR_NULL (media->information));
10091 if (media->connections && media->connections->len > 0) {
10094 GST_LOG_OBJECT (src, " connections:");
10095 for (i = 0; i < media->connections->len; i++) {
10096 GstSDPConnection *conn =
10097 &g_array_index (media->connections, GstSDPConnection, i);
10099 GST_LOG_OBJECT (src, " nettype: '%s'",
10100 GST_STR_NULL (conn->nettype));
10101 GST_LOG_OBJECT (src, " addrtype: '%s'",
10102 GST_STR_NULL (conn->addrtype));
10103 GST_LOG_OBJECT (src, " address: '%s'",
10104 GST_STR_NULL (conn->address));
10105 GST_LOG_OBJECT (src, " ttl: '%u'", conn->ttl);
10106 GST_LOG_OBJECT (src, " addr_number: '%u'", conn->addr_number);
10109 if (media->bandwidths && media->bandwidths->len > 0) {
10112 GST_LOG_OBJECT (src, " bandwidths:");
10113 for (i = 0; i < media->bandwidths->len; i++) {
10114 GstSDPBandwidth *bw =
10115 &g_array_index (media->bandwidths, GstSDPBandwidth, i);
10117 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (bw->bwtype));
10118 GST_LOG_OBJECT (src, " bandwidth: '%u'", bw->bandwidth);
10121 GST_LOG_OBJECT (src, " key:");
10122 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (media->key.type));
10123 GST_LOG_OBJECT (src, " data: '%s'", GST_STR_NULL (media->key.data));
10124 if (media->attributes && media->attributes->len > 0) {
10127 GST_LOG_OBJECT (src, " attributes:");
10128 for (i = 0; i < media->attributes->len; i++) {
10129 GstSDPAttribute *attr =
10130 &g_array_index (media->attributes, GstSDPAttribute, i);
10132 GST_LOG_OBJECT (src, " attribute '%s' : '%s'", attr->key, attr->value);
10138 gst_rtspsrc_print_sdp_message (GstRTSPSrc * src, const GstSDPMessage * msg)
10140 g_return_if_fail (src != NULL);
10141 g_return_if_fail (msg != NULL);
10143 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) < GST_LEVEL_LOG)
10146 GST_LOG_OBJECT (src, "--------------------------------------------");
10147 GST_LOG_OBJECT (src, "sdp packet %p:", msg);
10148 GST_LOG_OBJECT (src, " version: '%s'", GST_STR_NULL (msg->version));
10149 GST_LOG_OBJECT (src, " origin:");
10150 GST_LOG_OBJECT (src, " username: '%s'",
10151 GST_STR_NULL (msg->origin.username));
10152 GST_LOG_OBJECT (src, " sess_id: '%s'",
10153 GST_STR_NULL (msg->origin.sess_id));
10154 GST_LOG_OBJECT (src, " sess_version: '%s'",
10155 GST_STR_NULL (msg->origin.sess_version));
10156 GST_LOG_OBJECT (src, " nettype: '%s'",
10157 GST_STR_NULL (msg->origin.nettype));
10158 GST_LOG_OBJECT (src, " addrtype: '%s'",
10159 GST_STR_NULL (msg->origin.addrtype));
10160 GST_LOG_OBJECT (src, " addr: '%s'", GST_STR_NULL (msg->origin.addr));
10161 GST_LOG_OBJECT (src, " session_name: '%s'",
10162 GST_STR_NULL (msg->session_name));
10163 GST_LOG_OBJECT (src, " information: '%s'", GST_STR_NULL (msg->information));
10164 GST_LOG_OBJECT (src, " uri: '%s'", GST_STR_NULL (msg->uri));
10166 if (msg->emails && msg->emails->len > 0) {
10169 GST_LOG_OBJECT (src, " emails:");
10170 for (i = 0; i < msg->emails->len; i++) {
10171 GST_LOG_OBJECT (src, " email '%s'", g_array_index (msg->emails, gchar *,
10175 if (msg->phones && msg->phones->len > 0) {
10178 GST_LOG_OBJECT (src, " phones:");
10179 for (i = 0; i < msg->phones->len; i++) {
10180 GST_LOG_OBJECT (src, " phone '%s'", g_array_index (msg->phones, gchar *,
10184 GST_LOG_OBJECT (src, " connection:");
10185 GST_LOG_OBJECT (src, " nettype: '%s'",
10186 GST_STR_NULL (msg->connection.nettype));
10187 GST_LOG_OBJECT (src, " addrtype: '%s'",
10188 GST_STR_NULL (msg->connection.addrtype));
10189 GST_LOG_OBJECT (src, " address: '%s'",
10190 GST_STR_NULL (msg->connection.address));
10191 GST_LOG_OBJECT (src, " ttl: '%u'", msg->connection.ttl);
10192 GST_LOG_OBJECT (src, " addr_number: '%u'", msg->connection.addr_number);
10193 if (msg->bandwidths && msg->bandwidths->len > 0) {
10196 GST_LOG_OBJECT (src, " bandwidths:");
10197 for (i = 0; i < msg->bandwidths->len; i++) {
10198 GstSDPBandwidth *bw =
10199 &g_array_index (msg->bandwidths, GstSDPBandwidth, i);
10201 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (bw->bwtype));
10202 GST_LOG_OBJECT (src, " bandwidth: '%u'", bw->bandwidth);
10205 GST_LOG_OBJECT (src, " key:");
10206 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (msg->key.type));
10207 GST_LOG_OBJECT (src, " data: '%s'", GST_STR_NULL (msg->key.data));
10208 if (msg->attributes && msg->attributes->len > 0) {
10211 GST_LOG_OBJECT (src, " attributes:");
10212 for (i = 0; i < msg->attributes->len; i++) {
10213 GstSDPAttribute *attr =
10214 &g_array_index (msg->attributes, GstSDPAttribute, i);
10216 GST_LOG_OBJECT (src, " attribute '%s' : '%s'", attr->key, attr->value);
10219 if (msg->medias && msg->medias->len > 0) {
10222 GST_LOG_OBJECT (src, " medias:");
10223 for (i = 0; i < msg->medias->len; i++) {
10224 GST_LOG_OBJECT (src, " media %u:", i);
10225 gst_rtspsrc_print_sdp_media (src, &g_array_index (msg->medias,
10229 GST_LOG_OBJECT (src, "--------------------------------------------");