2 * Copyright (C) 2007-2008 Wouter Cloetens <wouter@mind.be>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more
16 * SECTION:element-souphttpsrc
18 * This plugin reads data from a remote location specified by a URI.
19 * Supported protocols are 'http', 'https'.
21 * An HTTP proxy must be specified by its URL.
22 * If the "http_proxy" environment variable is set, its value is used.
23 * If built with libsoup's GNOME integration features, the GNOME proxy
24 * configuration will be used, or failing that, proxy autodetection.
25 * The #GstSoupHTTPSrc:proxy property can be used to override the default.
27 * In case the #GstSoupHTTPSrc:iradio-mode property is set and the location is
28 * an HTTP resource, souphttpsrc will send special Icecast HTTP headers to the
29 * server to request additional Icecast meta-information.
30 * If the server is not an Icecast server, it will behave as if the
31 * #GstSoupHTTPSrc:iradio-mode property were not set. If it is, souphttpsrc will
32 * output data with a media type of application/x-icy, in which case you will
33 * need to use the #ICYDemux element as follow-up element to extract the Icecast
34 * metadata and to determine the underlying media type.
37 * <title>Example launch line</title>
39 * gst-launch-1.0 -v souphttpsrc location=https://some.server.org/index.html
40 * ! filesink location=/home/joe/server.html
41 * ]| The above pipeline reads a web page from a server using the HTTPS protocol
42 * and writes it to a local file.
44 * gst-launch-1.0 -v souphttpsrc user-agent="FooPlayer 0.99 beta"
45 * automatic-redirect=false proxy=http://proxy.intranet.local:8080
46 * location=http://music.foobar.com/demo.mp3 ! mad ! audioconvert
47 * ! audioresample ! alsasink
48 * ]| The above pipeline will read and decode and play an mp3 file from a
49 * web server using the HTTP protocol. If the server sends redirects,
50 * the request fails instead of following the redirect. The specified
51 * HTTP proxy server is used. The User-Agent HTTP request header
52 * is set to a custom string instead of "GStreamer souphttpsrc."
54 * gst-launch-1.0 -v souphttpsrc location=http://10.11.12.13/mjpeg
55 * do-timestamp=true ! multipartdemux
56 * ! image/jpeg,width=640,height=480 ! matroskamux
57 * ! filesink location=mjpeg.mkv
58 * ]| The above pipeline reads a motion JPEG stream from an IP camera
59 * using the HTTP protocol, encoded as mime/multipart image/jpeg
60 * parts, and writes a Matroska motion JPEG file. The width and
61 * height properties are set in the caps to provide the Matroska
62 * multiplexer with the information to set this in the header.
63 * Timestamps are set on the buffers as they arrive from the camera.
64 * These are used by the mime/multipart demultiplexer to emit timestamps
65 * on the JPEG-encoded video frame buffers. This allows the Matroska
66 * multiplexer to timestamp the frames in the resulting file.
76 #include <stdlib.h> /* atoi() */
78 #include <gst/gstelement.h>
79 #include <gst/gst-i18n-plugin.h>
80 #include <libsoup/soup.h>
81 #include "gstsouphttpsrc.h"
82 #include "gstsouputils.h"
84 #include <gst/tag/tag.h>
86 GST_DEBUG_CATEGORY_STATIC (souphttpsrc_debug);
87 #define GST_CAT_DEFAULT souphttpsrc_debug
89 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
100 PROP_AUTOMATIC_REDIRECT,
115 PROP_SSL_USE_SYSTEM_CA_FILE,
119 PROP_TLS_INTERACTION,
122 #define DEFAULT_USER_AGENT "GStreamer souphttpsrc " PACKAGE_VERSION " "
123 #define DEFAULT_IRADIO_MODE TRUE
124 #define DEFAULT_SOUP_LOG_LEVEL SOUP_LOGGER_LOG_HEADERS
125 #define DEFAULT_COMPRESS FALSE
126 #define DEFAULT_KEEP_ALIVE FALSE
127 #define DEFAULT_SSL_STRICT TRUE
128 #define DEFAULT_SSL_CA_FILE NULL
129 #define DEFAULT_SSL_USE_SYSTEM_CA_FILE TRUE
130 #define DEFAULT_TLS_DATABASE NULL
131 #define DEFAULT_TLS_INTERACTION NULL
132 #define DEFAULT_TIMEOUT 15
133 #define DEFAULT_RETRIES 3
134 #define DEFAULT_SOUP_METHOD NULL
136 #define GROW_BLOCKSIZE_LIMIT 1
137 #define GROW_BLOCKSIZE_COUNT 1
138 #define GROW_BLOCKSIZE_FACTOR 2
139 #define REDUCE_BLOCKSIZE_LIMIT 0.20
140 #define REDUCE_BLOCKSIZE_COUNT 2
141 #define REDUCE_BLOCKSIZE_FACTOR 0.5
143 static void gst_soup_http_src_uri_handler_init (gpointer g_iface,
144 gpointer iface_data);
145 static void gst_soup_http_src_finalize (GObject * gobject);
146 static void gst_soup_http_src_dispose (GObject * gobject);
148 static void gst_soup_http_src_set_property (GObject * object, guint prop_id,
149 const GValue * value, GParamSpec * pspec);
150 static void gst_soup_http_src_get_property (GObject * object, guint prop_id,
151 GValue * value, GParamSpec * pspec);
153 static GstStateChangeReturn gst_soup_http_src_change_state (GstElement *
154 element, GstStateChange transition);
155 static GstFlowReturn gst_soup_http_src_create (GstPushSrc * psrc,
156 GstBuffer ** outbuf);
157 static gboolean gst_soup_http_src_start (GstBaseSrc * bsrc);
158 static gboolean gst_soup_http_src_stop (GstBaseSrc * bsrc);
159 static gboolean gst_soup_http_src_get_size (GstBaseSrc * bsrc, guint64 * size);
160 static gboolean gst_soup_http_src_is_seekable (GstBaseSrc * bsrc);
161 static gboolean gst_soup_http_src_do_seek (GstBaseSrc * bsrc,
162 GstSegment * segment);
163 static gboolean gst_soup_http_src_query (GstBaseSrc * bsrc, GstQuery * query);
164 static gboolean gst_soup_http_src_unlock (GstBaseSrc * bsrc);
165 static gboolean gst_soup_http_src_unlock_stop (GstBaseSrc * bsrc);
166 static gboolean gst_soup_http_src_set_location (GstSoupHTTPSrc * src,
167 const gchar * uri, GError ** error);
168 static gboolean gst_soup_http_src_set_proxy (GstSoupHTTPSrc * src,
170 static char *gst_soup_http_src_unicodify (const char *str);
171 static gboolean gst_soup_http_src_build_message (GstSoupHTTPSrc * src,
172 const gchar * method);
173 static void gst_soup_http_src_cancel_message (GstSoupHTTPSrc * src);
174 static gboolean gst_soup_http_src_add_range_header (GstSoupHTTPSrc * src,
175 guint64 offset, guint64 stop_offset);
176 static gboolean gst_soup_http_src_session_open (GstSoupHTTPSrc * src);
177 static void gst_soup_http_src_session_close (GstSoupHTTPSrc * src);
178 static GstFlowReturn gst_soup_http_src_parse_status (SoupMessage * msg,
179 GstSoupHTTPSrc * src);
180 static GstFlowReturn gst_soup_http_src_got_headers (GstSoupHTTPSrc * src,
182 static void gst_soup_http_src_authenticate_cb (SoupSession * session,
183 SoupMessage * msg, SoupAuth * auth, gboolean retrying,
184 GstSoupHTTPSrc * src);
186 #define gst_soup_http_src_parent_class parent_class
187 G_DEFINE_TYPE_WITH_CODE (GstSoupHTTPSrc, gst_soup_http_src, GST_TYPE_PUSH_SRC,
188 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
189 gst_soup_http_src_uri_handler_init));
192 gst_soup_http_src_class_init (GstSoupHTTPSrcClass * klass)
194 GObjectClass *gobject_class;
195 GstElementClass *gstelement_class;
196 GstBaseSrcClass *gstbasesrc_class;
197 GstPushSrcClass *gstpushsrc_class;
199 gobject_class = (GObjectClass *) klass;
200 gstelement_class = (GstElementClass *) klass;
201 gstbasesrc_class = (GstBaseSrcClass *) klass;
202 gstpushsrc_class = (GstPushSrcClass *) klass;
204 gobject_class->set_property = gst_soup_http_src_set_property;
205 gobject_class->get_property = gst_soup_http_src_get_property;
206 gobject_class->finalize = gst_soup_http_src_finalize;
207 gobject_class->dispose = gst_soup_http_src_dispose;
209 g_object_class_install_property (gobject_class,
211 g_param_spec_string ("location", "Location",
212 "Location to read from", "",
213 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
214 g_object_class_install_property (gobject_class,
216 g_param_spec_string ("user-agent", "User-Agent",
217 "Value of the User-Agent HTTP request header field",
218 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
219 g_object_class_install_property (gobject_class,
220 PROP_AUTOMATIC_REDIRECT,
221 g_param_spec_boolean ("automatic-redirect", "automatic-redirect",
222 "Automatically follow HTTP redirects (HTTP Status Code 3xx)",
223 TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
224 g_object_class_install_property (gobject_class,
226 g_param_spec_string ("proxy", "Proxy",
227 "HTTP proxy server URI", "",
228 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
229 g_object_class_install_property (gobject_class,
231 g_param_spec_string ("user-id", "user-id",
232 "HTTP location URI user id for authentication", "",
233 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
234 g_object_class_install_property (gobject_class, PROP_USER_PW,
235 g_param_spec_string ("user-pw", "user-pw",
236 "HTTP location URI user password for authentication", "",
237 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
238 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
239 g_param_spec_string ("proxy-id", "proxy-id",
240 "HTTP proxy URI user id for authentication", "",
241 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
242 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
243 g_param_spec_string ("proxy-pw", "proxy-pw",
244 "HTTP proxy URI user password for authentication", "",
245 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
246 g_object_class_install_property (gobject_class, PROP_COOKIES,
247 g_param_spec_boxed ("cookies", "Cookies", "HTTP request cookies",
248 G_TYPE_STRV, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
249 g_object_class_install_property (gobject_class, PROP_IS_LIVE,
250 g_param_spec_boolean ("is-live", "is-live", "Act like a live source",
251 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
252 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
253 g_param_spec_uint ("timeout", "timeout",
254 "Value in seconds to timeout a blocking I/O (0 = No timeout).", 0,
255 3600, DEFAULT_TIMEOUT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
256 g_object_class_install_property (gobject_class, PROP_EXTRA_HEADERS,
257 g_param_spec_boxed ("extra-headers", "Extra Headers",
258 "Extra headers to append to the HTTP request",
259 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
260 g_object_class_install_property (gobject_class, PROP_IRADIO_MODE,
261 g_param_spec_boolean ("iradio-mode", "iradio-mode",
262 "Enable internet radio mode (ask server to send shoutcast/icecast "
263 "metadata interleaved with the actual stream data)",
264 DEFAULT_IRADIO_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
267 * GstSoupHTTPSrc::http-log-level:
269 * If set and > 0, captures and dumps HTTP session data as
270 * log messages if log level >= GST_LEVEL_TRACE
274 g_object_class_install_property (gobject_class, PROP_SOUP_LOG_LEVEL,
275 g_param_spec_enum ("http-log-level", "HTTP log level",
276 "Set log level for soup's HTTP session log",
277 SOUP_TYPE_LOGGER_LOG_LEVEL, DEFAULT_SOUP_LOG_LEVEL,
278 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
281 * GstSoupHTTPSrc::compress:
283 * If set to %TRUE, souphttpsrc will automatically handle gzip
284 * and deflate Content-Encodings. This does not make much difference
285 * and causes more load for normal media files, but makes a real
286 * difference in size for plaintext files.
290 g_object_class_install_property (gobject_class, PROP_COMPRESS,
291 g_param_spec_boolean ("compress", "Compress",
292 "Allow compressed content encodings",
293 DEFAULT_COMPRESS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
296 * GstSoupHTTPSrc::keep-alive:
298 * If set to %TRUE, souphttpsrc will keep alive connections when being
299 * set to READY state and only will close connections when connecting
300 * to a different server or when going to NULL state..
304 g_object_class_install_property (gobject_class, PROP_KEEP_ALIVE,
305 g_param_spec_boolean ("keep-alive", "keep-alive",
306 "Use HTTP persistent connections", DEFAULT_KEEP_ALIVE,
307 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
310 * GstSoupHTTPSrc::ssl-strict:
312 * If set to %TRUE, souphttpsrc will reject all SSL certificates that
313 * are considered invalid.
317 g_object_class_install_property (gobject_class, PROP_SSL_STRICT,
318 g_param_spec_boolean ("ssl-strict", "SSL Strict",
319 "Strict SSL certificate checking", DEFAULT_SSL_STRICT,
320 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
323 * GstSoupHTTPSrc::ssl-ca-file:
325 * A SSL anchor CA file that should be used for checking certificates
326 * instead of the system CA file.
328 * If this property is non-%NULL, #GstSoupHTTPSrc::ssl-use-system-ca-file
329 * value will be ignored.
331 * Deprecated: Use #GstSoupHTTPSrc::tls-database property instead.
334 g_object_class_install_property (gobject_class, PROP_SSL_CA_FILE,
335 g_param_spec_string ("ssl-ca-file", "SSL CA File",
336 "Location of a SSL anchor CA file to use", DEFAULT_SSL_CA_FILE,
337 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
340 * GstSoupHTTPSrc::ssl-use-system-ca-file:
342 * If set to %TRUE, souphttpsrc will use the system's CA file for
343 * checking certificates, unless #GstSoupHTTPSrc::ssl-ca-file or
344 * #GstSoupHTTPSrc::tls-database are non-%NULL.
348 g_object_class_install_property (gobject_class, PROP_SSL_USE_SYSTEM_CA_FILE,
349 g_param_spec_boolean ("ssl-use-system-ca-file", "Use System CA File",
350 "Use system CA file", DEFAULT_SSL_USE_SYSTEM_CA_FILE,
351 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
354 * GstSoupHTTPSrc::tls-database:
356 * TLS database with anchor certificate authorities used to validate
357 * the server certificate.
359 * If this property is non-%NULL, #GstSoupHTTPSrc::ssl-use-system-ca-file
360 * and #GstSoupHTTPSrc::ssl-ca-file values will be ignored.
364 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
365 g_param_spec_object ("tls-database", "TLS database",
366 "TLS database with anchor certificate authorities used to validate the server certificate",
367 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
370 * GstSoupHTTPSrc::tls-interaction:
372 * A #GTlsInteraction object to be used when the connection or certificate
373 * database need to interact with the user. This will be used to prompt the
374 * user for passwords or certificate where necessary.
378 g_object_class_install_property (gobject_class, PROP_TLS_INTERACTION,
379 g_param_spec_object ("tls-interaction", "TLS interaction",
380 "A GTlsInteraction object to be used when the connection or certificate database need to interact with the user.",
381 G_TYPE_TLS_INTERACTION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
384 * GstSoupHTTPSrc::retries:
386 * Maximum number of retries until giving up.
390 g_object_class_install_property (gobject_class, PROP_RETRIES,
391 g_param_spec_int ("retries", "Retries",
392 "Maximum number of retries until giving up (-1=infinite)", -1,
393 G_MAXINT, DEFAULT_RETRIES,
394 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
397 * GstSoupHTTPSrc::method
399 * The HTTP method to use when making a request
403 g_object_class_install_property (gobject_class, PROP_METHOD,
404 g_param_spec_string ("method", "HTTP method",
405 "The HTTP method to use (GET, HEAD, OPTIONS, etc)",
406 DEFAULT_SOUP_METHOD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
408 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
410 gst_element_class_set_static_metadata (gstelement_class, "HTTP client source",
412 "Receive data as a client over the network via HTTP using SOUP",
413 "Wouter Cloetens <wouter@mind.be>");
414 gstelement_class->change_state =
415 GST_DEBUG_FUNCPTR (gst_soup_http_src_change_state);
417 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_soup_http_src_start);
418 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_soup_http_src_stop);
419 gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_soup_http_src_unlock);
420 gstbasesrc_class->unlock_stop =
421 GST_DEBUG_FUNCPTR (gst_soup_http_src_unlock_stop);
422 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_soup_http_src_get_size);
423 gstbasesrc_class->is_seekable =
424 GST_DEBUG_FUNCPTR (gst_soup_http_src_is_seekable);
425 gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_soup_http_src_do_seek);
426 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_soup_http_src_query);
428 gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_soup_http_src_create);
430 GST_DEBUG_CATEGORY_INIT (souphttpsrc_debug, "souphttpsrc", 0,
435 gst_soup_http_src_reset (GstSoupHTTPSrc * src)
437 src->retry_count = 0;
438 src->have_size = FALSE;
439 src->got_headers = FALSE;
440 src->seekable = FALSE;
441 src->read_position = 0;
442 src->request_position = 0;
443 src->stop_position = -1;
444 src->content_size = 0;
445 src->have_body = FALSE;
447 src->reduce_blocksize_count = 0;
448 src->increase_blocksize_count = 0;
450 g_cancellable_reset (src->cancellable);
451 if (src->input_stream) {
452 g_object_unref (src->input_stream);
453 src->input_stream = NULL;
456 gst_caps_replace (&src->src_caps, NULL);
457 g_free (src->iradio_name);
458 src->iradio_name = NULL;
459 g_free (src->iradio_genre);
460 src->iradio_genre = NULL;
461 g_free (src->iradio_url);
462 src->iradio_url = NULL;
466 gst_soup_http_src_init (GstSoupHTTPSrc * src)
470 g_mutex_init (&src->mutex);
471 g_cond_init (&src->have_headers_cond);
472 src->cancellable = g_cancellable_new ();
473 src->location = NULL;
474 src->redirection_uri = NULL;
475 src->automatic_redirect = TRUE;
476 src->user_agent = g_strdup (DEFAULT_USER_AGENT);
479 src->proxy_id = NULL;
480 src->proxy_pw = NULL;
482 src->iradio_mode = DEFAULT_IRADIO_MODE;
485 src->timeout = DEFAULT_TIMEOUT;
486 src->log_level = DEFAULT_SOUP_LOG_LEVEL;
487 src->ssl_strict = DEFAULT_SSL_STRICT;
488 src->ssl_use_system_ca_file = DEFAULT_SSL_USE_SYSTEM_CA_FILE;
489 src->tls_database = DEFAULT_TLS_DATABASE;
490 src->tls_interaction = DEFAULT_TLS_INTERACTION;
491 src->max_retries = DEFAULT_RETRIES;
492 src->method = DEFAULT_SOUP_METHOD;
493 src->minimum_blocksize = gst_base_src_get_blocksize (GST_BASE_SRC_CAST (src));
494 proxy = g_getenv ("http_proxy");
495 if (!gst_soup_http_src_set_proxy (src, proxy)) {
496 GST_WARNING_OBJECT (src,
497 "The proxy in the http_proxy env var (\"%s\") cannot be parsed.",
501 gst_base_src_set_automatic_eos (GST_BASE_SRC (src), FALSE);
503 gst_soup_http_src_reset (src);
507 gst_soup_http_src_dispose (GObject * gobject)
509 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (gobject);
511 GST_DEBUG_OBJECT (src, "dispose");
513 gst_soup_http_src_session_close (src);
515 G_OBJECT_CLASS (parent_class)->dispose (gobject);
519 gst_soup_http_src_finalize (GObject * gobject)
521 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (gobject);
523 GST_DEBUG_OBJECT (src, "finalize");
525 g_mutex_clear (&src->mutex);
526 g_cond_clear (&src->have_headers_cond);
527 g_object_unref (src->cancellable);
528 g_free (src->location);
529 g_free (src->redirection_uri);
530 g_free (src->user_agent);
531 if (src->proxy != NULL) {
532 soup_uri_free (src->proxy);
534 g_free (src->user_id);
535 g_free (src->user_pw);
536 g_free (src->proxy_id);
537 g_free (src->proxy_pw);
538 g_strfreev (src->cookies);
540 if (src->extra_headers) {
541 gst_structure_free (src->extra_headers);
542 src->extra_headers = NULL;
545 g_free (src->ssl_ca_file);
547 if (src->tls_database)
548 g_object_unref (src->tls_database);
549 g_free (src->method);
551 if (src->tls_interaction)
552 g_object_unref (src->tls_interaction);
554 G_OBJECT_CLASS (parent_class)->finalize (gobject);
558 gst_soup_http_src_set_property (GObject * object, guint prop_id,
559 const GValue * value, GParamSpec * pspec)
561 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (object);
566 const gchar *location;
568 location = g_value_get_string (value);
570 if (location == NULL) {
571 GST_WARNING ("location property cannot be NULL");
574 if (!gst_soup_http_src_set_location (src, location, NULL)) {
575 GST_WARNING ("badly formatted location");
580 case PROP_USER_AGENT:
581 g_free (src->user_agent);
582 src->user_agent = g_value_dup_string (value);
584 case PROP_IRADIO_MODE:
585 src->iradio_mode = g_value_get_boolean (value);
587 case PROP_AUTOMATIC_REDIRECT:
588 src->automatic_redirect = g_value_get_boolean (value);
594 proxy = g_value_get_string (value);
595 if (!gst_soup_http_src_set_proxy (src, proxy)) {
596 GST_WARNING ("badly formatted proxy URI");
602 g_strfreev (src->cookies);
603 src->cookies = g_strdupv (g_value_get_boxed (value));
606 gst_base_src_set_live (GST_BASE_SRC (src), g_value_get_boolean (value));
609 g_free (src->user_id);
610 src->user_id = g_value_dup_string (value);
613 g_free (src->user_pw);
614 src->user_pw = g_value_dup_string (value);
617 g_free (src->proxy_id);
618 src->proxy_id = g_value_dup_string (value);
621 g_free (src->proxy_pw);
622 src->proxy_pw = g_value_dup_string (value);
625 src->timeout = g_value_get_uint (value);
627 case PROP_EXTRA_HEADERS:{
628 const GstStructure *s = gst_value_get_structure (value);
630 if (src->extra_headers)
631 gst_structure_free (src->extra_headers);
633 src->extra_headers = s ? gst_structure_copy (s) : NULL;
636 case PROP_SOUP_LOG_LEVEL:
637 src->log_level = g_value_get_enum (value);
640 src->compress = g_value_get_boolean (value);
642 case PROP_KEEP_ALIVE:
643 src->keep_alive = g_value_get_boolean (value);
645 case PROP_SSL_STRICT:
646 src->ssl_strict = g_value_get_boolean (value);
648 case PROP_SSL_CA_FILE:
649 g_free (src->ssl_ca_file);
650 src->ssl_ca_file = g_value_dup_string (value);
652 case PROP_SSL_USE_SYSTEM_CA_FILE:
653 src->ssl_use_system_ca_file = g_value_get_boolean (value);
655 case PROP_TLS_DATABASE:
656 g_clear_object (&src->tls_database);
657 src->tls_database = g_value_dup_object (value);
659 case PROP_TLS_INTERACTION:
660 g_clear_object (&src->tls_interaction);
661 src->tls_interaction = g_value_dup_object (value);
664 src->max_retries = g_value_get_int (value);
667 g_free (src->method);
668 src->method = g_value_dup_string (value);
671 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
679 gst_soup_http_src_get_property (GObject * object, guint prop_id,
680 GValue * value, GParamSpec * pspec)
682 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (object);
686 g_value_set_string (value, src->location);
688 case PROP_USER_AGENT:
689 g_value_set_string (value, src->user_agent);
691 case PROP_AUTOMATIC_REDIRECT:
692 g_value_set_boolean (value, src->automatic_redirect);
695 if (src->proxy == NULL)
696 g_value_set_static_string (value, "");
698 char *proxy = soup_uri_to_string (src->proxy, FALSE);
700 g_value_set_string (value, proxy);
705 g_value_set_boxed (value, g_strdupv (src->cookies));
708 g_value_set_boolean (value, gst_base_src_is_live (GST_BASE_SRC (src)));
710 case PROP_IRADIO_MODE:
711 g_value_set_boolean (value, src->iradio_mode);
714 g_value_set_string (value, src->user_id);
717 g_value_set_string (value, src->user_pw);
720 g_value_set_string (value, src->proxy_id);
723 g_value_set_string (value, src->proxy_pw);
726 g_value_set_uint (value, src->timeout);
728 case PROP_EXTRA_HEADERS:
729 gst_value_set_structure (value, src->extra_headers);
731 case PROP_SOUP_LOG_LEVEL:
732 g_value_set_enum (value, src->log_level);
735 g_value_set_boolean (value, src->compress);
737 case PROP_KEEP_ALIVE:
738 g_value_set_boolean (value, src->keep_alive);
740 case PROP_SSL_STRICT:
741 g_value_set_boolean (value, src->ssl_strict);
743 case PROP_SSL_CA_FILE:
744 g_value_set_string (value, src->ssl_ca_file);
746 case PROP_SSL_USE_SYSTEM_CA_FILE:
747 g_value_set_boolean (value, src->ssl_use_system_ca_file);
749 case PROP_TLS_DATABASE:
750 g_value_set_object (value, src->tls_database);
752 case PROP_TLS_INTERACTION:
753 g_value_set_object (value, src->tls_interaction);
756 g_value_set_int (value, src->max_retries);
759 g_value_set_string (value, src->method);
762 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
768 gst_soup_http_src_unicodify (const gchar * str)
770 const gchar *env_vars[] = { "GST_ICY_TAG_ENCODING",
771 "GST_TAG_ENCODING", NULL
774 return gst_tag_freeform_string_to_utf8 (str, -1, env_vars);
778 gst_soup_http_src_cancel_message (GstSoupHTTPSrc * src)
780 g_cancellable_cancel (src->cancellable);
781 g_cond_signal (&src->have_headers_cond);
785 gst_soup_http_src_add_range_header (GstSoupHTTPSrc * src, guint64 offset,
791 soup_message_headers_remove (src->msg->request_headers, "Range");
792 if (offset || stop_offset != -1) {
793 if (stop_offset != -1) {
794 g_assert (offset != stop_offset);
796 rc = g_snprintf (buf, sizeof (buf), "bytes=%" G_GUINT64_FORMAT "-%"
797 G_GUINT64_FORMAT, offset, (stop_offset > 0) ? stop_offset - 1 :
800 rc = g_snprintf (buf, sizeof (buf), "bytes=%" G_GUINT64_FORMAT "-",
803 if (rc > sizeof (buf) || rc < 0)
805 soup_message_headers_append (src->msg->request_headers, "Range", buf);
807 src->read_position = offset;
812 _append_extra_header (GQuark field_id, const GValue * value, gpointer user_data)
814 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (user_data);
815 const gchar *field_name = g_quark_to_string (field_id);
816 gchar *field_content = NULL;
818 if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
819 field_content = g_value_dup_string (value);
821 GValue dest = { 0, };
823 g_value_init (&dest, G_TYPE_STRING);
824 if (g_value_transform (value, &dest)) {
825 field_content = g_value_dup_string (&dest);
829 if (field_content == NULL) {
830 GST_ERROR_OBJECT (src, "extra-headers field '%s' contains no value "
831 "or can't be converted to a string", field_name);
835 GST_DEBUG_OBJECT (src, "Appending extra header: \"%s: %s\"", field_name,
837 soup_message_headers_append (src->msg->request_headers, field_name,
840 g_free (field_content);
846 _append_extra_headers (GQuark field_id, const GValue * value,
849 if (G_VALUE_TYPE (value) == GST_TYPE_ARRAY) {
850 guint n = gst_value_array_get_size (value);
853 for (i = 0; i < n; i++) {
854 const GValue *v = gst_value_array_get_value (value, i);
856 if (!_append_extra_header (field_id, v, user_data))
859 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
860 guint n = gst_value_list_get_size (value);
863 for (i = 0; i < n; i++) {
864 const GValue *v = gst_value_list_get_value (value, i);
866 if (!_append_extra_header (field_id, v, user_data))
870 return _append_extra_header (field_id, value, user_data);
878 gst_soup_http_src_add_extra_headers (GstSoupHTTPSrc * src)
880 if (!src->extra_headers)
883 return gst_structure_foreach (src->extra_headers, _append_extra_headers, src);
887 gst_soup_http_src_session_open (GstSoupHTTPSrc * src)
890 GST_DEBUG_OBJECT (src, "Session is already open");
894 if (!src->location) {
895 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (_("No URL set.")),
896 ("Missing location property"));
901 GST_DEBUG_OBJECT (src, "Creating session");
902 if (src->proxy == NULL) {
904 soup_session_new_with_options (SOUP_SESSION_USER_AGENT,
905 src->user_agent, SOUP_SESSION_TIMEOUT, src->timeout,
906 SOUP_SESSION_SSL_STRICT, src->ssl_strict,
907 SOUP_SESSION_TLS_INTERACTION, src->tls_interaction, NULL);
910 soup_session_new_with_options (SOUP_SESSION_PROXY_URI, src->proxy,
911 SOUP_SESSION_TIMEOUT, src->timeout,
912 SOUP_SESSION_SSL_STRICT, src->ssl_strict,
913 SOUP_SESSION_USER_AGENT, src->user_agent,
914 SOUP_SESSION_TLS_INTERACTION, src->tls_interaction, NULL);
918 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
919 (NULL), ("Failed to create async session"));
923 g_signal_connect (src->session, "authenticate",
924 G_CALLBACK (gst_soup_http_src_authenticate_cb), src);
927 gst_soup_util_log_setup (src->session, src->log_level, GST_ELEMENT (src));
928 if (src->tls_database)
929 g_object_set (src->session, "tls-database", src->tls_database, NULL);
930 else if (src->ssl_ca_file)
931 g_object_set (src->session, "ssl-ca-file", src->ssl_ca_file, NULL);
933 g_object_set (src->session, "ssl-use-system-ca-file",
934 src->ssl_use_system_ca_file, NULL);
936 GST_DEBUG_OBJECT (src, "Re-using session");
940 soup_session_add_feature_by_type (src->session, SOUP_TYPE_CONTENT_DECODER);
942 soup_session_remove_feature_by_type (src->session,
943 SOUP_TYPE_CONTENT_DECODER);
949 gst_soup_http_src_session_close (GstSoupHTTPSrc * src)
951 GST_DEBUG_OBJECT (src, "Closing session");
953 g_mutex_lock (&src->mutex);
955 soup_session_cancel_message (src->session, src->msg, SOUP_STATUS_CANCELLED);
956 g_object_unref (src->msg);
961 soup_session_abort (src->session);
962 g_object_unref (src->session);
965 g_mutex_unlock (&src->mutex);
969 gst_soup_http_src_authenticate_cb (SoupSession * session, SoupMessage * msg,
970 SoupAuth * auth, gboolean retrying, GstSoupHTTPSrc * src)
973 /* First time authentication only, if we fail and are called again with retry true fall through */
974 if (msg->status_code == SOUP_STATUS_UNAUTHORIZED) {
975 if (src->user_id && src->user_pw)
976 soup_auth_authenticate (auth, src->user_id, src->user_pw);
977 } else if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
978 if (src->proxy_id && src->proxy_pw)
979 soup_auth_authenticate (auth, src->proxy_id, src->proxy_pw);
985 insert_http_header (const gchar * name, const gchar * value, gpointer user_data)
987 GstStructure *headers = user_data;
990 if (!g_utf8_validate (name, -1, NULL) || !g_utf8_validate (value, -1, NULL))
993 gv = gst_structure_get_value (headers, name);
994 if (gv && GST_VALUE_HOLDS_ARRAY (gv)) {
995 GValue v = G_VALUE_INIT;
997 g_value_init (&v, G_TYPE_STRING);
998 g_value_set_string (&v, value);
999 gst_value_array_append_value ((GValue *) gv, &v);
1001 } else if (gv && G_VALUE_HOLDS_STRING (gv)) {
1002 GValue arr = G_VALUE_INIT;
1003 GValue v = G_VALUE_INIT;
1004 const gchar *old_value = g_value_get_string (gv);
1006 g_value_init (&arr, GST_TYPE_ARRAY);
1007 g_value_init (&v, G_TYPE_STRING);
1008 g_value_set_string (&v, old_value);
1009 gst_value_array_append_value (&arr, &v);
1010 g_value_set_string (&v, value);
1011 gst_value_array_append_value (&arr, &v);
1013 gst_structure_set_value (headers, name, &arr);
1015 g_value_unset (&arr);
1017 gst_structure_set (headers, name, G_TYPE_STRING, value, NULL);
1021 static GstFlowReturn
1022 gst_soup_http_src_got_headers (GstSoupHTTPSrc * src, SoupMessage * msg)
1025 GstTagList *tag_list;
1026 GstBaseSrc *basesrc;
1028 GHashTable *params = NULL;
1029 GstEvent *http_headers_event;
1030 GstStructure *http_headers, *headers;
1031 const gchar *accept_ranges;
1033 GST_INFO_OBJECT (src, "got headers");
1035 if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED &&
1036 src->proxy_id && src->proxy_pw) {
1037 /* wait for authenticate callback */
1041 if (msg->status_code == SOUP_STATUS_UNAUTHORIZED) {
1042 /* force an error */
1043 return gst_soup_http_src_parse_status (msg, src);
1046 src->got_headers = TRUE;
1047 g_cond_broadcast (&src->have_headers_cond);
1049 http_headers = gst_structure_new_empty ("http-headers");
1050 gst_structure_set (http_headers, "uri", G_TYPE_STRING, src->location, NULL);
1051 if (src->redirection_uri)
1052 gst_structure_set (http_headers, "redirection-uri", G_TYPE_STRING,
1053 src->redirection_uri, NULL);
1054 headers = gst_structure_new_empty ("request-headers");
1055 soup_message_headers_foreach (msg->request_headers, insert_http_header,
1057 gst_structure_set (http_headers, "request-headers", GST_TYPE_STRUCTURE,
1059 gst_structure_free (headers);
1060 headers = gst_structure_new_empty ("response-headers");
1061 soup_message_headers_foreach (msg->response_headers, insert_http_header,
1063 gst_structure_set (http_headers, "response-headers", GST_TYPE_STRUCTURE,
1065 gst_structure_free (headers);
1067 http_headers_event =
1068 gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_STICKY, http_headers);
1069 gst_event_replace (&src->http_headers_event, http_headers_event);
1070 gst_event_unref (http_headers_event);
1072 /* Parse Content-Length. */
1073 if (soup_message_headers_get_encoding (msg->response_headers) ==
1074 SOUP_ENCODING_CONTENT_LENGTH) {
1075 newsize = src->request_position +
1076 soup_message_headers_get_content_length (msg->response_headers);
1077 if (!src->have_size || (src->content_size != newsize)) {
1078 src->content_size = newsize;
1079 src->have_size = TRUE;
1080 src->seekable = TRUE;
1081 GST_DEBUG_OBJECT (src, "size = %" G_GUINT64_FORMAT, src->content_size);
1083 basesrc = GST_BASE_SRC_CAST (src);
1084 basesrc->segment.duration = src->content_size;
1085 gst_element_post_message (GST_ELEMENT (src),
1086 gst_message_new_duration_changed (GST_OBJECT (src)));
1090 /* If the server reports Accept-Ranges: none we don't have to try
1091 * doing range requests at all
1093 if ((accept_ranges =
1094 soup_message_headers_get_one (msg->response_headers,
1095 "Accept-Ranges"))) {
1096 if (g_ascii_strcasecmp (accept_ranges, "none") == 0)
1097 src->seekable = FALSE;
1101 tag_list = gst_tag_list_new_empty ();
1104 soup_message_headers_get_one (msg->response_headers,
1105 "icy-metaint")) != NULL) {
1108 if (g_utf8_validate (value, -1, NULL)) {
1109 icy_metaint = atoi (value);
1111 GST_DEBUG_OBJECT (src, "icy-metaint: %s (parsed: %d)", value,
1113 if (icy_metaint > 0) {
1115 gst_caps_unref (src->src_caps);
1117 src->src_caps = gst_caps_new_simple ("application/x-icy",
1118 "metadata-interval", G_TYPE_INT, icy_metaint, NULL);
1120 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
1125 soup_message_headers_get_content_type (msg->response_headers,
1126 ¶ms)) != NULL) {
1127 if (!g_utf8_validate (value, -1, NULL)) {
1128 GST_WARNING_OBJECT (src, "Content-Type is invalid UTF-8");
1129 } else if (g_ascii_strcasecmp (value, "audio/L16") == 0) {
1134 GST_DEBUG_OBJECT (src, "Content-Type: %s", value);
1136 if (src->src_caps) {
1137 gst_caps_unref (src->src_caps);
1138 src->src_caps = NULL;
1141 param = g_hash_table_lookup (params, "channels");
1142 if (param != NULL) {
1143 guint64 val = g_ascii_strtoull (param, NULL, 10);
1150 param = g_hash_table_lookup (params, "rate");
1151 if (param != NULL) {
1152 guint64 val = g_ascii_strtoull (param, NULL, 10);
1159 if (rate > 0 && channels > 0) {
1160 src->src_caps = gst_caps_new_simple ("audio/x-unaligned-raw",
1161 "format", G_TYPE_STRING, "S16BE",
1162 "layout", G_TYPE_STRING, "interleaved",
1163 "channels", G_TYPE_INT, channels, "rate", G_TYPE_INT, rate, NULL);
1165 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
1168 GST_DEBUG_OBJECT (src, "Content-Type: %s", value);
1170 /* Set the Content-Type field on the caps */
1171 if (src->src_caps) {
1172 src->src_caps = gst_caps_make_writable (src->src_caps);
1173 gst_caps_set_simple (src->src_caps, "content-type", G_TYPE_STRING,
1175 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
1181 g_hash_table_destroy (params);
1184 soup_message_headers_get_one (msg->response_headers,
1185 "icy-name")) != NULL) {
1186 if (g_utf8_validate (value, -1, NULL)) {
1187 g_free (src->iradio_name);
1188 src->iradio_name = gst_soup_http_src_unicodify (value);
1189 if (src->iradio_name) {
1190 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_ORGANIZATION,
1191 src->iradio_name, NULL);
1196 soup_message_headers_get_one (msg->response_headers,
1197 "icy-genre")) != NULL) {
1198 if (g_utf8_validate (value, -1, NULL)) {
1199 g_free (src->iradio_genre);
1200 src->iradio_genre = gst_soup_http_src_unicodify (value);
1201 if (src->iradio_genre) {
1202 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_GENRE,
1203 src->iradio_genre, NULL);
1207 if ((value = soup_message_headers_get_one (msg->response_headers, "icy-url"))
1209 if (g_utf8_validate (value, -1, NULL)) {
1210 g_free (src->iradio_url);
1211 src->iradio_url = gst_soup_http_src_unicodify (value);
1212 if (src->iradio_url) {
1213 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_LOCATION,
1214 src->iradio_url, NULL);
1218 if (!gst_tag_list_is_empty (tag_list)) {
1219 GST_DEBUG_OBJECT (src,
1220 "calling gst_element_found_tags with %" GST_PTR_FORMAT, tag_list);
1221 gst_pad_push_event (GST_BASE_SRC_PAD (src), gst_event_new_tag (tag_list));
1223 gst_tag_list_unref (tag_list);
1226 /* Handle HTTP errors. */
1227 return gst_soup_http_src_parse_status (msg, src);
1231 gst_soup_http_src_alloc_buffer (GstSoupHTTPSrc * src)
1233 GstBaseSrc *basesrc = GST_BASE_SRC_CAST (src);
1237 rc = GST_BASE_SRC_CLASS (parent_class)->alloc (basesrc, -1,
1238 basesrc->blocksize, &gstbuf);
1239 if (G_UNLIKELY (rc != GST_FLOW_OK)) {
1246 #define SOUP_HTTP_SRC_ERROR(src,soup_msg,cat,code,error_message) \
1248 GST_ELEMENT_ERROR_WITH_DETAILS ((src), cat, code, ("%s", error_message), \
1249 ("%s (%d), URL: %s, Redirect to: %s", (soup_msg)->reason_phrase, \
1250 (soup_msg)->status_code, (src)->location, GST_STR_NULL ((src)->redirection_uri)), \
1251 ("http-status-code", G_TYPE_UINT, msg->status_code, \
1252 "http-redirect-uri", G_TYPE_STRING, GST_STR_NULL ((src)->redirection_uri), NULL)); \
1255 static GstFlowReturn
1256 gst_soup_http_src_parse_status (SoupMessage * msg, GstSoupHTTPSrc * src)
1258 if (msg->method == SOUP_METHOD_HEAD) {
1259 if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code))
1260 GST_DEBUG_OBJECT (src, "Ignoring error %d during HEAD request",
1265 if (SOUP_STATUS_IS_TRANSPORT_ERROR (msg->status_code)) {
1266 switch (msg->status_code) {
1267 case SOUP_STATUS_CANT_RESOLVE:
1268 case SOUP_STATUS_CANT_RESOLVE_PROXY:
1269 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, NOT_FOUND,
1270 _("Could not resolve server name."));
1271 return GST_FLOW_ERROR;
1272 case SOUP_STATUS_CANT_CONNECT:
1273 case SOUP_STATUS_CANT_CONNECT_PROXY:
1274 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ,
1275 _("Could not establish connection to server."));
1276 return GST_FLOW_ERROR;
1277 case SOUP_STATUS_SSL_FAILED:
1278 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ,
1279 _("Secure connection setup failed."));
1280 return GST_FLOW_ERROR;
1281 case SOUP_STATUS_IO_ERROR:
1282 if (src->max_retries == -1 || src->retry_count < src->max_retries)
1283 return GST_FLOW_CUSTOM_ERROR;
1284 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, READ,
1285 _("A network error occurred, or the server closed the connection "
1287 return GST_FLOW_ERROR;
1288 case SOUP_STATUS_MALFORMED:
1289 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, READ,
1290 _("Server sent bad data."));
1291 return GST_FLOW_ERROR;
1292 case SOUP_STATUS_CANCELLED:
1293 /* No error message when interrupted by program. */
1299 if (SOUP_STATUS_IS_CLIENT_ERROR (msg->status_code) ||
1300 SOUP_STATUS_IS_REDIRECTION (msg->status_code) ||
1301 SOUP_STATUS_IS_SERVER_ERROR (msg->status_code)) {
1302 const gchar *reason_phrase;
1304 reason_phrase = msg->reason_phrase;
1305 if (reason_phrase && !g_utf8_validate (reason_phrase, -1, NULL)) {
1306 GST_ERROR_OBJECT (src, "Invalid UTF-8 in reason");
1307 reason_phrase = "(invalid)";
1310 /* Report HTTP error. */
1312 /* when content_size is unknown and we have just finished receiving
1313 * a body message, requests that go beyond the content limits will result
1314 * in an error. Here we convert those to EOS */
1315 if (msg->status_code == SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE &&
1316 src->have_body && !src->have_size) {
1317 GST_DEBUG_OBJECT (src, "Requested range out of limits and received full "
1318 "body, returning EOS");
1319 return GST_FLOW_EOS;
1322 /* FIXME: reason_phrase is not translated and not suitable for user
1323 * error dialog according to libsoup documentation.
1325 if (msg->status_code == SOUP_STATUS_NOT_FOUND) {
1326 GST_ELEMENT_ERROR_WITH_DETAILS (src, RESOURCE, NOT_FOUND,
1327 ("%s", reason_phrase),
1328 ("%s (%d), URL: %s, Redirect to: %s", reason_phrase,
1329 msg->status_code, src->location,
1330 GST_STR_NULL (src->redirection_uri)),
1331 ("http-status-code", G_TYPE_UINT, msg->status_code,
1332 "http-redirect-uri", G_TYPE_STRING,
1333 GST_STR_NULL (src->redirection_uri), NULL));
1334 } else if (msg->status_code == SOUP_STATUS_UNAUTHORIZED
1335 || msg->status_code == SOUP_STATUS_PAYMENT_REQUIRED
1336 || msg->status_code == SOUP_STATUS_FORBIDDEN
1337 || msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
1338 GST_ELEMENT_ERROR_WITH_DETAILS (src, RESOURCE, NOT_AUTHORIZED, ("%s",
1339 reason_phrase), ("%s (%d), URL: %s, Redirect to: %s",
1340 reason_phrase, msg->status_code, src->location,
1341 GST_STR_NULL (src->redirection_uri)), ("http-status-code",
1342 G_TYPE_UINT, msg->status_code, "http-redirect-uri", G_TYPE_STRING,
1343 GST_STR_NULL (src->redirection_uri), NULL));
1345 GST_ELEMENT_ERROR_WITH_DETAILS (src, RESOURCE, OPEN_READ,
1346 ("%s", reason_phrase),
1347 ("%s (%d), URL: %s, Redirect to: %s", reason_phrase,
1348 msg->status_code, src->location,
1349 GST_STR_NULL (src->redirection_uri)),
1350 ("http-status-code", G_TYPE_UINT, msg->status_code,
1351 "http-redirect-uri", G_TYPE_STRING,
1352 GST_STR_NULL (src->redirection_uri), NULL));
1354 return GST_FLOW_ERROR;
1361 gst_soup_http_src_restarted_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
1363 if (soup_session_would_redirect (src->session, msg)) {
1364 src->redirection_uri =
1365 soup_uri_to_string (soup_message_get_uri (msg), FALSE);
1366 src->redirection_permanent =
1367 (msg->status_code == SOUP_STATUS_MOVED_PERMANENTLY);
1368 GST_DEBUG_OBJECT (src, "%u redirect to \"%s\" (permanent %d)",
1369 msg->status_code, src->redirection_uri, src->redirection_permanent);
1374 gst_soup_http_src_build_message (GstSoupHTTPSrc * src, const gchar * method)
1376 g_return_val_if_fail (src->msg == NULL, FALSE);
1378 src->msg = soup_message_new (method, src->location);
1380 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1381 ("Error parsing URL."), ("URL: %s", src->location));
1384 if (!src->keep_alive) {
1385 soup_message_headers_append (src->msg->request_headers, "Connection",
1388 if (src->iradio_mode) {
1389 soup_message_headers_append (src->msg->request_headers, "icy-metadata",
1395 for (cookie = src->cookies; *cookie != NULL; cookie++) {
1396 soup_message_headers_append (src->msg->request_headers, "Cookie",
1401 soup_message_set_flags (src->msg, SOUP_MESSAGE_OVERWRITE_CHUNKS |
1402 (src->automatic_redirect ? 0 : SOUP_MESSAGE_NO_REDIRECT));
1404 if (src->automatic_redirect) {
1405 g_signal_connect (src->msg, "restarted",
1406 G_CALLBACK (gst_soup_http_src_restarted_cb), src);
1409 gst_soup_http_src_add_range_header (src, src->request_position,
1410 src->stop_position);
1412 gst_soup_http_src_add_extra_headers (src);
1417 static GstFlowReturn
1418 gst_soup_http_src_send_message (GstSoupHTTPSrc * src)
1421 GError *error = NULL;
1423 g_return_val_if_fail (src->msg != NULL, GST_FLOW_ERROR);
1426 soup_session_send (src->session, src->msg, src->cancellable, &error);
1428 if (g_cancellable_is_cancelled (src->cancellable)) {
1429 ret = GST_FLOW_FLUSHING;
1433 ret = gst_soup_http_src_got_headers (src, src->msg);
1434 if (ret != GST_FLOW_OK) {
1438 if (!src->input_stream) {
1439 GST_DEBUG_OBJECT (src, "Didn't get an input stream: %s", error->message);
1440 ret = GST_FLOW_ERROR;
1444 if (SOUP_STATUS_IS_SUCCESSFUL (src->msg->status_code)) {
1445 GST_DEBUG_OBJECT (src, "Successfully got a reply");
1447 /* FIXME - be more helpful to people debugging */
1448 ret = GST_FLOW_ERROR;
1453 g_error_free (error);
1457 static GstFlowReturn
1458 gst_soup_http_src_do_request (GstSoupHTTPSrc * src, const gchar * method)
1462 if (src->max_retries != -1 && src->retry_count > src->max_retries) {
1463 GST_DEBUG_OBJECT (src, "Max retries reached");
1464 return GST_FLOW_ERROR;
1468 /* EOS immediately if we have an empty segment */
1469 if (src->request_position == src->stop_position)
1470 return GST_FLOW_EOS;
1472 GST_LOG_OBJECT (src, "Running request for method: %s", method);
1474 /* Update the position if we are retrying */
1475 if (src->msg && src->request_position > 0) {
1476 gst_soup_http_src_add_range_header (src, src->request_position,
1477 src->stop_position);
1481 if (!gst_soup_http_src_build_message (src, method)) {
1482 return GST_FLOW_ERROR;
1486 if (g_cancellable_is_cancelled (src->cancellable)) {
1487 GST_INFO_OBJECT (src, "interrupted");
1488 return GST_FLOW_FLUSHING;
1491 ret = gst_soup_http_src_send_message (src);
1493 /* Check if Range header was respected. */
1494 if (ret == GST_FLOW_OK && src->request_position > 0 &&
1495 src->msg->status_code != SOUP_STATUS_PARTIAL_CONTENT) {
1496 src->seekable = FALSE;
1497 GST_ELEMENT_ERROR_WITH_DETAILS (src, RESOURCE, SEEK,
1498 (_("Server does not support seeking.")),
1499 ("Server does not accept Range HTTP header, URL: %s, Redirect to: %s",
1500 src->location, GST_STR_NULL (src->redirection_uri)),
1501 ("http-status-code", G_TYPE_UINT, src->msg->status_code,
1502 "http-redirection-uri", G_TYPE_STRING,
1503 GST_STR_NULL (src->redirection_uri), NULL));
1504 ret = GST_FLOW_ERROR;
1511 * Check if the bytes_read is above a certain threshold of the blocksize, if
1512 * that happens a few times in a row, increase the blocksize; Do the same in
1513 * the opposite direction to reduce the blocksize.
1516 gst_soup_http_src_check_update_blocksize (GstSoupHTTPSrc * src,
1519 guint blocksize = gst_base_src_get_blocksize (GST_BASE_SRC_CAST (src));
1521 GST_LOG_OBJECT (src, "Checking to update blocksize. Read:%" G_GINT64_FORMAT
1522 " blocksize:%u", bytes_read, blocksize);
1524 if (bytes_read >= blocksize * GROW_BLOCKSIZE_LIMIT) {
1525 src->reduce_blocksize_count = 0;
1526 src->increase_blocksize_count++;
1528 if (src->increase_blocksize_count >= GROW_BLOCKSIZE_COUNT) {
1529 blocksize *= GROW_BLOCKSIZE_FACTOR;
1530 GST_DEBUG_OBJECT (src, "Increased blocksize to %u", blocksize);
1531 gst_base_src_set_blocksize (GST_BASE_SRC_CAST (src), blocksize);
1532 src->increase_blocksize_count = 0;
1534 } else if (bytes_read < blocksize * REDUCE_BLOCKSIZE_LIMIT) {
1535 src->reduce_blocksize_count++;
1536 src->increase_blocksize_count = 0;
1538 if (src->reduce_blocksize_count >= REDUCE_BLOCKSIZE_COUNT) {
1539 blocksize *= REDUCE_BLOCKSIZE_FACTOR;
1540 blocksize = MAX (blocksize, src->minimum_blocksize);
1541 GST_DEBUG_OBJECT (src, "Decreased blocksize to %u", blocksize);
1542 gst_base_src_set_blocksize (GST_BASE_SRC_CAST (src), blocksize);
1543 src->reduce_blocksize_count = 0;
1546 src->reduce_blocksize_count = src->increase_blocksize_count = 0;
1551 gst_soup_http_src_update_position (GstSoupHTTPSrc * src, gint64 bytes_read)
1553 GstBaseSrc *basesrc = GST_BASE_SRC_CAST (src);
1554 guint64 new_position;
1556 new_position = src->read_position + bytes_read;
1557 if (G_LIKELY (src->request_position == src->read_position))
1558 src->request_position = new_position;
1559 src->read_position = new_position;
1561 if (src->have_size) {
1562 if (new_position > src->content_size) {
1563 GST_DEBUG_OBJECT (src, "Got position previous estimated content size "
1564 "(%" G_GINT64_FORMAT " > %" G_GINT64_FORMAT ")", new_position,
1566 src->content_size = new_position;
1567 basesrc->segment.duration = src->content_size;
1568 gst_element_post_message (GST_ELEMENT (src),
1569 gst_message_new_duration_changed (GST_OBJECT (src)));
1570 } else if (new_position == src->content_size) {
1571 GST_DEBUG_OBJECT (src, "We're EOS now");
1576 static GstFlowReturn
1577 gst_soup_http_src_read_buffer (GstSoupHTTPSrc * src, GstBuffer ** outbuf)
1584 bsrc = GST_BASE_SRC_CAST (src);
1586 *outbuf = gst_soup_http_src_alloc_buffer (src);
1588 GST_WARNING_OBJECT (src, "Failed to allocate buffer");
1589 return GST_FLOW_ERROR;
1592 if (!gst_buffer_map (*outbuf, &mapinfo, GST_MAP_WRITE)) {
1593 GST_WARNING_OBJECT (src, "Failed to map buffer");
1594 return GST_FLOW_ERROR;
1598 g_input_stream_read (src->input_stream, mapinfo.data, mapinfo.size,
1599 src->cancellable, NULL);
1600 GST_DEBUG_OBJECT (src, "Read %" G_GSSIZE_FORMAT " bytes from http input",
1603 g_mutex_lock (&src->mutex);
1604 if (g_cancellable_is_cancelled (src->cancellable)) {
1605 gst_buffer_unmap (*outbuf, &mapinfo);
1606 gst_buffer_unref (*outbuf);
1607 g_mutex_unlock (&src->mutex);
1608 return GST_FLOW_FLUSHING;
1611 gst_buffer_unmap (*outbuf, &mapinfo);
1612 if (read_bytes > 0) {
1613 gst_buffer_set_size (*outbuf, read_bytes);
1614 GST_BUFFER_OFFSET (*outbuf) = bsrc->segment.position;
1616 gst_soup_http_src_update_position (src, read_bytes);
1618 /* Got some data, reset retry counter */
1619 src->retry_count = 0;
1621 gst_soup_http_src_check_update_blocksize (src, read_bytes);
1623 /* If we're at the end of a range request, read again to let libsoup
1624 * finalize the request. This allows to reuse the connection again later,
1625 * otherwise we would have to cancel the message and close the connection
1627 if (bsrc->segment.stop != -1
1628 && bsrc->segment.position + read_bytes >= bsrc->segment.stop) {
1631 g_object_unref (src->msg);
1633 src->have_body = TRUE;
1635 /* This should return immediately as we're at the end of the range */
1637 g_input_stream_read (src->input_stream, tmp, sizeof (tmp),
1638 src->cancellable, NULL);
1640 GST_ERROR_OBJECT (src,
1641 "Read %" G_GSIZE_FORMAT " bytes after end of range", read_bytes);
1644 gst_buffer_unref (*outbuf);
1645 if (read_bytes < 0 ||
1646 (src->have_size && src->read_position < src->content_size)) {
1647 /* Maybe the server disconnected, retry */
1648 ret = GST_FLOW_CUSTOM_ERROR;
1650 g_object_unref (src->msg);
1653 src->have_body = TRUE;
1656 g_mutex_unlock (&src->mutex);
1661 static GstFlowReturn
1662 gst_soup_http_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
1664 GstSoupHTTPSrc *src;
1665 GstFlowReturn ret = GST_FLOW_OK;
1666 GstEvent *http_headers_event = NULL;
1668 src = GST_SOUP_HTTP_SRC (psrc);
1671 g_mutex_lock (&src->mutex);
1673 /* Check for pending position change */
1674 if (src->request_position != src->read_position) {
1675 if (src->input_stream) {
1676 g_input_stream_close (src->input_stream, src->cancellable, NULL);
1677 g_object_unref (src->input_stream);
1678 src->input_stream = NULL;
1682 if (g_cancellable_is_cancelled (src->cancellable)) {
1683 ret = GST_FLOW_FLUSHING;
1684 g_mutex_unlock (&src->mutex);
1688 /* If we have no open connection to the server, start one */
1689 if (!src->input_stream) {
1692 gst_soup_http_src_do_request (src,
1693 src->method ? src->method : SOUP_METHOD_GET);
1694 http_headers_event = src->http_headers_event;
1695 src->http_headers_event = NULL;
1697 g_mutex_unlock (&src->mutex);
1699 if (ret == GST_FLOW_OK || ret == GST_FLOW_CUSTOM_ERROR) {
1700 if (http_headers_event) {
1701 gst_pad_push_event (GST_BASE_SRC_PAD (src), http_headers_event);
1702 http_headers_event = NULL;
1706 if (ret == GST_FLOW_OK)
1707 ret = gst_soup_http_src_read_buffer (src, outbuf);
1710 GST_DEBUG_OBJECT (src, "Returning %d %s", ret, gst_flow_get_name (ret));
1711 if (ret != GST_FLOW_OK) {
1712 if (http_headers_event)
1713 gst_event_unref (http_headers_event);
1715 g_mutex_lock (&src->mutex);
1716 if (src->input_stream) {
1717 g_object_unref (src->input_stream);
1718 src->input_stream = NULL;
1720 g_mutex_unlock (&src->mutex);
1721 if (ret == GST_FLOW_CUSTOM_ERROR) {
1730 gst_soup_http_src_start (GstBaseSrc * bsrc)
1732 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1734 GST_DEBUG_OBJECT (src, "start(\"%s\")", src->location);
1736 return gst_soup_http_src_session_open (src);
1740 gst_soup_http_src_stop (GstBaseSrc * bsrc)
1742 GstSoupHTTPSrc *src;
1744 src = GST_SOUP_HTTP_SRC (bsrc);
1745 GST_DEBUG_OBJECT (src, "stop()");
1746 if (src->keep_alive && !src->msg)
1747 gst_soup_http_src_cancel_message (src);
1749 gst_soup_http_src_session_close (src);
1751 gst_soup_http_src_reset (src);
1755 static GstStateChangeReturn
1756 gst_soup_http_src_change_state (GstElement * element, GstStateChange transition)
1758 GstStateChangeReturn ret;
1759 GstSoupHTTPSrc *src;
1761 src = GST_SOUP_HTTP_SRC (element);
1763 switch (transition) {
1764 case GST_STATE_CHANGE_READY_TO_NULL:
1765 gst_soup_http_src_session_close (src);
1771 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1776 /* Interrupt a blocking request. */
1778 gst_soup_http_src_unlock (GstBaseSrc * bsrc)
1780 GstSoupHTTPSrc *src;
1782 src = GST_SOUP_HTTP_SRC (bsrc);
1783 GST_DEBUG_OBJECT (src, "unlock()");
1785 gst_soup_http_src_cancel_message (src);
1789 /* Interrupt interrupt. */
1791 gst_soup_http_src_unlock_stop (GstBaseSrc * bsrc)
1793 GstSoupHTTPSrc *src;
1795 src = GST_SOUP_HTTP_SRC (bsrc);
1796 GST_DEBUG_OBJECT (src, "unlock_stop()");
1798 g_cancellable_reset (src->cancellable);
1803 gst_soup_http_src_get_size (GstBaseSrc * bsrc, guint64 * size)
1805 GstSoupHTTPSrc *src;
1807 src = GST_SOUP_HTTP_SRC (bsrc);
1809 if (src->have_size) {
1810 GST_DEBUG_OBJECT (src, "get_size() = %" G_GUINT64_FORMAT,
1812 *size = src->content_size;
1815 GST_DEBUG_OBJECT (src, "get_size() = FALSE");
1820 gst_soup_http_src_check_seekable (GstSoupHTTPSrc * src)
1822 GstFlowReturn ret = GST_FLOW_OK;
1824 /* Special case to check if the server allows range requests
1825 * before really starting to get data in the buffer creation
1828 if (!src->got_headers && GST_STATE (src) >= GST_STATE_PAUSED) {
1829 g_mutex_lock (&src->mutex);
1830 while (!src->got_headers && !g_cancellable_is_cancelled (src->cancellable)
1831 && ret == GST_FLOW_OK) {
1832 if ((src->msg && src->msg->method != SOUP_METHOD_HEAD)) {
1833 /* wait for the current request to finish */
1834 g_cond_wait (&src->have_headers_cond, &src->mutex);
1836 if (gst_soup_http_src_session_open (src)) {
1837 ret = gst_soup_http_src_do_request (src, SOUP_METHOD_HEAD);
1841 g_mutex_unlock (&src->mutex);
1846 gst_soup_http_src_is_seekable (GstBaseSrc * bsrc)
1848 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1850 gst_soup_http_src_check_seekable (src);
1852 return src->seekable;
1856 gst_soup_http_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
1858 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1860 GST_DEBUG_OBJECT (src, "do_seek(%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1861 ")", segment->start, segment->stop);
1862 if (src->read_position == segment->start &&
1863 src->request_position == src->read_position &&
1864 src->stop_position == segment->stop) {
1865 GST_DEBUG_OBJECT (src,
1866 "Seek to current read/end position and no seek pending");
1870 gst_soup_http_src_check_seekable (src);
1872 /* If we have no headers we don't know yet if it is seekable or not.
1873 * Store the start position and error out later if it isn't */
1874 if (src->got_headers && !src->seekable) {
1875 GST_WARNING_OBJECT (src, "Not seekable");
1879 if (segment->rate < 0.0 || segment->format != GST_FORMAT_BYTES) {
1880 GST_WARNING_OBJECT (src, "Invalid seek segment");
1884 if (src->have_size && segment->start >= src->content_size) {
1885 GST_WARNING_OBJECT (src,
1886 "Potentially seeking behind end of file, might EOS immediately");
1889 /* Wait for create() to handle the jump in offset. */
1890 src->request_position = segment->start;
1891 src->stop_position = segment->stop;
1897 gst_soup_http_src_query (GstBaseSrc * bsrc, GstQuery * query)
1899 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1901 GstSchedulingFlags flags;
1902 gint minsize, maxsize, align;
1904 switch (GST_QUERY_TYPE (query)) {
1906 gst_query_set_uri (query, src->location);
1907 if (src->redirection_uri != NULL) {
1908 gst_query_set_uri_redirection (query, src->redirection_uri);
1909 gst_query_set_uri_redirection_permanent (query,
1910 src->redirection_permanent);
1920 ret = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
1922 switch (GST_QUERY_TYPE (query)) {
1923 case GST_QUERY_SCHEDULING:
1924 gst_query_parse_scheduling (query, &flags, &minsize, &maxsize, &align);
1925 flags |= GST_SCHEDULING_FLAG_BANDWIDTH_LIMITED;
1926 gst_query_set_scheduling (query, flags, minsize, maxsize, align);
1936 gst_soup_http_src_set_location (GstSoupHTTPSrc * src, const gchar * uri,
1939 const char *alt_schemes[] = { "icy://", "icyx://" };
1942 if (src->location) {
1943 g_free (src->location);
1944 src->location = NULL;
1950 for (i = 0; i < G_N_ELEMENTS (alt_schemes); i++) {
1951 if (g_str_has_prefix (uri, alt_schemes[i])) {
1953 g_strdup_printf ("http://%s", uri + strlen (alt_schemes[i]));
1958 if (src->redirection_uri) {
1959 g_free (src->redirection_uri);
1960 src->redirection_uri = NULL;
1963 src->location = g_strdup (uri);
1969 gst_soup_http_src_set_proxy (GstSoupHTTPSrc * src, const gchar * uri)
1972 soup_uri_free (src->proxy);
1976 if (uri == NULL || *uri == '\0')
1979 if (g_str_has_prefix (uri, "http://")) {
1980 src->proxy = soup_uri_new (uri);
1982 gchar *new_uri = g_strconcat ("http://", uri, NULL);
1984 src->proxy = soup_uri_new (new_uri);
1988 return (src->proxy != NULL);
1992 gst_soup_http_src_uri_get_type (GType type)
1997 static const gchar *const *
1998 gst_soup_http_src_uri_get_protocols (GType type)
2000 static const gchar *protocols[] = { "http", "https", "icy", "icyx", NULL };
2006 gst_soup_http_src_uri_get_uri (GstURIHandler * handler)
2008 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (handler);
2010 /* FIXME: make thread-safe */
2011 return g_strdup (src->location);
2015 gst_soup_http_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
2018 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (handler);
2020 return gst_soup_http_src_set_location (src, uri, error);
2024 gst_soup_http_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
2026 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
2028 iface->get_type = gst_soup_http_src_uri_get_type;
2029 iface->get_protocols = gst_soup_http_src_uri_get_protocols;
2030 iface->get_uri = gst_soup_http_src_uri_get_uri;
2031 iface->set_uri = gst_soup_http_src_uri_set_uri;