2 * Copyright (C) <2005,2006> Wim Taymans <wim at fluendo dot com>
3 * <2006> Lutz Mueller <lutz at topfrose dot de>
4 * <2015> Jan Schmidt <jan at centricular dot com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 * Unless otherwise indicated, Source Code is licensed under MIT license.
23 * See further explanation attached in License Statement (distributed in the file
26 * Permission is hereby granted, free of charge, to any person obtaining a copy of
27 * this software and associated documentation files (the "Software"), to deal in
28 * the Software without restriction, including without limitation the rights to
29 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
30 * of the Software, and to permit persons to whom the Software is furnished to do
31 * so, subject to the following conditions:
33 * The above copyright notice and this permission notice shall be included in all
34 * copies or substantial portions of the Software.
36 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
39 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
40 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
41 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
45 * SECTION:element-rtspclientsink
47 * Makes a connection to an RTSP server and send data via RTSP RECORD.
48 * rtspclientsink strictly follows RFC 2326
50 * RTSP supports transport over TCP or UDP in unicast or multicast mode. By
51 * default rtspclientsink will negotiate a connection in the following order:
52 * UDP unicast/UDP multicast/TCP. The order cannot be changed but the allowed
53 * protocols can be controlled with the #GstRTSPClientSink:protocols property.
55 * rtspclientsink will internally instantiate an RTP session manager element
56 * that will handle the RTCP messages to and from the server, jitter removal,
57 * and packet reordering.
58 * This feature is implemented using the gstrtpbin element.
60 * rtspclientsink accepts any stream for which there is an installed payloader,
61 * creates the payloader and manages payload-types, as well as RTX setup.
62 * The new-payloader signal is fired when a payloader is created, in case
63 * an app wants to do custom configuration (such as for MTU).
66 * <title>Example launch line</title>
68 * gst-launch-1.0 videotestsrc ! jpegenc ! rtspclientsink location=rtsp://some.server/url
69 * ]| Establish a connection to an RTSP server and send JPEG encoded video packets
74 * - Handle EOS properly and shutdown. The problem with EOS is we don't know
75 * when the server has received all data, so we don't know when to do teardown.
76 * At the moment, we forward EOS to the app as soon as we stop sending. Is there
77 * a way to know from the receiver that it's got all data? Some session timeout?
78 * - Implement extension support for Real / WMS if they support RECORD?
79 * - Add support for network clock synchronised streaming?
80 * - Fix crypto key nego so SAVP/SAVPF profiles work.
81 * - Test (&fix?) HTTP tunnel support
82 * - Add an address pool object for GstRTSPStreams to use for multicast
83 * - Test multicast UDP transport
92 #endif /* HAVE_UNISTD_H */
98 #include <gst/net/gstnet.h>
99 #include <gst/sdp/gstsdpmessage.h>
100 #include <gst/sdp/gstmikey.h>
101 #include <gst/rtp/rtp.h>
103 #include "gstrtspclientsink.h"
105 typedef struct _GstRtspClientSinkPad GstRtspClientSinkPad;
106 typedef GstGhostPadClass GstRtspClientSinkPadClass;
108 struct _GstRtspClientSinkPad
111 GstElement *custom_payloader;
112 guint ulpfec_percentage;
119 PROP_PAD_ULPFEC_PERCENTAGE
122 #define DEFAULT_PAD_ULPFEC_PERCENTAGE 0
124 static GType gst_rtsp_client_sink_pad_get_type (void);
125 G_DEFINE_TYPE (GstRtspClientSinkPad, gst_rtsp_client_sink_pad,
127 #define GST_TYPE_RTSP_CLIENT_SINK_PAD (gst_rtsp_client_sink_pad_get_type ())
128 #define GST_RTSP_CLIENT_SINK_PAD(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RTSP_CLIENT_SINK_PAD,GstRtspClientSinkPad))
131 gst_rtsp_client_sink_pad_set_property (GObject * object, guint prop_id,
132 const GValue * value, GParamSpec * pspec)
134 GstRtspClientSinkPad *pad;
136 pad = GST_RTSP_CLIENT_SINK_PAD (object);
139 case PROP_PAD_PAYLOADER:
140 GST_OBJECT_LOCK (pad);
141 if (pad->custom_payloader)
142 gst_object_unref (pad->custom_payloader);
143 pad->custom_payloader = g_value_get_object (value);
144 gst_object_ref_sink (pad->custom_payloader);
145 GST_OBJECT_UNLOCK (pad);
147 case PROP_PAD_ULPFEC_PERCENTAGE:
148 GST_OBJECT_LOCK (pad);
149 pad->ulpfec_percentage = g_value_get_uint (value);
150 GST_OBJECT_UNLOCK (pad);
153 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
159 gst_rtsp_client_sink_pad_get_property (GObject * object, guint prop_id,
160 GValue * value, GParamSpec * pspec)
162 GstRtspClientSinkPad *pad;
164 pad = GST_RTSP_CLIENT_SINK_PAD (object);
167 case PROP_PAD_PAYLOADER:
168 GST_OBJECT_LOCK (pad);
169 g_value_set_object (value, pad->custom_payloader);
170 GST_OBJECT_UNLOCK (pad);
172 case PROP_PAD_ULPFEC_PERCENTAGE:
173 GST_OBJECT_LOCK (pad);
174 g_value_set_uint (value, pad->ulpfec_percentage);
175 GST_OBJECT_UNLOCK (pad);
178 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
184 gst_rtsp_client_sink_pad_dispose (GObject * object)
186 GstRtspClientSinkPad *pad = GST_RTSP_CLIENT_SINK_PAD (object);
188 if (pad->custom_payloader)
189 gst_object_unref (pad->custom_payloader);
191 G_OBJECT_CLASS (gst_rtsp_client_sink_pad_parent_class)->dispose (object);
195 gst_rtsp_client_sink_pad_class_init (GstRtspClientSinkPadClass * klass)
197 GObjectClass *gobject_klass;
199 gobject_klass = (GObjectClass *) klass;
201 gobject_klass->set_property = gst_rtsp_client_sink_pad_set_property;
202 gobject_klass->get_property = gst_rtsp_client_sink_pad_get_property;
203 gobject_klass->dispose = gst_rtsp_client_sink_pad_dispose;
205 g_object_class_install_property (gobject_klass, PROP_PAD_PAYLOADER,
206 g_param_spec_object ("payloader", "Payloader",
207 "The payloader element to use (NULL = default automatically selected)",
208 GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
210 g_object_class_install_property (gobject_klass, PROP_PAD_ULPFEC_PERCENTAGE,
211 g_param_spec_uint ("ulpfec-percentage", "ULPFEC percentage",
212 "The percentage of ULP redundancy to apply", 0, 100,
213 DEFAULT_PAD_ULPFEC_PERCENTAGE,
214 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
218 gst_rtsp_client_sink_pad_init (GstRtspClientSinkPad * pad)
223 gst_rtsp_client_sink_pad_new (const GstPadTemplate * pad_tmpl,
226 GstRtspClientSinkPad *ret;
229 g_object_new (GST_TYPE_RTSP_CLIENT_SINK_PAD, "direction", GST_PAD_SINK,
230 "template", pad_tmpl, "name", name, NULL);
231 gst_ghost_pad_construct (GST_GHOST_PAD_CAST (ret));
233 return GST_PAD (ret);
236 GST_DEBUG_CATEGORY_STATIC (rtsp_client_sink_debug);
237 #define GST_CAT_DEFAULT (rtsp_client_sink_debug)
239 static GstStaticPadTemplate rtptemplate = GST_STATIC_PAD_TEMPLATE ("sink_%u",
242 GST_STATIC_CAPS_ANY); /* Actual caps come from available set of payloaders */
246 SIGNAL_HANDLE_REQUEST,
248 SIGNAL_NEW_PAYLOADER,
249 SIGNAL_REQUEST_RTCP_KEY,
250 SIGNAL_ACCEPT_CERTIFICATE,
254 enum _GstRTSPClientSinkNtpTimeSource
257 NTP_TIME_SOURCE_UNIX,
258 NTP_TIME_SOURCE_RUNNING_TIME,
259 NTP_TIME_SOURCE_CLOCK_TIME
262 #define GST_TYPE_RTSP_CLIENT_SINK_NTP_TIME_SOURCE (gst_rtsp_client_sink_ntp_time_source_get_type())
264 gst_rtsp_client_sink_ntp_time_source_get_type (void)
266 static GType ntp_time_source_type = 0;
267 static const GEnumValue ntp_time_source_values[] = {
268 {NTP_TIME_SOURCE_NTP, "NTP time based on realtime clock", "ntp"},
269 {NTP_TIME_SOURCE_UNIX, "UNIX time based on realtime clock", "unix"},
270 {NTP_TIME_SOURCE_RUNNING_TIME,
271 "Running time based on pipeline clock",
273 {NTP_TIME_SOURCE_CLOCK_TIME, "Pipeline clock time", "clock-time"},
277 if (!ntp_time_source_type) {
278 ntp_time_source_type =
279 g_enum_register_static ("GstRTSPClientSinkNtpTimeSource",
280 ntp_time_source_values);
282 return ntp_time_source_type;
285 #define DEFAULT_LOCATION NULL
286 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
287 #define DEFAULT_DEBUG FALSE
288 #define DEFAULT_RETRY 20
289 #define DEFAULT_TIMEOUT 5000000
290 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
291 #define DEFAULT_TCP_TIMEOUT 20000000
292 #define DEFAULT_LATENCY_MS 2000
293 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
294 #define DEFAULT_PROXY NULL
295 #define DEFAULT_RTP_BLOCKSIZE 0
296 #define DEFAULT_USER_ID NULL
297 #define DEFAULT_USER_PW NULL
298 #define DEFAULT_PORT_RANGE NULL
299 #define DEFAULT_UDP_RECONNECT TRUE
300 #define DEFAULT_MULTICAST_IFACE NULL
301 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
302 #define DEFAULT_TLS_DATABASE NULL
303 #define DEFAULT_TLS_INTERACTION NULL
304 #define DEFAULT_NTP_TIME_SOURCE NTP_TIME_SOURCE_NTP
305 #define DEFAULT_USER_AGENT "GStreamer/" PACKAGE_VERSION
306 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
307 #define DEFAULT_RTX_TIME_MS 500
320 PROP_DO_RTSP_KEEP_ALIVE,
328 PROP_UDP_BUFFER_SIZE,
330 PROP_MULTICAST_IFACE,
332 PROP_TLS_VALIDATION_FLAGS,
334 PROP_TLS_INTERACTION,
335 PROP_NTP_TIME_SOURCE,
340 static void gst_rtsp_client_sink_finalize (GObject * object);
342 static void gst_rtsp_client_sink_set_property (GObject * object, guint prop_id,
343 const GValue * value, GParamSpec * pspec);
344 static void gst_rtsp_client_sink_get_property (GObject * object, guint prop_id,
345 GValue * value, GParamSpec * pspec);
347 static GstClock *gst_rtsp_client_sink_provide_clock (GstElement * element);
349 static void gst_rtsp_client_sink_uri_handler_init (gpointer g_iface,
350 gpointer iface_data);
352 static gboolean gst_rtsp_client_sink_set_proxy (GstRTSPClientSink * rtsp,
353 const gchar * proxy);
354 static void gst_rtsp_client_sink_set_tcp_timeout (GstRTSPClientSink *
355 rtsp_client_sink, guint64 timeout);
357 static GstStateChangeReturn gst_rtsp_client_sink_change_state (GstElement *
358 element, GstStateChange transition);
359 static void gst_rtsp_client_sink_handle_message (GstBin * bin,
360 GstMessage * message);
362 static gboolean gst_rtsp_client_sink_setup_auth (GstRTSPClientSink * sink,
363 GstRTSPMessage * response);
365 static gboolean gst_rtsp_client_sink_loop_send_cmd (GstRTSPClientSink * sink,
366 gint cmd, gint mask);
368 static GstRTSPResult gst_rtsp_client_sink_open (GstRTSPClientSink * sink,
370 static GstRTSPResult gst_rtsp_client_sink_record (GstRTSPClientSink * sink,
372 static GstRTSPResult gst_rtsp_client_sink_pause (GstRTSPClientSink * sink,
374 static GstRTSPResult gst_rtsp_client_sink_close (GstRTSPClientSink * sink,
375 gboolean async, gboolean only_close);
376 static gboolean gst_rtsp_client_sink_collect_streams (GstRTSPClientSink * sink);
378 static gboolean gst_rtsp_client_sink_uri_set_uri (GstURIHandler * handler,
379 const gchar * uri, GError ** error);
380 static gchar *gst_rtsp_client_sink_uri_get_uri (GstURIHandler * handler);
382 static gboolean gst_rtsp_client_sink_loop (GstRTSPClientSink * sink);
383 static void gst_rtsp_client_sink_connection_flush (GstRTSPClientSink * sink,
386 static GstPad *gst_rtsp_client_sink_request_new_pad (GstElement * element,
387 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
388 static void gst_rtsp_client_sink_release_pad (GstElement * element,
391 /* commands we send to out loop to notify it of events */
392 #define CMD_OPEN (1 << 0)
393 #define CMD_RECORD (1 << 1)
394 #define CMD_PAUSE (1 << 2)
395 #define CMD_CLOSE (1 << 3)
396 #define CMD_WAIT (1 << 4)
397 #define CMD_RECONNECT (1 << 5)
398 #define CMD_LOOP (1 << 6)
400 /* mask for all commands */
401 #define CMD_ALL ((CMD_LOOP << 1) - 1)
403 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
405 gchar *__txt = _gst_element_error_printf text; \
406 gst_element_post_message (GST_ELEMENT_CAST (el), \
407 gst_message_new_progress (GST_OBJECT_CAST (el), \
408 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
412 static guint gst_rtsp_client_sink_signals[LAST_SIGNAL] = { 0 };
414 /*********************************
415 * GstChildProxy implementation *
416 *********************************/
418 gst_rtsp_client_sink_child_proxy_get_child_by_index (GstChildProxy *
419 child_proxy, guint index)
422 GstRTSPClientSink *cs = GST_RTSP_CLIENT_SINK (child_proxy);
424 GST_OBJECT_LOCK (cs);
425 if ((obj = g_list_nth_data (GST_ELEMENT (cs)->sinkpads, index)))
427 GST_OBJECT_UNLOCK (cs);
433 gst_rtsp_client_sink_child_proxy_get_children_count (GstChildProxy *
438 GST_OBJECT_LOCK (child_proxy);
439 count = GST_ELEMENT (child_proxy)->numsinkpads;
440 GST_OBJECT_UNLOCK (child_proxy);
442 GST_INFO_OBJECT (child_proxy, "Children Count: %d", count);
448 gst_rtsp_client_sink_child_proxy_init (gpointer g_iface, gpointer iface_data)
450 GstChildProxyInterface *iface = g_iface;
452 GST_INFO ("intializing child proxy interface");
453 iface->get_child_by_index =
454 gst_rtsp_client_sink_child_proxy_get_child_by_index;
455 iface->get_children_count =
456 gst_rtsp_client_sink_child_proxy_get_children_count;
459 #define gst_rtsp_client_sink_parent_class parent_class
460 G_DEFINE_TYPE_WITH_CODE (GstRTSPClientSink, gst_rtsp_client_sink, GST_TYPE_BIN,
461 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
462 gst_rtsp_client_sink_uri_handler_init);
463 G_IMPLEMENT_INTERFACE (GST_TYPE_CHILD_PROXY,
464 gst_rtsp_client_sink_child_proxy_init);
467 #ifndef GST_DISABLE_GST_DEBUG
468 static inline const gchar *
469 cmd_to_string (guint cmd)
493 gst_rtsp_client_sink_class_init (GstRTSPClientSinkClass * klass)
495 GObjectClass *gobject_class;
496 GstElementClass *gstelement_class;
497 GstBinClass *gstbin_class;
499 gobject_class = (GObjectClass *) klass;
500 gstelement_class = (GstElementClass *) klass;
501 gstbin_class = (GstBinClass *) klass;
503 GST_DEBUG_CATEGORY_INIT (rtsp_client_sink_debug, "rtspclientsink", 0,
504 "RTSP sink element");
506 gobject_class->set_property = gst_rtsp_client_sink_set_property;
507 gobject_class->get_property = gst_rtsp_client_sink_get_property;
509 gobject_class->finalize = gst_rtsp_client_sink_finalize;
511 g_object_class_install_property (gobject_class, PROP_LOCATION,
512 g_param_spec_string ("location", "RTSP Location",
513 "Location of the RTSP url to read",
514 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
516 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
517 g_param_spec_flags ("protocols", "Protocols",
518 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
519 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
521 g_object_class_install_property (gobject_class, PROP_PROFILES,
522 g_param_spec_flags ("profiles", "Profiles",
523 "Allowed RTSP profiles", GST_TYPE_RTSP_PROFILE,
524 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
526 g_object_class_install_property (gobject_class, PROP_DEBUG,
527 g_param_spec_boolean ("debug", "Debug",
528 "Dump request and response messages to stdout",
529 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
531 g_object_class_install_property (gobject_class, PROP_RETRY,
532 g_param_spec_uint ("retry", "Retry",
533 "Max number of retries when allocating RTP ports.",
534 0, G_MAXUINT16, DEFAULT_RETRY,
535 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
537 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
538 g_param_spec_uint64 ("timeout", "Timeout",
539 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
540 0, G_MAXUINT64, DEFAULT_TIMEOUT,
541 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
543 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
544 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
545 "Fail after timeout microseconds on TCP connections (0 = disabled)",
546 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
547 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
549 g_object_class_install_property (gobject_class, PROP_LATENCY,
550 g_param_spec_uint ("latency", "Buffer latency in ms",
551 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
552 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
554 g_object_class_install_property (gobject_class, PROP_RTX_TIME,
555 g_param_spec_uint ("rtx-time", "Retransmission buffer in ms",
556 "Amount of ms to buffer for retransmission. 0 disables retransmission",
557 0, G_MAXUINT, DEFAULT_RTX_TIME_MS,
558 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
561 * GstRTSPClientSink:do-rtsp-keep-alive:
563 * Enable RTSP keep alive support. Some old server don't like RTSP
564 * keep alive and then this property needs to be set to FALSE.
566 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
567 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
568 "Send RTSP keep alive packets, disable for old incompatible server.",
569 DEFAULT_DO_RTSP_KEEP_ALIVE,
570 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
573 * GstRTSPClientSink:proxy:
575 * Set the proxy parameters. This has to be a string of the format
576 * [http://][user:passwd@]host[:port].
578 g_object_class_install_property (gobject_class, PROP_PROXY,
579 g_param_spec_string ("proxy", "Proxy",
580 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
581 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
583 * GstRTSPClientSink:proxy-id:
585 * Sets the proxy URI user id for authentication. If the URI set via the
586 * "proxy" property contains a user-id already, that will take precedence.
589 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
590 g_param_spec_string ("proxy-id", "proxy-id",
591 "HTTP proxy URI user id for authentication", "",
592 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
594 * GstRTSPClientSink:proxy-pw:
596 * Sets the proxy URI password for authentication. If the URI set via the
597 * "proxy" property contains a password already, that will take precedence.
600 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
601 g_param_spec_string ("proxy-pw", "proxy-pw",
602 "HTTP proxy URI user password for authentication", "",
603 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
606 * GstRTSPClientSink:rtp-blocksize:
608 * RTP package size to suggest to server.
610 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
611 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
612 "RTP package size to suggest to server (0 = disabled)",
613 0, 65536, DEFAULT_RTP_BLOCKSIZE,
614 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
616 g_object_class_install_property (gobject_class,
618 g_param_spec_string ("user-id", "user-id",
619 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
620 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
621 g_object_class_install_property (gobject_class, PROP_USER_PW,
622 g_param_spec_string ("user-pw", "user-pw",
623 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
624 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
627 * GstRTSPClientSink:port-range:
629 * Configure the client port numbers that can be used to receive
632 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
633 g_param_spec_string ("port-range", "Port range",
634 "Client port range that can be used to receive RTCP data, "
635 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
636 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
639 * GstRTSPClientSink:udp-buffer-size:
641 * Size of the kernel UDP receive buffer in bytes.
643 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
644 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
645 "Size of the kernel UDP receive buffer in bytes, 0=default",
646 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
647 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
649 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
650 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
651 "Reconnect to the server if RTSP connection is closed when doing UDP",
652 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
654 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
655 g_param_spec_string ("multicast-iface", "Multicast Interface",
656 "The network interface on which to join the multicast group",
657 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
659 g_object_class_install_property (gobject_class, PROP_SDES,
660 g_param_spec_boxed ("sdes", "SDES",
661 "The SDES items of this session",
662 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
665 * GstRTSPClientSink::tls-validation-flags:
667 * TLS certificate validation flags used to validate server
671 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
672 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
673 "TLS certificate validation flags used to validate the server certificate",
674 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
675 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
678 * GstRTSPClientSink::tls-database:
680 * TLS database with anchor certificate authorities used to validate
681 * the server certificate.
684 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
685 g_param_spec_object ("tls-database", "TLS database",
686 "TLS database with anchor certificate authorities used to validate the server certificate",
687 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
690 * GstRTSPClientSink::tls-interaction:
692 * A #GTlsInteraction object to be used when the connection or certificate
693 * database need to interact with the user. This will be used to prompt the
694 * user for passwords where necessary.
697 g_object_class_install_property (gobject_class, PROP_TLS_INTERACTION,
698 g_param_spec_object ("tls-interaction", "TLS interaction",
699 "A GTlsInteraction object to prompt the user for password or certificate",
700 G_TYPE_TLS_INTERACTION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
703 * GstRTSPClientSink::ntp-time-source:
705 * allows to select the time source that should be used
706 * for the NTP time in outgoing packets
709 g_object_class_install_property (gobject_class, PROP_NTP_TIME_SOURCE,
710 g_param_spec_enum ("ntp-time-source", "NTP Time Source",
711 "NTP time source for RTCP packets",
712 GST_TYPE_RTSP_CLIENT_SINK_NTP_TIME_SOURCE, DEFAULT_NTP_TIME_SOURCE,
713 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
716 * GstRTSPClientSink::user-agent:
718 * The string to set in the User-Agent header.
721 g_object_class_install_property (gobject_class, PROP_USER_AGENT,
722 g_param_spec_string ("user-agent", "User Agent",
723 "The User-Agent string to send to the server",
724 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
727 * GstRTSPClientSink::handle-request:
728 * @rtsp_client_sink: a #GstRTSPClientSink
729 * @request: a #GstRTSPMessage
730 * @response: a #GstRTSPMessage
732 * Handle a server request in @request and prepare @response.
734 * This signal is called from the streaming thread, you should therefore not
735 * do any state changes on @rtsp_client_sink because this might deadlock. If you want
736 * to modify the state as a result of this signal, post a
737 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
741 gst_rtsp_client_sink_signals[SIGNAL_HANDLE_REQUEST] =
742 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
743 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
744 G_TYPE_POINTER, G_TYPE_POINTER);
747 * GstRTSPClientSink::new-manager:
748 * @rtsp_client_sink: a #GstRTSPClientSink
749 * @manager: a #GstElement
751 * Emitted after a new manager (like rtpbin) was created and the default
752 * properties were configured.
755 gst_rtsp_client_sink_signals[SIGNAL_NEW_MANAGER] =
756 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
757 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
758 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
761 * GstRTSPClientSink::new-payloader:
762 * @rtsp_client_sink: a #GstRTSPClientSink
763 * @payloader: a #GstElement
765 * Emitted after a new RTP payloader was created and the default
766 * properties were configured.
769 gst_rtsp_client_sink_signals[SIGNAL_NEW_PAYLOADER] =
770 g_signal_new_class_handler ("new-payloader", G_TYPE_FROM_CLASS (klass),
771 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
772 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
775 * GstRTSPClientSink::request-rtcp-key:
776 * @rtsp_client_sink: a #GstRTSPClientSink
777 * @num: the stream number
779 * Signal emitted to get the crypto parameters relevant to the RTCP
780 * stream. User should provide the key and the RTCP encryption ciphers
781 * and authentication, and return them wrapped in a GstCaps.
784 gst_rtsp_client_sink_signals[SIGNAL_REQUEST_RTCP_KEY] =
785 g_signal_new ("request-rtcp-key", G_TYPE_FROM_CLASS (klass),
786 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, GST_TYPE_CAPS, 1, G_TYPE_UINT);
789 * GstRTSPClientSink::accept-certificate:
790 * @rtsp_client_sink: a #GstRTSPClientSink
791 * @peer_cert: the peer's #GTlsCertificate
792 * @errors: the problems with @peer_cert
793 * @user_data: user data set when the signal handler was connected.
795 * This will directly map to #GTlsConnection 's "accept-certificate"
796 * signal and be performed after the default checks of #GstRTSPConnection
797 * (checking against the #GTlsDatabase with the given #GTlsCertificateFlags)
798 * have failed. If no #GTlsDatabase is set on this connection, only this
799 * signal will be emitted.
803 gst_rtsp_client_sink_signals[SIGNAL_ACCEPT_CERTIFICATE] =
804 g_signal_new ("accept-certificate", G_TYPE_FROM_CLASS (klass),
805 G_SIGNAL_RUN_LAST, 0, g_signal_accumulator_true_handled, NULL, NULL,
806 G_TYPE_BOOLEAN, 3, G_TYPE_TLS_CONNECTION, G_TYPE_TLS_CERTIFICATE,
807 G_TYPE_TLS_CERTIFICATE_FLAGS);
809 gstelement_class->provide_clock = gst_rtsp_client_sink_provide_clock;
810 gstelement_class->change_state = gst_rtsp_client_sink_change_state;
811 gstelement_class->request_new_pad =
812 GST_DEBUG_FUNCPTR (gst_rtsp_client_sink_request_new_pad);
813 gstelement_class->release_pad =
814 GST_DEBUG_FUNCPTR (gst_rtsp_client_sink_release_pad);
816 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
817 &rtptemplate, GST_TYPE_RTSP_CLIENT_SINK_PAD);
819 gst_element_class_set_static_metadata (gstelement_class,
820 "RTSP RECORD client", "Sink/Network",
821 "Send data over the network via RTSP RECORD(RFC 2326)",
822 "Jan Schmidt <jan@centricular.com>");
824 gstbin_class->handle_message = gst_rtsp_client_sink_handle_message;
828 gst_rtsp_client_sink_init (GstRTSPClientSink * sink)
830 sink->conninfo.location = g_strdup (DEFAULT_LOCATION);
831 sink->protocols = DEFAULT_PROTOCOLS;
832 sink->debug = DEFAULT_DEBUG;
833 sink->retry = DEFAULT_RETRY;
834 sink->udp_timeout = DEFAULT_TIMEOUT;
835 gst_rtsp_client_sink_set_tcp_timeout (sink, DEFAULT_TCP_TIMEOUT);
836 sink->latency = DEFAULT_LATENCY_MS;
837 sink->rtx_time = DEFAULT_RTX_TIME_MS;
838 sink->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
839 gst_rtsp_client_sink_set_proxy (sink, DEFAULT_PROXY);
840 sink->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
841 sink->user_id = g_strdup (DEFAULT_USER_ID);
842 sink->user_pw = g_strdup (DEFAULT_USER_PW);
843 sink->client_port_range.min = 0;
844 sink->client_port_range.max = 0;
845 sink->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
846 sink->udp_reconnect = DEFAULT_UDP_RECONNECT;
847 sink->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
849 sink->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
850 sink->tls_database = DEFAULT_TLS_DATABASE;
851 sink->tls_interaction = DEFAULT_TLS_INTERACTION;
852 sink->ntp_time_source = DEFAULT_NTP_TIME_SOURCE;
853 sink->user_agent = g_strdup (DEFAULT_USER_AGENT);
855 sink->profiles = DEFAULT_PROFILES;
857 /* protects the streaming thread in interleaved mode or the polling
858 * thread in UDP mode. */
859 g_rec_mutex_init (&sink->stream_rec_lock);
861 /* protects our state changes from multiple invocations */
862 g_rec_mutex_init (&sink->state_rec_lock);
864 g_mutex_init (&sink->send_lock);
866 g_mutex_init (&sink->preroll_lock);
867 g_cond_init (&sink->preroll_cond);
869 sink->state = GST_RTSP_STATE_INVALID;
871 g_mutex_init (&sink->conninfo.send_lock);
872 g_mutex_init (&sink->conninfo.recv_lock);
874 g_mutex_init (&sink->block_streams_lock);
875 g_cond_init (&sink->block_streams_cond);
877 g_mutex_init (&sink->open_conn_lock);
878 g_cond_init (&sink->open_conn_cond);
880 sink->internal_bin = (GstBin *) gst_bin_new ("rtspbin");
881 gst_element_set_locked_state (GST_ELEMENT_CAST (sink->internal_bin), TRUE);
882 gst_bin_add (GST_BIN (sink), GST_ELEMENT_CAST (sink->internal_bin));
884 sink->next_dyn_pt = 96;
886 gst_sdp_message_init (&sink->cursdp);
888 GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_FLAG_SINK);
892 gst_rtsp_client_sink_finalize (GObject * object)
894 GstRTSPClientSink *rtsp_client_sink;
896 rtsp_client_sink = GST_RTSP_CLIENT_SINK (object);
898 gst_sdp_message_uninit (&rtsp_client_sink->cursdp);
900 g_free (rtsp_client_sink->conninfo.location);
901 gst_rtsp_url_free (rtsp_client_sink->conninfo.url);
902 g_free (rtsp_client_sink->conninfo.url_str);
903 g_free (rtsp_client_sink->user_id);
904 g_free (rtsp_client_sink->user_pw);
905 g_free (rtsp_client_sink->multi_iface);
906 g_free (rtsp_client_sink->user_agent);
908 if (rtsp_client_sink->uri_sdp) {
909 gst_sdp_message_free (rtsp_client_sink->uri_sdp);
910 rtsp_client_sink->uri_sdp = NULL;
912 if (rtsp_client_sink->provided_clock)
913 gst_object_unref (rtsp_client_sink->provided_clock);
915 if (rtsp_client_sink->sdes)
916 gst_structure_free (rtsp_client_sink->sdes);
918 if (rtsp_client_sink->tls_database)
919 g_object_unref (rtsp_client_sink->tls_database);
921 if (rtsp_client_sink->tls_interaction)
922 g_object_unref (rtsp_client_sink->tls_interaction);
925 g_rec_mutex_clear (&rtsp_client_sink->stream_rec_lock);
926 g_rec_mutex_clear (&rtsp_client_sink->state_rec_lock);
928 g_mutex_clear (&rtsp_client_sink->conninfo.send_lock);
929 g_mutex_clear (&rtsp_client_sink->conninfo.recv_lock);
931 g_mutex_clear (&rtsp_client_sink->send_lock);
933 g_mutex_clear (&rtsp_client_sink->preroll_lock);
934 g_cond_clear (&rtsp_client_sink->preroll_cond);
936 g_mutex_clear (&rtsp_client_sink->block_streams_lock);
937 g_cond_clear (&rtsp_client_sink->block_streams_cond);
939 g_mutex_clear (&rtsp_client_sink->open_conn_lock);
940 g_cond_clear (&rtsp_client_sink->open_conn_cond);
942 G_OBJECT_CLASS (parent_class)->finalize (object);
946 gst_rtp_payloader_filter_func (GstPluginFeature * feature, gpointer user_data)
948 GstElementFactory *factory = NULL;
951 if (!GST_IS_ELEMENT_FACTORY (feature))
954 factory = GST_ELEMENT_FACTORY (feature);
956 if (gst_plugin_feature_get_rank (feature) == GST_RANK_NONE)
959 if (!gst_element_factory_list_is_type (factory,
960 GST_ELEMENT_FACTORY_TYPE_PAYLOADER))
964 gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_KLASS);
965 if (strstr (klass, "Codec") == NULL)
967 if (strstr (klass, "RTP") == NULL)
974 compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
977 const gchar *rname1, *rname2;
978 GstRank rank1, rank2;
980 rname1 = gst_plugin_feature_get_name (f1);
981 rname2 = gst_plugin_feature_get_name (f2);
983 rank1 = gst_plugin_feature_get_rank (f1);
984 rank2 = gst_plugin_feature_get_rank (f2);
986 /* HACK: Prefer rtpmp4apay over rtpmp4gpay */
987 if (g_str_equal (rname1, "rtpmp4apay"))
988 rank1 = GST_RANK_SECONDARY + 1;
989 if (g_str_equal (rname2, "rtpmp4apay"))
990 rank2 = GST_RANK_SECONDARY + 1;
992 diff = rank2 - rank1;
996 diff = strcmp (rname2, rname1);
1002 gst_rtsp_client_sink_get_factories (void)
1004 static GList *payloader_factories = NULL;
1006 if (g_once_init_enter (&payloader_factories)) {
1007 GList *all_factories;
1010 gst_registry_feature_filter (gst_registry_get (),
1011 gst_rtp_payloader_filter_func, FALSE, NULL);
1013 all_factories = g_list_sort (all_factories, (GCompareFunc) compare_ranks);
1015 g_once_init_leave (&payloader_factories, all_factories);
1018 return payloader_factories;
1022 gst_rtsp_client_sink_get_payloader_caps (GstElementFactory * factory)
1025 GstCaps *caps = gst_caps_new_empty ();
1027 for (tmp = gst_element_factory_get_static_pad_templates (factory);
1028 tmp; tmp = g_list_next (tmp)) {
1029 GstStaticPadTemplate *template = tmp->data;
1031 if (template->direction == GST_PAD_SINK) {
1032 GstCaps *static_caps = gst_static_pad_template_get_caps (template);
1034 GST_LOG ("Found pad template %s on factory %s",
1035 template->name_template, gst_plugin_feature_get_name (factory));
1038 caps = gst_caps_merge (caps, static_caps);
1040 /* Early out, any is absorbing */
1041 if (gst_caps_is_any (caps))
1051 gst_rtsp_client_sink_get_all_payloaders_caps (void)
1053 /* Cached caps result */
1054 static GstCaps *ret;
1056 if (g_once_init_enter (&ret)) {
1057 GList *factories, *cur;
1058 GstCaps *caps = gst_caps_new_empty ();
1060 factories = gst_rtsp_client_sink_get_factories ();
1061 for (cur = factories; cur != NULL; cur = g_list_next (cur)) {
1062 GstElementFactory *factory = GST_ELEMENT_FACTORY (cur->data);
1063 GstCaps *payloader_caps =
1064 gst_rtsp_client_sink_get_payloader_caps (factory);
1066 caps = gst_caps_merge (caps, payloader_caps);
1068 /* Early out, any is absorbing */
1069 if (gst_caps_is_any (caps))
1074 g_once_init_leave (&ret, caps);
1077 /* Return cached result */
1078 return gst_caps_ref (ret);
1082 gst_rtsp_client_sink_make_payloader (GstCaps * caps)
1084 GList *factories, *cur;
1086 factories = gst_rtsp_client_sink_get_factories ();
1087 for (cur = factories; cur != NULL; cur = g_list_next (cur)) {
1088 GstElementFactory *factory = GST_ELEMENT_FACTORY (cur->data);
1091 for (tmp = gst_element_factory_get_static_pad_templates (factory);
1092 tmp; tmp = g_list_next (tmp)) {
1093 GstStaticPadTemplate *template = tmp->data;
1095 if (template->direction == GST_PAD_SINK) {
1096 GstCaps *static_caps = gst_static_pad_template_get_caps (template);
1097 GstElement *payloader = NULL;
1099 if (gst_caps_can_intersect (static_caps, caps)) {
1100 GST_DEBUG ("caps %" GST_PTR_FORMAT " intersects with template %"
1101 GST_PTR_FORMAT " for payloader %s", caps, static_caps,
1102 gst_plugin_feature_get_name (factory));
1103 payloader = gst_element_factory_create (factory, NULL);
1106 gst_caps_unref (static_caps);
1117 static GstRTSPStream *
1118 gst_rtsp_client_sink_create_stream (GstRTSPClientSink * sink,
1119 GstRTSPStreamContext * context, GstElement * payloader, GstPad * pad)
1121 GstRTSPStream *stream = NULL;
1122 guint pt, aux_pt, ulpfec_pt;
1124 GST_OBJECT_LOCK (sink);
1126 g_object_get (G_OBJECT (payloader), "pt", &pt, NULL);
1127 if (pt >= 96 && pt <= sink->next_dyn_pt) {
1128 /* Payloader has a dynamic PT, but one that's already used */
1129 /* FIXME: Create a caps->ptmap instead? */
1130 pt = sink->next_dyn_pt;
1135 GST_DEBUG_OBJECT (sink, "Assigning pt %u to stream %d", pt, context->index);
1137 sink->next_dyn_pt++;
1139 GST_DEBUG_OBJECT (sink, "Keeping existing pt %u for stream %d",
1140 pt, context->index);
1143 aux_pt = sink->next_dyn_pt;
1146 sink->next_dyn_pt++;
1148 ulpfec_pt = sink->next_dyn_pt;
1149 if (ulpfec_pt > 127)
1151 sink->next_dyn_pt++;
1153 GST_OBJECT_UNLOCK (sink);
1156 g_object_set (G_OBJECT (payloader), "pt", pt, NULL);
1158 stream = gst_rtsp_stream_new (context->index, payloader, pad);
1160 gst_rtsp_stream_set_client_side (stream, TRUE);
1161 gst_rtsp_stream_set_retransmission_time (stream,
1162 (GstClockTime) (sink->rtx_time) * GST_MSECOND);
1163 gst_rtsp_stream_set_protocols (stream, sink->protocols);
1164 gst_rtsp_stream_set_profiles (stream, sink->profiles);
1165 gst_rtsp_stream_set_retransmission_pt (stream, aux_pt);
1166 gst_rtsp_stream_set_buffer_size (stream, sink->udp_buffer_size);
1167 if (sink->rtp_blocksize > 0)
1168 gst_rtsp_stream_set_mtu (stream, sink->rtp_blocksize);
1169 gst_rtsp_stream_set_multicast_iface (stream, sink->multi_iface);
1171 gst_rtsp_stream_set_ulpfec_pt (stream, ulpfec_pt);
1172 gst_rtsp_stream_set_ulpfec_percentage (stream, context->ulpfec_percentage);
1176 gst_rtsp_stream_set_address_pool (stream, priv->pool);
1181 GST_OBJECT_UNLOCK (sink);
1183 GST_ELEMENT_ERROR (sink, RESOURCE, NO_SPACE_LEFT, (NULL),
1184 ("Ran out of dynamic payload types."));
1189 static GstPadProbeReturn
1190 handle_payloader_block (GstPad * pad, GstPadProbeInfo * info,
1191 GstRTSPStreamContext * context)
1193 GstRTSPClientSink *sink = context->parent;
1195 GST_INFO_OBJECT (sink, "Block on pad %" GST_PTR_FORMAT, pad);
1197 g_mutex_lock (&sink->preroll_lock);
1198 context->prerolled = TRUE;
1199 g_cond_broadcast (&sink->preroll_cond);
1200 g_mutex_unlock (&sink->preroll_lock);
1202 GST_INFO_OBJECT (sink, "Announced preroll on pad %" GST_PTR_FORMAT, pad);
1204 return GST_PAD_PROBE_OK;
1208 gst_rtsp_client_sink_setup_payloader (GstRTSPClientSink * sink, GstPad * pad,
1211 GstRTSPStreamContext *context;
1212 GstRtspClientSinkPad *cspad = GST_RTSP_CLIENT_SINK_PAD (pad);
1214 GstElement *payloader;
1215 GstPad *sinkpad, *srcpad, *ghostsink;
1217 context = gst_pad_get_element_private (pad);
1219 if (cspad->custom_payloader) {
1220 payloader = cspad->custom_payloader;
1222 /* Find the payloader. */
1223 payloader = gst_rtsp_client_sink_make_payloader (caps);
1226 if (payloader == NULL)
1229 GST_DEBUG_OBJECT (sink, "Configuring payloader %" GST_PTR_FORMAT
1230 " for pad %" GST_PTR_FORMAT, payloader, pad);
1232 sinkpad = gst_element_get_static_pad (payloader, "sink");
1233 if (sinkpad == NULL)
1236 srcpad = gst_element_get_static_pad (payloader, "src");
1240 gst_bin_add (GST_BIN (sink->internal_bin), payloader);
1241 ghostsink = gst_ghost_pad_new (NULL, sinkpad);
1242 gst_pad_set_active (ghostsink, TRUE);
1243 gst_element_add_pad (GST_ELEMENT (sink->internal_bin), ghostsink);
1245 g_signal_emit (sink, gst_rtsp_client_sink_signals[SIGNAL_NEW_PAYLOADER], 0,
1248 GST_RTSP_STATE_LOCK (sink);
1249 context->payloader_block_id =
1250 gst_pad_add_probe (srcpad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
1251 (GstPadProbeCallback) handle_payloader_block, context, NULL);
1252 context->payloader = payloader;
1254 payloader = gst_object_ref (payloader);
1256 gst_ghost_pad_set_target (GST_GHOST_PAD (pad), ghostsink);
1257 gst_object_unref (GST_OBJECT (sinkpad));
1258 GST_RTSP_STATE_UNLOCK (sink);
1260 context->ulpfec_percentage = cspad->ulpfec_percentage;
1262 gst_element_sync_state_with_parent (payloader);
1264 gst_object_unref (payloader);
1265 gst_object_unref (GST_OBJECT (srcpad));
1270 GST_ERROR_OBJECT (sink,
1271 "Could not find sink pad on payloader %" GST_PTR_FORMAT, payloader);
1272 if (!cspad->custom_payloader)
1273 gst_object_unref (payloader);
1277 GST_ERROR_OBJECT (sink,
1278 "Could not find src pad on payloader %" GST_PTR_FORMAT, payloader);
1279 gst_object_unref (GST_OBJECT (sinkpad));
1280 gst_object_unref (payloader);
1285 gst_rtsp_client_sink_sinkpad_event (GstPad * pad, GstObject * parent,
1288 if (GST_EVENT_TYPE (event) == GST_EVENT_CAPS) {
1289 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD (pad));
1290 if (target == NULL) {
1293 /* No target yet - choose a payloader and configure it */
1294 gst_event_parse_caps (event, &caps);
1296 GST_DEBUG_OBJECT (parent,
1297 "Have set caps event on pad %" GST_PTR_FORMAT
1298 " caps %" GST_PTR_FORMAT, pad, caps);
1300 if (!gst_rtsp_client_sink_setup_payloader (GST_RTSP_CLIENT_SINK (parent),
1302 GstRtspClientSinkPad *cspad = GST_RTSP_CLIENT_SINK_PAD (pad);
1303 GST_ELEMENT_ERROR (parent, CORE, NEGOTIATION,
1304 ("Could not create payloader"),
1305 ("Custom payloader: %p, caps: %" GST_PTR_FORMAT,
1306 cspad->custom_payloader, caps));
1307 gst_event_unref (event);
1311 gst_object_unref (target);
1315 return gst_pad_event_default (pad, parent, event);
1319 gst_rtsp_client_sink_sinkpad_query (GstPad * pad, GstObject * parent,
1322 if (GST_QUERY_TYPE (query) == GST_QUERY_CAPS) {
1323 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD (pad));
1324 if (target == NULL) {
1325 GstRtspClientSinkPad *cspad = GST_RTSP_CLIENT_SINK_PAD (pad);
1328 if (cspad->custom_payloader) {
1330 gst_element_get_static_pad (cspad->custom_payloader, "sink");
1333 caps = gst_pad_query_caps (sinkpad, NULL);
1334 gst_object_unref (sinkpad);
1336 GST_ELEMENT_ERROR (parent, CORE, NEGOTIATION, (NULL),
1337 ("Custom payloaders are expected to expose a sink pad named 'sink'"));
1341 /* No target yet - return the union of all payloader caps */
1342 caps = gst_rtsp_client_sink_get_all_payloaders_caps ();
1345 GST_TRACE_OBJECT (parent, "Returning payloader caps %" GST_PTR_FORMAT,
1348 gst_query_set_caps_result (query, caps);
1349 gst_caps_unref (caps);
1353 gst_object_unref (target);
1356 return gst_pad_query_default (pad, parent, query);
1360 gst_rtsp_client_sink_request_new_pad (GstElement * element,
1361 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
1363 GstRTSPClientSink *sink = GST_RTSP_CLIENT_SINK (element);
1365 GstRTSPStreamContext *context;
1366 guint idx = (guint) - 1;
1369 g_mutex_lock (&sink->preroll_lock);
1370 if (sink->streams_collected) {
1371 GST_WARNING_OBJECT (element, "Can't add streams to a running session");
1372 g_mutex_unlock (&sink->preroll_lock);
1375 g_mutex_unlock (&sink->preroll_lock);
1377 GST_OBJECT_LOCK (sink);
1379 if (!sscanf (name, "sink_%u", &idx)) {
1380 GST_OBJECT_UNLOCK (sink);
1381 GST_ERROR_OBJECT (element, "Invalid sink pad name %s", name);
1385 if (idx >= sink->next_pad_id)
1386 sink->next_pad_id = idx + 1;
1388 if (idx == (guint) - 1) {
1389 idx = sink->next_pad_id;
1390 sink->next_pad_id++;
1392 GST_OBJECT_UNLOCK (sink);
1394 tmpname = g_strdup_printf ("sink_%u", idx);
1395 pad = gst_rtsp_client_sink_pad_new (templ, tmpname);
1398 GST_DEBUG_OBJECT (element, "Creating request pad %" GST_PTR_FORMAT, pad);
1400 gst_pad_set_event_function (pad,
1401 GST_DEBUG_FUNCPTR (gst_rtsp_client_sink_sinkpad_event));
1402 gst_pad_set_query_function (pad,
1403 GST_DEBUG_FUNCPTR (gst_rtsp_client_sink_sinkpad_query));
1405 context = g_new0 (GstRTSPStreamContext, 1);
1406 context->parent = sink;
1407 context->index = idx;
1409 gst_pad_set_element_private (pad, context);
1411 /* The rest of the context is configured on a caps set */
1412 gst_pad_set_active (pad, TRUE);
1413 gst_element_add_pad (element, pad);
1414 gst_child_proxy_child_added (GST_CHILD_PROXY (element), G_OBJECT (pad),
1415 GST_PAD_NAME (pad));
1417 (void) gst_rtsp_client_sink_get_factories ();
1419 g_mutex_init (&context->conninfo.send_lock);
1420 g_mutex_init (&context->conninfo.recv_lock);
1422 GST_RTSP_STATE_LOCK (sink);
1423 sink->contexts = g_list_prepend (sink->contexts, context);
1424 GST_RTSP_STATE_UNLOCK (sink);
1430 gst_rtsp_client_sink_release_pad (GstElement * element, GstPad * pad)
1432 GstRTSPClientSink *sink = GST_RTSP_CLIENT_SINK (element);
1433 GstRTSPStreamContext *context;
1435 context = gst_pad_get_element_private (pad);
1437 /* FIXME: we may need to change our blocking state waiting for
1438 * GstRTSPStreamBlocking messages */
1440 GST_RTSP_STATE_LOCK (sink);
1441 sink->contexts = g_list_remove (sink->contexts, context);
1442 GST_RTSP_STATE_UNLOCK (sink);
1444 /* FIXME: Shut down and clean up streaming on this pad,
1445 * do teardown if needed */
1446 GST_LOG_OBJECT (sink,
1447 "Cleaning up payloader and stream for released pad %" GST_PTR_FORMAT,
1450 if (context->stream_transport) {
1451 gst_rtsp_stream_transport_set_active (context->stream_transport, FALSE);
1452 gst_object_unref (context->stream_transport);
1453 context->stream_transport = NULL;
1455 if (context->stream) {
1456 if (context->joined) {
1457 gst_rtsp_stream_leave_bin (context->stream,
1458 GST_BIN (sink->internal_bin), sink->rtpbin);
1459 context->joined = FALSE;
1461 gst_object_unref (context->stream);
1462 context->stream = NULL;
1464 if (context->srtcpparams)
1465 gst_caps_unref (context->srtcpparams);
1467 g_free (context->conninfo.location);
1468 context->conninfo.location = NULL;
1470 g_mutex_clear (&context->conninfo.send_lock);
1471 g_mutex_clear (&context->conninfo.recv_lock);
1475 gst_element_remove_pad (element, pad);
1479 gst_rtsp_client_sink_provide_clock (GstElement * element)
1481 GstRTSPClientSink *sink = GST_RTSP_CLIENT_SINK (element);
1484 if ((clock = sink->provided_clock) != NULL)
1485 gst_object_ref (clock);
1490 /* a proxy string of the format [user:passwd@]host[:port] */
1492 gst_rtsp_client_sink_set_proxy (GstRTSPClientSink * rtsp, const gchar * proxy)
1494 gchar *p, *at, *col;
1496 g_free (rtsp->proxy_user);
1497 rtsp->proxy_user = NULL;
1498 g_free (rtsp->proxy_passwd);
1499 rtsp->proxy_passwd = NULL;
1500 g_free (rtsp->proxy_host);
1501 rtsp->proxy_host = NULL;
1502 rtsp->proxy_port = 0;
1504 p = (gchar *) proxy;
1509 /* we allow http:// in front but ignore it */
1510 if (g_str_has_prefix (p, "http://"))
1513 at = strchr (p, '@');
1515 /* look for user:passwd */
1516 col = strchr (proxy, ':');
1517 if (col == NULL || col > at)
1520 rtsp->proxy_user = g_strndup (p, col - p);
1522 rtsp->proxy_passwd = g_strndup (col, at - col);
1527 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
1528 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
1529 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
1530 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
1531 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
1532 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
1533 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
1536 col = strchr (p, ':');
1539 /* everything before the colon is the hostname */
1540 rtsp->proxy_host = g_strndup (p, col - p);
1542 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
1544 rtsp->proxy_host = g_strdup (p);
1545 rtsp->proxy_port = 8080;
1551 gst_rtsp_client_sink_set_tcp_timeout (GstRTSPClientSink * rtsp_client_sink,
1554 rtsp_client_sink->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
1555 rtsp_client_sink->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
1558 rtsp_client_sink->ptcp_timeout = &rtsp_client_sink->tcp_timeout;
1560 rtsp_client_sink->ptcp_timeout = NULL;
1564 gst_rtsp_client_sink_set_property (GObject * object, guint prop_id,
1565 const GValue * value, GParamSpec * pspec)
1567 GstRTSPClientSink *rtsp_client_sink;
1569 rtsp_client_sink = GST_RTSP_CLIENT_SINK (object);
1573 gst_rtsp_client_sink_uri_set_uri (GST_URI_HANDLER (rtsp_client_sink),
1574 g_value_get_string (value), NULL);
1576 case PROP_PROTOCOLS:
1577 rtsp_client_sink->protocols = g_value_get_flags (value);
1580 rtsp_client_sink->profiles = g_value_get_flags (value);
1583 rtsp_client_sink->debug = g_value_get_boolean (value);
1586 rtsp_client_sink->retry = g_value_get_uint (value);
1589 rtsp_client_sink->udp_timeout = g_value_get_uint64 (value);
1591 case PROP_TCP_TIMEOUT:
1592 gst_rtsp_client_sink_set_tcp_timeout (rtsp_client_sink,
1593 g_value_get_uint64 (value));
1596 rtsp_client_sink->latency = g_value_get_uint (value);
1599 rtsp_client_sink->rtx_time = g_value_get_uint (value);
1601 case PROP_DO_RTSP_KEEP_ALIVE:
1602 rtsp_client_sink->do_rtsp_keep_alive = g_value_get_boolean (value);
1605 gst_rtsp_client_sink_set_proxy (rtsp_client_sink,
1606 g_value_get_string (value));
1609 if (rtsp_client_sink->prop_proxy_id)
1610 g_free (rtsp_client_sink->prop_proxy_id);
1611 rtsp_client_sink->prop_proxy_id = g_value_dup_string (value);
1614 if (rtsp_client_sink->prop_proxy_pw)
1615 g_free (rtsp_client_sink->prop_proxy_pw);
1616 rtsp_client_sink->prop_proxy_pw = g_value_dup_string (value);
1618 case PROP_RTP_BLOCKSIZE:
1619 rtsp_client_sink->rtp_blocksize = g_value_get_uint (value);
1622 if (rtsp_client_sink->user_id)
1623 g_free (rtsp_client_sink->user_id);
1624 rtsp_client_sink->user_id = g_value_dup_string (value);
1627 if (rtsp_client_sink->user_pw)
1628 g_free (rtsp_client_sink->user_pw);
1629 rtsp_client_sink->user_pw = g_value_dup_string (value);
1631 case PROP_PORT_RANGE:
1635 str = g_value_get_string (value);
1636 if (!str || !sscanf (str, "%u-%u",
1637 &rtsp_client_sink->client_port_range.min,
1638 &rtsp_client_sink->client_port_range.max)) {
1639 rtsp_client_sink->client_port_range.min = 0;
1640 rtsp_client_sink->client_port_range.max = 0;
1644 case PROP_UDP_BUFFER_SIZE:
1645 rtsp_client_sink->udp_buffer_size = g_value_get_int (value);
1647 case PROP_UDP_RECONNECT:
1648 rtsp_client_sink->udp_reconnect = g_value_get_boolean (value);
1650 case PROP_MULTICAST_IFACE:
1651 g_free (rtsp_client_sink->multi_iface);
1653 if (g_value_get_string (value) == NULL)
1654 rtsp_client_sink->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1656 rtsp_client_sink->multi_iface = g_value_dup_string (value);
1659 rtsp_client_sink->sdes = g_value_dup_boxed (value);
1661 case PROP_TLS_VALIDATION_FLAGS:
1662 rtsp_client_sink->tls_validation_flags = g_value_get_flags (value);
1664 case PROP_TLS_DATABASE:
1665 g_clear_object (&rtsp_client_sink->tls_database);
1666 rtsp_client_sink->tls_database = g_value_dup_object (value);
1668 case PROP_TLS_INTERACTION:
1669 g_clear_object (&rtsp_client_sink->tls_interaction);
1670 rtsp_client_sink->tls_interaction = g_value_dup_object (value);
1672 case PROP_NTP_TIME_SOURCE:
1673 rtsp_client_sink->ntp_time_source = g_value_get_enum (value);
1675 case PROP_USER_AGENT:
1676 g_free (rtsp_client_sink->user_agent);
1677 rtsp_client_sink->user_agent = g_value_dup_string (value);
1680 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1686 gst_rtsp_client_sink_get_property (GObject * object, guint prop_id,
1687 GValue * value, GParamSpec * pspec)
1689 GstRTSPClientSink *rtsp_client_sink;
1691 rtsp_client_sink = GST_RTSP_CLIENT_SINK (object);
1695 g_value_set_string (value, rtsp_client_sink->conninfo.location);
1697 case PROP_PROTOCOLS:
1698 g_value_set_flags (value, rtsp_client_sink->protocols);
1701 g_value_set_flags (value, rtsp_client_sink->profiles);
1704 g_value_set_boolean (value, rtsp_client_sink->debug);
1707 g_value_set_uint (value, rtsp_client_sink->retry);
1710 g_value_set_uint64 (value, rtsp_client_sink->udp_timeout);
1712 case PROP_TCP_TIMEOUT:
1716 timeout = rtsp_client_sink->tcp_timeout.tv_sec * G_USEC_PER_SEC +
1717 rtsp_client_sink->tcp_timeout.tv_usec;
1718 g_value_set_uint64 (value, timeout);
1722 g_value_set_uint (value, rtsp_client_sink->latency);
1725 g_value_set_uint (value, rtsp_client_sink->rtx_time);
1727 case PROP_DO_RTSP_KEEP_ALIVE:
1728 g_value_set_boolean (value, rtsp_client_sink->do_rtsp_keep_alive);
1734 if (rtsp_client_sink->proxy_host) {
1736 g_strdup_printf ("%s:%d", rtsp_client_sink->proxy_host,
1737 rtsp_client_sink->proxy_port);
1741 g_value_take_string (value, str);
1745 g_value_set_string (value, rtsp_client_sink->prop_proxy_id);
1748 g_value_set_string (value, rtsp_client_sink->prop_proxy_pw);
1750 case PROP_RTP_BLOCKSIZE:
1751 g_value_set_uint (value, rtsp_client_sink->rtp_blocksize);
1754 g_value_set_string (value, rtsp_client_sink->user_id);
1757 g_value_set_string (value, rtsp_client_sink->user_pw);
1759 case PROP_PORT_RANGE:
1763 if (rtsp_client_sink->client_port_range.min != 0) {
1764 str = g_strdup_printf ("%u-%u", rtsp_client_sink->client_port_range.min,
1765 rtsp_client_sink->client_port_range.max);
1769 g_value_take_string (value, str);
1772 case PROP_UDP_BUFFER_SIZE:
1773 g_value_set_int (value, rtsp_client_sink->udp_buffer_size);
1775 case PROP_UDP_RECONNECT:
1776 g_value_set_boolean (value, rtsp_client_sink->udp_reconnect);
1778 case PROP_MULTICAST_IFACE:
1779 g_value_set_string (value, rtsp_client_sink->multi_iface);
1782 g_value_set_boxed (value, rtsp_client_sink->sdes);
1784 case PROP_TLS_VALIDATION_FLAGS:
1785 g_value_set_flags (value, rtsp_client_sink->tls_validation_flags);
1787 case PROP_TLS_DATABASE:
1788 g_value_set_object (value, rtsp_client_sink->tls_database);
1790 case PROP_TLS_INTERACTION:
1791 g_value_set_object (value, rtsp_client_sink->tls_interaction);
1793 case PROP_NTP_TIME_SOURCE:
1794 g_value_set_enum (value, rtsp_client_sink->ntp_time_source);
1796 case PROP_USER_AGENT:
1797 g_value_set_string (value, rtsp_client_sink->user_agent);
1800 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1805 static const gchar *
1806 get_aggregate_control (GstRTSPClientSink * sink)
1811 base = sink->control;
1812 else if (sink->content_base)
1813 base = sink->content_base;
1814 else if (sink->conninfo.url_str)
1815 base = sink->conninfo.url_str;
1823 gst_rtsp_client_sink_cleanup (GstRTSPClientSink * sink)
1827 GST_DEBUG_OBJECT (sink, "cleanup");
1829 gst_element_set_state (GST_ELEMENT (sink->internal_bin), GST_STATE_NULL);
1831 /* Clean up any left over stream objects */
1832 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
1833 GstRTSPStreamContext *context = (GstRTSPStreamContext *) (walk->data);
1834 if (context->stream_transport) {
1835 gst_rtsp_stream_transport_set_active (context->stream_transport, FALSE);
1836 gst_object_unref (context->stream_transport);
1837 context->stream_transport = NULL;
1840 if (context->stream) {
1841 if (context->joined) {
1842 gst_rtsp_stream_leave_bin (context->stream,
1843 GST_BIN (sink->internal_bin), sink->rtpbin);
1844 context->joined = FALSE;
1846 gst_object_unref (context->stream);
1847 context->stream = NULL;
1850 if (context->srtcpparams) {
1851 gst_caps_unref (context->srtcpparams);
1852 context->srtcpparams = NULL;
1854 g_free (context->conninfo.location);
1855 context->conninfo.location = NULL;
1859 gst_element_set_state (sink->rtpbin, GST_STATE_NULL);
1860 gst_bin_remove (GST_BIN_CAST (sink->internal_bin), sink->rtpbin);
1861 sink->rtpbin = NULL;
1864 g_free (sink->content_base);
1865 sink->content_base = NULL;
1867 g_free (sink->control);
1868 sink->control = NULL;
1871 gst_rtsp_range_free (sink->range);
1874 /* don't clear the SDP when it was used in the url */
1875 if (sink->uri_sdp && !sink->from_sdp) {
1876 gst_sdp_message_free (sink->uri_sdp);
1877 sink->uri_sdp = NULL;
1880 if (sink->provided_clock) {
1881 gst_object_unref (sink->provided_clock);
1882 sink->provided_clock = NULL;
1885 g_free (sink->server_ip);
1886 sink->server_ip = NULL;
1888 sink->next_pad_id = 0;
1889 sink->next_dyn_pt = 96;
1892 static GstRTSPResult
1893 gst_rtsp_client_sink_connection_send (GstRTSPClientSink * sink,
1894 GstRTSPConnInfo * conninfo, GstRTSPMessage * message, GTimeVal * timeout)
1898 if (conninfo->connection) {
1899 g_mutex_lock (&conninfo->send_lock);
1900 ret = gst_rtsp_connection_send (conninfo->connection, message, timeout);
1901 g_mutex_unlock (&conninfo->send_lock);
1903 ret = GST_RTSP_ERROR;
1909 static GstRTSPResult
1910 gst_rtsp_client_sink_connection_receive (GstRTSPClientSink * sink,
1911 GstRTSPConnInfo * conninfo, GstRTSPMessage * message, GTimeVal * timeout)
1915 if (conninfo->connection) {
1916 g_mutex_lock (&conninfo->recv_lock);
1917 ret = gst_rtsp_connection_receive (conninfo->connection, message, timeout);
1918 g_mutex_unlock (&conninfo->recv_lock);
1920 ret = GST_RTSP_ERROR;
1927 accept_certificate_cb (GTlsConnection * conn, GTlsCertificate * peer_cert,
1928 GTlsCertificateFlags errors, gpointer user_data)
1930 GstRTSPClientSink *sink = user_data;
1931 gboolean accept = FALSE;
1933 g_signal_emit (sink, gst_rtsp_client_sink_signals[SIGNAL_ACCEPT_CERTIFICATE],
1934 0, conn, peer_cert, errors, &accept);
1939 static GstRTSPResult
1940 gst_rtsp_conninfo_connect (GstRTSPClientSink * sink, GstRTSPConnInfo * info,
1945 if (info->connection == NULL) {
1946 if (info->url == NULL) {
1947 GST_DEBUG_OBJECT (sink, "parsing uri (%s)...", info->location);
1948 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
1952 /* create connection */
1953 GST_DEBUG_OBJECT (sink, "creating connection (%s)...", info->location);
1954 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
1955 goto could_not_create;
1958 g_free (info->url_str);
1959 info->url_str = gst_rtsp_url_get_request_uri (info->url);
1961 GST_DEBUG_OBJECT (sink, "sanitized uri %s", info->url_str);
1963 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
1964 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
1965 sink->tls_validation_flags))
1966 GST_WARNING_OBJECT (sink, "Unable to set TLS validation flags");
1968 if (sink->tls_database)
1969 gst_rtsp_connection_set_tls_database (info->connection,
1970 sink->tls_database);
1972 if (sink->tls_interaction)
1973 gst_rtsp_connection_set_tls_interaction (info->connection,
1974 sink->tls_interaction);
1976 gst_rtsp_connection_set_accept_certificate_func (info->connection,
1977 accept_certificate_cb, sink, NULL);
1980 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
1981 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
1983 if (sink->proxy_host) {
1984 GST_DEBUG_OBJECT (sink, "setting proxy %s:%d", sink->proxy_host,
1986 gst_rtsp_connection_set_proxy (info->connection, sink->proxy_host,
1991 if (!info->connected) {
1994 GST_ELEMENT_PROGRESS (sink, CONTINUE, "connect",
1995 ("Connecting to %s", info->location));
1996 GST_DEBUG_OBJECT (sink, "connecting (%s)...", info->location);
1998 gst_rtsp_connection_connect (info->connection,
1999 sink->ptcp_timeout)) < 0)
2000 goto could_not_connect;
2002 info->connected = TRUE;
2009 GST_ERROR_OBJECT (sink, "No valid RTSP URL was provided");
2014 gchar *str = gst_rtsp_strresult (res);
2015 GST_ERROR_OBJECT (sink, "Could not create connection. (%s)", str);
2021 gchar *str = gst_rtsp_strresult (res);
2022 GST_ERROR_OBJECT (sink, "Could not connect to server. (%s)", str);
2028 static GstRTSPResult
2029 gst_rtsp_conninfo_close (GstRTSPClientSink * sink, GstRTSPConnInfo * info,
2032 GST_RTSP_STATE_LOCK (sink);
2033 if (info->connected) {
2034 GST_DEBUG_OBJECT (sink, "closing connection...");
2035 gst_rtsp_connection_close (info->connection);
2036 info->connected = FALSE;
2038 if (free && info->connection) {
2039 /* free connection */
2040 GST_DEBUG_OBJECT (sink, "freeing connection...");
2041 gst_rtsp_connection_free (info->connection);
2042 g_mutex_lock (&sink->preroll_lock);
2043 info->connection = NULL;
2044 g_cond_broadcast (&sink->preroll_cond);
2045 g_mutex_unlock (&sink->preroll_lock);
2047 GST_RTSP_STATE_UNLOCK (sink);
2051 static GstRTSPResult
2052 gst_rtsp_conninfo_reconnect (GstRTSPClientSink * sink, GstRTSPConnInfo * info,
2057 GST_DEBUG_OBJECT (sink, "reconnecting connection...");
2058 gst_rtsp_conninfo_close (sink, info, FALSE);
2059 res = gst_rtsp_conninfo_connect (sink, info, async);
2065 gst_rtsp_client_sink_connection_flush (GstRTSPClientSink * sink, gboolean flush)
2069 GST_DEBUG_OBJECT (sink, "set flushing %d", flush);
2070 g_mutex_lock (&sink->preroll_lock);
2071 if (sink->conninfo.connection && sink->conninfo.flushing != flush) {
2072 GST_DEBUG_OBJECT (sink, "connection flush");
2073 gst_rtsp_connection_flush (sink->conninfo.connection, flush);
2074 sink->conninfo.flushing = flush;
2076 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
2077 GstRTSPStreamContext *stream = (GstRTSPStreamContext *) walk->data;
2078 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
2079 GST_DEBUG_OBJECT (sink, "stream %p flush", stream);
2080 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
2081 stream->conninfo.flushing = flush;
2084 g_cond_broadcast (&sink->preroll_cond);
2085 g_mutex_unlock (&sink->preroll_lock);
2088 static GstRTSPResult
2089 gst_rtsp_client_sink_init_request (GstRTSPClientSink * sink,
2090 GstRTSPMessage * msg, GstRTSPMethod method, const gchar * uri)
2094 res = gst_rtsp_message_init_request (msg, method, uri);
2098 /* set user-agent */
2099 if (sink->user_agent)
2100 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_USER_AGENT,
2106 /* FIXME, handle server request, reply with OK, for now */
2107 static GstRTSPResult
2108 gst_rtsp_client_sink_handle_request (GstRTSPClientSink * sink,
2109 GstRTSPConnInfo * conninfo, GstRTSPMessage * request)
2111 GstRTSPMessage response = { 0 };
2114 GST_DEBUG_OBJECT (sink, "got server request message");
2117 gst_rtsp_message_dump (request);
2119 /* default implementation, send OK */
2120 GST_DEBUG_OBJECT (sink, "prepare OK reply");
2122 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
2127 /* let app parse and reply */
2128 g_signal_emit (sink, gst_rtsp_client_sink_signals[SIGNAL_HANDLE_REQUEST],
2129 0, request, &response);
2132 gst_rtsp_message_dump (&response);
2134 res = gst_rtsp_client_sink_connection_send (sink, conninfo, &response, NULL);
2138 gst_rtsp_message_unset (&response);
2145 gst_rtsp_message_unset (&response);
2150 /* send server keep-alive */
2151 static GstRTSPResult
2152 gst_rtsp_client_sink_send_keep_alive (GstRTSPClientSink * sink)
2154 GstRTSPMessage request = { 0 };
2156 GstRTSPMethod method;
2157 const gchar *control;
2159 if (sink->do_rtsp_keep_alive == FALSE) {
2160 GST_DEBUG_OBJECT (sink, "do-rtsp-keep-alive is FALSE, not sending.");
2161 gst_rtsp_connection_reset_timeout (sink->conninfo.connection);
2165 GST_DEBUG_OBJECT (sink, "creating server keep-alive");
2167 /* find a method to use for keep-alive */
2168 if (sink->methods & GST_RTSP_GET_PARAMETER)
2169 method = GST_RTSP_GET_PARAMETER;
2171 method = GST_RTSP_OPTIONS;
2173 control = get_aggregate_control (sink);
2174 if (control == NULL)
2177 res = gst_rtsp_client_sink_init_request (sink, &request, method, control);
2182 gst_rtsp_message_dump (&request);
2185 gst_rtsp_client_sink_connection_send (sink, &sink->conninfo,
2190 gst_rtsp_connection_reset_timeout (sink->conninfo.connection);
2191 gst_rtsp_message_unset (&request);
2198 GST_WARNING_OBJECT (sink, "no control url to send keepalive");
2203 gchar *str = gst_rtsp_strresult (res);
2205 gst_rtsp_message_unset (&request);
2206 GST_ELEMENT_WARNING (sink, RESOURCE, WRITE, (NULL),
2207 ("Could not send keep-alive. (%s)", str));
2213 static GstFlowReturn
2214 gst_rtsp_client_sink_loop_rx (GstRTSPClientSink * sink)
2217 GstRTSPMessage message = { 0 };
2221 GTimeVal tv_timeout;
2223 /* get the next timeout interval */
2224 gst_rtsp_connection_next_timeout (sink->conninfo.connection, &tv_timeout);
2226 GST_DEBUG_OBJECT (sink, "doing receive with timeout %d seconds",
2227 (gint) tv_timeout.tv_sec);
2229 gst_rtsp_message_unset (&message);
2231 /* we should continue reading the TCP socket because the server might
2232 * send us requests. When the session timeout expires, we need to send a
2233 * keep-alive request to keep the session open. */
2235 gst_rtsp_client_sink_connection_receive (sink,
2236 &sink->conninfo, &message, &tv_timeout);
2240 GST_DEBUG_OBJECT (sink, "we received a server message");
2242 case GST_RTSP_EINTR:
2243 /* we got interrupted, see what we have to do */
2245 case GST_RTSP_ETIMEOUT:
2246 /* send keep-alive, ignore the result, a warning will be posted. */
2247 GST_DEBUG_OBJECT (sink, "timeout, sending keep-alive");
2249 gst_rtsp_client_sink_send_keep_alive (sink)) == GST_RTSP_EINTR)
2253 /* server closed the connection. not very fatal for UDP, reconnect and
2254 * see what happens. */
2255 GST_ELEMENT_WARNING (sink, RESOURCE, READ, (NULL),
2256 ("The server closed the connection."));
2257 if (sink->udp_reconnect) {
2259 gst_rtsp_conninfo_reconnect (sink, &sink->conninfo,
2268 GST_DEBUG_OBJECT (sink, "An ethernet problem occured.");
2270 GST_ELEMENT_WARNING (sink, RESOURCE, READ, (NULL),
2271 ("Unhandled return value %d.", res));
2275 switch (message.type) {
2276 case GST_RTSP_MESSAGE_REQUEST:
2277 /* server sends us a request message, handle it */
2279 gst_rtsp_client_sink_handle_request (sink,
2280 &sink->conninfo, &message);
2281 if (res == GST_RTSP_EEOF)
2284 goto handle_request_failed;
2286 case GST_RTSP_MESSAGE_RESPONSE:
2287 /* we ignore response and data messages */
2288 GST_DEBUG_OBJECT (sink, "ignoring response message");
2290 gst_rtsp_message_dump (&message);
2291 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
2292 GST_DEBUG_OBJECT (sink, "but is Unauthorized response ...");
2293 if (gst_rtsp_client_sink_setup_auth (sink, &message) && !(retry++)) {
2294 GST_DEBUG_OBJECT (sink, "so retrying keep-alive");
2296 gst_rtsp_client_sink_send_keep_alive (sink)) ==
2304 case GST_RTSP_MESSAGE_DATA:
2305 /* we ignore response and data messages */
2306 GST_DEBUG_OBJECT (sink, "ignoring data message");
2309 GST_WARNING_OBJECT (sink, "ignoring unknown message type %d",
2314 g_assert_not_reached ();
2316 /* we get here when the connection got interrupted */
2319 gst_rtsp_message_unset (&message);
2320 GST_DEBUG_OBJECT (sink, "got interrupted");
2321 return GST_FLOW_FLUSHING;
2325 gchar *str = gst_rtsp_strresult (res);
2328 sink->conninfo.connected = FALSE;
2329 if (res != GST_RTSP_EINTR) {
2330 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE, (NULL),
2331 ("Could not connect to server. (%s)", str));
2333 ret = GST_FLOW_ERROR;
2335 ret = GST_FLOW_FLUSHING;
2341 gchar *str = gst_rtsp_strresult (res);
2343 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
2344 ("Could not receive message. (%s)", str));
2346 return GST_FLOW_ERROR;
2348 handle_request_failed:
2350 gchar *str = gst_rtsp_strresult (res);
2353 gst_rtsp_message_unset (&message);
2354 if (res != GST_RTSP_EINTR) {
2355 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
2356 ("Could not handle server message. (%s)", str));
2358 ret = GST_FLOW_ERROR;
2360 ret = GST_FLOW_FLUSHING;
2366 GST_DEBUG_OBJECT (sink, "we got an eof from the server");
2367 GST_ELEMENT_WARNING (sink, RESOURCE, READ, (NULL),
2368 ("The server closed the connection."));
2369 sink->conninfo.connected = FALSE;
2370 gst_rtsp_message_unset (&message);
2371 return GST_FLOW_EOS;
2375 static GstRTSPResult
2376 gst_rtsp_client_sink_reconnect (GstRTSPClientSink * sink, gboolean async)
2378 GstRTSPResult res = GST_RTSP_OK;
2379 gboolean restart = FALSE;
2381 GST_DEBUG_OBJECT (sink, "doing reconnect");
2383 GST_FIXME_OBJECT (sink, "Reconnection is not yet implemented");
2385 /* no need to restart, we're done */
2389 /* we can try only TCP now */
2390 sink->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
2392 /* close and cleanup our state */
2393 if ((res = gst_rtsp_client_sink_close (sink, async, FALSE)) < 0)
2396 /* see if we have TCP left to try. Also don't try TCP when we were configured
2398 if (!(sink->protocols & GST_RTSP_LOWER_TRANS_TCP) || sink->from_sdp)
2401 /* We post a warning message now to inform the user
2402 * that nothing happened. It's most likely a firewall thing. */
2403 GST_ELEMENT_WARNING (sink, RESOURCE, READ, (NULL),
2404 ("Could not receive any UDP packets for %.4f seconds, maybe your "
2405 "firewall is blocking it. Retrying using a TCP connection.",
2406 gst_guint64_to_gdouble (sink->udp_timeout / 1000000.0)));
2408 /* open new connection using tcp */
2409 if (gst_rtsp_client_sink_open (sink, async) < 0)
2412 /* start recording */
2413 if (gst_rtsp_client_sink_record (sink, async) < 0)
2422 sink->cur_protocols = 0;
2423 /* no transport possible, post an error and stop */
2424 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
2425 ("Could not receive any UDP packets for %.4f seconds, maybe your "
2426 "firewall is blocking it. No other protocols to try.",
2427 gst_guint64_to_gdouble (sink->udp_timeout / 1000000.0)));
2428 return GST_RTSP_ERROR;
2432 GST_DEBUG_OBJECT (sink, "open failed");
2437 GST_DEBUG_OBJECT (sink, "play failed");
2443 gst_rtsp_client_sink_loop_start_cmd (GstRTSPClientSink * sink, gint cmd)
2447 GST_ELEMENT_PROGRESS (sink, START, "open", ("Opening Stream"));
2450 GST_ELEMENT_PROGRESS (sink, START, "request", ("Sending RECORD request"));
2453 GST_ELEMENT_PROGRESS (sink, START, "request", ("Sending PAUSE request"));
2456 GST_ELEMENT_PROGRESS (sink, START, "close", ("Closing Stream"));
2464 gst_rtsp_client_sink_loop_complete_cmd (GstRTSPClientSink * sink, gint cmd)
2468 GST_ELEMENT_PROGRESS (sink, COMPLETE, "open", ("Opened Stream"));
2471 GST_ELEMENT_PROGRESS (sink, COMPLETE, "request", ("Sent RECORD request"));
2474 GST_ELEMENT_PROGRESS (sink, COMPLETE, "request", ("Sent PAUSE request"));
2477 GST_ELEMENT_PROGRESS (sink, COMPLETE, "close", ("Closed Stream"));
2485 gst_rtsp_client_sink_loop_cancel_cmd (GstRTSPClientSink * sink, gint cmd)
2489 GST_ELEMENT_PROGRESS (sink, CANCELED, "open", ("Open canceled"));
2492 GST_ELEMENT_PROGRESS (sink, CANCELED, "request", ("RECORD canceled"));
2495 GST_ELEMENT_PROGRESS (sink, CANCELED, "request", ("PAUSE canceled"));
2498 GST_ELEMENT_PROGRESS (sink, CANCELED, "close", ("Close canceled"));
2506 gst_rtsp_client_sink_loop_error_cmd (GstRTSPClientSink * sink, gint cmd)
2510 GST_ELEMENT_PROGRESS (sink, ERROR, "open", ("Open failed"));
2513 GST_ELEMENT_PROGRESS (sink, ERROR, "request", ("RECORD failed"));
2516 GST_ELEMENT_PROGRESS (sink, ERROR, "request", ("PAUSE failed"));
2519 GST_ELEMENT_PROGRESS (sink, ERROR, "close", ("Close failed"));
2527 gst_rtsp_client_sink_loop_end_cmd (GstRTSPClientSink * sink, gint cmd,
2530 if (ret == GST_RTSP_OK)
2531 gst_rtsp_client_sink_loop_complete_cmd (sink, cmd);
2532 else if (ret == GST_RTSP_EINTR)
2533 gst_rtsp_client_sink_loop_cancel_cmd (sink, cmd);
2535 gst_rtsp_client_sink_loop_error_cmd (sink, cmd);
2539 gst_rtsp_client_sink_loop_send_cmd (GstRTSPClientSink * sink, gint cmd,
2543 gboolean flushed = FALSE;
2545 /* start new request */
2546 gst_rtsp_client_sink_loop_start_cmd (sink, cmd);
2548 GST_DEBUG_OBJECT (sink, "sending cmd %s", cmd_to_string (cmd));
2550 GST_OBJECT_LOCK (sink);
2551 old = sink->pending_cmd;
2552 if (old == CMD_RECONNECT) {
2553 GST_DEBUG_OBJECT (sink, "ignore, we were reconnecting");
2554 cmd = CMD_RECONNECT;
2556 if (old != CMD_WAIT) {
2557 sink->pending_cmd = CMD_WAIT;
2558 GST_OBJECT_UNLOCK (sink);
2559 /* cancel previous request */
2560 GST_DEBUG_OBJECT (sink, "cancel previous request %s", cmd_to_string (old));
2561 gst_rtsp_client_sink_loop_cancel_cmd (sink, old);
2562 GST_OBJECT_LOCK (sink);
2564 sink->pending_cmd = cmd;
2565 /* interrupt if allowed */
2566 if (sink->busy_cmd & mask) {
2567 GST_DEBUG_OBJECT (sink, "connection flush busy %s",
2568 cmd_to_string (sink->busy_cmd));
2569 gst_rtsp_client_sink_connection_flush (sink, TRUE);
2572 GST_DEBUG_OBJECT (sink, "not interrupting busy cmd %s",
2573 cmd_to_string (sink->busy_cmd));
2576 gst_task_start (sink->task);
2577 GST_OBJECT_UNLOCK (sink);
2583 gst_rtsp_client_sink_loop (GstRTSPClientSink * sink)
2587 if (!sink->conninfo.connection || !sink->conninfo.connected)
2590 ret = gst_rtsp_client_sink_loop_rx (sink);
2591 if (ret != GST_FLOW_OK)
2599 GST_WARNING_OBJECT (sink, "we are not connected");
2600 ret = GST_FLOW_FLUSHING;
2605 const gchar *reason = gst_flow_get_name (ret);
2607 GST_DEBUG_OBJECT (sink, "pausing task, reason %s", reason);
2608 gst_rtsp_client_sink_loop_send_cmd (sink, CMD_WAIT, CMD_LOOP);
2613 #ifndef GST_DISABLE_GST_DEBUG
2614 static const gchar *
2615 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
2619 while (method != 0) {
2636 /* Parse a WWW-Authenticate Response header and determine the
2637 * available authentication methods
2639 * This code should also cope with the fact that each WWW-Authenticate
2640 * header can contain multiple challenge methods + tokens
2642 * At the moment, for Basic auth, we just do a minimal check and don't
2643 * even parse out the realm */
2645 gst_rtsp_client_sink_parse_auth_hdr (GstRTSPMessage * response,
2646 GstRTSPAuthMethod * methods, GstRTSPConnection * conn, gboolean * stale)
2648 GstRTSPAuthCredential **credentials, **credential;
2650 g_return_if_fail (response != NULL);
2651 g_return_if_fail (methods != NULL);
2652 g_return_if_fail (stale != NULL);
2655 gst_rtsp_message_parse_auth_credentials (response,
2656 GST_RTSP_HDR_WWW_AUTHENTICATE);
2660 credential = credentials;
2661 while (*credential) {
2662 if ((*credential)->scheme == GST_RTSP_AUTH_BASIC) {
2663 *methods |= GST_RTSP_AUTH_BASIC;
2664 } else if ((*credential)->scheme == GST_RTSP_AUTH_DIGEST) {
2665 GstRTSPAuthParam **param = (*credential)->params;
2667 *methods |= GST_RTSP_AUTH_DIGEST;
2669 gst_rtsp_connection_clear_auth_params (conn);
2673 if (strcmp ((*param)->name, "stale") == 0
2674 && g_ascii_strcasecmp ((*param)->value, "TRUE") == 0)
2676 gst_rtsp_connection_set_auth_param (conn, (*param)->name,
2685 gst_rtsp_auth_credentials_free (credentials);
2689 * gst_rtsp_client_sink_setup_auth:
2690 * @src: the rtsp source
2692 * Configure a username and password and auth method on the
2693 * connection object based on a response we received from the
2696 * Currently, this requires that a username and password were supplied
2697 * in the uri. In the future, they may be requested on demand by sending
2698 * a message up the bus.
2700 * Returns: TRUE if authentication information could be set up correctly.
2703 gst_rtsp_client_sink_setup_auth (GstRTSPClientSink * sink,
2704 GstRTSPMessage * response)
2708 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
2709 GstRTSPAuthMethod method;
2710 GstRTSPResult auth_result;
2712 GstRTSPConnection *conn;
2713 gboolean stale = FALSE;
2715 conn = sink->conninfo.connection;
2717 /* Identify the available auth methods and see if any are supported */
2718 gst_rtsp_client_sink_parse_auth_hdr (response, &avail_methods, conn, &stale);
2720 if (avail_methods == GST_RTSP_AUTH_NONE)
2721 goto no_auth_available;
2723 /* For digest auth, if the response indicates that the session
2724 * data are stale, we just update them in the connection object and
2725 * return TRUE to retry the request */
2727 sink->tried_url_auth = FALSE;
2729 url = gst_rtsp_connection_get_url (conn);
2731 /* Do we have username and password available? */
2732 if (url != NULL && !sink->tried_url_auth && url->user != NULL
2733 && url->passwd != NULL) {
2736 sink->tried_url_auth = TRUE;
2737 GST_DEBUG_OBJECT (sink,
2738 "Attempting authentication using credentials from the URL");
2740 user = sink->user_id;
2741 pass = sink->user_pw;
2742 GST_DEBUG_OBJECT (sink,
2743 "Attempting authentication using credentials from the properties");
2746 /* FIXME: If the url didn't contain username and password or we tried them
2747 * already, request a username and passwd from the application via some kind
2748 * of credentials request message */
2750 /* If we don't have a username and passwd at this point, bail out. */
2751 if (user == NULL || pass == NULL)
2754 /* Try to configure for each available authentication method, strongest to
2756 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
2757 /* Check if this method is available on the server */
2758 if ((method & avail_methods) == 0)
2761 /* Pass the credentials to the connection to try on the next request */
2762 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
2763 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
2764 * ignore it and end up retrying later */
2765 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
2766 GST_DEBUG_OBJECT (sink, "Attempting %s authentication",
2767 gst_rtsp_auth_method_to_string (method));
2772 if (method == GST_RTSP_AUTH_NONE)
2773 goto no_auth_available;
2779 /* Output an error indicating that we couldn't connect because there were
2780 * no supported authentication protocols */
2781 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ, (NULL),
2782 ("No supported authentication protocol was found"));
2787 /* We don't fire an error message, we just return FALSE and let the
2788 * normal NOT_AUTHORIZED error be propagated */
2793 static GstRTSPResult
2794 gst_rtsp_client_sink_try_send (GstRTSPClientSink * sink,
2795 GstRTSPConnInfo * conninfo, GstRTSPMessage * request,
2796 GstRTSPMessage * response, GstRTSPStatusCode * code)
2799 GstRTSPStatusCode thecode;
2800 gchar *content_base = NULL;
2804 GST_DEBUG_OBJECT (sink, "sending message");
2807 gst_rtsp_message_dump (request);
2809 g_mutex_lock (&sink->send_lock);
2812 gst_rtsp_client_sink_connection_send (sink, conninfo, request,
2813 sink->ptcp_timeout);
2815 g_mutex_unlock (&sink->send_lock);
2819 gst_rtsp_connection_reset_timeout (conninfo->connection);
2821 /* See if we should handle the response */
2822 if (response == NULL) {
2823 g_mutex_unlock (&sink->send_lock);
2828 gst_rtsp_client_sink_connection_receive (sink, conninfo, response,
2829 sink->ptcp_timeout);
2831 g_mutex_unlock (&sink->send_lock);
2837 gst_rtsp_message_dump (response);
2840 switch (response->type) {
2841 case GST_RTSP_MESSAGE_REQUEST:
2842 res = gst_rtsp_client_sink_handle_request (sink, conninfo, response);
2843 if (res == GST_RTSP_EEOF)
2846 goto handle_request_failed;
2847 g_mutex_lock (&sink->send_lock);
2849 case GST_RTSP_MESSAGE_RESPONSE:
2850 /* ok, a response is good */
2851 GST_DEBUG_OBJECT (sink, "received response message");
2853 case GST_RTSP_MESSAGE_DATA:
2854 /* we ignore data messages */
2855 GST_DEBUG_OBJECT (sink, "ignoring data message");
2856 g_mutex_lock (&sink->send_lock);
2859 GST_WARNING_OBJECT (sink, "ignoring unknown message type %d",
2861 g_mutex_lock (&sink->send_lock);
2865 thecode = response->type_data.response.code;
2867 GST_DEBUG_OBJECT (sink, "got response message %d", thecode);
2869 /* if the caller wanted the result code, we store it. */
2873 /* If the request didn't succeed, bail out before doing any more */
2874 if (thecode != GST_RTSP_STS_OK)
2877 /* store new content base if any */
2878 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
2881 g_free (sink->content_base);
2882 sink->content_base = g_strdup (content_base);
2890 gchar *str = gst_rtsp_strresult (res);
2892 if (res != GST_RTSP_EINTR) {
2893 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
2894 ("Could not send message. (%s)", str));
2896 GST_WARNING_OBJECT (sink, "send interrupted");
2905 GST_WARNING_OBJECT (sink, "server closed connection");
2906 if ((try == 0) && !sink->interleaved && sink->udp_reconnect) {
2908 /* if reconnect succeeds, try again */
2910 gst_rtsp_conninfo_reconnect (sink, &sink->conninfo,
2914 /* only try once after reconnect, then fallthrough and error out */
2917 gchar *str = gst_rtsp_strresult (res);
2919 if (res != GST_RTSP_EINTR) {
2920 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
2921 ("Could not receive message. (%s)", str));
2923 GST_WARNING_OBJECT (sink, "receive interrupted");
2931 handle_request_failed:
2933 /* ERROR was posted */
2934 gst_rtsp_message_unset (response);
2939 GST_DEBUG_OBJECT (sink, "we got an eof from the server");
2940 GST_ELEMENT_WARNING (sink, RESOURCE, READ, (NULL),
2941 ("The server closed the connection."));
2942 gst_rtsp_message_unset (response);
2948 gst_rtsp_client_sink_set_state (GstRTSPClientSink * sink, GstState state)
2950 GST_DEBUG_OBJECT (sink, "Setting internal state to %s",
2951 gst_element_state_get_name (state));
2952 gst_element_set_state (GST_ELEMENT (sink->internal_bin), state);
2956 * gst_rtsp_client_sink_send:
2957 * @src: the rtsp source
2958 * @conn: the connection to send on
2959 * @request: must point to a valid request
2960 * @response: must point to an empty #GstRTSPMessage
2961 * @code: an optional code result
2963 * send @request and retrieve the response in @response. optionally @code can be
2964 * non-NULL in which case it will contain the status code of the response.
2966 * If This function returns #GST_RTSP_OK, @response will contain a valid response
2967 * message that should be cleaned with gst_rtsp_message_unset() after usage.
2969 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
2970 * @response message) if the response code was not 200 (OK).
2972 * If the attempt results in an authentication failure, then this will attempt
2973 * to retrieve authentication credentials via gst_rtsp_client_sink_setup_auth and retry
2976 * Returns: #GST_RTSP_OK if the processing was successful.
2978 static GstRTSPResult
2979 gst_rtsp_client_sink_send (GstRTSPClientSink * sink, GstRTSPConnInfo * conninfo,
2980 GstRTSPMessage * request, GstRTSPMessage * response,
2981 GstRTSPStatusCode * code)
2983 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
2984 GstRTSPResult res = GST_RTSP_ERROR;
2987 GstRTSPMethod method = GST_RTSP_INVALID;
2993 /* make sure we don't loop forever */
2997 /* save method so we can disable it when the server complains */
2998 method = request->type_data.request.method;
3001 gst_rtsp_client_sink_try_send (sink, conninfo, request, response,
3006 case GST_RTSP_STS_UNAUTHORIZED:
3007 if (gst_rtsp_client_sink_setup_auth (sink, response)) {
3008 /* Try the request/response again after configuring the auth info
3016 } while (retry == TRUE);
3018 /* If the user requested the code, let them handle errors, otherwise
3019 * post an error below */
3022 else if (int_code != GST_RTSP_STS_OK)
3023 goto error_response;
3030 GST_DEBUG_OBJECT (sink, "got error %d", res);
3035 res = GST_RTSP_ERROR;
3037 switch (response->type_data.response.code) {
3038 case GST_RTSP_STS_NOT_FOUND:
3039 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND, (NULL), ("%s",
3040 response->type_data.response.reason));
3042 case GST_RTSP_STS_UNAUTHORIZED:
3043 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_AUTHORIZED, (NULL), ("%s",
3044 response->type_data.response.reason));
3046 case GST_RTSP_STS_MOVED_PERMANENTLY:
3047 case GST_RTSP_STS_MOVE_TEMPORARILY:
3049 gchar *new_location;
3050 GstRTSPLowerTrans transports;
3052 GST_DEBUG_OBJECT (sink, "got redirection");
3053 /* if we don't have a Location Header, we must error */
3054 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
3055 &new_location, 0) < 0)
3058 /* When we receive a redirect result, we go back to the INIT state after
3059 * parsing the new URI. The caller should do the needed steps to issue
3060 * a new setup when it detects this state change. */
3061 GST_DEBUG_OBJECT (sink, "redirection to %s", new_location);
3063 /* save current transports */
3064 if (sink->conninfo.url)
3065 transports = sink->conninfo.url->transports;
3067 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
3069 gst_rtsp_client_sink_uri_set_uri (GST_URI_HANDLER (sink), new_location,
3072 /* set old transports */
3073 if (sink->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
3074 sink->conninfo.url->transports = transports;
3076 sink->need_redirect = TRUE;
3077 sink->state = GST_RTSP_STATE_INIT;
3081 case GST_RTSP_STS_NOT_ACCEPTABLE:
3082 case GST_RTSP_STS_NOT_IMPLEMENTED:
3083 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
3084 GST_WARNING_OBJECT (sink, "got NOT IMPLEMENTED, disable method %s",
3085 gst_rtsp_method_as_text (method));
3086 sink->methods &= ~method;
3090 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
3091 ("Got error response: %d (%s).", response->type_data.response.code,
3092 response->type_data.response.reason));
3095 /* if we return ERROR we should unset the response ourselves */
3096 if (res == GST_RTSP_ERROR)
3097 gst_rtsp_message_unset (response);
3103 /* parse the response and collect all the supported methods. We need this
3104 * information so that we don't try to send an unsupported request to the
3108 gst_rtsp_client_sink_parse_methods (GstRTSPClientSink * sink,
3109 GstRTSPMessage * response)
3111 GstRTSPHeaderField field;
3115 /* reset supported methods */
3118 /* Try Allow Header first */
3119 field = GST_RTSP_HDR_ALLOW;
3122 gst_rtsp_message_get_header (response, field, &respoptions, indx);
3123 if (indx == 0 && !respoptions) {
3124 /* if no Allow header was found then try the Public header... */
3125 field = GST_RTSP_HDR_PUBLIC;
3126 gst_rtsp_message_get_header (response, field, &respoptions, indx);
3131 sink->methods |= gst_rtsp_options_from_text (respoptions);
3136 if (sink->methods == 0) {
3137 /* neither Allow nor Public are required, assume the server supports
3138 * at least SETUP. */
3139 GST_DEBUG_OBJECT (sink, "could not get OPTIONS");
3140 sink->methods = GST_RTSP_SETUP;
3143 /* Even if the server replied, and didn't say it supports
3144 * RECORD|ANNOUNCE, try anyway by assuming it does */
3145 sink->methods |= GST_RTSP_ANNOUNCE | GST_RTSP_RECORD;
3147 if (!(sink->methods & GST_RTSP_SETUP))
3155 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ, (NULL),
3156 ("Server does not support SETUP."));
3161 static GstRTSPResult
3162 gst_rtsp_client_sink_connect_to_server (GstRTSPClientSink * sink,
3166 GstRTSPMessage request = { 0 };
3167 GstRTSPMessage response = { 0 };
3168 GSocket *conn_socket;
3172 sink->need_redirect = FALSE;
3174 /* can't continue without a valid url */
3175 if (G_UNLIKELY (sink->conninfo.url == NULL)) {
3176 res = GST_RTSP_EINVAL;
3179 sink->tried_url_auth = FALSE;
3181 if ((res = gst_rtsp_conninfo_connect (sink, &sink->conninfo, async)) < 0)
3182 goto connect_failed;
3184 conn_socket = gst_rtsp_connection_get_read_socket (sink->conninfo.connection);
3185 sa = g_socket_get_remote_address (conn_socket, NULL);
3186 ia = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (sa));
3188 sink->server_ip = g_inet_address_to_string (ia);
3190 g_object_unref (sa);
3192 /* create OPTIONS */
3193 GST_DEBUG_OBJECT (sink, "create options...");
3195 gst_rtsp_client_sink_init_request (sink, &request, GST_RTSP_OPTIONS,
3196 sink->conninfo.url_str);
3198 goto create_request_failed;
3201 GST_DEBUG_OBJECT (sink, "send options...");
3204 GST_ELEMENT_PROGRESS (sink, CONTINUE, "open",
3205 ("Retrieving server options"));
3208 gst_rtsp_client_sink_send (sink, &sink->conninfo, &request,
3209 &response, NULL)) < 0)
3213 if (!gst_rtsp_client_sink_parse_methods (sink, &response))
3216 /* FIXME: Do we need to handle REDIRECT responses for OPTIONS? */
3218 /* clean up any messages */
3219 gst_rtsp_message_unset (&request);
3220 gst_rtsp_message_unset (&response);
3227 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND, (NULL),
3228 ("No valid RTSP URL was provided"));
3233 gchar *str = gst_rtsp_strresult (res);
3235 if (res != GST_RTSP_EINTR) {
3236 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE, (NULL),
3237 ("Failed to connect. (%s)", str));
3239 GST_WARNING_OBJECT (sink, "connect interrupted");
3244 create_request_failed:
3246 gchar *str = gst_rtsp_strresult (res);
3248 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
3249 ("Could not create request. (%s)", str));
3255 /* Don't post a message - the rtsp_send method will have
3256 * taken care of it because we passed NULL for the response code */
3261 /* error was posted */
3262 res = GST_RTSP_ERROR;
3267 if (sink->conninfo.connection) {
3268 GST_DEBUG_OBJECT (sink, "free connection");
3269 gst_rtsp_conninfo_close (sink, &sink->conninfo, TRUE);
3271 gst_rtsp_message_unset (&request);
3272 gst_rtsp_message_unset (&response);
3277 static GstRTSPResult
3278 gst_rtsp_client_sink_open (GstRTSPClientSink * sink, gboolean async)
3283 GST_RTSP_SETUP | GST_RTSP_RECORD | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
3285 g_mutex_lock (&sink->open_conn_lock);
3286 sink->open_conn_start = TRUE;
3287 g_cond_broadcast (&sink->open_conn_cond);
3288 GST_DEBUG_OBJECT (sink, "connection to server started");
3289 g_mutex_unlock (&sink->open_conn_lock);
3291 if ((ret = gst_rtsp_client_sink_connect_to_server (sink, async)) < 0)
3295 gst_rtsp_client_sink_loop_end_cmd (sink, CMD_OPEN, ret);
3302 GST_WARNING_OBJECT (sink, "Failed to connect to server");
3303 sink->open_error = TRUE;
3305 gst_rtsp_client_sink_loop_end_cmd (sink, CMD_OPEN, ret);
3310 static GstRTSPResult
3311 gst_rtsp_client_sink_close (GstRTSPClientSink * sink, gboolean async,
3312 gboolean only_close)
3314 GstRTSPMessage request = { 0 };
3315 GstRTSPMessage response = { 0 };
3316 GstRTSPResult res = GST_RTSP_OK;
3318 const gchar *control;
3320 GST_DEBUG_OBJECT (sink, "TEARDOWN...");
3322 gst_rtsp_client_sink_set_state (sink, GST_STATE_NULL);
3324 if (sink->state < GST_RTSP_STATE_READY) {
3325 GST_DEBUG_OBJECT (sink, "not ready, doing cleanup");
3332 /* construct a control url */
3333 control = get_aggregate_control (sink);
3335 if (!(sink->methods & (GST_RTSP_RECORD | GST_RTSP_TEARDOWN)))
3338 /* stop streaming */
3339 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3340 GstRTSPStreamContext *context = (GstRTSPStreamContext *) walk->data;
3342 if (context->stream_transport)
3343 gst_rtsp_stream_transport_set_active (context->stream_transport, FALSE);
3345 if (context->joined) {
3346 gst_rtsp_stream_leave_bin (context->stream, GST_BIN (sink->internal_bin),
3348 context->joined = FALSE;
3352 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3353 GstRTSPStreamContext *context = (GstRTSPStreamContext *) walk->data;
3354 const gchar *setup_url;
3355 GstRTSPConnInfo *info;
3357 GST_DEBUG_OBJECT (sink, "Looking at stream %p for teardown",
3360 /* try aggregate control first but do non-aggregate control otherwise */
3362 setup_url = control;
3363 else if ((setup_url = context->conninfo.location) == NULL) {
3364 GST_DEBUG_OBJECT (sink, "Skipping TEARDOWN stream %p - no setup URL",
3369 if (sink->conninfo.connection) {
3370 info = &sink->conninfo;
3371 } else if (context->conninfo.connection) {
3372 info = &context->conninfo;
3376 if (!info->connected)
3380 GST_DEBUG_OBJECT (sink, "Sending teardown for stream %p at URL %s",
3381 context->stream, setup_url);
3383 gst_rtsp_client_sink_init_request (sink, &request, GST_RTSP_TEARDOWN,
3386 goto create_request_failed;
3389 GST_ELEMENT_PROGRESS (sink, CONTINUE, "close", ("Closing stream"));
3392 gst_rtsp_client_sink_send (sink, info, &request,
3393 &response, NULL)) < 0)
3396 /* FIXME, parse result? */
3397 gst_rtsp_message_unset (&request);
3398 gst_rtsp_message_unset (&response);
3401 /* early exit when we did aggregate control */
3407 /* close connections */
3408 GST_DEBUG_OBJECT (sink, "closing connection...");
3409 gst_rtsp_conninfo_close (sink, &sink->conninfo, TRUE);
3410 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3411 GstRTSPStreamContext *stream = (GstRTSPStreamContext *) walk->data;
3412 gst_rtsp_conninfo_close (sink, &stream->conninfo, TRUE);
3416 gst_rtsp_client_sink_cleanup (sink);
3418 sink->state = GST_RTSP_STATE_INVALID;
3421 gst_rtsp_client_sink_loop_end_cmd (sink, CMD_CLOSE, res);
3426 create_request_failed:
3428 gchar *str = gst_rtsp_strresult (res);
3430 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
3431 ("Could not create request. (%s)", str));
3437 gchar *str = gst_rtsp_strresult (res);
3439 gst_rtsp_message_unset (&request);
3440 if (res != GST_RTSP_EINTR) {
3441 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
3442 ("Could not send message. (%s)", str));
3444 GST_WARNING_OBJECT (sink, "TEARDOWN interrupted");
3451 GST_DEBUG_OBJECT (sink,
3452 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
3458 gst_rtsp_client_sink_configure_manager (GstRTSPClientSink * sink)
3461 GstStateChangeReturn ret;
3463 rtpbin = sink->rtpbin;
3465 if (rtpbin == NULL) {
3466 GObjectClass *klass;
3468 rtpbin = gst_element_factory_make ("rtpbin", NULL);
3472 gst_bin_add (GST_BIN_CAST (sink->internal_bin), rtpbin);
3474 sink->rtpbin = rtpbin;
3476 /* Any more settings we should configure on rtpbin here? */
3477 g_object_set (sink->rtpbin, "latency", sink->latency, NULL);
3479 klass = G_OBJECT_GET_CLASS (G_OBJECT (rtpbin));
3481 if (g_object_class_find_property (klass, "ntp-time-source")) {
3482 g_object_set (sink->rtpbin, "ntp-time-source", sink->ntp_time_source,
3486 if (sink->sdes && g_object_class_find_property (klass, "sdes")) {
3487 g_object_set (sink->rtpbin, "sdes", sink->sdes, NULL);
3490 g_signal_emit (sink, gst_rtsp_client_sink_signals[SIGNAL_NEW_MANAGER], 0,
3494 ret = gst_element_set_state (rtpbin, GST_STATE_PAUSED);
3495 if (ret == GST_STATE_CHANGE_FAILURE)
3496 goto start_manager_failure;
3502 GST_WARNING ("no rtpbin element");
3503 g_warning ("failed to create element 'rtpbin', check your installation");
3506 start_manager_failure:
3508 GST_DEBUG_OBJECT (sink, "could not start session manager");
3509 gst_bin_remove (GST_BIN_CAST (sink->internal_bin), rtpbin);
3515 request_aux_sender (GstElement * rtpbin, guint sessid, GstRTSPClientSink * sink)
3517 GstRTSPStream *stream = NULL;
3518 GstElement *ret = NULL;
3521 GST_RTSP_STATE_LOCK (sink);
3522 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3523 GstRTSPStreamContext *context = (GstRTSPStreamContext *) walk->data;
3525 if (sessid == gst_rtsp_stream_get_index (context->stream)) {
3526 stream = context->stream;
3531 if (stream != NULL) {
3532 GST_DEBUG_OBJECT (sink, "Creating aux sender for stream %u", sessid);
3533 ret = gst_rtsp_stream_request_aux_sender (stream, sessid);
3536 GST_RTSP_STATE_UNLOCK (sink);
3542 request_fec_encoder (GstElement * rtpbin, guint sessid,
3543 GstRTSPClientSink * sink)
3545 GstRTSPStream *stream = NULL;
3546 GstElement *ret = NULL;
3549 GST_RTSP_STATE_LOCK (sink);
3550 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3551 GstRTSPStreamContext *context = (GstRTSPStreamContext *) walk->data;
3553 if (sessid == gst_rtsp_stream_get_index (context->stream)) {
3554 stream = context->stream;
3559 if (stream != NULL) {
3560 ret = gst_rtsp_stream_request_ulpfec_encoder (stream, sessid);
3563 GST_RTSP_STATE_UNLOCK (sink);
3569 gst_rtsp_client_sink_collect_streams (GstRTSPClientSink * sink)
3571 GstRTSPStreamContext *context;
3576 GST_DEBUG_OBJECT (sink, "Collecting stream information");
3578 if (!gst_rtsp_client_sink_configure_manager (sink))
3581 base = get_aggregate_control (sink);
3582 /* check if the base ends with / */
3583 has_slash = g_str_has_suffix (base, "/");
3585 g_mutex_lock (&sink->preroll_lock);
3586 while (sink->contexts == NULL && !sink->conninfo.flushing) {
3587 g_cond_wait (&sink->preroll_cond, &sink->preroll_lock);
3589 g_mutex_unlock (&sink->preroll_lock);
3591 /* FIXME: Need different locking - need to protect against pad releases
3592 * and potential state changes ruining things here */
3593 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3596 context = (GstRTSPStreamContext *) walk->data;
3597 if (context->stream)
3600 g_mutex_lock (&sink->preroll_lock);
3601 while (!context->prerolled && !sink->conninfo.flushing) {
3602 GST_DEBUG_OBJECT (sink, "Waiting for caps on stream %d", context->index);
3603 g_cond_wait (&sink->preroll_cond, &sink->preroll_lock);
3605 if (sink->conninfo.flushing) {
3606 g_mutex_unlock (&sink->preroll_lock);
3609 g_mutex_unlock (&sink->preroll_lock);
3611 if (context->payloader == NULL)
3614 srcpad = gst_element_get_static_pad (context->payloader, "src");
3616 GST_DEBUG_OBJECT (sink, "Creating stream object for stream %d",
3619 gst_rtsp_client_sink_create_stream (sink, context, context->payloader,
3622 /* concatenate the two strings, insert / when not present */
3623 g_free (context->conninfo.location);
3624 context->conninfo.location =
3625 g_strdup_printf ("%s%sstream=%d", base, has_slash ? "" : "/",
3628 if (sink->rtx_time > 0) {
3629 /* enable retransmission by setting rtprtxsend as the "aux" element of rtpbin */
3630 g_signal_connect (sink->rtpbin, "request-aux-sender",
3631 (GCallback) request_aux_sender, sink);
3634 g_signal_connect (sink->rtpbin, "request-fec-encoder",
3635 (GCallback) request_fec_encoder, sink);
3637 if (!gst_rtsp_stream_join_bin (context->stream,
3638 GST_BIN (sink->internal_bin), sink->rtpbin, GST_STATE_PAUSED)) {
3639 goto join_bin_failed;
3641 context->joined = TRUE;
3643 /* Block the stream, as it does not have any transport parts yet */
3644 gst_rtsp_stream_set_blocked (context->stream, TRUE);
3646 /* Let the stream object receive data */
3647 gst_pad_remove_probe (srcpad, context->payloader_block_id);
3649 gst_object_unref (srcpad);
3652 /* Now wait for the preroll of the rtp bin */
3653 g_mutex_lock (&sink->preroll_lock);
3654 while (!sink->prerolled && sink->conninfo.connection
3655 && !sink->conninfo.flushing) {
3656 GST_LOG_OBJECT (sink, "Waiting for preroll before continuing");
3657 g_cond_wait (&sink->preroll_cond, &sink->preroll_lock);
3659 GST_LOG_OBJECT (sink, "Marking streams as collected");
3660 sink->streams_collected = TRUE;
3661 g_mutex_unlock (&sink->preroll_lock);
3667 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
3668 ("Could not start stream %d", context->index));
3672 static GstRTSPResult
3673 gst_rtsp_client_sink_create_transports_string (GstRTSPClientSink * sink,
3674 GstRTSPStreamContext * context, GSocketFamily family,
3675 GstRTSPLowerTrans protocols, GstRTSPProfile profiles, gchar ** transports)
3678 GstRTSPStream *stream = context->stream;
3679 gboolean first = TRUE;
3681 /* the default RTSP transports */
3682 result = g_string_new ("RTP");
3684 while (profiles != 0) {
3686 g_string_append (result, ",RTP");
3688 if (profiles & GST_RTSP_PROFILE_SAVPF) {
3689 g_string_append (result, "/SAVPF");
3690 profiles &= ~GST_RTSP_PROFILE_SAVPF;
3691 } else if (profiles & GST_RTSP_PROFILE_SAVP) {
3692 g_string_append (result, "/SAVP");
3693 profiles &= ~GST_RTSP_PROFILE_SAVP;
3694 } else if (profiles & GST_RTSP_PROFILE_AVPF) {
3695 g_string_append (result, "/AVPF");
3696 profiles &= ~GST_RTSP_PROFILE_AVPF;
3697 } else if (profiles & GST_RTSP_PROFILE_AVP) {
3698 g_string_append (result, "/AVP");
3699 profiles &= ~GST_RTSP_PROFILE_AVP;
3701 GST_WARNING_OBJECT (sink, "Unimplemented profile(s) 0x%x", profiles);
3705 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
3708 GST_DEBUG_OBJECT (sink, "adding UDP unicast");
3709 gst_rtsp_stream_get_server_port (stream, &ports, family);
3711 g_string_append_printf (result, "/UDP;unicast;client_port=%d-%d",
3712 ports.min, ports.max);
3713 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3714 GstRTSPAddress *addr =
3715 gst_rtsp_stream_get_multicast_address (stream, family);
3717 GST_DEBUG_OBJECT (sink, "adding UDP multicast");
3718 g_string_append_printf (result, "/UDP;multicast;client_port=%d-%d",
3719 addr->port, addr->port + addr->n_ports - 1);
3720 gst_rtsp_address_free (addr);
3722 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
3723 GST_DEBUG_OBJECT (sink, "adding TCP");
3724 g_string_append_printf (result, "/TCP;unicast;interleaved=%d-%d",
3725 sink->free_channel, sink->free_channel + 1);
3728 g_string_append (result, ";mode=RECORD");
3729 /* FIXME: Support appending too:
3731 g_string_append (result, ";append");
3738 /* No valid transport could be constructed */
3739 GST_ERROR_OBJECT (sink, "No supported profiles configured");
3743 *transports = g_string_free (result, FALSE);
3745 GST_DEBUG_OBJECT (sink, "prepared transports %s", GST_STR_NULL (*transports));
3749 g_string_free (result, TRUE);
3750 return GST_RTSP_ERROR;
3754 signal_get_srtcp_params (GstRTSPClientSink * sink,
3755 GstRTSPStreamContext * context)
3757 GstCaps *caps = NULL;
3759 g_signal_emit (sink, gst_rtsp_client_sink_signals[SIGNAL_REQUEST_RTCP_KEY], 0,
3760 context->index, &caps);
3763 GST_DEBUG_OBJECT (sink, "SRTP parameters received");
3769 gst_rtsp_client_sink_stream_make_keymgmt (GstRTSPClientSink * sink,
3770 GstRTSPStreamContext * context)
3772 gchar *base64, *result = NULL;
3773 GstMIKEYMessage *mikey_msg;
3775 context->srtcpparams = signal_get_srtcp_params (sink, context);
3776 if (context->srtcpparams == NULL)
3777 context->srtcpparams = gst_rtsp_stream_get_caps (context->stream);
3779 mikey_msg = gst_mikey_message_new_from_caps (context->srtcpparams);
3781 guint send_ssrc, send_rtx_ssrc;
3782 const GstStructure *s = gst_caps_get_structure (context->srtcpparams, 0);
3784 /* add policy '0' for our SSRC */
3785 gst_rtsp_stream_get_ssrc (context->stream, &send_ssrc);
3786 GST_LOG_OBJECT (sink, "Stream %p ssrc %x", context->stream, send_ssrc);
3787 gst_mikey_message_add_cs_srtp (mikey_msg, 0, send_ssrc, 0);
3789 if (gst_structure_get_uint (s, "rtx-ssrc", &send_rtx_ssrc))
3790 gst_mikey_message_add_cs_srtp (mikey_msg, 0, send_rtx_ssrc, 0);
3792 base64 = gst_mikey_message_base64_encode (mikey_msg);
3793 gst_mikey_message_unref (mikey_msg);
3796 result = gst_sdp_make_keymgmt (context->conninfo.location, base64);
3804 /* masks to be kept in sync with the hardcoded protocol order of preference
3806 static const guint protocol_masks[] = {
3807 GST_RTSP_LOWER_TRANS_UDP,
3808 GST_RTSP_LOWER_TRANS_UDP_MCAST,
3809 GST_RTSP_LOWER_TRANS_TCP,
3813 /* Same for profile_masks */
3814 static const guint profile_masks[] = {
3815 GST_RTSP_PROFILE_SAVPF,
3816 GST_RTSP_PROFILE_SAVP,
3817 GST_RTSP_PROFILE_AVPF,
3818 GST_RTSP_PROFILE_AVP,
3823 do_send_data (GstBuffer * buffer, guint8 channel,
3824 GstRTSPStreamContext * context)
3826 GstRTSPClientSink *sink = context->parent;
3827 GstRTSPMessage message = { 0 };
3828 GstRTSPResult res = GST_RTSP_OK;
3829 GstMapInfo map_info;
3833 gst_rtsp_message_init_data (&message, channel);
3835 /* FIXME, need some sort of iovec RTSPMessage here */
3836 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
3839 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
3842 gst_rtsp_client_sink_try_send (sink, &sink->conninfo, &message,
3845 gst_rtsp_message_steal_body (&message, &data, &usize);
3846 gst_buffer_unmap (buffer, &map_info);
3848 gst_rtsp_message_unset (&message);
3850 return res == GST_RTSP_OK;
3853 static GstRTSPResult
3854 gst_rtsp_client_sink_setup_streams (GstRTSPClientSink * sink, gboolean async)
3856 GstRTSPResult res = GST_RTSP_ERROR;
3857 GstRTSPMessage request = { 0 };
3858 GstRTSPMessage response = { 0 };
3859 GstRTSPLowerTrans protocols;
3860 GstRTSPStatusCode code;
3861 GSocketFamily family;
3863 GSocket *conn_socket;
3868 if (sink->conninfo.connection) {
3869 url = gst_rtsp_connection_get_url (sink->conninfo.connection);
3870 /* we initially allow all configured lower transports. based on the URL
3871 * transports and the replies from the server we narrow them down. */
3872 protocols = url->transports & sink->cur_protocols;
3875 protocols = sink->cur_protocols;
3881 GST_RTSP_STATE_LOCK (sink);
3883 if (G_UNLIKELY (sink->contexts == NULL))
3886 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3887 GstRTSPStreamContext *context = (GstRTSPStreamContext *) walk->data;
3888 GstRTSPStream *stream;
3890 GstRTSPConnInfo *info;
3891 GstRTSPProfile profiles;
3892 GstRTSPProfile cur_profile;
3895 guint profile_mask = 0;
3898 const GstSDPMedia *media;
3900 stream = context->stream;
3901 profiles = gst_rtsp_stream_get_profiles (stream);
3903 caps = gst_rtsp_stream_get_caps (stream);
3905 GST_DEBUG_OBJECT (sink, "skipping stream %p, no caps", stream);
3908 gst_caps_unref (caps);
3909 media = gst_sdp_message_get_media (&sink->cursdp, context->sdp_index);
3910 if (media == NULL) {
3911 GST_DEBUG_OBJECT (sink, "skipping stream %p, no SDP info", stream);
3915 /* skip setup if we have no URL for it */
3916 if (context->conninfo.location == NULL) {
3917 GST_DEBUG_OBJECT (sink, "skipping stream %p, no setup", stream);
3921 if (sink->conninfo.connection == NULL) {
3922 if (!gst_rtsp_conninfo_connect (sink, &context->conninfo, async)) {
3923 GST_DEBUG_OBJECT (sink, "skipping stream %p, failed to connect",
3927 info = &context->conninfo;
3929 info = &sink->conninfo;
3931 GST_DEBUG_OBJECT (sink, "doing setup of stream %p with %s", stream,
3932 context->conninfo.location);
3934 conn_socket = gst_rtsp_connection_get_read_socket (info->connection);
3935 sa = g_socket_get_local_address (conn_socket, NULL);
3936 family = g_socket_address_get_family (sa);
3937 g_object_unref (sa);
3940 /* first selectable profile */
3941 while (profile_masks[profile_mask]
3942 && !(profiles & profile_masks[profile_mask]))
3944 if (!profile_masks[profile_mask])
3947 /* first selectable protocol */
3948 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
3950 if (!protocol_masks[mask])
3954 GST_DEBUG_OBJECT (sink, "protocols = 0x%x, protocol mask = 0x%x", protocols,
3955 protocol_masks[mask]);
3956 /* create a string with first transport in line */
3958 cur_profile = profiles & profile_masks[profile_mask];
3959 res = gst_rtsp_client_sink_create_transports_string (sink, context, family,
3960 protocols & protocol_masks[mask], cur_profile, &transports);
3961 if (res < 0 || transports == NULL)
3962 goto setup_transport_failed;
3964 if (strlen (transports) == 0) {
3965 g_free (transports);
3966 GST_DEBUG_OBJECT (sink, "no transports found");
3972 GST_DEBUG_OBJECT (sink, "transport is %s", GST_STR_NULL (transports));
3974 /* create SETUP request */
3976 gst_rtsp_client_sink_init_request (sink, &request, GST_RTSP_SETUP,
3977 context->conninfo.location);
3979 g_free (transports);
3980 goto create_request_failed;
3984 if (cur_profile == GST_RTSP_PROFILE_SAVP ||
3985 cur_profile == GST_RTSP_PROFILE_SAVPF) {
3986 hval = gst_rtsp_client_sink_stream_make_keymgmt (sink, context);
3987 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
3990 /* if the user wants a non default RTP packet size we add the blocksize
3992 if (sink->rtp_blocksize > 0) {
3993 hval = g_strdup_printf ("%d", sink->rtp_blocksize);
3994 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
3998 GST_ELEMENT_PROGRESS (sink, CONTINUE, "request", ("SETUP stream %d",
4002 GstRTSPTransport *transport;
4004 gst_rtsp_transport_new (&transport);
4005 if (gst_rtsp_transport_parse (transports, transport) != GST_RTSP_OK)
4006 goto parse_transport_failed;
4007 if (transport->lower_transport != GST_RTSP_LOWER_TRANS_TCP) {
4008 if (!gst_rtsp_stream_allocate_udp_sockets (stream, family, transport,
4010 gst_rtsp_transport_free (transport);
4011 goto allocate_udp_ports_failed;
4014 if (!gst_rtsp_stream_complete_stream (stream, transport)) {
4015 gst_rtsp_transport_free (transport);
4016 goto complete_stream_failed;
4019 gst_rtsp_transport_free (transport);
4020 gst_rtsp_stream_set_blocked (stream, FALSE);
4024 * the creation of the transports string depends on
4025 * calling stream_get_server_port, which only starts returning
4026 * something meaningful after a call to stream_allocate_udp_sockets
4027 * has been made, this function expects a transport that we parse
4028 * from the transport string ...
4030 * Significant refactoring is in order, but does not look entirely
4031 * trivial, for now we put a band aid on and create a second transport
4032 * string after the stream has been completed, to pass it in
4033 * the request headers instead of the previous, incomplete one.
4035 g_free (transports);
4037 res = gst_rtsp_client_sink_create_transports_string (sink, context, family,
4038 protocols & protocol_masks[mask], cur_profile, &transports);
4040 if (res < 0 || transports == NULL)
4041 goto setup_transport_failed;
4043 /* select transport */
4044 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
4046 /* handle the code ourselves */
4047 res = gst_rtsp_client_sink_send (sink, info, &request, &response, &code);
4052 case GST_RTSP_STS_OK:
4054 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
4055 gst_rtsp_message_unset (&request);
4056 gst_rtsp_message_unset (&response);
4058 /* Try another profile. If no more, move to the next protocol */
4060 while (profile_masks[profile_mask]
4061 && !(profiles & profile_masks[profile_mask]))
4063 if (profile_masks[profile_mask])
4066 /* select next available protocol, give up on this stream if none */
4067 /* Reset profiles to try: */
4071 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
4073 if (!protocol_masks[mask])
4078 goto response_error;
4081 /* parse response transport */
4083 gchar *resptrans = NULL;
4084 GstRTSPTransport *transport;
4086 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
4092 gst_rtsp_transport_new (&transport);
4094 /* parse transport, go to next stream on parse error */
4095 if (gst_rtsp_transport_parse (resptrans, transport) != GST_RTSP_OK) {
4096 GST_WARNING_OBJECT (sink, "failed to parse transport %s", resptrans);
4100 /* update allowed transports for other streams. once the transport of
4101 * one stream has been determined, we make sure that all other streams
4102 * are configured in the same way */
4103 switch (transport->lower_transport) {
4104 case GST_RTSP_LOWER_TRANS_TCP:
4105 GST_DEBUG_OBJECT (sink, "stream %p as TCP interleaved", stream);
4106 protocols = GST_RTSP_LOWER_TRANS_TCP;
4107 sink->interleaved = TRUE;
4108 /* update free channels */
4109 sink->free_channel =
4110 MAX (transport->interleaved.min, sink->free_channel);
4111 sink->free_channel =
4112 MAX (transport->interleaved.max, sink->free_channel);
4113 sink->free_channel++;
4115 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
4116 /* only allow multicast for other streams */
4117 GST_DEBUG_OBJECT (sink, "stream %p as UDP multicast", stream);
4118 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
4120 case GST_RTSP_LOWER_TRANS_UDP:
4121 /* only allow unicast for other streams */
4122 GST_DEBUG_OBJECT (sink, "stream %p as UDP unicast", stream);
4123 protocols = GST_RTSP_LOWER_TRANS_UDP;
4124 /* Update transport with server destination if not provided by the server */
4125 if (transport->destination == NULL) {
4126 transport->destination = g_strdup (sink->server_ip);
4130 GST_DEBUG_OBJECT (sink, "stream %p unknown transport %d", stream,
4131 transport->lower_transport);
4136 GST_DEBUG ("Configuring the stream transport for stream %d",
4138 if (context->stream_transport == NULL)
4139 context->stream_transport =
4140 gst_rtsp_stream_transport_new (stream, transport);
4142 gst_rtsp_stream_transport_set_transport (context->stream_transport,
4145 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
4146 /* our callbacks to send data on this TCP connection */
4147 gst_rtsp_stream_transport_set_callbacks (context->stream_transport,
4148 (GstRTSPSendFunc) do_send_data,
4149 (GstRTSPSendFunc) do_send_data, context, NULL);
4152 /* The stream_transport now owns the transport */
4155 gst_rtsp_stream_transport_set_active (context->stream_transport, TRUE);
4159 gst_rtsp_transport_free (transport);
4160 /* clean up used RTSP messages */
4161 gst_rtsp_message_unset (&request);
4162 gst_rtsp_message_unset (&response);
4165 GST_RTSP_STATE_UNLOCK (sink);
4167 /* store the transport protocol that was configured */
4168 sink->cur_protocols = protocols;
4174 GST_RTSP_STATE_UNLOCK (sink);
4175 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
4176 ("SDP contains no streams"));
4177 return GST_RTSP_ERROR;
4179 setup_transport_failed:
4181 GST_RTSP_STATE_UNLOCK (sink);
4182 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
4183 ("Could not setup transport."));
4184 res = GST_RTSP_ERROR;
4189 GST_RTSP_STATE_UNLOCK (sink);
4190 /* no transport possible, post an error and stop */
4191 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
4192 ("Could not connect to server, no profiles left"));
4193 return GST_RTSP_ERROR;
4197 GST_RTSP_STATE_UNLOCK (sink);
4198 /* no transport possible, post an error and stop */
4199 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
4200 ("Could not connect to server, no protocols left"));
4201 return GST_RTSP_ERROR;
4205 GST_RTSP_STATE_UNLOCK (sink);
4206 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
4207 ("Server did not select transport."));
4208 res = GST_RTSP_ERROR;
4211 create_request_failed:
4213 gchar *str = gst_rtsp_strresult (res);
4215 GST_RTSP_STATE_UNLOCK (sink);
4216 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
4217 ("Could not create request. (%s)", str));
4221 parse_transport_failed:
4223 GST_RTSP_STATE_UNLOCK (sink);
4224 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
4225 ("Could not parse transport."));
4226 res = GST_RTSP_ERROR;
4229 allocate_udp_ports_failed:
4231 GST_RTSP_STATE_UNLOCK (sink);
4232 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
4233 ("Could not parse transport."));
4234 res = GST_RTSP_ERROR;
4237 complete_stream_failed:
4239 GST_RTSP_STATE_UNLOCK (sink);
4240 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
4241 ("Could not parse transport."));
4242 res = GST_RTSP_ERROR;
4247 gchar *str = gst_rtsp_strresult (res);
4249 GST_RTSP_STATE_UNLOCK (sink);
4250 if (res != GST_RTSP_EINTR) {
4251 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
4252 ("Could not send message. (%s)", str));
4254 GST_WARNING_OBJECT (sink, "send interrupted");
4261 const gchar *str = gst_rtsp_status_as_text (code);
4263 GST_RTSP_STATE_UNLOCK (sink);
4264 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
4265 ("Error (%d): %s", code, GST_STR_NULL (str)));
4266 res = GST_RTSP_ERROR;
4271 gst_rtsp_message_unset (&request);
4272 gst_rtsp_message_unset (&response);
4277 static GstRTSPResult
4278 gst_rtsp_client_sink_ensure_open (GstRTSPClientSink * sink, gboolean async)
4280 GstRTSPResult res = GST_RTSP_OK;
4282 if (sink->state < GST_RTSP_STATE_READY) {
4283 res = GST_RTSP_ERROR;
4284 if (sink->open_error) {
4285 GST_DEBUG_OBJECT (sink, "the stream was in error");
4289 gst_rtsp_client_sink_loop_start_cmd (sink, CMD_OPEN);
4291 if ((res = gst_rtsp_client_sink_open (sink, async)) < 0) {
4292 GST_DEBUG_OBJECT (sink, "failed to open stream");
4301 static GstRTSPResult
4302 gst_rtsp_client_sink_record (GstRTSPClientSink * sink, gboolean async)
4304 GstRTSPMessage request = { 0 };
4305 GstRTSPMessage response = { 0 };
4306 GstRTSPResult res = GST_RTSP_OK;
4308 guint sdp_index = 0;
4309 GstSDPInfo info = { 0, };
4314 gchar *sess_id, *client_ip, *str;
4317 GSocket *conn_socket;
4320 g_mutex_lock (&sink->preroll_lock);
4321 if (sink->state == GST_RTSP_STATE_PLAYING) {
4322 /* Already recording, don't send another request */
4323 GST_LOG_OBJECT (sink, "Already in RECORD. Skipping duplicate request.");
4324 g_mutex_unlock (&sink->preroll_lock);
4327 g_mutex_unlock (&sink->preroll_lock);
4329 /* Collect all our input streams and create
4330 * stream objects before actually returning.
4331 * The streams are blocked at this point as we do not have any transport
4333 gst_rtsp_client_sink_collect_streams (sink);
4335 g_mutex_lock (&sink->block_streams_lock);
4336 /* Wait for streams to be blocked */
4337 while (sink->n_streams_blocked < g_list_length (sink->contexts)) {
4338 GST_DEBUG_OBJECT (sink, "waiting for streams to be blocked");
4339 g_cond_wait (&sink->block_streams_cond, &sink->block_streams_lock);
4341 g_mutex_unlock (&sink->block_streams_lock);
4343 /* Send announce, then setup for all streams */
4344 gst_sdp_message_init (&sink->cursdp);
4345 sdp = &sink->cursdp;
4347 /* some standard things first */
4348 gst_sdp_message_set_version (sdp, "0");
4350 /* session ID doesn't have to be super-unique in this case */
4351 sess_id = g_strdup_printf ("%u", g_random_int ());
4353 if (sink->conninfo.connection == NULL)
4354 return GST_RTSP_ERROR;
4356 conn_socket = gst_rtsp_connection_get_read_socket (sink->conninfo.connection);
4358 sa = g_socket_get_local_address (conn_socket, NULL);
4359 ia = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (sa));
4360 client_ip = g_inet_address_to_string (ia);
4361 if (g_socket_address_get_family (sa) == G_SOCKET_FAMILY_IPV6) {
4362 info.is_ipv6 = TRUE;
4364 } else if (g_socket_address_get_family (sa) == G_SOCKET_FAMILY_IPV4)
4367 g_assert_not_reached ();
4368 g_object_unref (sa);
4370 /* FIXME: Should this actually be the server's IP or ours? */
4371 info.server_ip = sink->server_ip;
4373 gst_sdp_message_set_origin (sdp, "-", sess_id, "1", "IN", proto, client_ip);
4375 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
4376 gst_sdp_message_set_information (sdp, "rtspclientsink");
4377 gst_sdp_message_add_time (sdp, "0", "0", NULL);
4378 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
4381 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
4382 GstRTSPStreamContext *context = (GstRTSPStreamContext *) walk->data;
4384 gst_rtsp_sdp_from_stream (sdp, &info, context->stream);
4385 context->sdp_index = sdp_index++;
4391 /* send ANNOUNCE request */
4392 GST_DEBUG_OBJECT (sink, "create ANNOUNCE request...");
4394 gst_rtsp_client_sink_init_request (sink, &request, GST_RTSP_ANNOUNCE,
4395 sink->conninfo.url_str);
4397 goto create_request_failed;
4399 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CONTENT_TYPE,
4402 /* add SDP to the request body */
4403 str = gst_sdp_message_as_text (sdp);
4404 gst_rtsp_message_take_body (&request, (guint8 *) str, strlen (str));
4407 GST_DEBUG_OBJECT (sink, "sending announce...");
4410 GST_ELEMENT_PROGRESS (sink, CONTINUE, "record",
4411 ("Sending server stream info"));
4414 gst_rtsp_client_sink_send (sink, &sink->conninfo, &request,
4415 &response, NULL)) < 0)
4418 /* parse the keymgmt */
4420 walk = sink->contexts;
4421 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_KEYMGMT,
4422 &keymgmt, i++) == GST_RTSP_OK) {
4423 GstRTSPStreamContext *context = (GstRTSPStreamContext *) walk->data;
4424 walk = g_list_next (walk);
4425 if (!gst_rtsp_stream_handle_keymgmt (context->stream, keymgmt))
4429 /* send setup for all streams */
4430 if ((res = gst_rtsp_client_sink_setup_streams (sink, async)) < 0)
4433 res = gst_rtsp_client_sink_init_request (sink, &request, GST_RTSP_RECORD,
4434 sink->conninfo.url_str);
4437 goto create_request_failed;
4439 #if 0 /* FIXME: Configure a range based on input segments? */
4440 if (src->need_range) {
4441 hval = gen_range_header (src, segment);
4443 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
4446 if (segment->rate != 1.0) {
4447 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
4449 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
4451 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
4453 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
4458 GST_ELEMENT_PROGRESS (sink, CONTINUE, "record", ("Starting recording"));
4460 gst_rtsp_client_sink_send (sink, &sink->conninfo, &request,
4461 &response, NULL)) < 0)
4464 #if 0 /* FIXME: Check if servers return these for record: */
4465 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
4466 * for the RTP packets. If this is not present, we assume all starts from 0...
4467 * This is info for the RTP session manager that we pass to it in caps. */
4469 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
4470 &hval, hval_idx++) == GST_RTSP_OK)
4471 gst_rtspsrc_parse_rtpinfo (src, hval);
4473 /* some servers indicate RTCP parameters in PLAY response,
4474 * rather than properly in SDP */
4475 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
4476 &hval, 0) == GST_RTSP_OK)
4477 gst_rtspsrc_handle_rtcp_interval (src, hval);
4480 gst_rtsp_client_sink_set_state (sink, GST_STATE_PLAYING);
4481 sink->state = GST_RTSP_STATE_PLAYING;
4483 /* clean up any messages */
4484 gst_rtsp_message_unset (&request);
4485 gst_rtsp_message_unset (&response);
4490 create_request_failed:
4492 gchar *str = gst_rtsp_strresult (res);
4494 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
4495 ("Could not create request. (%s)", str));
4501 /* Don't post a message - the rtsp_send method will have
4502 * taken care of it because we passed NULL for the response code */
4507 GST_ELEMENT_ERROR (sink, STREAM, DECRYPT_NOKEY, (NULL),
4508 ("Could not handle KeyMgmt"));
4512 GST_ERROR_OBJECT (sink, "setup failed");
4517 if (sink->conninfo.connection) {
4518 GST_DEBUG_OBJECT (sink, "free connection");
4519 gst_rtsp_conninfo_close (sink, &sink->conninfo, TRUE);
4521 gst_rtsp_message_unset (&request);
4522 gst_rtsp_message_unset (&response);
4527 static GstRTSPResult
4528 gst_rtsp_client_sink_pause (GstRTSPClientSink * sink, gboolean async)
4530 GstRTSPResult res = GST_RTSP_OK;
4531 GstRTSPMessage request = { 0 };
4532 GstRTSPMessage response = { 0 };
4534 const gchar *control;
4536 GST_DEBUG_OBJECT (sink, "PAUSE...");
4538 if ((res = gst_rtsp_client_sink_ensure_open (sink, async)) < 0)
4541 if (!(sink->methods & GST_RTSP_PAUSE))
4544 if (sink->state == GST_RTSP_STATE_READY)
4547 if (!sink->conninfo.connection || !sink->conninfo.connected)
4550 /* construct a control url */
4551 control = get_aggregate_control (sink);
4553 /* loop over the streams. We might exit the loop early when we could do an
4554 * aggregate control */
4555 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
4556 GstRTSPStreamContext *stream = (GstRTSPStreamContext *) walk->data;
4557 GstRTSPConnInfo *info;
4558 const gchar *setup_url;
4560 /* try aggregate control first but do non-aggregate control otherwise */
4562 setup_url = control;
4563 else if ((setup_url = stream->conninfo.location) == NULL)
4566 if (sink->conninfo.connection) {
4567 info = &sink->conninfo;
4568 } else if (stream->conninfo.connection) {
4569 info = &stream->conninfo;
4575 GST_ELEMENT_PROGRESS (sink, CONTINUE, "request",
4576 ("Sending PAUSE request"));
4579 gst_rtsp_client_sink_init_request (sink, &request, GST_RTSP_PAUSE,
4581 goto create_request_failed;
4584 gst_rtsp_client_sink_send (sink, info, &request, &response,
4588 gst_rtsp_message_unset (&request);
4589 gst_rtsp_message_unset (&response);
4591 /* exit early when we did agregate control */
4596 /* change element states now */
4597 gst_rtsp_client_sink_set_state (sink, GST_STATE_PAUSED);
4600 sink->state = GST_RTSP_STATE_READY;
4604 gst_rtsp_client_sink_loop_end_cmd (sink, CMD_PAUSE, res);
4611 GST_DEBUG_OBJECT (sink, "failed to open stream");
4616 GST_DEBUG_OBJECT (sink, "PAUSE is not supported");
4621 GST_DEBUG_OBJECT (sink, "we were already PAUSED");
4624 create_request_failed:
4626 gchar *str = gst_rtsp_strresult (res);
4628 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
4629 ("Could not create request. (%s)", str));
4635 gchar *str = gst_rtsp_strresult (res);
4637 gst_rtsp_message_unset (&request);
4638 if (res != GST_RTSP_EINTR) {
4639 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
4640 ("Could not send message. (%s)", str));
4642 GST_WARNING_OBJECT (sink, "PAUSE interrupted");
4650 gst_rtsp_client_sink_handle_message (GstBin * bin, GstMessage * message)
4652 GstRTSPClientSink *rtsp_client_sink;
4654 rtsp_client_sink = GST_RTSP_CLIENT_SINK (bin);
4656 switch (GST_MESSAGE_TYPE (message)) {
4657 case GST_MESSAGE_ELEMENT:
4659 const GstStructure *s = gst_message_get_structure (message);
4661 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
4662 gboolean ignore_timeout;
4664 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
4666 GST_OBJECT_LOCK (rtsp_client_sink);
4667 ignore_timeout = rtsp_client_sink->ignore_timeout;
4668 rtsp_client_sink->ignore_timeout = TRUE;
4669 GST_OBJECT_UNLOCK (rtsp_client_sink);
4671 /* we only act on the first udp timeout message, others are irrelevant
4672 * and can be ignored. */
4673 if (!ignore_timeout)
4674 gst_rtsp_client_sink_loop_send_cmd (rtsp_client_sink, CMD_RECONNECT,
4677 gst_message_unref (message);
4679 } else if (gst_structure_has_name (s, "GstRTSPStreamBlocking")) {
4680 /* An RTSPStream has prerolled */
4681 GST_DEBUG_OBJECT (rtsp_client_sink, "received GstRTSPStreamBlocking");
4682 g_mutex_lock (&rtsp_client_sink->block_streams_lock);
4683 rtsp_client_sink->n_streams_blocked++;
4684 g_cond_broadcast (&rtsp_client_sink->block_streams_cond);
4685 g_mutex_unlock (&rtsp_client_sink->block_streams_lock);
4687 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
4690 case GST_MESSAGE_ASYNC_START:{
4693 sender = GST_MESSAGE_SRC (message);
4695 GST_LOG_OBJECT (rtsp_client_sink,
4696 "Have async-start from %" GST_PTR_FORMAT, sender);
4697 if (sender == GST_OBJECT (rtsp_client_sink->internal_bin)) {
4698 GST_LOG_OBJECT (rtsp_client_sink, "child bin is now ASYNC");
4700 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
4703 case GST_MESSAGE_ASYNC_DONE:
4706 gboolean need_async_done;
4708 sender = GST_MESSAGE_SRC (message);
4709 GST_LOG_OBJECT (rtsp_client_sink, "Have async-done from %" GST_PTR_FORMAT,
4712 g_mutex_lock (&rtsp_client_sink->preroll_lock);
4713 if (sender == GST_OBJECT_CAST (rtsp_client_sink->internal_bin)) {
4714 GST_LOG_OBJECT (rtsp_client_sink, "child bin is no longer ASYNC");
4716 need_async_done = rtsp_client_sink->in_async;
4717 if (rtsp_client_sink->in_async) {
4718 rtsp_client_sink->in_async = FALSE;
4719 g_cond_broadcast (&rtsp_client_sink->preroll_cond);
4721 g_mutex_unlock (&rtsp_client_sink->preroll_lock);
4723 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
4725 if (need_async_done) {
4726 GST_DEBUG_OBJECT (rtsp_client_sink, "Posting ASYNC-DONE");
4727 gst_element_post_message (GST_ELEMENT_CAST (rtsp_client_sink),
4728 gst_message_new_async_done (GST_OBJECT_CAST (rtsp_client_sink),
4729 GST_CLOCK_TIME_NONE));
4733 case GST_MESSAGE_ERROR:
4737 sender = GST_MESSAGE_SRC (message);
4739 GST_DEBUG_OBJECT (rtsp_client_sink, "got error from %s",
4740 GST_ELEMENT_NAME (sender));
4742 /* FIXME: Ignore errors on RTCP? */
4743 /* fatal but not our message, forward */
4744 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
4747 case GST_MESSAGE_STATE_CHANGED:
4749 if (GST_MESSAGE_SRC (message) ==
4750 (GstObject *) rtsp_client_sink->internal_bin) {
4751 GstState newstate, pending;
4752 gst_message_parse_state_changed (message, NULL, &newstate, &pending);
4753 g_mutex_lock (&rtsp_client_sink->preroll_lock);
4754 rtsp_client_sink->prerolled = (newstate >= GST_STATE_PAUSED)
4755 && pending == GST_STATE_VOID_PENDING;
4756 g_cond_broadcast (&rtsp_client_sink->preroll_cond);
4757 g_mutex_unlock (&rtsp_client_sink->preroll_lock);
4758 GST_DEBUG_OBJECT (bin,
4759 "Internal bin changed state to %s (pending %s). Prerolled now %d",
4760 gst_element_state_get_name (newstate),
4761 gst_element_state_get_name (pending), rtsp_client_sink->prerolled);
4767 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
4773 /* the thread where everything happens */
4775 gst_rtsp_client_sink_thread (GstRTSPClientSink * sink)
4779 GST_OBJECT_LOCK (sink);
4780 cmd = sink->pending_cmd;
4781 if (cmd == CMD_RECONNECT || cmd == CMD_RECORD || cmd == CMD_PAUSE
4782 || cmd == CMD_LOOP || cmd == CMD_OPEN)
4783 sink->pending_cmd = CMD_LOOP;
4785 sink->pending_cmd = CMD_WAIT;
4786 GST_DEBUG_OBJECT (sink, "got command %s", cmd_to_string (cmd));
4788 /* we got the message command, so ensure communication is possible again */
4789 gst_rtsp_client_sink_connection_flush (sink, FALSE);
4791 sink->busy_cmd = cmd;
4792 GST_OBJECT_UNLOCK (sink);
4796 if (gst_rtsp_client_sink_open (sink, TRUE) == GST_RTSP_ERROR)
4797 gst_rtsp_client_sink_loop_send_cmd (sink, CMD_WAIT,
4798 CMD_ALL & ~CMD_CLOSE);
4801 gst_rtsp_client_sink_record (sink, TRUE);
4804 gst_rtsp_client_sink_pause (sink, TRUE);
4807 gst_rtsp_client_sink_close (sink, TRUE, FALSE);
4810 gst_rtsp_client_sink_loop (sink);
4813 gst_rtsp_client_sink_reconnect (sink, FALSE);
4819 GST_OBJECT_LOCK (sink);
4820 /* and go back to sleep */
4821 if (sink->pending_cmd == CMD_WAIT) {
4823 gst_task_pause (sink->task);
4826 sink->busy_cmd = CMD_WAIT;
4827 GST_OBJECT_UNLOCK (sink);
4831 gst_rtsp_client_sink_start (GstRTSPClientSink * sink)
4833 GST_DEBUG_OBJECT (sink, "starting");
4835 sink->streams_collected = FALSE;
4836 gst_element_set_locked_state (GST_ELEMENT (sink->internal_bin), TRUE);
4838 gst_rtsp_client_sink_set_state (sink, GST_STATE_READY);
4840 GST_OBJECT_LOCK (sink);
4841 sink->pending_cmd = CMD_WAIT;
4843 if (sink->task == NULL) {
4845 gst_task_new ((GstTaskFunction) gst_rtsp_client_sink_thread, sink,
4847 if (sink->task == NULL)
4850 gst_task_set_lock (sink->task, GST_RTSP_STREAM_GET_LOCK (sink));
4852 GST_OBJECT_UNLOCK (sink);
4859 GST_OBJECT_UNLOCK (sink);
4860 GST_ERROR_OBJECT (sink, "failed to create task");
4866 gst_rtsp_client_sink_stop (GstRTSPClientSink * sink)
4870 GST_DEBUG_OBJECT (sink, "stopping");
4872 /* also cancels pending task */
4873 gst_rtsp_client_sink_loop_send_cmd (sink, CMD_WAIT, CMD_ALL & ~CMD_CLOSE);
4875 GST_OBJECT_LOCK (sink);
4876 if ((task = sink->task)) {
4878 GST_OBJECT_UNLOCK (sink);
4880 gst_task_stop (task);
4882 /* make sure it is not running */
4883 GST_RTSP_STREAM_LOCK (sink);
4884 GST_RTSP_STREAM_UNLOCK (sink);
4886 /* now wait for the task to finish */
4887 gst_task_join (task);
4889 /* and free the task */
4890 gst_object_unref (GST_OBJECT (task));
4892 GST_OBJECT_LOCK (sink);
4894 GST_OBJECT_UNLOCK (sink);
4896 /* ensure synchronously all is closed and clean */
4897 gst_rtsp_client_sink_close (sink, FALSE, TRUE);
4902 static GstStateChangeReturn
4903 gst_rtsp_client_sink_change_state (GstElement * element,
4904 GstStateChange transition)
4906 GstRTSPClientSink *rtsp_client_sink;
4907 GstStateChangeReturn ret;
4909 rtsp_client_sink = GST_RTSP_CLIENT_SINK (element);
4911 switch (transition) {
4912 case GST_STATE_CHANGE_NULL_TO_READY:
4913 if (!gst_rtsp_client_sink_start (rtsp_client_sink))
4916 case GST_STATE_CHANGE_READY_TO_PAUSED:
4917 /* init some state */
4918 rtsp_client_sink->cur_protocols = rtsp_client_sink->protocols;
4919 /* first attempt, don't ignore timeouts */
4920 rtsp_client_sink->ignore_timeout = FALSE;
4921 rtsp_client_sink->open_error = FALSE;
4923 gst_rtsp_client_sink_set_state (rtsp_client_sink, GST_STATE_PAUSED);
4925 g_mutex_lock (&rtsp_client_sink->preroll_lock);
4926 if (rtsp_client_sink->in_async) {
4927 GST_DEBUG_OBJECT (rtsp_client_sink, "Posting ASYNC-START");
4928 gst_element_post_message (GST_ELEMENT_CAST (rtsp_client_sink),
4929 gst_message_new_async_start (GST_OBJECT_CAST (rtsp_client_sink)));
4931 g_mutex_unlock (&rtsp_client_sink->preroll_lock);
4934 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4936 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4937 /* unblock the tcp tasks and make the loop waiting */
4938 if (gst_rtsp_client_sink_loop_send_cmd (rtsp_client_sink, CMD_WAIT,
4940 /* make sure it is waiting before we send PLAY below */
4941 GST_RTSP_STREAM_LOCK (rtsp_client_sink);
4942 GST_RTSP_STREAM_UNLOCK (rtsp_client_sink);
4945 case GST_STATE_CHANGE_PAUSED_TO_READY:
4946 gst_rtsp_client_sink_set_state (rtsp_client_sink, GST_STATE_READY);
4952 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4953 if (ret == GST_STATE_CHANGE_FAILURE)
4956 switch (transition) {
4957 case GST_STATE_CHANGE_NULL_TO_READY:
4958 ret = GST_STATE_CHANGE_SUCCESS;
4960 case GST_STATE_CHANGE_READY_TO_PAUSED:
4961 /* Return ASYNC and preroll input streams */
4962 g_mutex_lock (&rtsp_client_sink->preroll_lock);
4963 if (rtsp_client_sink->in_async)
4964 ret = GST_STATE_CHANGE_ASYNC;
4965 g_mutex_unlock (&rtsp_client_sink->preroll_lock);
4966 gst_rtsp_client_sink_loop_send_cmd (rtsp_client_sink, CMD_OPEN, 0);
4968 /* CMD_OPEN has been scheduled. Wait until the sink thread starts
4969 * opening connection to the server */
4970 g_mutex_lock (&rtsp_client_sink->open_conn_lock);
4971 while (!rtsp_client_sink->open_conn_start) {
4972 GST_DEBUG_OBJECT (rtsp_client_sink,
4973 "wait for connection to be started");
4974 g_cond_wait (&rtsp_client_sink->open_conn_cond,
4975 &rtsp_client_sink->open_conn_lock);
4977 rtsp_client_sink->open_conn_start = FALSE;
4978 g_mutex_unlock (&rtsp_client_sink->open_conn_lock);
4980 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:{
4981 GST_DEBUG_OBJECT (rtsp_client_sink,
4982 "Switching to playing -sending RECORD");
4983 gst_rtsp_client_sink_loop_send_cmd (rtsp_client_sink, CMD_RECORD, 0);
4984 ret = GST_STATE_CHANGE_SUCCESS;
4987 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4988 /* send pause request and keep the idle task around */
4989 gst_rtsp_client_sink_loop_send_cmd (rtsp_client_sink, CMD_PAUSE,
4991 ret = GST_STATE_CHANGE_NO_PREROLL;
4993 case GST_STATE_CHANGE_PAUSED_TO_READY:
4994 gst_rtsp_client_sink_loop_send_cmd (rtsp_client_sink, CMD_CLOSE,
4996 ret = GST_STATE_CHANGE_SUCCESS;
4998 case GST_STATE_CHANGE_READY_TO_NULL:
4999 gst_rtsp_client_sink_stop (rtsp_client_sink);
5000 ret = GST_STATE_CHANGE_SUCCESS;
5011 GST_DEBUG_OBJECT (rtsp_client_sink, "start failed");
5012 return GST_STATE_CHANGE_FAILURE;
5016 /*** GSTURIHANDLER INTERFACE *************************************************/
5019 gst_rtsp_client_sink_uri_get_type (GType type)
5021 return GST_URI_SINK;
5024 static const gchar *const *
5025 gst_rtsp_client_sink_uri_get_protocols (GType type)
5027 static const gchar *protocols[] =
5028 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
5029 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
5036 gst_rtsp_client_sink_uri_get_uri (GstURIHandler * handler)
5038 GstRTSPClientSink *sink = GST_RTSP_CLIENT_SINK (handler);
5040 /* FIXME: make thread-safe */
5041 return g_strdup (sink->conninfo.location);
5045 gst_rtsp_client_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri,
5048 GstRTSPClientSink *sink;
5051 GstRTSPUrl *newurl = NULL;
5052 GstSDPMessage *sdp = NULL;
5054 sink = GST_RTSP_CLIENT_SINK (handler);
5056 /* same URI, we're fine */
5057 if (sink->conninfo.location && uri && !strcmp (uri, sink->conninfo.location))
5060 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
5061 sres = gst_sdp_message_new (&sdp);
5065 GST_DEBUG_OBJECT (sink, "parsing SDP message");
5066 sres = gst_sdp_message_parse_uri (uri, sdp);
5071 GST_DEBUG_OBJECT (sink, "parsing URI");
5072 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
5076 /* if worked, free previous and store new url object along with the original
5078 GST_DEBUG_OBJECT (sink, "configuring URI");
5079 g_free (sink->conninfo.location);
5080 sink->conninfo.location = g_strdup (uri);
5081 gst_rtsp_url_free (sink->conninfo.url);
5082 sink->conninfo.url = newurl;
5083 g_free (sink->conninfo.url_str);
5085 sink->conninfo.url_str = gst_rtsp_url_get_request_uri (sink->conninfo.url);
5087 sink->conninfo.url_str = NULL;
5090 gst_sdp_message_free (sink->uri_sdp);
5091 sink->uri_sdp = sdp;
5092 sink->from_sdp = sdp != NULL;
5094 GST_DEBUG_OBJECT (sink, "set uri: %s", GST_STR_NULL (uri));
5095 GST_DEBUG_OBJECT (sink, "request uri is: %s",
5096 GST_STR_NULL (sink->conninfo.url_str));
5103 GST_DEBUG_OBJECT (sink, "URI was ok: '%s'", GST_STR_NULL (uri));
5108 GST_ERROR_OBJECT (sink, "Could not create new SDP (%d)", sres);
5109 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
5110 "Could not create SDP");
5115 GST_ERROR_OBJECT (sink, "Not a valid SDP (%d) '%s'", sres,
5116 GST_STR_NULL (uri));
5117 gst_sdp_message_free (sdp);
5118 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
5124 GST_ERROR_OBJECT (sink, "Not a valid RTSP url '%s' (%d)",
5125 GST_STR_NULL (uri), res);
5126 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
5127 "Invalid RTSP URI");
5133 gst_rtsp_client_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
5135 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
5137 iface->get_type = gst_rtsp_client_sink_uri_get_type;
5138 iface->get_protocols = gst_rtsp_client_sink_uri_get_protocols;
5139 iface->get_uri = gst_rtsp_client_sink_uri_get_uri;
5140 iface->set_uri = gst_rtsp_client_sink_uri_set_uri;