2 * Copyright (C) <2005,2006> Wim Taymans <wim at fluendo dot com>
3 * <2006> Lutz Mueller <lutz at topfrose dot de>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 * Unless otherwise indicated, Source Code is licensed under MIT license.
22 * See further explanation attached in License Statement (distributed in the file
25 * Permission is hereby granted, free of charge, to any person obtaining a copy of
26 * this software and associated documentation files (the "Software"), to deal in
27 * the Software without restriction, including without limitation the rights to
28 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
29 * of the Software, and to permit persons to whom the Software is furnished to do
30 * so, subject to the following conditions:
32 * The above copyright notice and this permission notice shall be included in all
33 * copies or substantial portions of the Software.
35 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
44 * SECTION:element-rtspsrc
46 * Makes a connection to an RTSP server and read the data.
47 * rtspsrc strictly follows RFC 2326 and therefore does not (yet) support
48 * RealMedia/Quicktime/Microsoft extensions.
50 * RTSP supports transport over TCP or UDP in unicast or multicast mode. By
51 * default rtspsrc 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 #GstRTSPSrc:protocols property.
55 * rtspsrc currently understands SDP as the format of the session description.
56 * For each stream listed in the SDP a new rtp_stream\%d pad will be created
57 * with caps derived from the SDP media description. This is a caps of mime type
58 * "application/x-rtp" that can be connected to any available RTP depayloader
61 * rtspsrc will internally instantiate an RTP session manager element
62 * that will handle the RTCP messages to and from the server, jitter removal,
63 * packet reordering along with providing a clock for the pipeline.
64 * This feature is implemented using the gstrtpbin element.
66 * rtspsrc acts like a live source and will therefore only generate data in the
70 * <title>Example launch line</title>
72 * gst-launch-1.0 rtspsrc location=rtsp://some.server/url ! fakesink
73 * ]| Establish a connection to an RTSP server and send the raw RTP packets to a
84 #endif /* HAVE_UNISTD_H */
90 #include <gst/net/gstnet.h>
91 #include <gst/sdp/gstsdpmessage.h>
92 #include <gst/sdp/gstmikey.h>
93 #include <gst/rtp/rtp.h>
95 #include "gst/gst-i18n-plugin.h"
97 #include "gstrtspsrc.h"
99 GST_DEBUG_CATEGORY_STATIC (rtspsrc_debug);
100 #define GST_CAT_DEFAULT (rtspsrc_debug)
102 static GstStaticPadTemplate rtptemplate = GST_STATIC_PAD_TEMPLATE ("stream_%u",
105 GST_STATIC_CAPS ("application/x-rtp; application/x-rdt"));
107 /* templates used internally */
108 static GstStaticPadTemplate anysrctemplate =
109 GST_STATIC_PAD_TEMPLATE ("internalsrc_%u",
112 GST_STATIC_CAPS_ANY);
114 static GstStaticPadTemplate anysinktemplate =
115 GST_STATIC_PAD_TEMPLATE ("internalsink_%u",
118 GST_STATIC_CAPS_ANY);
122 SIGNAL_HANDLE_REQUEST,
124 SIGNAL_SELECT_STREAM,
126 SIGNAL_REQUEST_RTCP_KEY,
130 enum _GstRtspSrcRtcpSyncMode
137 enum _GstRtspSrcBufferMode
146 #define GST_TYPE_RTSP_SRC_BUFFER_MODE (gst_rtsp_src_buffer_mode_get_type())
148 gst_rtsp_src_buffer_mode_get_type (void)
150 static GType buffer_mode_type = 0;
151 static const GEnumValue buffer_modes[] = {
152 {BUFFER_MODE_NONE, "Only use RTP timestamps", "none"},
153 {BUFFER_MODE_SLAVE, "Slave receiver to sender clock", "slave"},
154 {BUFFER_MODE_BUFFER, "Do low/high watermark buffering", "buffer"},
155 {BUFFER_MODE_AUTO, "Choose mode depending on stream live", "auto"},
156 {BUFFER_MODE_SYNCED, "Synchronized sender and receiver clocks", "synced"},
160 if (!buffer_mode_type) {
162 g_enum_register_static ("GstRTSPSrcBufferMode", buffer_modes);
164 return buffer_mode_type;
167 enum _GstRtspSrcNtpTimeSource
170 NTP_TIME_SOURCE_UNIX,
171 NTP_TIME_SOURCE_RUNNING_TIME,
172 NTP_TIME_SOURCE_CLOCK_TIME
175 #define GST_TYPE_RTSP_SRC_NTP_TIME_SOURCE (gst_rtsp_src_ntp_time_source_get_type())
177 gst_rtsp_src_ntp_time_source_get_type (void)
179 static GType ntp_time_source_type = 0;
180 static const GEnumValue ntp_time_source_values[] = {
181 {NTP_TIME_SOURCE_NTP, "NTP time based on realtime clock", "ntp"},
182 {NTP_TIME_SOURCE_UNIX, "UNIX time based on realtime clock", "unix"},
183 {NTP_TIME_SOURCE_RUNNING_TIME,
184 "Running time based on pipeline clock",
186 {NTP_TIME_SOURCE_CLOCK_TIME, "Pipeline clock time", "clock-time"},
190 if (!ntp_time_source_type) {
191 ntp_time_source_type =
192 g_enum_register_static ("GstRTSPSrcNtpTimeSource",
193 ntp_time_source_values);
195 return ntp_time_source_type;
198 #define AES_128_KEY_LEN 16
199 #define AES_256_KEY_LEN 32
201 #define HMAC_32_KEY_LEN 4
202 #define HMAC_80_KEY_LEN 10
204 #define DEFAULT_LOCATION NULL
205 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
206 #define DEFAULT_DEBUG FALSE
207 #define DEFAULT_RETRY 20
208 #define DEFAULT_TIMEOUT 5000000
209 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
210 #define DEFAULT_TCP_TIMEOUT 20000000
211 #define DEFAULT_LATENCY_MS 2000
212 #define DEFAULT_DROP_ON_LATENCY FALSE
213 #define DEFAULT_CONNECTION_SPEED 0
214 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
215 #define DEFAULT_DO_RTCP TRUE
216 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
217 #define DEFAULT_PROXY NULL
218 #define DEFAULT_RTP_BLOCKSIZE 0
219 #define DEFAULT_USER_ID NULL
220 #define DEFAULT_USER_PW NULL
221 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
222 #define DEFAULT_PORT_RANGE NULL
223 #define DEFAULT_SHORT_HEADER FALSE
224 #define DEFAULT_PROBATION 2
225 #define DEFAULT_UDP_RECONNECT TRUE
226 #define DEFAULT_MULTICAST_IFACE NULL
227 #define DEFAULT_NTP_SYNC FALSE
228 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
229 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
230 #define DEFAULT_TLS_DATABASE NULL
231 #define DEFAULT_TLS_INTERACTION NULL
232 #define DEFAULT_DO_RETRANSMISSION TRUE
233 #define DEFAULT_NTP_TIME_SOURCE NTP_TIME_SOURCE_NTP
234 #define DEFAULT_USER_AGENT "GStreamer/" PACKAGE_VERSION
236 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
237 #define DEFAULT_START_POSITION 0
248 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
250 PROP_RESUME_POSITION,
254 PROP_DROP_ON_LATENCY,
255 PROP_CONNECTION_SPEED,
258 PROP_DO_RTSP_KEEP_ALIVE,
267 PROP_UDP_BUFFER_SIZE,
271 PROP_MULTICAST_IFACE,
273 PROP_USE_PIPELINE_CLOCK,
275 PROP_TLS_VALIDATION_FLAGS,
277 PROP_TLS_INTERACTION,
278 PROP_DO_RETRANSMISSION,
279 PROP_NTP_TIME_SOURCE,
283 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
285 gst_rtsp_nat_method_get_type (void)
287 static GType rtsp_nat_method_type = 0;
288 static const GEnumValue rtsp_nat_method[] = {
289 {GST_RTSP_NAT_NONE, "None", "none"},
290 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
294 if (!rtsp_nat_method_type) {
295 rtsp_nat_method_type =
296 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
298 return rtsp_nat_method_type;
301 static void gst_rtspsrc_finalize (GObject * object);
303 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
304 const GValue * value, GParamSpec * pspec);
305 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
306 GValue * value, GParamSpec * pspec);
308 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
310 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
311 gpointer iface_data);
313 static void gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes,
316 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
317 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
319 static GstCaps *gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media);
321 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
322 GstStateChange transition);
323 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
324 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
326 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
327 GstRTSPMessage * response);
329 static gboolean gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
331 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
332 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
334 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
335 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
337 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
338 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
339 gboolean only_close);
341 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
342 const gchar * uri, GError ** error);
343 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
345 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
346 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
347 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
348 GstRTSPStream * stream, GstEvent * event);
349 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
350 static void gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush);
358 /* commands we send to out loop to notify it of events */
359 #define CMD_OPEN (1 << 0)
360 #define CMD_PLAY (1 << 1)
361 #define CMD_PAUSE (1 << 2)
362 #define CMD_CLOSE (1 << 3)
363 #define CMD_WAIT (1 << 4)
364 #define CMD_RECONNECT (1 << 5)
365 #define CMD_LOOP (1 << 6)
367 /* mask for all commands */
368 #define CMD_ALL ((CMD_LOOP << 1) - 1)
370 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
372 gchar *__txt = _gst_element_error_printf text; \
373 gst_element_post_message (GST_ELEMENT_CAST (el), \
374 gst_message_new_progress (GST_OBJECT_CAST (el), \
375 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
379 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
381 #define gst_rtspsrc_parent_class parent_class
382 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
383 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
385 #ifndef GST_DISABLE_GST_DEBUG
386 static inline const char *
387 cmd_to_string (guint cmd)
410 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
412 gst_rtspsrc_post_error_message (GstRTSPSrc * src, GstRTSPSrcError error_id,
413 const gchar * error_string)
416 GstStructure *structure;
419 GST_ERROR_OBJECT (src, "[%d] %s", error_id, error_string);
421 structure = gst_structure_new ("streaming_error",
422 "error_id", G_TYPE_UINT, error_id,
423 "error_string", G_TYPE_STRING, error_string, NULL);
426 gst_message_new_custom (GST_MESSAGE_ERROR, GST_OBJECT (src), structure);
428 ret = gst_element_post_message (GST_ELEMENT (src), message);
430 GST_ERROR_OBJECT (src, "fail to post error message.");
437 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
439 GST_DEBUG_OBJECT (src, "default handler");
444 select_stream_accum (GSignalInvocationHint * ihint,
445 GValue * return_accu, const GValue * handler_return, gpointer data)
449 myboolean = g_value_get_boolean (handler_return);
450 GST_DEBUG ("accum %d", myboolean);
451 g_value_set_boolean (return_accu, myboolean);
453 /* stop emission if FALSE */
458 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
460 GObjectClass *gobject_class;
461 GstElementClass *gstelement_class;
462 GstBinClass *gstbin_class;
464 gobject_class = (GObjectClass *) klass;
465 gstelement_class = (GstElementClass *) klass;
466 gstbin_class = (GstBinClass *) klass;
468 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
470 gobject_class->set_property = gst_rtspsrc_set_property;
471 gobject_class->get_property = gst_rtspsrc_get_property;
473 gobject_class->finalize = gst_rtspsrc_finalize;
475 g_object_class_install_property (gobject_class, PROP_LOCATION,
476 g_param_spec_string ("location", "RTSP Location",
477 "Location of the RTSP url to read",
478 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
480 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
481 g_param_spec_flags ("protocols", "Protocols",
482 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
483 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
485 g_object_class_install_property (gobject_class, PROP_DEBUG,
486 g_param_spec_boolean ("debug", "Debug",
487 "Dump request and response messages to stdout",
488 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
490 g_object_class_install_property (gobject_class, PROP_RETRY,
491 g_param_spec_uint ("retry", "Retry",
492 "Max number of retries when allocating RTP ports.",
493 0, G_MAXUINT16, DEFAULT_RETRY,
494 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
496 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
497 g_param_spec_uint64 ("timeout", "Timeout",
498 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
499 0, G_MAXUINT64, DEFAULT_TIMEOUT,
500 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
501 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
502 g_object_class_install_property (gobject_class, PROP_START_POSITION,
503 g_param_spec_uint64 ("pending-start-position", "set start position",
504 "Set start position before PLAYING request.",
505 0, G_MAXUINT64, DEFAULT_START_POSITION,
506 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
507 g_object_class_install_property (gobject_class, PROP_RESUME_POSITION,
508 g_param_spec_uint64 ("resume-position", "set resume position",
509 "Set resume position before PLAYING request after pause.",
510 0, G_MAXUINT64, DEFAULT_START_POSITION,
511 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
513 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
514 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
515 "Fail after timeout microseconds on TCP connections (0 = disabled)",
516 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
517 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
519 g_object_class_install_property (gobject_class, PROP_LATENCY,
520 g_param_spec_uint ("latency", "Buffer latency in ms",
521 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
522 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
524 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
525 g_param_spec_boolean ("drop-on-latency",
526 "Drop buffers when maximum latency is reached",
527 "Tells the jitterbuffer to never exceed the given latency in size",
528 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
530 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
531 g_param_spec_uint64 ("connection-speed", "Connection Speed",
532 "Network connection speed in kbps (0 = unknown)",
533 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
534 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
536 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
537 g_param_spec_enum ("nat-method", "NAT Method",
538 "Method to use for traversing firewalls and NAT",
539 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
540 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
543 * GstRTSPSrc:do-rtcp:
545 * Enable RTCP support. Some old server don't like RTCP and then this property
546 * needs to be set to FALSE.
548 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
549 g_param_spec_boolean ("do-rtcp", "Do RTCP",
550 "Send RTCP packets, disable for old incompatible server.",
551 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
554 * GstRTSPSrc:do-rtsp-keep-alive:
556 * Enable RTSP keep alive support. Some old server don't like RTSP
557 * keep alive and then this property needs to be set to FALSE.
559 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
560 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
561 "Send RTSP keep alive packets, disable for old incompatible server.",
562 DEFAULT_DO_RTSP_KEEP_ALIVE,
563 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
568 * Set the proxy parameters. This has to be a string of the format
569 * [http://][user:passwd@]host[:port].
571 g_object_class_install_property (gobject_class, PROP_PROXY,
572 g_param_spec_string ("proxy", "Proxy",
573 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
574 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
576 * GstRTSPSrc:proxy-id:
578 * Sets the proxy URI user id for authentication. If the URI set via the
579 * "proxy" property contains a user-id already, that will take precedence.
583 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
584 g_param_spec_string ("proxy-id", "proxy-id",
585 "HTTP proxy URI user id for authentication", "",
586 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
588 * GstRTSPSrc:proxy-pw:
590 * Sets the proxy URI password for authentication. If the URI set via the
591 * "proxy" property contains a password already, that will take precedence.
595 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
596 g_param_spec_string ("proxy-pw", "proxy-pw",
597 "HTTP proxy URI user password for authentication", "",
598 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
601 * GstRTSPSrc:rtp-blocksize:
603 * RTP package size to suggest to server.
605 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
606 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
607 "RTP package size to suggest to server (0 = disabled)",
608 0, 65536, DEFAULT_RTP_BLOCKSIZE,
609 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
611 g_object_class_install_property (gobject_class,
613 g_param_spec_string ("user-id", "user-id",
614 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
615 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
616 g_object_class_install_property (gobject_class, PROP_USER_PW,
617 g_param_spec_string ("user-pw", "user-pw",
618 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
619 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
622 * GstRTSPSrc:buffer-mode:
624 * Control the buffering and timestamping mode used by the jitterbuffer.
626 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
627 g_param_spec_enum ("buffer-mode", "Buffer Mode",
628 "Control the buffering algorithm in use",
629 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
630 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
633 * GstRTSPSrc:port-range:
635 * Configure the client port numbers that can be used to recieve RTP and
638 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
639 g_param_spec_string ("port-range", "Port range",
640 "Client port range that can be used to receive RTP and RTCP data, "
641 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
642 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
645 * GstRTSPSrc:udp-buffer-size:
647 * Size of the kernel UDP receive buffer in bytes.
649 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
650 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
651 "Size of the kernel UDP receive buffer in bytes, 0=default",
652 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
653 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
656 * GstRTSPSrc:short-header:
658 * Only send the basic RTSP headers for broken encoders.
660 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
661 g_param_spec_boolean ("short-header", "Short Header",
662 "Only send the basic RTSP headers for broken encoders",
663 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
665 g_object_class_install_property (gobject_class, PROP_PROBATION,
666 g_param_spec_uint ("probation", "Number of probations",
667 "Consecutive packet sequence numbers to accept the source",
668 0, G_MAXUINT, DEFAULT_PROBATION,
669 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
671 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
672 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
673 "Reconnect to the server if RTSP connection is closed when doing UDP",
674 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
676 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
677 g_param_spec_string ("multicast-iface", "Multicast Interface",
678 "The network interface on which to join the multicast group",
679 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
681 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
682 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
683 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
684 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
686 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
687 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
688 "Use the pipeline running-time to set the NTP time in the RTCP SR messages"
689 "(DEPRECATED: Use ntp-time-source property)",
690 DEFAULT_USE_PIPELINE_CLOCK,
691 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
693 g_object_class_install_property (gobject_class, PROP_SDES,
694 g_param_spec_boxed ("sdes", "SDES",
695 "The SDES items of this session",
696 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
699 * GstRTSPSrc::tls-validation-flags:
701 * TLS certificate validation flags used to validate server
706 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
707 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
708 "TLS certificate validation flags used to validate the server certificate",
709 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
710 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
713 * GstRTSPSrc::tls-database:
715 * TLS database with anchor certificate authorities used to validate
716 * the server certificate.
720 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
721 g_param_spec_object ("tls-database", "TLS database",
722 "TLS database with anchor certificate authorities used to validate the server certificate",
723 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
726 * GstRTSPSrc::tls-interaction:
728 * A #GTlsInteraction object to be used when the connection or certificate
729 * database need to interact with the user. This will be used to prompt the
730 * user for passwords where necessary.
734 g_object_class_install_property (gobject_class, PROP_TLS_INTERACTION,
735 g_param_spec_object ("tls-interaction", "TLS interaction",
736 "A GTlsInteraction object to promt the user for password or certificate",
737 G_TYPE_TLS_INTERACTION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
740 * GstRTSPSrc::do-retransmission:
742 * Attempt to ask the server to retransmit lost packets according to RFC4588.
744 * Note: currently only works with SSRC-multiplexed retransmission streams
748 g_object_class_install_property (gobject_class, PROP_DO_RETRANSMISSION,
749 g_param_spec_boolean ("do-retransmission", "Retransmission",
750 "Ask the server to retransmit lost packets",
751 DEFAULT_DO_RETRANSMISSION,
752 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
755 * GstRTSPSrc::ntp-time-source:
757 * allows to select the time source that should be used
758 * for the NTP time in RTCP packets
762 g_object_class_install_property (gobject_class, PROP_NTP_TIME_SOURCE,
763 g_param_spec_enum ("ntp-time-source", "NTP Time Source",
764 "NTP time source for RTCP packets",
765 GST_TYPE_RTSP_SRC_NTP_TIME_SOURCE, DEFAULT_NTP_TIME_SOURCE,
766 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
769 * GstRTSPSrc::user-agent:
771 * The string to set in the User-Agent header.
775 g_object_class_install_property (gobject_class, PROP_USER_AGENT,
776 g_param_spec_string ("user-agent", "User Agent",
777 "The User-Agent string to send to the server",
778 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
781 * GstRTSPSrc::handle-request:
782 * @rtspsrc: a #GstRTSPSrc
783 * @request: a #GstRTSPMessage
784 * @response: a #GstRTSPMessage
786 * Handle a server request in @request and prepare @response.
788 * This signal is called from the streaming thread, you should therefore not
789 * do any state changes on @rtspsrc because this might deadlock. If you want
790 * to modify the state as a result of this signal, post a
791 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
796 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
797 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
798 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
799 G_TYPE_POINTER, G_TYPE_POINTER);
802 * GstRTSPSrc::on-sdp:
803 * @rtspsrc: a #GstRTSPSrc
804 * @sdp: a #GstSDPMessage
806 * Emited when the client has retrieved the SDP and before it configures the
807 * streams in the SDP. @sdp can be inspected and modified.
809 * This signal is called from the streaming thread, you should therefore not
810 * do any state changes on @rtspsrc because this might deadlock. If you want
811 * to modify the state as a result of this signal, post a
812 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
817 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
818 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
819 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
820 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
823 * GstRTSPSrc::select-stream:
824 * @rtspsrc: a #GstRTSPSrc
825 * @num: the stream number
826 * @caps: the stream caps
828 * Emited before the client decides to configure the stream @num with
831 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
836 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
837 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
838 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
839 (GCallback) default_select_stream, select_stream_accum, NULL,
840 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, G_TYPE_UINT,
843 * GstRTSPSrc::new-manager:
844 * @rtspsrc: a #GstRTSPSrc
845 * @manager: a #GstElement
847 * Emited after a new manager (like rtpbin) was created and the default
848 * properties were configured.
852 gst_rtspsrc_signals[SIGNAL_NEW_MANAGER] =
853 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
854 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
855 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
858 * GstRTSPSrc::request-rtcp-key:
859 * @rtspsrc: a #GstRTSPSrc
860 * @num: the stream number
862 * Signal emited to get the crypto parameters relevant to the RTCP
863 * stream. User should provide the key and the RTCP encryption ciphers
864 * and authentication, and return them wrapped in a GstCaps.
868 gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY] =
869 g_signal_new ("request-rtcp-key", G_TYPE_FROM_CLASS (klass),
870 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, GST_TYPE_CAPS, 1, G_TYPE_UINT);
872 gstelement_class->send_event = gst_rtspsrc_send_event;
873 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
874 gstelement_class->change_state = gst_rtspsrc_change_state;
876 gst_element_class_add_pad_template (gstelement_class,
877 gst_static_pad_template_get (&rtptemplate));
879 gst_element_class_set_static_metadata (gstelement_class,
880 "RTSP packet receiver", "Source/Network",
881 "Receive data over the network via RTSP (RFC 2326)",
882 "Wim Taymans <wim@fluendo.com>, "
883 "Thijs Vermeir <thijs.vermeir@barco.com>, "
884 "Lutz Mueller <lutz@topfrose.de>");
886 gstbin_class->handle_message = gst_rtspsrc_handle_message;
888 gst_rtsp_ext_list_init ();
892 gst_rtspsrc_init (GstRTSPSrc * src)
894 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
895 src->protocols = DEFAULT_PROTOCOLS;
896 src->debug = DEFAULT_DEBUG;
897 src->retry = DEFAULT_RETRY;
898 src->udp_timeout = DEFAULT_TIMEOUT;
899 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
900 src->start_position = DEFAULT_START_POSITION;
901 src->is_audio_codec_supported = FALSE;
902 src->is_video_codec_supported = FALSE;
903 src->audio_codec = NULL;
904 src->video_codec = NULL;
905 src->video_frame_size = NULL;
907 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
908 src->latency = DEFAULT_LATENCY_MS;
909 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
910 src->connection_speed = DEFAULT_CONNECTION_SPEED;
911 src->nat_method = DEFAULT_NAT_METHOD;
912 src->do_rtcp = DEFAULT_DO_RTCP;
913 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
914 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
915 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
916 src->user_id = g_strdup (DEFAULT_USER_ID);
917 src->user_pw = g_strdup (DEFAULT_USER_PW);
918 src->buffer_mode = DEFAULT_BUFFER_MODE;
919 src->client_port_range.min = 0;
920 src->client_port_range.max = 0;
921 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
922 src->short_header = DEFAULT_SHORT_HEADER;
923 src->probation = DEFAULT_PROBATION;
924 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
925 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
926 src->ntp_sync = DEFAULT_NTP_SYNC;
927 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
929 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
930 src->tls_database = DEFAULT_TLS_DATABASE;
931 src->tls_interaction = DEFAULT_TLS_INTERACTION;
932 src->do_retransmission = DEFAULT_DO_RETRANSMISSION;
933 src->ntp_time_source = DEFAULT_NTP_TIME_SOURCE;
934 src->user_agent = g_strdup (DEFAULT_USER_AGENT);
936 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
937 g_mutex_init (&(src)->pause_lock);
938 g_cond_init (&(src)->open_end);
940 /* get a list of all extensions */
941 src->extensions = gst_rtsp_ext_list_get ();
943 /* connect to send signal */
944 gst_rtsp_ext_list_connect (src->extensions, "send",
945 (GCallback) gst_rtspsrc_send_cb, src);
947 /* protects the streaming thread in interleaved mode or the polling
948 * thread in UDP mode. */
949 g_rec_mutex_init (&src->stream_rec_lock);
951 /* protects our state changes from multiple invocations */
952 g_rec_mutex_init (&src->state_rec_lock);
954 src->state = GST_RTSP_STATE_INVALID;
956 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
960 gst_rtspsrc_finalize (GObject * object)
964 rtspsrc = GST_RTSPSRC (object);
966 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
967 rtspsrc->is_audio_codec_supported = FALSE;
968 rtspsrc->is_video_codec_supported = FALSE;
969 if (rtspsrc->audio_codec) {
970 g_free (rtspsrc->audio_codec);
971 rtspsrc->audio_codec = NULL;
973 if (rtspsrc->video_codec) {
974 g_free (rtspsrc->video_codec);
975 rtspsrc->video_codec = NULL;
977 if (rtspsrc->video_frame_size) {
978 g_free (rtspsrc->video_frame_size);
979 rtspsrc->video_frame_size = NULL;
982 gst_rtsp_ext_list_free (rtspsrc->extensions);
983 g_free (rtspsrc->conninfo.location);
984 gst_rtsp_url_free (rtspsrc->conninfo.url);
985 g_free (rtspsrc->conninfo.url_str);
986 g_free (rtspsrc->user_id);
987 g_free (rtspsrc->user_pw);
988 g_free (rtspsrc->multi_iface);
989 g_free (rtspsrc->user_agent);
991 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
992 g_mutex_clear (&(rtspsrc)->pause_lock);
993 g_cond_clear (&(rtspsrc)->open_end);
997 gst_sdp_message_free (rtspsrc->sdp);
1000 if (rtspsrc->provided_clock)
1001 gst_object_unref (rtspsrc->provided_clock);
1004 gst_structure_free (rtspsrc->sdes);
1006 if (rtspsrc->tls_database)
1007 g_object_unref (rtspsrc->tls_database);
1009 if (rtspsrc->tls_interaction)
1010 g_object_unref (rtspsrc->tls_interaction);
1013 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
1014 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
1016 G_OBJECT_CLASS (parent_class)->finalize (object);
1020 gst_rtspsrc_provide_clock (GstElement * element)
1022 GstRTSPSrc *src = GST_RTSPSRC (element);
1025 if ((clock = src->provided_clock) != NULL)
1026 gst_object_ref (clock);
1031 /* a proxy string of the format [user:passwd@]host[:port] */
1033 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
1035 gchar *p, *at, *col;
1037 g_free (rtsp->proxy_user);
1038 rtsp->proxy_user = NULL;
1039 g_free (rtsp->proxy_passwd);
1040 rtsp->proxy_passwd = NULL;
1041 g_free (rtsp->proxy_host);
1042 rtsp->proxy_host = NULL;
1043 rtsp->proxy_port = 0;
1045 p = (gchar *) proxy;
1050 /* we allow http:// in front but ignore it */
1051 if (g_str_has_prefix (p, "http://"))
1054 at = strchr (p, '@');
1056 /* look for user:passwd */
1057 col = strchr (proxy, ':');
1058 if (col == NULL || col > at)
1061 rtsp->proxy_user = g_strndup (p, col - p);
1063 rtsp->proxy_passwd = g_strndup (col, at - col);
1068 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
1069 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
1070 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
1071 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
1072 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
1073 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
1074 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
1077 col = strchr (p, ':');
1080 /* everything before the colon is the hostname */
1081 rtsp->proxy_host = g_strndup (p, col - p);
1083 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
1085 rtsp->proxy_host = g_strdup (p);
1086 rtsp->proxy_port = 8080;
1092 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
1094 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
1095 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
1098 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
1100 rtspsrc->ptcp_timeout = NULL;
1104 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
1107 GstRTSPSrc *rtspsrc;
1109 rtspsrc = GST_RTSPSRC (object);
1113 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
1114 g_value_get_string (value), NULL);
1116 case PROP_PROTOCOLS:
1117 rtspsrc->protocols = g_value_get_flags (value);
1120 rtspsrc->debug = g_value_get_boolean (value);
1123 rtspsrc->retry = g_value_get_uint (value);
1126 rtspsrc->udp_timeout = g_value_get_uint64 (value);
1128 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
1129 case PROP_START_POSITION:
1130 rtspsrc->start_position = g_value_get_uint64 (value);
1132 case PROP_RESUME_POSITION:
1133 rtspsrc->last_pos = g_value_get_uint64 (value);
1134 GST_DEBUG_OBJECT (rtspsrc, "src->last_pos value set to %" GST_TIME_FORMAT,
1135 GST_TIME_ARGS (rtspsrc->last_pos));
1138 case PROP_TCP_TIMEOUT:
1139 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
1142 rtspsrc->latency = g_value_get_uint (value);
1144 case PROP_DROP_ON_LATENCY:
1145 rtspsrc->drop_on_latency = g_value_get_boolean (value);
1147 case PROP_CONNECTION_SPEED:
1148 rtspsrc->connection_speed = g_value_get_uint64 (value);
1150 case PROP_NAT_METHOD:
1151 rtspsrc->nat_method = g_value_get_enum (value);
1154 rtspsrc->do_rtcp = g_value_get_boolean (value);
1156 case PROP_DO_RTSP_KEEP_ALIVE:
1157 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
1160 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
1163 if (rtspsrc->prop_proxy_id)
1164 g_free (rtspsrc->prop_proxy_id);
1165 rtspsrc->prop_proxy_id = g_value_dup_string (value);
1168 if (rtspsrc->prop_proxy_pw)
1169 g_free (rtspsrc->prop_proxy_pw);
1170 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
1172 case PROP_RTP_BLOCKSIZE:
1173 rtspsrc->rtp_blocksize = g_value_get_uint (value);
1176 if (rtspsrc->user_id)
1177 g_free (rtspsrc->user_id);
1178 rtspsrc->user_id = g_value_dup_string (value);
1181 if (rtspsrc->user_pw)
1182 g_free (rtspsrc->user_pw);
1183 rtspsrc->user_pw = g_value_dup_string (value);
1185 case PROP_BUFFER_MODE:
1186 rtspsrc->buffer_mode = g_value_get_enum (value);
1188 case PROP_PORT_RANGE:
1192 str = g_value_get_string (value);
1194 sscanf (str, "%u-%u",
1195 &rtspsrc->client_port_range.min, &rtspsrc->client_port_range.max);
1197 rtspsrc->client_port_range.min = 0;
1198 rtspsrc->client_port_range.max = 0;
1202 case PROP_UDP_BUFFER_SIZE:
1203 rtspsrc->udp_buffer_size = g_value_get_int (value);
1205 case PROP_SHORT_HEADER:
1206 rtspsrc->short_header = g_value_get_boolean (value);
1208 case PROP_PROBATION:
1209 rtspsrc->probation = g_value_get_uint (value);
1211 case PROP_UDP_RECONNECT:
1212 rtspsrc->udp_reconnect = g_value_get_boolean (value);
1214 case PROP_MULTICAST_IFACE:
1215 g_free (rtspsrc->multi_iface);
1217 if (g_value_get_string (value) == NULL)
1218 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1220 rtspsrc->multi_iface = g_value_dup_string (value);
1223 rtspsrc->ntp_sync = g_value_get_boolean (value);
1225 case PROP_USE_PIPELINE_CLOCK:
1226 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
1229 rtspsrc->sdes = g_value_dup_boxed (value);
1231 case PROP_TLS_VALIDATION_FLAGS:
1232 rtspsrc->tls_validation_flags = g_value_get_flags (value);
1234 case PROP_TLS_DATABASE:
1235 g_clear_object (&rtspsrc->tls_database);
1236 rtspsrc->tls_database = g_value_dup_object (value);
1238 case PROP_TLS_INTERACTION:
1239 g_clear_object (&rtspsrc->tls_interaction);
1240 rtspsrc->tls_interaction = g_value_dup_object (value);
1242 case PROP_DO_RETRANSMISSION:
1243 rtspsrc->do_retransmission = g_value_get_boolean (value);
1245 case PROP_NTP_TIME_SOURCE:
1246 rtspsrc->ntp_time_source = g_value_get_enum (value);
1248 case PROP_USER_AGENT:
1249 g_free (rtspsrc->user_agent);
1250 rtspsrc->user_agent = g_value_dup_string (value);
1253 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1259 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
1262 GstRTSPSrc *rtspsrc;
1264 rtspsrc = GST_RTSPSRC (object);
1268 g_value_set_string (value, rtspsrc->conninfo.location);
1270 case PROP_PROTOCOLS:
1271 g_value_set_flags (value, rtspsrc->protocols);
1274 g_value_set_boolean (value, rtspsrc->debug);
1277 g_value_set_uint (value, rtspsrc->retry);
1280 g_value_set_uint64 (value, rtspsrc->udp_timeout);
1282 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
1283 case PROP_START_POSITION:
1284 g_value_set_uint64 (value, rtspsrc->start_position);
1286 case PROP_RESUME_POSITION:
1287 g_value_set_uint64 (value, rtspsrc->last_pos);
1290 case PROP_TCP_TIMEOUT:
1294 timeout = rtspsrc->tcp_timeout.tv_sec * G_USEC_PER_SEC +
1295 rtspsrc->tcp_timeout.tv_usec;
1296 g_value_set_uint64 (value, timeout);
1300 g_value_set_uint (value, rtspsrc->latency);
1302 case PROP_DROP_ON_LATENCY:
1303 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1305 case PROP_CONNECTION_SPEED:
1306 g_value_set_uint64 (value, rtspsrc->connection_speed);
1308 case PROP_NAT_METHOD:
1309 g_value_set_enum (value, rtspsrc->nat_method);
1312 g_value_set_boolean (value, rtspsrc->do_rtcp);
1314 case PROP_DO_RTSP_KEEP_ALIVE:
1315 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1321 if (rtspsrc->proxy_host) {
1323 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1327 g_value_take_string (value, str);
1331 g_value_set_string (value, rtspsrc->prop_proxy_id);
1334 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1336 case PROP_RTP_BLOCKSIZE:
1337 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1340 g_value_set_string (value, rtspsrc->user_id);
1343 g_value_set_string (value, rtspsrc->user_pw);
1345 case PROP_BUFFER_MODE:
1346 g_value_set_enum (value, rtspsrc->buffer_mode);
1348 case PROP_PORT_RANGE:
1352 if (rtspsrc->client_port_range.min != 0) {
1353 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1354 rtspsrc->client_port_range.max);
1358 g_value_take_string (value, str);
1361 case PROP_UDP_BUFFER_SIZE:
1362 g_value_set_int (value, rtspsrc->udp_buffer_size);
1364 case PROP_SHORT_HEADER:
1365 g_value_set_boolean (value, rtspsrc->short_header);
1367 case PROP_PROBATION:
1368 g_value_set_uint (value, rtspsrc->probation);
1370 case PROP_UDP_RECONNECT:
1371 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1373 case PROP_MULTICAST_IFACE:
1374 g_value_set_string (value, rtspsrc->multi_iface);
1377 g_value_set_boolean (value, rtspsrc->ntp_sync);
1379 case PROP_USE_PIPELINE_CLOCK:
1380 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1383 g_value_set_boxed (value, rtspsrc->sdes);
1385 case PROP_TLS_VALIDATION_FLAGS:
1386 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1388 case PROP_TLS_DATABASE:
1389 g_value_set_object (value, rtspsrc->tls_database);
1391 case PROP_TLS_INTERACTION:
1392 g_value_set_object (value, rtspsrc->tls_interaction);
1394 case PROP_DO_RETRANSMISSION:
1395 g_value_set_boolean (value, rtspsrc->do_retransmission);
1397 case PROP_NTP_TIME_SOURCE:
1398 g_value_set_enum (value, rtspsrc->ntp_time_source);
1400 case PROP_USER_AGENT:
1401 g_value_set_string (value, rtspsrc->user_agent);
1404 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1410 find_stream_by_id (GstRTSPStream * stream, gint * id)
1412 if (stream->id == *id)
1419 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1421 if (stream->channel[0] == *channel || stream->channel[1] == *channel)
1428 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1430 GstElement *src = (GstElement *) a;
1432 if (stream->udpsrc[0] == src)
1434 if (stream->udpsrc[1] == src)
1441 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1443 if (stream->conninfo.location) {
1444 /* check qualified setup_url */
1445 if (!strcmp (stream->conninfo.location, (gchar *) a))
1448 if (stream->control_url) {
1449 /* check original control_url */
1450 if (!strcmp (stream->control_url, (gchar *) a))
1453 /* check if qualified setup_url ends with string */
1454 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1461 static GstRTSPStream *
1462 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1466 /* find and get stream */
1467 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1468 return (GstRTSPStream *) lstream->data;
1473 static const GstSDPBandwidth *
1474 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1475 const GstSDPMedia * media, const gchar * type)
1479 /* first look in the media specific section */
1480 len = gst_sdp_media_bandwidths_len (media);
1481 for (i = 0; i < len; i++) {
1482 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1484 if (strcmp (bw->bwtype, type) == 0)
1487 /* then look in the message specific section */
1488 len = gst_sdp_message_bandwidths_len (sdp);
1489 for (i = 0; i < len; i++) {
1490 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1492 if (strcmp (bw->bwtype, type) == 0)
1499 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1500 const GstSDPMedia * media, GstRTSPStream * stream)
1502 const GstSDPBandwidth *bw;
1504 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1505 stream->as_bandwidth = bw->bandwidth;
1507 stream->as_bandwidth = -1;
1509 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1510 stream->rr_bandwidth = bw->bandwidth;
1512 stream->rr_bandwidth = -1;
1514 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1515 stream->rs_bandwidth = bw->bandwidth;
1517 stream->rs_bandwidth = -1;
1521 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1522 const GstSDPConnection * conn)
1524 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1527 if (conn->addrtype == NULL)
1530 /* check for IPV6 */
1531 if (strcmp (conn->addrtype, "IP4") == 0)
1532 stream->is_ipv6 = FALSE;
1533 else if (strcmp (conn->addrtype, "IP6") == 0)
1534 stream->is_ipv6 = TRUE;
1539 g_free (stream->destination);
1540 stream->destination = g_strdup (conn->address);
1542 /* check for multicast */
1543 stream->is_multicast =
1544 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1546 stream->ttl = conn->ttl;
1549 /* Go over the connections for a stream.
1550 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1552 * - If we are dealing with a localhost address, we disable multicast
1555 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1556 const GstSDPMedia * media, GstRTSPStream * stream)
1558 const GstSDPConnection *conn;
1561 /* first look in the media specific section */
1562 len = gst_sdp_media_connections_len (media);
1563 for (i = 0; i < len; i++) {
1564 conn = gst_sdp_media_get_connection (media, i);
1566 gst_rtspsrc_do_stream_connection (src, stream, conn);
1568 /* then look in the message specific section */
1569 if ((conn = gst_sdp_message_get_connection (sdp))) {
1570 gst_rtspsrc_do_stream_connection (src, stream, conn);
1574 /* m=<media> <UDP port> RTP/AVP <payload>
1577 gst_rtspsrc_collect_payloads (GstRTSPSrc * src, const GstSDPMessage * sdp,
1578 const GstSDPMedia * media, GstRTSPStream * stream)
1582 GstCaps *global_caps;
1585 proto = gst_sdp_media_get_proto (media);
1589 if (g_str_equal (proto, "RTP/AVP"))
1590 stream->profile = GST_RTSP_PROFILE_AVP;
1591 else if (g_str_equal (proto, "RTP/SAVP"))
1592 stream->profile = GST_RTSP_PROFILE_SAVP;
1593 else if (g_str_equal (proto, "RTP/AVPF"))
1594 stream->profile = GST_RTSP_PROFILE_AVPF;
1595 else if (g_str_equal (proto, "RTP/SAVPF"))
1596 stream->profile = GST_RTSP_PROFILE_SAVPF;
1600 /* Parse global SDP attributes once */
1601 global_caps = gst_caps_new_empty_simple ("application/x-unknown");
1602 GST_DEBUG ("mapping sdp session level attributes to caps");
1603 gst_rtspsrc_sdp_attributes_to_caps (sdp->attributes, global_caps);
1604 GST_DEBUG ("mapping sdp media level attributes to caps");
1605 gst_rtspsrc_sdp_attributes_to_caps (media->attributes, global_caps);
1607 len = gst_sdp_media_formats_len (media);
1608 for (i = 0; i < len; i++) {
1610 GstCaps *caps, *outcaps;
1614 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
1615 const gchar *encoder, *mediatype;
1617 pt = atoi (gst_sdp_media_get_format (media, i));
1619 GST_DEBUG_OBJECT (src, " looking at %d pt: %d", i, pt);
1622 caps = gst_rtspsrc_media_to_caps (pt, media);
1624 GST_WARNING_OBJECT (src, " skipping pt %d without caps", pt);
1628 /* do some tweaks */
1629 s = gst_caps_get_structure (caps, 0);
1630 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
1631 stream->is_real = (strstr (enc, "-REAL") != NULL);
1632 if (strcmp (enc, "X-ASF-PF") == 0)
1633 stream->container = TRUE;
1635 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
1636 if ((mediatype = gst_structure_get_string (s, "media"))) {
1637 GST_DEBUG_OBJECT (src, " mediatype : %s", mediatype);
1638 if (!strcmp (mediatype, "video")) {
1639 if ((encoder = gst_structure_get_string (s, "encoding-name"))) {
1640 GST_DEBUG_OBJECT (src, " encoder : %s", encoder);
1641 if ((!strcmp (encoder, "H261")) ||
1642 (!strcmp (encoder, "H263")) ||
1643 (!strcmp (encoder, "H263-1998"))
1644 || (!strcmp (encoder, "H263-2000")) || (!strcmp (encoder, "H264"))
1645 || (!strcmp (encoder, "MP4V-ES"))) {
1646 src->is_video_codec_supported = TRUE;
1647 GST_DEBUG_OBJECT (src, "Supported Video Codec %s", encoder);
1649 GST_DEBUG_OBJECT (src, "Unsupported Video Codec %s", encoder);
1653 src->video_codec = g_strdup (encoder);
1654 src->video_frame_size =
1655 g_strdup (gst_structure_get_string (s, "a-framesize"));
1656 GST_DEBUG_OBJECT (src, "video_codec %s , video_frame_size %s ",
1657 src->video_codec, src->video_frame_size);
1658 } else if (!strcmp (mediatype, "audio")) {
1659 if ((encoder = gst_structure_get_string (s, "encoding-name"))) {
1660 GST_DEBUG_OBJECT (src, " encoder : %s", encoder);
1661 if ((!strcmp (encoder, "MP4A-LATM")) ||
1662 (!strcmp (encoder, "AMR")) || (!strcmp (encoder, "AMR-WB"))
1663 || (!strcmp (encoder, "AMR-NB"))
1664 || (!strcmp (encoder, "mpeg4-generic"))
1665 || (!strcmp (encoder, "MPEG4-GENERIC"))
1666 || (!strcmp (encoder, "QCELP")) || ((strstr (encoder, "G726"))
1667 || (strstr (encoder, "PCMU")))) {
1668 src->is_audio_codec_supported = TRUE;
1669 GST_DEBUG_OBJECT (src, "Supported Audio Codec %s", encoder);
1671 GST_DEBUG_OBJECT (src, "Unsupported Audio Codec %s", encoder);
1675 src->audio_codec = g_strdup (encoder);
1676 GST_DEBUG_OBJECT (src, "audio_codec %s ", src->audio_codec);
1681 /* Merge in global caps */
1682 /* Intersect will merge in missing fields to the current caps */
1683 outcaps = gst_caps_intersect (caps, global_caps);
1684 gst_caps_unref (caps);
1686 /* the first pt will be the default */
1687 if (stream->ptmap->len == 0)
1688 stream->default_pt = pt;
1691 item.caps = outcaps;
1693 g_array_append_val (stream->ptmap, item);
1696 gst_caps_unref (global_caps);
1701 GST_ERROR_OBJECT (src, "can't find proto in media");
1706 GST_ERROR_OBJECT (src, "unknown proto in media %s", proto);
1711 static const gchar *
1712 get_aggregate_control (GstRTSPSrc * src)
1717 base = src->control;
1718 else if (src->content_base)
1719 base = src->content_base;
1720 else if (src->conninfo.url_str)
1721 base = src->conninfo.url_str;
1729 clear_ptmap_item (PtMapItem * item)
1732 gst_caps_unref (item->caps);
1735 static GstRTSPStream *
1736 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
1738 GstRTSPStream *stream;
1739 const gchar *control_url;
1740 const GstSDPMedia *media;
1742 /* get media, should not return NULL */
1743 media = gst_sdp_message_get_media (sdp, idx);
1747 stream = g_new0 (GstRTSPStream, 1);
1748 stream->parent = src;
1749 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1751 stream->last_ret = GST_FLOW_NOT_LINKED;
1752 stream->added = FALSE;
1753 stream->setup = FALSE;
1754 stream->skipped = FALSE;
1756 stream->eos = FALSE;
1757 stream->discont = TRUE;
1758 stream->seqbase = -1;
1759 stream->timebase = -1;
1760 stream->send_ssrc = g_random_int ();
1761 stream->profile = GST_RTSP_PROFILE_AVP;
1762 stream->ptmap = g_array_new (FALSE, FALSE, sizeof (PtMapItem));
1763 g_array_set_clear_func (stream->ptmap, (GDestroyNotify) clear_ptmap_item);
1765 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1766 * session manager to scale RTCP. */
1767 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1769 /* collect connection info */
1770 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1772 /* make the payload type map */
1773 gst_rtspsrc_collect_payloads (src, sdp, media, stream);
1775 /* collect port number */
1776 stream->port = gst_sdp_media_get_port (media);
1778 /* get control url to construct the setup url. The setup url is used to
1779 * configure the transport of the stream and is used to identity the stream in
1780 * the RTP-Info header field returned from PLAY. */
1781 control_url = gst_sdp_media_get_attribute_val (media, "control");
1782 if (control_url == NULL)
1783 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1785 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1786 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1787 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1788 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1790 if (control_url != NULL) {
1791 stream->control_url = g_strdup (control_url);
1792 /* Build a fully qualified url using the content_base if any or by prefixing
1793 * the original request.
1794 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1795 * likely build a URL that the server will fail to understand, this is ok,
1796 * we will fail then. */
1797 if (g_str_has_prefix (control_url, "rtsp://"))
1798 stream->conninfo.location = g_strdup (control_url);
1803 if (g_strcmp0 (control_url, "*") == 0)
1806 base = get_aggregate_control (src);
1808 /* check if the base ends or control starts with / */
1809 has_slash = g_str_has_prefix (control_url, "/");
1810 has_slash = has_slash || g_str_has_suffix (base, "/");
1812 /* concatenate the two strings, insert / when not present */
1813 stream->conninfo.location =
1814 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1817 GST_DEBUG_OBJECT (src, " setup: %s",
1818 GST_STR_NULL (stream->conninfo.location));
1820 /* we keep track of all streams */
1821 src->streams = g_list_append (src->streams, stream);
1829 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1833 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1835 g_array_free (stream->ptmap, TRUE);
1837 g_free (stream->destination);
1838 g_free (stream->control_url);
1839 g_free (stream->conninfo.location);
1841 for (i = 0; i < 2; i++) {
1842 if (stream->udpsrc[i]) {
1843 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1844 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1845 gst_object_unref (stream->udpsrc[i]);
1847 if (stream->channelpad[i])
1848 gst_object_unref (stream->channelpad[i]);
1850 if (stream->udpsink[i]) {
1851 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1852 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1853 gst_object_unref (stream->udpsink[i]);
1856 if (stream->fakesrc) {
1857 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1858 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1859 gst_object_unref (stream->fakesrc);
1861 if (stream->srcpad) {
1862 gst_pad_set_active (stream->srcpad, FALSE);
1864 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1866 if (stream->srtpenc)
1867 gst_object_unref (stream->srtpenc);
1868 if (stream->srtpdec)
1869 gst_object_unref (stream->srtpdec);
1870 if (stream->srtcpparams)
1871 gst_caps_unref (stream->srtcpparams);
1872 if (stream->rtcppad)
1873 gst_object_unref (stream->rtcppad);
1874 if (stream->session)
1875 g_object_unref (stream->session);
1876 if (stream->rtx_pt_map)
1877 gst_structure_free (stream->rtx_pt_map);
1882 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1886 GST_DEBUG_OBJECT (src, "cleanup");
1888 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1889 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1891 gst_rtspsrc_stream_free (src, stream);
1893 g_list_free (src->streams);
1894 src->streams = NULL;
1896 if (src->manager_sig_id) {
1897 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1898 src->manager_sig_id = 0;
1900 gst_element_set_state (src->manager, GST_STATE_NULL);
1901 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1902 src->manager = NULL;
1905 gst_structure_free (src->props);
1908 g_free (src->content_base);
1909 src->content_base = NULL;
1911 g_free (src->control);
1912 src->control = NULL;
1915 gst_rtsp_range_free (src->range);
1918 /* don't clear the SDP when it was used in the url */
1919 if (src->sdp && !src->from_sdp) {
1920 gst_sdp_message_free (src->sdp);
1924 src->need_segment = FALSE;
1926 if (src->provided_clock) {
1927 gst_object_unref (src->provided_clock);
1928 src->provided_clock = NULL;
1932 #define PARSE_INT(p, del, res) \
1935 p = strstr (p, del); \
1945 #define PARSE_STRING(p, del, res) \
1948 p = strstr (p, del); \
1960 #define SKIP_SPACES(p) \
1961 while (*p && g_ascii_isspace (*p)) \
1966 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1969 gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
1970 gint * rate, gchar ** params)
1974 p = (gchar *) rtpmap;
1976 PARSE_INT (p, " ", *payload);
1984 PARSE_STRING (p, "/", *name);
1985 if (*name == NULL) {
1986 GST_DEBUG ("no rate, name %s", p);
1987 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
1988 * streams seem to omit the rate. */
1995 p = strstr (p, "/");
2013 parse_keymgmt (const gchar * keymgmt, GstCaps * caps)
2015 gboolean res = FALSE;
2018 GstMIKEYMessage *msg;
2019 const GstMIKEYPayload *payload;
2020 const gchar *srtp_cipher;
2021 const gchar *srtp_auth;
2027 p = orig_value = g_strdup (keymgmt);
2031 g_free (orig_value);
2035 PARSE_STRING (p, " ", kmpid);
2036 if (kmpid == NULL || !g_str_equal (kmpid, "mikey")) {
2037 g_free (orig_value);
2040 data = g_base64_decode (p, &size);
2042 g_free (orig_value); /* Don't need this any more */
2048 msg = gst_mikey_message_new_from_data (data, size, NULL, NULL);
2053 srtp_cipher = "aes-128-icm";
2054 srtp_auth = "hmac-sha1-80";
2056 /* check the Security policy if any */
2057 if ((payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, 0))) {
2058 GstMIKEYPayloadSP *p = (GstMIKEYPayloadSP *) payload;
2061 if (p->proto != GST_MIKEY_SEC_PROTO_SRTP)
2064 len = gst_mikey_payload_sp_get_n_params (payload);
2065 for (i = 0; i < len; i++) {
2066 const GstMIKEYPayloadSPParam *param =
2067 gst_mikey_payload_sp_get_param (payload, i);
2069 switch (param->type) {
2070 case GST_MIKEY_SP_SRTP_ENC_ALG:
2071 switch (param->val[0]) {
2073 srtp_cipher = "null";
2077 srtp_cipher = "aes-128-icm";
2083 case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
2084 switch (param->val[0]) {
2085 case AES_128_KEY_LEN:
2086 srtp_cipher = "aes-128-icm";
2088 case AES_256_KEY_LEN:
2089 srtp_cipher = "aes-256-icm";
2095 case GST_MIKEY_SP_SRTP_AUTH_ALG:
2096 switch (param->val[0]) {
2102 srtp_auth = "hmac-sha1-80";
2108 case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
2109 switch (param->val[0]) {
2110 case HMAC_32_KEY_LEN:
2111 srtp_auth = "hmac-sha1-32";
2113 case HMAC_80_KEY_LEN:
2114 srtp_auth = "hmac-sha1-80";
2120 case GST_MIKEY_SP_SRTP_SRTP_ENC:
2122 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
2130 if (!(payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_KEMAC, 0)))
2133 GstMIKEYPayloadKEMAC *p = (GstMIKEYPayloadKEMAC *) payload;
2134 const GstMIKEYPayload *sub;
2135 GstMIKEYPayloadKeyData *pkd;
2138 if (p->enc_alg != GST_MIKEY_ENC_NULL || p->mac_alg != GST_MIKEY_MAC_NULL)
2141 if (!(sub = gst_mikey_payload_kemac_get_sub (payload, 0)))
2144 if (sub->type != GST_MIKEY_PT_KEY_DATA)
2147 pkd = (GstMIKEYPayloadKeyData *) sub;
2149 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
2151 gst_caps_set_simple (caps, "srtp-key", GST_TYPE_BUFFER, buf, NULL);
2152 gst_buffer_unref (buf);
2155 gst_caps_set_simple (caps,
2156 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
2157 "srtp-auth", G_TYPE_STRING, srtp_auth,
2158 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
2159 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
2163 gst_mikey_message_unref (msg);
2169 * Mapping SDP attributes to caps
2171 * prepend 'a-' to IANA registered sdp attributes names
2172 * (ie: not prefixed with 'x-') in order to avoid
2173 * collision with gstreamer standard caps properties names
2176 gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
2178 if (attributes->len > 0) {
2182 s = gst_caps_get_structure (caps, 0);
2184 for (i = 0; i < attributes->len; i++) {
2185 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
2186 gchar *tofree, *key;
2190 /* skip some of the attribute we already handle */
2191 if (!strcmp (key, "fmtp"))
2193 if (!strcmp (key, "rtpmap"))
2195 if (!strcmp (key, "control"))
2197 if (!strcmp (key, "range"))
2199 if (!strcmp (key, "framesize"))
2201 if (g_str_equal (key, "key-mgmt")) {
2202 parse_keymgmt (attr->value, caps);
2206 /* string must be valid UTF8 */
2207 if (!g_utf8_validate (attr->value, -1, NULL))
2210 if (!g_str_has_prefix (key, "x-"))
2211 tofree = key = g_strdup_printf ("a-%s", key);
2215 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
2216 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
2222 static const gchar *
2223 rtsp_get_attribute_for_pt (const GstSDPMedia * media, const gchar * name,
2232 if ((attr = gst_sdp_media_get_attribute_val_n (media, name, i)) == NULL)
2235 if (sscanf (attr, "%d ", &val) != 1)
2245 * Mapping of caps to and from SDP fields:
2247 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
2248 * a=framesize:<payload> <width>-<height>
2249 * a=fmtp:<payload> <param>[=<value>];...
2252 gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
2255 const gchar *rtpmap;
2257 const gchar *framesize;
2260 gchar *params = NULL;
2266 /* get and parse rtpmap */
2267 rtpmap = rtsp_get_attribute_for_pt (media, "rtpmap", pt);
2270 ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
2272 g_warning ("error parsing rtpmap, ignoring");
2276 /* dynamic payloads need rtpmap or we fail */
2277 if (rtpmap == NULL && pt >= 96)
2280 /* check if we have a rate, if not, we need to look up the rate from the
2281 * default rates based on the payload types. */
2283 const GstRTPPayloadInfo *info;
2285 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
2286 /* dynamic types, use media and encoding_name */
2287 tmp = g_ascii_strdown (media->media, -1);
2288 info = gst_rtp_payload_info_for_name (tmp, name);
2291 /* static types, use payload type */
2292 info = gst_rtp_payload_info_for_pt (pt);
2296 if ((rate = info->clock_rate) == 0)
2299 /* we fail if we cannot find one */
2304 tmp = g_ascii_strdown (media->media, -1);
2305 caps = gst_caps_new_simple ("application/x-unknown",
2306 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
2308 s = gst_caps_get_structure (caps, 0);
2310 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
2312 /* encoding name must be upper case */
2314 tmp = g_ascii_strup (name, -1);
2315 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
2319 /* params must be lower case */
2320 if (params != NULL) {
2321 tmp = g_ascii_strdown (params, -1);
2322 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
2326 /* parse optional fmtp: field */
2327 if ((fmtp = rtsp_get_attribute_for_pt (media, "fmtp", pt))) {
2333 /* p is now of the format <payload> <param>[=<value>];... */
2334 PARSE_INT (p, " ", payload);
2335 if (payload != -1 && payload == pt) {
2339 /* <param>[=<value>] are separated with ';' */
2340 pairs = g_strsplit (p, ";", 0);
2341 for (i = 0; pairs[i]; i++) {
2343 const gchar *val, *key;
2345 const gchar *reserved_keys[] =
2346 { "media", "payload", "clock-rate", "encoding-name",
2350 /* the key may not have a '=', the value can have other '='s */
2351 valpos = strstr (pairs[i], "=");
2353 /* we have a '=' and thus a value, remove the '=' with \0 */
2355 /* value is everything between '=' and ';'. We split the pairs at ;
2356 * boundaries so we can take the remainder of the value. Some servers
2357 * put spaces around the value which we strip off here. Alternatively
2358 * we could strip those spaces in the depayloaders should these spaces
2359 * actually carry any meaning in the future. */
2360 val = g_strstrip (valpos + 1);
2362 /* simple <param>;.. is translated into <param>=1;... */
2365 /* strip the key of spaces, convert key to lowercase but not the value. */
2366 key = g_strstrip (pairs[i]);
2368 /* skip keys from the fmtp, which we already use ourselves for the
2369 * caps. Some software is adding random things like clock-rate into
2370 * the fmtp, and we would otherwise here set a string-typed clock-rate
2371 * in the caps... and thus fail to create valid RTP caps
2373 for (j = 0; j < G_N_ELEMENTS (reserved_keys); j++) {
2374 if (g_ascii_strcasecmp (reserved_keys[j], key) == 0) {
2380 if (strlen (key) > 1) {
2381 tmp = g_ascii_strdown (key, -1);
2382 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
2390 /* parse framesize: field */
2391 if ((framesize = gst_sdp_media_get_attribute_val (media, "framesize"))) {
2394 /* p is now of the format <payload> <width>-<height> */
2395 p = (gchar *) framesize;
2397 PARSE_INT (p, " ", payload);
2398 if (payload != -1 && payload == pt) {
2399 gst_structure_set (s, "a-framesize", G_TYPE_STRING, p, NULL);
2407 g_warning ("rtpmap type not given for dynamic payload %d", pt);
2412 g_warning ("rate unknown for payload type %d", pt);
2418 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
2419 gint * rtpport, gint * rtcpport)
2422 GstStateChangeReturn ret;
2423 GstElement *udpsrc0, *udpsrc1;
2424 gint tmp_rtp, tmp_rtcp;
2428 src = stream->parent;
2434 /* Start at next port */
2435 tmp_rtp = src->next_port_num;
2437 if (stream->is_ipv6)
2438 host = "udp://[::0]";
2440 host = "udp://0.0.0.0";
2442 /* try to allocate 2 UDP ports, the RTP port should be an even
2443 * number and the RTCP port should be the next (uneven) port */
2446 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
2447 tmp_rtp >= src->client_port_range.max)
2450 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2451 if (udpsrc0 == NULL)
2452 goto no_udp_protocol;
2453 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
2455 if (src->udp_buffer_size != 0)
2456 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
2459 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
2460 if (ret == GST_STATE_CHANGE_FAILURE) {
2462 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
2465 if (++count > src->retry)
2468 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2469 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2470 gst_object_unref (udpsrc0);
2473 GST_DEBUG_OBJECT (src, "retry %d", count);
2476 goto no_udp_protocol;
2479 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
2480 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
2482 /* check if port is even */
2483 if ((tmp_rtp & 0x01) != 0) {
2484 /* port not even, close and allocate another */
2485 if (++count > src->retry)
2488 GST_DEBUG_OBJECT (src, "RTP port not even");
2490 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2491 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2492 gst_object_unref (udpsrc0);
2495 GST_DEBUG_OBJECT (src, "retry %d", count);
2500 /* allocate port+1 for RTCP now */
2501 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2502 if (udpsrc1 == NULL)
2503 goto no_udp_rtcp_protocol;
2506 tmp_rtcp = tmp_rtp + 1;
2507 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
2510 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
2512 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
2513 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
2514 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
2515 if (ret == GST_STATE_CHANGE_FAILURE) {
2516 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
2518 if (++count > src->retry)
2521 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2522 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2523 gst_object_unref (udpsrc0);
2526 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
2527 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2528 gst_object_unref (udpsrc1);
2532 GST_DEBUG_OBJECT (src, "retry %d", count);
2536 /* all fine, do port check */
2537 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
2538 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
2540 /* this should not happen... */
2541 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
2544 /* we keep these elements, we configure all in configure_transport when the
2545 * server told us to really use the UDP ports. */
2546 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
2547 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
2548 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
2549 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
2551 /* keep track of next available port number when we have a range
2553 if (src->next_port_num != 0)
2554 src->next_port_num = tmp_rtcp + 1;
2561 GST_DEBUG_OBJECT (src, "could not get UDP source");
2566 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
2570 no_udp_rtcp_protocol:
2572 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
2577 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
2578 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
2584 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2585 gst_object_unref (udpsrc0);
2588 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2589 gst_object_unref (udpsrc1);
2596 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
2600 GST_WARNING_OBJECT (src, "Setting [%s] element state to: %s \n",
2601 GST_ELEMENT_NAME (GST_ELEMENT_CAST (src)),
2602 gst_element_state_get_name (state));
2604 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
2606 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2607 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2610 for (i = 0; i < 2; i++) {
2611 if (stream->udpsrc[i])
2612 gst_element_set_state (stream->udpsrc[i], state);
2618 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
2625 event = gst_event_new_flush_start ();
2626 GST_DEBUG_OBJECT (src, "start flush");
2628 state = GST_STATE_PAUSED;
2630 event = gst_event_new_flush_stop (FALSE);
2631 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2634 state = GST_STATE_PLAYING;
2636 state = GST_STATE_PAUSED;
2638 gst_rtspsrc_push_event (src, event);
2639 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2640 gst_rtspsrc_set_state (src, state);
2643 static GstRTSPResult
2644 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
2645 GstRTSPMessage * message, GTimeVal * timeout)
2650 ret = gst_rtsp_connection_send (conn, message, timeout);
2652 ret = GST_RTSP_ERROR;
2657 static GstRTSPResult
2658 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
2659 GstRTSPMessage * message, GTimeVal * timeout)
2664 ret = gst_rtsp_connection_receive (conn, message, timeout);
2666 ret = GST_RTSP_ERROR;
2672 gst_rtspsrc_get_position (GstRTSPSrc * src)
2677 query = gst_query_new_position (GST_FORMAT_TIME);
2678 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2679 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2680 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2684 if (stream->srcpad) {
2685 if (gst_pad_query (stream->srcpad, query)) {
2686 gst_query_parse_position (query, &fmt, &pos);
2687 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2688 GST_TIME_ARGS (pos));
2689 src->last_pos = pos;
2699 gst_query_unref (query);
2703 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2708 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2710 gboolean flush, skip;
2713 GstSegment seeksegment = { 0, };
2717 GST_DEBUG_OBJECT (src, "doing seek with event");
2719 gst_event_parse_seek (event, &rate, &format, &flags,
2720 &cur_type, &cur, &stop_type, &stop);
2722 /* no negative rates yet */
2726 /* we need TIME format */
2727 if (format != src->segment.format)
2730 GST_DEBUG_OBJECT (src, "doing seek without event");
2732 cur_type = GST_SEEK_TYPE_SET;
2733 stop_type = GST_SEEK_TYPE_SET;
2736 /* get flush flag */
2737 flush = flags & GST_SEEK_FLAG_FLUSH;
2738 skip = flags & GST_SEEK_FLAG_SKIP;
2740 /* now we need to make sure the streaming thread is stopped. We do this by
2741 * either sending a FLUSH_START event downstream which will cause the
2742 * streaming thread to stop with a WRONG_STATE.
2743 * For a non-flushing seek we simply pause the task, which will happen as soon
2744 * as it completes one iteration (and thus might block when the sink is
2745 * blocking in preroll). */
2747 GST_DEBUG_OBJECT (src, "starting flush");
2748 gst_rtspsrc_flush (src, TRUE, FALSE);
2751 gst_task_pause (src->task);
2755 /* we should now be able to grab the streaming thread because we stopped it
2756 * with the above flush/pause code */
2757 GST_RTSP_STREAM_LOCK (src);
2759 GST_DEBUG_OBJECT (src, "stopped streaming");
2761 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2762 gst_rtspsrc_connection_flush (src, FALSE);
2764 /* copy segment, we need this because we still need the old
2765 * segment when we close the current segment. */
2766 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2768 /* configure the seek parameters in the seeksegment. We will then have the
2769 * right values in the segment to perform the seek */
2771 GST_DEBUG_OBJECT (src, "configuring seek");
2772 gst_segment_do_seek (&seeksegment, rate, format, flags,
2773 cur_type, cur, stop_type, stop, &update);
2776 /* figure out the last position we need to play. If it's configured (stop !=
2777 * -1), use that, else we play until the total duration of the file */
2778 if ((stop = seeksegment.stop) == -1)
2779 stop = seeksegment.duration;
2781 playing = (src->state == GST_RTSP_STATE_PLAYING);
2783 /* if we were playing, pause first */
2785 /* obtain current position in case seek fails */
2786 gst_rtspsrc_get_position (src);
2787 gst_rtspsrc_pause (src, FALSE);
2791 src->state = GST_RTSP_STATE_SEEKING;
2793 /* PLAY will add the range header now. */
2794 src->need_range = TRUE;
2796 /* and continue playing */
2798 gst_rtspsrc_play (src, &seeksegment, FALSE);
2800 /* prepare for streaming again */
2802 /* if we started flush, we stop now */
2803 GST_DEBUG_OBJECT (src, "stopping flush");
2804 gst_rtspsrc_flush (src, FALSE, playing);
2807 /* now we did the seek and can activate the new segment values */
2808 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2810 /* if we're doing a segment seek, post a SEGMENT_START message */
2811 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2812 gst_element_post_message (GST_ELEMENT_CAST (src),
2813 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2814 src->segment.format, src->segment.position));
2817 /* now create the newsegment */
2818 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2819 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2822 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2823 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2824 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2825 stream->discont = TRUE;
2828 GST_RTSP_STREAM_UNLOCK (src);
2835 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2840 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2846 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2850 gboolean res = TRUE;
2853 src = GST_RTSPSRC_CAST (parent);
2855 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2856 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2858 switch (GST_EVENT_TYPE (event)) {
2859 case GST_EVENT_SEEK:
2860 res = gst_rtspsrc_perform_seek (src, event);
2864 case GST_EVENT_NAVIGATION:
2865 case GST_EVENT_LATENCY:
2873 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2874 res = gst_pad_send_event (target, event);
2875 gst_object_unref (target);
2877 gst_event_unref (event);
2880 gst_event_unref (event);
2886 /* this is the final event function we receive on the internal source pad when
2887 * we deal with TCP connections */
2889 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2894 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2896 switch (GST_EVENT_TYPE (event)) {
2897 case GST_EVENT_SEEK:
2899 case GST_EVENT_NAVIGATION:
2900 case GST_EVENT_LATENCY:
2902 gst_event_unref (event);
2909 /* this is the final query function we receive on the internal source pad when
2910 * we deal with TCP connections */
2912 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2916 gboolean res = TRUE;
2918 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2920 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2921 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2923 switch (GST_QUERY_TYPE (query)) {
2924 case GST_QUERY_POSITION:
2929 case GST_QUERY_DURATION:
2933 gst_query_parse_duration (query, &format, NULL);
2936 case GST_FORMAT_TIME:
2937 gst_query_set_duration (query, format, src->segment.duration);
2945 case GST_QUERY_LATENCY:
2947 /* we are live with a min latency of 0 and unlimited max latency, this
2948 * result will be updated by the session manager if there is any. */
2949 gst_query_set_latency (query, TRUE, 0, -1);
2959 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2961 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2965 gboolean res = FALSE;
2967 src = GST_RTSPSRC_CAST (parent);
2969 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2970 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2972 switch (GST_QUERY_TYPE (query)) {
2973 case GST_QUERY_DURATION:
2977 gst_query_parse_duration (query, &format, NULL);
2980 case GST_FORMAT_TIME:
2981 gst_query_set_duration (query, format, src->segment.duration);
2989 case GST_QUERY_SEEKING:
2993 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2994 if (format == GST_FORMAT_TIME) {
2996 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2998 /* seeking without duration is unlikely */
2999 seekable = seekable && src->seekable && src->segment.duration &&
3000 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
3002 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0,
3003 src->segment.duration);
3012 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
3014 gst_query_set_uri (query, uri);
3022 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
3024 /* forward the query to the proxy target pad */
3026 res = gst_pad_query (target, query);
3027 gst_object_unref (target);
3036 /* callback for RTCP messages to be sent to the server when operating in TCP
3038 static GstFlowReturn
3039 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
3042 GstRTSPStream *stream;
3043 GstFlowReturn res = GST_FLOW_OK;
3048 GstRTSPMessage message = { 0 };
3049 GstRTSPConnection *conn;
3051 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
3052 src = stream->parent;
3054 gst_buffer_map (buffer, &map, GST_MAP_READ);
3058 gst_rtsp_message_init_data (&message, stream->channel[1]);
3060 /* lend the body data to the message */
3061 gst_rtsp_message_take_body (&message, data, size);
3063 if (stream->conninfo.connection)
3064 conn = stream->conninfo.connection;
3066 conn = src->conninfo.connection;
3068 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
3069 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
3070 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
3072 /* and steal it away again because we will free it when unreffing the
3074 gst_rtsp_message_steal_body (&message, &data, &size);
3075 gst_rtsp_message_unset (&message);
3077 gst_buffer_unmap (buffer, &map);
3078 gst_buffer_unref (buffer);
3083 static GstPadProbeReturn
3084 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3086 GstRTSPSrc *src = user_data;
3088 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
3089 GST_DEBUG_PAD_NAME (pad));
3091 /* activate the streams */
3092 GST_OBJECT_LOCK (src);
3093 if (!src->need_activate)
3096 src->need_activate = FALSE;
3097 GST_OBJECT_UNLOCK (src);
3099 gst_rtspsrc_activate_streams (src);
3101 return GST_PAD_PROBE_OK;
3105 GST_OBJECT_UNLOCK (src);
3106 return GST_PAD_PROBE_OK;
3111 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
3113 GstPad *gpad = GST_PAD_CAST (user_data);
3115 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
3116 gst_pad_store_sticky_event (gpad, *event);
3121 /* this callback is called when the session manager generated a new src pad with
3122 * payloaded RTP packets. We simply ghost the pad here. */
3124 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
3127 GstPadTemplate *template;
3130 GstRTSPStream *stream;
3133 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
3135 GST_RTSP_STATE_LOCK (src);
3137 name = gst_object_get_name (GST_OBJECT_CAST (pad));
3138 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
3139 goto unknown_stream;
3141 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
3143 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
3145 goto unknown_stream;
3148 stream->ssrc = ssrc;
3150 /* we'll add it later see below */
3151 stream->added = TRUE;
3153 /* check if we added all streams */
3155 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
3156 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
3158 GST_DEBUG_OBJECT (src, "stream %p, container %d, added %d, setup %d",
3159 ostream, ostream->container, ostream->added, ostream->setup);
3161 /* if we find a stream for which we did a setup that is not added, we
3162 * need to wait some more */
3163 if (ostream->setup && !ostream->added) {
3168 GST_RTSP_STATE_UNLOCK (src);
3170 /* create a new pad we will use to stream to */
3171 template = gst_static_pad_template_get (&rtptemplate);
3172 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
3173 gst_object_unref (template);
3176 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3177 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3178 gst_pad_set_active (stream->srcpad, TRUE);
3179 gst_pad_sticky_events_foreach (pad, copy_sticky_events, stream->srcpad);
3180 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3183 GST_DEBUG_OBJECT (src, "We added all streams");
3184 /* when we get here, all stream are added and we can fire the no-more-pads
3186 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
3194 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
3195 GST_RTSP_STATE_UNLOCK (src);
3202 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
3206 len = stream->ptmap->len;
3207 for (i = 0; i < len; i++) {
3208 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3216 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
3218 GstRTSPStream *stream;
3221 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
3223 GST_RTSP_STATE_LOCK (src);
3224 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
3226 goto unknown_stream;
3228 if ((caps = stream_get_caps_for_pt (stream, pt)))
3229 gst_caps_ref (caps);
3230 GST_RTSP_STATE_UNLOCK (src);
3236 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
3237 GST_RTSP_STATE_UNLOCK (src);
3243 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
3245 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
3251 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
3257 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
3263 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
3265 GstRTSPSrc *src = stream->parent;
3268 g_object_get (source, "ssrc", &ssrc, NULL);
3270 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
3271 ssrc, stream->ssrc, stream->id);
3273 if (ssrc == stream->ssrc)
3274 gst_rtspsrc_do_stream_eos (src, stream);
3278 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
3280 GstRTSPSrc *src = stream->parent;
3283 g_object_get (source, "ssrc", &ssrc, NULL);
3285 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
3286 ssrc, stream->ssrc, stream->id);
3288 if (ssrc == stream->ssrc)
3289 gst_rtspsrc_do_stream_eos (src, stream);
3293 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
3295 GstRTSPStream *stream;
3297 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
3299 /* get stream for session */
3300 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
3302 gst_rtspsrc_do_stream_eos (src, stream);
3307 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
3309 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
3314 set_manager_buffer_mode (GstRTSPSrc * src)
3316 GObjectClass *klass;
3318 if (src->manager == NULL)
3321 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3323 if (!g_object_class_find_property (klass, "buffer-mode"))
3326 if (src->buffer_mode != BUFFER_MODE_AUTO) {
3327 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
3332 GST_DEBUG_OBJECT (src,
3333 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
3335 if (src->provided_clock) {
3336 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
3338 if (clock == src->provided_clock) {
3339 GST_DEBUG_OBJECT (src, "selected synced");
3340 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
3343 gst_object_unref (clock);
3348 /* Otherwise fall-through and use another buffer mode */
3350 gst_object_unref (clock);
3353 GST_DEBUG_OBJECT (src, "auto buffering mode");
3354 if (src->use_buffering) {
3355 GST_DEBUG_OBJECT (src, "selected buffer");
3356 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
3358 GST_DEBUG_OBJECT (src, "selected slave");
3359 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
3364 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
3366 GST_DEBUG ("request key %u", ssrc);
3367 return gst_caps_ref (stream_get_caps_for_pt (stream, stream->default_pt));
3371 request_rtp_decoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
3373 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3374 if (stream->id != session)
3377 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3378 stream->profile != GST_RTSP_PROFILE_SAVPF)
3381 if (stream->srtpdec == NULL) {
3384 name = g_strdup_printf ("srtpdec_%u", session);
3385 stream->srtpdec = gst_element_factory_make ("srtpdec", name);
3388 g_signal_connect (stream->srtpdec, "request-key",
3389 (GCallback) request_key, stream);
3391 return gst_object_ref (stream->srtpdec);
3395 request_rtcp_encoder (GstElement * rtpbin, guint session,
3396 GstRTSPStream * stream)
3401 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3402 if (stream->id != session)
3405 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3406 stream->profile != GST_RTSP_PROFILE_SAVPF)
3409 if (stream->srtpenc == NULL) {
3412 name = g_strdup_printf ("srtpenc_%u", session);
3413 stream->srtpenc = gst_element_factory_make ("srtpenc", name);
3416 /* get RTCP crypto parameters from caps */
3417 s = gst_caps_get_structure (stream->srtcpparams, 0);
3421 GType ciphertype, authtype;
3422 GValue rtcp_cipher = G_VALUE_INIT, rtcp_auth = G_VALUE_INIT;
3424 ciphertype = g_type_from_name ("GstSrtpCipherType");
3425 authtype = g_type_from_name ("GstSrtpAuthType");
3426 g_value_init (&rtcp_cipher, ciphertype);
3427 g_value_init (&rtcp_auth, authtype);
3429 str = gst_structure_get_string (s, "srtcp-cipher");
3430 gst_value_deserialize (&rtcp_cipher, str);
3431 str = gst_structure_get_string (s, "srtcp-auth");
3432 gst_value_deserialize (&rtcp_auth, str);
3433 gst_structure_get (s, "srtp-key", GST_TYPE_BUFFER, &buf, NULL);
3435 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-cipher",
3437 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-auth",
3439 g_object_set (stream->srtpenc, "key", buf, NULL);
3441 g_value_unset (&rtcp_cipher);
3442 g_value_unset (&rtcp_auth);
3443 gst_buffer_unref (buf);
3446 name = g_strdup_printf ("rtcp_sink_%d", session);
3447 pad = gst_element_get_request_pad (stream->srtpenc, name);
3449 gst_object_unref (pad);
3451 return gst_object_ref (stream->srtpenc);
3455 request_aux_receiver (GstElement * rtpbin, guint sessid, GstRTSPSrc * src)
3457 GstElement *rtx, *bin;
3460 GstRTSPStream *stream;
3462 stream = find_stream (src, &sessid, (gpointer) find_stream_by_id);
3464 GST_WARNING_OBJECT (src, "Stream %u not found", sessid);
3468 GST_INFO_OBJECT (src, "creating retransmision receiver for session %u "
3469 "with map %" GST_PTR_FORMAT, sessid, stream->rtx_pt_map);
3470 bin = gst_bin_new (NULL);
3471 rtx = gst_element_factory_make ("rtprtxreceive", NULL);
3472 g_object_set (rtx, "payload-type-map", stream->rtx_pt_map, NULL);
3473 gst_bin_add (GST_BIN (bin), rtx);
3475 pad = gst_element_get_static_pad (rtx, "src");
3476 name = g_strdup_printf ("src_%u", sessid);
3477 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3479 gst_object_unref (pad);
3481 pad = gst_element_get_static_pad (rtx, "sink");
3482 name = g_strdup_printf ("sink_%u", sessid);
3483 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3485 gst_object_unref (pad);
3491 add_retransmission (GstRTSPSrc * src, GstRTSPTransport * transport)
3495 gboolean do_retransmission = FALSE;
3497 if (transport->trans != GST_RTSP_TRANS_RTP)
3499 if (transport->profile != GST_RTSP_PROFILE_AVPF &&
3500 transport->profile != GST_RTSP_PROFILE_SAVPF)
3503 signal_id = g_signal_lookup ("request-aux-receiver",
3504 G_OBJECT_TYPE (src->manager));
3505 /* there's already something connected */
3506 if (g_signal_handler_find (src->manager, G_SIGNAL_MATCH_ID, signal_id, 0,
3507 NULL, NULL, NULL) != 0) {
3508 GST_DEBUG_OBJECT (src, "Not adding RTX AUX element as "
3509 "\"request-aux-receiver\" signal is "
3510 "already used by the application");
3514 /* build the retransmission payload type map */
3515 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3516 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3517 gboolean do_retransmission_stream = FALSE;
3520 if (stream->rtx_pt_map)
3521 gst_structure_free (stream->rtx_pt_map);
3522 stream->rtx_pt_map = gst_structure_new_empty ("application/x-rtp-pt-map");
3524 for (i = 0; i < stream->ptmap->len; i++) {
3525 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3526 GstStructure *s = gst_caps_get_structure (item->caps, 0);
3527 const gchar *encoding;
3529 /* we only care about RTX streams */
3530 if ((encoding = gst_structure_get_string (s, "encoding-name"))
3531 && g_strcmp0 (encoding, "RTX") == 0) {
3532 const gchar *stream_pt_s;
3535 if (gst_structure_get_int (s, "payload", &rtx_pt)
3536 && (stream_pt_s = gst_structure_get_string (s, "apt"))) {
3539 gst_structure_set (stream->rtx_pt_map, stream_pt_s, G_TYPE_UINT,
3541 do_retransmission_stream = TRUE;
3547 if (do_retransmission_stream) {
3548 GST_DEBUG_OBJECT (src, "built retransmission payload map for stream "
3549 "id %i: %" GST_PTR_FORMAT, stream->id, stream->rtx_pt_map);
3550 do_retransmission = TRUE;
3552 GST_DEBUG_OBJECT (src, "no retransmission payload map for stream "
3553 "id %i", stream->id);
3554 gst_structure_free (stream->rtx_pt_map);
3555 stream->rtx_pt_map = NULL;
3559 if (do_retransmission) {
3560 GST_DEBUG_OBJECT (src, "Enabling retransmissions");
3562 g_object_set (src->manager, "do-retransmission", TRUE, NULL);
3564 /* enable RFC4588 retransmission handling by setting rtprtxreceive
3565 * as the "aux" element of rtpbin */
3566 g_signal_connect (src->manager, "request-aux-receiver",
3567 (GCallback) request_aux_receiver, src);
3569 GST_DEBUG_OBJECT (src,
3570 "Not enabling retransmissions as no stream had a retransmission payload map");
3574 /* try to get and configure a manager */
3576 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
3577 GstRTSPTransport * transport)
3579 const gchar *manager;
3581 GstStateChangeReturn ret;
3583 /* find a manager */
3584 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
3588 GST_DEBUG_OBJECT (src, "using manager %s", manager);
3590 /* configure the manager */
3591 if (src->manager == NULL) {
3592 GObjectClass *klass;
3594 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
3596 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
3600 goto use_no_manager;
3602 if (!(src->manager = gst_element_factory_make (manager, "manager")))
3603 goto manager_failed;
3606 /* we manage this element */
3607 gst_element_set_locked_state (src->manager, TRUE);
3608 gst_bin_add (GST_BIN_CAST (src), src->manager);
3610 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
3611 if (ret == GST_STATE_CHANGE_FAILURE)
3612 goto start_manager_failure;
3614 g_object_set (src->manager, "latency", src->latency, NULL);
3616 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3618 if (g_object_class_find_property (klass, "ntp-sync")) {
3619 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
3622 if (src->use_pipeline_clock) {
3623 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
3624 g_object_set (src->manager, "use-pipeline-clock", TRUE, NULL);
3627 if (g_object_class_find_property (klass, "ntp-time-source")) {
3628 g_object_set (src->manager, "ntp-time-source", src->ntp_time_source,
3633 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
3634 g_object_set (src->manager, "sdes", src->sdes, NULL);
3637 if (g_object_class_find_property (klass, "drop-on-latency")) {
3638 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
3642 /* buffer mode pauses are handled by adding offsets to buffer times,
3643 * but some depayloaders may have a hard time syncing output times
3644 * with such input times, e.g. container ones, most notably ASF */
3645 /* TODO alternatives are having an event that indicates these shifts,
3646 * or having rtsp extensions provide suggestion on buffer mode */
3647 /* valid duration implies not likely live pipeline,
3648 * so slaving in jitterbuffer does not make much sense
3649 * (and might mess things up due to bursts) */
3650 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
3651 src->segment.duration && stream->container) {
3652 src->use_buffering = TRUE;
3654 src->use_buffering = FALSE;
3657 set_manager_buffer_mode (src);
3659 /* connect to signals */
3660 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
3662 src->manager_sig_id =
3663 g_signal_connect (src->manager, "pad-added",
3664 (GCallback) new_manager_pad, src);
3665 src->manager_ptmap_id =
3666 g_signal_connect (src->manager, "request-pt-map",
3667 (GCallback) request_pt_map, src);
3669 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
3672 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
3675 if (src->do_retransmission)
3676 add_retransmission (src, transport);
3678 g_signal_connect (src->manager, "request-rtp-decoder",
3679 (GCallback) request_rtp_decoder, stream);
3680 g_signal_connect (src->manager, "request-rtcp-decoder",
3681 (GCallback) request_rtp_decoder, stream);
3682 g_signal_connect (src->manager, "request-rtcp-encoder",
3683 (GCallback) request_rtcp_encoder, stream);
3685 /* we stream directly to the manager, get some pads. Each RTSP stream goes
3686 * into a separate RTP session. */
3687 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
3688 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
3690 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
3691 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
3694 /* now configure the bandwidth in the manager */
3695 if (g_signal_lookup ("get-internal-session",
3696 G_OBJECT_TYPE (src->manager)) != 0) {
3697 GObject *rtpsession;
3699 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
3702 GstRTPProfile rtp_profile;
3704 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
3706 stream->session = rtpsession;
3708 if (stream->as_bandwidth != -1) {
3709 GST_INFO_OBJECT (src, "setting AS: %f",
3710 (gdouble) (stream->as_bandwidth * 1000));
3711 g_object_set (rtpsession, "bandwidth",
3712 (gdouble) (stream->as_bandwidth * 1000), NULL);
3714 if (stream->rr_bandwidth != -1) {
3715 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
3716 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
3719 if (stream->rs_bandwidth != -1) {
3720 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
3721 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
3725 switch (stream->profile) {
3726 case GST_RTSP_PROFILE_AVPF:
3727 rtp_profile = GST_RTP_PROFILE_AVPF;
3729 case GST_RTSP_PROFILE_SAVP:
3730 rtp_profile = GST_RTP_PROFILE_SAVP;
3732 case GST_RTSP_PROFILE_SAVPF:
3733 rtp_profile = GST_RTP_PROFILE_SAVPF;
3735 case GST_RTSP_PROFILE_AVP:
3737 rtp_profile = GST_RTP_PROFILE_AVP;
3741 g_object_set (rtpsession, "rtp-profile", rtp_profile, NULL);
3743 g_object_set (rtpsession, "probation", src->probation, NULL);
3745 g_object_set (rtpsession, "internal-ssrc", stream->send_ssrc, NULL);
3747 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
3749 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
3751 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
3753 g_signal_connect (rtpsession, "on-ssrc-active",
3754 (GCallback) on_ssrc_active, stream);
3765 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3770 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
3773 start_manager_failure:
3775 GST_DEBUG_OBJECT (src, "could not start session manager");
3780 /* free the UDP sources allocated when negotiating a transport.
3781 * This function is called when the server negotiated to a transport where the
3782 * UDP sources are not needed anymore, such as TCP or multicast. */
3784 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
3788 for (i = 0; i < 2; i++) {
3789 if (stream->udpsrc[i]) {
3790 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
3791 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
3792 gst_object_unref (stream->udpsrc[i]);
3793 stream->udpsrc[i] = NULL;
3798 /* for TCP, create pads to send and receive data to and from the manager and to
3799 * intercept various events and queries
3802 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
3803 GstRTSPTransport * transport, GstPad ** outpad)
3806 GstPadTemplate *template;
3807 GstPad *pad0, *pad1;
3809 /* configure for interleaved delivery, nothing needs to be done
3810 * here, the loop function will call the chain functions of the
3811 * session manager. */
3812 stream->channel[0] = transport->interleaved.min;
3813 stream->channel[1] = transport->interleaved.max;
3814 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
3815 stream->channel[0], stream->channel[1]);
3817 /* we can remove the allocated UDP ports now */
3818 gst_rtspsrc_stream_free_udp (stream);
3820 /* no session manager, send data to srcpad directly */
3821 if (!stream->channelpad[0]) {
3822 GST_DEBUG_OBJECT (src, "no manager, creating pad");
3824 /* create a new pad we will use to stream to */
3825 name = g_strdup_printf ("stream_%u", stream->id);
3826 template = gst_static_pad_template_get (&rtptemplate);
3827 stream->channelpad[0] = gst_pad_new_from_template (template, name);
3828 gst_object_unref (template);
3831 /* set caps and activate */
3832 gst_pad_use_fixed_caps (stream->channelpad[0]);
3833 gst_pad_set_active (stream->channelpad[0], TRUE);
3835 *outpad = gst_object_ref (stream->channelpad[0]);
3837 GST_DEBUG_OBJECT (src, "using manager source pad");
3839 template = gst_static_pad_template_get (&anysrctemplate);
3841 /* allocate pads for sending the channel data into the manager */
3842 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
3843 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
3844 gst_object_unref (stream->channelpad[0]);
3845 stream->channelpad[0] = pad0;
3846 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
3847 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
3848 gst_pad_set_element_private (pad0, src);
3849 gst_pad_set_active (pad0, TRUE);
3851 if (stream->channelpad[1]) {
3852 /* if we have a sinkpad for the other channel, create a pad and link to the
3854 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
3855 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
3856 gst_pad_link_full (pad1, stream->channelpad[1],
3857 GST_PAD_LINK_CHECK_NOTHING);
3858 gst_object_unref (stream->channelpad[1]);
3859 stream->channelpad[1] = pad1;
3860 gst_pad_set_active (pad1, TRUE);
3862 gst_object_unref (template);
3864 /* setup RTCP transport back to the server if we have to. */
3865 if (src->manager && src->do_rtcp) {
3868 template = gst_static_pad_template_get (&anysinktemplate);
3870 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
3871 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
3872 gst_pad_set_element_private (stream->rtcppad, stream);
3873 gst_pad_set_active (stream->rtcppad, TRUE);
3875 /* get session RTCP pad */
3876 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3877 pad = gst_element_get_request_pad (src->manager, name);
3882 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3883 gst_object_unref (pad);
3886 gst_object_unref (template);
3892 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
3893 GstRTSPTransport * transport, const gchar ** destination, gint * min,
3894 gint * max, guint * ttl)
3896 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3898 if (!(*destination = transport->destination))
3899 *destination = stream->destination;
3902 /* transport first */
3903 *min = transport->port.min;
3904 *max = transport->port.max;
3905 if (*min == -1 && *max == -1) {
3906 /* then try from SDP */
3907 if (stream->port != 0) {
3908 *min = stream->port;
3909 *max = stream->port + 1;
3915 if (!(*ttl = transport->ttl))
3920 /* first take the source, then the endpoint to figure out where to send
3922 if (!(*destination = transport->source)) {
3923 if (src->conninfo.connection)
3924 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3925 else if (stream->conninfo.connection)
3927 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3931 /* for unicast we only expect the ports here */
3932 *min = transport->server_port.min;
3933 *max = transport->server_port.max;
3938 /* For multicast create UDP sources and join the multicast group. */
3940 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3941 GstRTSPTransport * transport, GstPad ** outpad)
3944 const gchar *destination;
3947 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3949 /* we can remove the allocated UDP ports now */
3950 gst_rtspsrc_stream_free_udp (stream);
3952 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3955 /* we need a destination now */
3956 if (destination == NULL)
3957 goto no_destination;
3959 /* we really need ports now or we won't be able to receive anything at all */
3960 if (min == -1 && max == -1)
3963 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3964 destination, min, max);
3966 /* creating UDP source for RTP */
3968 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3970 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3972 if (stream->udpsrc[0] == NULL)
3975 /* take ownership */
3976 gst_object_ref_sink (stream->udpsrc[0]);
3978 if (src->udp_buffer_size != 0)
3979 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3980 src->udp_buffer_size, NULL);
3982 if (src->multi_iface != NULL)
3983 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3984 src->multi_iface, NULL);
3987 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3988 gst_element_set_state (stream->udpsrc[0], GST_STATE_READY);
3991 /* creating another UDP source for RTCP */
3995 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3997 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3999 if (stream->udpsrc[1] == NULL)
4002 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
4003 stream->profile == GST_RTSP_PROFILE_SAVPF)
4004 caps = gst_caps_new_empty_simple ("application/x-srtcp");
4006 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4007 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
4008 gst_caps_unref (caps);
4010 /* take ownership */
4011 gst_object_ref_sink (stream->udpsrc[1]);
4013 if (src->multi_iface != NULL)
4014 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
4015 src->multi_iface, NULL);
4017 gst_element_set_state (stream->udpsrc[1], GST_STATE_READY);
4024 GST_DEBUG_OBJECT (src, "no UDP source element found");
4029 GST_DEBUG_OBJECT (src, "no destination found");
4034 GST_DEBUG_OBJECT (src, "no ports found");
4039 /* configure the remainder of the UDP ports */
4041 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
4042 GstRTSPTransport * transport, GstPad ** outpad)
4044 /* we manage the UDP elements now. For unicast, the UDP sources where
4045 * allocated in the stream when we suggested a transport. */
4046 if (stream->udpsrc[0]) {
4049 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
4050 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
4052 GST_DEBUG_OBJECT (src, "setting up UDP source");
4054 /* configure a timeout on the UDP port. When the timeout message is
4055 * posted, we assume UDP transport is not possible. We reconnect using TCP
4057 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
4058 src->udp_timeout * 1000, NULL);
4060 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt)))
4061 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
4063 /* get output pad of the UDP source. */
4064 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
4066 /* save it so we can unblock */
4067 stream->blockedpad = *outpad;
4069 /* configure pad block on the pad. As soon as there is dataflow on the
4070 * UDP source, we know that UDP is not blocked by a firewall and we can
4071 * configure all the streams to let the application autoplug decoders. */
4073 gst_pad_add_probe (stream->blockedpad,
4074 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
4075 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocked, src, NULL);
4077 if (stream->channelpad[0]) {
4078 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
4079 /* configure for UDP delivery, we need to connect the UDP pads to
4080 * the session plugin. */
4081 gst_pad_link_full (*outpad, stream->channelpad[0],
4082 GST_PAD_LINK_CHECK_NOTHING);
4083 gst_object_unref (*outpad);
4085 /* we connected to pad-added signal to get pads from the manager */
4087 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
4092 if (stream->udpsrc[1]) {
4095 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
4096 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
4098 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
4099 stream->profile == GST_RTSP_PROFILE_SAVPF)
4100 caps = gst_caps_new_empty_simple ("application/x-srtcp");
4102 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4103 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
4104 gst_caps_unref (caps);
4106 if (stream->channelpad[1]) {
4109 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
4111 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
4112 gst_pad_link_full (pad, stream->channelpad[1],
4113 GST_PAD_LINK_CHECK_NOTHING);
4114 gst_object_unref (pad);
4116 /* leave unlinked */
4122 /* configure the UDP sink back to the server for status reports */
4124 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
4125 GstRTSPStream * stream, GstRTSPTransport * transport)
4128 gint rtp_port, rtcp_port;
4129 gboolean do_rtp, do_rtcp;
4130 const gchar *destination;
4135 /* get transport info */
4136 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
4137 &rtp_port, &rtcp_port, &ttl);
4139 /* see what we need to do */
4140 do_rtp = (rtp_port != -1);
4141 /* it's possible that the server does not want us to send RTCP in which case
4143 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
4145 /* we need a destination when we have RTP or RTCP ports */
4146 if (destination == NULL && (do_rtp || do_rtcp))
4147 goto no_destination;
4149 /* try to construct the fakesrc to the RTP port of the server to open up any
4152 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
4155 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
4156 stream->udpsink[0] =
4157 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
4159 if (stream->udpsink[0] == NULL)
4160 goto no_sink_element;
4162 /* don't join multicast group, we will have the source socket do that */
4163 /* no sync or async state changes needed */
4164 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
4165 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
4167 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
4169 if (stream->udpsrc[0]) {
4170 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
4171 * so that NAT firewalls will open a hole for us */
4172 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
4173 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
4174 /* configure socket and make sure udpsink does not close it when shutting
4175 * down, it belongs to udpsrc after all. */
4176 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
4177 "close-socket", FALSE, NULL);
4178 g_object_unref (socket);
4181 /* the source for the dummy packets to open up NAT */
4182 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
4183 if (stream->fakesrc == NULL)
4184 goto no_fakesrc_element;
4186 /* random data in 5 buffers, a size of 200 bytes should be fine */
4187 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
4188 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
4190 /* we don't want to consider this a sink */
4191 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
4193 /* keep everything locked */
4194 gst_element_set_locked_state (stream->udpsink[0], TRUE);
4195 gst_element_set_locked_state (stream->fakesrc, TRUE);
4197 gst_object_ref (stream->udpsink[0]);
4198 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
4199 gst_object_ref (stream->fakesrc);
4200 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
4202 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
4203 "sink", GST_PAD_LINK_CHECK_NOTHING);
4206 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
4209 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
4210 stream->udpsink[1] =
4211 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
4213 if (stream->udpsink[1] == NULL)
4214 goto no_sink_element;
4216 /* don't join multicast group, we will have the source socket do that */
4217 /* no sync or async state changes needed */
4218 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
4219 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
4221 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
4223 if (stream->udpsrc[1]) {
4224 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
4225 * because some servers check the port number of where it sends RTCP to identify
4226 * the RTCP packets it receives */
4227 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
4228 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
4229 /* configure socket and make sure udpsink does not close it when shutting
4230 * down, it belongs to udpsrc after all. */
4231 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
4232 "close-socket", FALSE, NULL);
4233 g_object_unref (socket);
4236 /* we don't want to consider this a sink */
4237 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
4239 /* we keep this playing always */
4240 gst_element_set_locked_state (stream->udpsink[1], TRUE);
4241 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
4243 gst_object_ref (stream->udpsink[1]);
4244 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
4246 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
4248 /* get session RTCP pad */
4249 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
4250 pad = gst_element_get_request_pad (src->manager, name);
4255 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
4256 gst_object_unref (pad);
4265 GST_DEBUG_OBJECT (src, "no destination address specified");
4270 GST_DEBUG_OBJECT (src, "no UDP sink element found");
4275 GST_DEBUG_OBJECT (src, "no fakesrc element found");
4280 /* sets up all elements needed for streaming over the specified transport.
4281 * Does not yet expose the element pads, this will be done when there is actuall
4282 * dataflow detected, which might never happen when UDP is blocked in a
4283 * firewall, for example.
4286 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
4287 GstRTSPTransport * transport)
4290 GstPad *outpad = NULL;
4291 GstPadTemplate *template;
4293 const gchar *media_type;
4296 src = stream->parent;
4298 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
4300 /* get the proper media type for this stream now */
4301 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
4302 goto unknown_transport;
4304 goto unknown_transport;
4306 /* configure the final media type */
4307 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
4309 len = stream->ptmap->len;
4310 for (i = 0; i < len; i++) {
4312 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
4314 if (item->caps == NULL)
4317 s = gst_caps_get_structure (item->caps, 0);
4318 gst_structure_set_name (s, media_type);
4319 /* set ssrc if known */
4320 if (transport->ssrc)
4321 gst_structure_set (s, "ssrc", G_TYPE_UINT, transport->ssrc, NULL);
4324 /* try to get and configure a manager, channelpad[0-1] will be configured with
4325 * the pads for the manager, or NULL when no manager is needed. */
4326 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
4329 switch (transport->lower_transport) {
4330 case GST_RTSP_LOWER_TRANS_TCP:
4331 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
4332 goto transport_failed;
4334 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
4335 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
4336 goto transport_failed;
4337 /* fallthrough, the rest is the same for UDP and MCAST */
4338 case GST_RTSP_LOWER_TRANS_UDP:
4339 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
4340 goto transport_failed;
4341 /* configure udpsinks back to the server for RTCP messages and for the
4342 * dummy RTP messages to open NAT. */
4343 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
4344 goto transport_failed;
4347 goto unknown_transport;
4351 GST_DEBUG_OBJECT (src, "creating ghostpad");
4353 gst_pad_use_fixed_caps (outpad);
4355 /* create ghostpad, don't add just yet, this will be done when we activate
4357 name = g_strdup_printf ("stream_%u", stream->id);
4358 template = gst_static_pad_template_get (&rtptemplate);
4359 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
4360 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
4361 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
4362 gst_object_unref (template);
4365 gst_object_unref (outpad);
4367 /* mark pad as ok */
4368 stream->last_ret = GST_FLOW_OK;
4375 GST_DEBUG_OBJECT (src, "failed to configure transport");
4380 GST_DEBUG_OBJECT (src, "unknown transport");
4385 GST_DEBUG_OBJECT (src, "cannot get a session manager");
4390 /* send a couple of dummy random packets on the receiver RTP port to the server,
4391 * this should make a firewall think we initiated the data transfer and
4392 * hopefully allow packets to go from the sender port to our RTP receiver port */
4394 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
4398 if (src->nat_method != GST_RTSP_NAT_DUMMY)
4401 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4402 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4404 if (stream->fakesrc && stream->udpsink[0]) {
4405 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
4406 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
4407 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
4408 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
4409 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
4415 /* Adds the source pads of all configured streams to the element.
4416 * This code is performed when we detected dataflow.
4418 * We detect dataflow from either the _loop function or with pad probes on the
4422 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
4426 GST_DEBUG_OBJECT (src, "activating streams");
4428 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4429 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4431 if (stream->udpsrc[0]) {
4432 /* remove timeout, we are streaming now and timeouts will be handled by
4433 * the session manager and jitter buffer */
4434 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
4436 if (stream->srcpad) {
4437 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
4438 gst_pad_set_active (stream->srcpad, TRUE);
4440 /* if we don't have a session manager, set the caps now. If we have a
4441 * session, we will get a notification of the pad and the caps. */
4442 if (!src->manager) {
4445 caps = stream_get_caps_for_pt (stream, stream->default_pt);
4446 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
4447 gst_pad_set_caps (stream->srcpad, caps);
4450 if (!stream->added) {
4451 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
4452 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
4453 stream->added = TRUE;
4458 /* unblock all pads */
4459 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4460 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4462 if (stream->blockid) {
4463 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
4464 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
4465 stream->blockid = 0;
4473 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
4474 gboolean reset_manager)
4477 guint64 start, stop;
4478 gdouble play_speed, play_scale;
4480 GST_DEBUG_OBJECT (src, "configuring stream caps");
4482 start = segment->position;
4483 stop = segment->duration;
4484 play_speed = segment->rate;
4485 play_scale = segment->applied_rate;
4487 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4488 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4494 len = stream->ptmap->len;
4495 for (j = 0; j < len; j++) {
4497 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
4499 if (item->caps == NULL)
4502 caps = gst_caps_make_writable (item->caps);
4504 if (stream->timebase != -1)
4505 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
4506 (guint) stream->timebase, NULL);
4507 if (stream->seqbase != -1)
4508 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
4509 (guint) stream->seqbase, NULL);
4510 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
4512 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
4513 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
4514 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
4517 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
4520 if (item->pt == stream->default_pt && stream->udpsrc[0]) {
4521 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
4525 if (reset_manager && src->manager) {
4526 GST_DEBUG_OBJECT (src, "clear session");
4527 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
4531 static GstFlowReturn
4532 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
4537 /* store the value */
4538 stream->last_ret = ret;
4540 /* if it's success we can return the value right away */
4541 if (ret == GST_FLOW_OK)
4544 /* any other error that is not-linked can be returned right
4546 if (ret != GST_FLOW_NOT_LINKED)
4549 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4550 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4551 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4553 ret = ostream->last_ret;
4554 /* some other return value (must be SUCCESS but we can return
4555 * other values as well) */
4556 if (ret != GST_FLOW_NOT_LINKED)
4559 /* if we get here, all other pads were unlinked and we return
4560 * NOT_LINKED then */
4566 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
4569 gboolean res = TRUE;
4571 /* only streams that have a connection to the outside world */
4575 if (stream->udpsrc[0]) {
4576 gst_event_ref (event);
4577 res = gst_element_send_event (stream->udpsrc[0], event);
4578 } else if (stream->channelpad[0]) {
4579 gst_event_ref (event);
4580 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4581 res = gst_pad_push_event (stream->channelpad[0], event);
4583 res = gst_pad_send_event (stream->channelpad[0], event);
4586 if (stream->udpsrc[1]) {
4587 gst_event_ref (event);
4588 res &= gst_element_send_event (stream->udpsrc[1], event);
4589 } else if (stream->channelpad[1]) {
4590 gst_event_ref (event);
4591 if (GST_PAD_IS_SRC (stream->channelpad[1]))
4592 res &= gst_pad_push_event (stream->channelpad[1], event);
4594 res &= gst_pad_send_event (stream->channelpad[1], event);
4598 gst_event_unref (event);
4604 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
4607 gboolean res = TRUE;
4609 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4610 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4612 gst_event_ref (event);
4613 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
4615 gst_event_unref (event);
4620 static GstRTSPResult
4621 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4626 if (info->connection == NULL) {
4627 if (info->url == NULL) {
4628 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
4629 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
4633 /* create connection */
4634 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
4635 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
4636 goto could_not_create;
4639 g_free (info->url_str);
4640 info->url_str = gst_rtsp_url_get_request_uri (info->url);
4642 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
4644 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
4645 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
4646 src->tls_validation_flags))
4647 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
4649 if (src->tls_database)
4650 gst_rtsp_connection_set_tls_database (info->connection,
4653 if (src->tls_interaction)
4654 gst_rtsp_connection_set_tls_interaction (info->connection,
4655 src->tls_interaction);
4658 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
4659 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
4661 if (src->proxy_host) {
4662 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
4664 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
4669 if (!info->connected) {
4672 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
4673 ("Connecting to %s", info->location));
4674 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
4676 gst_rtsp_connection_connect (info->connection,
4677 src->ptcp_timeout)) < 0)
4678 goto could_not_connect;
4680 info->connected = TRUE;
4687 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
4692 gchar *str = gst_rtsp_strresult (res);
4693 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
4699 gchar *str = gst_rtsp_strresult (res);
4700 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
4706 static GstRTSPResult
4707 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
4710 GST_RTSP_STATE_LOCK (src);
4711 if (info->connected) {
4712 GST_DEBUG_OBJECT (src, "closing connection...");
4713 gst_rtsp_connection_close (info->connection);
4714 info->connected = FALSE;
4716 if (free && info->connection) {
4717 /* free connection */
4718 GST_DEBUG_OBJECT (src, "freeing connection...");
4719 gst_rtsp_connection_free (info->connection);
4720 info->connection = NULL;
4722 GST_RTSP_STATE_UNLOCK (src);
4726 static GstRTSPResult
4727 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4732 GST_DEBUG_OBJECT (src, "reconnecting connection...");
4733 gst_rtsp_conninfo_close (src, info, FALSE);
4734 res = gst_rtsp_conninfo_connect (src, info, async);
4740 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
4744 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
4745 GST_RTSP_STATE_LOCK (src);
4746 if (src->conninfo.connection && src->conninfo.flushing != flush) {
4747 GST_DEBUG_OBJECT (src, "connection flush");
4748 gst_rtsp_connection_flush (src->conninfo.connection, flush);
4749 src->conninfo.flushing = flush;
4751 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4752 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4753 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
4754 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
4755 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
4756 stream->conninfo.flushing = flush;
4759 GST_RTSP_STATE_UNLOCK (src);
4762 static GstRTSPResult
4763 gst_rtspsrc_init_request (GstRTSPSrc * src, GstRTSPMessage * msg,
4764 GstRTSPMethod method, const gchar * uri)
4768 res = gst_rtsp_message_init_request (msg, method, uri);
4772 /* set user-agent */
4773 if (src->user_agent)
4774 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_USER_AGENT, src->user_agent);
4779 /* FIXME, handle server request, reply with OK, for now */
4780 static GstRTSPResult
4781 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
4782 GstRTSPMessage * request)
4784 GstRTSPMessage response = { 0 };
4787 GST_DEBUG_OBJECT (src, "got server request message");
4790 gst_rtsp_message_dump (request);
4792 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
4794 if (res == GST_RTSP_ENOTIMPL) {
4795 /* default implementation, send OK */
4796 GST_DEBUG_OBJECT (src, "prepare OK reply");
4798 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
4803 /* let app parse and reply */
4804 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
4805 0, request, &response);
4808 gst_rtsp_message_dump (&response);
4810 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
4814 gst_rtsp_message_unset (&response);
4815 } else if (res == GST_RTSP_EEOF)
4823 gst_rtsp_message_unset (&response);
4828 /* send server keep-alive */
4829 static GstRTSPResult
4830 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
4832 GstRTSPMessage request = { 0 };
4834 GstRTSPMethod method;
4835 const gchar *control;
4837 if (src->do_rtsp_keep_alive == FALSE) {
4838 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
4839 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4843 GST_DEBUG_OBJECT (src, "creating server keep-alive");
4845 /* find a method to use for keep-alive */
4846 if (src->methods & GST_RTSP_GET_PARAMETER)
4847 method = GST_RTSP_GET_PARAMETER;
4849 method = GST_RTSP_OPTIONS;
4851 control = get_aggregate_control (src);
4852 if (control == NULL)
4855 res = gst_rtspsrc_init_request (src, &request, method, control);
4860 gst_rtsp_message_dump (&request);
4863 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
4868 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4869 gst_rtsp_message_unset (&request);
4876 GST_WARNING_OBJECT (src, "no control url to send keepalive");
4881 gchar *str = gst_rtsp_strresult (res);
4883 gst_rtsp_message_unset (&request);
4884 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
4885 ("Could not send keep-alive. (%s)", str));
4891 static GstFlowReturn
4892 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
4894 GstFlowReturn ret = GST_FLOW_OK;
4896 GstRTSPStream *stream;
4897 GstPad *outpad = NULL;
4903 channel = message->type_data.data.channel;
4905 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
4907 goto unknown_stream;
4909 if (channel == stream->channel[0]) {
4910 outpad = stream->channelpad[0];
4912 } else if (channel == stream->channel[1]) {
4913 outpad = stream->channelpad[1];
4919 /* take a look at the body to figure out what we have */
4920 gst_rtsp_message_get_body (message, &data, &size);
4922 goto invalid_length;
4924 /* channels are not correct on some servers, do extra check */
4925 if (data[1] >= 200 && data[1] <= 204) {
4926 /* hmm RTCP message switch to the RTCP pad of the same stream. */
4927 outpad = stream->channelpad[1];
4931 /* we have no clue what this is, just ignore then. */
4933 goto unknown_stream;
4935 /* take the message body for further processing */
4936 gst_rtsp_message_steal_body (message, &data, &size);
4938 /* strip the trailing \0 */
4941 buf = gst_buffer_new ();
4942 gst_buffer_append_memory (buf,
4943 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
4945 /* don't need message anymore */
4946 gst_rtsp_message_unset (message);
4948 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
4951 if (src->need_activate) {
4957 guint group_id = gst_util_group_id_next ();
4959 /* generate an SHA256 sum of the URI */
4960 cs = g_checksum_new (G_CHECKSUM_SHA256);
4961 uri = src->conninfo.location;
4962 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
4964 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4965 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4969 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
4970 event = gst_event_new_stream_start (stream_id);
4971 gst_event_set_group_id (event, group_id);
4974 gst_rtspsrc_stream_push_event (src, ostream, event);
4976 if ((caps = stream_get_caps_for_pt (ostream, ostream->default_pt))) {
4977 /* only streams that have a connection to the outside world */
4978 if (ostream->setup) {
4979 if (ostream->udpsrc[0]) {
4980 gst_element_send_event (ostream->udpsrc[0],
4981 gst_event_new_caps (caps));
4982 } else if (ostream->channelpad[0]) {
4983 if (GST_PAD_IS_SRC (ostream->channelpad[0]))
4984 gst_pad_push_event (ostream->channelpad[0],
4985 gst_event_new_caps (caps));
4987 gst_pad_send_event (ostream->channelpad[0],
4988 gst_event_new_caps (caps));
4991 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4993 if (ostream->udpsrc[1]) {
4994 gst_element_send_event (ostream->udpsrc[1],
4995 gst_event_new_caps (caps));
4996 } else if (ostream->channelpad[1]) {
4997 if (GST_PAD_IS_SRC (ostream->channelpad[1]))
4998 gst_pad_push_event (ostream->channelpad[1],
4999 gst_event_new_caps (caps));
5001 gst_pad_send_event (ostream->channelpad[1],
5002 gst_event_new_caps (caps));
5005 gst_caps_unref (caps);
5009 g_checksum_free (cs);
5011 gst_rtspsrc_activate_streams (src);
5012 src->need_activate = FALSE;
5013 src->need_segment = TRUE;
5016 if (src->base_time == -1) {
5017 /* Take current running_time. This timestamp will be put on
5018 * the first buffer of each stream because we are a live source and so we
5019 * timestamp with the running_time. When we are dealing with TCP, we also
5020 * only timestamp the first buffer (using the DISCONT flag) because a server
5021 * typically bursts data, for which we don't want to compensate by speeding
5022 * up the media. The other timestamps will be interpollated from this one
5023 * using the RTP timestamps. */
5024 GST_OBJECT_LOCK (src);
5025 if (GST_ELEMENT_CLOCK (src)) {
5027 GstClockTime base_time;
5029 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
5030 base_time = GST_ELEMENT_CAST (src)->base_time;
5032 src->base_time = now - base_time;
5034 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
5035 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
5037 GST_OBJECT_UNLOCK (src);
5040 /* If needed send a new segment, don't forget we are live and buffer are
5041 * timestamped with running time */
5042 if (src->need_segment) {
5044 src->need_segment = FALSE;
5045 gst_segment_init (&segment, GST_FORMAT_TIME);
5046 gst_rtspsrc_push_event (src, gst_event_new_segment (&segment));
5049 if (stream->discont && !is_rtcp) {
5050 /* mark first RTP buffer as discont */
5051 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
5052 stream->discont = FALSE;
5053 /* first buffer gets the timestamp, other buffers are not timestamped and
5054 * their presentation time will be interpollated from the rtp timestamps. */
5055 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
5056 GST_TIME_ARGS (src->base_time));
5058 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
5061 /* chain to the peer pad */
5062 if (GST_PAD_IS_SINK (outpad))
5063 ret = gst_pad_chain (outpad, buf);
5065 ret = gst_pad_push (outpad, buf);
5068 /* combine all stream flows for the data transport */
5069 ret = gst_rtspsrc_combine_flows (src, stream, ret);
5076 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
5077 gst_rtsp_message_unset (message);
5082 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5083 ("Short message received, ignoring."));
5084 gst_rtsp_message_unset (message);
5089 static GstFlowReturn
5090 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
5092 GstRTSPMessage message = { 0 };
5094 GstFlowReturn ret = GST_FLOW_OK;
5095 GTimeVal tv_timeout;
5098 /* get the next timeout interval */
5099 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
5101 /* see if the timeout period expired */
5102 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
5103 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
5104 /* send keep-alive, only act on interrupt, a warning will be posted for
5106 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5108 /* get new timeout */
5109 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
5112 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
5113 tv_timeout.tv_sec, tv_timeout.tv_usec);
5115 /* protect the connection with the connection lock so that we can see when
5116 * we are finished doing server communication */
5118 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
5119 &message, src->ptcp_timeout);
5123 GST_DEBUG_OBJECT (src, "we received a server message");
5125 case GST_RTSP_EINTR:
5126 /* we got interrupted this means we need to stop */
5128 case GST_RTSP_ETIMEOUT:
5129 /* no reply, send keep alive */
5130 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
5131 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5135 /* go EOS when the server closed the connection */
5141 switch (message.type) {
5142 case GST_RTSP_MESSAGE_REQUEST:
5143 /* server sends us a request message, handle it */
5145 gst_rtspsrc_handle_request (src, src->conninfo.connection,
5147 if (res == GST_RTSP_EEOF)
5150 goto handle_request_failed;
5152 case GST_RTSP_MESSAGE_RESPONSE:
5153 /* we ignore response messages */
5154 GST_DEBUG_OBJECT (src, "ignoring response message");
5156 gst_rtsp_message_dump (&message);
5158 case GST_RTSP_MESSAGE_DATA:
5159 GST_DEBUG_OBJECT (src, "got data message");
5160 ret = gst_rtspsrc_handle_data (src, &message);
5161 if (ret != GST_FLOW_OK)
5162 goto handle_data_failed;
5165 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5170 g_assert_not_reached ();
5175 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5176 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5177 ("The server closed the connection."));
5178 src->conninfo.connected = FALSE;
5179 gst_rtsp_message_unset (&message);
5180 return GST_FLOW_EOS;
5184 gst_rtsp_message_unset (&message);
5185 GST_DEBUG_OBJECT (src, "got interrupted");
5186 return GST_FLOW_FLUSHING;
5190 gchar *str = gst_rtsp_strresult (res);
5192 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
5193 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_BAD_SERVER,
5194 "Could not receive message.");
5196 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5197 ("Could not receive message. (%s)", str));
5201 gst_rtsp_message_unset (&message);
5202 return GST_FLOW_ERROR;
5204 handle_request_failed:
5206 gchar *str = gst_rtsp_strresult (res);
5208 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
5209 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_SERVICE_UNAVAILABLE,
5210 "Could not handle server message.");
5212 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5213 ("Could not handle server message. (%s)", str));
5216 gst_rtsp_message_unset (&message);
5217 return GST_FLOW_ERROR;
5221 GST_DEBUG_OBJECT (src, "could no handle data message");
5226 static GstFlowReturn
5227 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
5230 GstRTSPMessage message = { 0 };
5234 GTimeVal tv_timeout;
5236 /* get the next timeout interval */
5237 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
5239 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
5240 (gint) tv_timeout.tv_sec);
5242 gst_rtsp_message_unset (&message);
5244 /* we should continue reading the TCP socket because the server might
5245 * send us requests. When the session timeout expires, we need to send a
5246 * keep-alive request to keep the session open. */
5247 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
5248 &message, &tv_timeout);
5252 GST_DEBUG_OBJECT (src, "we received a server message");
5254 case GST_RTSP_EINTR:
5255 /* we got interrupted, see what we have to do */
5257 case GST_RTSP_ETIMEOUT:
5258 /* send keep-alive, ignore the result, a warning will be posted. */
5259 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
5260 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5264 /* server closed the connection. not very fatal for UDP, reconnect and
5265 * see what happens. */
5266 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5267 ("The server closed the connection."));
5268 if (src->udp_reconnect) {
5270 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
5277 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
5279 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5280 ("Unhandled return value %d.", res));
5284 switch (message.type) {
5285 case GST_RTSP_MESSAGE_REQUEST:
5286 /* server sends us a request message, handle it */
5288 gst_rtspsrc_handle_request (src, src->conninfo.connection,
5290 if (res == GST_RTSP_EEOF)
5293 goto handle_request_failed;
5295 case GST_RTSP_MESSAGE_RESPONSE:
5296 /* we ignore response and data messages */
5297 GST_DEBUG_OBJECT (src, "ignoring response message");
5299 gst_rtsp_message_dump (&message);
5300 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
5301 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
5302 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
5303 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
5304 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5311 case GST_RTSP_MESSAGE_DATA:
5312 /* we ignore response and data messages */
5313 GST_DEBUG_OBJECT (src, "ignoring data message");
5316 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5321 g_assert_not_reached ();
5323 /* we get here when the connection got interrupted */
5326 gst_rtsp_message_unset (&message);
5327 GST_DEBUG_OBJECT (src, "got interrupted");
5328 return GST_FLOW_FLUSHING;
5332 gchar *str = gst_rtsp_strresult (res);
5335 src->conninfo.connected = FALSE;
5336 if (res != GST_RTSP_EINTR) {
5337 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
5338 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_CONNECTION_FAIL,
5339 "Could not connect to server.");
5341 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
5342 ("Could not connect to server. (%s)", str));
5345 ret = GST_FLOW_ERROR;
5347 ret = GST_FLOW_FLUSHING;
5353 gchar *str = gst_rtsp_strresult (res);
5355 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
5356 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_SERVER_DISCONNECTED,
5357 "Could not receive message.");
5359 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5360 ("Could not receive message. (%s)", str));
5363 return GST_FLOW_ERROR;
5365 handle_request_failed:
5367 gchar *str = gst_rtsp_strresult (res);
5370 gst_rtsp_message_unset (&message);
5371 if (res != GST_RTSP_EINTR) {
5372 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
5373 gst_rtspsrc_post_error_message (src,
5374 GST_RTSPSRC_ERROR_SERVICE_UNAVAILABLE,
5375 "Could not handle server message.");
5377 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5378 ("Could not handle server message. (%s)", str));
5381 ret = GST_FLOW_ERROR;
5383 ret = GST_FLOW_FLUSHING;
5389 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5390 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5391 ("The server closed the connection."));
5392 src->conninfo.connected = FALSE;
5393 gst_rtsp_message_unset (&message);
5394 return GST_FLOW_EOS;
5398 static GstRTSPResult
5399 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
5401 GstRTSPResult res = GST_RTSP_OK;
5404 GST_DEBUG_OBJECT (src, "doing reconnect");
5406 GST_OBJECT_LOCK (src);
5407 /* only restart when the pads were not yet activated, else we were
5408 * streaming over UDP */
5409 restart = src->need_activate;
5410 GST_OBJECT_UNLOCK (src);
5412 /* no need to restart, we're done */
5416 /* we can try only TCP now */
5417 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
5419 /* close and cleanup our state */
5420 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
5423 /* see if we have TCP left to try. Also don't try TCP when we were configured
5425 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
5428 /* We post a warning message now to inform the user
5429 * that nothing happened. It's most likely a firewall thing. */
5430 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5431 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5432 "firewall is blocking it. Retrying using a TCP connection.",
5433 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
5435 /* open new connection using tcp */
5436 if (gst_rtspsrc_open (src, async) < 0)
5439 /* start playback */
5440 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
5449 src->cur_protocols = 0;
5450 /* no transport possible, post an error and stop */
5451 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
5452 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_BAD_TRANSPORT,
5453 "Could not receive any UDP packets for seconds, maybe your firewall is blocking it. No other protocols to try.");
5455 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5456 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5457 "firewall is blocking it. No other protocols to try.",
5458 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
5460 return GST_RTSP_ERROR;
5464 GST_DEBUG_OBJECT (src, "open failed");
5469 GST_DEBUG_OBJECT (src, "play failed");
5475 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
5479 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
5482 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
5485 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
5488 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
5496 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
5498 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
5501 GST_WARNING_OBJECT (src, "Got cmd %s", cmd_to_string (cmd));
5505 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
5506 GST_DEBUG_OBJECT (src,
5507 "rtsp_duration %" GST_TIME_FORMAT
5508 ", rtsp_audio_codec %s , rtsp_video_codec %s , rtsp_video_frame_size %s",
5509 GST_TIME_ARGS (src->segment.duration), src->audio_codec,
5510 src->video_codec, src->video_frame_size);
5513 s = gst_message_new_element (GST_OBJECT_CAST (src),
5514 gst_structure_new ("rtspsrc_properties",
5515 "rtsp_duration", G_TYPE_UINT64, src->segment.duration,
5516 "rtsp_audio_codec", G_TYPE_STRING, src->audio_codec,
5517 "rtsp_video_codec", G_TYPE_STRING, src->video_codec,
5518 "rtsp_video_frame_size", G_TYPE_STRING, src->video_frame_size,
5521 gst_element_post_message (GST_ELEMENT_CAST (src), s);
5523 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
5524 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
5525 /* rtspsrc PAUSE state should be here for parsing sdp before PAUSE state changed. */
5526 g_mutex_lock (&(src)->pause_lock);
5527 g_cond_signal (&(src)->open_end);
5528 g_mutex_unlock (&(src)->pause_lock);
5532 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
5535 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
5538 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
5546 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
5550 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
5553 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
5556 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
5559 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
5567 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
5571 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
5572 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
5573 /* Ending conditional wait for pause when open fails.*/
5574 g_mutex_lock (&(src)->pause_lock);
5575 g_cond_signal (&(src)->open_end);
5576 g_mutex_unlock (&(src)->pause_lock);
5577 GST_WARNING_OBJECT (src,
5578 "ending conditional wait for pause as open is failed.");
5582 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
5585 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
5588 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
5596 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
5598 if (ret == GST_RTSP_OK)
5599 gst_rtspsrc_loop_complete_cmd (src, cmd);
5600 else if (ret == GST_RTSP_EINTR)
5601 gst_rtspsrc_loop_cancel_cmd (src, cmd);
5603 gst_rtspsrc_loop_error_cmd (src, cmd);
5607 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
5610 gboolean flushed = FALSE;
5612 /* start new request */
5613 gst_rtspsrc_loop_start_cmd (src, cmd);
5615 GST_DEBUG_OBJECT (src, "sending cmd %s", cmd_to_string (cmd));
5617 GST_OBJECT_LOCK (src);
5618 old = src->pending_cmd;
5619 if (old == CMD_RECONNECT) {
5620 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
5621 cmd = CMD_RECONNECT;
5623 if (old != CMD_WAIT) {
5624 src->pending_cmd = CMD_WAIT;
5625 GST_OBJECT_UNLOCK (src);
5626 /* cancel previous request */
5627 GST_DEBUG_OBJECT (src, "cancel previous request %s", cmd_to_string (old));
5628 gst_rtspsrc_loop_cancel_cmd (src, old);
5629 GST_OBJECT_LOCK (src);
5631 src->pending_cmd = cmd;
5632 /* interrupt if allowed */
5633 if (src->busy_cmd & mask) {
5634 GST_DEBUG_OBJECT (src, "connection flush busy %s",
5635 cmd_to_string (src->busy_cmd));
5636 gst_rtspsrc_connection_flush (src, TRUE);
5639 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %s",
5640 cmd_to_string (src->busy_cmd));
5643 gst_task_start (src->task);
5644 GST_OBJECT_UNLOCK (src);
5650 gst_rtspsrc_loop (GstRTSPSrc * src)
5654 if (!src->conninfo.connection || !src->conninfo.connected)
5657 if (src->interleaved)
5658 ret = gst_rtspsrc_loop_interleaved (src);
5660 ret = gst_rtspsrc_loop_udp (src);
5662 if (ret != GST_FLOW_OK)
5670 GST_WARNING_OBJECT (src, "we are not connected");
5671 ret = GST_FLOW_FLUSHING;
5676 const gchar *reason = gst_flow_get_name (ret);
5678 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
5679 src->running = FALSE;
5680 if (ret == GST_FLOW_EOS) {
5681 /* perform EOS logic */
5682 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5683 gst_element_post_message (GST_ELEMENT_CAST (src),
5684 gst_message_new_segment_done (GST_OBJECT_CAST (src),
5685 src->segment.format, src->segment.position));
5686 gst_rtspsrc_push_event (src,
5687 gst_event_new_segment_done (src->segment.format,
5688 src->segment.position));
5690 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5692 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
5693 /* for fatal errors we post an error message, post the error before the
5694 * EOS so the app knows about the error first. */
5695 GST_ELEMENT_ERROR (src, STREAM, FAILED,
5696 ("Internal data flow error."),
5697 ("streaming task paused, reason %s (%d)", reason, ret));
5698 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5700 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
5705 #ifndef GST_DISABLE_GST_DEBUG
5706 static const gchar *
5707 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
5711 while (method != 0) {
5728 static const gchar *
5729 gst_rtspsrc_skip_lws (const gchar * s)
5731 while (g_ascii_isspace (*s))
5736 static const gchar *
5737 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
5739 while (s > start && g_ascii_isspace (*(s - 1)))
5744 static const gchar *
5745 gst_rtspsrc_skip_commas (const gchar * s)
5747 /* The grammar allows for multiple commas */
5748 while (g_ascii_isspace (*s) || *s == ',')
5753 static const gchar *
5754 gst_rtspsrc_skip_item (const gchar * s)
5756 gboolean quoted = FALSE;
5757 const gchar *start = s;
5759 /* A list item ends at the last non-whitespace character
5760 * before a comma which is not inside a quoted-string. Or at
5761 * the end of the string.
5767 if (*s == '\\' && *(s + 1))
5776 return gst_rtspsrc_unskip_lws (s, start);
5780 gst_rtsp_decode_quoted_string (gchar * quoted_string)
5784 src = quoted_string + 1;
5785 dst = quoted_string;
5786 while (*src && *src != '"') {
5787 if (*src == '\\' && *(src + 1))
5794 /* Extract the authentication tokens that the server provided for each method
5795 * into an array of structures and give those to the connection object.
5798 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
5799 const gchar * header, gboolean * stale)
5801 GSList *list = NULL, *iter;
5803 gchar *item, *eq, *name_end, *value;
5805 g_return_if_fail (stale != NULL);
5807 gst_rtsp_connection_clear_auth_params (conn);
5810 /* Parse a header whose content is described by RFC2616 as
5811 * "#something", where "something" does not itself contain commas,
5812 * except as part of quoted-strings, into a list of allocated strings.
5814 header = gst_rtspsrc_skip_commas (header);
5816 end = gst_rtspsrc_skip_item (header);
5817 list = g_slist_prepend (list, g_strndup (header, end - header));
5818 header = gst_rtspsrc_skip_commas (end);
5823 list = g_slist_reverse (list);
5824 for (iter = list; iter; iter = iter->next) {
5827 eq = strchr (item, '=');
5829 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
5830 if (name_end == item) {
5831 /* That's no good... */
5838 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
5840 gst_rtsp_decode_quoted_string (value);
5844 if (value && strcmp (item, "stale") == 0 && strcmp (value, "TRUE") == 0)
5846 gst_rtsp_connection_set_auth_param (conn, item, value);
5850 g_slist_free (list);
5853 /* Parse a WWW-Authenticate Response header and determine the
5854 * available authentication methods
5856 * This code should also cope with the fact that each WWW-Authenticate
5857 * header can contain multiple challenge methods + tokens
5859 * At the moment, for Basic auth, we just do a minimal check and don't
5860 * even parse out the realm */
5862 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
5863 GstRTSPConnection * conn, gboolean * stale)
5867 g_return_if_fail (hdr != NULL);
5868 g_return_if_fail (methods != NULL);
5869 g_return_if_fail (stale != NULL);
5871 /* Skip whitespace at the start of the string */
5872 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
5874 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
5875 *methods |= GST_RTSP_AUTH_BASIC;
5876 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
5877 *methods |= GST_RTSP_AUTH_DIGEST;
5878 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
5883 * gst_rtspsrc_setup_auth:
5884 * @src: the rtsp source
5886 * Configure a username and password and auth method on the
5887 * connection object based on a response we received from the
5890 * Currently, this requires that a username and password were supplied
5891 * in the uri. In the future, they may be requested on demand by sending
5892 * a message up the bus.
5894 * Returns: TRUE if authentication information could be set up correctly.
5897 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
5901 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
5902 GstRTSPAuthMethod method;
5903 GstRTSPResult auth_result;
5905 GstRTSPConnection *conn;
5907 gboolean stale = FALSE;
5909 conn = src->conninfo.connection;
5911 /* Identify the available auth methods and see if any are supported */
5912 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
5913 &hdr, 0) == GST_RTSP_OK) {
5914 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
5917 if (avail_methods == GST_RTSP_AUTH_NONE)
5918 goto no_auth_available;
5920 /* For digest auth, if the response indicates that the session
5921 * data are stale, we just update them in the connection object and
5922 * return TRUE to retry the request */
5924 src->tried_url_auth = FALSE;
5926 url = gst_rtsp_connection_get_url (conn);
5928 /* Do we have username and password available? */
5929 if (url != NULL && !src->tried_url_auth && url->user != NULL
5930 && url->passwd != NULL) {
5933 src->tried_url_auth = TRUE;
5934 GST_DEBUG_OBJECT (src,
5935 "Attempting authentication using credentials from the URL");
5937 user = src->user_id;
5938 pass = src->user_pw;
5939 GST_DEBUG_OBJECT (src,
5940 "Attempting authentication using credentials from the properties");
5943 /* FIXME: If the url didn't contain username and password or we tried them
5944 * already, request a username and passwd from the application via some kind
5945 * of credentials request message */
5947 /* If we don't have a username and passwd at this point, bail out. */
5948 if (user == NULL || pass == NULL)
5951 /* Try to configure for each available authentication method, strongest to
5953 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
5954 /* Check if this method is available on the server */
5955 if ((method & avail_methods) == 0)
5958 /* Pass the credentials to the connection to try on the next request */
5959 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
5960 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
5961 * ignore it and end up retrying later */
5962 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
5963 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
5964 gst_rtsp_auth_method_to_string (method));
5969 if (method == GST_RTSP_AUTH_NONE)
5970 goto no_auth_available;
5976 /* Output an error indicating that we couldn't connect because there were
5977 * no supported authentication protocols */
5978 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
5979 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_NOT_AUTHORIZED,
5980 "No supported authentication protocol was found");
5982 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5983 ("No supported authentication protocol was found"));
5989 /* We don't fire an error message, we just return FALSE and let the
5990 * normal NOT_AUTHORIZED error be propagated */
5995 static GstRTSPResult
5996 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5997 GstRTSPMessage * request, GstRTSPMessage * response,
5998 GstRTSPStatusCode * code)
6001 GstRTSPStatusCode thecode;
6002 gchar *content_base = NULL;
6006 if (!src->short_header)
6007 gst_rtsp_ext_list_before_send (src->extensions, request);
6009 GST_DEBUG_OBJECT (src, "sending message");
6012 gst_rtsp_message_dump (request);
6014 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
6018 gst_rtsp_connection_reset_timeout (conn);
6021 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
6026 gst_rtsp_message_dump (response);
6028 switch (response->type) {
6029 case GST_RTSP_MESSAGE_REQUEST:
6030 res = gst_rtspsrc_handle_request (src, conn, response);
6031 if (res == GST_RTSP_EEOF)
6034 goto handle_request_failed;
6036 case GST_RTSP_MESSAGE_RESPONSE:
6037 /* ok, a response is good */
6038 GST_DEBUG_OBJECT (src, "received response message");
6040 case GST_RTSP_MESSAGE_DATA:
6041 /* get next response */
6042 GST_DEBUG_OBJECT (src, "handle data response message");
6043 gst_rtspsrc_handle_data (src, response);
6046 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
6051 thecode = response->type_data.response.code;
6053 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
6055 /* if the caller wanted the result code, we store it. */
6059 /* If the request didn't succeed, bail out before doing any more */
6060 if (thecode != GST_RTSP_STS_OK)
6063 /* store new content base if any */
6064 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
6067 g_free (src->content_base);
6068 src->content_base = g_strdup (content_base);
6070 gst_rtsp_ext_list_after_send (src->extensions, request, response);
6077 gchar *str = gst_rtsp_strresult (res);
6079 if (res != GST_RTSP_EINTR) {
6080 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
6081 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_CONNECTION_FAIL,
6082 "Could not send message.");
6084 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6085 ("Could not send message. (%s)", str));
6088 GST_WARNING_OBJECT (src, "send interrupted");
6097 GST_WARNING_OBJECT (src, "server closed connection");
6098 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
6100 /* if reconnect succeeds, try again */
6102 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
6106 /* only try once after reconnect, then fallthrough and error out */
6109 gchar *str = gst_rtsp_strresult (res);
6111 if (res != GST_RTSP_EINTR) {
6112 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
6113 gst_rtspsrc_post_error_message (src,
6114 GST_RTSPSRC_ERROR_SERVER_DISCONNECTED,
6115 "Could not receive message.");
6117 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6118 ("Could not receive message. (%s)", str));
6121 GST_WARNING_OBJECT (src, "receive interrupted");
6129 handle_request_failed:
6131 /* ERROR was posted */
6132 gst_rtsp_message_unset (response);
6137 GST_DEBUG_OBJECT (src, "we got an eof from the server");
6138 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
6139 ("The server closed the connection."));
6140 gst_rtsp_message_unset (response);
6147 * @src: the rtsp source
6148 * @conn: the connection to send on
6149 * @request: must point to a valid request
6150 * @response: must point to an empty #GstRTSPMessage
6151 * @code: an optional code result
6153 * send @request and retrieve the response in @response. optionally @code can be
6154 * non-NULL in which case it will contain the status code of the response.
6156 * If This function returns #GST_RTSP_OK, @response will contain a valid response
6157 * message that should be cleaned with gst_rtsp_message_unset() after usage.
6159 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
6160 * @response message) if the response code was not 200 (OK).
6162 * If the attempt results in an authentication failure, then this will attempt
6163 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
6166 * Returns: #GST_RTSP_OK if the processing was successful.
6168 static GstRTSPResult
6169 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
6170 GstRTSPMessage * request, GstRTSPMessage * response,
6171 GstRTSPStatusCode * code)
6173 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
6174 GstRTSPResult res = GST_RTSP_ERROR;
6177 GstRTSPMethod method = GST_RTSP_INVALID;
6183 /* make sure we don't loop forever */
6187 /* save method so we can disable it when the server complains */
6188 method = request->type_data.request.method;
6191 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
6195 case GST_RTSP_STS_UNAUTHORIZED:
6196 case GST_RTSP_STS_NOT_FOUND:
6197 if (gst_rtspsrc_setup_auth (src, response)) {
6198 /* Try the request/response again after configuring the auth info
6206 } while (retry == TRUE);
6208 /* If the user requested the code, let them handle errors, otherwise
6209 * post an error below */
6212 else if (int_code != GST_RTSP_STS_OK)
6213 goto error_response;
6220 GST_DEBUG_OBJECT (src, "got error %d", res);
6225 res = GST_RTSP_ERROR;
6227 switch (response->type_data.response.code) {
6228 case GST_RTSP_STS_NOT_FOUND:
6229 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
6230 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_BAD_REQUEST,
6233 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
6234 response->type_data.response.reason));
6237 case GST_RTSP_STS_UNAUTHORIZED:
6238 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
6239 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_NOT_AUTHORIZED,
6240 "STS NOT AUTHORIZED");
6242 GST_ELEMENT_ERROR (src, RESOURCE, NOT_AUTHORIZED, (NULL), ("%s",
6243 response->type_data.response.reason));
6246 case GST_RTSP_STS_MOVED_PERMANENTLY:
6247 case GST_RTSP_STS_MOVE_TEMPORARILY:
6249 gchar *new_location;
6250 GstRTSPLowerTrans transports;
6252 GST_DEBUG_OBJECT (src, "got redirection");
6253 /* if we don't have a Location Header, we must error */
6254 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
6255 &new_location, 0) < 0)
6258 /* When we receive a redirect result, we go back to the INIT state after
6259 * parsing the new URI. The caller should do the needed steps to issue
6260 * a new setup when it detects this state change. */
6261 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
6263 /* save current transports */
6264 if (src->conninfo.url)
6265 transports = src->conninfo.url->transports;
6267 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
6269 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
6271 /* set old transports */
6272 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
6273 src->conninfo.url->transports = transports;
6275 src->need_redirect = TRUE;
6276 src->state = GST_RTSP_STATE_INIT;
6280 case GST_RTSP_STS_NOT_ACCEPTABLE:
6281 case GST_RTSP_STS_NOT_IMPLEMENTED:
6282 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
6283 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
6284 gst_rtsp_method_as_text (method));
6285 src->methods &= ~method;
6289 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
6290 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_UNEXPECTED_MSG,
6291 "Got error response from Server");
6293 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6294 ("Got error response: %d (%s).", response->type_data.response.code,
6295 response->type_data.response.reason));
6299 /* if we return ERROR we should unset the response ourselves */
6300 if (res == GST_RTSP_ERROR)
6301 gst_rtsp_message_unset (response);
6307 static GstRTSPResult
6308 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
6309 GstRTSPMessage * response, GstRTSPSrc * src)
6311 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
6316 /* parse the response and collect all the supported methods. We need this
6317 * information so that we don't try to send an unsupported request to the
6321 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
6323 GstRTSPHeaderField field;
6327 /* reset supported methods */
6330 /* Try Allow Header first */
6331 field = GST_RTSP_HDR_ALLOW;
6334 gst_rtsp_message_get_header (response, field, &respoptions, indx);
6335 if (indx == 0 && !respoptions) {
6336 /* if no Allow header was found then try the Public header... */
6337 field = GST_RTSP_HDR_PUBLIC;
6338 gst_rtsp_message_get_header (response, field, &respoptions, indx);
6343 src->methods |= gst_rtsp_options_from_text (respoptions);
6348 if (src->methods == 0) {
6349 /* neither Allow nor Public are required, assume the server supports
6350 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
6352 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
6353 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
6355 /* always assume PLAY, FIXME, extensions should be able to override
6357 src->methods |= GST_RTSP_PLAY;
6358 /* also assume it will support Range */
6359 src->seekable = TRUE;
6361 /* we need describe and setup */
6362 if (!(src->methods & GST_RTSP_DESCRIBE))
6364 if (!(src->methods & GST_RTSP_SETUP))
6372 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
6373 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_METHOD_NOT_ALLOWED,
6374 "Server does not support DESCRIBE.");
6376 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
6377 ("Server does not support DESCRIBE."));
6383 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
6384 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_METHOD_NOT_ALLOWED,
6385 "Server does not support SETUP.");
6387 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
6388 ("Server does not support SETUP."));
6394 /* masks to be kept in sync with the hardcoded protocol order of preference
6396 static const guint protocol_masks[] = {
6397 GST_RTSP_LOWER_TRANS_UDP,
6398 GST_RTSP_LOWER_TRANS_UDP_MCAST,
6399 GST_RTSP_LOWER_TRANS_TCP,
6403 static GstRTSPResult
6404 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
6405 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
6409 gboolean add_udp_str;
6414 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
6419 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
6421 /* extension listed transports, use those */
6422 if (*transports != NULL)
6425 /* it's the default */
6426 add_udp_str = FALSE;
6428 /* the default RTSP transports */
6429 result = g_string_new ("RTP");
6432 case GST_RTSP_PROFILE_AVP:
6433 g_string_append (result, "/AVP");
6435 case GST_RTSP_PROFILE_SAVP:
6436 g_string_append (result, "/SAVP");
6438 case GST_RTSP_PROFILE_AVPF:
6439 g_string_append (result, "/AVPF");
6441 case GST_RTSP_PROFILE_SAVPF:
6442 g_string_append (result, "/SAVPF");
6448 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
6449 GST_DEBUG_OBJECT (src, "adding UDP unicast");
6451 g_string_append (result, "/UDP");
6452 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
6453 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
6454 GST_DEBUG_OBJECT (src, "adding UDP multicast");
6455 /* we don't have to allocate any UDP ports yet, if the selected transport
6456 * turns out to be multicast we can create them and join the multicast
6457 * group indicated in the transport reply */
6459 g_string_append (result, "/UDP");
6460 g_string_append (result, ";multicast");
6461 if (src->next_port_num != 0) {
6462 if (src->client_port_range.max > 0 &&
6463 src->next_port_num >= src->client_port_range.max)
6466 g_string_append_printf (result, ";client_port=%d-%d",
6467 src->next_port_num, src->next_port_num + 1);
6469 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
6470 GST_DEBUG_OBJECT (src, "adding TCP");
6472 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
6474 *transports = g_string_free (result, FALSE);
6476 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
6483 GST_ERROR ("extension gave error %d", res);
6488 GST_ERROR ("no more ports available");
6489 return GST_RTSP_ERROR;
6493 static GstRTSPResult
6494 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
6495 gint orig_rtpport, gint orig_rtcpport)
6498 gint nr_udp, nr_int;
6500 gint rtpport = 0, rtcpport = 0;
6503 src = stream->parent;
6505 /* find number of placeholders first */
6506 if (strstr (*transports, "%%i2"))
6508 else if (strstr (*transports, "%%i1"))
6513 if (strstr (*transports, "%%u2"))
6515 else if (strstr (*transports, "%%u1"))
6520 if (nr_udp == 0 && nr_int == 0)
6524 if (!orig_rtpport || !orig_rtcpport) {
6525 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
6528 rtpport = orig_rtpport;
6529 rtcpport = orig_rtcpport;
6533 str = g_string_new ("");
6535 while ((next = strstr (p, "%%"))) {
6536 g_string_append_len (str, p, next - p);
6537 if (next[2] == 'u') {
6539 g_string_append_printf (str, "%d", rtpport);
6540 else if (next[3] == '2')
6541 g_string_append_printf (str, "%d", rtcpport);
6543 if (next[2] == 'i') {
6545 g_string_append_printf (str, "%d", src->free_channel);
6546 else if (next[3] == '2')
6547 g_string_append_printf (str, "%d", src->free_channel + 1);
6552 /* append final part */
6553 g_string_append (str, p);
6555 g_free (*transports);
6556 *transports = g_string_free (str, FALSE);
6564 GST_ERROR ("failed to allocate udp ports");
6565 return GST_RTSP_ERROR;
6570 enc_key_length_from_cipher_name (const gchar * cipher)
6572 if (g_strcmp0 (cipher, "aes-128-icm") == 0)
6573 return AES_128_KEY_LEN;
6574 else if (g_strcmp0 (cipher, "aes-256-icm") == 0)
6575 return AES_256_KEY_LEN;
6577 GST_ERROR ("encryption algorithm '%s' not supported", cipher);
6583 auth_key_length_from_auth_name (const gchar * auth)
6585 if (g_strcmp0 (auth, "hmac-sha1-32") == 0)
6586 return HMAC_32_KEY_LEN;
6587 else if (g_strcmp0 (auth, "hmac-sha1-80") == 0)
6588 return HMAC_80_KEY_LEN;
6590 GST_ERROR ("authentication algorithm '%s' not supported", auth);
6596 signal_get_srtcp_params (GstRTSPSrc * src, GstRTSPStream * stream)
6598 GstCaps *caps = NULL;
6600 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY], 0,
6604 GST_DEBUG_OBJECT (src, "SRTP parameters received");
6610 default_srtcp_params (void)
6617 guint data_size = GST_ROUND_UP_4 (KEY_SIZE);
6619 /* create a random key */
6620 key_data = g_malloc (data_size);
6621 for (i = 0; i < data_size; i += 4)
6622 GST_WRITE_UINT32_BE (key_data + i, g_random_int ());
6624 buf = gst_buffer_new_wrapped (key_data, KEY_SIZE);
6626 caps = gst_caps_new_simple ("application/x-srtp",
6627 "srtp-key", GST_TYPE_BUFFER, buf,
6628 "srtcp-cipher", G_TYPE_STRING, "aes-128-icm",
6629 "srtcp-auth", G_TYPE_STRING, "hmac-sha1-80", NULL);
6631 gst_buffer_unref (buf);
6637 gst_rtspsrc_stream_make_keymgmt (GstRTSPSrc * src, GstRTSPStream * stream)
6640 gchar *result, *base64;
6643 GstMIKEYMessage *msg;
6644 GstMIKEYPayload *payload, *pkd;
6650 const gchar *srtcpcipher, *srtcpauth;
6652 stream->srtcpparams = signal_get_srtcp_params (src, stream);
6653 if (stream->srtcpparams == NULL)
6654 stream->srtcpparams = default_srtcp_params ();
6656 s = gst_caps_get_structure (stream->srtcpparams, 0);
6658 srtcpcipher = gst_structure_get_string (s, "srtcp-cipher");
6659 srtcpauth = gst_structure_get_string (s, "srtcp-auth");
6660 val = gst_structure_get_value (s, "srtp-key");
6662 if (srtcpcipher == NULL || srtcpauth == NULL || val == NULL) {
6663 GST_ERROR_OBJECT (src, "could not find the right SRTP parameters in caps");
6667 srtpkey = gst_value_get_buffer (val);
6669 msg = gst_mikey_message_new ();
6670 /* unencrypted MIKEY message, we send this over TLS so this is allowed */
6671 gst_mikey_message_set_info (msg, GST_MIKEY_VERSION, GST_MIKEY_TYPE_PSK_INIT,
6672 FALSE, GST_MIKEY_PRF_MIKEY_1, g_random_int (), GST_MIKEY_MAP_TYPE_SRTP);
6673 /* add policy '0' for our SSRC */
6674 gst_mikey_message_add_cs_srtp (msg, 0, stream->send_ssrc, 0);
6675 /* timestamp is now */
6676 gst_mikey_message_add_t_now_ntp_utc (msg);
6677 /* add some random data */
6678 gst_mikey_message_add_rand_len (msg, 16);
6680 /* the policy '0' is SRTP */
6681 payload = gst_mikey_payload_new (GST_MIKEY_PT_SP);
6682 gst_mikey_payload_sp_set (payload, 0, GST_MIKEY_SEC_PROTO_SRTP);
6684 /* only AES-CM is supported */
6686 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_ALG, 1, &byte);
6687 /* encryption key length */
6688 byte = enc_key_length_from_cipher_name (srtcpcipher);
6689 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_KEY_LEN, 1,
6691 /* only HMAC-SHA1 */
6692 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_ALG, 1,
6694 /* authentication key length */
6695 byte = auth_key_length_from_auth_name (srtcpauth);
6696 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_KEY_LEN, 1,
6698 /* we enable encryption on RTP and RTCP */
6699 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_ENC, 1,
6701 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTCP_ENC, 1,
6703 /* we enable authentication on RTP and RTCP */
6704 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_AUTH, 1,
6706 gst_mikey_message_add_payload (msg, payload);
6708 /* make unencrypted KEMAC */
6709 payload = gst_mikey_payload_new (GST_MIKEY_PT_KEMAC);
6710 gst_mikey_payload_kemac_set (payload, GST_MIKEY_ENC_NULL, GST_MIKEY_MAC_NULL);
6711 /* add the key in KEMAC */
6712 pkd = gst_mikey_payload_new (GST_MIKEY_PT_KEY_DATA);
6713 gst_buffer_map (srtpkey, &info, GST_MAP_READ);
6714 gst_mikey_payload_key_data_set_key (pkd, GST_MIKEY_KD_TEK, info.size,
6716 gst_buffer_unmap (srtpkey, &info);
6717 gst_mikey_payload_kemac_add_sub (payload, pkd);
6718 gst_mikey_message_add_payload (msg, payload);
6720 /* now serialize this to bytes */
6721 bytes = gst_mikey_message_to_bytes (msg, NULL, NULL);
6722 gst_mikey_message_unref (msg);
6723 /* and make it into base64 */
6724 data = g_bytes_get_data (bytes, &size);
6725 base64 = g_base64_encode (data, size);
6726 g_bytes_unref (bytes);
6728 result = g_strdup_printf ("prot=mikey;uri=\"%s\";data=\"%s\"",
6729 stream->conninfo.location, base64);
6736 /* Perform the SETUP request for all the streams.
6738 * We ask the server for a specific transport, which initially includes all the
6739 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
6740 * two local UDP ports that we send to the server.
6742 * Once the server replied with a transport, we configure the other streams
6743 * with the same transport.
6745 * This function will also configure the stream for the selected transport,
6746 * which basically means creating the pipeline.
6748 static GstRTSPResult
6749 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
6752 GstRTSPResult res = GST_RTSP_ERROR;
6753 GstRTSPMessage request = { 0 };
6754 GstRTSPMessage response = { 0 };
6755 GstRTSPStream *stream = NULL;
6756 GstRTSPLowerTrans protocols;
6757 GstRTSPStatusCode code;
6758 gboolean unsupported_real = FALSE;
6759 gint rtpport, rtcpport;
6763 if (src->conninfo.connection) {
6764 url = gst_rtsp_connection_get_url (src->conninfo.connection);
6765 /* we initially allow all configured lower transports. based on the URL
6766 * transports and the replies from the server we narrow them down. */
6767 protocols = url->transports & src->cur_protocols;
6770 protocols = src->cur_protocols;
6776 /* reset some state */
6777 src->free_channel = 0;
6778 src->interleaved = FALSE;
6779 src->need_activate = FALSE;
6780 /* keep track of next port number, 0 is random */
6781 src->next_port_num = src->client_port_range.min;
6782 rtpport = rtcpport = 0;
6784 if (G_UNLIKELY (src->streams == NULL))
6787 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6788 GstRTSPConnection *conn;
6795 stream = (GstRTSPStream *) walk->data;
6797 caps = stream_get_caps_for_pt (stream, stream->default_pt);
6799 GST_DEBUG_OBJECT (src, "skipping stream %p, no caps", stream);
6803 if (stream->skipped) {
6804 GST_DEBUG_OBJECT (src, "skipping stream %p", stream);
6808 /* see if we need to configure this stream */
6809 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
6810 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
6815 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
6816 stream->id, caps, &selected);
6818 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
6822 /* merge/overwrite global caps */
6827 s = gst_caps_get_structure (caps, 0);
6829 num = gst_structure_n_fields (src->props);
6830 for (j = 0; j < num; j++) {
6834 name = gst_structure_nth_field_name (src->props, j);
6835 val = gst_structure_get_value (src->props, name);
6836 gst_structure_set_value (s, name, val);
6838 GST_DEBUG_OBJECT (src, "copied %s", name);
6842 /* skip setup if we have no URL for it */
6843 if (stream->conninfo.location == NULL) {
6844 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
6848 if (src->conninfo.connection == NULL) {
6849 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
6850 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
6853 conn = stream->conninfo.connection;
6855 conn = src->conninfo.connection;
6857 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
6858 stream->conninfo.location);
6860 /* if we have a multicast connection, only suggest multicast from now on */
6861 if (stream->is_multicast)
6862 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
6865 /* first selectable protocol */
6866 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6868 if (!protocol_masks[mask])
6872 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
6873 protocol_masks[mask]);
6874 /* create a string with first transport in line */
6876 res = gst_rtspsrc_create_transports_string (src,
6877 protocols & protocol_masks[mask], stream->profile, &transports);
6878 if (res < 0 || transports == NULL)
6879 goto setup_transport_failed;
6881 if (strlen (transports) == 0) {
6882 g_free (transports);
6883 GST_DEBUG_OBJECT (src, "no transports found");
6888 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
6890 /* replace placeholders with real values, this function will optionally
6891 * allocate UDP ports and other info needed to execute the setup request */
6892 res = gst_rtspsrc_prepare_transports (stream, &transports,
6893 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
6895 g_free (transports);
6896 goto setup_transport_failed;
6899 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
6901 /* create SETUP request */
6903 gst_rtspsrc_init_request (src, &request, GST_RTSP_SETUP,
6904 stream->conninfo.location);
6906 g_free (transports);
6907 goto create_request_failed;
6910 /* select transport */
6911 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
6914 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
6915 stream->profile == GST_RTSP_PROFILE_SAVPF) {
6916 hval = gst_rtspsrc_stream_make_keymgmt (src, stream);
6917 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
6920 /* if the user wants a non default RTP packet size we add the blocksize
6922 if (src->rtp_blocksize > 0) {
6923 hval = g_strdup_printf ("%d", src->rtp_blocksize);
6924 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
6928 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
6931 /* handle the code ourselves */
6932 res = gst_rtspsrc_send (src, conn, &request, &response, &code);
6937 case GST_RTSP_STS_OK:
6939 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
6940 gst_rtsp_message_unset (&request);
6941 gst_rtsp_message_unset (&response);
6942 /* cleanup of leftover transport */
6943 gst_rtspsrc_stream_free_udp (stream);
6944 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
6945 * we might be in this case */
6946 if (stream->container && rtpport && rtcpport && !retry) {
6947 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
6952 /* this transport did not go down well, but we may have others to try
6953 * that we did not send yet, try those and only give up then
6954 * but not without checking for lost cause/extension so we can
6955 * post a nicer/more useful error message later */
6956 if (!unsupported_real)
6957 unsupported_real = stream->is_real;
6958 /* select next available protocol, give up on this stream if none */
6960 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6962 if (!protocol_masks[mask] || unsupported_real)
6967 /* cleanup of leftover transport and move to the next stream */
6968 gst_rtspsrc_stream_free_udp (stream);
6969 goto response_error;
6972 /* parse response transport */
6974 gchar *resptrans = NULL;
6975 GstRTSPTransport transport = { 0 };
6977 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
6980 gst_rtspsrc_stream_free_udp (stream);
6984 /* parse transport, go to next stream on parse error */
6985 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
6986 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
6990 /* update allowed transports for other streams. once the transport of
6991 * one stream has been determined, we make sure that all other streams
6992 * are configured in the same way */
6993 switch (transport.lower_transport) {
6994 case GST_RTSP_LOWER_TRANS_TCP:
6995 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
6996 protocols = GST_RTSP_LOWER_TRANS_TCP;
6997 src->interleaved = TRUE;
6998 /* update free channels */
7000 MAX (transport.interleaved.min, src->free_channel);
7002 MAX (transport.interleaved.max, src->free_channel);
7003 src->free_channel++;
7005 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
7006 /* only allow multicast for other streams */
7007 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
7008 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
7009 /* if the server selected our ports, increment our counters so that
7010 * we select a new port later */
7011 if (src->next_port_num == transport.port.min &&
7012 src->next_port_num + 1 == transport.port.max) {
7013 src->next_port_num += 2;
7016 case GST_RTSP_LOWER_TRANS_UDP:
7017 /* only allow unicast for other streams */
7018 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
7019 protocols = GST_RTSP_LOWER_TRANS_UDP;
7022 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
7023 transport.lower_transport);
7027 if (!src->interleaved || !retry) {
7028 /* now configure the stream with the selected transport */
7029 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
7030 GST_DEBUG_OBJECT (src,
7031 "could not configure stream %p transport, skipping stream",
7034 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
7035 /* retain the first allocated UDP port pair */
7036 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
7037 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
7040 /* we need to activate at least one streams when we detect activity */
7041 src->need_activate = TRUE;
7043 /* stream is setup now */
7044 stream->setup = TRUE;
7049 GstRTSPStream *sskip;
7051 skip = g_list_next (skip);
7055 sskip = (GstRTSPStream *) skip->data;
7057 /* skip all streams with the same control url */
7058 if (g_str_equal (stream->conninfo.location, sskip->conninfo.location)) {
7059 GST_DEBUG_OBJECT (src, "found stream %p with same control %s",
7060 sskip, sskip->conninfo.location);
7061 sskip->skipped = TRUE;
7066 /* clean up our transport struct */
7067 gst_rtsp_transport_init (&transport);
7068 /* clean up used RTSP messages */
7069 gst_rtsp_message_unset (&request);
7070 gst_rtsp_message_unset (&response);
7074 /* store the transport protocol that was configured */
7075 src->cur_protocols = protocols;
7077 gst_rtsp_ext_list_stream_select (src->extensions, url);
7079 /* if there is nothing to activate, error out */
7080 if (!src->need_activate)
7081 goto nothing_to_activate;
7088 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
7089 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_INVALID_PROTOCOL,
7090 "Could not connect to server, no protocols left");
7092 /* no transport possible, post an error and stop */
7093 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
7094 ("Could not connect to server, no protocols left"));
7096 return GST_RTSP_ERROR;
7100 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
7101 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_CONTENT_NOT_FOUND,
7102 "SDP contains no streams");
7104 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7105 ("SDP contains no streams"));
7107 return GST_RTSP_ERROR;
7109 create_request_failed:
7111 gchar *str = gst_rtsp_strresult (res);
7113 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
7114 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_BAD_REQUEST,
7115 "Could not create request.");
7117 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7118 ("Could not create request. (%s)", str));
7123 setup_transport_failed:
7125 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
7126 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_BAD_REQUEST,
7127 "Could not setup transport.");
7129 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7130 ("Could not setup transport."));
7132 res = GST_RTSP_ERROR;
7137 #ifndef TIZEN_FEATURE_RTSP_MODIFICATION
7138 const gchar *str = gst_rtsp_status_as_text (code);
7141 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
7142 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_UNEXPECTED_MSG,
7143 "Error from Server .");
7145 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7146 ("Error (%d): %s", code, GST_STR_NULL (str)));
7148 res = GST_RTSP_ERROR;
7153 gchar *str = gst_rtsp_strresult (res);
7155 if (res != GST_RTSP_EINTR) {
7156 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
7157 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_CONNECTION_FAIL,
7158 "Could not send message.");
7160 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7161 ("Could not send message. (%s)", str));
7164 GST_WARNING_OBJECT (src, "send interrupted");
7171 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
7172 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_BAD_TRANSPORT,
7173 "Server did not select transport.");
7175 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7176 ("Server did not select transport."));
7178 res = GST_RTSP_ERROR;
7181 nothing_to_activate:
7183 /* none of the available error codes is really right .. */
7184 if (unsupported_real) {
7185 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
7186 gst_rtspsrc_post_error_message (src,
7187 GST_RTSPSRC_ERROR_UNSUPPORTED_MEDIA_TYPE,
7188 "No supported stream was found. You might need to install a GStreamer RTSP extension plugin for Real media streams.");
7190 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
7191 (_("No supported stream was found. You might need to install a "
7192 "GStreamer RTSP extension plugin for Real media streams.")),
7196 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
7197 gst_rtspsrc_post_error_message (src,
7198 GST_RTSPSRC_ERROR_UNSUPPORTED_MEDIA_TYPE,
7199 "No supported stream was found. You might need to allow more transport protocols or may otherwise be missing the right GStreamer RTSP extension plugin.");
7201 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
7202 (_("No supported stream was found. You might need to allow "
7203 "more transport protocols or may otherwise be missing "
7204 "the right GStreamer RTSP extension plugin.")), (NULL));
7207 return GST_RTSP_ERROR;
7211 gst_rtsp_message_unset (&request);
7212 gst_rtsp_message_unset (&response);
7218 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
7219 GstSegment * segment)
7222 GstRTSPTimeRange *therange;
7225 gst_rtsp_range_free (src->range);
7227 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
7228 GST_DEBUG_OBJECT (src, "parsed range %s", range);
7229 src->range = therange;
7231 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
7233 gst_segment_init (segment, GST_FORMAT_TIME);
7237 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
7238 therange->min.type, therange->min.seconds, therange->max.type,
7239 therange->max.seconds);
7241 if (therange->min.type == GST_RTSP_TIME_NOW)
7243 else if (therange->min.type == GST_RTSP_TIME_END)
7246 seconds = therange->min.seconds * GST_SECOND;
7248 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
7249 GST_TIME_ARGS (seconds));
7251 /* we need to start playback without clipping from the position reported by
7253 segment->start = seconds;
7254 #ifndef TIZEN_FEATURE_RTSP_MODIFICATION
7256 The range-min points to the start of the segment , not the current position.
7257 After getting the current position from MSL during normal pause/resume or during seek , we should not
7258 update the segment->position again with the rtp header npt timestamp.
7260 segment->position = seconds;
7263 if (therange->max.type == GST_RTSP_TIME_NOW)
7264 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
7269 else if (therange->max.type == GST_RTSP_TIME_END)
7272 seconds = therange->max.seconds * GST_SECOND;
7274 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
7275 GST_TIME_ARGS (seconds));
7277 /* live (WMS) server might send overflowed large max as its idea of infinity,
7278 * compensate to prevent problems later on */
7279 if (seconds != -1 && seconds < 0) {
7281 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
7284 /* live (WMS) might send min == max, which is not worth recording */
7285 if (segment->duration == -1 && seconds == segment->start)
7288 /* don't change duration with unknown value, we might have a valid value
7289 * there that we want to keep. */
7291 segment->duration = seconds;
7296 /* Parse clock profived by the server with following syntax:
7298 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
7301 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
7303 gboolean res = FALSE;
7305 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
7306 gchar **fields = NULL, **parts = NULL;
7307 gchar *remote_ip, *str;
7309 GstClockTime base_time;
7312 fields = g_strsplit (gstclock, " ", 0);
7314 /* wrapped clock, not very interesting for now */
7315 if (fields[1] == NULL)
7318 /* remote IP address and port */
7319 if ((str = fields[2]) == NULL)
7322 parts = g_strsplit (str, ":", 0);
7324 if ((remote_ip = parts[0]) == NULL)
7327 if ((str = parts[1]) == NULL)
7335 if ((str = fields[3]) == NULL)
7338 base_time = g_ascii_strtoull (str, NULL, 10);
7341 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
7344 if (src->provided_clock)
7345 gst_object_unref (src->provided_clock);
7346 src->provided_clock = netclock;
7348 gst_element_post_message (GST_ELEMENT_CAST (src),
7349 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
7350 src->provided_clock, TRUE));
7354 g_strfreev (fields);
7360 /* must be called with the RTSP state lock */
7361 static GstRTSPResult
7362 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
7368 /* prepare global stream caps properties */
7370 gst_structure_remove_all_fields (src->props);
7372 src->props = gst_structure_new_empty ("RTSPProperties");
7375 gst_sdp_message_dump (sdp);
7377 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
7379 /* let the app inspect and change the SDP */
7380 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
7382 gst_segment_init (&src->segment, GST_FORMAT_TIME);
7384 /* parse range for duration reporting. */
7389 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
7393 /* keep track of the range and configure it in the segment */
7394 if (gst_rtspsrc_parse_range (src, range, &src->segment))
7398 /* parse clock information. This is GStreamer specific, a server can tell the
7399 * client what clock it is using and wrap that in a network clock. The
7400 * advantage of that is that we can slave to it. */
7402 const gchar *gstclock;
7405 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
7406 if (gstclock == NULL)
7409 /* parse the clock and expose it in the provide_clock method */
7410 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
7414 /* try to find a global control attribute. Note that a '*' means that we should
7415 * do aggregate control with the current url (so we don't do anything and
7416 * leave the current connection as is) */
7418 const gchar *control;
7421 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
7422 if (control == NULL)
7425 /* only take fully qualified urls */
7426 if (g_str_has_prefix (control, "rtsp://"))
7430 g_free (src->conninfo.location);
7431 src->conninfo.location = g_strdup (control);
7432 /* make a connection for this, if there was a connection already, nothing
7434 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
7435 GST_ERROR_OBJECT (src, "could not connect");
7438 /* we need to keep the control url separate from the connection url because
7439 * the rules for constructing the media control url need it */
7440 g_free (src->control);
7441 src->control = g_strdup (control);
7444 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
7445 src->is_audio_codec_supported = FALSE;
7446 src->is_video_codec_supported = FALSE;
7449 /* create streams */
7450 n_streams = gst_sdp_message_medias_len (sdp);
7451 for (i = 0; i < n_streams; i++) {
7452 gst_rtspsrc_create_stream (src, sdp, i);
7455 src->state = GST_RTSP_STATE_INIT;
7456 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
7457 /* Check for the support for the Media codecs */
7458 if ((!src->is_audio_codec_supported) && (!src->is_video_codec_supported)) {
7459 GST_ERROR_OBJECT (src, "UnSupported Media Type !!!! \n");
7460 goto unsupported_file_type;
7462 GST_DEBUG_OBJECT (src, "Supported Media Type. \n");
7466 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
7469 /* reset our state */
7470 src->need_range = TRUE;
7473 src->state = GST_RTSP_STATE_READY;
7480 GST_ERROR_OBJECT (src, "setup failed");
7481 gst_rtspsrc_cleanup (src);
7484 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
7485 unsupported_file_type:
7487 gst_rtspsrc_post_error_message (src,
7488 GST_RTSPSRC_ERROR_UNSUPPORTED_MEDIA_TYPE,
7489 "No supported stream was found");
7490 res = GST_RTSP_ERROR;
7491 gst_rtspsrc_cleanup (src);
7497 static GstRTSPResult
7498 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
7502 GstRTSPMessage request = { 0 };
7503 GstRTSPMessage response = { 0 };
7506 gchar *respcont = NULL;
7509 src->need_redirect = FALSE;
7511 /* can't continue without a valid url */
7512 if (G_UNLIKELY (src->conninfo.url == NULL)) {
7513 res = GST_RTSP_EINVAL;
7516 src->tried_url_auth = FALSE;
7518 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
7519 goto connect_failed;
7521 /* create OPTIONS */
7522 GST_DEBUG_OBJECT (src, "create options...");
7524 gst_rtspsrc_init_request (src, &request, GST_RTSP_OPTIONS,
7525 src->conninfo.url_str);
7527 goto create_request_failed;
7530 GST_DEBUG_OBJECT (src, "send options...");
7533 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
7536 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
7541 if (!gst_rtspsrc_parse_methods (src, &response))
7544 /* create DESCRIBE */
7545 GST_DEBUG_OBJECT (src, "create describe...");
7547 gst_rtspsrc_init_request (src, &request, GST_RTSP_DESCRIBE,
7548 src->conninfo.url_str);
7550 goto create_request_failed;
7552 /* we only accept SDP for now */
7553 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
7557 GST_DEBUG_OBJECT (src, "send describe...");
7560 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
7563 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
7567 /* we only perform redirect for the describe, currently */
7568 if (src->need_redirect) {
7569 /* close connection, we don't have to send a TEARDOWN yet, ignore the
7571 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7573 gst_rtsp_message_unset (&request);
7574 gst_rtsp_message_unset (&response);
7580 /* it could be that the DESCRIBE method was not implemented */
7581 if (!(src->methods & GST_RTSP_DESCRIBE))
7584 /* check if reply is SDP */
7585 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
7587 /* could not be set but since the request returned OK, we assume it
7588 * was SDP, else check it. */
7590 if (g_ascii_strcasecmp (respcont, "application/sdp") != 0)
7591 goto wrong_content_type;
7594 /* get message body and parse as SDP */
7595 gst_rtsp_message_get_body (&response, &data, &size);
7596 if (data == NULL || size == 0)
7599 GST_DEBUG_OBJECT (src, "parse SDP...");
7600 gst_sdp_message_new (sdp);
7601 gst_sdp_message_parse_buffer (data, size, *sdp);
7603 /* clean up any messages */
7604 gst_rtsp_message_unset (&request);
7605 gst_rtsp_message_unset (&response);
7612 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
7613 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_INVALID_URL,
7614 "No valid RTSP URL was provided");
7616 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
7617 ("No valid RTSP URL was provided"));
7623 gchar *str = gst_rtsp_strresult (res);
7625 if (res != GST_RTSP_EINTR) {
7626 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
7627 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_CONNECTION_FAIL,
7628 "Failed to connect.");
7630 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
7631 ("Failed to connect. (%s)", str));
7634 GST_WARNING_OBJECT (src, "connect interrupted");
7639 create_request_failed:
7641 gchar *str = gst_rtsp_strresult (res);
7643 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
7644 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_BAD_REQUEST,
7645 "Could not create request.");
7647 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7648 ("Could not create request. (%s)", str));
7655 /* Don't post a message - the rtsp_send method will have
7656 * taken care of it because we passed NULL for the response code */
7661 /* error was posted */
7662 res = GST_RTSP_ERROR;
7667 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
7668 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_OPTION_NOT_SUPPORTED,
7669 "Server does not support SDP. ");
7671 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7672 ("Server does not support SDP, got %s.", respcont));
7674 res = GST_RTSP_ERROR;
7679 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
7680 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_METHOD_NOT_ALLOWED,
7681 "Server can not provide an SDP.");
7683 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7684 ("Server can not provide an SDP."));
7686 res = GST_RTSP_ERROR;
7691 if (src->conninfo.connection) {
7692 GST_DEBUG_OBJECT (src, "free connection");
7693 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7695 gst_rtsp_message_unset (&request);
7696 gst_rtsp_message_unset (&response);
7701 static GstRTSPResult
7702 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
7707 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
7709 if (src->sdp == NULL) {
7710 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
7714 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
7719 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
7726 GST_WARNING_OBJECT (src, "can't get sdp");
7727 src->open_error = TRUE;
7732 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
7733 src->open_error = TRUE;
7738 static GstRTSPResult
7739 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
7741 GstRTSPMessage request = { 0 };
7742 GstRTSPMessage response = { 0 };
7743 GstRTSPResult res = GST_RTSP_OK;
7745 const gchar *control;
7747 GST_DEBUG_OBJECT (src, "TEARDOWN...");
7749 gst_rtspsrc_set_state (src, GST_STATE_READY);
7751 if (src->state < GST_RTSP_STATE_READY) {
7752 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
7759 /* construct a control url */
7760 control = get_aggregate_control (src);
7762 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
7765 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7766 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7767 const gchar *setup_url;
7768 GstRTSPConnInfo *info;
7770 /* try aggregate control first but do non-aggregate control otherwise */
7772 setup_url = control;
7773 else if ((setup_url = stream->conninfo.location) == NULL)
7776 if (src->conninfo.connection) {
7777 info = &src->conninfo;
7778 } else if (stream->conninfo.connection) {
7779 info = &stream->conninfo;
7783 if (!info->connected)
7788 gst_rtspsrc_init_request (src, &request, GST_RTSP_TEARDOWN, setup_url);
7790 goto create_request_failed;
7793 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
7796 gst_rtspsrc_send (src, info->connection, &request, &response,
7800 /* FIXME, parse result? */
7801 gst_rtsp_message_unset (&request);
7802 gst_rtsp_message_unset (&response);
7805 /* early exit when we did aggregate control */
7811 /* close connections */
7812 GST_DEBUG_OBJECT (src, "closing connection...");
7813 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7814 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7815 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7816 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
7820 gst_rtspsrc_cleanup (src);
7822 src->state = GST_RTSP_STATE_INVALID;
7825 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
7830 create_request_failed:
7832 gchar *str = gst_rtsp_strresult (res);
7834 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
7835 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_BAD_REQUEST,
7836 "Could not create request.");
7838 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7839 ("Could not create request. (%s)", str));
7846 gchar *str = gst_rtsp_strresult (res);
7848 gst_rtsp_message_unset (&request);
7849 if (res != GST_RTSP_EINTR) {
7850 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
7851 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_CONNECTION_FAIL,
7852 "Could not send message.");
7854 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7855 ("Could not send message. (%s)", str));
7858 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
7865 GST_DEBUG_OBJECT (src,
7866 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
7871 /* RTP-Info is of the format:
7873 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
7875 * rtptime corresponds to the timestamp for the NPT time given in the header
7876 * seqbase corresponds to the next sequence number we received. This number
7877 * indicates the first seqnum after the seek and should be used to discard
7878 * packets that are from before the seek.
7881 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
7886 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
7888 infos = g_strsplit (rtpinfo, ",", 0);
7889 for (i = 0; infos[i]; i++) {
7891 GstRTSPStream *stream;
7895 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
7897 /* init values, types of seqbase and timebase are bigger than needed so we
7898 * can store -1 as uninitialized values */
7903 /* parse url, find stream for url.
7904 * parse seq and rtptime. The seq number should be configured in the rtp
7905 * depayloader or session manager to detect gaps. Same for the rtptime, it
7906 * should be used to create an initial time newsegment. */
7907 fields = g_strsplit (infos[i], ";", 0);
7908 for (j = 0; fields[j]; j++) {
7909 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
7910 /* remove leading whitespace */
7911 fields[j] = g_strchug (fields[j]);
7912 if (g_str_has_prefix (fields[j], "url=")) {
7913 /* get the url and the stream */
7915 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
7916 } else if (g_str_has_prefix (fields[j], "seq=")) {
7917 seqbase = atoi (fields[j] + 4);
7918 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
7919 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
7922 g_strfreev (fields);
7923 /* now we need to store the values for the caps of the stream */
7924 if (stream != NULL) {
7925 GST_DEBUG_OBJECT (src,
7926 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
7927 stream, seqbase, timebase);
7929 /* we have a stream, configure detected params */
7930 stream->seqbase = seqbase;
7931 stream->timebase = timebase;
7940 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
7945 interval = strtoul (rtcp, NULL, 10);
7946 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
7951 interval *= GST_MSECOND;
7953 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7954 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7956 /* already (optionally) retrieved this when configuring manager */
7957 if (stream->session) {
7958 GObject *rtpsession = stream->session;
7960 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
7962 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
7966 /* now it happens that (Xenon) server sending this may also provide bogus
7967 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
7968 * and just use RTP-Info to sync */
7970 GObjectClass *klass;
7972 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
7973 if (g_object_class_find_property (klass, "rtcp-sync")) {
7974 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
7975 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
7981 gst_rtspsrc_get_float (const gchar * dstr)
7983 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7985 /* canonicalise floating point string so we can handle float strings
7986 * in the form "24.930" or "24,930" irrespective of the current locale */
7987 g_strlcpy (s, dstr, sizeof (s));
7988 g_strdelimit (s, ",", '.');
7989 return g_ascii_strtod (s, NULL);
7993 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
7995 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7996 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
7997 if (src->start_position != 0 && segment->position == 0) {
7998 segment->position = src->start_position;
7999 src->start_position = 0;
8002 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
8003 g_strlcpy (val_str, "now", sizeof (val_str));
8005 if (segment->position == 0) {
8006 g_strlcpy (val_str, "0", sizeof (val_str));
8008 g_ascii_dtostr (val_str, sizeof (val_str),
8009 ((gdouble) segment->position) / GST_SECOND);
8012 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
8013 GST_DEBUG_OBJECT (src, "Range Header Added : npt=%s-", val_str);
8015 return g_strdup_printf ("npt=%s-", val_str);
8019 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
8023 stream->timebase = -1;
8024 stream->seqbase = -1;
8026 len = stream->ptmap->len;
8027 for (i = 0; i < len; i++) {
8028 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
8031 if (item->caps == NULL)
8034 item->caps = gst_caps_make_writable (item->caps);
8035 s = gst_caps_get_structure (item->caps, 0);
8036 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
8040 static GstRTSPResult
8041 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
8043 GstRTSPResult res = GST_RTSP_OK;
8045 if (src->state < GST_RTSP_STATE_READY) {
8046 res = GST_RTSP_ERROR;
8047 if (src->open_error) {
8048 GST_DEBUG_OBJECT (src, "the stream was in error");
8052 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
8054 if ((res = gst_rtspsrc_open (src, async)) < 0) {
8055 GST_DEBUG_OBJECT (src, "failed to open stream");
8064 static GstRTSPResult
8065 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
8067 GstRTSPMessage request = { 0 };
8068 GstRTSPMessage response = { 0 };
8069 GstRTSPResult res = GST_RTSP_OK;
8073 const gchar *control;
8075 GST_DEBUG_OBJECT (src, "PLAY...");
8077 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
8080 if (!(src->methods & GST_RTSP_PLAY))
8083 if (src->state == GST_RTSP_STATE_PLAYING)
8086 if (!src->conninfo.connection || !src->conninfo.connected)
8089 /* send some dummy packets before we activate the receive in the
8091 gst_rtspsrc_send_dummy_packets (src);
8093 /* require new SR packets */
8095 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
8097 /* construct a control url */
8098 control = get_aggregate_control (src);
8100 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8101 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8102 const gchar *setup_url;
8103 GstRTSPConnection *conn;
8105 /* try aggregate control first but do non-aggregate control otherwise */
8107 setup_url = control;
8108 else if ((setup_url = stream->conninfo.location) == NULL)
8111 if (src->conninfo.connection) {
8112 conn = src->conninfo.connection;
8113 } else if (stream->conninfo.connection) {
8114 conn = stream->conninfo.connection;
8120 res = gst_rtspsrc_init_request (src, &request, GST_RTSP_PLAY, setup_url);
8122 goto create_request_failed;
8124 if (src->need_range) {
8125 #ifndef TIZEN_FEATURE_RTSP_MODIFICATION
8126 hval = gen_range_header (src, segment);
8128 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
8131 /* store the newsegment event so it can be sent from the streaming thread. */
8132 src->need_segment = TRUE;
8134 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
8137 Updating position with the MSL current position as gst_rtspsrc_get_position() does not return correct position.
8139 GST_DEBUG_OBJECT (src,
8140 " During normal pause-resume , segment->position=%" GST_TIME_FORMAT
8141 ",src->start_position=%" GST_TIME_FORMAT,
8142 GST_TIME_ARGS (segment->position),
8143 GST_TIME_ARGS (src->start_position));
8144 segment->position = src->last_pos;
8148 Sending the npt range request for each play request for updating the segment position properly.
8150 hval = gen_range_header (src, segment);
8151 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
8154 if (segment->rate != 1.0) {
8155 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
8157 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
8159 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
8161 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
8165 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
8167 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
8170 /* seek may have silently failed as it is not supported */
8171 if (!(src->methods & GST_RTSP_PLAY)) {
8172 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
8173 /* obviously it is supported as we made it here */
8174 src->methods |= GST_RTSP_PLAY;
8175 src->seekable = FALSE;
8176 /* but there is nothing to parse in the response,
8177 * so convey we have no idea and not to expect anything particular */
8178 clear_rtp_base (src, stream);
8182 /* need to do for all streams */
8183 for (run = src->streams; run; run = g_list_next (run))
8184 clear_rtp_base (src, (GstRTSPStream *) run->data);
8186 /* NOTE the above also disables npt based eos detection */
8187 /* and below forces position to 0,
8188 * which is visible feedback we lost the plot */
8189 segment->start = segment->position = src->last_pos;
8192 gst_rtsp_message_unset (&request);
8194 /* parse RTP npt field. This is the current position in the stream (Normal
8195 * Play Time) and should be put in the NEWSEGMENT position field. */
8196 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
8198 gst_rtspsrc_parse_range (src, hval, segment);
8200 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
8201 segment->rate = 1.0;
8203 /* parse Speed header. This is the intended playback rate of the stream
8204 * and should be put in the NEWSEGMENT rate field. */
8205 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
8206 0) == GST_RTSP_OK) {
8207 segment->rate = gst_rtspsrc_get_float (hval);
8208 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
8209 &hval, 0) == GST_RTSP_OK) {
8210 segment->rate = gst_rtspsrc_get_float (hval);
8213 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
8214 * for the RTP packets. If this is not present, we assume all starts from 0...
8215 * This is info for the RTP session manager that we pass to it in caps. */
8217 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
8218 &hval, hval_idx++) == GST_RTSP_OK)
8219 gst_rtspsrc_parse_rtpinfo (src, hval);
8221 /* some servers indicate RTCP parameters in PLAY response,
8222 * rather than properly in SDP */
8223 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
8224 &hval, 0) == GST_RTSP_OK)
8225 gst_rtspsrc_handle_rtcp_interval (src, hval);
8227 gst_rtsp_message_unset (&response);
8229 /* early exit when we did aggregate control */
8233 /* configure the caps of the streams after we parsed all headers. Only reset
8234 * the manager object when we set a new Range header (we did a seek) */
8235 gst_rtspsrc_configure_caps (src, segment, src->need_range);
8237 /* set to PLAYING after we have configured the caps, otherwise we
8238 * might end up calling request_key (with SRTP) while caps are still
8239 * being configured. */
8240 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
8242 /* set again when needed */
8243 src->need_range = FALSE;
8245 src->running = TRUE;
8246 src->base_time = -1;
8247 src->state = GST_RTSP_STATE_PLAYING;
8250 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
8251 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8252 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8253 stream->discont = TRUE;
8258 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
8265 GST_DEBUG_OBJECT (src, "failed to open stream");
8270 GST_DEBUG_OBJECT (src, "PLAY is not supported");
8275 GST_DEBUG_OBJECT (src, "we were already PLAYING");
8278 create_request_failed:
8280 gchar *str = gst_rtsp_strresult (res);
8282 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
8283 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_BAD_REQUEST,
8284 "Could not create request. ");
8286 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
8287 ("Could not create request. (%s)", str));
8294 gchar *str = gst_rtsp_strresult (res);
8296 gst_rtsp_message_unset (&request);
8297 if (res != GST_RTSP_EINTR) {
8298 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
8299 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_CONNECTION_FAIL,
8300 "Could not send message.");
8302 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
8303 ("Could not send message. (%s)", str));
8306 GST_WARNING_OBJECT (src, "PLAY interrupted");
8313 static GstRTSPResult
8314 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
8316 GstRTSPResult res = GST_RTSP_OK;
8317 GstRTSPMessage request = { 0 };
8318 GstRTSPMessage response = { 0 };
8320 const gchar *control;
8322 GST_DEBUG_OBJECT (src, "PAUSE...");
8324 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
8327 if (!(src->methods & GST_RTSP_PAUSE))
8330 if (src->state == GST_RTSP_STATE_READY)
8333 if (!src->conninfo.connection || !src->conninfo.connected)
8336 /* construct a control url */
8337 control = get_aggregate_control (src);
8339 /* loop over the streams. We might exit the loop early when we could do an
8340 * aggregate control */
8341 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8342 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8343 GstRTSPConnection *conn;
8344 const gchar *setup_url;
8346 /* try aggregate control first but do non-aggregate control otherwise */
8348 setup_url = control;
8349 else if ((setup_url = stream->conninfo.location) == NULL)
8352 if (src->conninfo.connection) {
8353 conn = src->conninfo.connection;
8354 } else if (stream->conninfo.connection) {
8355 conn = stream->conninfo.connection;
8361 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
8362 ("Sending PAUSE request"));
8365 gst_rtspsrc_init_request (src, &request, GST_RTSP_PAUSE,
8367 goto create_request_failed;
8369 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
8372 gst_rtsp_message_unset (&request);
8373 gst_rtsp_message_unset (&response);
8375 /* exit early when we did agregate control */
8380 /* change element states now */
8381 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
8384 src->state = GST_RTSP_STATE_READY;
8388 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
8395 GST_DEBUG_OBJECT (src, "failed to open stream");
8400 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
8405 GST_DEBUG_OBJECT (src, "we were already PAUSED");
8408 create_request_failed:
8410 gchar *str = gst_rtsp_strresult (res);
8412 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
8413 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_BAD_REQUEST,
8414 "Could not create request.");
8416 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
8417 ("Could not create request. (%s)", str));
8424 gchar *str = gst_rtsp_strresult (res);
8426 gst_rtsp_message_unset (&request);
8427 if (res != GST_RTSP_EINTR) {
8428 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
8429 gst_rtspsrc_post_error_message (src, GST_RTSPSRC_ERROR_CONNECTION_FAIL,
8430 "Could not send message. ");
8432 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
8433 ("Could not send message. (%s)", str));
8436 GST_WARNING_OBJECT (src, "PAUSE interrupted");
8444 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
8446 GstRTSPSrc *rtspsrc;
8448 rtspsrc = GST_RTSPSRC (bin);
8450 switch (GST_MESSAGE_TYPE (message)) {
8451 case GST_MESSAGE_EOS:
8452 gst_message_unref (message);
8454 case GST_MESSAGE_ELEMENT:
8456 const GstStructure *s = gst_message_get_structure (message);
8458 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
8459 gboolean ignore_timeout;
8461 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
8463 GST_OBJECT_LOCK (rtspsrc);
8464 ignore_timeout = rtspsrc->ignore_timeout;
8465 rtspsrc->ignore_timeout = TRUE;
8466 GST_OBJECT_UNLOCK (rtspsrc);
8468 /* we only act on the first udp timeout message, others are irrelevant
8469 * and can be ignored. */
8470 if (!ignore_timeout)
8471 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
8473 gst_message_unref (message);
8476 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8479 case GST_MESSAGE_ERROR:
8482 GstRTSPStream *stream;
8485 udpsrc = GST_MESSAGE_SRC (message);
8487 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
8488 GST_ELEMENT_NAME (udpsrc));
8490 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
8494 /* we ignore the RTCP udpsrc */
8495 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
8498 /* if we get error messages from the udp sources, that's not a problem as
8499 * long as not all of them error out. We also don't really know what the
8500 * problem is, the message does not give enough detail... */
8501 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
8502 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
8503 if (ret != GST_FLOW_OK)
8507 gst_message_unref (message);
8511 /* fatal but not our message, forward */
8512 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8517 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8523 /* the thread where everything happens */
8525 gst_rtspsrc_thread (GstRTSPSrc * src)
8529 GST_OBJECT_LOCK (src);
8530 cmd = src->pending_cmd;
8531 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
8532 || cmd == CMD_LOOP || cmd == CMD_OPEN)
8533 src->pending_cmd = CMD_LOOP;
8535 src->pending_cmd = CMD_WAIT;
8536 GST_DEBUG_OBJECT (src, "got command %s", cmd_to_string (cmd));
8538 /* we got the message command, so ensure communication is possible again */
8539 gst_rtspsrc_connection_flush (src, FALSE);
8541 src->busy_cmd = cmd;
8542 GST_OBJECT_UNLOCK (src);
8546 gst_rtspsrc_open (src, TRUE);
8549 gst_rtspsrc_play (src, &src->segment, TRUE);
8552 gst_rtspsrc_pause (src, TRUE);
8555 gst_rtspsrc_close (src, TRUE, FALSE);
8558 gst_rtspsrc_loop (src);
8561 gst_rtspsrc_reconnect (src, FALSE);
8567 GST_OBJECT_LOCK (src);
8568 /* and go back to sleep */
8569 if (src->pending_cmd == CMD_WAIT) {
8571 gst_task_pause (src->task);
8574 src->busy_cmd = CMD_WAIT;
8575 GST_OBJECT_UNLOCK (src);
8579 gst_rtspsrc_start (GstRTSPSrc * src)
8581 GST_DEBUG_OBJECT (src, "starting");
8583 GST_OBJECT_LOCK (src);
8585 src->pending_cmd = CMD_WAIT;
8587 if (src->task == NULL) {
8588 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
8589 if (src->task == NULL)
8592 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
8594 GST_OBJECT_UNLOCK (src);
8601 GST_OBJECT_UNLOCK (src);
8602 GST_ERROR_OBJECT (src, "failed to create task");
8608 gst_rtspsrc_stop (GstRTSPSrc * src)
8612 GST_DEBUG_OBJECT (src, "stopping");
8614 /* also cancels pending task */
8615 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
8617 GST_OBJECT_LOCK (src);
8618 if ((task = src->task)) {
8620 GST_OBJECT_UNLOCK (src);
8622 gst_task_stop (task);
8624 /* make sure it is not running */
8625 GST_RTSP_STREAM_LOCK (src);
8626 GST_RTSP_STREAM_UNLOCK (src);
8628 /* now wait for the task to finish */
8629 gst_task_join (task);
8631 /* and free the task */
8632 gst_object_unref (GST_OBJECT (task));
8634 GST_OBJECT_LOCK (src);
8636 GST_OBJECT_UNLOCK (src);
8638 /* ensure synchronously all is closed and clean */
8639 gst_rtspsrc_close (src, FALSE, TRUE);
8644 static GstStateChangeReturn
8645 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
8647 GstRTSPSrc *rtspsrc;
8648 GstStateChangeReturn ret;
8649 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
8653 rtspsrc = GST_RTSPSRC (element);
8654 GST_WARNING_OBJECT (rtspsrc, "State change transition: %d \n", transition);
8656 switch (transition) {
8657 case GST_STATE_CHANGE_NULL_TO_READY:
8658 if (!gst_rtspsrc_start (rtspsrc))
8661 case GST_STATE_CHANGE_READY_TO_PAUSED:
8662 /* init some state */
8663 rtspsrc->cur_protocols = rtspsrc->protocols;
8664 /* first attempt, don't ignore timeouts */
8665 rtspsrc->ignore_timeout = FALSE;
8666 rtspsrc->open_error = FALSE;
8667 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
8669 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
8670 set_manager_buffer_mode (rtspsrc);
8672 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
8673 /* unblock the tcp tasks and make the loop waiting */
8674 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
8675 /* make sure it is waiting before we send PAUSE or PLAY below */
8676 GST_RTSP_STREAM_LOCK (rtspsrc);
8677 GST_RTSP_STREAM_UNLOCK (rtspsrc);
8680 case GST_STATE_CHANGE_PAUSED_TO_READY:
8686 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
8687 if (ret == GST_STATE_CHANGE_FAILURE)
8690 switch (transition) {
8691 case GST_STATE_CHANGE_NULL_TO_READY:
8692 ret = GST_STATE_CHANGE_SUCCESS;
8694 case GST_STATE_CHANGE_READY_TO_PAUSED:
8695 #ifdef TIZEN_FEATURE_RTSP_MODIFICATION
8696 /* don't change to PAUSE state before complete stream opend.
8697 see gst_rtspsrc_loop_complete_cmd() */
8698 g_mutex_lock (&(rtspsrc)->pause_lock);
8699 end_time = g_get_monotonic_time () + 10 * G_TIME_SPAN_SECOND;
8700 if (!g_cond_wait_until (&(rtspsrc)->open_end, &(rtspsrc)->pause_lock,
8702 GST_WARNING_OBJECT (rtspsrc,
8703 "time out: stream opend is not completed yet..");
8705 g_mutex_unlock (&(rtspsrc)->pause_lock);
8707 ret = GST_STATE_CHANGE_NO_PREROLL;
8709 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
8710 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
8711 ret = GST_STATE_CHANGE_SUCCESS;
8713 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
8714 /* send pause request and keep the idle task around */
8715 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
8716 ret = GST_STATE_CHANGE_NO_PREROLL;
8718 case GST_STATE_CHANGE_PAUSED_TO_READY:
8719 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_PAUSE);
8720 ret = GST_STATE_CHANGE_SUCCESS;
8722 case GST_STATE_CHANGE_READY_TO_NULL:
8723 gst_rtspsrc_stop (rtspsrc);
8724 ret = GST_STATE_CHANGE_SUCCESS;
8735 GST_DEBUG_OBJECT (rtspsrc, "start failed");
8736 return GST_STATE_CHANGE_FAILURE;
8741 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
8744 GstRTSPSrc *rtspsrc;
8746 rtspsrc = GST_RTSPSRC (element);
8748 if (GST_EVENT_IS_DOWNSTREAM (event)) {
8749 res = gst_rtspsrc_push_event (rtspsrc, event);
8751 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
8758 /*** GSTURIHANDLER INTERFACE *************************************************/
8761 gst_rtspsrc_uri_get_type (GType type)
8766 static const gchar *const *
8767 gst_rtspsrc_uri_get_protocols (GType type)
8769 static const gchar *protocols[] =
8770 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
8771 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
8778 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
8780 GstRTSPSrc *src = GST_RTSPSRC (handler);
8782 /* FIXME: make thread-safe */
8783 return g_strdup (src->conninfo.location);
8787 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
8793 GstRTSPUrl *newurl = NULL;
8794 GstSDPMessage *sdp = NULL;
8796 src = GST_RTSPSRC (handler);
8798 /* same URI, we're fine */
8799 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
8802 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
8803 sres = gst_sdp_message_new (&sdp);
8807 GST_DEBUG_OBJECT (src, "parsing SDP message");
8808 sres = gst_sdp_message_parse_uri (uri, sdp);
8813 GST_DEBUG_OBJECT (src, "parsing URI");
8814 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
8818 /* if worked, free previous and store new url object along with the original
8820 GST_DEBUG_OBJECT (src, "configuring URI");
8821 g_free (src->conninfo.location);
8822 src->conninfo.location = g_strdup (uri);
8823 gst_rtsp_url_free (src->conninfo.url);
8824 src->conninfo.url = newurl;
8825 g_free (src->conninfo.url_str);
8827 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
8829 src->conninfo.url_str = NULL;
8832 gst_sdp_message_free (src->sdp);
8834 src->from_sdp = sdp != NULL;
8836 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
8837 GST_DEBUG_OBJECT (src, "request uri is: %s",
8838 GST_STR_NULL (src->conninfo.url_str));
8845 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
8850 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", sres);
8851 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8852 "Could not create SDP");
8857 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", sres,
8858 GST_STR_NULL (uri));
8859 gst_sdp_message_free (sdp);
8860 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8866 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
8867 GST_STR_NULL (uri), res);
8868 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8869 "Invalid RTSP URI");
8875 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
8877 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
8879 iface->get_type = gst_rtspsrc_uri_get_type;
8880 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
8881 iface->get_uri = gst_rtspsrc_uri_get_uri;
8882 iface->set_uri = gst_rtspsrc_uri_set_uri;