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 GST_DEBUG_CATEGORY_STATIC (rtsp_client_sink_debug);
106 #define GST_CAT_DEFAULT (rtsp_client_sink_debug)
108 static GstStaticPadTemplate rtptemplate = GST_STATIC_PAD_TEMPLATE ("stream_%u",
111 GST_STATIC_CAPS_ANY); /* Actual caps come from available set of payloaders */
115 SIGNAL_HANDLE_REQUEST,
117 SIGNAL_NEW_PAYLOADER,
118 SIGNAL_REQUEST_RTCP_KEY,
122 enum _GstRTSPClientSinkNtpTimeSource
125 NTP_TIME_SOURCE_UNIX,
126 NTP_TIME_SOURCE_RUNNING_TIME,
127 NTP_TIME_SOURCE_CLOCK_TIME
130 #define GST_TYPE_RTSP_CLIENT_SINK_NTP_TIME_SOURCE (gst_rtsp_client_sink_ntp_time_source_get_type())
132 gst_rtsp_client_sink_ntp_time_source_get_type (void)
134 static GType ntp_time_source_type = 0;
135 static const GEnumValue ntp_time_source_values[] = {
136 {NTP_TIME_SOURCE_NTP, "NTP time based on realtime clock", "ntp"},
137 {NTP_TIME_SOURCE_UNIX, "UNIX time based on realtime clock", "unix"},
138 {NTP_TIME_SOURCE_RUNNING_TIME,
139 "Running time based on pipeline clock",
141 {NTP_TIME_SOURCE_CLOCK_TIME, "Pipeline clock time", "clock-time"},
145 if (!ntp_time_source_type) {
146 ntp_time_source_type =
147 g_enum_register_static ("GstRTSPClientSinkNtpTimeSource",
148 ntp_time_source_values);
150 return ntp_time_source_type;
153 #define DEFAULT_LOCATION NULL
154 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
155 #define DEFAULT_DEBUG FALSE
156 #define DEFAULT_RETRY 20
157 #define DEFAULT_TIMEOUT 5000000
158 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
159 #define DEFAULT_TCP_TIMEOUT 20000000
160 #define DEFAULT_LATENCY_MS 2000
161 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
162 #define DEFAULT_PROXY NULL
163 #define DEFAULT_RTP_BLOCKSIZE 0
164 #define DEFAULT_USER_ID NULL
165 #define DEFAULT_USER_PW NULL
166 #define DEFAULT_PORT_RANGE NULL
167 #define DEFAULT_UDP_RECONNECT TRUE
168 #define DEFAULT_MULTICAST_IFACE NULL
169 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
170 #define DEFAULT_TLS_DATABASE NULL
171 #define DEFAULT_TLS_INTERACTION NULL
172 #define DEFAULT_NTP_TIME_SOURCE NTP_TIME_SOURCE_NTP
173 #define DEFAULT_USER_AGENT "GStreamer/" PACKAGE_VERSION
174 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
175 #define DEFAULT_RTX_TIME_MS 500
188 PROP_DO_RTSP_KEEP_ALIVE,
196 PROP_UDP_BUFFER_SIZE,
198 PROP_MULTICAST_IFACE,
200 PROP_TLS_VALIDATION_FLAGS,
202 PROP_TLS_INTERACTION,
203 PROP_NTP_TIME_SOURCE,
208 static void gst_rtsp_client_sink_finalize (GObject * object);
210 static void gst_rtsp_client_sink_set_property (GObject * object, guint prop_id,
211 const GValue * value, GParamSpec * pspec);
212 static void gst_rtsp_client_sink_get_property (GObject * object, guint prop_id,
213 GValue * value, GParamSpec * pspec);
215 static GstClock *gst_rtsp_client_sink_provide_clock (GstElement * element);
217 static void gst_rtsp_client_sink_uri_handler_init (gpointer g_iface,
218 gpointer iface_data);
220 static gboolean gst_rtsp_client_sink_set_proxy (GstRTSPClientSink * rtsp,
221 const gchar * proxy);
222 static void gst_rtsp_client_sink_set_tcp_timeout (GstRTSPClientSink *
223 rtsp_client_sink, guint64 timeout);
225 static GstStateChangeReturn gst_rtsp_client_sink_change_state (GstElement *
226 element, GstStateChange transition);
227 static void gst_rtsp_client_sink_handle_message (GstBin * bin,
228 GstMessage * message);
230 static gboolean gst_rtsp_client_sink_setup_auth (GstRTSPClientSink * sink,
231 GstRTSPMessage * response);
233 static gboolean gst_rtsp_client_sink_loop_send_cmd (GstRTSPClientSink * sink,
234 gint cmd, gint mask);
236 static GstRTSPResult gst_rtsp_client_sink_open (GstRTSPClientSink * sink,
238 static GstRTSPResult gst_rtsp_client_sink_record (GstRTSPClientSink * sink,
240 static GstRTSPResult gst_rtsp_client_sink_pause (GstRTSPClientSink * sink,
242 static GstRTSPResult gst_rtsp_client_sink_close (GstRTSPClientSink * sink,
243 gboolean async, gboolean only_close);
244 static gboolean gst_rtsp_client_sink_collect_streams (GstRTSPClientSink * sink);
246 static gboolean gst_rtsp_client_sink_uri_set_uri (GstURIHandler * handler,
247 const gchar * uri, GError ** error);
248 static gchar *gst_rtsp_client_sink_uri_get_uri (GstURIHandler * handler);
250 static gboolean gst_rtsp_client_sink_loop (GstRTSPClientSink * sink);
251 static void gst_rtsp_client_sink_connection_flush (GstRTSPClientSink * sink,
254 static GstPad *gst_rtsp_client_sink_request_new_pad (GstElement * element,
255 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
256 static void gst_rtsp_client_sink_release_pad (GstElement * element,
259 /* commands we send to out loop to notify it of events */
260 #define CMD_OPEN (1 << 0)
261 #define CMD_RECORD (1 << 1)
262 #define CMD_PAUSE (1 << 2)
263 #define CMD_CLOSE (1 << 3)
264 #define CMD_WAIT (1 << 4)
265 #define CMD_RECONNECT (1 << 5)
266 #define CMD_LOOP (1 << 6)
268 /* mask for all commands */
269 #define CMD_ALL ((CMD_LOOP << 1) - 1)
271 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
273 gchar *__txt = _gst_element_error_printf text; \
274 gst_element_post_message (GST_ELEMENT_CAST (el), \
275 gst_message_new_progress (GST_OBJECT_CAST (el), \
276 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
280 static guint gst_rtsp_client_sink_signals[LAST_SIGNAL] = { 0 };
282 #define gst_rtsp_client_sink_parent_class parent_class
283 G_DEFINE_TYPE_WITH_CODE (GstRTSPClientSink, gst_rtsp_client_sink, GST_TYPE_BIN,
284 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
285 gst_rtsp_client_sink_uri_handler_init));
287 #ifndef GST_DISABLE_GST_DEBUG
288 static inline const gchar *
289 cmd_to_string (guint cmd)
313 gst_rtsp_client_sink_class_init (GstRTSPClientSinkClass * klass)
315 GObjectClass *gobject_class;
316 GstElementClass *gstelement_class;
317 GstBinClass *gstbin_class;
319 gobject_class = (GObjectClass *) klass;
320 gstelement_class = (GstElementClass *) klass;
321 gstbin_class = (GstBinClass *) klass;
323 GST_DEBUG_CATEGORY_INIT (rtsp_client_sink_debug, "rtspclientsink", 0,
324 "RTSP sink element");
326 gobject_class->set_property = gst_rtsp_client_sink_set_property;
327 gobject_class->get_property = gst_rtsp_client_sink_get_property;
329 gobject_class->finalize = gst_rtsp_client_sink_finalize;
331 g_object_class_install_property (gobject_class, PROP_LOCATION,
332 g_param_spec_string ("location", "RTSP Location",
333 "Location of the RTSP url to read",
334 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
336 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
337 g_param_spec_flags ("protocols", "Protocols",
338 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
339 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
341 g_object_class_install_property (gobject_class, PROP_PROFILES,
342 g_param_spec_flags ("profiles", "Profiles",
343 "Allowed RTSP profiles", GST_TYPE_RTSP_PROFILE,
344 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
346 g_object_class_install_property (gobject_class, PROP_DEBUG,
347 g_param_spec_boolean ("debug", "Debug",
348 "Dump request and response messages to stdout",
349 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
351 g_object_class_install_property (gobject_class, PROP_RETRY,
352 g_param_spec_uint ("retry", "Retry",
353 "Max number of retries when allocating RTP ports.",
354 0, G_MAXUINT16, DEFAULT_RETRY,
355 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
357 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
358 g_param_spec_uint64 ("timeout", "Timeout",
359 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
360 0, G_MAXUINT64, DEFAULT_TIMEOUT,
361 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
363 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
364 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
365 "Fail after timeout microseconds on TCP connections (0 = disabled)",
366 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
367 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
369 g_object_class_install_property (gobject_class, PROP_LATENCY,
370 g_param_spec_uint ("latency", "Buffer latency in ms",
371 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
372 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
374 g_object_class_install_property (gobject_class, PROP_RTX_TIME,
375 g_param_spec_uint ("rtx-time", "Retransmission buffer in ms",
376 "Amount of ms to buffer for retransmission. 0 disables retransmission",
377 0, G_MAXUINT, DEFAULT_RTX_TIME_MS,
378 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
381 * GstRTSPClientSink:do-rtsp-keep-alive:
383 * Enable RTSP keep alive support. Some old server don't like RTSP
384 * keep alive and then this property needs to be set to FALSE.
386 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
387 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
388 "Send RTSP keep alive packets, disable for old incompatible server.",
389 DEFAULT_DO_RTSP_KEEP_ALIVE,
390 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
393 * GstRTSPClientSink:proxy:
395 * Set the proxy parameters. This has to be a string of the format
396 * [http://][user:passwd@]host[:port].
398 g_object_class_install_property (gobject_class, PROP_PROXY,
399 g_param_spec_string ("proxy", "Proxy",
400 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
401 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
403 * GstRTSPClientSink:proxy-id:
405 * Sets the proxy URI user id for authentication. If the URI set via the
406 * "proxy" property contains a user-id already, that will take precedence.
409 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
410 g_param_spec_string ("proxy-id", "proxy-id",
411 "HTTP proxy URI user id for authentication", "",
412 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
414 * GstRTSPClientSink:proxy-pw:
416 * Sets the proxy URI password for authentication. If the URI set via the
417 * "proxy" property contains a password already, that will take precedence.
420 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
421 g_param_spec_string ("proxy-pw", "proxy-pw",
422 "HTTP proxy URI user password for authentication", "",
423 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
426 * GstRTSPClientSink:rtp-blocksize:
428 * RTP package size to suggest to server.
430 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
431 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
432 "RTP package size to suggest to server (0 = disabled)",
433 0, 65536, DEFAULT_RTP_BLOCKSIZE,
434 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
436 g_object_class_install_property (gobject_class,
438 g_param_spec_string ("user-id", "user-id",
439 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
440 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
441 g_object_class_install_property (gobject_class, PROP_USER_PW,
442 g_param_spec_string ("user-pw", "user-pw",
443 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
444 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
447 * GstRTSPClientSink:port-range:
449 * Configure the client port numbers that can be used to receive
452 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
453 g_param_spec_string ("port-range", "Port range",
454 "Client port range that can be used to receive RTCP data, "
455 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
456 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
459 * GstRTSPClientSink:udp-buffer-size:
461 * Size of the kernel UDP receive buffer in bytes.
463 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
464 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
465 "Size of the kernel UDP receive buffer in bytes, 0=default",
466 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
467 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
469 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
470 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
471 "Reconnect to the server if RTSP connection is closed when doing UDP",
472 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
474 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
475 g_param_spec_string ("multicast-iface", "Multicast Interface",
476 "The network interface on which to join the multicast group",
477 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
479 g_object_class_install_property (gobject_class, PROP_SDES,
480 g_param_spec_boxed ("sdes", "SDES",
481 "The SDES items of this session",
482 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
485 * GstRTSPClientSink::tls-validation-flags:
487 * TLS certificate validation flags used to validate server
491 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
492 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
493 "TLS certificate validation flags used to validate the server certificate",
494 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
495 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
498 * GstRTSPClientSink::tls-database:
500 * TLS database with anchor certificate authorities used to validate
501 * the server certificate.
504 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
505 g_param_spec_object ("tls-database", "TLS database",
506 "TLS database with anchor certificate authorities used to validate the server certificate",
507 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
510 * GstRTSPClientSink::tls-interaction:
512 * A #GTlsInteraction object to be used when the connection or certificate
513 * database need to interact with the user. This will be used to prompt the
514 * user for passwords where necessary.
517 g_object_class_install_property (gobject_class, PROP_TLS_INTERACTION,
518 g_param_spec_object ("tls-interaction", "TLS interaction",
519 "A GTlsInteraction object to prompt the user for password or certificate",
520 G_TYPE_TLS_INTERACTION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
523 * GstRTSPClientSink::ntp-time-source:
525 * allows to select the time source that should be used
526 * for the NTP time in outgoing packets
529 g_object_class_install_property (gobject_class, PROP_NTP_TIME_SOURCE,
530 g_param_spec_enum ("ntp-time-source", "NTP Time Source",
531 "NTP time source for RTCP packets",
532 GST_TYPE_RTSP_CLIENT_SINK_NTP_TIME_SOURCE, DEFAULT_NTP_TIME_SOURCE,
533 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
536 * GstRTSPClientSink::user-agent:
538 * The string to set in the User-Agent header.
541 g_object_class_install_property (gobject_class, PROP_USER_AGENT,
542 g_param_spec_string ("user-agent", "User Agent",
543 "The User-Agent string to send to the server",
544 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
547 * GstRTSPClientSink::handle-request:
548 * @rtsp_client_sink: a #GstRTSPClientSink
549 * @request: a #GstRTSPMessage
550 * @response: a #GstRTSPMessage
552 * Handle a server request in @request and prepare @response.
554 * This signal is called from the streaming thread, you should therefore not
555 * do any state changes on @rtsp_client_sink because this might deadlock. If you want
556 * to modify the state as a result of this signal, post a
557 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
561 gst_rtsp_client_sink_signals[SIGNAL_HANDLE_REQUEST] =
562 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
563 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
564 G_TYPE_POINTER, G_TYPE_POINTER);
567 * GstRTSPClientSink::new-manager:
568 * @rtsp_client_sink: a #GstRTSPClientSink
569 * @manager: a #GstElement
571 * Emitted after a new manager (like rtpbin) was created and the default
572 * properties were configured.
575 gst_rtsp_client_sink_signals[SIGNAL_NEW_MANAGER] =
576 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
577 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
578 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
581 * GstRTSPClientSink::new-payloader:
582 * @rtsp_client_sink: a #GstRTSPClientSink
583 * @payloader: a #GstElement
585 * Emitted after a new RTP payloader was created and the default
586 * properties were configured.
589 gst_rtsp_client_sink_signals[SIGNAL_NEW_PAYLOADER] =
590 g_signal_new_class_handler ("new-payloader", G_TYPE_FROM_CLASS (klass),
591 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
592 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
595 * GstRTSPClientSink::request-rtcp-key:
596 * @rtsp_client_sink: a #GstRTSPClientSink
597 * @num: the stream number
599 * Signal emitted to get the crypto parameters relevant to the RTCP
600 * stream. User should provide the key and the RTCP encryption ciphers
601 * and authentication, and return them wrapped in a GstCaps.
604 gst_rtsp_client_sink_signals[SIGNAL_REQUEST_RTCP_KEY] =
605 g_signal_new ("request-rtcp-key", G_TYPE_FROM_CLASS (klass),
606 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, GST_TYPE_CAPS, 1, G_TYPE_UINT);
608 gstelement_class->provide_clock = gst_rtsp_client_sink_provide_clock;
609 gstelement_class->change_state = gst_rtsp_client_sink_change_state;
610 gstelement_class->request_new_pad =
611 GST_DEBUG_FUNCPTR (gst_rtsp_client_sink_request_new_pad);
612 gstelement_class->release_pad =
613 GST_DEBUG_FUNCPTR (gst_rtsp_client_sink_release_pad);
615 gst_element_class_add_static_pad_template (gstelement_class, &rtptemplate);
617 gst_element_class_set_static_metadata (gstelement_class,
618 "RTSP RECORD client", "Sink/Network",
619 "Send data over the network via RTSP RECORD(RFC 2326)",
620 "Jan Schmidt <jan@centricular.com>");
622 gstbin_class->handle_message = gst_rtsp_client_sink_handle_message;
626 gst_rtsp_client_sink_init (GstRTSPClientSink * sink)
628 sink->conninfo.location = g_strdup (DEFAULT_LOCATION);
629 sink->protocols = DEFAULT_PROTOCOLS;
630 sink->debug = DEFAULT_DEBUG;
631 sink->retry = DEFAULT_RETRY;
632 sink->udp_timeout = DEFAULT_TIMEOUT;
633 gst_rtsp_client_sink_set_tcp_timeout (sink, DEFAULT_TCP_TIMEOUT);
634 sink->latency = DEFAULT_LATENCY_MS;
635 sink->rtx_time = DEFAULT_RTX_TIME_MS;
636 sink->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
637 gst_rtsp_client_sink_set_proxy (sink, DEFAULT_PROXY);
638 sink->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
639 sink->user_id = g_strdup (DEFAULT_USER_ID);
640 sink->user_pw = g_strdup (DEFAULT_USER_PW);
641 sink->client_port_range.min = 0;
642 sink->client_port_range.max = 0;
643 sink->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
644 sink->udp_reconnect = DEFAULT_UDP_RECONNECT;
645 sink->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
647 sink->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
648 sink->tls_database = DEFAULT_TLS_DATABASE;
649 sink->tls_interaction = DEFAULT_TLS_INTERACTION;
650 sink->ntp_time_source = DEFAULT_NTP_TIME_SOURCE;
651 sink->user_agent = g_strdup (DEFAULT_USER_AGENT);
653 sink->profiles = DEFAULT_PROFILES;
655 /* protects the streaming thread in interleaved mode or the polling
656 * thread in UDP mode. */
657 g_rec_mutex_init (&sink->stream_rec_lock);
659 /* protects our state changes from multiple invocations */
660 g_rec_mutex_init (&sink->state_rec_lock);
662 g_mutex_init (&sink->send_lock);
664 g_mutex_init (&sink->preroll_lock);
665 g_cond_init (&sink->preroll_cond);
667 sink->state = GST_RTSP_STATE_INVALID;
669 g_mutex_init (&sink->conninfo.send_lock);
670 g_mutex_init (&sink->conninfo.recv_lock);
672 sink->internal_bin = (GstBin *) gst_bin_new ("rtspbin");
673 gst_element_set_locked_state (GST_ELEMENT_CAST (sink->internal_bin), TRUE);
674 gst_bin_add (GST_BIN (sink), GST_ELEMENT_CAST (sink->internal_bin));
676 sink->next_dyn_pt = 96;
678 gst_sdp_message_init (&sink->cursdp);
680 GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_FLAG_SINK);
684 gst_rtsp_client_sink_finalize (GObject * object)
686 GstRTSPClientSink *rtsp_client_sink;
688 rtsp_client_sink = GST_RTSP_CLIENT_SINK (object);
690 gst_sdp_message_uninit (&rtsp_client_sink->cursdp);
692 g_free (rtsp_client_sink->conninfo.location);
693 gst_rtsp_url_free (rtsp_client_sink->conninfo.url);
694 g_free (rtsp_client_sink->conninfo.url_str);
695 g_free (rtsp_client_sink->user_id);
696 g_free (rtsp_client_sink->user_pw);
697 g_free (rtsp_client_sink->multi_iface);
698 g_free (rtsp_client_sink->user_agent);
700 if (rtsp_client_sink->uri_sdp) {
701 gst_sdp_message_free (rtsp_client_sink->uri_sdp);
702 rtsp_client_sink->uri_sdp = NULL;
704 if (rtsp_client_sink->provided_clock)
705 gst_object_unref (rtsp_client_sink->provided_clock);
707 if (rtsp_client_sink->sdes)
708 gst_structure_free (rtsp_client_sink->sdes);
710 if (rtsp_client_sink->tls_database)
711 g_object_unref (rtsp_client_sink->tls_database);
713 if (rtsp_client_sink->tls_interaction)
714 g_object_unref (rtsp_client_sink->tls_interaction);
717 g_rec_mutex_clear (&rtsp_client_sink->stream_rec_lock);
718 g_rec_mutex_clear (&rtsp_client_sink->state_rec_lock);
720 g_mutex_clear (&rtsp_client_sink->conninfo.send_lock);
721 g_mutex_clear (&rtsp_client_sink->conninfo.recv_lock);
723 g_mutex_clear (&rtsp_client_sink->send_lock);
725 g_mutex_clear (&rtsp_client_sink->preroll_lock);
726 g_cond_clear (&rtsp_client_sink->preroll_cond);
728 G_OBJECT_CLASS (parent_class)->finalize (object);
732 gst_rtp_payloader_filter_func (GstPluginFeature * feature, gpointer user_data)
734 GstElementFactory *factory = NULL;
737 if (!GST_IS_ELEMENT_FACTORY (feature))
740 factory = GST_ELEMENT_FACTORY (feature);
742 if (gst_plugin_feature_get_rank (feature) == GST_RANK_NONE)
745 if (!gst_element_factory_list_is_type (factory,
746 GST_ELEMENT_FACTORY_TYPE_PAYLOADER))
750 gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_KLASS);
751 if (strstr (klass, "Codec") == NULL)
753 if (strstr (klass, "RTP") == NULL)
760 compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
763 const gchar *rname1, *rname2;
764 GstRank rank1, rank2;
766 rname1 = gst_plugin_feature_get_name (f1);
767 rname2 = gst_plugin_feature_get_name (f2);
769 rank1 = gst_plugin_feature_get_rank (f1);
770 rank2 = gst_plugin_feature_get_rank (f2);
772 /* HACK: Prefer rtpmp4apay over rtpmp4gpay */
773 if (g_str_equal (rname1, "rtpmp4apay"))
774 rank1 = GST_RANK_SECONDARY + 1;
775 if (g_str_equal (rname2, "rtpmp4apay"))
776 rank2 = GST_RANK_SECONDARY + 1;
778 diff = rank2 - rank1;
782 diff = strcmp (rname2, rname1);
788 gst_rtsp_client_sink_get_factories (void)
790 static GList *payloader_factories = NULL;
792 if (g_once_init_enter (&payloader_factories)) {
793 GList *all_factories;
796 gst_registry_feature_filter (gst_registry_get (),
797 gst_rtp_payloader_filter_func, FALSE, NULL);
799 all_factories = g_list_sort (all_factories, (GCompareFunc) compare_ranks);
801 g_once_init_leave (&payloader_factories, all_factories);
804 return payloader_factories;
808 gst_rtsp_client_sink_get_payloader_caps (void)
810 /* Cached caps result */
813 if (g_once_init_enter (&ret)) {
814 GList *factories, *cur;
815 GstCaps *caps = gst_caps_new_empty ();
817 factories = gst_rtsp_client_sink_get_factories ();
818 for (cur = factories; cur != NULL; cur = g_list_next (cur)) {
819 GstElementFactory *factory = GST_ELEMENT_FACTORY (cur->data);
822 for (tmp = gst_element_factory_get_static_pad_templates (factory);
823 tmp; tmp = g_list_next (tmp)) {
824 GstStaticPadTemplate *template = tmp->data;
826 if (template->direction == GST_PAD_SINK) {
827 GstCaps *static_caps = gst_static_pad_template_get_caps (template);
829 GST_LOG ("Found pad template %s on factory %s",
830 template->name_template, gst_plugin_feature_get_name (factory));
833 caps = gst_caps_merge (caps, static_caps);
835 /* Early out, any is absorbing */
836 if (gst_caps_is_any (caps))
842 g_once_init_leave (&ret, caps);
845 /* Return cached result */
846 return gst_caps_ref (ret);
850 gst_rtsp_client_sink_make_payloader (GstCaps * caps)
852 GList *factories, *cur;
854 factories = gst_rtsp_client_sink_get_factories ();
855 for (cur = factories; cur != NULL; cur = g_list_next (cur)) {
856 GstElementFactory *factory = GST_ELEMENT_FACTORY (cur->data);
859 for (tmp = gst_element_factory_get_static_pad_templates (factory);
860 tmp; tmp = g_list_next (tmp)) {
861 GstStaticPadTemplate *template = tmp->data;
863 if (template->direction == GST_PAD_SINK) {
864 GstCaps *static_caps = gst_static_pad_template_get_caps (template);
865 GstElement *payloader = NULL;
867 if (gst_caps_can_intersect (static_caps, caps)) {
868 GST_DEBUG ("caps %" GST_PTR_FORMAT " intersects with template %"
869 GST_PTR_FORMAT " for payloader %s", caps, static_caps,
870 gst_plugin_feature_get_name (factory));
871 payloader = gst_element_factory_create (factory, NULL);
874 gst_caps_unref (static_caps);
885 static GstRTSPStream *
886 gst_rtsp_client_sink_create_stream (GstRTSPClientSink * sink,
887 GstRTSPStreamContext * context, GstElement * payloader, GstPad * pad)
889 GstRTSPStream *stream = NULL;
892 GST_OBJECT_LOCK (sink);
894 g_object_get (G_OBJECT (payloader), "pt", &pt, NULL);
895 if (pt >= 96 && pt <= sink->next_dyn_pt) {
896 /* Payloader has a dynamic PT, but one that's already used */
897 /* FIXME: Create a caps->ptmap instead? */
898 pt = sink->next_dyn_pt;
903 GST_DEBUG_OBJECT (sink, "Assigning pt %u to stream %d", pt, context->index);
907 GST_DEBUG_OBJECT (sink, "Keeping existing pt %u for stream %d",
911 aux_pt = sink->next_dyn_pt;
916 GST_OBJECT_UNLOCK (sink);
919 g_object_set (G_OBJECT (payloader), "pt", pt, NULL);
921 stream = gst_rtsp_stream_new (context->index, payloader, pad);
923 gst_rtsp_stream_set_client_side (stream, TRUE);
924 gst_rtsp_stream_set_retransmission_time (stream,
925 (GstClockTime) (sink->rtx_time) * GST_MSECOND);
926 gst_rtsp_stream_set_protocols (stream, sink->protocols);
927 gst_rtsp_stream_set_profiles (stream, sink->profiles);
928 gst_rtsp_stream_set_retransmission_pt (stream, aux_pt);
929 gst_rtsp_stream_set_buffer_size (stream, sink->udp_buffer_size);
930 if (sink->rtp_blocksize > 0)
931 gst_rtsp_stream_set_mtu (stream, sink->rtp_blocksize);
932 gst_rtsp_stream_set_multicast_iface (stream, sink->multi_iface);
936 gst_rtsp_stream_set_address_pool (stream, priv->pool);
941 GST_OBJECT_UNLOCK (sink);
943 GST_ELEMENT_ERROR (sink, RESOURCE, NO_SPACE_LEFT, (NULL),
944 ("Ran out of dynamic payload types."));
949 static GstPadProbeReturn
950 handle_payloader_block (GstPad * pad, GstPadProbeInfo * info,
951 GstRTSPStreamContext * context)
953 GstRTSPClientSink *sink = context->parent;
955 GST_INFO_OBJECT (sink, "Block on pad %" GST_PTR_FORMAT, pad);
957 g_mutex_lock (&sink->preroll_lock);
958 context->prerolled = TRUE;
959 g_cond_broadcast (&sink->preroll_cond);
960 g_mutex_unlock (&sink->preroll_lock);
962 GST_INFO_OBJECT (sink, "Announced preroll on pad %" GST_PTR_FORMAT, pad);
964 return GST_PAD_PROBE_OK;
968 gst_rtsp_client_sink_setup_payloader (GstRTSPClientSink * sink, GstPad * pad,
971 GstRTSPStreamContext *context;
973 GstElement *payloader;
974 GstPad *sinkpad, *srcpad, *ghostsink;
976 context = gst_pad_get_element_private (pad);
978 /* Find the payloader. FIXME: Allow user to provide payloader via pad property */
979 payloader = gst_rtsp_client_sink_make_payloader (caps);
980 if (payloader == NULL)
983 GST_DEBUG_OBJECT (sink, "Configuring payloader %" GST_PTR_FORMAT
984 " for pad %" GST_PTR_FORMAT, payloader, pad);
986 sinkpad = gst_element_get_static_pad (payloader, "sink");
990 srcpad = gst_element_get_static_pad (payloader, "src");
994 gst_bin_add (GST_BIN (sink->internal_bin), payloader);
995 ghostsink = gst_ghost_pad_new (NULL, sinkpad);
996 gst_pad_set_active (ghostsink, TRUE);
997 gst_element_add_pad (GST_ELEMENT (sink->internal_bin), ghostsink);
999 g_signal_emit (sink, gst_rtsp_client_sink_signals[SIGNAL_NEW_PAYLOADER], 0,
1002 GST_RTSP_STATE_LOCK (sink);
1003 context->payloader_block_id =
1004 gst_pad_add_probe (srcpad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
1005 (GstPadProbeCallback) handle_payloader_block, context, NULL);
1006 context->payloader = payloader;
1008 payloader = gst_object_ref (payloader);
1010 gst_ghost_pad_set_target (GST_GHOST_PAD (pad), ghostsink);
1011 gst_object_unref (GST_OBJECT (sinkpad));
1012 GST_RTSP_STATE_UNLOCK (sink);
1014 gst_element_sync_state_with_parent (payloader);
1016 gst_object_unref (payloader);
1017 gst_object_unref (GST_OBJECT (srcpad));
1022 GST_ERROR_OBJECT (sink,
1023 "Could not find sink pad on payloader %" GST_PTR_FORMAT, payloader);
1024 gst_object_unref (payloader);
1028 GST_ERROR_OBJECT (sink,
1029 "Could not find src pad on payloader %" GST_PTR_FORMAT, payloader);
1030 gst_object_unref (GST_OBJECT (sinkpad));
1031 gst_object_unref (payloader);
1036 gst_rtsp_client_sink_sinkpad_event (GstPad * pad, GstObject * parent,
1039 if (GST_EVENT_TYPE (event) == GST_EVENT_CAPS) {
1040 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD (pad));
1041 if (target == NULL) {
1044 /* No target yet - choose a payloader and configure it */
1045 gst_event_parse_caps (event, &caps);
1047 GST_DEBUG_OBJECT (parent,
1048 "Have set caps event on pad %" GST_PTR_FORMAT
1049 " caps %" GST_PTR_FORMAT, pad, caps);
1051 if (!gst_rtsp_client_sink_setup_payloader (GST_RTSP_CLIENT_SINK (parent),
1053 gst_event_unref (event);
1057 gst_object_unref (target);
1061 return gst_pad_event_default (pad, parent, event);
1065 gst_rtsp_client_sink_sinkpad_query (GstPad * pad, GstObject * parent,
1068 if (GST_QUERY_TYPE (query) == GST_QUERY_CAPS) {
1069 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD (pad));
1070 if (target == NULL) {
1071 /* No target yet - return the union of all payloader caps */
1072 GstCaps *caps = gst_rtsp_client_sink_get_payloader_caps ();
1074 GST_TRACE_OBJECT (parent, "Returning payloader caps %" GST_PTR_FORMAT,
1077 gst_query_set_caps_result (query, caps);
1078 gst_caps_unref (caps);
1082 gst_object_unref (target);
1085 return gst_pad_query_default (pad, parent, query);
1089 gst_rtsp_client_sink_request_new_pad (GstElement * element,
1090 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
1092 GstRTSPClientSink *sink = GST_RTSP_CLIENT_SINK (element);
1094 GstRTSPStreamContext *context;
1095 guint idx = (guint) - 1;
1098 g_mutex_lock (&sink->preroll_lock);
1099 if (sink->streams_collected) {
1100 GST_WARNING_OBJECT (element, "Can't add streams to a running session");
1101 g_mutex_unlock (&sink->preroll_lock);
1104 g_mutex_unlock (&sink->preroll_lock);
1106 GST_OBJECT_LOCK (sink);
1108 if (!sscanf (name, "sink_%u", &idx)) {
1109 GST_OBJECT_UNLOCK (sink);
1110 GST_ERROR_OBJECT (element, "Invalid sink pad name %s", name);
1114 if (idx >= sink->next_pad_id)
1115 sink->next_pad_id = idx + 1;
1117 if (idx == (guint) - 1) {
1118 idx = sink->next_pad_id;
1119 sink->next_pad_id++;
1121 GST_OBJECT_UNLOCK (sink);
1123 tmpname = g_strdup_printf ("sink_%u", idx);
1124 pad = gst_ghost_pad_new_no_target_from_template (tmpname, templ);
1127 GST_DEBUG_OBJECT (element, "Creating request pad %" GST_PTR_FORMAT, pad);
1129 gst_pad_set_event_function (pad,
1130 GST_DEBUG_FUNCPTR (gst_rtsp_client_sink_sinkpad_event));
1131 gst_pad_set_query_function (pad,
1132 GST_DEBUG_FUNCPTR (gst_rtsp_client_sink_sinkpad_query));
1134 context = g_new0 (GstRTSPStreamContext, 1);
1135 context->parent = sink;
1136 context->index = idx;
1138 gst_pad_set_element_private (pad, context);
1140 /* The rest of the context is configured on a caps set */
1141 gst_pad_set_active (pad, TRUE);
1142 gst_element_add_pad (element, pad);
1144 (void) gst_rtsp_client_sink_get_factories ();
1146 g_mutex_init (&context->conninfo.send_lock);
1147 g_mutex_init (&context->conninfo.recv_lock);
1149 GST_RTSP_STATE_LOCK (sink);
1150 sink->contexts = g_list_prepend (sink->contexts, context);
1151 GST_RTSP_STATE_UNLOCK (sink);
1157 gst_rtsp_client_sink_release_pad (GstElement * element, GstPad * pad)
1159 GstRTSPClientSink *sink = GST_RTSP_CLIENT_SINK (element);
1160 GstRTSPStreamContext *context;
1162 context = gst_pad_get_element_private (pad);
1164 GST_RTSP_STATE_LOCK (sink);
1165 sink->contexts = g_list_remove (sink->contexts, context);
1166 GST_RTSP_STATE_UNLOCK (sink);
1168 /* FIXME: Shut down and clean up streaming on this pad,
1169 * do teardown if needed */
1170 GST_LOG_OBJECT (sink,
1171 "Cleaning up payloader and stream for released pad %" GST_PTR_FORMAT,
1174 if (context->stream_transport) {
1175 gst_rtsp_stream_transport_set_active (context->stream_transport, FALSE);
1176 gst_object_unref (context->stream_transport);
1177 context->stream_transport = NULL;
1179 if (context->stream) {
1180 if (context->joined) {
1181 gst_rtsp_stream_leave_bin (context->stream,
1182 GST_BIN (sink->internal_bin), sink->rtpbin);
1183 context->joined = FALSE;
1185 gst_object_unref (context->stream);
1186 context->stream = NULL;
1188 if (context->srtcpparams)
1189 gst_caps_unref (context->srtcpparams);
1191 g_free (context->conninfo.location);
1192 context->conninfo.location = NULL;
1194 g_mutex_clear (&context->conninfo.send_lock);
1195 g_mutex_clear (&context->conninfo.recv_lock);
1199 gst_element_remove_pad (element, pad);
1203 gst_rtsp_client_sink_provide_clock (GstElement * element)
1205 GstRTSPClientSink *sink = GST_RTSP_CLIENT_SINK (element);
1208 if ((clock = sink->provided_clock) != NULL)
1209 gst_object_ref (clock);
1214 /* a proxy string of the format [user:passwd@]host[:port] */
1216 gst_rtsp_client_sink_set_proxy (GstRTSPClientSink * rtsp, const gchar * proxy)
1218 gchar *p, *at, *col;
1220 g_free (rtsp->proxy_user);
1221 rtsp->proxy_user = NULL;
1222 g_free (rtsp->proxy_passwd);
1223 rtsp->proxy_passwd = NULL;
1224 g_free (rtsp->proxy_host);
1225 rtsp->proxy_host = NULL;
1226 rtsp->proxy_port = 0;
1228 p = (gchar *) proxy;
1233 /* we allow http:// in front but ignore it */
1234 if (g_str_has_prefix (p, "http://"))
1237 at = strchr (p, '@');
1239 /* look for user:passwd */
1240 col = strchr (proxy, ':');
1241 if (col == NULL || col > at)
1244 rtsp->proxy_user = g_strndup (p, col - p);
1246 rtsp->proxy_passwd = g_strndup (col, at - col);
1251 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
1252 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
1253 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
1254 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
1255 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
1256 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
1257 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
1260 col = strchr (p, ':');
1263 /* everything before the colon is the hostname */
1264 rtsp->proxy_host = g_strndup (p, col - p);
1266 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
1268 rtsp->proxy_host = g_strdup (p);
1269 rtsp->proxy_port = 8080;
1275 gst_rtsp_client_sink_set_tcp_timeout (GstRTSPClientSink * rtsp_client_sink,
1278 rtsp_client_sink->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
1279 rtsp_client_sink->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
1282 rtsp_client_sink->ptcp_timeout = &rtsp_client_sink->tcp_timeout;
1284 rtsp_client_sink->ptcp_timeout = NULL;
1288 gst_rtsp_client_sink_set_property (GObject * object, guint prop_id,
1289 const GValue * value, GParamSpec * pspec)
1291 GstRTSPClientSink *rtsp_client_sink;
1293 rtsp_client_sink = GST_RTSP_CLIENT_SINK (object);
1297 gst_rtsp_client_sink_uri_set_uri (GST_URI_HANDLER (rtsp_client_sink),
1298 g_value_get_string (value), NULL);
1300 case PROP_PROTOCOLS:
1301 rtsp_client_sink->protocols = g_value_get_flags (value);
1304 rtsp_client_sink->profiles = g_value_get_flags (value);
1307 rtsp_client_sink->debug = g_value_get_boolean (value);
1310 rtsp_client_sink->retry = g_value_get_uint (value);
1313 rtsp_client_sink->udp_timeout = g_value_get_uint64 (value);
1315 case PROP_TCP_TIMEOUT:
1316 gst_rtsp_client_sink_set_tcp_timeout (rtsp_client_sink,
1317 g_value_get_uint64 (value));
1320 rtsp_client_sink->latency = g_value_get_uint (value);
1323 rtsp_client_sink->rtx_time = g_value_get_uint (value);
1325 case PROP_DO_RTSP_KEEP_ALIVE:
1326 rtsp_client_sink->do_rtsp_keep_alive = g_value_get_boolean (value);
1329 gst_rtsp_client_sink_set_proxy (rtsp_client_sink,
1330 g_value_get_string (value));
1333 if (rtsp_client_sink->prop_proxy_id)
1334 g_free (rtsp_client_sink->prop_proxy_id);
1335 rtsp_client_sink->prop_proxy_id = g_value_dup_string (value);
1338 if (rtsp_client_sink->prop_proxy_pw)
1339 g_free (rtsp_client_sink->prop_proxy_pw);
1340 rtsp_client_sink->prop_proxy_pw = g_value_dup_string (value);
1342 case PROP_RTP_BLOCKSIZE:
1343 rtsp_client_sink->rtp_blocksize = g_value_get_uint (value);
1346 if (rtsp_client_sink->user_id)
1347 g_free (rtsp_client_sink->user_id);
1348 rtsp_client_sink->user_id = g_value_dup_string (value);
1351 if (rtsp_client_sink->user_pw)
1352 g_free (rtsp_client_sink->user_pw);
1353 rtsp_client_sink->user_pw = g_value_dup_string (value);
1355 case PROP_PORT_RANGE:
1359 str = g_value_get_string (value);
1360 if (!str || !sscanf (str, "%u-%u",
1361 &rtsp_client_sink->client_port_range.min,
1362 &rtsp_client_sink->client_port_range.max)) {
1363 rtsp_client_sink->client_port_range.min = 0;
1364 rtsp_client_sink->client_port_range.max = 0;
1368 case PROP_UDP_BUFFER_SIZE:
1369 rtsp_client_sink->udp_buffer_size = g_value_get_int (value);
1371 case PROP_UDP_RECONNECT:
1372 rtsp_client_sink->udp_reconnect = g_value_get_boolean (value);
1374 case PROP_MULTICAST_IFACE:
1375 g_free (rtsp_client_sink->multi_iface);
1377 if (g_value_get_string (value) == NULL)
1378 rtsp_client_sink->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1380 rtsp_client_sink->multi_iface = g_value_dup_string (value);
1383 rtsp_client_sink->sdes = g_value_dup_boxed (value);
1385 case PROP_TLS_VALIDATION_FLAGS:
1386 rtsp_client_sink->tls_validation_flags = g_value_get_flags (value);
1388 case PROP_TLS_DATABASE:
1389 g_clear_object (&rtsp_client_sink->tls_database);
1390 rtsp_client_sink->tls_database = g_value_dup_object (value);
1392 case PROP_TLS_INTERACTION:
1393 g_clear_object (&rtsp_client_sink->tls_interaction);
1394 rtsp_client_sink->tls_interaction = g_value_dup_object (value);
1396 case PROP_NTP_TIME_SOURCE:
1397 rtsp_client_sink->ntp_time_source = g_value_get_enum (value);
1399 case PROP_USER_AGENT:
1400 g_free (rtsp_client_sink->user_agent);
1401 rtsp_client_sink->user_agent = g_value_dup_string (value);
1404 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1410 gst_rtsp_client_sink_get_property (GObject * object, guint prop_id,
1411 GValue * value, GParamSpec * pspec)
1413 GstRTSPClientSink *rtsp_client_sink;
1415 rtsp_client_sink = GST_RTSP_CLIENT_SINK (object);
1419 g_value_set_string (value, rtsp_client_sink->conninfo.location);
1421 case PROP_PROTOCOLS:
1422 g_value_set_flags (value, rtsp_client_sink->protocols);
1425 g_value_set_flags (value, rtsp_client_sink->profiles);
1428 g_value_set_boolean (value, rtsp_client_sink->debug);
1431 g_value_set_uint (value, rtsp_client_sink->retry);
1434 g_value_set_uint64 (value, rtsp_client_sink->udp_timeout);
1436 case PROP_TCP_TIMEOUT:
1440 timeout = rtsp_client_sink->tcp_timeout.tv_sec * G_USEC_PER_SEC +
1441 rtsp_client_sink->tcp_timeout.tv_usec;
1442 g_value_set_uint64 (value, timeout);
1446 g_value_set_uint (value, rtsp_client_sink->latency);
1449 g_value_set_uint (value, rtsp_client_sink->rtx_time);
1451 case PROP_DO_RTSP_KEEP_ALIVE:
1452 g_value_set_boolean (value, rtsp_client_sink->do_rtsp_keep_alive);
1458 if (rtsp_client_sink->proxy_host) {
1460 g_strdup_printf ("%s:%d", rtsp_client_sink->proxy_host,
1461 rtsp_client_sink->proxy_port);
1465 g_value_take_string (value, str);
1469 g_value_set_string (value, rtsp_client_sink->prop_proxy_id);
1472 g_value_set_string (value, rtsp_client_sink->prop_proxy_pw);
1474 case PROP_RTP_BLOCKSIZE:
1475 g_value_set_uint (value, rtsp_client_sink->rtp_blocksize);
1478 g_value_set_string (value, rtsp_client_sink->user_id);
1481 g_value_set_string (value, rtsp_client_sink->user_pw);
1483 case PROP_PORT_RANGE:
1487 if (rtsp_client_sink->client_port_range.min != 0) {
1488 str = g_strdup_printf ("%u-%u", rtsp_client_sink->client_port_range.min,
1489 rtsp_client_sink->client_port_range.max);
1493 g_value_take_string (value, str);
1496 case PROP_UDP_BUFFER_SIZE:
1497 g_value_set_int (value, rtsp_client_sink->udp_buffer_size);
1499 case PROP_UDP_RECONNECT:
1500 g_value_set_boolean (value, rtsp_client_sink->udp_reconnect);
1502 case PROP_MULTICAST_IFACE:
1503 g_value_set_string (value, rtsp_client_sink->multi_iface);
1506 g_value_set_boxed (value, rtsp_client_sink->sdes);
1508 case PROP_TLS_VALIDATION_FLAGS:
1509 g_value_set_flags (value, rtsp_client_sink->tls_validation_flags);
1511 case PROP_TLS_DATABASE:
1512 g_value_set_object (value, rtsp_client_sink->tls_database);
1514 case PROP_TLS_INTERACTION:
1515 g_value_set_object (value, rtsp_client_sink->tls_interaction);
1517 case PROP_NTP_TIME_SOURCE:
1518 g_value_set_enum (value, rtsp_client_sink->ntp_time_source);
1520 case PROP_USER_AGENT:
1521 g_value_set_string (value, rtsp_client_sink->user_agent);
1524 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1529 static const gchar *
1530 get_aggregate_control (GstRTSPClientSink * sink)
1535 base = sink->control;
1536 else if (sink->content_base)
1537 base = sink->content_base;
1538 else if (sink->conninfo.url_str)
1539 base = sink->conninfo.url_str;
1547 gst_rtsp_client_sink_cleanup (GstRTSPClientSink * sink)
1551 GST_DEBUG_OBJECT (sink, "cleanup");
1553 gst_element_set_state (GST_ELEMENT (sink->internal_bin), GST_STATE_NULL);
1555 /* Clean up any left over stream objects */
1556 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
1557 GstRTSPStreamContext *context = (GstRTSPStreamContext *) (walk->data);
1558 if (context->stream_transport) {
1559 gst_rtsp_stream_transport_set_active (context->stream_transport, FALSE);
1560 gst_object_unref (context->stream_transport);
1561 context->stream_transport = NULL;
1564 if (context->stream) {
1565 if (context->joined) {
1566 gst_rtsp_stream_leave_bin (context->stream,
1567 GST_BIN (sink->internal_bin), sink->rtpbin);
1568 context->joined = FALSE;
1570 gst_object_unref (context->stream);
1571 context->stream = NULL;
1574 if (context->srtcpparams) {
1575 gst_caps_unref (context->srtcpparams);
1576 context->srtcpparams = NULL;
1578 g_free (context->conninfo.location);
1579 context->conninfo.location = NULL;
1583 gst_element_set_state (sink->rtpbin, GST_STATE_NULL);
1584 gst_bin_remove (GST_BIN_CAST (sink->internal_bin), sink->rtpbin);
1585 sink->rtpbin = NULL;
1588 g_free (sink->content_base);
1589 sink->content_base = NULL;
1591 g_free (sink->control);
1592 sink->control = NULL;
1595 gst_rtsp_range_free (sink->range);
1598 /* don't clear the SDP when it was used in the url */
1599 if (sink->uri_sdp && !sink->from_sdp) {
1600 gst_sdp_message_free (sink->uri_sdp);
1601 sink->uri_sdp = NULL;
1604 if (sink->provided_clock) {
1605 gst_object_unref (sink->provided_clock);
1606 sink->provided_clock = NULL;
1609 g_free (sink->server_ip);
1610 sink->server_ip = NULL;
1612 sink->next_pad_id = 0;
1613 sink->next_dyn_pt = 96;
1616 static GstRTSPResult
1617 gst_rtsp_client_sink_connection_send (GstRTSPClientSink * sink,
1618 GstRTSPConnInfo * conninfo, GstRTSPMessage * message, GTimeVal * timeout)
1622 if (conninfo->connection) {
1623 g_mutex_lock (&conninfo->send_lock);
1624 ret = gst_rtsp_connection_send (conninfo->connection, message, timeout);
1625 g_mutex_unlock (&conninfo->send_lock);
1627 ret = GST_RTSP_ERROR;
1633 static GstRTSPResult
1634 gst_rtsp_client_sink_connection_receive (GstRTSPClientSink * sink,
1635 GstRTSPConnInfo * conninfo, GstRTSPMessage * message, GTimeVal * timeout)
1639 if (conninfo->connection) {
1640 g_mutex_lock (&conninfo->recv_lock);
1641 ret = gst_rtsp_connection_receive (conninfo->connection, message, timeout);
1642 g_mutex_unlock (&conninfo->recv_lock);
1644 ret = GST_RTSP_ERROR;
1650 static GstRTSPResult
1651 gst_rtsp_conninfo_connect (GstRTSPClientSink * sink, GstRTSPConnInfo * info,
1656 if (info->connection == NULL) {
1657 if (info->url == NULL) {
1658 GST_DEBUG_OBJECT (sink, "parsing uri (%s)...", info->location);
1659 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
1663 /* create connection */
1664 GST_DEBUG_OBJECT (sink, "creating connection (%s)...", info->location);
1665 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
1666 goto could_not_create;
1669 g_free (info->url_str);
1670 info->url_str = gst_rtsp_url_get_request_uri (info->url);
1672 GST_DEBUG_OBJECT (sink, "sanitized uri %s", info->url_str);
1674 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
1675 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
1676 sink->tls_validation_flags))
1677 GST_WARNING_OBJECT (sink, "Unable to set TLS validation flags");
1679 if (sink->tls_database)
1680 gst_rtsp_connection_set_tls_database (info->connection,
1681 sink->tls_database);
1683 if (sink->tls_interaction)
1684 gst_rtsp_connection_set_tls_interaction (info->connection,
1685 sink->tls_interaction);
1688 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
1689 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
1691 if (sink->proxy_host) {
1692 GST_DEBUG_OBJECT (sink, "setting proxy %s:%d", sink->proxy_host,
1694 gst_rtsp_connection_set_proxy (info->connection, sink->proxy_host,
1699 if (!info->connected) {
1702 GST_ELEMENT_PROGRESS (sink, CONTINUE, "connect",
1703 ("Connecting to %s", info->location));
1704 GST_DEBUG_OBJECT (sink, "connecting (%s)...", info->location);
1706 gst_rtsp_connection_connect (info->connection,
1707 sink->ptcp_timeout)) < 0)
1708 goto could_not_connect;
1710 info->connected = TRUE;
1717 GST_ERROR_OBJECT (sink, "No valid RTSP URL was provided");
1722 gchar *str = gst_rtsp_strresult (res);
1723 GST_ERROR_OBJECT (sink, "Could not create connection. (%s)", str);
1729 gchar *str = gst_rtsp_strresult (res);
1730 GST_ERROR_OBJECT (sink, "Could not connect to server. (%s)", str);
1736 static GstRTSPResult
1737 gst_rtsp_conninfo_close (GstRTSPClientSink * sink, GstRTSPConnInfo * info,
1740 GST_RTSP_STATE_LOCK (sink);
1741 if (info->connected) {
1742 GST_DEBUG_OBJECT (sink, "closing connection...");
1743 gst_rtsp_connection_close (info->connection);
1744 info->connected = FALSE;
1746 if (free && info->connection) {
1747 /* free connection */
1748 GST_DEBUG_OBJECT (sink, "freeing connection...");
1749 gst_rtsp_connection_free (info->connection);
1750 info->connection = NULL;
1752 GST_RTSP_STATE_UNLOCK (sink);
1756 static GstRTSPResult
1757 gst_rtsp_conninfo_reconnect (GstRTSPClientSink * sink, GstRTSPConnInfo * info,
1762 GST_DEBUG_OBJECT (sink, "reconnecting connection...");
1763 gst_rtsp_conninfo_close (sink, info, FALSE);
1764 res = gst_rtsp_conninfo_connect (sink, info, async);
1770 gst_rtsp_client_sink_connection_flush (GstRTSPClientSink * sink, gboolean flush)
1774 GST_DEBUG_OBJECT (sink, "set flushing %d", flush);
1775 g_mutex_lock (&sink->preroll_lock);
1776 if (sink->conninfo.connection && sink->conninfo.flushing != flush) {
1777 GST_DEBUG_OBJECT (sink, "connection flush");
1778 gst_rtsp_connection_flush (sink->conninfo.connection, flush);
1779 sink->conninfo.flushing = flush;
1781 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
1782 GstRTSPStreamContext *stream = (GstRTSPStreamContext *) walk->data;
1783 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
1784 GST_DEBUG_OBJECT (sink, "stream %p flush", stream);
1785 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
1786 stream->conninfo.flushing = flush;
1789 g_cond_broadcast (&sink->preroll_cond);
1790 g_mutex_unlock (&sink->preroll_lock);
1793 static GstRTSPResult
1794 gst_rtsp_client_sink_init_request (GstRTSPClientSink * sink,
1795 GstRTSPMessage * msg, GstRTSPMethod method, const gchar * uri)
1799 res = gst_rtsp_message_init_request (msg, method, uri);
1803 /* set user-agent */
1804 if (sink->user_agent)
1805 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_USER_AGENT,
1811 /* FIXME, handle server request, reply with OK, for now */
1812 static GstRTSPResult
1813 gst_rtsp_client_sink_handle_request (GstRTSPClientSink * sink,
1814 GstRTSPConnInfo * conninfo, GstRTSPMessage * request)
1816 GstRTSPMessage response = { 0 };
1819 GST_DEBUG_OBJECT (sink, "got server request message");
1822 gst_rtsp_message_dump (request);
1824 /* default implementation, send OK */
1825 GST_DEBUG_OBJECT (sink, "prepare OK reply");
1827 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
1832 /* let app parse and reply */
1833 g_signal_emit (sink, gst_rtsp_client_sink_signals[SIGNAL_HANDLE_REQUEST],
1834 0, request, &response);
1837 gst_rtsp_message_dump (&response);
1839 res = gst_rtsp_client_sink_connection_send (sink, conninfo, &response, NULL);
1843 gst_rtsp_message_unset (&response);
1850 gst_rtsp_message_unset (&response);
1855 /* send server keep-alive */
1856 static GstRTSPResult
1857 gst_rtsp_client_sink_send_keep_alive (GstRTSPClientSink * sink)
1859 GstRTSPMessage request = { 0 };
1861 GstRTSPMethod method;
1862 const gchar *control;
1864 if (sink->do_rtsp_keep_alive == FALSE) {
1865 GST_DEBUG_OBJECT (sink, "do-rtsp-keep-alive is FALSE, not sending.");
1866 gst_rtsp_connection_reset_timeout (sink->conninfo.connection);
1870 GST_DEBUG_OBJECT (sink, "creating server keep-alive");
1872 /* find a method to use for keep-alive */
1873 if (sink->methods & GST_RTSP_GET_PARAMETER)
1874 method = GST_RTSP_GET_PARAMETER;
1876 method = GST_RTSP_OPTIONS;
1878 control = get_aggregate_control (sink);
1879 if (control == NULL)
1882 res = gst_rtsp_client_sink_init_request (sink, &request, method, control);
1887 gst_rtsp_message_dump (&request);
1890 gst_rtsp_client_sink_connection_send (sink, &sink->conninfo,
1895 gst_rtsp_connection_reset_timeout (sink->conninfo.connection);
1896 gst_rtsp_message_unset (&request);
1903 GST_WARNING_OBJECT (sink, "no control url to send keepalive");
1908 gchar *str = gst_rtsp_strresult (res);
1910 gst_rtsp_message_unset (&request);
1911 GST_ELEMENT_WARNING (sink, RESOURCE, WRITE, (NULL),
1912 ("Could not send keep-alive. (%s)", str));
1918 static GstFlowReturn
1919 gst_rtsp_client_sink_loop_rx (GstRTSPClientSink * sink)
1922 GstRTSPMessage message = { 0 };
1926 GTimeVal tv_timeout;
1928 /* get the next timeout interval */
1929 gst_rtsp_connection_next_timeout (sink->conninfo.connection, &tv_timeout);
1931 GST_DEBUG_OBJECT (sink, "doing receive with timeout %d seconds",
1932 (gint) tv_timeout.tv_sec);
1934 gst_rtsp_message_unset (&message);
1936 /* we should continue reading the TCP socket because the server might
1937 * send us requests. When the session timeout expires, we need to send a
1938 * keep-alive request to keep the session open. */
1940 gst_rtsp_client_sink_connection_receive (sink,
1941 &sink->conninfo, &message, &tv_timeout);
1945 GST_DEBUG_OBJECT (sink, "we received a server message");
1947 case GST_RTSP_EINTR:
1948 /* we got interrupted, see what we have to do */
1950 case GST_RTSP_ETIMEOUT:
1951 /* send keep-alive, ignore the result, a warning will be posted. */
1952 GST_DEBUG_OBJECT (sink, "timeout, sending keep-alive");
1954 gst_rtsp_client_sink_send_keep_alive (sink)) == GST_RTSP_EINTR)
1958 /* server closed the connection. not very fatal for UDP, reconnect and
1959 * see what happens. */
1960 GST_ELEMENT_WARNING (sink, RESOURCE, READ, (NULL),
1961 ("The server closed the connection."));
1962 if (sink->udp_reconnect) {
1964 gst_rtsp_conninfo_reconnect (sink, &sink->conninfo,
1973 GST_DEBUG_OBJECT (sink, "An ethernet problem occured.");
1975 GST_ELEMENT_WARNING (sink, RESOURCE, READ, (NULL),
1976 ("Unhandled return value %d.", res));
1980 switch (message.type) {
1981 case GST_RTSP_MESSAGE_REQUEST:
1982 /* server sends us a request message, handle it */
1984 gst_rtsp_client_sink_handle_request (sink,
1985 &sink->conninfo, &message);
1986 if (res == GST_RTSP_EEOF)
1989 goto handle_request_failed;
1991 case GST_RTSP_MESSAGE_RESPONSE:
1992 /* we ignore response and data messages */
1993 GST_DEBUG_OBJECT (sink, "ignoring response message");
1995 gst_rtsp_message_dump (&message);
1996 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
1997 GST_DEBUG_OBJECT (sink, "but is Unauthorized response ...");
1998 if (gst_rtsp_client_sink_setup_auth (sink, &message) && !(retry++)) {
1999 GST_DEBUG_OBJECT (sink, "so retrying keep-alive");
2001 gst_rtsp_client_sink_send_keep_alive (sink)) ==
2009 case GST_RTSP_MESSAGE_DATA:
2010 /* we ignore response and data messages */
2011 GST_DEBUG_OBJECT (sink, "ignoring data message");
2014 GST_WARNING_OBJECT (sink, "ignoring unknown message type %d",
2019 g_assert_not_reached ();
2021 /* we get here when the connection got interrupted */
2024 gst_rtsp_message_unset (&message);
2025 GST_DEBUG_OBJECT (sink, "got interrupted");
2026 return GST_FLOW_FLUSHING;
2030 gchar *str = gst_rtsp_strresult (res);
2033 sink->conninfo.connected = FALSE;
2034 if (res != GST_RTSP_EINTR) {
2035 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE, (NULL),
2036 ("Could not connect to server. (%s)", str));
2038 ret = GST_FLOW_ERROR;
2040 ret = GST_FLOW_FLUSHING;
2046 gchar *str = gst_rtsp_strresult (res);
2048 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
2049 ("Could not receive message. (%s)", str));
2051 return GST_FLOW_ERROR;
2053 handle_request_failed:
2055 gchar *str = gst_rtsp_strresult (res);
2058 gst_rtsp_message_unset (&message);
2059 if (res != GST_RTSP_EINTR) {
2060 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
2061 ("Could not handle server message. (%s)", str));
2063 ret = GST_FLOW_ERROR;
2065 ret = GST_FLOW_FLUSHING;
2071 GST_DEBUG_OBJECT (sink, "we got an eof from the server");
2072 GST_ELEMENT_WARNING (sink, RESOURCE, READ, (NULL),
2073 ("The server closed the connection."));
2074 sink->conninfo.connected = FALSE;
2075 gst_rtsp_message_unset (&message);
2076 return GST_FLOW_EOS;
2080 static GstRTSPResult
2081 gst_rtsp_client_sink_reconnect (GstRTSPClientSink * sink, gboolean async)
2083 GstRTSPResult res = GST_RTSP_OK;
2084 gboolean restart = FALSE;
2086 GST_DEBUG_OBJECT (sink, "doing reconnect");
2088 GST_FIXME_OBJECT (sink, "Reconnection is not yet implemented");
2090 /* no need to restart, we're done */
2094 /* we can try only TCP now */
2095 sink->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
2097 /* close and cleanup our state */
2098 if ((res = gst_rtsp_client_sink_close (sink, async, FALSE)) < 0)
2101 /* see if we have TCP left to try. Also don't try TCP when we were configured
2103 if (!(sink->protocols & GST_RTSP_LOWER_TRANS_TCP) || sink->from_sdp)
2106 /* We post a warning message now to inform the user
2107 * that nothing happened. It's most likely a firewall thing. */
2108 GST_ELEMENT_WARNING (sink, RESOURCE, READ, (NULL),
2109 ("Could not receive any UDP packets for %.4f seconds, maybe your "
2110 "firewall is blocking it. Retrying using a TCP connection.",
2111 gst_guint64_to_gdouble (sink->udp_timeout / 1000000.0)));
2113 /* open new connection using tcp */
2114 if (gst_rtsp_client_sink_open (sink, async) < 0)
2117 /* start recording */
2118 if (gst_rtsp_client_sink_record (sink, async) < 0)
2127 sink->cur_protocols = 0;
2128 /* no transport possible, post an error and stop */
2129 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
2130 ("Could not receive any UDP packets for %.4f seconds, maybe your "
2131 "firewall is blocking it. No other protocols to try.",
2132 gst_guint64_to_gdouble (sink->udp_timeout / 1000000.0)));
2133 return GST_RTSP_ERROR;
2137 GST_DEBUG_OBJECT (sink, "open failed");
2142 GST_DEBUG_OBJECT (sink, "play failed");
2148 gst_rtsp_client_sink_loop_start_cmd (GstRTSPClientSink * sink, gint cmd)
2152 GST_ELEMENT_PROGRESS (sink, START, "open", ("Opening Stream"));
2155 GST_ELEMENT_PROGRESS (sink, START, "request", ("Sending RECORD request"));
2158 GST_ELEMENT_PROGRESS (sink, START, "request", ("Sending PAUSE request"));
2161 GST_ELEMENT_PROGRESS (sink, START, "close", ("Closing Stream"));
2169 gst_rtsp_client_sink_loop_complete_cmd (GstRTSPClientSink * sink, gint cmd)
2173 GST_ELEMENT_PROGRESS (sink, COMPLETE, "open", ("Opened Stream"));
2176 GST_ELEMENT_PROGRESS (sink, COMPLETE, "request", ("Sent RECORD request"));
2179 GST_ELEMENT_PROGRESS (sink, COMPLETE, "request", ("Sent PAUSE request"));
2182 GST_ELEMENT_PROGRESS (sink, COMPLETE, "close", ("Closed Stream"));
2190 gst_rtsp_client_sink_loop_cancel_cmd (GstRTSPClientSink * sink, gint cmd)
2194 GST_ELEMENT_PROGRESS (sink, CANCELED, "open", ("Open canceled"));
2197 GST_ELEMENT_PROGRESS (sink, CANCELED, "request", ("RECORD canceled"));
2200 GST_ELEMENT_PROGRESS (sink, CANCELED, "request", ("PAUSE canceled"));
2203 GST_ELEMENT_PROGRESS (sink, CANCELED, "close", ("Close canceled"));
2211 gst_rtsp_client_sink_loop_error_cmd (GstRTSPClientSink * sink, gint cmd)
2215 GST_ELEMENT_PROGRESS (sink, ERROR, "open", ("Open failed"));
2218 GST_ELEMENT_PROGRESS (sink, ERROR, "request", ("RECORD failed"));
2221 GST_ELEMENT_PROGRESS (sink, ERROR, "request", ("PAUSE failed"));
2224 GST_ELEMENT_PROGRESS (sink, ERROR, "close", ("Close failed"));
2232 gst_rtsp_client_sink_loop_end_cmd (GstRTSPClientSink * sink, gint cmd,
2235 if (ret == GST_RTSP_OK)
2236 gst_rtsp_client_sink_loop_complete_cmd (sink, cmd);
2237 else if (ret == GST_RTSP_EINTR)
2238 gst_rtsp_client_sink_loop_cancel_cmd (sink, cmd);
2240 gst_rtsp_client_sink_loop_error_cmd (sink, cmd);
2244 gst_rtsp_client_sink_loop_send_cmd (GstRTSPClientSink * sink, gint cmd,
2248 gboolean flushed = FALSE;
2250 /* start new request */
2251 gst_rtsp_client_sink_loop_start_cmd (sink, cmd);
2253 GST_DEBUG_OBJECT (sink, "sending cmd %s", cmd_to_string (cmd));
2255 GST_OBJECT_LOCK (sink);
2256 old = sink->pending_cmd;
2257 if (old == CMD_RECONNECT) {
2258 GST_DEBUG_OBJECT (sink, "ignore, we were reconnecting");
2259 cmd = CMD_RECONNECT;
2261 if (old != CMD_WAIT) {
2262 sink->pending_cmd = CMD_WAIT;
2263 GST_OBJECT_UNLOCK (sink);
2264 /* cancel previous request */
2265 GST_DEBUG_OBJECT (sink, "cancel previous request %s", cmd_to_string (old));
2266 gst_rtsp_client_sink_loop_cancel_cmd (sink, old);
2267 GST_OBJECT_LOCK (sink);
2269 sink->pending_cmd = cmd;
2270 /* interrupt if allowed */
2271 if (sink->busy_cmd & mask) {
2272 GST_DEBUG_OBJECT (sink, "connection flush busy %s",
2273 cmd_to_string (sink->busy_cmd));
2274 gst_rtsp_client_sink_connection_flush (sink, TRUE);
2277 GST_DEBUG_OBJECT (sink, "not interrupting busy cmd %s",
2278 cmd_to_string (sink->busy_cmd));
2281 gst_task_start (sink->task);
2282 GST_OBJECT_UNLOCK (sink);
2288 gst_rtsp_client_sink_loop (GstRTSPClientSink * sink)
2292 if (!sink->conninfo.connection || !sink->conninfo.connected)
2295 ret = gst_rtsp_client_sink_loop_rx (sink);
2296 if (ret != GST_FLOW_OK)
2304 GST_WARNING_OBJECT (sink, "we are not connected");
2305 ret = GST_FLOW_FLUSHING;
2310 const gchar *reason = gst_flow_get_name (ret);
2312 GST_DEBUG_OBJECT (sink, "pausing task, reason %s", reason);
2313 gst_rtsp_client_sink_loop_send_cmd (sink, CMD_WAIT, CMD_LOOP);
2318 #ifndef GST_DISABLE_GST_DEBUG
2319 static const gchar *
2320 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
2324 while (method != 0) {
2341 /* Parse a WWW-Authenticate Response header and determine the
2342 * available authentication methods
2344 * This code should also cope with the fact that each WWW-Authenticate
2345 * header can contain multiple challenge methods + tokens
2347 * At the moment, for Basic auth, we just do a minimal check and don't
2348 * even parse out the realm */
2350 gst_rtsp_client_sink_parse_auth_hdr (GstRTSPMessage * response,
2351 GstRTSPAuthMethod * methods, GstRTSPConnection * conn, gboolean * stale)
2353 GstRTSPAuthCredential **credentials, **credential;
2355 g_return_if_fail (response != NULL);
2356 g_return_if_fail (methods != NULL);
2357 g_return_if_fail (stale != NULL);
2360 gst_rtsp_message_parse_auth_credentials (response,
2361 GST_RTSP_HDR_WWW_AUTHENTICATE);
2365 credential = credentials;
2366 while (*credential) {
2367 if ((*credential)->scheme == GST_RTSP_AUTH_BASIC) {
2368 *methods |= GST_RTSP_AUTH_BASIC;
2369 } else if ((*credential)->scheme == GST_RTSP_AUTH_DIGEST) {
2370 GstRTSPAuthParam **param = (*credential)->params;
2372 *methods |= GST_RTSP_AUTH_DIGEST;
2374 gst_rtsp_connection_clear_auth_params (conn);
2378 if (strcmp ((*param)->name, "stale") == 0
2379 && g_ascii_strcasecmp ((*param)->value, "TRUE") == 0)
2381 gst_rtsp_connection_set_auth_param (conn, (*param)->name,
2390 gst_rtsp_auth_credentials_free (credentials);
2394 * gst_rtsp_client_sink_setup_auth:
2395 * @src: the rtsp source
2397 * Configure a username and password and auth method on the
2398 * connection object based on a response we received from the
2401 * Currently, this requires that a username and password were supplied
2402 * in the uri. In the future, they may be requested on demand by sending
2403 * a message up the bus.
2405 * Returns: TRUE if authentication information could be set up correctly.
2408 gst_rtsp_client_sink_setup_auth (GstRTSPClientSink * sink,
2409 GstRTSPMessage * response)
2413 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
2414 GstRTSPAuthMethod method;
2415 GstRTSPResult auth_result;
2417 GstRTSPConnection *conn;
2418 gboolean stale = FALSE;
2420 conn = sink->conninfo.connection;
2422 /* Identify the available auth methods and see if any are supported */
2423 gst_rtsp_client_sink_parse_auth_hdr (response, &avail_methods, conn, &stale);
2425 if (avail_methods == GST_RTSP_AUTH_NONE)
2426 goto no_auth_available;
2428 /* For digest auth, if the response indicates that the session
2429 * data are stale, we just update them in the connection object and
2430 * return TRUE to retry the request */
2432 sink->tried_url_auth = FALSE;
2434 url = gst_rtsp_connection_get_url (conn);
2436 /* Do we have username and password available? */
2437 if (url != NULL && !sink->tried_url_auth && url->user != NULL
2438 && url->passwd != NULL) {
2441 sink->tried_url_auth = TRUE;
2442 GST_DEBUG_OBJECT (sink,
2443 "Attempting authentication using credentials from the URL");
2445 user = sink->user_id;
2446 pass = sink->user_pw;
2447 GST_DEBUG_OBJECT (sink,
2448 "Attempting authentication using credentials from the properties");
2451 /* FIXME: If the url didn't contain username and password or we tried them
2452 * already, request a username and passwd from the application via some kind
2453 * of credentials request message */
2455 /* If we don't have a username and passwd at this point, bail out. */
2456 if (user == NULL || pass == NULL)
2459 /* Try to configure for each available authentication method, strongest to
2461 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
2462 /* Check if this method is available on the server */
2463 if ((method & avail_methods) == 0)
2466 /* Pass the credentials to the connection to try on the next request */
2467 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
2468 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
2469 * ignore it and end up retrying later */
2470 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
2471 GST_DEBUG_OBJECT (sink, "Attempting %s authentication",
2472 gst_rtsp_auth_method_to_string (method));
2477 if (method == GST_RTSP_AUTH_NONE)
2478 goto no_auth_available;
2484 /* Output an error indicating that we couldn't connect because there were
2485 * no supported authentication protocols */
2486 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ, (NULL),
2487 ("No supported authentication protocol was found"));
2492 /* We don't fire an error message, we just return FALSE and let the
2493 * normal NOT_AUTHORIZED error be propagated */
2498 static GstRTSPResult
2499 gst_rtsp_client_sink_try_send (GstRTSPClientSink * sink,
2500 GstRTSPConnInfo * conninfo, GstRTSPMessage * request,
2501 GstRTSPMessage * response, GstRTSPStatusCode * code)
2504 GstRTSPStatusCode thecode;
2505 gchar *content_base = NULL;
2509 GST_DEBUG_OBJECT (sink, "sending message");
2512 gst_rtsp_message_dump (request);
2514 g_mutex_lock (&sink->send_lock);
2517 gst_rtsp_client_sink_connection_send (sink, conninfo, request,
2518 sink->ptcp_timeout);
2520 g_mutex_unlock (&sink->send_lock);
2524 gst_rtsp_connection_reset_timeout (conninfo->connection);
2526 /* See if we should handle the response */
2527 if (response == NULL) {
2528 g_mutex_unlock (&sink->send_lock);
2533 gst_rtsp_client_sink_connection_receive (sink, conninfo, response,
2534 sink->ptcp_timeout);
2536 g_mutex_unlock (&sink->send_lock);
2542 gst_rtsp_message_dump (response);
2545 switch (response->type) {
2546 case GST_RTSP_MESSAGE_REQUEST:
2547 res = gst_rtsp_client_sink_handle_request (sink, conninfo, response);
2548 if (res == GST_RTSP_EEOF)
2551 goto handle_request_failed;
2552 g_mutex_lock (&sink->send_lock);
2554 case GST_RTSP_MESSAGE_RESPONSE:
2555 /* ok, a response is good */
2556 GST_DEBUG_OBJECT (sink, "received response message");
2558 case GST_RTSP_MESSAGE_DATA:
2559 /* we ignore data messages */
2560 GST_DEBUG_OBJECT (sink, "ignoring data message");
2561 g_mutex_lock (&sink->send_lock);
2564 GST_WARNING_OBJECT (sink, "ignoring unknown message type %d",
2566 g_mutex_lock (&sink->send_lock);
2570 thecode = response->type_data.response.code;
2572 GST_DEBUG_OBJECT (sink, "got response message %d", thecode);
2574 /* if the caller wanted the result code, we store it. */
2578 /* If the request didn't succeed, bail out before doing any more */
2579 if (thecode != GST_RTSP_STS_OK)
2582 /* store new content base if any */
2583 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
2586 g_free (sink->content_base);
2587 sink->content_base = g_strdup (content_base);
2595 gchar *str = gst_rtsp_strresult (res);
2597 if (res != GST_RTSP_EINTR) {
2598 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
2599 ("Could not send message. (%s)", str));
2601 GST_WARNING_OBJECT (sink, "send interrupted");
2610 GST_WARNING_OBJECT (sink, "server closed connection");
2611 if ((try == 0) && !sink->interleaved && sink->udp_reconnect) {
2613 /* if reconnect succeeds, try again */
2615 gst_rtsp_conninfo_reconnect (sink, &sink->conninfo,
2619 /* only try once after reconnect, then fallthrough and error out */
2622 gchar *str = gst_rtsp_strresult (res);
2624 if (res != GST_RTSP_EINTR) {
2625 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
2626 ("Could not receive message. (%s)", str));
2628 GST_WARNING_OBJECT (sink, "receive interrupted");
2636 handle_request_failed:
2638 /* ERROR was posted */
2639 gst_rtsp_message_unset (response);
2644 GST_DEBUG_OBJECT (sink, "we got an eof from the server");
2645 GST_ELEMENT_WARNING (sink, RESOURCE, READ, (NULL),
2646 ("The server closed the connection."));
2647 gst_rtsp_message_unset (response);
2653 gst_rtsp_client_sink_set_state (GstRTSPClientSink * sink, GstState state)
2655 GST_DEBUG_OBJECT (sink, "Setting internal state to %s",
2656 gst_element_state_get_name (state));
2657 gst_element_set_state (GST_ELEMENT (sink->internal_bin), state);
2661 * gst_rtsp_client_sink_send:
2662 * @src: the rtsp source
2663 * @conn: the connection to send on
2664 * @request: must point to a valid request
2665 * @response: must point to an empty #GstRTSPMessage
2666 * @code: an optional code result
2668 * send @request and retrieve the response in @response. optionally @code can be
2669 * non-NULL in which case it will contain the status code of the response.
2671 * If This function returns #GST_RTSP_OK, @response will contain a valid response
2672 * message that should be cleaned with gst_rtsp_message_unset() after usage.
2674 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
2675 * @response message) if the response code was not 200 (OK).
2677 * If the attempt results in an authentication failure, then this will attempt
2678 * to retrieve authentication credentials via gst_rtsp_client_sink_setup_auth and retry
2681 * Returns: #GST_RTSP_OK if the processing was successful.
2683 static GstRTSPResult
2684 gst_rtsp_client_sink_send (GstRTSPClientSink * sink, GstRTSPConnInfo * conninfo,
2685 GstRTSPMessage * request, GstRTSPMessage * response,
2686 GstRTSPStatusCode * code)
2688 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
2689 GstRTSPResult res = GST_RTSP_ERROR;
2692 GstRTSPMethod method = GST_RTSP_INVALID;
2698 /* make sure we don't loop forever */
2702 /* save method so we can disable it when the server complains */
2703 method = request->type_data.request.method;
2706 gst_rtsp_client_sink_try_send (sink, conninfo, request, response,
2711 case GST_RTSP_STS_UNAUTHORIZED:
2712 if (gst_rtsp_client_sink_setup_auth (sink, response)) {
2713 /* Try the request/response again after configuring the auth info
2721 } while (retry == TRUE);
2723 /* If the user requested the code, let them handle errors, otherwise
2724 * post an error below */
2727 else if (int_code != GST_RTSP_STS_OK)
2728 goto error_response;
2735 GST_DEBUG_OBJECT (sink, "got error %d", res);
2740 res = GST_RTSP_ERROR;
2742 switch (response->type_data.response.code) {
2743 case GST_RTSP_STS_NOT_FOUND:
2744 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND, (NULL), ("%s",
2745 response->type_data.response.reason));
2747 case GST_RTSP_STS_UNAUTHORIZED:
2748 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_AUTHORIZED, (NULL), ("%s",
2749 response->type_data.response.reason));
2751 case GST_RTSP_STS_MOVED_PERMANENTLY:
2752 case GST_RTSP_STS_MOVE_TEMPORARILY:
2754 gchar *new_location;
2755 GstRTSPLowerTrans transports;
2757 GST_DEBUG_OBJECT (sink, "got redirection");
2758 /* if we don't have a Location Header, we must error */
2759 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
2760 &new_location, 0) < 0)
2763 /* When we receive a redirect result, we go back to the INIT state after
2764 * parsing the new URI. The caller should do the needed steps to issue
2765 * a new setup when it detects this state change. */
2766 GST_DEBUG_OBJECT (sink, "redirection to %s", new_location);
2768 /* save current transports */
2769 if (sink->conninfo.url)
2770 transports = sink->conninfo.url->transports;
2772 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
2774 gst_rtsp_client_sink_uri_set_uri (GST_URI_HANDLER (sink), new_location,
2777 /* set old transports */
2778 if (sink->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
2779 sink->conninfo.url->transports = transports;
2781 sink->need_redirect = TRUE;
2782 sink->state = GST_RTSP_STATE_INIT;
2786 case GST_RTSP_STS_NOT_ACCEPTABLE:
2787 case GST_RTSP_STS_NOT_IMPLEMENTED:
2788 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
2789 GST_WARNING_OBJECT (sink, "got NOT IMPLEMENTED, disable method %s",
2790 gst_rtsp_method_as_text (method));
2791 sink->methods &= ~method;
2795 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
2796 ("Got error response: %d (%s).", response->type_data.response.code,
2797 response->type_data.response.reason));
2800 /* if we return ERROR we should unset the response ourselves */
2801 if (res == GST_RTSP_ERROR)
2802 gst_rtsp_message_unset (response);
2808 /* parse the response and collect all the supported methods. We need this
2809 * information so that we don't try to send an unsupported request to the
2813 gst_rtsp_client_sink_parse_methods (GstRTSPClientSink * sink,
2814 GstRTSPMessage * response)
2816 GstRTSPHeaderField field;
2820 /* reset supported methods */
2823 /* Try Allow Header first */
2824 field = GST_RTSP_HDR_ALLOW;
2827 gst_rtsp_message_get_header (response, field, &respoptions, indx);
2828 if (indx == 0 && !respoptions) {
2829 /* if no Allow header was found then try the Public header... */
2830 field = GST_RTSP_HDR_PUBLIC;
2831 gst_rtsp_message_get_header (response, field, &respoptions, indx);
2836 sink->methods |= gst_rtsp_options_from_text (respoptions);
2841 if (sink->methods == 0) {
2842 /* neither Allow nor Public are required, assume the server supports
2843 * at least SETUP. */
2844 GST_DEBUG_OBJECT (sink, "could not get OPTIONS");
2845 sink->methods = GST_RTSP_SETUP;
2848 /* Even if the server replied, and didn't say it supports
2849 * RECORD|ANNOUNCE, try anyway by assuming it does */
2850 sink->methods |= GST_RTSP_ANNOUNCE | GST_RTSP_RECORD;
2852 if (!(sink->methods & GST_RTSP_SETUP))
2860 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ, (NULL),
2861 ("Server does not support SETUP."));
2866 static GstRTSPResult
2867 gst_rtsp_client_sink_connect_to_server (GstRTSPClientSink * sink,
2871 GstRTSPMessage request = { 0 };
2872 GstRTSPMessage response = { 0 };
2873 GSocket *conn_socket;
2877 sink->need_redirect = FALSE;
2879 /* can't continue without a valid url */
2880 if (G_UNLIKELY (sink->conninfo.url == NULL)) {
2881 res = GST_RTSP_EINVAL;
2884 sink->tried_url_auth = FALSE;
2886 if ((res = gst_rtsp_conninfo_connect (sink, &sink->conninfo, async)) < 0)
2887 goto connect_failed;
2889 conn_socket = gst_rtsp_connection_get_read_socket (sink->conninfo.connection);
2890 sa = g_socket_get_remote_address (conn_socket, NULL);
2891 ia = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (sa));
2893 sink->server_ip = g_inet_address_to_string (ia);
2895 g_object_unref (sa);
2897 /* create OPTIONS */
2898 GST_DEBUG_OBJECT (sink, "create options...");
2900 gst_rtsp_client_sink_init_request (sink, &request, GST_RTSP_OPTIONS,
2901 sink->conninfo.url_str);
2903 goto create_request_failed;
2906 GST_DEBUG_OBJECT (sink, "send options...");
2909 GST_ELEMENT_PROGRESS (sink, CONTINUE, "open",
2910 ("Retrieving server options"));
2913 gst_rtsp_client_sink_send (sink, &sink->conninfo, &request,
2914 &response, NULL)) < 0)
2918 if (!gst_rtsp_client_sink_parse_methods (sink, &response))
2921 /* FIXME: Do we need to handle REDIRECT responses for OPTIONS? */
2923 /* clean up any messages */
2924 gst_rtsp_message_unset (&request);
2925 gst_rtsp_message_unset (&response);
2932 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND, (NULL),
2933 ("No valid RTSP URL was provided"));
2938 gchar *str = gst_rtsp_strresult (res);
2940 if (res != GST_RTSP_EINTR) {
2941 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE, (NULL),
2942 ("Failed to connect. (%s)", str));
2944 GST_WARNING_OBJECT (sink, "connect interrupted");
2949 create_request_failed:
2951 gchar *str = gst_rtsp_strresult (res);
2953 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
2954 ("Could not create request. (%s)", str));
2960 /* Don't post a message - the rtsp_send method will have
2961 * taken care of it because we passed NULL for the response code */
2966 /* error was posted */
2967 res = GST_RTSP_ERROR;
2972 if (sink->conninfo.connection) {
2973 GST_DEBUG_OBJECT (sink, "free connection");
2974 gst_rtsp_conninfo_close (sink, &sink->conninfo, TRUE);
2976 gst_rtsp_message_unset (&request);
2977 gst_rtsp_message_unset (&response);
2982 static GstRTSPResult
2983 gst_rtsp_client_sink_open (GstRTSPClientSink * sink, gboolean async)
2988 GST_RTSP_SETUP | GST_RTSP_RECORD | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
2990 if ((ret = gst_rtsp_client_sink_connect_to_server (sink, async)) < 0)
2994 gst_rtsp_client_sink_loop_end_cmd (sink, CMD_OPEN, ret);
2996 /* Collect all our input streams and create
2997 * stream objects before actually returning */
2998 gst_rtsp_client_sink_collect_streams (sink);
3005 GST_WARNING_OBJECT (sink, "Failed to connect to server");
3006 sink->open_error = TRUE;
3008 gst_rtsp_client_sink_loop_end_cmd (sink, CMD_OPEN, ret);
3013 static GstRTSPResult
3014 gst_rtsp_client_sink_close (GstRTSPClientSink * sink, gboolean async,
3015 gboolean only_close)
3017 GstRTSPMessage request = { 0 };
3018 GstRTSPMessage response = { 0 };
3019 GstRTSPResult res = GST_RTSP_OK;
3021 const gchar *control;
3023 GST_DEBUG_OBJECT (sink, "TEARDOWN...");
3025 gst_rtsp_client_sink_set_state (sink, GST_STATE_NULL);
3027 if (sink->state < GST_RTSP_STATE_READY) {
3028 GST_DEBUG_OBJECT (sink, "not ready, doing cleanup");
3035 /* construct a control url */
3036 control = get_aggregate_control (sink);
3038 if (!(sink->methods & (GST_RTSP_RECORD | GST_RTSP_TEARDOWN)))
3041 /* stop streaming */
3042 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3043 GstRTSPStreamContext *context = (GstRTSPStreamContext *) walk->data;
3045 if (context->stream_transport)
3046 gst_rtsp_stream_transport_set_active (context->stream_transport, FALSE);
3048 if (context->joined) {
3049 gst_rtsp_stream_leave_bin (context->stream, GST_BIN (sink->internal_bin),
3051 context->joined = FALSE;
3055 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3056 GstRTSPStreamContext *context = (GstRTSPStreamContext *) walk->data;
3057 const gchar *setup_url;
3058 GstRTSPConnInfo *info;
3060 GST_DEBUG_OBJECT (sink, "Looking at stream %p for teardown",
3063 /* try aggregate control first but do non-aggregate control otherwise */
3065 setup_url = control;
3066 else if ((setup_url = context->conninfo.location) == NULL) {
3067 GST_DEBUG_OBJECT (sink, "Skipping TEARDOWN stream %p - no setup URL",
3072 if (sink->conninfo.connection) {
3073 info = &sink->conninfo;
3074 } else if (context->conninfo.connection) {
3075 info = &context->conninfo;
3079 if (!info->connected)
3083 GST_DEBUG_OBJECT (sink, "Sending teardown for stream %p at URL %s",
3084 context->stream, setup_url);
3086 gst_rtsp_client_sink_init_request (sink, &request, GST_RTSP_TEARDOWN,
3089 goto create_request_failed;
3092 GST_ELEMENT_PROGRESS (sink, CONTINUE, "close", ("Closing stream"));
3095 gst_rtsp_client_sink_send (sink, info, &request,
3096 &response, NULL)) < 0)
3099 /* FIXME, parse result? */
3100 gst_rtsp_message_unset (&request);
3101 gst_rtsp_message_unset (&response);
3104 /* early exit when we did aggregate control */
3110 /* close connections */
3111 GST_DEBUG_OBJECT (sink, "closing connection...");
3112 gst_rtsp_conninfo_close (sink, &sink->conninfo, TRUE);
3113 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3114 GstRTSPStreamContext *stream = (GstRTSPStreamContext *) walk->data;
3115 gst_rtsp_conninfo_close (sink, &stream->conninfo, TRUE);
3119 gst_rtsp_client_sink_cleanup (sink);
3121 sink->state = GST_RTSP_STATE_INVALID;
3124 gst_rtsp_client_sink_loop_end_cmd (sink, CMD_CLOSE, res);
3129 create_request_failed:
3131 gchar *str = gst_rtsp_strresult (res);
3133 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
3134 ("Could not create request. (%s)", str));
3140 gchar *str = gst_rtsp_strresult (res);
3142 gst_rtsp_message_unset (&request);
3143 if (res != GST_RTSP_EINTR) {
3144 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
3145 ("Could not send message. (%s)", str));
3147 GST_WARNING_OBJECT (sink, "TEARDOWN interrupted");
3154 GST_DEBUG_OBJECT (sink,
3155 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
3161 gst_rtsp_client_sink_configure_manager (GstRTSPClientSink * sink)
3164 GstStateChangeReturn ret;
3166 rtpbin = sink->rtpbin;
3168 if (rtpbin == NULL) {
3169 GObjectClass *klass;
3171 rtpbin = gst_element_factory_make ("rtpbin", NULL);
3175 gst_bin_add (GST_BIN_CAST (sink->internal_bin), rtpbin);
3177 sink->rtpbin = rtpbin;
3179 /* Any more settings we should configure on rtpbin here? */
3180 g_object_set (sink->rtpbin, "latency", sink->latency, NULL);
3182 klass = G_OBJECT_GET_CLASS (G_OBJECT (rtpbin));
3184 if (g_object_class_find_property (klass, "ntp-time-source")) {
3185 g_object_set (sink->rtpbin, "ntp-time-source", sink->ntp_time_source,
3189 if (sink->sdes && g_object_class_find_property (klass, "sdes")) {
3190 g_object_set (sink->rtpbin, "sdes", sink->sdes, NULL);
3193 g_signal_emit (sink, gst_rtsp_client_sink_signals[SIGNAL_NEW_MANAGER], 0,
3197 ret = gst_element_set_state (rtpbin, GST_STATE_PAUSED);
3198 if (ret == GST_STATE_CHANGE_FAILURE)
3199 goto start_manager_failure;
3205 GST_WARNING ("no rtpbin element");
3206 g_warning ("failed to create element 'rtpbin', check your installation");
3209 start_manager_failure:
3211 GST_DEBUG_OBJECT (sink, "could not start session manager");
3212 gst_bin_remove (GST_BIN_CAST (sink->internal_bin), rtpbin);
3218 request_aux_sender (GstElement * rtpbin, guint sessid, GstRTSPClientSink * sink)
3220 GstRTSPStream *stream = NULL;
3221 GstElement *ret = NULL;
3224 GST_RTSP_STATE_LOCK (sink);
3225 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3226 GstRTSPStreamContext *context = (GstRTSPStreamContext *) walk->data;
3228 if (sessid == gst_rtsp_stream_get_index (context->stream)) {
3229 stream = context->stream;
3234 if (stream != NULL) {
3235 GST_DEBUG_OBJECT (sink, "Creating aux sender for stream %u", sessid);
3236 ret = gst_rtsp_stream_request_aux_sender (stream, sessid);
3239 GST_RTSP_STATE_UNLOCK (sink);
3245 gst_rtsp_client_sink_collect_streams (GstRTSPClientSink * sink)
3247 GstRTSPStreamContext *context;
3252 GST_DEBUG_OBJECT (sink, "Collecting stream information");
3254 if (!gst_rtsp_client_sink_configure_manager (sink))
3257 base = get_aggregate_control (sink);
3258 /* check if the base ends with / */
3259 has_slash = g_str_has_suffix (base, "/");
3261 g_mutex_lock (&sink->preroll_lock);
3262 while (sink->contexts == NULL && !sink->conninfo.flushing) {
3263 g_cond_wait (&sink->preroll_cond, &sink->preroll_lock);
3265 g_mutex_unlock (&sink->preroll_lock);
3267 /* FIXME: Need different locking - need to protect against pad releases
3268 * and potential state changes ruining things here */
3269 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3272 context = (GstRTSPStreamContext *) walk->data;
3273 if (context->stream)
3276 g_mutex_lock (&sink->preroll_lock);
3277 while (!context->prerolled && !sink->conninfo.flushing) {
3278 GST_DEBUG_OBJECT (sink, "Waiting for caps on stream %d", context->index);
3279 g_cond_wait (&sink->preroll_cond, &sink->preroll_lock);
3281 if (sink->conninfo.flushing) {
3282 g_mutex_unlock (&sink->preroll_lock);
3285 g_mutex_unlock (&sink->preroll_lock);
3287 if (context->payloader == NULL)
3290 srcpad = gst_element_get_static_pad (context->payloader, "src");
3292 GST_DEBUG_OBJECT (sink, "Creating stream object for stream %d",
3295 gst_rtsp_client_sink_create_stream (sink, context, context->payloader,
3298 /* concatenate the two strings, insert / when not present */
3299 g_free (context->conninfo.location);
3300 context->conninfo.location =
3301 g_strdup_printf ("%s%sstream=%d", base, has_slash ? "" : "/",
3304 if (sink->rtx_time > 0) {
3305 /* enable retransmission by setting rtprtxsend as the "aux" element of rtpbin */
3306 g_signal_connect (sink->rtpbin, "request-aux-sender",
3307 (GCallback) request_aux_sender, sink);
3310 if (!gst_rtsp_stream_join_bin (context->stream,
3311 GST_BIN (sink->internal_bin), sink->rtpbin, GST_STATE_PAUSED)) {
3312 goto join_bin_failed;
3314 context->joined = TRUE;
3316 /* Let the stream object receive data */
3317 gst_pad_remove_probe (srcpad, context->payloader_block_id);
3319 gst_object_unref (srcpad);
3322 /* Now wait for the preroll of the rtp bin */
3323 g_mutex_lock (&sink->preroll_lock);
3324 while (!sink->prerolled && !sink->conninfo.flushing) {
3325 GST_LOG_OBJECT (sink, "Waiting for preroll before continuing");
3326 g_cond_wait (&sink->preroll_cond, &sink->preroll_lock);
3328 GST_LOG_OBJECT (sink, "Marking streams as collected");
3329 sink->streams_collected = TRUE;
3330 g_mutex_unlock (&sink->preroll_lock);
3336 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
3337 ("Could not start stream %d", context->index));
3341 static GstRTSPResult
3342 gst_rtsp_client_sink_create_transports_string (GstRTSPClientSink * sink,
3343 GstRTSPStreamContext * context, GSocketFamily family,
3344 GstRTSPLowerTrans protocols, GstRTSPProfile profiles, gchar ** transports)
3347 GstRTSPStream *stream = context->stream;
3348 gboolean first = TRUE;
3350 /* the default RTSP transports */
3351 result = g_string_new ("RTP");
3353 while (profiles != 0) {
3355 g_string_append (result, ",RTP");
3357 if (profiles & GST_RTSP_PROFILE_SAVPF) {
3358 g_string_append (result, "/SAVPF");
3359 profiles &= ~GST_RTSP_PROFILE_SAVPF;
3360 } else if (profiles & GST_RTSP_PROFILE_SAVP) {
3361 g_string_append (result, "/SAVP");
3362 profiles &= ~GST_RTSP_PROFILE_SAVP;
3363 } else if (profiles & GST_RTSP_PROFILE_AVPF) {
3364 g_string_append (result, "/AVPF");
3365 profiles &= ~GST_RTSP_PROFILE_AVPF;
3366 } else if (profiles & GST_RTSP_PROFILE_AVP) {
3367 g_string_append (result, "/AVP");
3368 profiles &= ~GST_RTSP_PROFILE_AVP;
3370 GST_WARNING_OBJECT (sink, "Unimplemented profile(s) 0x%x", profiles);
3374 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
3377 GST_DEBUG_OBJECT (sink, "adding UDP unicast");
3378 gst_rtsp_stream_get_server_port (stream, &ports, family);
3380 g_string_append_printf (result, "/UDP;unicast;client_port=%d-%d",
3381 ports.min, ports.max);
3382 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3383 GstRTSPAddress *addr =
3384 gst_rtsp_stream_get_multicast_address (stream, family);
3386 GST_DEBUG_OBJECT (sink, "adding UDP multicast");
3387 g_string_append_printf (result, "/UDP;multicast;client_port=%d-%d",
3388 addr->port, addr->port + addr->n_ports - 1);
3389 gst_rtsp_address_free (addr);
3391 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
3392 GST_DEBUG_OBJECT (sink, "adding TCP");
3393 g_string_append_printf (result, "/TCP;unicast;interleaved=%d-%d",
3394 sink->free_channel, sink->free_channel + 1);
3397 g_string_append (result, ";mode=RECORD");
3398 /* FIXME: Support appending too:
3400 g_string_append (result, ";append");
3407 /* No valid transport could be constructed */
3408 GST_ERROR_OBJECT (sink, "No supported profiles configured");
3412 *transports = g_string_free (result, FALSE);
3414 GST_DEBUG_OBJECT (sink, "prepared transports %s", GST_STR_NULL (*transports));
3418 g_string_free (result, TRUE);
3419 return GST_RTSP_ERROR;
3423 signal_get_srtcp_params (GstRTSPClientSink * sink,
3424 GstRTSPStreamContext * context)
3426 GstCaps *caps = NULL;
3428 g_signal_emit (sink, gst_rtsp_client_sink_signals[SIGNAL_REQUEST_RTCP_KEY], 0,
3429 context->index, &caps);
3432 GST_DEBUG_OBJECT (sink, "SRTP parameters received");
3438 gst_rtsp_client_sink_stream_make_keymgmt (GstRTSPClientSink * sink,
3439 GstRTSPStreamContext * context)
3441 gchar *base64, *result = NULL;
3442 GstMIKEYMessage *mikey_msg;
3444 context->srtcpparams = signal_get_srtcp_params (sink, context);
3445 if (context->srtcpparams == NULL)
3446 context->srtcpparams = gst_rtsp_stream_get_caps (context->stream);
3448 mikey_msg = gst_mikey_message_new_from_caps (context->srtcpparams);
3452 /* add policy '0' for our SSRC */
3453 gst_rtsp_stream_get_ssrc (context->stream, &send_ssrc);
3454 GST_LOG_OBJECT (sink, "Stream %p ssrc %x", context->stream, send_ssrc);
3455 gst_mikey_message_add_cs_srtp (mikey_msg, 0, send_ssrc, 0);
3457 base64 = gst_mikey_message_base64_encode (mikey_msg);
3458 gst_mikey_message_unref (mikey_msg);
3461 result = gst_sdp_make_keymgmt (context->conninfo.location, base64);
3469 /* masks to be kept in sync with the hardcoded protocol order of preference
3471 static const guint protocol_masks[] = {
3472 GST_RTSP_LOWER_TRANS_UDP,
3473 GST_RTSP_LOWER_TRANS_UDP_MCAST,
3474 GST_RTSP_LOWER_TRANS_TCP,
3478 /* Same for profile_masks */
3479 static const guint profile_masks[] = {
3480 GST_RTSP_PROFILE_SAVPF,
3481 GST_RTSP_PROFILE_SAVP,
3482 GST_RTSP_PROFILE_AVPF,
3483 GST_RTSP_PROFILE_AVP,
3488 do_send_data (GstBuffer * buffer, guint8 channel,
3489 GstRTSPStreamContext * context)
3491 GstRTSPClientSink *sink = context->parent;
3492 GstRTSPMessage message = { 0 };
3493 GstRTSPResult res = GST_RTSP_OK;
3494 GstMapInfo map_info;
3498 gst_rtsp_message_init_data (&message, channel);
3500 /* FIXME, need some sort of iovec RTSPMessage here */
3501 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
3504 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
3507 gst_rtsp_client_sink_try_send (sink, &sink->conninfo, &message,
3510 gst_rtsp_message_steal_body (&message, &data, &usize);
3511 gst_buffer_unmap (buffer, &map_info);
3513 gst_rtsp_message_unset (&message);
3515 return res == GST_RTSP_OK;
3518 static GstRTSPResult
3519 gst_rtsp_client_sink_setup_streams (GstRTSPClientSink * sink, gboolean async)
3521 GstRTSPResult res = GST_RTSP_ERROR;
3522 GstRTSPMessage request = { 0 };
3523 GstRTSPMessage response = { 0 };
3524 GstRTSPLowerTrans protocols;
3525 GstRTSPStatusCode code;
3526 GSocketFamily family;
3528 GSocket *conn_socket;
3533 if (sink->conninfo.connection) {
3534 url = gst_rtsp_connection_get_url (sink->conninfo.connection);
3535 /* we initially allow all configured lower transports. based on the URL
3536 * transports and the replies from the server we narrow them down. */
3537 protocols = url->transports & sink->cur_protocols;
3540 protocols = sink->cur_protocols;
3546 GST_RTSP_STATE_LOCK (sink);
3548 if (G_UNLIKELY (sink->contexts == NULL))
3551 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3552 GstRTSPStreamContext *context = (GstRTSPStreamContext *) walk->data;
3553 GstRTSPStream *stream;
3555 GstRTSPConnInfo *info;
3556 GstRTSPProfile profiles;
3557 GstRTSPProfile cur_profile;
3560 guint profile_mask = 0;
3563 const GstSDPMedia *media;
3565 stream = context->stream;
3566 profiles = gst_rtsp_stream_get_profiles (stream);
3568 caps = gst_rtsp_stream_get_caps (stream);
3570 GST_DEBUG_OBJECT (sink, "skipping stream %p, no caps", stream);
3573 gst_caps_unref (caps);
3574 media = gst_sdp_message_get_media (&sink->cursdp, context->sdp_index);
3575 if (media == NULL) {
3576 GST_DEBUG_OBJECT (sink, "skipping stream %p, no SDP info", stream);
3580 /* skip setup if we have no URL for it */
3581 if (context->conninfo.location == NULL) {
3582 GST_DEBUG_OBJECT (sink, "skipping stream %p, no setup", stream);
3586 if (sink->conninfo.connection == NULL) {
3587 if (!gst_rtsp_conninfo_connect (sink, &context->conninfo, async)) {
3588 GST_DEBUG_OBJECT (sink, "skipping stream %p, failed to connect",
3592 info = &context->conninfo;
3594 info = &sink->conninfo;
3596 GST_DEBUG_OBJECT (sink, "doing setup of stream %p with %s", stream,
3597 context->conninfo.location);
3599 conn_socket = gst_rtsp_connection_get_read_socket (info->connection);
3600 sa = g_socket_get_local_address (conn_socket, NULL);
3601 family = g_socket_address_get_family (sa);
3602 g_object_unref (sa);
3605 /* first selectable profile */
3606 while (profile_masks[profile_mask]
3607 && !(profiles & profile_masks[profile_mask]))
3609 if (!profile_masks[profile_mask])
3612 /* first selectable protocol */
3613 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
3615 if (!protocol_masks[mask])
3619 GST_DEBUG_OBJECT (sink, "protocols = 0x%x, protocol mask = 0x%x", protocols,
3620 protocol_masks[mask]);
3621 /* create a string with first transport in line */
3623 cur_profile = profiles & profile_masks[profile_mask];
3624 res = gst_rtsp_client_sink_create_transports_string (sink, context, family,
3625 protocols & protocol_masks[mask], cur_profile, &transports);
3626 if (res < 0 || transports == NULL)
3627 goto setup_transport_failed;
3629 if (strlen (transports) == 0) {
3630 g_free (transports);
3631 GST_DEBUG_OBJECT (sink, "no transports found");
3637 GST_DEBUG_OBJECT (sink, "transport is %s", GST_STR_NULL (transports));
3639 /* create SETUP request */
3641 gst_rtsp_client_sink_init_request (sink, &request, GST_RTSP_SETUP,
3642 context->conninfo.location);
3644 g_free (transports);
3645 goto create_request_failed;
3648 /* select transport */
3649 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
3652 if (cur_profile == GST_RTSP_PROFILE_SAVP ||
3653 cur_profile == GST_RTSP_PROFILE_SAVPF) {
3654 hval = gst_rtsp_client_sink_stream_make_keymgmt (sink, context);
3655 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
3658 /* if the user wants a non default RTP packet size we add the blocksize
3660 if (sink->rtp_blocksize > 0) {
3661 hval = g_strdup_printf ("%d", sink->rtp_blocksize);
3662 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
3666 GST_ELEMENT_PROGRESS (sink, CONTINUE, "request", ("SETUP stream %d",
3669 /* handle the code ourselves */
3670 res = gst_rtsp_client_sink_send (sink, info, &request, &response, &code);
3675 case GST_RTSP_STS_OK:
3677 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
3678 gst_rtsp_message_unset (&request);
3679 gst_rtsp_message_unset (&response);
3681 /* Try another profile. If no more, move to the next protocol */
3683 while (profile_masks[profile_mask]
3684 && !(profiles & profile_masks[profile_mask]))
3686 if (profile_masks[profile_mask])
3689 /* select next available protocol, give up on this stream if none */
3690 /* Reset profiles to try: */
3694 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
3696 if (!protocol_masks[mask])
3701 goto response_error;
3704 /* parse response transport */
3706 gchar *resptrans = NULL;
3707 GstRTSPTransport *transport;
3709 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
3715 gst_rtsp_transport_new (&transport);
3717 /* parse transport, go to next stream on parse error */
3718 if (gst_rtsp_transport_parse (resptrans, transport) != GST_RTSP_OK) {
3719 GST_WARNING_OBJECT (sink, "failed to parse transport %s", resptrans);
3723 /* update allowed transports for other streams. once the transport of
3724 * one stream has been determined, we make sure that all other streams
3725 * are configured in the same way */
3726 switch (transport->lower_transport) {
3727 case GST_RTSP_LOWER_TRANS_TCP:
3728 GST_DEBUG_OBJECT (sink, "stream %p as TCP interleaved", stream);
3729 protocols = GST_RTSP_LOWER_TRANS_TCP;
3730 sink->interleaved = TRUE;
3731 /* update free channels */
3732 sink->free_channel =
3733 MAX (transport->interleaved.min, sink->free_channel);
3734 sink->free_channel =
3735 MAX (transport->interleaved.max, sink->free_channel);
3736 sink->free_channel++;
3738 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3739 /* only allow multicast for other streams */
3740 GST_DEBUG_OBJECT (sink, "stream %p as UDP multicast", stream);
3741 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
3743 case GST_RTSP_LOWER_TRANS_UDP:
3744 /* only allow unicast for other streams */
3745 GST_DEBUG_OBJECT (sink, "stream %p as UDP unicast", stream);
3746 protocols = GST_RTSP_LOWER_TRANS_UDP;
3747 /* Update transport with server destination if not provided by the server */
3748 if (transport->destination == NULL) {
3749 transport->destination = g_strdup (sink->server_ip);
3753 GST_DEBUG_OBJECT (sink, "stream %p unknown transport %d", stream,
3754 transport->lower_transport);
3759 GST_DEBUG ("Configuring the stream transport for stream %d",
3761 if (context->stream_transport == NULL)
3762 context->stream_transport =
3763 gst_rtsp_stream_transport_new (stream, transport);
3765 gst_rtsp_stream_transport_set_transport (context->stream_transport,
3768 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
3769 /* our callbacks to send data on this TCP connection */
3770 gst_rtsp_stream_transport_set_callbacks (context->stream_transport,
3771 (GstRTSPSendFunc) do_send_data,
3772 (GstRTSPSendFunc) do_send_data, context, NULL);
3775 /* The stream_transport now owns the transport */
3778 gst_rtsp_stream_transport_set_active (context->stream_transport, TRUE);
3782 gst_rtsp_transport_free (transport);
3783 /* clean up used RTSP messages */
3784 gst_rtsp_message_unset (&request);
3785 gst_rtsp_message_unset (&response);
3788 GST_RTSP_STATE_UNLOCK (sink);
3790 /* store the transport protocol that was configured */
3791 sink->cur_protocols = protocols;
3797 GST_RTSP_STATE_UNLOCK (sink);
3798 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
3799 ("SDP contains no streams"));
3800 return GST_RTSP_ERROR;
3802 setup_transport_failed:
3804 GST_RTSP_STATE_UNLOCK (sink);
3805 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
3806 ("Could not setup transport."));
3807 res = GST_RTSP_ERROR;
3812 GST_RTSP_STATE_UNLOCK (sink);
3813 /* no transport possible, post an error and stop */
3814 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
3815 ("Could not connect to server, no profiles left"));
3816 return GST_RTSP_ERROR;
3820 GST_RTSP_STATE_UNLOCK (sink);
3821 /* no transport possible, post an error and stop */
3822 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
3823 ("Could not connect to server, no protocols left"));
3824 return GST_RTSP_ERROR;
3828 GST_RTSP_STATE_UNLOCK (sink);
3829 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
3830 ("Server did not select transport."));
3831 res = GST_RTSP_ERROR;
3834 create_request_failed:
3836 gchar *str = gst_rtsp_strresult (res);
3838 GST_RTSP_STATE_UNLOCK (sink);
3839 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
3840 ("Could not create request. (%s)", str));
3846 gchar *str = gst_rtsp_strresult (res);
3848 GST_RTSP_STATE_UNLOCK (sink);
3849 if (res != GST_RTSP_EINTR) {
3850 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
3851 ("Could not send message. (%s)", str));
3853 GST_WARNING_OBJECT (sink, "send interrupted");
3860 const gchar *str = gst_rtsp_status_as_text (code);
3862 GST_RTSP_STATE_UNLOCK (sink);
3863 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
3864 ("Error (%d): %s", code, GST_STR_NULL (str)));
3865 res = GST_RTSP_ERROR;
3870 gst_rtsp_message_unset (&request);
3871 gst_rtsp_message_unset (&response);
3876 static GstRTSPResult
3877 gst_rtsp_client_sink_ensure_open (GstRTSPClientSink * sink, gboolean async)
3879 GstRTSPResult res = GST_RTSP_OK;
3881 if (sink->state < GST_RTSP_STATE_READY) {
3882 res = GST_RTSP_ERROR;
3883 if (sink->open_error) {
3884 GST_DEBUG_OBJECT (sink, "the stream was in error");
3888 gst_rtsp_client_sink_loop_start_cmd (sink, CMD_OPEN);
3890 if ((res = gst_rtsp_client_sink_open (sink, async)) < 0) {
3891 GST_DEBUG_OBJECT (sink, "failed to open stream");
3900 static GstRTSPResult
3901 gst_rtsp_client_sink_record (GstRTSPClientSink * sink, gboolean async)
3903 GstRTSPMessage request = { 0 };
3904 GstRTSPMessage response = { 0 };
3905 GstRTSPResult res = GST_RTSP_OK;
3907 guint sdp_index = 0;
3908 GstSDPInfo info = { 0, };
3911 gchar *sess_id, *client_ip, *str;
3914 GSocket *conn_socket;
3917 /* Wait for streams to preroll */
3918 g_mutex_lock (&sink->preroll_lock);
3919 while (sink->in_async) {
3920 GST_LOG_OBJECT (sink, "Waiting for ASYNC_DONE preroll");
3921 g_cond_wait (&sink->preroll_cond, &sink->preroll_lock);
3923 g_mutex_unlock (&sink->preroll_lock);
3925 if (sink->state == GST_RTSP_STATE_PLAYING) {
3926 /* Already recording, don't send another request */
3927 GST_LOG_OBJECT (sink, "Already in RECORD. Skipping duplicate request.");
3931 /* Send announce, then setup for all streams */
3932 gst_sdp_message_init (&sink->cursdp);
3933 sdp = &sink->cursdp;
3935 /* some standard things first */
3936 gst_sdp_message_set_version (sdp, "0");
3938 /* session ID doesn't have to be super-unique in this case */
3939 sess_id = g_strdup_printf ("%u", g_random_int ());
3941 if (sink->conninfo.connection == NULL)
3942 return GST_RTSP_ERROR;
3944 conn_socket = gst_rtsp_connection_get_read_socket (sink->conninfo.connection);
3946 sa = g_socket_get_local_address (conn_socket, NULL);
3947 ia = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (sa));
3948 client_ip = g_inet_address_to_string (ia);
3949 if (g_socket_address_get_family (sa) == G_SOCKET_FAMILY_IPV6) {
3950 info.is_ipv6 = TRUE;
3952 } else if (g_socket_address_get_family (sa) == G_SOCKET_FAMILY_IPV4)
3955 g_assert_not_reached ();
3956 g_object_unref (sa);
3958 /* FIXME: Should this actually be the server's IP or ours? */
3959 info.server_ip = sink->server_ip;
3961 gst_sdp_message_set_origin (sdp, "-", sess_id, "1", "IN", proto, client_ip);
3963 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
3964 gst_sdp_message_set_information (sdp, "rtspclientsink");
3965 gst_sdp_message_add_time (sdp, "0", "0", NULL);
3966 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
3969 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3970 GstRTSPStreamContext *context = (GstRTSPStreamContext *) walk->data;
3972 gst_rtsp_sdp_from_stream (sdp, &info, context->stream);
3973 context->sdp_index = sdp_index++;
3979 /* send ANNOUNCE request */
3980 GST_DEBUG_OBJECT (sink, "create ANNOUNCE request...");
3982 gst_rtsp_client_sink_init_request (sink, &request, GST_RTSP_ANNOUNCE,
3983 sink->conninfo.url_str);
3985 goto create_request_failed;
3987 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CONTENT_TYPE,
3990 /* add SDP to the request body */
3991 str = gst_sdp_message_as_text (sdp);
3992 gst_rtsp_message_take_body (&request, (guint8 *) str, strlen (str));
3995 GST_DEBUG_OBJECT (sink, "sending announce...");
3998 GST_ELEMENT_PROGRESS (sink, CONTINUE, "record",
3999 ("Sending server stream info"));
4002 gst_rtsp_client_sink_send (sink, &sink->conninfo, &request,
4003 &response, NULL)) < 0)
4006 /* send setup for all streams */
4007 if ((res = gst_rtsp_client_sink_setup_streams (sink, async)) < 0)
4010 res = gst_rtsp_client_sink_init_request (sink, &request, GST_RTSP_RECORD,
4011 sink->conninfo.url_str);
4014 goto create_request_failed;
4016 #if 0 /* FIXME: Configure a range based on input segments? */
4017 if (src->need_range) {
4018 hval = gen_range_header (src, segment);
4020 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
4023 if (segment->rate != 1.0) {
4024 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
4026 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
4028 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
4030 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
4035 GST_ELEMENT_PROGRESS (sink, CONTINUE, "record", ("Starting recording"));
4037 gst_rtsp_client_sink_send (sink, &sink->conninfo, &request,
4038 &response, NULL)) < 0)
4041 #if 0 /* FIXME: Check if servers return these for record: */
4042 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
4043 * for the RTP packets. If this is not present, we assume all starts from 0...
4044 * This is info for the RTP session manager that we pass to it in caps. */
4046 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
4047 &hval, hval_idx++) == GST_RTSP_OK)
4048 gst_rtspsrc_parse_rtpinfo (src, hval);
4050 /* some servers indicate RTCP parameters in PLAY response,
4051 * rather than properly in SDP */
4052 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
4053 &hval, 0) == GST_RTSP_OK)
4054 gst_rtspsrc_handle_rtcp_interval (src, hval);
4057 gst_rtsp_client_sink_set_state (sink, GST_STATE_PLAYING);
4058 sink->state = GST_RTSP_STATE_PLAYING;
4060 /* clean up any messages */
4061 gst_rtsp_message_unset (&request);
4062 gst_rtsp_message_unset (&response);
4067 create_request_failed:
4069 gchar *str = gst_rtsp_strresult (res);
4071 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
4072 ("Could not create request. (%s)", str));
4078 /* Don't post a message - the rtsp_send method will have
4079 * taken care of it because we passed NULL for the response code */
4084 GST_ERROR_OBJECT (sink, "setup failed");
4089 if (sink->conninfo.connection) {
4090 GST_DEBUG_OBJECT (sink, "free connection");
4091 gst_rtsp_conninfo_close (sink, &sink->conninfo, TRUE);
4093 gst_rtsp_message_unset (&request);
4094 gst_rtsp_message_unset (&response);
4099 static GstRTSPResult
4100 gst_rtsp_client_sink_pause (GstRTSPClientSink * sink, gboolean async)
4102 GstRTSPResult res = GST_RTSP_OK;
4103 GstRTSPMessage request = { 0 };
4104 GstRTSPMessage response = { 0 };
4106 const gchar *control;
4108 GST_DEBUG_OBJECT (sink, "PAUSE...");
4110 if ((res = gst_rtsp_client_sink_ensure_open (sink, async)) < 0)
4113 if (!(sink->methods & GST_RTSP_PAUSE))
4116 if (sink->state == GST_RTSP_STATE_READY)
4119 if (!sink->conninfo.connection || !sink->conninfo.connected)
4122 /* construct a control url */
4123 control = get_aggregate_control (sink);
4125 /* loop over the streams. We might exit the loop early when we could do an
4126 * aggregate control */
4127 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
4128 GstRTSPStreamContext *stream = (GstRTSPStreamContext *) walk->data;
4129 GstRTSPConnInfo *info;
4130 const gchar *setup_url;
4132 /* try aggregate control first but do non-aggregate control otherwise */
4134 setup_url = control;
4135 else if ((setup_url = stream->conninfo.location) == NULL)
4138 if (sink->conninfo.connection) {
4139 info = &sink->conninfo;
4140 } else if (stream->conninfo.connection) {
4141 info = &stream->conninfo;
4147 GST_ELEMENT_PROGRESS (sink, CONTINUE, "request",
4148 ("Sending PAUSE request"));
4151 gst_rtsp_client_sink_init_request (sink, &request, GST_RTSP_PAUSE,
4153 goto create_request_failed;
4156 gst_rtsp_client_sink_send (sink, info, &request, &response,
4160 gst_rtsp_message_unset (&request);
4161 gst_rtsp_message_unset (&response);
4163 /* exit early when we did agregate control */
4168 /* change element states now */
4169 gst_rtsp_client_sink_set_state (sink, GST_STATE_PAUSED);
4172 sink->state = GST_RTSP_STATE_READY;
4176 gst_rtsp_client_sink_loop_end_cmd (sink, CMD_PAUSE, res);
4183 GST_DEBUG_OBJECT (sink, "failed to open stream");
4188 GST_DEBUG_OBJECT (sink, "PAUSE is not supported");
4193 GST_DEBUG_OBJECT (sink, "we were already PAUSED");
4196 create_request_failed:
4198 gchar *str = gst_rtsp_strresult (res);
4200 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
4201 ("Could not create request. (%s)", str));
4207 gchar *str = gst_rtsp_strresult (res);
4209 gst_rtsp_message_unset (&request);
4210 if (res != GST_RTSP_EINTR) {
4211 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
4212 ("Could not send message. (%s)", str));
4214 GST_WARNING_OBJECT (sink, "PAUSE interrupted");
4222 gst_rtsp_client_sink_handle_message (GstBin * bin, GstMessage * message)
4224 GstRTSPClientSink *rtsp_client_sink;
4226 rtsp_client_sink = GST_RTSP_CLIENT_SINK (bin);
4228 switch (GST_MESSAGE_TYPE (message)) {
4229 case GST_MESSAGE_ELEMENT:
4231 const GstStructure *s = gst_message_get_structure (message);
4233 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
4234 gboolean ignore_timeout;
4236 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
4238 GST_OBJECT_LOCK (rtsp_client_sink);
4239 ignore_timeout = rtsp_client_sink->ignore_timeout;
4240 rtsp_client_sink->ignore_timeout = TRUE;
4241 GST_OBJECT_UNLOCK (rtsp_client_sink);
4243 /* we only act on the first udp timeout message, others are irrelevant
4244 * and can be ignored. */
4245 if (!ignore_timeout)
4246 gst_rtsp_client_sink_loop_send_cmd (rtsp_client_sink, CMD_RECONNECT,
4249 gst_message_unref (message);
4251 } else if (gst_structure_has_name (s, "GstRTSPStreamBlocking")) {
4252 /* An RTSPStream has prerolled */
4253 g_cond_broadcast (&rtsp_client_sink->preroll_cond);
4255 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
4258 case GST_MESSAGE_ASYNC_START:{
4261 sender = GST_MESSAGE_SRC (message);
4263 GST_LOG_OBJECT (rtsp_client_sink,
4264 "Have async-start from %" GST_PTR_FORMAT, sender);
4265 if (sender == GST_OBJECT (rtsp_client_sink->internal_bin)) {
4266 GST_LOG_OBJECT (rtsp_client_sink, "child bin is now ASYNC");
4268 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
4271 case GST_MESSAGE_ASYNC_DONE:
4274 gboolean need_async_done;
4276 sender = GST_MESSAGE_SRC (message);
4277 GST_LOG_OBJECT (rtsp_client_sink, "Have async-done from %" GST_PTR_FORMAT,
4280 g_mutex_lock (&rtsp_client_sink->preroll_lock);
4281 if (sender == GST_OBJECT_CAST (rtsp_client_sink->internal_bin)) {
4282 GST_LOG_OBJECT (rtsp_client_sink, "child bin is no longer ASYNC");
4284 need_async_done = rtsp_client_sink->in_async;
4285 if (rtsp_client_sink->in_async) {
4286 rtsp_client_sink->in_async = FALSE;
4287 g_cond_broadcast (&rtsp_client_sink->preroll_cond);
4289 g_mutex_unlock (&rtsp_client_sink->preroll_lock);
4291 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
4293 if (need_async_done) {
4294 GST_DEBUG_OBJECT (rtsp_client_sink, "Posting ASYNC-DONE");
4295 gst_element_post_message (GST_ELEMENT_CAST (rtsp_client_sink),
4296 gst_message_new_async_done (GST_OBJECT_CAST (rtsp_client_sink),
4297 GST_CLOCK_TIME_NONE));
4301 case GST_MESSAGE_ERROR:
4305 sender = GST_MESSAGE_SRC (message);
4307 GST_DEBUG_OBJECT (rtsp_client_sink, "got error from %s",
4308 GST_ELEMENT_NAME (sender));
4310 /* FIXME: Ignore errors on RTCP? */
4311 /* fatal but not our message, forward */
4312 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
4315 case GST_MESSAGE_STATE_CHANGED:
4317 if (GST_MESSAGE_SRC (message) ==
4318 (GstObject *) rtsp_client_sink->internal_bin) {
4319 GstState newstate, pending;
4320 gst_message_parse_state_changed (message, NULL, &newstate, &pending);
4321 g_mutex_lock (&rtsp_client_sink->preroll_lock);
4322 rtsp_client_sink->prerolled = (newstate >= GST_STATE_PAUSED)
4323 && pending == GST_STATE_VOID_PENDING;
4324 g_cond_broadcast (&rtsp_client_sink->preroll_cond);
4325 g_mutex_unlock (&rtsp_client_sink->preroll_lock);
4326 GST_DEBUG_OBJECT (bin,
4327 "Internal bin changed state to %s (pending %s). Prerolled now %d",
4328 gst_element_state_get_name (newstate),
4329 gst_element_state_get_name (pending), rtsp_client_sink->prerolled);
4335 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
4341 /* the thread where everything happens */
4343 gst_rtsp_client_sink_thread (GstRTSPClientSink * sink)
4347 GST_OBJECT_LOCK (sink);
4348 cmd = sink->pending_cmd;
4349 if (cmd == CMD_RECONNECT || cmd == CMD_RECORD || cmd == CMD_PAUSE
4350 || cmd == CMD_LOOP || cmd == CMD_OPEN)
4351 sink->pending_cmd = CMD_LOOP;
4353 sink->pending_cmd = CMD_WAIT;
4354 GST_DEBUG_OBJECT (sink, "got command %s", cmd_to_string (cmd));
4356 /* we got the message command, so ensure communication is possible again */
4357 gst_rtsp_client_sink_connection_flush (sink, FALSE);
4359 sink->busy_cmd = cmd;
4360 GST_OBJECT_UNLOCK (sink);
4364 gst_rtsp_client_sink_open (sink, TRUE);
4367 gst_rtsp_client_sink_record (sink, TRUE);
4370 gst_rtsp_client_sink_pause (sink, TRUE);
4373 gst_rtsp_client_sink_close (sink, TRUE, FALSE);
4376 gst_rtsp_client_sink_loop (sink);
4379 gst_rtsp_client_sink_reconnect (sink, FALSE);
4385 GST_OBJECT_LOCK (sink);
4386 /* and go back to sleep */
4387 if (sink->pending_cmd == CMD_WAIT) {
4389 gst_task_pause (sink->task);
4392 sink->busy_cmd = CMD_WAIT;
4393 GST_OBJECT_UNLOCK (sink);
4397 gst_rtsp_client_sink_start (GstRTSPClientSink * sink)
4399 GST_DEBUG_OBJECT (sink, "starting");
4401 sink->streams_collected = FALSE;
4402 sink->in_async = TRUE;
4403 gst_element_set_locked_state (GST_ELEMENT (sink->internal_bin), TRUE);
4405 gst_rtsp_client_sink_set_state (sink, GST_STATE_READY);
4407 GST_OBJECT_LOCK (sink);
4408 sink->pending_cmd = CMD_WAIT;
4410 if (sink->task == NULL) {
4412 gst_task_new ((GstTaskFunction) gst_rtsp_client_sink_thread, sink,
4414 if (sink->task == NULL)
4417 gst_task_set_lock (sink->task, GST_RTSP_STREAM_GET_LOCK (sink));
4419 GST_OBJECT_UNLOCK (sink);
4426 GST_OBJECT_UNLOCK (sink);
4427 GST_ERROR_OBJECT (sink, "failed to create task");
4433 gst_rtsp_client_sink_stop (GstRTSPClientSink * sink)
4437 GST_DEBUG_OBJECT (sink, "stopping");
4439 /* also cancels pending task */
4440 gst_rtsp_client_sink_loop_send_cmd (sink, CMD_WAIT, CMD_ALL & ~CMD_CLOSE);
4442 GST_OBJECT_LOCK (sink);
4443 if ((task = sink->task)) {
4445 GST_OBJECT_UNLOCK (sink);
4447 gst_task_stop (task);
4449 /* make sure it is not running */
4450 GST_RTSP_STREAM_LOCK (sink);
4451 GST_RTSP_STREAM_UNLOCK (sink);
4453 /* now wait for the task to finish */
4454 gst_task_join (task);
4456 /* and free the task */
4457 gst_object_unref (GST_OBJECT (task));
4459 GST_OBJECT_LOCK (sink);
4461 GST_OBJECT_UNLOCK (sink);
4463 /* ensure synchronously all is closed and clean */
4464 gst_rtsp_client_sink_close (sink, FALSE, TRUE);
4469 static GstStateChangeReturn
4470 gst_rtsp_client_sink_change_state (GstElement * element,
4471 GstStateChange transition)
4473 GstRTSPClientSink *rtsp_client_sink;
4474 GstStateChangeReturn ret;
4476 rtsp_client_sink = GST_RTSP_CLIENT_SINK (element);
4478 switch (transition) {
4479 case GST_STATE_CHANGE_NULL_TO_READY:
4480 if (!gst_rtsp_client_sink_start (rtsp_client_sink))
4483 case GST_STATE_CHANGE_READY_TO_PAUSED:
4484 /* init some state */
4485 rtsp_client_sink->cur_protocols = rtsp_client_sink->protocols;
4486 /* first attempt, don't ignore timeouts */
4487 rtsp_client_sink->ignore_timeout = FALSE;
4488 rtsp_client_sink->open_error = FALSE;
4490 gst_rtsp_client_sink_set_state (rtsp_client_sink, GST_STATE_PAUSED);
4492 g_mutex_lock (&rtsp_client_sink->preroll_lock);
4493 if (rtsp_client_sink->in_async) {
4494 GST_DEBUG_OBJECT (rtsp_client_sink, "Posting ASYNC-START");
4495 gst_element_post_message (GST_ELEMENT_CAST (rtsp_client_sink),
4496 gst_message_new_async_start (GST_OBJECT_CAST (rtsp_client_sink)));
4498 g_mutex_unlock (&rtsp_client_sink->preroll_lock);
4501 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4503 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4504 /* unblock the tcp tasks and make the loop waiting */
4505 if (gst_rtsp_client_sink_loop_send_cmd (rtsp_client_sink, CMD_WAIT,
4507 /* make sure it is waiting before we send PLAY below */
4508 GST_RTSP_STREAM_LOCK (rtsp_client_sink);
4509 GST_RTSP_STREAM_UNLOCK (rtsp_client_sink);
4512 case GST_STATE_CHANGE_PAUSED_TO_READY:
4513 gst_rtsp_client_sink_set_state (rtsp_client_sink, GST_STATE_READY);
4519 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4520 if (ret == GST_STATE_CHANGE_FAILURE)
4523 switch (transition) {
4524 case GST_STATE_CHANGE_NULL_TO_READY:
4525 ret = GST_STATE_CHANGE_SUCCESS;
4527 case GST_STATE_CHANGE_READY_TO_PAUSED:
4528 /* Return ASYNC and preroll input streams */
4529 g_mutex_lock (&rtsp_client_sink->preroll_lock);
4530 if (rtsp_client_sink->in_async)
4531 ret = GST_STATE_CHANGE_ASYNC;
4532 g_mutex_unlock (&rtsp_client_sink->preroll_lock);
4533 gst_rtsp_client_sink_loop_send_cmd (rtsp_client_sink, CMD_OPEN, 0);
4535 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:{
4536 GST_DEBUG_OBJECT (rtsp_client_sink,
4537 "Switching to playing -sending RECORD");
4538 gst_rtsp_client_sink_loop_send_cmd (rtsp_client_sink, CMD_RECORD, 0);
4539 ret = GST_STATE_CHANGE_SUCCESS;
4542 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4543 /* send pause request and keep the idle task around */
4544 gst_rtsp_client_sink_loop_send_cmd (rtsp_client_sink, CMD_PAUSE,
4546 ret = GST_STATE_CHANGE_NO_PREROLL;
4548 case GST_STATE_CHANGE_PAUSED_TO_READY:
4549 gst_rtsp_client_sink_loop_send_cmd (rtsp_client_sink, CMD_CLOSE,
4551 ret = GST_STATE_CHANGE_SUCCESS;
4553 case GST_STATE_CHANGE_READY_TO_NULL:
4554 gst_rtsp_client_sink_stop (rtsp_client_sink);
4555 ret = GST_STATE_CHANGE_SUCCESS;
4566 GST_DEBUG_OBJECT (rtsp_client_sink, "start failed");
4567 return GST_STATE_CHANGE_FAILURE;
4571 /*** GSTURIHANDLER INTERFACE *************************************************/
4574 gst_rtsp_client_sink_uri_get_type (GType type)
4576 return GST_URI_SINK;
4579 static const gchar *const *
4580 gst_rtsp_client_sink_uri_get_protocols (GType type)
4582 static const gchar *protocols[] =
4583 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
4584 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
4591 gst_rtsp_client_sink_uri_get_uri (GstURIHandler * handler)
4593 GstRTSPClientSink *sink = GST_RTSP_CLIENT_SINK (handler);
4595 /* FIXME: make thread-safe */
4596 return g_strdup (sink->conninfo.location);
4600 gst_rtsp_client_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri,
4603 GstRTSPClientSink *sink;
4606 GstRTSPUrl *newurl = NULL;
4607 GstSDPMessage *sdp = NULL;
4609 sink = GST_RTSP_CLIENT_SINK (handler);
4611 /* same URI, we're fine */
4612 if (sink->conninfo.location && uri && !strcmp (uri, sink->conninfo.location))
4615 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
4616 sres = gst_sdp_message_new (&sdp);
4620 GST_DEBUG_OBJECT (sink, "parsing SDP message");
4621 sres = gst_sdp_message_parse_uri (uri, sdp);
4626 GST_DEBUG_OBJECT (sink, "parsing URI");
4627 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
4631 /* if worked, free previous and store new url object along with the original
4633 GST_DEBUG_OBJECT (sink, "configuring URI");
4634 g_free (sink->conninfo.location);
4635 sink->conninfo.location = g_strdup (uri);
4636 gst_rtsp_url_free (sink->conninfo.url);
4637 sink->conninfo.url = newurl;
4638 g_free (sink->conninfo.url_str);
4640 sink->conninfo.url_str = gst_rtsp_url_get_request_uri (sink->conninfo.url);
4642 sink->conninfo.url_str = NULL;
4645 gst_sdp_message_free (sink->uri_sdp);
4646 sink->uri_sdp = sdp;
4647 sink->from_sdp = sdp != NULL;
4649 GST_DEBUG_OBJECT (sink, "set uri: %s", GST_STR_NULL (uri));
4650 GST_DEBUG_OBJECT (sink, "request uri is: %s",
4651 GST_STR_NULL (sink->conninfo.url_str));
4658 GST_DEBUG_OBJECT (sink, "URI was ok: '%s'", GST_STR_NULL (uri));
4663 GST_ERROR_OBJECT (sink, "Could not create new SDP (%d)", sres);
4664 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
4665 "Could not create SDP");
4670 GST_ERROR_OBJECT (sink, "Not a valid SDP (%d) '%s'", sres,
4671 GST_STR_NULL (uri));
4672 gst_sdp_message_free (sdp);
4673 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
4679 GST_ERROR_OBJECT (sink, "Not a valid RTSP url '%s' (%d)",
4680 GST_STR_NULL (uri), res);
4681 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
4682 "Invalid RTSP URI");
4688 gst_rtsp_client_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
4690 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
4692 iface->get_type = gst_rtsp_client_sink_uri_get_type;
4693 iface->get_protocols = gst_rtsp_client_sink_uri_get_protocols;
4694 iface->get_uri = gst_rtsp_client_sink_uri_get_uri;
4695 iface->set_uri = gst_rtsp_client_sink_uri_set_uri;