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,
119 SIGNAL_ACCEPT_CERTIFICATE,
123 enum _GstRTSPClientSinkNtpTimeSource
126 NTP_TIME_SOURCE_UNIX,
127 NTP_TIME_SOURCE_RUNNING_TIME,
128 NTP_TIME_SOURCE_CLOCK_TIME
131 #define GST_TYPE_RTSP_CLIENT_SINK_NTP_TIME_SOURCE (gst_rtsp_client_sink_ntp_time_source_get_type())
133 gst_rtsp_client_sink_ntp_time_source_get_type (void)
135 static GType ntp_time_source_type = 0;
136 static const GEnumValue ntp_time_source_values[] = {
137 {NTP_TIME_SOURCE_NTP, "NTP time based on realtime clock", "ntp"},
138 {NTP_TIME_SOURCE_UNIX, "UNIX time based on realtime clock", "unix"},
139 {NTP_TIME_SOURCE_RUNNING_TIME,
140 "Running time based on pipeline clock",
142 {NTP_TIME_SOURCE_CLOCK_TIME, "Pipeline clock time", "clock-time"},
146 if (!ntp_time_source_type) {
147 ntp_time_source_type =
148 g_enum_register_static ("GstRTSPClientSinkNtpTimeSource",
149 ntp_time_source_values);
151 return ntp_time_source_type;
154 #define DEFAULT_LOCATION NULL
155 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
156 #define DEFAULT_DEBUG FALSE
157 #define DEFAULT_RETRY 20
158 #define DEFAULT_TIMEOUT 5000000
159 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
160 #define DEFAULT_TCP_TIMEOUT 20000000
161 #define DEFAULT_LATENCY_MS 2000
162 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
163 #define DEFAULT_PROXY NULL
164 #define DEFAULT_RTP_BLOCKSIZE 0
165 #define DEFAULT_USER_ID NULL
166 #define DEFAULT_USER_PW NULL
167 #define DEFAULT_PORT_RANGE NULL
168 #define DEFAULT_UDP_RECONNECT TRUE
169 #define DEFAULT_MULTICAST_IFACE NULL
170 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
171 #define DEFAULT_TLS_DATABASE NULL
172 #define DEFAULT_TLS_INTERACTION NULL
173 #define DEFAULT_NTP_TIME_SOURCE NTP_TIME_SOURCE_NTP
174 #define DEFAULT_USER_AGENT "GStreamer/" PACKAGE_VERSION
175 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
176 #define DEFAULT_RTX_TIME_MS 500
189 PROP_DO_RTSP_KEEP_ALIVE,
197 PROP_UDP_BUFFER_SIZE,
199 PROP_MULTICAST_IFACE,
201 PROP_TLS_VALIDATION_FLAGS,
203 PROP_TLS_INTERACTION,
204 PROP_NTP_TIME_SOURCE,
209 static void gst_rtsp_client_sink_finalize (GObject * object);
211 static void gst_rtsp_client_sink_set_property (GObject * object, guint prop_id,
212 const GValue * value, GParamSpec * pspec);
213 static void gst_rtsp_client_sink_get_property (GObject * object, guint prop_id,
214 GValue * value, GParamSpec * pspec);
216 static GstClock *gst_rtsp_client_sink_provide_clock (GstElement * element);
218 static void gst_rtsp_client_sink_uri_handler_init (gpointer g_iface,
219 gpointer iface_data);
221 static gboolean gst_rtsp_client_sink_set_proxy (GstRTSPClientSink * rtsp,
222 const gchar * proxy);
223 static void gst_rtsp_client_sink_set_tcp_timeout (GstRTSPClientSink *
224 rtsp_client_sink, guint64 timeout);
226 static GstStateChangeReturn gst_rtsp_client_sink_change_state (GstElement *
227 element, GstStateChange transition);
228 static void gst_rtsp_client_sink_handle_message (GstBin * bin,
229 GstMessage * message);
231 static gboolean gst_rtsp_client_sink_setup_auth (GstRTSPClientSink * sink,
232 GstRTSPMessage * response);
234 static gboolean gst_rtsp_client_sink_loop_send_cmd (GstRTSPClientSink * sink,
235 gint cmd, gint mask);
237 static GstRTSPResult gst_rtsp_client_sink_open (GstRTSPClientSink * sink,
239 static GstRTSPResult gst_rtsp_client_sink_record (GstRTSPClientSink * sink,
241 static GstRTSPResult gst_rtsp_client_sink_pause (GstRTSPClientSink * sink,
243 static GstRTSPResult gst_rtsp_client_sink_close (GstRTSPClientSink * sink,
244 gboolean async, gboolean only_close);
245 static gboolean gst_rtsp_client_sink_collect_streams (GstRTSPClientSink * sink);
247 static gboolean gst_rtsp_client_sink_uri_set_uri (GstURIHandler * handler,
248 const gchar * uri, GError ** error);
249 static gchar *gst_rtsp_client_sink_uri_get_uri (GstURIHandler * handler);
251 static gboolean gst_rtsp_client_sink_loop (GstRTSPClientSink * sink);
252 static void gst_rtsp_client_sink_connection_flush (GstRTSPClientSink * sink,
255 static GstPad *gst_rtsp_client_sink_request_new_pad (GstElement * element,
256 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
257 static void gst_rtsp_client_sink_release_pad (GstElement * element,
260 /* commands we send to out loop to notify it of events */
261 #define CMD_OPEN (1 << 0)
262 #define CMD_RECORD (1 << 1)
263 #define CMD_PAUSE (1 << 2)
264 #define CMD_CLOSE (1 << 3)
265 #define CMD_WAIT (1 << 4)
266 #define CMD_RECONNECT (1 << 5)
267 #define CMD_LOOP (1 << 6)
269 /* mask for all commands */
270 #define CMD_ALL ((CMD_LOOP << 1) - 1)
272 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
274 gchar *__txt = _gst_element_error_printf text; \
275 gst_element_post_message (GST_ELEMENT_CAST (el), \
276 gst_message_new_progress (GST_OBJECT_CAST (el), \
277 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
281 static guint gst_rtsp_client_sink_signals[LAST_SIGNAL] = { 0 };
283 #define gst_rtsp_client_sink_parent_class parent_class
284 G_DEFINE_TYPE_WITH_CODE (GstRTSPClientSink, gst_rtsp_client_sink, GST_TYPE_BIN,
285 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
286 gst_rtsp_client_sink_uri_handler_init));
288 #ifndef GST_DISABLE_GST_DEBUG
289 static inline const gchar *
290 cmd_to_string (guint cmd)
314 gst_rtsp_client_sink_class_init (GstRTSPClientSinkClass * klass)
316 GObjectClass *gobject_class;
317 GstElementClass *gstelement_class;
318 GstBinClass *gstbin_class;
320 gobject_class = (GObjectClass *) klass;
321 gstelement_class = (GstElementClass *) klass;
322 gstbin_class = (GstBinClass *) klass;
324 GST_DEBUG_CATEGORY_INIT (rtsp_client_sink_debug, "rtspclientsink", 0,
325 "RTSP sink element");
327 gobject_class->set_property = gst_rtsp_client_sink_set_property;
328 gobject_class->get_property = gst_rtsp_client_sink_get_property;
330 gobject_class->finalize = gst_rtsp_client_sink_finalize;
332 g_object_class_install_property (gobject_class, PROP_LOCATION,
333 g_param_spec_string ("location", "RTSP Location",
334 "Location of the RTSP url to read",
335 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
337 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
338 g_param_spec_flags ("protocols", "Protocols",
339 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
340 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
342 g_object_class_install_property (gobject_class, PROP_PROFILES,
343 g_param_spec_flags ("profiles", "Profiles",
344 "Allowed RTSP profiles", GST_TYPE_RTSP_PROFILE,
345 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
347 g_object_class_install_property (gobject_class, PROP_DEBUG,
348 g_param_spec_boolean ("debug", "Debug",
349 "Dump request and response messages to stdout",
350 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
352 g_object_class_install_property (gobject_class, PROP_RETRY,
353 g_param_spec_uint ("retry", "Retry",
354 "Max number of retries when allocating RTP ports.",
355 0, G_MAXUINT16, DEFAULT_RETRY,
356 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
358 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
359 g_param_spec_uint64 ("timeout", "Timeout",
360 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
361 0, G_MAXUINT64, DEFAULT_TIMEOUT,
362 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
364 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
365 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
366 "Fail after timeout microseconds on TCP connections (0 = disabled)",
367 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
368 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
370 g_object_class_install_property (gobject_class, PROP_LATENCY,
371 g_param_spec_uint ("latency", "Buffer latency in ms",
372 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
373 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
375 g_object_class_install_property (gobject_class, PROP_RTX_TIME,
376 g_param_spec_uint ("rtx-time", "Retransmission buffer in ms",
377 "Amount of ms to buffer for retransmission. 0 disables retransmission",
378 0, G_MAXUINT, DEFAULT_RTX_TIME_MS,
379 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
382 * GstRTSPClientSink:do-rtsp-keep-alive:
384 * Enable RTSP keep alive support. Some old server don't like RTSP
385 * keep alive and then this property needs to be set to FALSE.
387 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
388 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
389 "Send RTSP keep alive packets, disable for old incompatible server.",
390 DEFAULT_DO_RTSP_KEEP_ALIVE,
391 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
394 * GstRTSPClientSink:proxy:
396 * Set the proxy parameters. This has to be a string of the format
397 * [http://][user:passwd@]host[:port].
399 g_object_class_install_property (gobject_class, PROP_PROXY,
400 g_param_spec_string ("proxy", "Proxy",
401 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
402 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
404 * GstRTSPClientSink:proxy-id:
406 * Sets the proxy URI user id for authentication. If the URI set via the
407 * "proxy" property contains a user-id already, that will take precedence.
410 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
411 g_param_spec_string ("proxy-id", "proxy-id",
412 "HTTP proxy URI user id for authentication", "",
413 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
415 * GstRTSPClientSink:proxy-pw:
417 * Sets the proxy URI password for authentication. If the URI set via the
418 * "proxy" property contains a password already, that will take precedence.
421 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
422 g_param_spec_string ("proxy-pw", "proxy-pw",
423 "HTTP proxy URI user password for authentication", "",
424 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
427 * GstRTSPClientSink:rtp-blocksize:
429 * RTP package size to suggest to server.
431 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
432 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
433 "RTP package size to suggest to server (0 = disabled)",
434 0, 65536, DEFAULT_RTP_BLOCKSIZE,
435 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
437 g_object_class_install_property (gobject_class,
439 g_param_spec_string ("user-id", "user-id",
440 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
441 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
442 g_object_class_install_property (gobject_class, PROP_USER_PW,
443 g_param_spec_string ("user-pw", "user-pw",
444 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
445 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
448 * GstRTSPClientSink:port-range:
450 * Configure the client port numbers that can be used to receive
453 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
454 g_param_spec_string ("port-range", "Port range",
455 "Client port range that can be used to receive RTCP data, "
456 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
457 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
460 * GstRTSPClientSink:udp-buffer-size:
462 * Size of the kernel UDP receive buffer in bytes.
464 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
465 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
466 "Size of the kernel UDP receive buffer in bytes, 0=default",
467 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
468 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
470 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
471 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
472 "Reconnect to the server if RTSP connection is closed when doing UDP",
473 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
475 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
476 g_param_spec_string ("multicast-iface", "Multicast Interface",
477 "The network interface on which to join the multicast group",
478 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
480 g_object_class_install_property (gobject_class, PROP_SDES,
481 g_param_spec_boxed ("sdes", "SDES",
482 "The SDES items of this session",
483 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
486 * GstRTSPClientSink::tls-validation-flags:
488 * TLS certificate validation flags used to validate server
492 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
493 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
494 "TLS certificate validation flags used to validate the server certificate",
495 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
496 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
499 * GstRTSPClientSink::tls-database:
501 * TLS database with anchor certificate authorities used to validate
502 * the server certificate.
505 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
506 g_param_spec_object ("tls-database", "TLS database",
507 "TLS database with anchor certificate authorities used to validate the server certificate",
508 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
511 * GstRTSPClientSink::tls-interaction:
513 * A #GTlsInteraction object to be used when the connection or certificate
514 * database need to interact with the user. This will be used to prompt the
515 * user for passwords where necessary.
518 g_object_class_install_property (gobject_class, PROP_TLS_INTERACTION,
519 g_param_spec_object ("tls-interaction", "TLS interaction",
520 "A GTlsInteraction object to prompt the user for password or certificate",
521 G_TYPE_TLS_INTERACTION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
524 * GstRTSPClientSink::ntp-time-source:
526 * allows to select the time source that should be used
527 * for the NTP time in outgoing packets
530 g_object_class_install_property (gobject_class, PROP_NTP_TIME_SOURCE,
531 g_param_spec_enum ("ntp-time-source", "NTP Time Source",
532 "NTP time source for RTCP packets",
533 GST_TYPE_RTSP_CLIENT_SINK_NTP_TIME_SOURCE, DEFAULT_NTP_TIME_SOURCE,
534 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
537 * GstRTSPClientSink::user-agent:
539 * The string to set in the User-Agent header.
542 g_object_class_install_property (gobject_class, PROP_USER_AGENT,
543 g_param_spec_string ("user-agent", "User Agent",
544 "The User-Agent string to send to the server",
545 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
548 * GstRTSPClientSink::handle-request:
549 * @rtsp_client_sink: a #GstRTSPClientSink
550 * @request: a #GstRTSPMessage
551 * @response: a #GstRTSPMessage
553 * Handle a server request in @request and prepare @response.
555 * This signal is called from the streaming thread, you should therefore not
556 * do any state changes on @rtsp_client_sink because this might deadlock. If you want
557 * to modify the state as a result of this signal, post a
558 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
562 gst_rtsp_client_sink_signals[SIGNAL_HANDLE_REQUEST] =
563 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
564 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
565 G_TYPE_POINTER, G_TYPE_POINTER);
568 * GstRTSPClientSink::new-manager:
569 * @rtsp_client_sink: a #GstRTSPClientSink
570 * @manager: a #GstElement
572 * Emitted after a new manager (like rtpbin) was created and the default
573 * properties were configured.
576 gst_rtsp_client_sink_signals[SIGNAL_NEW_MANAGER] =
577 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
578 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
579 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
582 * GstRTSPClientSink::new-payloader:
583 * @rtsp_client_sink: a #GstRTSPClientSink
584 * @payloader: a #GstElement
586 * Emitted after a new RTP payloader was created and the default
587 * properties were configured.
590 gst_rtsp_client_sink_signals[SIGNAL_NEW_PAYLOADER] =
591 g_signal_new_class_handler ("new-payloader", G_TYPE_FROM_CLASS (klass),
592 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
593 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
596 * GstRTSPClientSink::request-rtcp-key:
597 * @rtsp_client_sink: a #GstRTSPClientSink
598 * @num: the stream number
600 * Signal emitted to get the crypto parameters relevant to the RTCP
601 * stream. User should provide the key and the RTCP encryption ciphers
602 * and authentication, and return them wrapped in a GstCaps.
605 gst_rtsp_client_sink_signals[SIGNAL_REQUEST_RTCP_KEY] =
606 g_signal_new ("request-rtcp-key", G_TYPE_FROM_CLASS (klass),
607 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, GST_TYPE_CAPS, 1, G_TYPE_UINT);
610 * GstRTSPClientSink::accept-certificate:
611 * @rtsp_client_sink: a #GstRTSPClientSink
612 * @peer_cert: the peer's #GTlsCertificate
613 * @errors: the problems with @peer_cert
614 * @user_data: user data set when the signal handler was connected.
616 * This will directly map to #GTlsConnection 's "accept-certificate"
617 * signal and be performed after the default checks of #GstRTSPConnection
618 * (checking against the #GTlsDatabase with the given #GTlsCertificateFlags)
619 * have failed. If no #GTlsDatabase is set on this connection, only this
620 * signal will be emitted.
624 gst_rtsp_client_sink_signals[SIGNAL_ACCEPT_CERTIFICATE] =
625 g_signal_new ("accept-certificate", G_TYPE_FROM_CLASS (klass),
626 G_SIGNAL_RUN_LAST, 0, g_signal_accumulator_true_handled, NULL, NULL,
627 G_TYPE_BOOLEAN, 3, G_TYPE_TLS_CONNECTION, G_TYPE_TLS_CERTIFICATE,
628 G_TYPE_TLS_CERTIFICATE_FLAGS);
630 gstelement_class->provide_clock = gst_rtsp_client_sink_provide_clock;
631 gstelement_class->change_state = gst_rtsp_client_sink_change_state;
632 gstelement_class->request_new_pad =
633 GST_DEBUG_FUNCPTR (gst_rtsp_client_sink_request_new_pad);
634 gstelement_class->release_pad =
635 GST_DEBUG_FUNCPTR (gst_rtsp_client_sink_release_pad);
637 gst_element_class_add_static_pad_template (gstelement_class, &rtptemplate);
639 gst_element_class_set_static_metadata (gstelement_class,
640 "RTSP RECORD client", "Sink/Network",
641 "Send data over the network via RTSP RECORD(RFC 2326)",
642 "Jan Schmidt <jan@centricular.com>");
644 gstbin_class->handle_message = gst_rtsp_client_sink_handle_message;
648 gst_rtsp_client_sink_init (GstRTSPClientSink * sink)
650 sink->conninfo.location = g_strdup (DEFAULT_LOCATION);
651 sink->protocols = DEFAULT_PROTOCOLS;
652 sink->debug = DEFAULT_DEBUG;
653 sink->retry = DEFAULT_RETRY;
654 sink->udp_timeout = DEFAULT_TIMEOUT;
655 gst_rtsp_client_sink_set_tcp_timeout (sink, DEFAULT_TCP_TIMEOUT);
656 sink->latency = DEFAULT_LATENCY_MS;
657 sink->rtx_time = DEFAULT_RTX_TIME_MS;
658 sink->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
659 gst_rtsp_client_sink_set_proxy (sink, DEFAULT_PROXY);
660 sink->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
661 sink->user_id = g_strdup (DEFAULT_USER_ID);
662 sink->user_pw = g_strdup (DEFAULT_USER_PW);
663 sink->client_port_range.min = 0;
664 sink->client_port_range.max = 0;
665 sink->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
666 sink->udp_reconnect = DEFAULT_UDP_RECONNECT;
667 sink->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
669 sink->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
670 sink->tls_database = DEFAULT_TLS_DATABASE;
671 sink->tls_interaction = DEFAULT_TLS_INTERACTION;
672 sink->ntp_time_source = DEFAULT_NTP_TIME_SOURCE;
673 sink->user_agent = g_strdup (DEFAULT_USER_AGENT);
675 sink->profiles = DEFAULT_PROFILES;
677 /* protects the streaming thread in interleaved mode or the polling
678 * thread in UDP mode. */
679 g_rec_mutex_init (&sink->stream_rec_lock);
681 /* protects our state changes from multiple invocations */
682 g_rec_mutex_init (&sink->state_rec_lock);
684 g_mutex_init (&sink->send_lock);
686 g_mutex_init (&sink->preroll_lock);
687 g_cond_init (&sink->preroll_cond);
689 sink->state = GST_RTSP_STATE_INVALID;
691 g_mutex_init (&sink->conninfo.send_lock);
692 g_mutex_init (&sink->conninfo.recv_lock);
694 sink->internal_bin = (GstBin *) gst_bin_new ("rtspbin");
695 gst_element_set_locked_state (GST_ELEMENT_CAST (sink->internal_bin), TRUE);
696 gst_bin_add (GST_BIN (sink), GST_ELEMENT_CAST (sink->internal_bin));
698 sink->next_dyn_pt = 96;
700 gst_sdp_message_init (&sink->cursdp);
702 GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_FLAG_SINK);
706 gst_rtsp_client_sink_finalize (GObject * object)
708 GstRTSPClientSink *rtsp_client_sink;
710 rtsp_client_sink = GST_RTSP_CLIENT_SINK (object);
712 gst_sdp_message_uninit (&rtsp_client_sink->cursdp);
714 g_free (rtsp_client_sink->conninfo.location);
715 gst_rtsp_url_free (rtsp_client_sink->conninfo.url);
716 g_free (rtsp_client_sink->conninfo.url_str);
717 g_free (rtsp_client_sink->user_id);
718 g_free (rtsp_client_sink->user_pw);
719 g_free (rtsp_client_sink->multi_iface);
720 g_free (rtsp_client_sink->user_agent);
722 if (rtsp_client_sink->uri_sdp) {
723 gst_sdp_message_free (rtsp_client_sink->uri_sdp);
724 rtsp_client_sink->uri_sdp = NULL;
726 if (rtsp_client_sink->provided_clock)
727 gst_object_unref (rtsp_client_sink->provided_clock);
729 if (rtsp_client_sink->sdes)
730 gst_structure_free (rtsp_client_sink->sdes);
732 if (rtsp_client_sink->tls_database)
733 g_object_unref (rtsp_client_sink->tls_database);
735 if (rtsp_client_sink->tls_interaction)
736 g_object_unref (rtsp_client_sink->tls_interaction);
739 g_rec_mutex_clear (&rtsp_client_sink->stream_rec_lock);
740 g_rec_mutex_clear (&rtsp_client_sink->state_rec_lock);
742 g_mutex_clear (&rtsp_client_sink->conninfo.send_lock);
743 g_mutex_clear (&rtsp_client_sink->conninfo.recv_lock);
745 g_mutex_clear (&rtsp_client_sink->send_lock);
747 g_mutex_clear (&rtsp_client_sink->preroll_lock);
748 g_cond_clear (&rtsp_client_sink->preroll_cond);
750 G_OBJECT_CLASS (parent_class)->finalize (object);
754 gst_rtp_payloader_filter_func (GstPluginFeature * feature, gpointer user_data)
756 GstElementFactory *factory = NULL;
759 if (!GST_IS_ELEMENT_FACTORY (feature))
762 factory = GST_ELEMENT_FACTORY (feature);
764 if (gst_plugin_feature_get_rank (feature) == GST_RANK_NONE)
767 if (!gst_element_factory_list_is_type (factory,
768 GST_ELEMENT_FACTORY_TYPE_PAYLOADER))
772 gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_KLASS);
773 if (strstr (klass, "Codec") == NULL)
775 if (strstr (klass, "RTP") == NULL)
782 compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
785 const gchar *rname1, *rname2;
786 GstRank rank1, rank2;
788 rname1 = gst_plugin_feature_get_name (f1);
789 rname2 = gst_plugin_feature_get_name (f2);
791 rank1 = gst_plugin_feature_get_rank (f1);
792 rank2 = gst_plugin_feature_get_rank (f2);
794 /* HACK: Prefer rtpmp4apay over rtpmp4gpay */
795 if (g_str_equal (rname1, "rtpmp4apay"))
796 rank1 = GST_RANK_SECONDARY + 1;
797 if (g_str_equal (rname2, "rtpmp4apay"))
798 rank2 = GST_RANK_SECONDARY + 1;
800 diff = rank2 - rank1;
804 diff = strcmp (rname2, rname1);
810 gst_rtsp_client_sink_get_factories (void)
812 static GList *payloader_factories = NULL;
814 if (g_once_init_enter (&payloader_factories)) {
815 GList *all_factories;
818 gst_registry_feature_filter (gst_registry_get (),
819 gst_rtp_payloader_filter_func, FALSE, NULL);
821 all_factories = g_list_sort (all_factories, (GCompareFunc) compare_ranks);
823 g_once_init_leave (&payloader_factories, all_factories);
826 return payloader_factories;
830 gst_rtsp_client_sink_get_payloader_caps (void)
832 /* Cached caps result */
835 if (g_once_init_enter (&ret)) {
836 GList *factories, *cur;
837 GstCaps *caps = gst_caps_new_empty ();
839 factories = gst_rtsp_client_sink_get_factories ();
840 for (cur = factories; cur != NULL; cur = g_list_next (cur)) {
841 GstElementFactory *factory = GST_ELEMENT_FACTORY (cur->data);
844 for (tmp = gst_element_factory_get_static_pad_templates (factory);
845 tmp; tmp = g_list_next (tmp)) {
846 GstStaticPadTemplate *template = tmp->data;
848 if (template->direction == GST_PAD_SINK) {
849 GstCaps *static_caps = gst_static_pad_template_get_caps (template);
851 GST_LOG ("Found pad template %s on factory %s",
852 template->name_template, gst_plugin_feature_get_name (factory));
855 caps = gst_caps_merge (caps, static_caps);
857 /* Early out, any is absorbing */
858 if (gst_caps_is_any (caps))
864 g_once_init_leave (&ret, caps);
867 /* Return cached result */
868 return gst_caps_ref (ret);
872 gst_rtsp_client_sink_make_payloader (GstCaps * caps)
874 GList *factories, *cur;
876 factories = gst_rtsp_client_sink_get_factories ();
877 for (cur = factories; cur != NULL; cur = g_list_next (cur)) {
878 GstElementFactory *factory = GST_ELEMENT_FACTORY (cur->data);
881 for (tmp = gst_element_factory_get_static_pad_templates (factory);
882 tmp; tmp = g_list_next (tmp)) {
883 GstStaticPadTemplate *template = tmp->data;
885 if (template->direction == GST_PAD_SINK) {
886 GstCaps *static_caps = gst_static_pad_template_get_caps (template);
887 GstElement *payloader = NULL;
889 if (gst_caps_can_intersect (static_caps, caps)) {
890 GST_DEBUG ("caps %" GST_PTR_FORMAT " intersects with template %"
891 GST_PTR_FORMAT " for payloader %s", caps, static_caps,
892 gst_plugin_feature_get_name (factory));
893 payloader = gst_element_factory_create (factory, NULL);
896 gst_caps_unref (static_caps);
907 static GstRTSPStream *
908 gst_rtsp_client_sink_create_stream (GstRTSPClientSink * sink,
909 GstRTSPStreamContext * context, GstElement * payloader, GstPad * pad)
911 GstRTSPStream *stream = NULL;
914 GST_OBJECT_LOCK (sink);
916 g_object_get (G_OBJECT (payloader), "pt", &pt, NULL);
917 if (pt >= 96 && pt <= sink->next_dyn_pt) {
918 /* Payloader has a dynamic PT, but one that's already used */
919 /* FIXME: Create a caps->ptmap instead? */
920 pt = sink->next_dyn_pt;
925 GST_DEBUG_OBJECT (sink, "Assigning pt %u to stream %d", pt, context->index);
929 GST_DEBUG_OBJECT (sink, "Keeping existing pt %u for stream %d",
933 aux_pt = sink->next_dyn_pt;
938 GST_OBJECT_UNLOCK (sink);
941 g_object_set (G_OBJECT (payloader), "pt", pt, NULL);
943 stream = gst_rtsp_stream_new (context->index, payloader, pad);
945 gst_rtsp_stream_set_client_side (stream, TRUE);
946 gst_rtsp_stream_set_retransmission_time (stream,
947 (GstClockTime) (sink->rtx_time) * GST_MSECOND);
948 gst_rtsp_stream_set_protocols (stream, sink->protocols);
949 gst_rtsp_stream_set_profiles (stream, sink->profiles);
950 gst_rtsp_stream_set_retransmission_pt (stream, aux_pt);
951 gst_rtsp_stream_set_buffer_size (stream, sink->udp_buffer_size);
952 if (sink->rtp_blocksize > 0)
953 gst_rtsp_stream_set_mtu (stream, sink->rtp_blocksize);
954 gst_rtsp_stream_set_multicast_iface (stream, sink->multi_iface);
958 gst_rtsp_stream_set_address_pool (stream, priv->pool);
963 GST_OBJECT_UNLOCK (sink);
965 GST_ELEMENT_ERROR (sink, RESOURCE, NO_SPACE_LEFT, (NULL),
966 ("Ran out of dynamic payload types."));
971 static GstPadProbeReturn
972 handle_payloader_block (GstPad * pad, GstPadProbeInfo * info,
973 GstRTSPStreamContext * context)
975 GstRTSPClientSink *sink = context->parent;
977 GST_INFO_OBJECT (sink, "Block on pad %" GST_PTR_FORMAT, pad);
979 g_mutex_lock (&sink->preroll_lock);
980 context->prerolled = TRUE;
981 g_cond_broadcast (&sink->preroll_cond);
982 g_mutex_unlock (&sink->preroll_lock);
984 GST_INFO_OBJECT (sink, "Announced preroll on pad %" GST_PTR_FORMAT, pad);
986 return GST_PAD_PROBE_OK;
990 gst_rtsp_client_sink_setup_payloader (GstRTSPClientSink * sink, GstPad * pad,
993 GstRTSPStreamContext *context;
995 GstElement *payloader;
996 GstPad *sinkpad, *srcpad, *ghostsink;
998 context = gst_pad_get_element_private (pad);
1000 /* Find the payloader. FIXME: Allow user to provide payloader via pad property */
1001 payloader = gst_rtsp_client_sink_make_payloader (caps);
1002 if (payloader == NULL)
1005 GST_DEBUG_OBJECT (sink, "Configuring payloader %" GST_PTR_FORMAT
1006 " for pad %" GST_PTR_FORMAT, payloader, pad);
1008 sinkpad = gst_element_get_static_pad (payloader, "sink");
1009 if (sinkpad == NULL)
1012 srcpad = gst_element_get_static_pad (payloader, "src");
1016 gst_bin_add (GST_BIN (sink->internal_bin), payloader);
1017 ghostsink = gst_ghost_pad_new (NULL, sinkpad);
1018 gst_pad_set_active (ghostsink, TRUE);
1019 gst_element_add_pad (GST_ELEMENT (sink->internal_bin), ghostsink);
1021 g_signal_emit (sink, gst_rtsp_client_sink_signals[SIGNAL_NEW_PAYLOADER], 0,
1024 GST_RTSP_STATE_LOCK (sink);
1025 context->payloader_block_id =
1026 gst_pad_add_probe (srcpad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
1027 (GstPadProbeCallback) handle_payloader_block, context, NULL);
1028 context->payloader = payloader;
1030 payloader = gst_object_ref (payloader);
1032 gst_ghost_pad_set_target (GST_GHOST_PAD (pad), ghostsink);
1033 gst_object_unref (GST_OBJECT (sinkpad));
1034 GST_RTSP_STATE_UNLOCK (sink);
1036 gst_element_sync_state_with_parent (payloader);
1038 gst_object_unref (payloader);
1039 gst_object_unref (GST_OBJECT (srcpad));
1044 GST_ERROR_OBJECT (sink,
1045 "Could not find sink pad on payloader %" GST_PTR_FORMAT, payloader);
1046 gst_object_unref (payloader);
1050 GST_ERROR_OBJECT (sink,
1051 "Could not find src pad on payloader %" GST_PTR_FORMAT, payloader);
1052 gst_object_unref (GST_OBJECT (sinkpad));
1053 gst_object_unref (payloader);
1058 gst_rtsp_client_sink_sinkpad_event (GstPad * pad, GstObject * parent,
1061 if (GST_EVENT_TYPE (event) == GST_EVENT_CAPS) {
1062 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD (pad));
1063 if (target == NULL) {
1066 /* No target yet - choose a payloader and configure it */
1067 gst_event_parse_caps (event, &caps);
1069 GST_DEBUG_OBJECT (parent,
1070 "Have set caps event on pad %" GST_PTR_FORMAT
1071 " caps %" GST_PTR_FORMAT, pad, caps);
1073 if (!gst_rtsp_client_sink_setup_payloader (GST_RTSP_CLIENT_SINK (parent),
1075 gst_event_unref (event);
1079 gst_object_unref (target);
1083 return gst_pad_event_default (pad, parent, event);
1087 gst_rtsp_client_sink_sinkpad_query (GstPad * pad, GstObject * parent,
1090 if (GST_QUERY_TYPE (query) == GST_QUERY_CAPS) {
1091 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD (pad));
1092 if (target == NULL) {
1093 /* No target yet - return the union of all payloader caps */
1094 GstCaps *caps = gst_rtsp_client_sink_get_payloader_caps ();
1096 GST_TRACE_OBJECT (parent, "Returning payloader caps %" GST_PTR_FORMAT,
1099 gst_query_set_caps_result (query, caps);
1100 gst_caps_unref (caps);
1104 gst_object_unref (target);
1107 return gst_pad_query_default (pad, parent, query);
1111 gst_rtsp_client_sink_request_new_pad (GstElement * element,
1112 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
1114 GstRTSPClientSink *sink = GST_RTSP_CLIENT_SINK (element);
1116 GstRTSPStreamContext *context;
1117 guint idx = (guint) - 1;
1120 g_mutex_lock (&sink->preroll_lock);
1121 if (sink->streams_collected) {
1122 GST_WARNING_OBJECT (element, "Can't add streams to a running session");
1123 g_mutex_unlock (&sink->preroll_lock);
1126 g_mutex_unlock (&sink->preroll_lock);
1128 GST_OBJECT_LOCK (sink);
1130 if (!sscanf (name, "sink_%u", &idx)) {
1131 GST_OBJECT_UNLOCK (sink);
1132 GST_ERROR_OBJECT (element, "Invalid sink pad name %s", name);
1136 if (idx >= sink->next_pad_id)
1137 sink->next_pad_id = idx + 1;
1139 if (idx == (guint) - 1) {
1140 idx = sink->next_pad_id;
1141 sink->next_pad_id++;
1143 GST_OBJECT_UNLOCK (sink);
1145 tmpname = g_strdup_printf ("sink_%u", idx);
1146 pad = gst_ghost_pad_new_no_target_from_template (tmpname, templ);
1149 GST_DEBUG_OBJECT (element, "Creating request pad %" GST_PTR_FORMAT, pad);
1151 gst_pad_set_event_function (pad,
1152 GST_DEBUG_FUNCPTR (gst_rtsp_client_sink_sinkpad_event));
1153 gst_pad_set_query_function (pad,
1154 GST_DEBUG_FUNCPTR (gst_rtsp_client_sink_sinkpad_query));
1156 context = g_new0 (GstRTSPStreamContext, 1);
1157 context->parent = sink;
1158 context->index = idx;
1160 gst_pad_set_element_private (pad, context);
1162 /* The rest of the context is configured on a caps set */
1163 gst_pad_set_active (pad, TRUE);
1164 gst_element_add_pad (element, pad);
1166 (void) gst_rtsp_client_sink_get_factories ();
1168 g_mutex_init (&context->conninfo.send_lock);
1169 g_mutex_init (&context->conninfo.recv_lock);
1171 GST_RTSP_STATE_LOCK (sink);
1172 sink->contexts = g_list_prepend (sink->contexts, context);
1173 GST_RTSP_STATE_UNLOCK (sink);
1179 gst_rtsp_client_sink_release_pad (GstElement * element, GstPad * pad)
1181 GstRTSPClientSink *sink = GST_RTSP_CLIENT_SINK (element);
1182 GstRTSPStreamContext *context;
1184 context = gst_pad_get_element_private (pad);
1186 GST_RTSP_STATE_LOCK (sink);
1187 sink->contexts = g_list_remove (sink->contexts, context);
1188 GST_RTSP_STATE_UNLOCK (sink);
1190 /* FIXME: Shut down and clean up streaming on this pad,
1191 * do teardown if needed */
1192 GST_LOG_OBJECT (sink,
1193 "Cleaning up payloader and stream for released pad %" GST_PTR_FORMAT,
1196 if (context->stream_transport) {
1197 gst_rtsp_stream_transport_set_active (context->stream_transport, FALSE);
1198 gst_object_unref (context->stream_transport);
1199 context->stream_transport = NULL;
1201 if (context->stream) {
1202 if (context->joined) {
1203 gst_rtsp_stream_leave_bin (context->stream,
1204 GST_BIN (sink->internal_bin), sink->rtpbin);
1205 context->joined = FALSE;
1207 gst_object_unref (context->stream);
1208 context->stream = NULL;
1210 if (context->srtcpparams)
1211 gst_caps_unref (context->srtcpparams);
1213 g_free (context->conninfo.location);
1214 context->conninfo.location = NULL;
1216 g_mutex_clear (&context->conninfo.send_lock);
1217 g_mutex_clear (&context->conninfo.recv_lock);
1221 gst_element_remove_pad (element, pad);
1225 gst_rtsp_client_sink_provide_clock (GstElement * element)
1227 GstRTSPClientSink *sink = GST_RTSP_CLIENT_SINK (element);
1230 if ((clock = sink->provided_clock) != NULL)
1231 gst_object_ref (clock);
1236 /* a proxy string of the format [user:passwd@]host[:port] */
1238 gst_rtsp_client_sink_set_proxy (GstRTSPClientSink * rtsp, const gchar * proxy)
1240 gchar *p, *at, *col;
1242 g_free (rtsp->proxy_user);
1243 rtsp->proxy_user = NULL;
1244 g_free (rtsp->proxy_passwd);
1245 rtsp->proxy_passwd = NULL;
1246 g_free (rtsp->proxy_host);
1247 rtsp->proxy_host = NULL;
1248 rtsp->proxy_port = 0;
1250 p = (gchar *) proxy;
1255 /* we allow http:// in front but ignore it */
1256 if (g_str_has_prefix (p, "http://"))
1259 at = strchr (p, '@');
1261 /* look for user:passwd */
1262 col = strchr (proxy, ':');
1263 if (col == NULL || col > at)
1266 rtsp->proxy_user = g_strndup (p, col - p);
1268 rtsp->proxy_passwd = g_strndup (col, at - col);
1273 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
1274 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
1275 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
1276 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
1277 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
1278 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
1279 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
1282 col = strchr (p, ':');
1285 /* everything before the colon is the hostname */
1286 rtsp->proxy_host = g_strndup (p, col - p);
1288 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
1290 rtsp->proxy_host = g_strdup (p);
1291 rtsp->proxy_port = 8080;
1297 gst_rtsp_client_sink_set_tcp_timeout (GstRTSPClientSink * rtsp_client_sink,
1300 rtsp_client_sink->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
1301 rtsp_client_sink->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
1304 rtsp_client_sink->ptcp_timeout = &rtsp_client_sink->tcp_timeout;
1306 rtsp_client_sink->ptcp_timeout = NULL;
1310 gst_rtsp_client_sink_set_property (GObject * object, guint prop_id,
1311 const GValue * value, GParamSpec * pspec)
1313 GstRTSPClientSink *rtsp_client_sink;
1315 rtsp_client_sink = GST_RTSP_CLIENT_SINK (object);
1319 gst_rtsp_client_sink_uri_set_uri (GST_URI_HANDLER (rtsp_client_sink),
1320 g_value_get_string (value), NULL);
1322 case PROP_PROTOCOLS:
1323 rtsp_client_sink->protocols = g_value_get_flags (value);
1326 rtsp_client_sink->profiles = g_value_get_flags (value);
1329 rtsp_client_sink->debug = g_value_get_boolean (value);
1332 rtsp_client_sink->retry = g_value_get_uint (value);
1335 rtsp_client_sink->udp_timeout = g_value_get_uint64 (value);
1337 case PROP_TCP_TIMEOUT:
1338 gst_rtsp_client_sink_set_tcp_timeout (rtsp_client_sink,
1339 g_value_get_uint64 (value));
1342 rtsp_client_sink->latency = g_value_get_uint (value);
1345 rtsp_client_sink->rtx_time = g_value_get_uint (value);
1347 case PROP_DO_RTSP_KEEP_ALIVE:
1348 rtsp_client_sink->do_rtsp_keep_alive = g_value_get_boolean (value);
1351 gst_rtsp_client_sink_set_proxy (rtsp_client_sink,
1352 g_value_get_string (value));
1355 if (rtsp_client_sink->prop_proxy_id)
1356 g_free (rtsp_client_sink->prop_proxy_id);
1357 rtsp_client_sink->prop_proxy_id = g_value_dup_string (value);
1360 if (rtsp_client_sink->prop_proxy_pw)
1361 g_free (rtsp_client_sink->prop_proxy_pw);
1362 rtsp_client_sink->prop_proxy_pw = g_value_dup_string (value);
1364 case PROP_RTP_BLOCKSIZE:
1365 rtsp_client_sink->rtp_blocksize = g_value_get_uint (value);
1368 if (rtsp_client_sink->user_id)
1369 g_free (rtsp_client_sink->user_id);
1370 rtsp_client_sink->user_id = g_value_dup_string (value);
1373 if (rtsp_client_sink->user_pw)
1374 g_free (rtsp_client_sink->user_pw);
1375 rtsp_client_sink->user_pw = g_value_dup_string (value);
1377 case PROP_PORT_RANGE:
1381 str = g_value_get_string (value);
1382 if (!str || !sscanf (str, "%u-%u",
1383 &rtsp_client_sink->client_port_range.min,
1384 &rtsp_client_sink->client_port_range.max)) {
1385 rtsp_client_sink->client_port_range.min = 0;
1386 rtsp_client_sink->client_port_range.max = 0;
1390 case PROP_UDP_BUFFER_SIZE:
1391 rtsp_client_sink->udp_buffer_size = g_value_get_int (value);
1393 case PROP_UDP_RECONNECT:
1394 rtsp_client_sink->udp_reconnect = g_value_get_boolean (value);
1396 case PROP_MULTICAST_IFACE:
1397 g_free (rtsp_client_sink->multi_iface);
1399 if (g_value_get_string (value) == NULL)
1400 rtsp_client_sink->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1402 rtsp_client_sink->multi_iface = g_value_dup_string (value);
1405 rtsp_client_sink->sdes = g_value_dup_boxed (value);
1407 case PROP_TLS_VALIDATION_FLAGS:
1408 rtsp_client_sink->tls_validation_flags = g_value_get_flags (value);
1410 case PROP_TLS_DATABASE:
1411 g_clear_object (&rtsp_client_sink->tls_database);
1412 rtsp_client_sink->tls_database = g_value_dup_object (value);
1414 case PROP_TLS_INTERACTION:
1415 g_clear_object (&rtsp_client_sink->tls_interaction);
1416 rtsp_client_sink->tls_interaction = g_value_dup_object (value);
1418 case PROP_NTP_TIME_SOURCE:
1419 rtsp_client_sink->ntp_time_source = g_value_get_enum (value);
1421 case PROP_USER_AGENT:
1422 g_free (rtsp_client_sink->user_agent);
1423 rtsp_client_sink->user_agent = g_value_dup_string (value);
1426 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1432 gst_rtsp_client_sink_get_property (GObject * object, guint prop_id,
1433 GValue * value, GParamSpec * pspec)
1435 GstRTSPClientSink *rtsp_client_sink;
1437 rtsp_client_sink = GST_RTSP_CLIENT_SINK (object);
1441 g_value_set_string (value, rtsp_client_sink->conninfo.location);
1443 case PROP_PROTOCOLS:
1444 g_value_set_flags (value, rtsp_client_sink->protocols);
1447 g_value_set_flags (value, rtsp_client_sink->profiles);
1450 g_value_set_boolean (value, rtsp_client_sink->debug);
1453 g_value_set_uint (value, rtsp_client_sink->retry);
1456 g_value_set_uint64 (value, rtsp_client_sink->udp_timeout);
1458 case PROP_TCP_TIMEOUT:
1462 timeout = rtsp_client_sink->tcp_timeout.tv_sec * G_USEC_PER_SEC +
1463 rtsp_client_sink->tcp_timeout.tv_usec;
1464 g_value_set_uint64 (value, timeout);
1468 g_value_set_uint (value, rtsp_client_sink->latency);
1471 g_value_set_uint (value, rtsp_client_sink->rtx_time);
1473 case PROP_DO_RTSP_KEEP_ALIVE:
1474 g_value_set_boolean (value, rtsp_client_sink->do_rtsp_keep_alive);
1480 if (rtsp_client_sink->proxy_host) {
1482 g_strdup_printf ("%s:%d", rtsp_client_sink->proxy_host,
1483 rtsp_client_sink->proxy_port);
1487 g_value_take_string (value, str);
1491 g_value_set_string (value, rtsp_client_sink->prop_proxy_id);
1494 g_value_set_string (value, rtsp_client_sink->prop_proxy_pw);
1496 case PROP_RTP_BLOCKSIZE:
1497 g_value_set_uint (value, rtsp_client_sink->rtp_blocksize);
1500 g_value_set_string (value, rtsp_client_sink->user_id);
1503 g_value_set_string (value, rtsp_client_sink->user_pw);
1505 case PROP_PORT_RANGE:
1509 if (rtsp_client_sink->client_port_range.min != 0) {
1510 str = g_strdup_printf ("%u-%u", rtsp_client_sink->client_port_range.min,
1511 rtsp_client_sink->client_port_range.max);
1515 g_value_take_string (value, str);
1518 case PROP_UDP_BUFFER_SIZE:
1519 g_value_set_int (value, rtsp_client_sink->udp_buffer_size);
1521 case PROP_UDP_RECONNECT:
1522 g_value_set_boolean (value, rtsp_client_sink->udp_reconnect);
1524 case PROP_MULTICAST_IFACE:
1525 g_value_set_string (value, rtsp_client_sink->multi_iface);
1528 g_value_set_boxed (value, rtsp_client_sink->sdes);
1530 case PROP_TLS_VALIDATION_FLAGS:
1531 g_value_set_flags (value, rtsp_client_sink->tls_validation_flags);
1533 case PROP_TLS_DATABASE:
1534 g_value_set_object (value, rtsp_client_sink->tls_database);
1536 case PROP_TLS_INTERACTION:
1537 g_value_set_object (value, rtsp_client_sink->tls_interaction);
1539 case PROP_NTP_TIME_SOURCE:
1540 g_value_set_enum (value, rtsp_client_sink->ntp_time_source);
1542 case PROP_USER_AGENT:
1543 g_value_set_string (value, rtsp_client_sink->user_agent);
1546 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1551 static const gchar *
1552 get_aggregate_control (GstRTSPClientSink * sink)
1557 base = sink->control;
1558 else if (sink->content_base)
1559 base = sink->content_base;
1560 else if (sink->conninfo.url_str)
1561 base = sink->conninfo.url_str;
1569 gst_rtsp_client_sink_cleanup (GstRTSPClientSink * sink)
1573 GST_DEBUG_OBJECT (sink, "cleanup");
1575 gst_element_set_state (GST_ELEMENT (sink->internal_bin), GST_STATE_NULL);
1577 /* Clean up any left over stream objects */
1578 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
1579 GstRTSPStreamContext *context = (GstRTSPStreamContext *) (walk->data);
1580 if (context->stream_transport) {
1581 gst_rtsp_stream_transport_set_active (context->stream_transport, FALSE);
1582 gst_object_unref (context->stream_transport);
1583 context->stream_transport = NULL;
1586 if (context->stream) {
1587 if (context->joined) {
1588 gst_rtsp_stream_leave_bin (context->stream,
1589 GST_BIN (sink->internal_bin), sink->rtpbin);
1590 context->joined = FALSE;
1592 gst_object_unref (context->stream);
1593 context->stream = NULL;
1596 if (context->srtcpparams) {
1597 gst_caps_unref (context->srtcpparams);
1598 context->srtcpparams = NULL;
1600 g_free (context->conninfo.location);
1601 context->conninfo.location = NULL;
1605 gst_element_set_state (sink->rtpbin, GST_STATE_NULL);
1606 gst_bin_remove (GST_BIN_CAST (sink->internal_bin), sink->rtpbin);
1607 sink->rtpbin = NULL;
1610 g_free (sink->content_base);
1611 sink->content_base = NULL;
1613 g_free (sink->control);
1614 sink->control = NULL;
1617 gst_rtsp_range_free (sink->range);
1620 /* don't clear the SDP when it was used in the url */
1621 if (sink->uri_sdp && !sink->from_sdp) {
1622 gst_sdp_message_free (sink->uri_sdp);
1623 sink->uri_sdp = NULL;
1626 if (sink->provided_clock) {
1627 gst_object_unref (sink->provided_clock);
1628 sink->provided_clock = NULL;
1631 g_free (sink->server_ip);
1632 sink->server_ip = NULL;
1634 sink->next_pad_id = 0;
1635 sink->next_dyn_pt = 96;
1638 static GstRTSPResult
1639 gst_rtsp_client_sink_connection_send (GstRTSPClientSink * sink,
1640 GstRTSPConnInfo * conninfo, GstRTSPMessage * message, GTimeVal * timeout)
1644 if (conninfo->connection) {
1645 g_mutex_lock (&conninfo->send_lock);
1646 ret = gst_rtsp_connection_send (conninfo->connection, message, timeout);
1647 g_mutex_unlock (&conninfo->send_lock);
1649 ret = GST_RTSP_ERROR;
1655 static GstRTSPResult
1656 gst_rtsp_client_sink_connection_receive (GstRTSPClientSink * sink,
1657 GstRTSPConnInfo * conninfo, GstRTSPMessage * message, GTimeVal * timeout)
1661 if (conninfo->connection) {
1662 g_mutex_lock (&conninfo->recv_lock);
1663 ret = gst_rtsp_connection_receive (conninfo->connection, message, timeout);
1664 g_mutex_unlock (&conninfo->recv_lock);
1666 ret = GST_RTSP_ERROR;
1673 accept_certificate_cb (GTlsConnection * conn, GTlsCertificate * peer_cert,
1674 GTlsCertificateFlags errors, gpointer user_data)
1676 GstRTSPClientSink *sink = user_data;
1677 gboolean accept = FALSE;
1679 g_signal_emit (sink, gst_rtsp_client_sink_signals[SIGNAL_ACCEPT_CERTIFICATE],
1680 0, conn, peer_cert, errors, &accept);
1685 static GstRTSPResult
1686 gst_rtsp_conninfo_connect (GstRTSPClientSink * sink, GstRTSPConnInfo * info,
1691 if (info->connection == NULL) {
1692 if (info->url == NULL) {
1693 GST_DEBUG_OBJECT (sink, "parsing uri (%s)...", info->location);
1694 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
1698 /* create connection */
1699 GST_DEBUG_OBJECT (sink, "creating connection (%s)...", info->location);
1700 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
1701 goto could_not_create;
1704 g_free (info->url_str);
1705 info->url_str = gst_rtsp_url_get_request_uri (info->url);
1707 GST_DEBUG_OBJECT (sink, "sanitized uri %s", info->url_str);
1709 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
1710 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
1711 sink->tls_validation_flags))
1712 GST_WARNING_OBJECT (sink, "Unable to set TLS validation flags");
1714 if (sink->tls_database)
1715 gst_rtsp_connection_set_tls_database (info->connection,
1716 sink->tls_database);
1718 if (sink->tls_interaction)
1719 gst_rtsp_connection_set_tls_interaction (info->connection,
1720 sink->tls_interaction);
1722 gst_rtsp_connection_set_accept_certificate_func (info->connection,
1723 accept_certificate_cb, sink, NULL);
1726 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
1727 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
1729 if (sink->proxy_host) {
1730 GST_DEBUG_OBJECT (sink, "setting proxy %s:%d", sink->proxy_host,
1732 gst_rtsp_connection_set_proxy (info->connection, sink->proxy_host,
1737 if (!info->connected) {
1740 GST_ELEMENT_PROGRESS (sink, CONTINUE, "connect",
1741 ("Connecting to %s", info->location));
1742 GST_DEBUG_OBJECT (sink, "connecting (%s)...", info->location);
1744 gst_rtsp_connection_connect (info->connection,
1745 sink->ptcp_timeout)) < 0)
1746 goto could_not_connect;
1748 info->connected = TRUE;
1755 GST_ERROR_OBJECT (sink, "No valid RTSP URL was provided");
1760 gchar *str = gst_rtsp_strresult (res);
1761 GST_ERROR_OBJECT (sink, "Could not create connection. (%s)", str);
1767 gchar *str = gst_rtsp_strresult (res);
1768 GST_ERROR_OBJECT (sink, "Could not connect to server. (%s)", str);
1774 static GstRTSPResult
1775 gst_rtsp_conninfo_close (GstRTSPClientSink * sink, GstRTSPConnInfo * info,
1778 GST_RTSP_STATE_LOCK (sink);
1779 if (info->connected) {
1780 GST_DEBUG_OBJECT (sink, "closing connection...");
1781 gst_rtsp_connection_close (info->connection);
1782 info->connected = FALSE;
1784 if (free && info->connection) {
1785 /* free connection */
1786 GST_DEBUG_OBJECT (sink, "freeing connection...");
1787 gst_rtsp_connection_free (info->connection);
1788 info->connection = NULL;
1790 GST_RTSP_STATE_UNLOCK (sink);
1794 static GstRTSPResult
1795 gst_rtsp_conninfo_reconnect (GstRTSPClientSink * sink, GstRTSPConnInfo * info,
1800 GST_DEBUG_OBJECT (sink, "reconnecting connection...");
1801 gst_rtsp_conninfo_close (sink, info, FALSE);
1802 res = gst_rtsp_conninfo_connect (sink, info, async);
1808 gst_rtsp_client_sink_connection_flush (GstRTSPClientSink * sink, gboolean flush)
1812 GST_DEBUG_OBJECT (sink, "set flushing %d", flush);
1813 g_mutex_lock (&sink->preroll_lock);
1814 if (sink->conninfo.connection && sink->conninfo.flushing != flush) {
1815 GST_DEBUG_OBJECT (sink, "connection flush");
1816 gst_rtsp_connection_flush (sink->conninfo.connection, flush);
1817 sink->conninfo.flushing = flush;
1819 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
1820 GstRTSPStreamContext *stream = (GstRTSPStreamContext *) walk->data;
1821 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
1822 GST_DEBUG_OBJECT (sink, "stream %p flush", stream);
1823 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
1824 stream->conninfo.flushing = flush;
1827 g_cond_broadcast (&sink->preroll_cond);
1828 g_mutex_unlock (&sink->preroll_lock);
1831 static GstRTSPResult
1832 gst_rtsp_client_sink_init_request (GstRTSPClientSink * sink,
1833 GstRTSPMessage * msg, GstRTSPMethod method, const gchar * uri)
1837 res = gst_rtsp_message_init_request (msg, method, uri);
1841 /* set user-agent */
1842 if (sink->user_agent)
1843 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_USER_AGENT,
1849 /* FIXME, handle server request, reply with OK, for now */
1850 static GstRTSPResult
1851 gst_rtsp_client_sink_handle_request (GstRTSPClientSink * sink,
1852 GstRTSPConnInfo * conninfo, GstRTSPMessage * request)
1854 GstRTSPMessage response = { 0 };
1857 GST_DEBUG_OBJECT (sink, "got server request message");
1860 gst_rtsp_message_dump (request);
1862 /* default implementation, send OK */
1863 GST_DEBUG_OBJECT (sink, "prepare OK reply");
1865 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
1870 /* let app parse and reply */
1871 g_signal_emit (sink, gst_rtsp_client_sink_signals[SIGNAL_HANDLE_REQUEST],
1872 0, request, &response);
1875 gst_rtsp_message_dump (&response);
1877 res = gst_rtsp_client_sink_connection_send (sink, conninfo, &response, NULL);
1881 gst_rtsp_message_unset (&response);
1888 gst_rtsp_message_unset (&response);
1893 /* send server keep-alive */
1894 static GstRTSPResult
1895 gst_rtsp_client_sink_send_keep_alive (GstRTSPClientSink * sink)
1897 GstRTSPMessage request = { 0 };
1899 GstRTSPMethod method;
1900 const gchar *control;
1902 if (sink->do_rtsp_keep_alive == FALSE) {
1903 GST_DEBUG_OBJECT (sink, "do-rtsp-keep-alive is FALSE, not sending.");
1904 gst_rtsp_connection_reset_timeout (sink->conninfo.connection);
1908 GST_DEBUG_OBJECT (sink, "creating server keep-alive");
1910 /* find a method to use for keep-alive */
1911 if (sink->methods & GST_RTSP_GET_PARAMETER)
1912 method = GST_RTSP_GET_PARAMETER;
1914 method = GST_RTSP_OPTIONS;
1916 control = get_aggregate_control (sink);
1917 if (control == NULL)
1920 res = gst_rtsp_client_sink_init_request (sink, &request, method, control);
1925 gst_rtsp_message_dump (&request);
1928 gst_rtsp_client_sink_connection_send (sink, &sink->conninfo,
1933 gst_rtsp_connection_reset_timeout (sink->conninfo.connection);
1934 gst_rtsp_message_unset (&request);
1941 GST_WARNING_OBJECT (sink, "no control url to send keepalive");
1946 gchar *str = gst_rtsp_strresult (res);
1948 gst_rtsp_message_unset (&request);
1949 GST_ELEMENT_WARNING (sink, RESOURCE, WRITE, (NULL),
1950 ("Could not send keep-alive. (%s)", str));
1956 static GstFlowReturn
1957 gst_rtsp_client_sink_loop_rx (GstRTSPClientSink * sink)
1960 GstRTSPMessage message = { 0 };
1964 GTimeVal tv_timeout;
1966 /* get the next timeout interval */
1967 gst_rtsp_connection_next_timeout (sink->conninfo.connection, &tv_timeout);
1969 GST_DEBUG_OBJECT (sink, "doing receive with timeout %d seconds",
1970 (gint) tv_timeout.tv_sec);
1972 gst_rtsp_message_unset (&message);
1974 /* we should continue reading the TCP socket because the server might
1975 * send us requests. When the session timeout expires, we need to send a
1976 * keep-alive request to keep the session open. */
1978 gst_rtsp_client_sink_connection_receive (sink,
1979 &sink->conninfo, &message, &tv_timeout);
1983 GST_DEBUG_OBJECT (sink, "we received a server message");
1985 case GST_RTSP_EINTR:
1986 /* we got interrupted, see what we have to do */
1988 case GST_RTSP_ETIMEOUT:
1989 /* send keep-alive, ignore the result, a warning will be posted. */
1990 GST_DEBUG_OBJECT (sink, "timeout, sending keep-alive");
1992 gst_rtsp_client_sink_send_keep_alive (sink)) == GST_RTSP_EINTR)
1996 /* server closed the connection. not very fatal for UDP, reconnect and
1997 * see what happens. */
1998 GST_ELEMENT_WARNING (sink, RESOURCE, READ, (NULL),
1999 ("The server closed the connection."));
2000 if (sink->udp_reconnect) {
2002 gst_rtsp_conninfo_reconnect (sink, &sink->conninfo,
2011 GST_DEBUG_OBJECT (sink, "An ethernet problem occured.");
2013 GST_ELEMENT_WARNING (sink, RESOURCE, READ, (NULL),
2014 ("Unhandled return value %d.", res));
2018 switch (message.type) {
2019 case GST_RTSP_MESSAGE_REQUEST:
2020 /* server sends us a request message, handle it */
2022 gst_rtsp_client_sink_handle_request (sink,
2023 &sink->conninfo, &message);
2024 if (res == GST_RTSP_EEOF)
2027 goto handle_request_failed;
2029 case GST_RTSP_MESSAGE_RESPONSE:
2030 /* we ignore response and data messages */
2031 GST_DEBUG_OBJECT (sink, "ignoring response message");
2033 gst_rtsp_message_dump (&message);
2034 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
2035 GST_DEBUG_OBJECT (sink, "but is Unauthorized response ...");
2036 if (gst_rtsp_client_sink_setup_auth (sink, &message) && !(retry++)) {
2037 GST_DEBUG_OBJECT (sink, "so retrying keep-alive");
2039 gst_rtsp_client_sink_send_keep_alive (sink)) ==
2047 case GST_RTSP_MESSAGE_DATA:
2048 /* we ignore response and data messages */
2049 GST_DEBUG_OBJECT (sink, "ignoring data message");
2052 GST_WARNING_OBJECT (sink, "ignoring unknown message type %d",
2057 g_assert_not_reached ();
2059 /* we get here when the connection got interrupted */
2062 gst_rtsp_message_unset (&message);
2063 GST_DEBUG_OBJECT (sink, "got interrupted");
2064 return GST_FLOW_FLUSHING;
2068 gchar *str = gst_rtsp_strresult (res);
2071 sink->conninfo.connected = FALSE;
2072 if (res != GST_RTSP_EINTR) {
2073 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE, (NULL),
2074 ("Could not connect to server. (%s)", str));
2076 ret = GST_FLOW_ERROR;
2078 ret = GST_FLOW_FLUSHING;
2084 gchar *str = gst_rtsp_strresult (res);
2086 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
2087 ("Could not receive message. (%s)", str));
2089 return GST_FLOW_ERROR;
2091 handle_request_failed:
2093 gchar *str = gst_rtsp_strresult (res);
2096 gst_rtsp_message_unset (&message);
2097 if (res != GST_RTSP_EINTR) {
2098 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
2099 ("Could not handle server message. (%s)", str));
2101 ret = GST_FLOW_ERROR;
2103 ret = GST_FLOW_FLUSHING;
2109 GST_DEBUG_OBJECT (sink, "we got an eof from the server");
2110 GST_ELEMENT_WARNING (sink, RESOURCE, READ, (NULL),
2111 ("The server closed the connection."));
2112 sink->conninfo.connected = FALSE;
2113 gst_rtsp_message_unset (&message);
2114 return GST_FLOW_EOS;
2118 static GstRTSPResult
2119 gst_rtsp_client_sink_reconnect (GstRTSPClientSink * sink, gboolean async)
2121 GstRTSPResult res = GST_RTSP_OK;
2122 gboolean restart = FALSE;
2124 GST_DEBUG_OBJECT (sink, "doing reconnect");
2126 GST_FIXME_OBJECT (sink, "Reconnection is not yet implemented");
2128 /* no need to restart, we're done */
2132 /* we can try only TCP now */
2133 sink->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
2135 /* close and cleanup our state */
2136 if ((res = gst_rtsp_client_sink_close (sink, async, FALSE)) < 0)
2139 /* see if we have TCP left to try. Also don't try TCP when we were configured
2141 if (!(sink->protocols & GST_RTSP_LOWER_TRANS_TCP) || sink->from_sdp)
2144 /* We post a warning message now to inform the user
2145 * that nothing happened. It's most likely a firewall thing. */
2146 GST_ELEMENT_WARNING (sink, RESOURCE, READ, (NULL),
2147 ("Could not receive any UDP packets for %.4f seconds, maybe your "
2148 "firewall is blocking it. Retrying using a TCP connection.",
2149 gst_guint64_to_gdouble (sink->udp_timeout / 1000000.0)));
2151 /* open new connection using tcp */
2152 if (gst_rtsp_client_sink_open (sink, async) < 0)
2155 /* start recording */
2156 if (gst_rtsp_client_sink_record (sink, async) < 0)
2165 sink->cur_protocols = 0;
2166 /* no transport possible, post an error and stop */
2167 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
2168 ("Could not receive any UDP packets for %.4f seconds, maybe your "
2169 "firewall is blocking it. No other protocols to try.",
2170 gst_guint64_to_gdouble (sink->udp_timeout / 1000000.0)));
2171 return GST_RTSP_ERROR;
2175 GST_DEBUG_OBJECT (sink, "open failed");
2180 GST_DEBUG_OBJECT (sink, "play failed");
2186 gst_rtsp_client_sink_loop_start_cmd (GstRTSPClientSink * sink, gint cmd)
2190 GST_ELEMENT_PROGRESS (sink, START, "open", ("Opening Stream"));
2193 GST_ELEMENT_PROGRESS (sink, START, "request", ("Sending RECORD request"));
2196 GST_ELEMENT_PROGRESS (sink, START, "request", ("Sending PAUSE request"));
2199 GST_ELEMENT_PROGRESS (sink, START, "close", ("Closing Stream"));
2207 gst_rtsp_client_sink_loop_complete_cmd (GstRTSPClientSink * sink, gint cmd)
2211 GST_ELEMENT_PROGRESS (sink, COMPLETE, "open", ("Opened Stream"));
2214 GST_ELEMENT_PROGRESS (sink, COMPLETE, "request", ("Sent RECORD request"));
2217 GST_ELEMENT_PROGRESS (sink, COMPLETE, "request", ("Sent PAUSE request"));
2220 GST_ELEMENT_PROGRESS (sink, COMPLETE, "close", ("Closed Stream"));
2228 gst_rtsp_client_sink_loop_cancel_cmd (GstRTSPClientSink * sink, gint cmd)
2232 GST_ELEMENT_PROGRESS (sink, CANCELED, "open", ("Open canceled"));
2235 GST_ELEMENT_PROGRESS (sink, CANCELED, "request", ("RECORD canceled"));
2238 GST_ELEMENT_PROGRESS (sink, CANCELED, "request", ("PAUSE canceled"));
2241 GST_ELEMENT_PROGRESS (sink, CANCELED, "close", ("Close canceled"));
2249 gst_rtsp_client_sink_loop_error_cmd (GstRTSPClientSink * sink, gint cmd)
2253 GST_ELEMENT_PROGRESS (sink, ERROR, "open", ("Open failed"));
2256 GST_ELEMENT_PROGRESS (sink, ERROR, "request", ("RECORD failed"));
2259 GST_ELEMENT_PROGRESS (sink, ERROR, "request", ("PAUSE failed"));
2262 GST_ELEMENT_PROGRESS (sink, ERROR, "close", ("Close failed"));
2270 gst_rtsp_client_sink_loop_end_cmd (GstRTSPClientSink * sink, gint cmd,
2273 if (ret == GST_RTSP_OK)
2274 gst_rtsp_client_sink_loop_complete_cmd (sink, cmd);
2275 else if (ret == GST_RTSP_EINTR)
2276 gst_rtsp_client_sink_loop_cancel_cmd (sink, cmd);
2278 gst_rtsp_client_sink_loop_error_cmd (sink, cmd);
2282 gst_rtsp_client_sink_loop_send_cmd (GstRTSPClientSink * sink, gint cmd,
2286 gboolean flushed = FALSE;
2288 /* start new request */
2289 gst_rtsp_client_sink_loop_start_cmd (sink, cmd);
2291 GST_DEBUG_OBJECT (sink, "sending cmd %s", cmd_to_string (cmd));
2293 GST_OBJECT_LOCK (sink);
2294 old = sink->pending_cmd;
2295 if (old == CMD_RECONNECT) {
2296 GST_DEBUG_OBJECT (sink, "ignore, we were reconnecting");
2297 cmd = CMD_RECONNECT;
2299 if (old != CMD_WAIT) {
2300 sink->pending_cmd = CMD_WAIT;
2301 GST_OBJECT_UNLOCK (sink);
2302 /* cancel previous request */
2303 GST_DEBUG_OBJECT (sink, "cancel previous request %s", cmd_to_string (old));
2304 gst_rtsp_client_sink_loop_cancel_cmd (sink, old);
2305 GST_OBJECT_LOCK (sink);
2307 sink->pending_cmd = cmd;
2308 /* interrupt if allowed */
2309 if (sink->busy_cmd & mask) {
2310 GST_DEBUG_OBJECT (sink, "connection flush busy %s",
2311 cmd_to_string (sink->busy_cmd));
2312 gst_rtsp_client_sink_connection_flush (sink, TRUE);
2315 GST_DEBUG_OBJECT (sink, "not interrupting busy cmd %s",
2316 cmd_to_string (sink->busy_cmd));
2319 gst_task_start (sink->task);
2320 GST_OBJECT_UNLOCK (sink);
2326 gst_rtsp_client_sink_loop (GstRTSPClientSink * sink)
2330 if (!sink->conninfo.connection || !sink->conninfo.connected)
2333 ret = gst_rtsp_client_sink_loop_rx (sink);
2334 if (ret != GST_FLOW_OK)
2342 GST_WARNING_OBJECT (sink, "we are not connected");
2343 ret = GST_FLOW_FLUSHING;
2348 const gchar *reason = gst_flow_get_name (ret);
2350 GST_DEBUG_OBJECT (sink, "pausing task, reason %s", reason);
2351 gst_rtsp_client_sink_loop_send_cmd (sink, CMD_WAIT, CMD_LOOP);
2356 #ifndef GST_DISABLE_GST_DEBUG
2357 static const gchar *
2358 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
2362 while (method != 0) {
2379 /* Parse a WWW-Authenticate Response header and determine the
2380 * available authentication methods
2382 * This code should also cope with the fact that each WWW-Authenticate
2383 * header can contain multiple challenge methods + tokens
2385 * At the moment, for Basic auth, we just do a minimal check and don't
2386 * even parse out the realm */
2388 gst_rtsp_client_sink_parse_auth_hdr (GstRTSPMessage * response,
2389 GstRTSPAuthMethod * methods, GstRTSPConnection * conn, gboolean * stale)
2391 GstRTSPAuthCredential **credentials, **credential;
2393 g_return_if_fail (response != NULL);
2394 g_return_if_fail (methods != NULL);
2395 g_return_if_fail (stale != NULL);
2398 gst_rtsp_message_parse_auth_credentials (response,
2399 GST_RTSP_HDR_WWW_AUTHENTICATE);
2403 credential = credentials;
2404 while (*credential) {
2405 if ((*credential)->scheme == GST_RTSP_AUTH_BASIC) {
2406 *methods |= GST_RTSP_AUTH_BASIC;
2407 } else if ((*credential)->scheme == GST_RTSP_AUTH_DIGEST) {
2408 GstRTSPAuthParam **param = (*credential)->params;
2410 *methods |= GST_RTSP_AUTH_DIGEST;
2412 gst_rtsp_connection_clear_auth_params (conn);
2416 if (strcmp ((*param)->name, "stale") == 0
2417 && g_ascii_strcasecmp ((*param)->value, "TRUE") == 0)
2419 gst_rtsp_connection_set_auth_param (conn, (*param)->name,
2428 gst_rtsp_auth_credentials_free (credentials);
2432 * gst_rtsp_client_sink_setup_auth:
2433 * @src: the rtsp source
2435 * Configure a username and password and auth method on the
2436 * connection object based on a response we received from the
2439 * Currently, this requires that a username and password were supplied
2440 * in the uri. In the future, they may be requested on demand by sending
2441 * a message up the bus.
2443 * Returns: TRUE if authentication information could be set up correctly.
2446 gst_rtsp_client_sink_setup_auth (GstRTSPClientSink * sink,
2447 GstRTSPMessage * response)
2451 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
2452 GstRTSPAuthMethod method;
2453 GstRTSPResult auth_result;
2455 GstRTSPConnection *conn;
2456 gboolean stale = FALSE;
2458 conn = sink->conninfo.connection;
2460 /* Identify the available auth methods and see if any are supported */
2461 gst_rtsp_client_sink_parse_auth_hdr (response, &avail_methods, conn, &stale);
2463 if (avail_methods == GST_RTSP_AUTH_NONE)
2464 goto no_auth_available;
2466 /* For digest auth, if the response indicates that the session
2467 * data are stale, we just update them in the connection object and
2468 * return TRUE to retry the request */
2470 sink->tried_url_auth = FALSE;
2472 url = gst_rtsp_connection_get_url (conn);
2474 /* Do we have username and password available? */
2475 if (url != NULL && !sink->tried_url_auth && url->user != NULL
2476 && url->passwd != NULL) {
2479 sink->tried_url_auth = TRUE;
2480 GST_DEBUG_OBJECT (sink,
2481 "Attempting authentication using credentials from the URL");
2483 user = sink->user_id;
2484 pass = sink->user_pw;
2485 GST_DEBUG_OBJECT (sink,
2486 "Attempting authentication using credentials from the properties");
2489 /* FIXME: If the url didn't contain username and password or we tried them
2490 * already, request a username and passwd from the application via some kind
2491 * of credentials request message */
2493 /* If we don't have a username and passwd at this point, bail out. */
2494 if (user == NULL || pass == NULL)
2497 /* Try to configure for each available authentication method, strongest to
2499 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
2500 /* Check if this method is available on the server */
2501 if ((method & avail_methods) == 0)
2504 /* Pass the credentials to the connection to try on the next request */
2505 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
2506 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
2507 * ignore it and end up retrying later */
2508 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
2509 GST_DEBUG_OBJECT (sink, "Attempting %s authentication",
2510 gst_rtsp_auth_method_to_string (method));
2515 if (method == GST_RTSP_AUTH_NONE)
2516 goto no_auth_available;
2522 /* Output an error indicating that we couldn't connect because there were
2523 * no supported authentication protocols */
2524 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ, (NULL),
2525 ("No supported authentication protocol was found"));
2530 /* We don't fire an error message, we just return FALSE and let the
2531 * normal NOT_AUTHORIZED error be propagated */
2536 static GstRTSPResult
2537 gst_rtsp_client_sink_try_send (GstRTSPClientSink * sink,
2538 GstRTSPConnInfo * conninfo, GstRTSPMessage * request,
2539 GstRTSPMessage * response, GstRTSPStatusCode * code)
2542 GstRTSPStatusCode thecode;
2543 gchar *content_base = NULL;
2547 GST_DEBUG_OBJECT (sink, "sending message");
2550 gst_rtsp_message_dump (request);
2552 g_mutex_lock (&sink->send_lock);
2555 gst_rtsp_client_sink_connection_send (sink, conninfo, request,
2556 sink->ptcp_timeout);
2558 g_mutex_unlock (&sink->send_lock);
2562 gst_rtsp_connection_reset_timeout (conninfo->connection);
2564 /* See if we should handle the response */
2565 if (response == NULL) {
2566 g_mutex_unlock (&sink->send_lock);
2571 gst_rtsp_client_sink_connection_receive (sink, conninfo, response,
2572 sink->ptcp_timeout);
2574 g_mutex_unlock (&sink->send_lock);
2580 gst_rtsp_message_dump (response);
2583 switch (response->type) {
2584 case GST_RTSP_MESSAGE_REQUEST:
2585 res = gst_rtsp_client_sink_handle_request (sink, conninfo, response);
2586 if (res == GST_RTSP_EEOF)
2589 goto handle_request_failed;
2590 g_mutex_lock (&sink->send_lock);
2592 case GST_RTSP_MESSAGE_RESPONSE:
2593 /* ok, a response is good */
2594 GST_DEBUG_OBJECT (sink, "received response message");
2596 case GST_RTSP_MESSAGE_DATA:
2597 /* we ignore data messages */
2598 GST_DEBUG_OBJECT (sink, "ignoring data message");
2599 g_mutex_lock (&sink->send_lock);
2602 GST_WARNING_OBJECT (sink, "ignoring unknown message type %d",
2604 g_mutex_lock (&sink->send_lock);
2608 thecode = response->type_data.response.code;
2610 GST_DEBUG_OBJECT (sink, "got response message %d", thecode);
2612 /* if the caller wanted the result code, we store it. */
2616 /* If the request didn't succeed, bail out before doing any more */
2617 if (thecode != GST_RTSP_STS_OK)
2620 /* store new content base if any */
2621 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
2624 g_free (sink->content_base);
2625 sink->content_base = g_strdup (content_base);
2633 gchar *str = gst_rtsp_strresult (res);
2635 if (res != GST_RTSP_EINTR) {
2636 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
2637 ("Could not send message. (%s)", str));
2639 GST_WARNING_OBJECT (sink, "send interrupted");
2648 GST_WARNING_OBJECT (sink, "server closed connection");
2649 if ((try == 0) && !sink->interleaved && sink->udp_reconnect) {
2651 /* if reconnect succeeds, try again */
2653 gst_rtsp_conninfo_reconnect (sink, &sink->conninfo,
2657 /* only try once after reconnect, then fallthrough and error out */
2660 gchar *str = gst_rtsp_strresult (res);
2662 if (res != GST_RTSP_EINTR) {
2663 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
2664 ("Could not receive message. (%s)", str));
2666 GST_WARNING_OBJECT (sink, "receive interrupted");
2674 handle_request_failed:
2676 /* ERROR was posted */
2677 gst_rtsp_message_unset (response);
2682 GST_DEBUG_OBJECT (sink, "we got an eof from the server");
2683 GST_ELEMENT_WARNING (sink, RESOURCE, READ, (NULL),
2684 ("The server closed the connection."));
2685 gst_rtsp_message_unset (response);
2691 gst_rtsp_client_sink_set_state (GstRTSPClientSink * sink, GstState state)
2693 GST_DEBUG_OBJECT (sink, "Setting internal state to %s",
2694 gst_element_state_get_name (state));
2695 gst_element_set_state (GST_ELEMENT (sink->internal_bin), state);
2699 * gst_rtsp_client_sink_send:
2700 * @src: the rtsp source
2701 * @conn: the connection to send on
2702 * @request: must point to a valid request
2703 * @response: must point to an empty #GstRTSPMessage
2704 * @code: an optional code result
2706 * send @request and retrieve the response in @response. optionally @code can be
2707 * non-NULL in which case it will contain the status code of the response.
2709 * If This function returns #GST_RTSP_OK, @response will contain a valid response
2710 * message that should be cleaned with gst_rtsp_message_unset() after usage.
2712 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
2713 * @response message) if the response code was not 200 (OK).
2715 * If the attempt results in an authentication failure, then this will attempt
2716 * to retrieve authentication credentials via gst_rtsp_client_sink_setup_auth and retry
2719 * Returns: #GST_RTSP_OK if the processing was successful.
2721 static GstRTSPResult
2722 gst_rtsp_client_sink_send (GstRTSPClientSink * sink, GstRTSPConnInfo * conninfo,
2723 GstRTSPMessage * request, GstRTSPMessage * response,
2724 GstRTSPStatusCode * code)
2726 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
2727 GstRTSPResult res = GST_RTSP_ERROR;
2730 GstRTSPMethod method = GST_RTSP_INVALID;
2736 /* make sure we don't loop forever */
2740 /* save method so we can disable it when the server complains */
2741 method = request->type_data.request.method;
2744 gst_rtsp_client_sink_try_send (sink, conninfo, request, response,
2749 case GST_RTSP_STS_UNAUTHORIZED:
2750 if (gst_rtsp_client_sink_setup_auth (sink, response)) {
2751 /* Try the request/response again after configuring the auth info
2759 } while (retry == TRUE);
2761 /* If the user requested the code, let them handle errors, otherwise
2762 * post an error below */
2765 else if (int_code != GST_RTSP_STS_OK)
2766 goto error_response;
2773 GST_DEBUG_OBJECT (sink, "got error %d", res);
2778 res = GST_RTSP_ERROR;
2780 switch (response->type_data.response.code) {
2781 case GST_RTSP_STS_NOT_FOUND:
2782 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND, (NULL), ("%s",
2783 response->type_data.response.reason));
2785 case GST_RTSP_STS_UNAUTHORIZED:
2786 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_AUTHORIZED, (NULL), ("%s",
2787 response->type_data.response.reason));
2789 case GST_RTSP_STS_MOVED_PERMANENTLY:
2790 case GST_RTSP_STS_MOVE_TEMPORARILY:
2792 gchar *new_location;
2793 GstRTSPLowerTrans transports;
2795 GST_DEBUG_OBJECT (sink, "got redirection");
2796 /* if we don't have a Location Header, we must error */
2797 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
2798 &new_location, 0) < 0)
2801 /* When we receive a redirect result, we go back to the INIT state after
2802 * parsing the new URI. The caller should do the needed steps to issue
2803 * a new setup when it detects this state change. */
2804 GST_DEBUG_OBJECT (sink, "redirection to %s", new_location);
2806 /* save current transports */
2807 if (sink->conninfo.url)
2808 transports = sink->conninfo.url->transports;
2810 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
2812 gst_rtsp_client_sink_uri_set_uri (GST_URI_HANDLER (sink), new_location,
2815 /* set old transports */
2816 if (sink->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
2817 sink->conninfo.url->transports = transports;
2819 sink->need_redirect = TRUE;
2820 sink->state = GST_RTSP_STATE_INIT;
2824 case GST_RTSP_STS_NOT_ACCEPTABLE:
2825 case GST_RTSP_STS_NOT_IMPLEMENTED:
2826 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
2827 GST_WARNING_OBJECT (sink, "got NOT IMPLEMENTED, disable method %s",
2828 gst_rtsp_method_as_text (method));
2829 sink->methods &= ~method;
2833 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
2834 ("Got error response: %d (%s).", response->type_data.response.code,
2835 response->type_data.response.reason));
2838 /* if we return ERROR we should unset the response ourselves */
2839 if (res == GST_RTSP_ERROR)
2840 gst_rtsp_message_unset (response);
2846 /* parse the response and collect all the supported methods. We need this
2847 * information so that we don't try to send an unsupported request to the
2851 gst_rtsp_client_sink_parse_methods (GstRTSPClientSink * sink,
2852 GstRTSPMessage * response)
2854 GstRTSPHeaderField field;
2858 /* reset supported methods */
2861 /* Try Allow Header first */
2862 field = GST_RTSP_HDR_ALLOW;
2865 gst_rtsp_message_get_header (response, field, &respoptions, indx);
2866 if (indx == 0 && !respoptions) {
2867 /* if no Allow header was found then try the Public header... */
2868 field = GST_RTSP_HDR_PUBLIC;
2869 gst_rtsp_message_get_header (response, field, &respoptions, indx);
2874 sink->methods |= gst_rtsp_options_from_text (respoptions);
2879 if (sink->methods == 0) {
2880 /* neither Allow nor Public are required, assume the server supports
2881 * at least SETUP. */
2882 GST_DEBUG_OBJECT (sink, "could not get OPTIONS");
2883 sink->methods = GST_RTSP_SETUP;
2886 /* Even if the server replied, and didn't say it supports
2887 * RECORD|ANNOUNCE, try anyway by assuming it does */
2888 sink->methods |= GST_RTSP_ANNOUNCE | GST_RTSP_RECORD;
2890 if (!(sink->methods & GST_RTSP_SETUP))
2898 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ, (NULL),
2899 ("Server does not support SETUP."));
2904 static GstRTSPResult
2905 gst_rtsp_client_sink_connect_to_server (GstRTSPClientSink * sink,
2909 GstRTSPMessage request = { 0 };
2910 GstRTSPMessage response = { 0 };
2911 GSocket *conn_socket;
2915 sink->need_redirect = FALSE;
2917 /* can't continue without a valid url */
2918 if (G_UNLIKELY (sink->conninfo.url == NULL)) {
2919 res = GST_RTSP_EINVAL;
2922 sink->tried_url_auth = FALSE;
2924 if ((res = gst_rtsp_conninfo_connect (sink, &sink->conninfo, async)) < 0)
2925 goto connect_failed;
2927 conn_socket = gst_rtsp_connection_get_read_socket (sink->conninfo.connection);
2928 sa = g_socket_get_remote_address (conn_socket, NULL);
2929 ia = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (sa));
2931 sink->server_ip = g_inet_address_to_string (ia);
2933 g_object_unref (sa);
2935 /* create OPTIONS */
2936 GST_DEBUG_OBJECT (sink, "create options...");
2938 gst_rtsp_client_sink_init_request (sink, &request, GST_RTSP_OPTIONS,
2939 sink->conninfo.url_str);
2941 goto create_request_failed;
2944 GST_DEBUG_OBJECT (sink, "send options...");
2947 GST_ELEMENT_PROGRESS (sink, CONTINUE, "open",
2948 ("Retrieving server options"));
2951 gst_rtsp_client_sink_send (sink, &sink->conninfo, &request,
2952 &response, NULL)) < 0)
2956 if (!gst_rtsp_client_sink_parse_methods (sink, &response))
2959 /* FIXME: Do we need to handle REDIRECT responses for OPTIONS? */
2961 /* clean up any messages */
2962 gst_rtsp_message_unset (&request);
2963 gst_rtsp_message_unset (&response);
2970 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND, (NULL),
2971 ("No valid RTSP URL was provided"));
2976 gchar *str = gst_rtsp_strresult (res);
2978 if (res != GST_RTSP_EINTR) {
2979 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE, (NULL),
2980 ("Failed to connect. (%s)", str));
2982 GST_WARNING_OBJECT (sink, "connect interrupted");
2987 create_request_failed:
2989 gchar *str = gst_rtsp_strresult (res);
2991 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
2992 ("Could not create request. (%s)", str));
2998 /* Don't post a message - the rtsp_send method will have
2999 * taken care of it because we passed NULL for the response code */
3004 /* error was posted */
3005 res = GST_RTSP_ERROR;
3010 if (sink->conninfo.connection) {
3011 GST_DEBUG_OBJECT (sink, "free connection");
3012 gst_rtsp_conninfo_close (sink, &sink->conninfo, TRUE);
3014 gst_rtsp_message_unset (&request);
3015 gst_rtsp_message_unset (&response);
3020 static GstRTSPResult
3021 gst_rtsp_client_sink_open (GstRTSPClientSink * sink, gboolean async)
3026 GST_RTSP_SETUP | GST_RTSP_RECORD | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
3028 if ((ret = gst_rtsp_client_sink_connect_to_server (sink, async)) < 0)
3032 gst_rtsp_client_sink_loop_end_cmd (sink, CMD_OPEN, ret);
3034 /* Collect all our input streams and create
3035 * stream objects before actually returning */
3036 gst_rtsp_client_sink_collect_streams (sink);
3043 GST_WARNING_OBJECT (sink, "Failed to connect to server");
3044 sink->open_error = TRUE;
3046 gst_rtsp_client_sink_loop_end_cmd (sink, CMD_OPEN, ret);
3051 static GstRTSPResult
3052 gst_rtsp_client_sink_close (GstRTSPClientSink * sink, gboolean async,
3053 gboolean only_close)
3055 GstRTSPMessage request = { 0 };
3056 GstRTSPMessage response = { 0 };
3057 GstRTSPResult res = GST_RTSP_OK;
3059 const gchar *control;
3061 GST_DEBUG_OBJECT (sink, "TEARDOWN...");
3063 gst_rtsp_client_sink_set_state (sink, GST_STATE_NULL);
3065 if (sink->state < GST_RTSP_STATE_READY) {
3066 GST_DEBUG_OBJECT (sink, "not ready, doing cleanup");
3073 /* construct a control url */
3074 control = get_aggregate_control (sink);
3076 if (!(sink->methods & (GST_RTSP_RECORD | GST_RTSP_TEARDOWN)))
3079 /* stop streaming */
3080 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3081 GstRTSPStreamContext *context = (GstRTSPStreamContext *) walk->data;
3083 if (context->stream_transport)
3084 gst_rtsp_stream_transport_set_active (context->stream_transport, FALSE);
3086 if (context->joined) {
3087 gst_rtsp_stream_leave_bin (context->stream, GST_BIN (sink->internal_bin),
3089 context->joined = FALSE;
3093 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3094 GstRTSPStreamContext *context = (GstRTSPStreamContext *) walk->data;
3095 const gchar *setup_url;
3096 GstRTSPConnInfo *info;
3098 GST_DEBUG_OBJECT (sink, "Looking at stream %p for teardown",
3101 /* try aggregate control first but do non-aggregate control otherwise */
3103 setup_url = control;
3104 else if ((setup_url = context->conninfo.location) == NULL) {
3105 GST_DEBUG_OBJECT (sink, "Skipping TEARDOWN stream %p - no setup URL",
3110 if (sink->conninfo.connection) {
3111 info = &sink->conninfo;
3112 } else if (context->conninfo.connection) {
3113 info = &context->conninfo;
3117 if (!info->connected)
3121 GST_DEBUG_OBJECT (sink, "Sending teardown for stream %p at URL %s",
3122 context->stream, setup_url);
3124 gst_rtsp_client_sink_init_request (sink, &request, GST_RTSP_TEARDOWN,
3127 goto create_request_failed;
3130 GST_ELEMENT_PROGRESS (sink, CONTINUE, "close", ("Closing stream"));
3133 gst_rtsp_client_sink_send (sink, info, &request,
3134 &response, NULL)) < 0)
3137 /* FIXME, parse result? */
3138 gst_rtsp_message_unset (&request);
3139 gst_rtsp_message_unset (&response);
3142 /* early exit when we did aggregate control */
3148 /* close connections */
3149 GST_DEBUG_OBJECT (sink, "closing connection...");
3150 gst_rtsp_conninfo_close (sink, &sink->conninfo, TRUE);
3151 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3152 GstRTSPStreamContext *stream = (GstRTSPStreamContext *) walk->data;
3153 gst_rtsp_conninfo_close (sink, &stream->conninfo, TRUE);
3157 gst_rtsp_client_sink_cleanup (sink);
3159 sink->state = GST_RTSP_STATE_INVALID;
3162 gst_rtsp_client_sink_loop_end_cmd (sink, CMD_CLOSE, res);
3167 create_request_failed:
3169 gchar *str = gst_rtsp_strresult (res);
3171 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
3172 ("Could not create request. (%s)", str));
3178 gchar *str = gst_rtsp_strresult (res);
3180 gst_rtsp_message_unset (&request);
3181 if (res != GST_RTSP_EINTR) {
3182 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
3183 ("Could not send message. (%s)", str));
3185 GST_WARNING_OBJECT (sink, "TEARDOWN interrupted");
3192 GST_DEBUG_OBJECT (sink,
3193 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
3199 gst_rtsp_client_sink_configure_manager (GstRTSPClientSink * sink)
3202 GstStateChangeReturn ret;
3204 rtpbin = sink->rtpbin;
3206 if (rtpbin == NULL) {
3207 GObjectClass *klass;
3209 rtpbin = gst_element_factory_make ("rtpbin", NULL);
3213 gst_bin_add (GST_BIN_CAST (sink->internal_bin), rtpbin);
3215 sink->rtpbin = rtpbin;
3217 /* Any more settings we should configure on rtpbin here? */
3218 g_object_set (sink->rtpbin, "latency", sink->latency, NULL);
3220 klass = G_OBJECT_GET_CLASS (G_OBJECT (rtpbin));
3222 if (g_object_class_find_property (klass, "ntp-time-source")) {
3223 g_object_set (sink->rtpbin, "ntp-time-source", sink->ntp_time_source,
3227 if (sink->sdes && g_object_class_find_property (klass, "sdes")) {
3228 g_object_set (sink->rtpbin, "sdes", sink->sdes, NULL);
3231 g_signal_emit (sink, gst_rtsp_client_sink_signals[SIGNAL_NEW_MANAGER], 0,
3235 ret = gst_element_set_state (rtpbin, GST_STATE_PAUSED);
3236 if (ret == GST_STATE_CHANGE_FAILURE)
3237 goto start_manager_failure;
3243 GST_WARNING ("no rtpbin element");
3244 g_warning ("failed to create element 'rtpbin', check your installation");
3247 start_manager_failure:
3249 GST_DEBUG_OBJECT (sink, "could not start session manager");
3250 gst_bin_remove (GST_BIN_CAST (sink->internal_bin), rtpbin);
3256 request_aux_sender (GstElement * rtpbin, guint sessid, GstRTSPClientSink * sink)
3258 GstRTSPStream *stream = NULL;
3259 GstElement *ret = NULL;
3262 GST_RTSP_STATE_LOCK (sink);
3263 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3264 GstRTSPStreamContext *context = (GstRTSPStreamContext *) walk->data;
3266 if (sessid == gst_rtsp_stream_get_index (context->stream)) {
3267 stream = context->stream;
3272 if (stream != NULL) {
3273 GST_DEBUG_OBJECT (sink, "Creating aux sender for stream %u", sessid);
3274 ret = gst_rtsp_stream_request_aux_sender (stream, sessid);
3277 GST_RTSP_STATE_UNLOCK (sink);
3283 gst_rtsp_client_sink_collect_streams (GstRTSPClientSink * sink)
3285 GstRTSPStreamContext *context;
3290 GST_DEBUG_OBJECT (sink, "Collecting stream information");
3292 if (!gst_rtsp_client_sink_configure_manager (sink))
3295 base = get_aggregate_control (sink);
3296 /* check if the base ends with / */
3297 has_slash = g_str_has_suffix (base, "/");
3299 g_mutex_lock (&sink->preroll_lock);
3300 while (sink->contexts == NULL && !sink->conninfo.flushing) {
3301 g_cond_wait (&sink->preroll_cond, &sink->preroll_lock);
3303 g_mutex_unlock (&sink->preroll_lock);
3305 /* FIXME: Need different locking - need to protect against pad releases
3306 * and potential state changes ruining things here */
3307 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3310 context = (GstRTSPStreamContext *) walk->data;
3311 if (context->stream)
3314 g_mutex_lock (&sink->preroll_lock);
3315 while (!context->prerolled && !sink->conninfo.flushing) {
3316 GST_DEBUG_OBJECT (sink, "Waiting for caps on stream %d", context->index);
3317 g_cond_wait (&sink->preroll_cond, &sink->preroll_lock);
3319 if (sink->conninfo.flushing) {
3320 g_mutex_unlock (&sink->preroll_lock);
3323 g_mutex_unlock (&sink->preroll_lock);
3325 if (context->payloader == NULL)
3328 srcpad = gst_element_get_static_pad (context->payloader, "src");
3330 GST_DEBUG_OBJECT (sink, "Creating stream object for stream %d",
3333 gst_rtsp_client_sink_create_stream (sink, context, context->payloader,
3336 /* concatenate the two strings, insert / when not present */
3337 g_free (context->conninfo.location);
3338 context->conninfo.location =
3339 g_strdup_printf ("%s%sstream=%d", base, has_slash ? "" : "/",
3342 if (sink->rtx_time > 0) {
3343 /* enable retransmission by setting rtprtxsend as the "aux" element of rtpbin */
3344 g_signal_connect (sink->rtpbin, "request-aux-sender",
3345 (GCallback) request_aux_sender, sink);
3348 if (!gst_rtsp_stream_join_bin (context->stream,
3349 GST_BIN (sink->internal_bin), sink->rtpbin, GST_STATE_PAUSED)) {
3350 goto join_bin_failed;
3352 context->joined = TRUE;
3354 /* Let the stream object receive data */
3355 gst_pad_remove_probe (srcpad, context->payloader_block_id);
3357 gst_object_unref (srcpad);
3360 /* Now wait for the preroll of the rtp bin */
3361 g_mutex_lock (&sink->preroll_lock);
3362 while (!sink->prerolled && !sink->conninfo.flushing) {
3363 GST_LOG_OBJECT (sink, "Waiting for preroll before continuing");
3364 g_cond_wait (&sink->preroll_cond, &sink->preroll_lock);
3366 GST_LOG_OBJECT (sink, "Marking streams as collected");
3367 sink->streams_collected = TRUE;
3368 g_mutex_unlock (&sink->preroll_lock);
3374 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
3375 ("Could not start stream %d", context->index));
3379 static GstRTSPResult
3380 gst_rtsp_client_sink_create_transports_string (GstRTSPClientSink * sink,
3381 GstRTSPStreamContext * context, GSocketFamily family,
3382 GstRTSPLowerTrans protocols, GstRTSPProfile profiles, gchar ** transports)
3385 GstRTSPStream *stream = context->stream;
3386 gboolean first = TRUE;
3388 /* the default RTSP transports */
3389 result = g_string_new ("RTP");
3391 while (profiles != 0) {
3393 g_string_append (result, ",RTP");
3395 if (profiles & GST_RTSP_PROFILE_SAVPF) {
3396 g_string_append (result, "/SAVPF");
3397 profiles &= ~GST_RTSP_PROFILE_SAVPF;
3398 } else if (profiles & GST_RTSP_PROFILE_SAVP) {
3399 g_string_append (result, "/SAVP");
3400 profiles &= ~GST_RTSP_PROFILE_SAVP;
3401 } else if (profiles & GST_RTSP_PROFILE_AVPF) {
3402 g_string_append (result, "/AVPF");
3403 profiles &= ~GST_RTSP_PROFILE_AVPF;
3404 } else if (profiles & GST_RTSP_PROFILE_AVP) {
3405 g_string_append (result, "/AVP");
3406 profiles &= ~GST_RTSP_PROFILE_AVP;
3408 GST_WARNING_OBJECT (sink, "Unimplemented profile(s) 0x%x", profiles);
3412 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
3415 GST_DEBUG_OBJECT (sink, "adding UDP unicast");
3416 gst_rtsp_stream_get_server_port (stream, &ports, family);
3418 g_string_append_printf (result, "/UDP;unicast;client_port=%d-%d",
3419 ports.min, ports.max);
3420 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3421 GstRTSPAddress *addr =
3422 gst_rtsp_stream_get_multicast_address (stream, family);
3424 GST_DEBUG_OBJECT (sink, "adding UDP multicast");
3425 g_string_append_printf (result, "/UDP;multicast;client_port=%d-%d",
3426 addr->port, addr->port + addr->n_ports - 1);
3427 gst_rtsp_address_free (addr);
3429 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
3430 GST_DEBUG_OBJECT (sink, "adding TCP");
3431 g_string_append_printf (result, "/TCP;unicast;interleaved=%d-%d",
3432 sink->free_channel, sink->free_channel + 1);
3435 g_string_append (result, ";mode=RECORD");
3436 /* FIXME: Support appending too:
3438 g_string_append (result, ";append");
3445 /* No valid transport could be constructed */
3446 GST_ERROR_OBJECT (sink, "No supported profiles configured");
3450 *transports = g_string_free (result, FALSE);
3452 GST_DEBUG_OBJECT (sink, "prepared transports %s", GST_STR_NULL (*transports));
3456 g_string_free (result, TRUE);
3457 return GST_RTSP_ERROR;
3461 signal_get_srtcp_params (GstRTSPClientSink * sink,
3462 GstRTSPStreamContext * context)
3464 GstCaps *caps = NULL;
3466 g_signal_emit (sink, gst_rtsp_client_sink_signals[SIGNAL_REQUEST_RTCP_KEY], 0,
3467 context->index, &caps);
3470 GST_DEBUG_OBJECT (sink, "SRTP parameters received");
3476 gst_rtsp_client_sink_stream_make_keymgmt (GstRTSPClientSink * sink,
3477 GstRTSPStreamContext * context)
3479 gchar *base64, *result = NULL;
3480 GstMIKEYMessage *mikey_msg;
3482 context->srtcpparams = signal_get_srtcp_params (sink, context);
3483 if (context->srtcpparams == NULL)
3484 context->srtcpparams = gst_rtsp_stream_get_caps (context->stream);
3486 mikey_msg = gst_mikey_message_new_from_caps (context->srtcpparams);
3490 /* add policy '0' for our SSRC */
3491 gst_rtsp_stream_get_ssrc (context->stream, &send_ssrc);
3492 GST_LOG_OBJECT (sink, "Stream %p ssrc %x", context->stream, send_ssrc);
3493 gst_mikey_message_add_cs_srtp (mikey_msg, 0, send_ssrc, 0);
3495 base64 = gst_mikey_message_base64_encode (mikey_msg);
3496 gst_mikey_message_unref (mikey_msg);
3499 result = gst_sdp_make_keymgmt (context->conninfo.location, base64);
3507 /* masks to be kept in sync with the hardcoded protocol order of preference
3509 static const guint protocol_masks[] = {
3510 GST_RTSP_LOWER_TRANS_UDP,
3511 GST_RTSP_LOWER_TRANS_UDP_MCAST,
3512 GST_RTSP_LOWER_TRANS_TCP,
3516 /* Same for profile_masks */
3517 static const guint profile_masks[] = {
3518 GST_RTSP_PROFILE_SAVPF,
3519 GST_RTSP_PROFILE_SAVP,
3520 GST_RTSP_PROFILE_AVPF,
3521 GST_RTSP_PROFILE_AVP,
3526 do_send_data (GstBuffer * buffer, guint8 channel,
3527 GstRTSPStreamContext * context)
3529 GstRTSPClientSink *sink = context->parent;
3530 GstRTSPMessage message = { 0 };
3531 GstRTSPResult res = GST_RTSP_OK;
3532 GstMapInfo map_info;
3536 gst_rtsp_message_init_data (&message, channel);
3538 /* FIXME, need some sort of iovec RTSPMessage here */
3539 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
3542 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
3545 gst_rtsp_client_sink_try_send (sink, &sink->conninfo, &message,
3548 gst_rtsp_message_steal_body (&message, &data, &usize);
3549 gst_buffer_unmap (buffer, &map_info);
3551 gst_rtsp_message_unset (&message);
3553 return res == GST_RTSP_OK;
3556 static GstRTSPResult
3557 gst_rtsp_client_sink_setup_streams (GstRTSPClientSink * sink, gboolean async)
3559 GstRTSPResult res = GST_RTSP_ERROR;
3560 GstRTSPMessage request = { 0 };
3561 GstRTSPMessage response = { 0 };
3562 GstRTSPLowerTrans protocols;
3563 GstRTSPStatusCode code;
3564 GSocketFamily family;
3566 GSocket *conn_socket;
3571 if (sink->conninfo.connection) {
3572 url = gst_rtsp_connection_get_url (sink->conninfo.connection);
3573 /* we initially allow all configured lower transports. based on the URL
3574 * transports and the replies from the server we narrow them down. */
3575 protocols = url->transports & sink->cur_protocols;
3578 protocols = sink->cur_protocols;
3584 GST_RTSP_STATE_LOCK (sink);
3586 if (G_UNLIKELY (sink->contexts == NULL))
3589 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3590 GstRTSPStreamContext *context = (GstRTSPStreamContext *) walk->data;
3591 GstRTSPStream *stream;
3593 GstRTSPConnInfo *info;
3594 GstRTSPProfile profiles;
3595 GstRTSPProfile cur_profile;
3598 guint profile_mask = 0;
3601 const GstSDPMedia *media;
3603 stream = context->stream;
3604 profiles = gst_rtsp_stream_get_profiles (stream);
3606 caps = gst_rtsp_stream_get_caps (stream);
3608 GST_DEBUG_OBJECT (sink, "skipping stream %p, no caps", stream);
3611 gst_caps_unref (caps);
3612 media = gst_sdp_message_get_media (&sink->cursdp, context->sdp_index);
3613 if (media == NULL) {
3614 GST_DEBUG_OBJECT (sink, "skipping stream %p, no SDP info", stream);
3618 /* skip setup if we have no URL for it */
3619 if (context->conninfo.location == NULL) {
3620 GST_DEBUG_OBJECT (sink, "skipping stream %p, no setup", stream);
3624 if (sink->conninfo.connection == NULL) {
3625 if (!gst_rtsp_conninfo_connect (sink, &context->conninfo, async)) {
3626 GST_DEBUG_OBJECT (sink, "skipping stream %p, failed to connect",
3630 info = &context->conninfo;
3632 info = &sink->conninfo;
3634 GST_DEBUG_OBJECT (sink, "doing setup of stream %p with %s", stream,
3635 context->conninfo.location);
3637 conn_socket = gst_rtsp_connection_get_read_socket (info->connection);
3638 sa = g_socket_get_local_address (conn_socket, NULL);
3639 family = g_socket_address_get_family (sa);
3640 g_object_unref (sa);
3643 /* first selectable profile */
3644 while (profile_masks[profile_mask]
3645 && !(profiles & profile_masks[profile_mask]))
3647 if (!profile_masks[profile_mask])
3650 /* first selectable protocol */
3651 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
3653 if (!protocol_masks[mask])
3657 GST_DEBUG_OBJECT (sink, "protocols = 0x%x, protocol mask = 0x%x", protocols,
3658 protocol_masks[mask]);
3659 /* create a string with first transport in line */
3661 cur_profile = profiles & profile_masks[profile_mask];
3662 res = gst_rtsp_client_sink_create_transports_string (sink, context, family,
3663 protocols & protocol_masks[mask], cur_profile, &transports);
3664 if (res < 0 || transports == NULL)
3665 goto setup_transport_failed;
3667 if (strlen (transports) == 0) {
3668 g_free (transports);
3669 GST_DEBUG_OBJECT (sink, "no transports found");
3675 GST_DEBUG_OBJECT (sink, "transport is %s", GST_STR_NULL (transports));
3677 /* create SETUP request */
3679 gst_rtsp_client_sink_init_request (sink, &request, GST_RTSP_SETUP,
3680 context->conninfo.location);
3682 g_free (transports);
3683 goto create_request_failed;
3686 /* select transport */
3687 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
3690 if (cur_profile == GST_RTSP_PROFILE_SAVP ||
3691 cur_profile == GST_RTSP_PROFILE_SAVPF) {
3692 hval = gst_rtsp_client_sink_stream_make_keymgmt (sink, context);
3693 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
3696 /* if the user wants a non default RTP packet size we add the blocksize
3698 if (sink->rtp_blocksize > 0) {
3699 hval = g_strdup_printf ("%d", sink->rtp_blocksize);
3700 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
3704 GST_ELEMENT_PROGRESS (sink, CONTINUE, "request", ("SETUP stream %d",
3707 /* handle the code ourselves */
3708 res = gst_rtsp_client_sink_send (sink, info, &request, &response, &code);
3713 case GST_RTSP_STS_OK:
3715 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
3716 gst_rtsp_message_unset (&request);
3717 gst_rtsp_message_unset (&response);
3719 /* Try another profile. If no more, move to the next protocol */
3721 while (profile_masks[profile_mask]
3722 && !(profiles & profile_masks[profile_mask]))
3724 if (profile_masks[profile_mask])
3727 /* select next available protocol, give up on this stream if none */
3728 /* Reset profiles to try: */
3732 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
3734 if (!protocol_masks[mask])
3739 goto response_error;
3742 /* parse response transport */
3744 gchar *resptrans = NULL;
3745 GstRTSPTransport *transport;
3747 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
3753 gst_rtsp_transport_new (&transport);
3755 /* parse transport, go to next stream on parse error */
3756 if (gst_rtsp_transport_parse (resptrans, transport) != GST_RTSP_OK) {
3757 GST_WARNING_OBJECT (sink, "failed to parse transport %s", resptrans);
3761 /* update allowed transports for other streams. once the transport of
3762 * one stream has been determined, we make sure that all other streams
3763 * are configured in the same way */
3764 switch (transport->lower_transport) {
3765 case GST_RTSP_LOWER_TRANS_TCP:
3766 GST_DEBUG_OBJECT (sink, "stream %p as TCP interleaved", stream);
3767 protocols = GST_RTSP_LOWER_TRANS_TCP;
3768 sink->interleaved = TRUE;
3769 /* update free channels */
3770 sink->free_channel =
3771 MAX (transport->interleaved.min, sink->free_channel);
3772 sink->free_channel =
3773 MAX (transport->interleaved.max, sink->free_channel);
3774 sink->free_channel++;
3776 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3777 /* only allow multicast for other streams */
3778 GST_DEBUG_OBJECT (sink, "stream %p as UDP multicast", stream);
3779 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
3781 case GST_RTSP_LOWER_TRANS_UDP:
3782 /* only allow unicast for other streams */
3783 GST_DEBUG_OBJECT (sink, "stream %p as UDP unicast", stream);
3784 protocols = GST_RTSP_LOWER_TRANS_UDP;
3785 /* Update transport with server destination if not provided by the server */
3786 if (transport->destination == NULL) {
3787 transport->destination = g_strdup (sink->server_ip);
3791 GST_DEBUG_OBJECT (sink, "stream %p unknown transport %d", stream,
3792 transport->lower_transport);
3797 GST_DEBUG ("Configuring the stream transport for stream %d",
3799 if (context->stream_transport == NULL)
3800 context->stream_transport =
3801 gst_rtsp_stream_transport_new (stream, transport);
3803 gst_rtsp_stream_transport_set_transport (context->stream_transport,
3806 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
3807 /* our callbacks to send data on this TCP connection */
3808 gst_rtsp_stream_transport_set_callbacks (context->stream_transport,
3809 (GstRTSPSendFunc) do_send_data,
3810 (GstRTSPSendFunc) do_send_data, context, NULL);
3813 /* The stream_transport now owns the transport */
3816 gst_rtsp_stream_transport_set_active (context->stream_transport, TRUE);
3820 gst_rtsp_transport_free (transport);
3821 /* clean up used RTSP messages */
3822 gst_rtsp_message_unset (&request);
3823 gst_rtsp_message_unset (&response);
3826 GST_RTSP_STATE_UNLOCK (sink);
3828 /* store the transport protocol that was configured */
3829 sink->cur_protocols = protocols;
3835 GST_RTSP_STATE_UNLOCK (sink);
3836 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
3837 ("SDP contains no streams"));
3838 return GST_RTSP_ERROR;
3840 setup_transport_failed:
3842 GST_RTSP_STATE_UNLOCK (sink);
3843 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
3844 ("Could not setup transport."));
3845 res = GST_RTSP_ERROR;
3850 GST_RTSP_STATE_UNLOCK (sink);
3851 /* no transport possible, post an error and stop */
3852 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
3853 ("Could not connect to server, no profiles left"));
3854 return GST_RTSP_ERROR;
3858 GST_RTSP_STATE_UNLOCK (sink);
3859 /* no transport possible, post an error and stop */
3860 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
3861 ("Could not connect to server, no protocols left"));
3862 return GST_RTSP_ERROR;
3866 GST_RTSP_STATE_UNLOCK (sink);
3867 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
3868 ("Server did not select transport."));
3869 res = GST_RTSP_ERROR;
3872 create_request_failed:
3874 gchar *str = gst_rtsp_strresult (res);
3876 GST_RTSP_STATE_UNLOCK (sink);
3877 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
3878 ("Could not create request. (%s)", str));
3884 gchar *str = gst_rtsp_strresult (res);
3886 GST_RTSP_STATE_UNLOCK (sink);
3887 if (res != GST_RTSP_EINTR) {
3888 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
3889 ("Could not send message. (%s)", str));
3891 GST_WARNING_OBJECT (sink, "send interrupted");
3898 const gchar *str = gst_rtsp_status_as_text (code);
3900 GST_RTSP_STATE_UNLOCK (sink);
3901 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
3902 ("Error (%d): %s", code, GST_STR_NULL (str)));
3903 res = GST_RTSP_ERROR;
3908 gst_rtsp_message_unset (&request);
3909 gst_rtsp_message_unset (&response);
3914 static GstRTSPResult
3915 gst_rtsp_client_sink_ensure_open (GstRTSPClientSink * sink, gboolean async)
3917 GstRTSPResult res = GST_RTSP_OK;
3919 if (sink->state < GST_RTSP_STATE_READY) {
3920 res = GST_RTSP_ERROR;
3921 if (sink->open_error) {
3922 GST_DEBUG_OBJECT (sink, "the stream was in error");
3926 gst_rtsp_client_sink_loop_start_cmd (sink, CMD_OPEN);
3928 if ((res = gst_rtsp_client_sink_open (sink, async)) < 0) {
3929 GST_DEBUG_OBJECT (sink, "failed to open stream");
3938 static GstRTSPResult
3939 gst_rtsp_client_sink_record (GstRTSPClientSink * sink, gboolean async)
3941 GstRTSPMessage request = { 0 };
3942 GstRTSPMessage response = { 0 };
3943 GstRTSPResult res = GST_RTSP_OK;
3945 guint sdp_index = 0;
3946 GstSDPInfo info = { 0, };
3949 gchar *sess_id, *client_ip, *str;
3952 GSocket *conn_socket;
3955 /* Wait for streams to preroll */
3956 g_mutex_lock (&sink->preroll_lock);
3957 while (sink->in_async) {
3958 GST_LOG_OBJECT (sink, "Waiting for ASYNC_DONE preroll");
3959 g_cond_wait (&sink->preroll_cond, &sink->preroll_lock);
3961 g_mutex_unlock (&sink->preroll_lock);
3963 if (sink->state == GST_RTSP_STATE_PLAYING) {
3964 /* Already recording, don't send another request */
3965 GST_LOG_OBJECT (sink, "Already in RECORD. Skipping duplicate request.");
3969 /* Send announce, then setup for all streams */
3970 gst_sdp_message_init (&sink->cursdp);
3971 sdp = &sink->cursdp;
3973 /* some standard things first */
3974 gst_sdp_message_set_version (sdp, "0");
3976 /* session ID doesn't have to be super-unique in this case */
3977 sess_id = g_strdup_printf ("%u", g_random_int ());
3979 if (sink->conninfo.connection == NULL)
3980 return GST_RTSP_ERROR;
3982 conn_socket = gst_rtsp_connection_get_read_socket (sink->conninfo.connection);
3984 sa = g_socket_get_local_address (conn_socket, NULL);
3985 ia = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (sa));
3986 client_ip = g_inet_address_to_string (ia);
3987 if (g_socket_address_get_family (sa) == G_SOCKET_FAMILY_IPV6) {
3988 info.is_ipv6 = TRUE;
3990 } else if (g_socket_address_get_family (sa) == G_SOCKET_FAMILY_IPV4)
3993 g_assert_not_reached ();
3994 g_object_unref (sa);
3996 /* FIXME: Should this actually be the server's IP or ours? */
3997 info.server_ip = sink->server_ip;
3999 gst_sdp_message_set_origin (sdp, "-", sess_id, "1", "IN", proto, client_ip);
4001 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
4002 gst_sdp_message_set_information (sdp, "rtspclientsink");
4003 gst_sdp_message_add_time (sdp, "0", "0", NULL);
4004 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
4007 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
4008 GstRTSPStreamContext *context = (GstRTSPStreamContext *) walk->data;
4010 gst_rtsp_sdp_from_stream (sdp, &info, context->stream);
4011 context->sdp_index = sdp_index++;
4017 /* send ANNOUNCE request */
4018 GST_DEBUG_OBJECT (sink, "create ANNOUNCE request...");
4020 gst_rtsp_client_sink_init_request (sink, &request, GST_RTSP_ANNOUNCE,
4021 sink->conninfo.url_str);
4023 goto create_request_failed;
4025 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CONTENT_TYPE,
4028 /* add SDP to the request body */
4029 str = gst_sdp_message_as_text (sdp);
4030 gst_rtsp_message_take_body (&request, (guint8 *) str, strlen (str));
4033 GST_DEBUG_OBJECT (sink, "sending announce...");
4036 GST_ELEMENT_PROGRESS (sink, CONTINUE, "record",
4037 ("Sending server stream info"));
4040 gst_rtsp_client_sink_send (sink, &sink->conninfo, &request,
4041 &response, NULL)) < 0)
4044 /* send setup for all streams */
4045 if ((res = gst_rtsp_client_sink_setup_streams (sink, async)) < 0)
4048 res = gst_rtsp_client_sink_init_request (sink, &request, GST_RTSP_RECORD,
4049 sink->conninfo.url_str);
4052 goto create_request_failed;
4054 #if 0 /* FIXME: Configure a range based on input segments? */
4055 if (src->need_range) {
4056 hval = gen_range_header (src, segment);
4058 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
4061 if (segment->rate != 1.0) {
4062 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
4064 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
4066 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
4068 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
4073 GST_ELEMENT_PROGRESS (sink, CONTINUE, "record", ("Starting recording"));
4075 gst_rtsp_client_sink_send (sink, &sink->conninfo, &request,
4076 &response, NULL)) < 0)
4079 #if 0 /* FIXME: Check if servers return these for record: */
4080 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
4081 * for the RTP packets. If this is not present, we assume all starts from 0...
4082 * This is info for the RTP session manager that we pass to it in caps. */
4084 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
4085 &hval, hval_idx++) == GST_RTSP_OK)
4086 gst_rtspsrc_parse_rtpinfo (src, hval);
4088 /* some servers indicate RTCP parameters in PLAY response,
4089 * rather than properly in SDP */
4090 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
4091 &hval, 0) == GST_RTSP_OK)
4092 gst_rtspsrc_handle_rtcp_interval (src, hval);
4095 gst_rtsp_client_sink_set_state (sink, GST_STATE_PLAYING);
4096 sink->state = GST_RTSP_STATE_PLAYING;
4098 /* clean up any messages */
4099 gst_rtsp_message_unset (&request);
4100 gst_rtsp_message_unset (&response);
4105 create_request_failed:
4107 gchar *str = gst_rtsp_strresult (res);
4109 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
4110 ("Could not create request. (%s)", str));
4116 /* Don't post a message - the rtsp_send method will have
4117 * taken care of it because we passed NULL for the response code */
4122 GST_ERROR_OBJECT (sink, "setup failed");
4127 if (sink->conninfo.connection) {
4128 GST_DEBUG_OBJECT (sink, "free connection");
4129 gst_rtsp_conninfo_close (sink, &sink->conninfo, TRUE);
4131 gst_rtsp_message_unset (&request);
4132 gst_rtsp_message_unset (&response);
4137 static GstRTSPResult
4138 gst_rtsp_client_sink_pause (GstRTSPClientSink * sink, gboolean async)
4140 GstRTSPResult res = GST_RTSP_OK;
4141 GstRTSPMessage request = { 0 };
4142 GstRTSPMessage response = { 0 };
4144 const gchar *control;
4146 GST_DEBUG_OBJECT (sink, "PAUSE...");
4148 if ((res = gst_rtsp_client_sink_ensure_open (sink, async)) < 0)
4151 if (!(sink->methods & GST_RTSP_PAUSE))
4154 if (sink->state == GST_RTSP_STATE_READY)
4157 if (!sink->conninfo.connection || !sink->conninfo.connected)
4160 /* construct a control url */
4161 control = get_aggregate_control (sink);
4163 /* loop over the streams. We might exit the loop early when we could do an
4164 * aggregate control */
4165 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
4166 GstRTSPStreamContext *stream = (GstRTSPStreamContext *) walk->data;
4167 GstRTSPConnInfo *info;
4168 const gchar *setup_url;
4170 /* try aggregate control first but do non-aggregate control otherwise */
4172 setup_url = control;
4173 else if ((setup_url = stream->conninfo.location) == NULL)
4176 if (sink->conninfo.connection) {
4177 info = &sink->conninfo;
4178 } else if (stream->conninfo.connection) {
4179 info = &stream->conninfo;
4185 GST_ELEMENT_PROGRESS (sink, CONTINUE, "request",
4186 ("Sending PAUSE request"));
4189 gst_rtsp_client_sink_init_request (sink, &request, GST_RTSP_PAUSE,
4191 goto create_request_failed;
4194 gst_rtsp_client_sink_send (sink, info, &request, &response,
4198 gst_rtsp_message_unset (&request);
4199 gst_rtsp_message_unset (&response);
4201 /* exit early when we did agregate control */
4206 /* change element states now */
4207 gst_rtsp_client_sink_set_state (sink, GST_STATE_PAUSED);
4210 sink->state = GST_RTSP_STATE_READY;
4214 gst_rtsp_client_sink_loop_end_cmd (sink, CMD_PAUSE, res);
4221 GST_DEBUG_OBJECT (sink, "failed to open stream");
4226 GST_DEBUG_OBJECT (sink, "PAUSE is not supported");
4231 GST_DEBUG_OBJECT (sink, "we were already PAUSED");
4234 create_request_failed:
4236 gchar *str = gst_rtsp_strresult (res);
4238 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
4239 ("Could not create request. (%s)", str));
4245 gchar *str = gst_rtsp_strresult (res);
4247 gst_rtsp_message_unset (&request);
4248 if (res != GST_RTSP_EINTR) {
4249 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
4250 ("Could not send message. (%s)", str));
4252 GST_WARNING_OBJECT (sink, "PAUSE interrupted");
4260 gst_rtsp_client_sink_handle_message (GstBin * bin, GstMessage * message)
4262 GstRTSPClientSink *rtsp_client_sink;
4264 rtsp_client_sink = GST_RTSP_CLIENT_SINK (bin);
4266 switch (GST_MESSAGE_TYPE (message)) {
4267 case GST_MESSAGE_ELEMENT:
4269 const GstStructure *s = gst_message_get_structure (message);
4271 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
4272 gboolean ignore_timeout;
4274 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
4276 GST_OBJECT_LOCK (rtsp_client_sink);
4277 ignore_timeout = rtsp_client_sink->ignore_timeout;
4278 rtsp_client_sink->ignore_timeout = TRUE;
4279 GST_OBJECT_UNLOCK (rtsp_client_sink);
4281 /* we only act on the first udp timeout message, others are irrelevant
4282 * and can be ignored. */
4283 if (!ignore_timeout)
4284 gst_rtsp_client_sink_loop_send_cmd (rtsp_client_sink, CMD_RECONNECT,
4287 gst_message_unref (message);
4289 } else if (gst_structure_has_name (s, "GstRTSPStreamBlocking")) {
4290 /* An RTSPStream has prerolled */
4291 g_cond_broadcast (&rtsp_client_sink->preroll_cond);
4293 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
4296 case GST_MESSAGE_ASYNC_START:{
4299 sender = GST_MESSAGE_SRC (message);
4301 GST_LOG_OBJECT (rtsp_client_sink,
4302 "Have async-start from %" GST_PTR_FORMAT, sender);
4303 if (sender == GST_OBJECT (rtsp_client_sink->internal_bin)) {
4304 GST_LOG_OBJECT (rtsp_client_sink, "child bin is now ASYNC");
4306 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
4309 case GST_MESSAGE_ASYNC_DONE:
4312 gboolean need_async_done;
4314 sender = GST_MESSAGE_SRC (message);
4315 GST_LOG_OBJECT (rtsp_client_sink, "Have async-done from %" GST_PTR_FORMAT,
4318 g_mutex_lock (&rtsp_client_sink->preroll_lock);
4319 if (sender == GST_OBJECT_CAST (rtsp_client_sink->internal_bin)) {
4320 GST_LOG_OBJECT (rtsp_client_sink, "child bin is no longer ASYNC");
4322 need_async_done = rtsp_client_sink->in_async;
4323 if (rtsp_client_sink->in_async) {
4324 rtsp_client_sink->in_async = FALSE;
4325 g_cond_broadcast (&rtsp_client_sink->preroll_cond);
4327 g_mutex_unlock (&rtsp_client_sink->preroll_lock);
4329 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
4331 if (need_async_done) {
4332 GST_DEBUG_OBJECT (rtsp_client_sink, "Posting ASYNC-DONE");
4333 gst_element_post_message (GST_ELEMENT_CAST (rtsp_client_sink),
4334 gst_message_new_async_done (GST_OBJECT_CAST (rtsp_client_sink),
4335 GST_CLOCK_TIME_NONE));
4339 case GST_MESSAGE_ERROR:
4343 sender = GST_MESSAGE_SRC (message);
4345 GST_DEBUG_OBJECT (rtsp_client_sink, "got error from %s",
4346 GST_ELEMENT_NAME (sender));
4348 /* FIXME: Ignore errors on RTCP? */
4349 /* fatal but not our message, forward */
4350 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
4353 case GST_MESSAGE_STATE_CHANGED:
4355 if (GST_MESSAGE_SRC (message) ==
4356 (GstObject *) rtsp_client_sink->internal_bin) {
4357 GstState newstate, pending;
4358 gst_message_parse_state_changed (message, NULL, &newstate, &pending);
4359 g_mutex_lock (&rtsp_client_sink->preroll_lock);
4360 rtsp_client_sink->prerolled = (newstate >= GST_STATE_PAUSED)
4361 && pending == GST_STATE_VOID_PENDING;
4362 g_cond_broadcast (&rtsp_client_sink->preroll_cond);
4363 g_mutex_unlock (&rtsp_client_sink->preroll_lock);
4364 GST_DEBUG_OBJECT (bin,
4365 "Internal bin changed state to %s (pending %s). Prerolled now %d",
4366 gst_element_state_get_name (newstate),
4367 gst_element_state_get_name (pending), rtsp_client_sink->prerolled);
4373 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
4379 /* the thread where everything happens */
4381 gst_rtsp_client_sink_thread (GstRTSPClientSink * sink)
4385 GST_OBJECT_LOCK (sink);
4386 cmd = sink->pending_cmd;
4387 if (cmd == CMD_RECONNECT || cmd == CMD_RECORD || cmd == CMD_PAUSE
4388 || cmd == CMD_LOOP || cmd == CMD_OPEN)
4389 sink->pending_cmd = CMD_LOOP;
4391 sink->pending_cmd = CMD_WAIT;
4392 GST_DEBUG_OBJECT (sink, "got command %s", cmd_to_string (cmd));
4394 /* we got the message command, so ensure communication is possible again */
4395 gst_rtsp_client_sink_connection_flush (sink, FALSE);
4397 sink->busy_cmd = cmd;
4398 GST_OBJECT_UNLOCK (sink);
4402 gst_rtsp_client_sink_open (sink, TRUE);
4405 gst_rtsp_client_sink_record (sink, TRUE);
4408 gst_rtsp_client_sink_pause (sink, TRUE);
4411 gst_rtsp_client_sink_close (sink, TRUE, FALSE);
4414 gst_rtsp_client_sink_loop (sink);
4417 gst_rtsp_client_sink_reconnect (sink, FALSE);
4423 GST_OBJECT_LOCK (sink);
4424 /* and go back to sleep */
4425 if (sink->pending_cmd == CMD_WAIT) {
4427 gst_task_pause (sink->task);
4430 sink->busy_cmd = CMD_WAIT;
4431 GST_OBJECT_UNLOCK (sink);
4435 gst_rtsp_client_sink_start (GstRTSPClientSink * sink)
4437 GST_DEBUG_OBJECT (sink, "starting");
4439 sink->streams_collected = FALSE;
4440 sink->in_async = TRUE;
4441 gst_element_set_locked_state (GST_ELEMENT (sink->internal_bin), TRUE);
4443 gst_rtsp_client_sink_set_state (sink, GST_STATE_READY);
4445 GST_OBJECT_LOCK (sink);
4446 sink->pending_cmd = CMD_WAIT;
4448 if (sink->task == NULL) {
4450 gst_task_new ((GstTaskFunction) gst_rtsp_client_sink_thread, sink,
4452 if (sink->task == NULL)
4455 gst_task_set_lock (sink->task, GST_RTSP_STREAM_GET_LOCK (sink));
4457 GST_OBJECT_UNLOCK (sink);
4464 GST_OBJECT_UNLOCK (sink);
4465 GST_ERROR_OBJECT (sink, "failed to create task");
4471 gst_rtsp_client_sink_stop (GstRTSPClientSink * sink)
4475 GST_DEBUG_OBJECT (sink, "stopping");
4477 /* also cancels pending task */
4478 gst_rtsp_client_sink_loop_send_cmd (sink, CMD_WAIT, CMD_ALL & ~CMD_CLOSE);
4480 GST_OBJECT_LOCK (sink);
4481 if ((task = sink->task)) {
4483 GST_OBJECT_UNLOCK (sink);
4485 gst_task_stop (task);
4487 /* make sure it is not running */
4488 GST_RTSP_STREAM_LOCK (sink);
4489 GST_RTSP_STREAM_UNLOCK (sink);
4491 /* now wait for the task to finish */
4492 gst_task_join (task);
4494 /* and free the task */
4495 gst_object_unref (GST_OBJECT (task));
4497 GST_OBJECT_LOCK (sink);
4499 GST_OBJECT_UNLOCK (sink);
4501 /* ensure synchronously all is closed and clean */
4502 gst_rtsp_client_sink_close (sink, FALSE, TRUE);
4507 static GstStateChangeReturn
4508 gst_rtsp_client_sink_change_state (GstElement * element,
4509 GstStateChange transition)
4511 GstRTSPClientSink *rtsp_client_sink;
4512 GstStateChangeReturn ret;
4514 rtsp_client_sink = GST_RTSP_CLIENT_SINK (element);
4516 switch (transition) {
4517 case GST_STATE_CHANGE_NULL_TO_READY:
4518 if (!gst_rtsp_client_sink_start (rtsp_client_sink))
4521 case GST_STATE_CHANGE_READY_TO_PAUSED:
4522 /* init some state */
4523 rtsp_client_sink->cur_protocols = rtsp_client_sink->protocols;
4524 /* first attempt, don't ignore timeouts */
4525 rtsp_client_sink->ignore_timeout = FALSE;
4526 rtsp_client_sink->open_error = FALSE;
4528 gst_rtsp_client_sink_set_state (rtsp_client_sink, GST_STATE_PAUSED);
4530 g_mutex_lock (&rtsp_client_sink->preroll_lock);
4531 if (rtsp_client_sink->in_async) {
4532 GST_DEBUG_OBJECT (rtsp_client_sink, "Posting ASYNC-START");
4533 gst_element_post_message (GST_ELEMENT_CAST (rtsp_client_sink),
4534 gst_message_new_async_start (GST_OBJECT_CAST (rtsp_client_sink)));
4536 g_mutex_unlock (&rtsp_client_sink->preroll_lock);
4539 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4541 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4542 /* unblock the tcp tasks and make the loop waiting */
4543 if (gst_rtsp_client_sink_loop_send_cmd (rtsp_client_sink, CMD_WAIT,
4545 /* make sure it is waiting before we send PLAY below */
4546 GST_RTSP_STREAM_LOCK (rtsp_client_sink);
4547 GST_RTSP_STREAM_UNLOCK (rtsp_client_sink);
4550 case GST_STATE_CHANGE_PAUSED_TO_READY:
4551 gst_rtsp_client_sink_set_state (rtsp_client_sink, GST_STATE_READY);
4557 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4558 if (ret == GST_STATE_CHANGE_FAILURE)
4561 switch (transition) {
4562 case GST_STATE_CHANGE_NULL_TO_READY:
4563 ret = GST_STATE_CHANGE_SUCCESS;
4565 case GST_STATE_CHANGE_READY_TO_PAUSED:
4566 /* Return ASYNC and preroll input streams */
4567 g_mutex_lock (&rtsp_client_sink->preroll_lock);
4568 if (rtsp_client_sink->in_async)
4569 ret = GST_STATE_CHANGE_ASYNC;
4570 g_mutex_unlock (&rtsp_client_sink->preroll_lock);
4571 gst_rtsp_client_sink_loop_send_cmd (rtsp_client_sink, CMD_OPEN, 0);
4573 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:{
4574 GST_DEBUG_OBJECT (rtsp_client_sink,
4575 "Switching to playing -sending RECORD");
4576 gst_rtsp_client_sink_loop_send_cmd (rtsp_client_sink, CMD_RECORD, 0);
4577 ret = GST_STATE_CHANGE_SUCCESS;
4580 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4581 /* send pause request and keep the idle task around */
4582 gst_rtsp_client_sink_loop_send_cmd (rtsp_client_sink, CMD_PAUSE,
4584 ret = GST_STATE_CHANGE_NO_PREROLL;
4586 case GST_STATE_CHANGE_PAUSED_TO_READY:
4587 gst_rtsp_client_sink_loop_send_cmd (rtsp_client_sink, CMD_CLOSE,
4589 ret = GST_STATE_CHANGE_SUCCESS;
4591 case GST_STATE_CHANGE_READY_TO_NULL:
4592 gst_rtsp_client_sink_stop (rtsp_client_sink);
4593 ret = GST_STATE_CHANGE_SUCCESS;
4604 GST_DEBUG_OBJECT (rtsp_client_sink, "start failed");
4605 return GST_STATE_CHANGE_FAILURE;
4609 /*** GSTURIHANDLER INTERFACE *************************************************/
4612 gst_rtsp_client_sink_uri_get_type (GType type)
4614 return GST_URI_SINK;
4617 static const gchar *const *
4618 gst_rtsp_client_sink_uri_get_protocols (GType type)
4620 static const gchar *protocols[] =
4621 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
4622 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
4629 gst_rtsp_client_sink_uri_get_uri (GstURIHandler * handler)
4631 GstRTSPClientSink *sink = GST_RTSP_CLIENT_SINK (handler);
4633 /* FIXME: make thread-safe */
4634 return g_strdup (sink->conninfo.location);
4638 gst_rtsp_client_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri,
4641 GstRTSPClientSink *sink;
4644 GstRTSPUrl *newurl = NULL;
4645 GstSDPMessage *sdp = NULL;
4647 sink = GST_RTSP_CLIENT_SINK (handler);
4649 /* same URI, we're fine */
4650 if (sink->conninfo.location && uri && !strcmp (uri, sink->conninfo.location))
4653 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
4654 sres = gst_sdp_message_new (&sdp);
4658 GST_DEBUG_OBJECT (sink, "parsing SDP message");
4659 sres = gst_sdp_message_parse_uri (uri, sdp);
4664 GST_DEBUG_OBJECT (sink, "parsing URI");
4665 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
4669 /* if worked, free previous and store new url object along with the original
4671 GST_DEBUG_OBJECT (sink, "configuring URI");
4672 g_free (sink->conninfo.location);
4673 sink->conninfo.location = g_strdup (uri);
4674 gst_rtsp_url_free (sink->conninfo.url);
4675 sink->conninfo.url = newurl;
4676 g_free (sink->conninfo.url_str);
4678 sink->conninfo.url_str = gst_rtsp_url_get_request_uri (sink->conninfo.url);
4680 sink->conninfo.url_str = NULL;
4683 gst_sdp_message_free (sink->uri_sdp);
4684 sink->uri_sdp = sdp;
4685 sink->from_sdp = sdp != NULL;
4687 GST_DEBUG_OBJECT (sink, "set uri: %s", GST_STR_NULL (uri));
4688 GST_DEBUG_OBJECT (sink, "request uri is: %s",
4689 GST_STR_NULL (sink->conninfo.url_str));
4696 GST_DEBUG_OBJECT (sink, "URI was ok: '%s'", GST_STR_NULL (uri));
4701 GST_ERROR_OBJECT (sink, "Could not create new SDP (%d)", sres);
4702 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
4703 "Could not create SDP");
4708 GST_ERROR_OBJECT (sink, "Not a valid SDP (%d) '%s'", sres,
4709 GST_STR_NULL (uri));
4710 gst_sdp_message_free (sdp);
4711 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
4717 GST_ERROR_OBJECT (sink, "Not a valid RTSP url '%s' (%d)",
4718 GST_STR_NULL (uri), res);
4719 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
4720 "Invalid RTSP URI");
4726 gst_rtsp_client_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
4728 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
4730 iface->get_type = gst_rtsp_client_sink_uri_get_type;
4731 iface->get_protocols = gst_rtsp_client_sink_uri_get_protocols;
4732 iface->get_uri = gst_rtsp_client_sink_uri_get_uri;
4733 iface->set_uri = gst_rtsp_client_sink_uri_set_uri;