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>
81 #include <libsoup/soup.h>
82 #include "gstsouphttpsrc.h"
83 #include "gstsouputils.h"
85 #include <gst/tag/tag.h>
87 GST_DEBUG_CATEGORY_STATIC (souphttpsrc_debug);
88 #define GST_CAT_DEFAULT souphttpsrc_debug
90 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
101 PROP_AUTOMATIC_REDIRECT,
116 PROP_SSL_USE_SYSTEM_CA_FILE,
120 PROP_TLS_INTERACTION,
123 #define DEFAULT_USER_AGENT "GStreamer souphttpsrc "
124 #define DEFAULT_IRADIO_MODE TRUE
125 #define DEFAULT_SOUP_LOG_LEVEL SOUP_LOGGER_LOG_HEADERS
126 #define DEFAULT_COMPRESS FALSE
127 #define DEFAULT_KEEP_ALIVE FALSE
128 #define DEFAULT_SSL_STRICT TRUE
129 #define DEFAULT_SSL_CA_FILE NULL
130 #define DEFAULT_SSL_USE_SYSTEM_CA_FILE TRUE
131 #define DEFAULT_TLS_DATABASE NULL
132 #define DEFAULT_TLS_INTERACTION NULL
133 #define DEFAULT_TIMEOUT 15
134 #define DEFAULT_RETRIES 3
135 #define DEFAULT_SOUP_METHOD NULL
137 #define GROW_BLOCKSIZE_LIMIT 1
138 #define GROW_BLOCKSIZE_COUNT 1
139 #define GROW_BLOCKSIZE_FACTOR 2
140 #define REDUCE_BLOCKSIZE_LIMIT 0.20
141 #define REDUCE_BLOCKSIZE_COUNT 2
142 #define REDUCE_BLOCKSIZE_FACTOR 0.5
144 static void gst_soup_http_src_uri_handler_init (gpointer g_iface,
145 gpointer iface_data);
146 static void gst_soup_http_src_finalize (GObject * gobject);
147 static void gst_soup_http_src_dispose (GObject * gobject);
149 static void gst_soup_http_src_set_property (GObject * object, guint prop_id,
150 const GValue * value, GParamSpec * pspec);
151 static void gst_soup_http_src_get_property (GObject * object, guint prop_id,
152 GValue * value, GParamSpec * pspec);
154 static GstStateChangeReturn gst_soup_http_src_change_state (GstElement *
155 element, GstStateChange transition);
156 static GstFlowReturn gst_soup_http_src_create (GstPushSrc * psrc,
157 GstBuffer ** outbuf);
158 static gboolean gst_soup_http_src_start (GstBaseSrc * bsrc);
159 static gboolean gst_soup_http_src_stop (GstBaseSrc * bsrc);
160 static gboolean gst_soup_http_src_get_size (GstBaseSrc * bsrc, guint64 * size);
161 static gboolean gst_soup_http_src_is_seekable (GstBaseSrc * bsrc);
162 static gboolean gst_soup_http_src_do_seek (GstBaseSrc * bsrc,
163 GstSegment * segment);
164 static gboolean gst_soup_http_src_query (GstBaseSrc * bsrc, GstQuery * query);
165 static gboolean gst_soup_http_src_unlock (GstBaseSrc * bsrc);
166 static gboolean gst_soup_http_src_unlock_stop (GstBaseSrc * bsrc);
167 static gboolean gst_soup_http_src_set_location (GstSoupHTTPSrc * src,
168 const gchar * uri, GError ** error);
169 static gboolean gst_soup_http_src_set_proxy (GstSoupHTTPSrc * src,
171 static char *gst_soup_http_src_unicodify (const char *str);
172 static gboolean gst_soup_http_src_build_message (GstSoupHTTPSrc * src,
173 const gchar * method);
174 static void gst_soup_http_src_cancel_message (GstSoupHTTPSrc * src);
175 static gboolean gst_soup_http_src_add_range_header (GstSoupHTTPSrc * src,
176 guint64 offset, guint64 stop_offset);
177 static gboolean gst_soup_http_src_session_open (GstSoupHTTPSrc * src);
178 static void gst_soup_http_src_session_close (GstSoupHTTPSrc * src);
179 static void gst_soup_http_src_parse_status (SoupMessage * msg,
180 GstSoupHTTPSrc * src);
181 static void gst_soup_http_src_got_headers (GstSoupHTTPSrc * src,
183 static void gst_soup_http_src_authenticate_cb (SoupSession * session,
184 SoupMessage * msg, SoupAuth * auth, gboolean retrying,
185 GstSoupHTTPSrc * src);
186 static void gst_soup_http_src_destroy_input_stream (GstSoupHTTPSrc * src);
188 #define gst_soup_http_src_parent_class parent_class
189 G_DEFINE_TYPE_WITH_CODE (GstSoupHTTPSrc, gst_soup_http_src, GST_TYPE_PUSH_SRC,
190 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
191 gst_soup_http_src_uri_handler_init));
194 gst_soup_http_src_class_init (GstSoupHTTPSrcClass * klass)
196 GObjectClass *gobject_class;
197 GstElementClass *gstelement_class;
198 GstBaseSrcClass *gstbasesrc_class;
199 GstPushSrcClass *gstpushsrc_class;
201 gobject_class = (GObjectClass *) klass;
202 gstelement_class = (GstElementClass *) klass;
203 gstbasesrc_class = (GstBaseSrcClass *) klass;
204 gstpushsrc_class = (GstPushSrcClass *) klass;
206 gobject_class->set_property = gst_soup_http_src_set_property;
207 gobject_class->get_property = gst_soup_http_src_get_property;
208 gobject_class->finalize = gst_soup_http_src_finalize;
209 gobject_class->dispose = gst_soup_http_src_dispose;
211 g_object_class_install_property (gobject_class,
213 g_param_spec_string ("location", "Location",
214 "Location to read from", "",
215 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
216 g_object_class_install_property (gobject_class,
218 g_param_spec_string ("user-agent", "User-Agent",
219 "Value of the User-Agent HTTP request header field",
220 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
221 g_object_class_install_property (gobject_class,
222 PROP_AUTOMATIC_REDIRECT,
223 g_param_spec_boolean ("automatic-redirect", "automatic-redirect",
224 "Automatically follow HTTP redirects (HTTP Status Code 3xx)",
225 TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
226 g_object_class_install_property (gobject_class,
228 g_param_spec_string ("proxy", "Proxy",
229 "HTTP proxy server URI", "",
230 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
231 g_object_class_install_property (gobject_class,
233 g_param_spec_string ("user-id", "user-id",
234 "HTTP location URI user id for authentication", "",
235 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
236 g_object_class_install_property (gobject_class, PROP_USER_PW,
237 g_param_spec_string ("user-pw", "user-pw",
238 "HTTP location URI user password for authentication", "",
239 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
240 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
241 g_param_spec_string ("proxy-id", "proxy-id",
242 "HTTP proxy URI user id for authentication", "",
243 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
244 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
245 g_param_spec_string ("proxy-pw", "proxy-pw",
246 "HTTP proxy URI user password for authentication", "",
247 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
248 g_object_class_install_property (gobject_class, PROP_COOKIES,
249 g_param_spec_boxed ("cookies", "Cookies", "HTTP request cookies",
250 G_TYPE_STRV, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
251 g_object_class_install_property (gobject_class, PROP_IS_LIVE,
252 g_param_spec_boolean ("is-live", "is-live", "Act like a live source",
253 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
254 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
255 g_param_spec_uint ("timeout", "timeout",
256 "Value in seconds to timeout a blocking I/O (0 = No timeout).", 0,
257 3600, DEFAULT_TIMEOUT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
258 g_object_class_install_property (gobject_class, PROP_EXTRA_HEADERS,
259 g_param_spec_boxed ("extra-headers", "Extra Headers",
260 "Extra headers to append to the HTTP request",
261 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
262 g_object_class_install_property (gobject_class, PROP_IRADIO_MODE,
263 g_param_spec_boolean ("iradio-mode", "iradio-mode",
264 "Enable internet radio mode (ask server to send shoutcast/icecast "
265 "metadata interleaved with the actual stream data)",
266 DEFAULT_IRADIO_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
269 * GstSoupHTTPSrc::http-log-level:
271 * If set and > 0, captures and dumps HTTP session data as
272 * log messages if log level >= GST_LEVEL_TRACE
276 g_object_class_install_property (gobject_class, PROP_SOUP_LOG_LEVEL,
277 g_param_spec_enum ("http-log-level", "HTTP log level",
278 "Set log level for soup's HTTP session log",
279 SOUP_TYPE_LOGGER_LOG_LEVEL, DEFAULT_SOUP_LOG_LEVEL,
280 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
283 * GstSoupHTTPSrc::compress:
285 * If set to %TRUE, souphttpsrc will automatically handle gzip
286 * and deflate Content-Encodings. This does not make much difference
287 * and causes more load for normal media files, but makes a real
288 * difference in size for plaintext files.
292 g_object_class_install_property (gobject_class, PROP_COMPRESS,
293 g_param_spec_boolean ("compress", "Compress",
294 "Allow compressed content encodings",
295 DEFAULT_COMPRESS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
298 * GstSoupHTTPSrc::keep-alive:
300 * If set to %TRUE, souphttpsrc will keep alive connections when being
301 * set to READY state and only will close connections when connecting
302 * to a different server or when going to NULL state..
306 g_object_class_install_property (gobject_class, PROP_KEEP_ALIVE,
307 g_param_spec_boolean ("keep-alive", "keep-alive",
308 "Use HTTP persistent connections", DEFAULT_KEEP_ALIVE,
309 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
312 * GstSoupHTTPSrc::ssl-strict:
314 * If set to %TRUE, souphttpsrc will reject all SSL certificates that
315 * are considered invalid.
319 g_object_class_install_property (gobject_class, PROP_SSL_STRICT,
320 g_param_spec_boolean ("ssl-strict", "SSL Strict",
321 "Strict SSL certificate checking", DEFAULT_SSL_STRICT,
322 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
325 * GstSoupHTTPSrc::ssl-ca-file:
327 * A SSL anchor CA file that should be used for checking certificates
328 * instead of the system CA file.
330 * If this property is non-%NULL, #GstSoupHTTPSrc::ssl-use-system-ca-file
331 * value will be ignored.
333 * Deprecated: Use #GstSoupHTTPSrc::tls-database property instead.
336 g_object_class_install_property (gobject_class, PROP_SSL_CA_FILE,
337 g_param_spec_string ("ssl-ca-file", "SSL CA File",
338 "Location of a SSL anchor CA file to use", DEFAULT_SSL_CA_FILE,
339 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
342 * GstSoupHTTPSrc::ssl-use-system-ca-file:
344 * If set to %TRUE, souphttpsrc will use the system's CA file for
345 * checking certificates, unless #GstSoupHTTPSrc::ssl-ca-file or
346 * #GstSoupHTTPSrc::tls-database are non-%NULL.
350 g_object_class_install_property (gobject_class, PROP_SSL_USE_SYSTEM_CA_FILE,
351 g_param_spec_boolean ("ssl-use-system-ca-file", "Use System CA File",
352 "Use system CA file", DEFAULT_SSL_USE_SYSTEM_CA_FILE,
353 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
356 * GstSoupHTTPSrc::tls-database:
358 * TLS database with anchor certificate authorities used to validate
359 * the server certificate.
361 * If this property is non-%NULL, #GstSoupHTTPSrc::ssl-use-system-ca-file
362 * and #GstSoupHTTPSrc::ssl-ca-file values will be ignored.
366 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
367 g_param_spec_object ("tls-database", "TLS database",
368 "TLS database with anchor certificate authorities used to validate the server certificate",
369 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
372 * GstSoupHTTPSrc::tls-interaction:
374 * A #GTlsInteraction object to be used when the connection or certificate
375 * database need to interact with the user. This will be used to prompt the
376 * user for passwords or certificate where necessary.
380 g_object_class_install_property (gobject_class, PROP_TLS_INTERACTION,
381 g_param_spec_object ("tls-interaction", "TLS interaction",
382 "A GTlsInteraction object to be used when the connection or certificate database need to interact with the user.",
383 G_TYPE_TLS_INTERACTION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
386 * GstSoupHTTPSrc::retries:
388 * Maximum number of retries until giving up.
392 g_object_class_install_property (gobject_class, PROP_RETRIES,
393 g_param_spec_int ("retries", "Retries",
394 "Maximum number of retries until giving up (-1=infinite)", -1,
395 G_MAXINT, DEFAULT_RETRIES,
396 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
399 * GstSoupHTTPSrc::method
401 * The HTTP method to use when making a request
405 g_object_class_install_property (gobject_class, PROP_METHOD,
406 g_param_spec_string ("method", "HTTP method",
407 "The HTTP method to use (GET, HEAD, OPTIONS, etc)",
408 DEFAULT_SOUP_METHOD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
410 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
412 gst_element_class_set_static_metadata (gstelement_class, "HTTP client source",
414 "Receive data as a client over the network via HTTP using SOUP",
415 "Wouter Cloetens <wouter@mind.be>");
416 gstelement_class->change_state =
417 GST_DEBUG_FUNCPTR (gst_soup_http_src_change_state);
419 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_soup_http_src_start);
420 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_soup_http_src_stop);
421 gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_soup_http_src_unlock);
422 gstbasesrc_class->unlock_stop =
423 GST_DEBUG_FUNCPTR (gst_soup_http_src_unlock_stop);
424 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_soup_http_src_get_size);
425 gstbasesrc_class->is_seekable =
426 GST_DEBUG_FUNCPTR (gst_soup_http_src_is_seekable);
427 gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_soup_http_src_do_seek);
428 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_soup_http_src_query);
430 gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_soup_http_src_create);
432 GST_DEBUG_CATEGORY_INIT (souphttpsrc_debug, "souphttpsrc", 0,
437 gst_soup_http_src_reset (GstSoupHTTPSrc * src)
439 src->retry_count = 0;
440 src->have_size = FALSE;
441 src->got_headers = FALSE;
442 src->seekable = FALSE;
443 src->read_position = 0;
444 src->request_position = 0;
445 src->stop_position = -1;
446 src->content_size = 0;
447 src->have_body = FALSE;
449 src->reduce_blocksize_count = 0;
450 src->increase_blocksize_count = 0;
452 src->ret = GST_FLOW_OK;
453 g_cancellable_reset (src->cancellable);
454 gst_soup_http_src_destroy_input_stream (src);
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->poll_context = g_main_context_new ();
474 src->location = NULL;
475 src->redirection_uri = NULL;
476 src->automatic_redirect = TRUE;
477 src->user_agent = g_strdup (DEFAULT_USER_AGENT);
480 src->proxy_id = NULL;
481 src->proxy_pw = NULL;
483 src->iradio_mode = DEFAULT_IRADIO_MODE;
486 src->timeout = DEFAULT_TIMEOUT;
487 src->log_level = DEFAULT_SOUP_LOG_LEVEL;
488 src->ssl_strict = DEFAULT_SSL_STRICT;
489 src->ssl_use_system_ca_file = DEFAULT_SSL_USE_SYSTEM_CA_FILE;
490 src->tls_database = DEFAULT_TLS_DATABASE;
491 src->tls_interaction = DEFAULT_TLS_INTERACTION;
492 src->max_retries = DEFAULT_RETRIES;
493 src->method = DEFAULT_SOUP_METHOD;
494 src->minimum_blocksize = gst_base_src_get_blocksize (GST_BASE_SRC_CAST (src));
495 proxy = g_getenv ("http_proxy");
496 if (!gst_soup_http_src_set_proxy (src, proxy)) {
497 GST_WARNING_OBJECT (src,
498 "The proxy in the http_proxy env var (\"%s\") cannot be parsed.",
502 gst_base_src_set_automatic_eos (GST_BASE_SRC (src), FALSE);
504 gst_soup_http_src_reset (src);
508 gst_soup_http_src_dispose (GObject * gobject)
510 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (gobject);
512 GST_DEBUG_OBJECT (src, "dispose");
514 gst_soup_http_src_session_close (src);
516 G_OBJECT_CLASS (parent_class)->dispose (gobject);
520 gst_soup_http_src_finalize (GObject * gobject)
522 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (gobject);
524 GST_DEBUG_OBJECT (src, "finalize");
526 g_mutex_clear (&src->mutex);
527 g_cond_clear (&src->have_headers_cond);
528 g_object_unref (src->cancellable);
529 g_main_context_unref (src->poll_context);
530 g_free (src->location);
531 g_free (src->redirection_uri);
532 g_free (src->user_agent);
533 if (src->proxy != NULL) {
534 soup_uri_free (src->proxy);
536 g_free (src->user_id);
537 g_free (src->user_pw);
538 g_free (src->proxy_id);
539 g_free (src->proxy_pw);
540 g_strfreev (src->cookies);
542 if (src->extra_headers) {
543 gst_structure_free (src->extra_headers);
544 src->extra_headers = NULL;
547 g_free (src->ssl_ca_file);
549 if (src->tls_database)
550 g_object_unref (src->tls_database);
551 g_free (src->method);
553 if (src->tls_interaction)
554 g_object_unref (src->tls_interaction);
556 G_OBJECT_CLASS (parent_class)->finalize (gobject);
560 gst_soup_http_src_set_property (GObject * object, guint prop_id,
561 const GValue * value, GParamSpec * pspec)
563 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (object);
568 const gchar *location;
570 location = g_value_get_string (value);
572 if (location == NULL) {
573 GST_WARNING ("location property cannot be NULL");
576 if (!gst_soup_http_src_set_location (src, location, NULL)) {
577 GST_WARNING ("badly formatted location");
582 case PROP_USER_AGENT:
583 g_free (src->user_agent);
584 src->user_agent = g_value_dup_string (value);
586 case PROP_IRADIO_MODE:
587 src->iradio_mode = g_value_get_boolean (value);
589 case PROP_AUTOMATIC_REDIRECT:
590 src->automatic_redirect = g_value_get_boolean (value);
596 proxy = g_value_get_string (value);
597 if (!gst_soup_http_src_set_proxy (src, proxy)) {
598 GST_WARNING ("badly formatted proxy URI");
604 g_strfreev (src->cookies);
605 src->cookies = g_strdupv (g_value_get_boxed (value));
608 gst_base_src_set_live (GST_BASE_SRC (src), g_value_get_boolean (value));
611 g_free (src->user_id);
612 src->user_id = g_value_dup_string (value);
615 g_free (src->user_pw);
616 src->user_pw = g_value_dup_string (value);
619 g_free (src->proxy_id);
620 src->proxy_id = g_value_dup_string (value);
623 g_free (src->proxy_pw);
624 src->proxy_pw = g_value_dup_string (value);
627 src->timeout = g_value_get_uint (value);
629 case PROP_EXTRA_HEADERS:{
630 const GstStructure *s = gst_value_get_structure (value);
632 if (src->extra_headers)
633 gst_structure_free (src->extra_headers);
635 src->extra_headers = s ? gst_structure_copy (s) : NULL;
638 case PROP_SOUP_LOG_LEVEL:
639 src->log_level = g_value_get_enum (value);
642 src->compress = g_value_get_boolean (value);
644 case PROP_KEEP_ALIVE:
645 src->keep_alive = g_value_get_boolean (value);
647 case PROP_SSL_STRICT:
648 src->ssl_strict = g_value_get_boolean (value);
650 case PROP_SSL_CA_FILE:
651 g_free (src->ssl_ca_file);
652 src->ssl_ca_file = g_value_dup_string (value);
654 case PROP_SSL_USE_SYSTEM_CA_FILE:
655 src->ssl_use_system_ca_file = g_value_get_boolean (value);
657 case PROP_TLS_DATABASE:
658 g_clear_object (&src->tls_database);
659 src->tls_database = g_value_dup_object (value);
661 case PROP_TLS_INTERACTION:
662 g_clear_object (&src->tls_interaction);
663 src->tls_interaction = g_value_dup_object (value);
666 src->max_retries = g_value_get_int (value);
669 g_free (src->method);
670 src->method = g_value_dup_string (value);
673 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
681 gst_soup_http_src_get_property (GObject * object, guint prop_id,
682 GValue * value, GParamSpec * pspec)
684 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (object);
688 g_value_set_string (value, src->location);
690 case PROP_USER_AGENT:
691 g_value_set_string (value, src->user_agent);
693 case PROP_AUTOMATIC_REDIRECT:
694 g_value_set_boolean (value, src->automatic_redirect);
697 if (src->proxy == NULL)
698 g_value_set_static_string (value, "");
700 char *proxy = soup_uri_to_string (src->proxy, FALSE);
702 g_value_set_string (value, proxy);
707 g_value_set_boxed (value, g_strdupv (src->cookies));
710 g_value_set_boolean (value, gst_base_src_is_live (GST_BASE_SRC (src)));
712 case PROP_IRADIO_MODE:
713 g_value_set_boolean (value, src->iradio_mode);
716 g_value_set_string (value, src->user_id);
719 g_value_set_string (value, src->user_pw);
722 g_value_set_string (value, src->proxy_id);
725 g_value_set_string (value, src->proxy_pw);
728 g_value_set_uint (value, src->timeout);
730 case PROP_EXTRA_HEADERS:
731 gst_value_set_structure (value, src->extra_headers);
733 case PROP_SOUP_LOG_LEVEL:
734 g_value_set_enum (value, src->log_level);
737 g_value_set_boolean (value, src->compress);
739 case PROP_KEEP_ALIVE:
740 g_value_set_boolean (value, src->keep_alive);
742 case PROP_SSL_STRICT:
743 g_value_set_boolean (value, src->ssl_strict);
745 case PROP_SSL_CA_FILE:
746 g_value_set_string (value, src->ssl_ca_file);
748 case PROP_SSL_USE_SYSTEM_CA_FILE:
749 g_value_set_boolean (value, src->ssl_use_system_ca_file);
751 case PROP_TLS_DATABASE:
752 g_value_set_object (value, src->tls_database);
754 case PROP_TLS_INTERACTION:
755 g_value_set_object (value, src->tls_interaction);
758 g_value_set_int (value, src->max_retries);
761 g_value_set_string (value, src->method);
764 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
770 gst_soup_http_src_unicodify (const gchar * str)
772 const gchar *env_vars[] = { "GST_ICY_TAG_ENCODING",
773 "GST_TAG_ENCODING", NULL
776 return gst_tag_freeform_string_to_utf8 (str, -1, env_vars);
780 gst_soup_http_src_destroy_input_stream (GstSoupHTTPSrc * src)
782 if (src->input_stream) {
783 if (src->poll_source) {
784 g_source_destroy (src->poll_source);
785 g_source_unref (src->poll_source);
786 src->poll_source = NULL;
788 g_input_stream_close (src->input_stream, src->cancellable, NULL);
789 g_object_unref (src->input_stream);
790 src->input_stream = NULL;
795 gst_soup_http_src_cancel_message (GstSoupHTTPSrc * src)
797 g_cancellable_cancel (src->cancellable);
798 g_cond_signal (&src->have_headers_cond);
802 gst_soup_http_src_add_range_header (GstSoupHTTPSrc * src, guint64 offset,
808 soup_message_headers_remove (src->msg->request_headers, "Range");
809 if (offset || stop_offset != -1) {
810 if (stop_offset != -1) {
811 g_assert (offset != stop_offset);
813 rc = g_snprintf (buf, sizeof (buf), "bytes=%" G_GUINT64_FORMAT "-%"
814 G_GUINT64_FORMAT, offset, (stop_offset > 0) ? stop_offset - 1 :
817 rc = g_snprintf (buf, sizeof (buf), "bytes=%" G_GUINT64_FORMAT "-",
820 if (rc > sizeof (buf) || rc < 0)
822 soup_message_headers_append (src->msg->request_headers, "Range", buf);
824 src->read_position = offset;
829 _append_extra_header (GQuark field_id, const GValue * value, gpointer user_data)
831 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (user_data);
832 const gchar *field_name = g_quark_to_string (field_id);
833 gchar *field_content = NULL;
835 if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
836 field_content = g_value_dup_string (value);
838 GValue dest = { 0, };
840 g_value_init (&dest, G_TYPE_STRING);
841 if (g_value_transform (value, &dest)) {
842 field_content = g_value_dup_string (&dest);
846 if (field_content == NULL) {
847 GST_ERROR_OBJECT (src, "extra-headers field '%s' contains no value "
848 "or can't be converted to a string", field_name);
852 GST_DEBUG_OBJECT (src, "Appending extra header: \"%s: %s\"", field_name,
854 soup_message_headers_append (src->msg->request_headers, field_name,
857 g_free (field_content);
863 _append_extra_headers (GQuark field_id, const GValue * value,
866 if (G_VALUE_TYPE (value) == GST_TYPE_ARRAY) {
867 guint n = gst_value_array_get_size (value);
870 for (i = 0; i < n; i++) {
871 const GValue *v = gst_value_array_get_value (value, i);
873 if (!_append_extra_header (field_id, v, user_data))
876 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
877 guint n = gst_value_list_get_size (value);
880 for (i = 0; i < n; i++) {
881 const GValue *v = gst_value_list_get_value (value, i);
883 if (!_append_extra_header (field_id, v, user_data))
887 return _append_extra_header (field_id, value, user_data);
895 gst_soup_http_src_add_extra_headers (GstSoupHTTPSrc * src)
897 if (!src->extra_headers)
900 return gst_structure_foreach (src->extra_headers, _append_extra_headers, src);
904 gst_soup_http_src_session_open (GstSoupHTTPSrc * src)
907 GST_DEBUG_OBJECT (src, "Session is already open");
911 if (!src->location) {
912 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (_("No URL set.")),
913 ("Missing location property"));
918 GST_DEBUG_OBJECT (src, "Creating session");
919 if (src->proxy == NULL) {
921 soup_session_new_with_options (SOUP_SESSION_USER_AGENT,
922 src->user_agent, SOUP_SESSION_TIMEOUT, src->timeout,
923 SOUP_SESSION_SSL_STRICT, src->ssl_strict,
924 SOUP_SESSION_TLS_INTERACTION, src->tls_interaction, NULL);
927 soup_session_new_with_options (SOUP_SESSION_PROXY_URI, src->proxy,
928 SOUP_SESSION_TIMEOUT, src->timeout,
929 SOUP_SESSION_SSL_STRICT, src->ssl_strict,
930 SOUP_SESSION_USER_AGENT, src->user_agent,
931 SOUP_SESSION_TLS_INTERACTION, src->tls_interaction, NULL);
935 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
936 (NULL), ("Failed to create async session"));
940 g_signal_connect (src->session, "authenticate",
941 G_CALLBACK (gst_soup_http_src_authenticate_cb), src);
944 gst_soup_util_log_setup (src->session, src->log_level, GST_ELEMENT (src));
945 if (src->tls_database)
946 g_object_set (src->session, "tls-database", src->tls_database, NULL);
947 else if (src->ssl_ca_file)
948 g_object_set (src->session, "ssl-ca-file", src->ssl_ca_file, NULL);
950 g_object_set (src->session, "ssl-use-system-ca-file",
951 src->ssl_use_system_ca_file, NULL);
953 GST_DEBUG_OBJECT (src, "Re-using session");
957 soup_session_add_feature_by_type (src->session, SOUP_TYPE_CONTENT_DECODER);
959 soup_session_remove_feature_by_type (src->session,
960 SOUP_TYPE_CONTENT_DECODER);
966 gst_soup_http_src_session_close (GstSoupHTTPSrc * src)
968 GST_DEBUG_OBJECT (src, "Closing session");
970 g_mutex_lock (&src->mutex);
972 soup_session_abort (src->session); /* This unrefs the message. */
973 g_object_unref (src->session);
977 g_mutex_unlock (&src->mutex);
981 gst_soup_http_src_authenticate_cb (SoupSession * session, SoupMessage * msg,
982 SoupAuth * auth, gboolean retrying, GstSoupHTTPSrc * src)
985 /* First time authentication only, if we fail and are called again with retry true fall through */
986 if (msg->status_code == SOUP_STATUS_UNAUTHORIZED) {
987 if (src->user_id && src->user_pw)
988 soup_auth_authenticate (auth, src->user_id, src->user_pw);
989 } else if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
990 if (src->proxy_id && src->proxy_pw)
991 soup_auth_authenticate (auth, src->proxy_id, src->proxy_pw);
997 insert_http_header (const gchar * name, const gchar * value, gpointer user_data)
999 GstStructure *headers = user_data;
1002 gv = gst_structure_get_value (headers, name);
1003 if (gv && GST_VALUE_HOLDS_ARRAY (gv)) {
1004 GValue v = G_VALUE_INIT;
1006 g_value_init (&v, G_TYPE_STRING);
1007 g_value_set_string (&v, value);
1008 gst_value_array_append_value ((GValue *) gv, &v);
1010 } else if (gv && G_VALUE_HOLDS_STRING (gv)) {
1011 GValue arr = G_VALUE_INIT;
1012 GValue v = G_VALUE_INIT;
1013 const gchar *old_value = g_value_get_string (gv);
1015 g_value_init (&arr, GST_TYPE_ARRAY);
1016 g_value_init (&v, G_TYPE_STRING);
1017 g_value_set_string (&v, old_value);
1018 gst_value_array_append_value (&arr, &v);
1019 g_value_set_string (&v, value);
1020 gst_value_array_append_value (&arr, &v);
1022 gst_structure_set_value (headers, name, &arr);
1024 g_value_unset (&arr);
1026 gst_structure_set (headers, name, G_TYPE_STRING, value, NULL);
1031 gst_soup_http_src_got_headers (GstSoupHTTPSrc * src, SoupMessage * msg)
1034 GstTagList *tag_list;
1035 GstBaseSrc *basesrc;
1037 GHashTable *params = NULL;
1038 GstEvent *http_headers_event;
1039 GstStructure *http_headers, *headers;
1040 const gchar *accept_ranges;
1042 GST_INFO_OBJECT (src, "got headers");
1044 if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED &&
1045 src->proxy_id && src->proxy_pw)
1048 if (src->automatic_redirect && SOUP_STATUS_IS_REDIRECTION (msg->status_code)) {
1049 src->redirection_uri = g_strdup (soup_message_headers_get_one
1050 (msg->response_headers, "Location"));
1051 src->redirection_permanent =
1052 (msg->status_code == SOUP_STATUS_MOVED_PERMANENTLY);
1053 GST_DEBUG_OBJECT (src, "%u redirect to \"%s\" (permanent %d)",
1054 msg->status_code, src->redirection_uri, src->redirection_permanent);
1058 if (msg->status_code == SOUP_STATUS_UNAUTHORIZED) {
1059 /* force an error */
1060 gst_soup_http_src_parse_status (msg, src);
1064 src->got_headers = TRUE;
1065 g_cond_broadcast (&src->have_headers_cond);
1067 http_headers = gst_structure_new_empty ("http-headers");
1068 gst_structure_set (http_headers, "uri", G_TYPE_STRING, src->location, NULL);
1069 if (src->redirection_uri)
1070 gst_structure_set (http_headers, "redirection-uri", G_TYPE_STRING,
1071 src->redirection_uri, NULL);
1072 headers = gst_structure_new_empty ("request-headers");
1073 soup_message_headers_foreach (msg->request_headers, insert_http_header,
1075 gst_structure_set (http_headers, "request-headers", GST_TYPE_STRUCTURE,
1077 gst_structure_free (headers);
1078 headers = gst_structure_new_empty ("response-headers");
1079 soup_message_headers_foreach (msg->response_headers, insert_http_header,
1081 gst_structure_set (http_headers, "response-headers", GST_TYPE_STRUCTURE,
1083 gst_structure_free (headers);
1085 http_headers_event =
1086 gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_STICKY, http_headers);
1087 gst_event_replace (&src->http_headers_event, http_headers_event);
1088 gst_event_unref (http_headers_event);
1090 /* Parse Content-Length. */
1091 if (soup_message_headers_get_encoding (msg->response_headers) ==
1092 SOUP_ENCODING_CONTENT_LENGTH) {
1093 newsize = src->request_position +
1094 soup_message_headers_get_content_length (msg->response_headers);
1095 if (!src->have_size || (src->content_size != newsize)) {
1096 src->content_size = newsize;
1097 src->have_size = TRUE;
1098 src->seekable = TRUE;
1099 GST_DEBUG_OBJECT (src, "size = %" G_GUINT64_FORMAT, src->content_size);
1101 basesrc = GST_BASE_SRC_CAST (src);
1102 basesrc->segment.duration = src->content_size;
1103 gst_element_post_message (GST_ELEMENT (src),
1104 gst_message_new_duration_changed (GST_OBJECT (src)));
1108 /* If the server reports Accept-Ranges: none we don't have to try
1109 * doing range requests at all
1111 if ((accept_ranges =
1112 soup_message_headers_get_one (msg->response_headers,
1113 "Accept-Ranges"))) {
1114 if (g_ascii_strcasecmp (accept_ranges, "none") == 0)
1115 src->seekable = FALSE;
1119 tag_list = gst_tag_list_new_empty ();
1122 soup_message_headers_get_one (msg->response_headers,
1123 "icy-metaint")) != NULL) {
1124 gint icy_metaint = atoi (value);
1126 GST_DEBUG_OBJECT (src, "icy-metaint: %s (parsed: %d)", value, icy_metaint);
1127 if (icy_metaint > 0) {
1129 gst_caps_unref (src->src_caps);
1131 src->src_caps = gst_caps_new_simple ("application/x-icy",
1132 "metadata-interval", G_TYPE_INT, icy_metaint, NULL);
1134 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
1138 soup_message_headers_get_content_type (msg->response_headers,
1139 ¶ms)) != NULL) {
1140 GST_DEBUG_OBJECT (src, "Content-Type: %s", value);
1141 if (g_ascii_strcasecmp (value, "audio/L16") == 0) {
1147 gst_caps_unref (src->src_caps);
1149 param = g_hash_table_lookup (params, "channels");
1151 channels = atol (param);
1153 param = g_hash_table_lookup (params, "rate");
1155 rate = atol (param);
1157 src->src_caps = gst_caps_new_simple ("audio/x-unaligned-raw",
1158 "format", G_TYPE_STRING, "S16BE",
1159 "layout", G_TYPE_STRING, "interleaved",
1160 "channels", G_TYPE_INT, channels, "rate", G_TYPE_INT, rate, NULL);
1162 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
1164 /* Set the Content-Type field on the caps */
1165 if (src->src_caps) {
1166 src->src_caps = gst_caps_make_writable (src->src_caps);
1167 gst_caps_set_simple (src->src_caps, "content-type", G_TYPE_STRING,
1169 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
1175 g_hash_table_destroy (params);
1178 soup_message_headers_get_one (msg->response_headers,
1179 "icy-name")) != NULL) {
1180 g_free (src->iradio_name);
1181 src->iradio_name = gst_soup_http_src_unicodify (value);
1182 if (src->iradio_name) {
1183 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_ORGANIZATION,
1184 src->iradio_name, NULL);
1188 soup_message_headers_get_one (msg->response_headers,
1189 "icy-genre")) != NULL) {
1190 g_free (src->iradio_genre);
1191 src->iradio_genre = gst_soup_http_src_unicodify (value);
1192 if (src->iradio_genre) {
1193 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_GENRE,
1194 src->iradio_genre, NULL);
1197 if ((value = soup_message_headers_get_one (msg->response_headers, "icy-url"))
1199 g_free (src->iradio_url);
1200 src->iradio_url = gst_soup_http_src_unicodify (value);
1201 if (src->iradio_url) {
1202 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_LOCATION,
1203 src->iradio_url, NULL);
1206 if (!gst_tag_list_is_empty (tag_list)) {
1207 GST_DEBUG_OBJECT (src,
1208 "calling gst_element_found_tags with %" GST_PTR_FORMAT, tag_list);
1209 gst_pad_push_event (GST_BASE_SRC_PAD (src), gst_event_new_tag (tag_list));
1211 gst_tag_list_unref (tag_list);
1214 /* Handle HTTP errors. */
1215 gst_soup_http_src_parse_status (msg, src);
1217 /* Check if Range header was respected. */
1218 if (src->ret == GST_FLOW_CUSTOM_ERROR &&
1219 src->read_position && msg->status_code != SOUP_STATUS_PARTIAL_CONTENT) {
1220 src->seekable = FALSE;
1221 GST_ELEMENT_ERROR (src, RESOURCE, SEEK,
1222 (_("Server does not support seeking.")),
1223 ("Server does not accept Range HTTP header, URL: %s, Redirect to: %s",
1224 src->location, GST_STR_NULL (src->redirection_uri)));
1225 src->ret = GST_FLOW_ERROR;
1228 /* If we are going to error out, stop all processing right here, so we
1229 * don't output any data (such as an error html page), and return
1230 * GST_FLOW_ERROR from the create function instead of having
1231 * got_chunk_cb overwrite src->ret with FLOW_OK again. */
1232 if (src->ret == GST_FLOW_ERROR || src->ret == GST_FLOW_EOS) {
1237 gst_soup_http_src_alloc_buffer (GstSoupHTTPSrc * src)
1239 GstBaseSrc *basesrc = GST_BASE_SRC_CAST (src);
1243 rc = GST_BASE_SRC_CLASS (parent_class)->alloc (basesrc, -1,
1244 basesrc->blocksize, &gstbuf);
1245 if (G_UNLIKELY (rc != GST_FLOW_OK)) {
1252 #define SOUP_HTTP_SRC_ERROR(src,soup_msg,cat,code,error_message) \
1253 GST_ELEMENT_ERROR ((src), cat, code, ("%s", error_message), \
1254 ("%s (%d), URL: %s, Redirect to: %s", (soup_msg)->reason_phrase, \
1255 (soup_msg)->status_code, (src)->location, GST_STR_NULL ((src)->redirection_uri)));
1258 gst_soup_http_src_parse_status (SoupMessage * msg, GstSoupHTTPSrc * src)
1260 if (msg->method == SOUP_METHOD_HEAD) {
1261 if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code))
1262 GST_DEBUG_OBJECT (src, "Ignoring error %d during HEAD request",
1264 } else if (SOUP_STATUS_IS_TRANSPORT_ERROR (msg->status_code)) {
1265 switch (msg->status_code) {
1266 case SOUP_STATUS_CANT_RESOLVE:
1267 case SOUP_STATUS_CANT_RESOLVE_PROXY:
1268 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, NOT_FOUND,
1269 _("Could not resolve server name."));
1270 src->ret = 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 src->ret = GST_FLOW_ERROR;
1278 case SOUP_STATUS_SSL_FAILED:
1279 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ,
1280 _("Secure connection setup failed."));
1281 src->ret = GST_FLOW_ERROR;
1283 case SOUP_STATUS_IO_ERROR:
1284 if (src->max_retries == -1 || src->retry_count < src->max_retries) {
1285 src->ret = GST_FLOW_CUSTOM_ERROR;
1287 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, READ,
1288 _("A network error occurred, or the server closed the connection "
1290 src->ret = GST_FLOW_ERROR;
1293 case SOUP_STATUS_MALFORMED:
1294 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, READ,
1295 _("Server sent bad data."));
1296 src->ret = GST_FLOW_ERROR;
1298 case SOUP_STATUS_CANCELLED:
1299 /* No error message when interrupted by program. */
1302 } else if (SOUP_STATUS_IS_CLIENT_ERROR (msg->status_code) ||
1303 SOUP_STATUS_IS_REDIRECTION (msg->status_code) ||
1304 SOUP_STATUS_IS_SERVER_ERROR (msg->status_code)) {
1305 /* Report HTTP error. */
1307 /* when content_size is unknown and we have just finished receiving
1308 * a body message, requests that go beyond the content limits will result
1309 * in an error. Here we convert those to EOS */
1310 if (msg->status_code == SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE &&
1311 src->have_body && !src->have_size) {
1312 GST_DEBUG_OBJECT (src, "Requested range out of limits and received full "
1313 "body, returning EOS");
1314 src->ret = GST_FLOW_EOS;
1318 /* FIXME: reason_phrase is not translated and not suitable for user
1319 * error dialog according to libsoup documentation.
1321 if (msg->status_code == SOUP_STATUS_NOT_FOUND) {
1322 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
1323 ("%s", msg->reason_phrase),
1324 ("%s (%d), URL: %s, Redirect to: %s", msg->reason_phrase,
1325 msg->status_code, src->location,
1326 GST_STR_NULL (src->redirection_uri)));
1327 } else if (msg->status_code == SOUP_STATUS_UNAUTHORIZED
1328 || msg->status_code == SOUP_STATUS_PAYMENT_REQUIRED
1329 || msg->status_code == SOUP_STATUS_FORBIDDEN
1330 || msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
1331 GST_ELEMENT_ERROR (src, RESOURCE, NOT_AUTHORIZED, ("%s",
1332 msg->reason_phrase), ("%s (%d), URL: %s, Redirect to: %s",
1333 msg->reason_phrase, msg->status_code, src->location,
1334 GST_STR_NULL (src->redirection_uri)));
1336 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1337 ("%s", msg->reason_phrase),
1338 ("%s (%d), URL: %s, Redirect to: %s", msg->reason_phrase,
1339 msg->status_code, src->location,
1340 GST_STR_NULL (src->redirection_uri)));
1342 src->ret = GST_FLOW_ERROR;
1347 gst_soup_http_src_build_message (GstSoupHTTPSrc * src, const gchar * method)
1349 g_return_val_if_fail (src->msg == NULL, FALSE);
1351 src->msg = soup_message_new (method, src->location);
1353 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1354 ("Error parsing URL."), ("URL: %s", src->location));
1357 if (!src->keep_alive) {
1358 soup_message_headers_append (src->msg->request_headers, "Connection",
1361 if (src->iradio_mode) {
1362 soup_message_headers_append (src->msg->request_headers, "icy-metadata",
1368 for (cookie = src->cookies; *cookie != NULL; cookie++) {
1369 soup_message_headers_append (src->msg->request_headers, "Cookie",
1374 soup_message_set_flags (src->msg, SOUP_MESSAGE_OVERWRITE_CHUNKS |
1375 (src->automatic_redirect ? 0 : SOUP_MESSAGE_NO_REDIRECT));
1376 gst_soup_http_src_add_range_header (src, src->request_position,
1377 src->stop_position);
1379 gst_soup_http_src_add_extra_headers (src);
1385 gst_soup_http_src_check_input_stream_interfaces (GstSoupHTTPSrc * src)
1387 if (!src->input_stream)
1390 src->has_pollable_interface = G_IS_POLLABLE_INPUT_STREAM (src->input_stream)
1391 && g_pollable_input_stream_can_poll ((GPollableInputStream *)
1395 static GstFlowReturn
1396 gst_soup_http_src_send_message (GstSoupHTTPSrc * src)
1398 g_return_val_if_fail (src->msg != NULL, GST_FLOW_ERROR);
1400 g_assert (src->input_stream == NULL);
1401 g_assert (src->poll_source == NULL);
1403 /* FIXME We are ignoring the GError here, might be useful to debug */
1405 soup_session_send (src->session, src->msg, src->cancellable, NULL);
1407 if (g_cancellable_is_cancelled (src->cancellable))
1408 return GST_FLOW_FLUSHING;
1410 gst_soup_http_src_got_headers (src, src->msg);
1411 if (src->ret != GST_FLOW_OK) {
1415 if (!src->input_stream) {
1416 GST_DEBUG_OBJECT (src, "Didn't get an input stream");
1417 return GST_FLOW_ERROR;
1420 if (SOUP_STATUS_IS_SUCCESSFUL (src->msg->status_code)) {
1421 GST_DEBUG_OBJECT (src, "Successfully got a reply");
1423 /* FIXME - be more helpful to people debugging */
1424 return GST_FLOW_ERROR;
1427 gst_soup_http_src_check_input_stream_interfaces (src);
1432 static GstFlowReturn
1433 gst_soup_http_src_do_request (GstSoupHTTPSrc * src, const gchar * method)
1435 if (src->max_retries != -1 && src->retry_count > src->max_retries) {
1436 GST_DEBUG_OBJECT (src, "Max retries reached");
1437 src->ret = GST_FLOW_ERROR;
1442 /* EOS immediately if we have an empty segment */
1443 if (src->request_position == src->stop_position)
1444 return GST_FLOW_EOS;
1446 GST_LOG_OBJECT (src, "Running request for method: %s", method);
1448 /* Update the position if we are retrying */
1449 if (src->msg && (src->request_position != src->read_position)) {
1450 gst_soup_http_src_add_range_header (src, src->request_position,
1451 src->stop_position);
1455 if (!gst_soup_http_src_build_message (src, method)) {
1456 return GST_FLOW_ERROR;
1460 if (g_cancellable_is_cancelled (src->cancellable)) {
1461 GST_INFO_OBJECT (src, "interrupted");
1462 src->ret = GST_FLOW_FLUSHING;
1465 src->ret = gst_soup_http_src_send_message (src);
1472 * Check if the bytes_read is above a certain threshold of the blocksize, if
1473 * that happens a few times in a row, increase the blocksize; Do the same in
1474 * the opposite direction to reduce the blocksize.
1477 gst_soup_http_src_check_update_blocksize (GstSoupHTTPSrc * src,
1480 guint blocksize = gst_base_src_get_blocksize (GST_BASE_SRC_CAST (src));
1482 GST_LOG_OBJECT (src, "Checking to update blocksize. Read:%" G_GINT64_FORMAT
1483 " blocksize:%u", bytes_read, blocksize);
1485 if (bytes_read >= blocksize * GROW_BLOCKSIZE_LIMIT) {
1486 src->reduce_blocksize_count = 0;
1487 src->increase_blocksize_count++;
1489 if (src->increase_blocksize_count >= GROW_BLOCKSIZE_COUNT) {
1490 blocksize *= GROW_BLOCKSIZE_FACTOR;
1491 GST_DEBUG_OBJECT (src, "Increased blocksize to %u", blocksize);
1492 gst_base_src_set_blocksize (GST_BASE_SRC_CAST (src), blocksize);
1493 src->increase_blocksize_count = 0;
1495 } else if (bytes_read < blocksize * REDUCE_BLOCKSIZE_LIMIT) {
1496 src->reduce_blocksize_count++;
1497 src->increase_blocksize_count = 0;
1499 if (src->reduce_blocksize_count >= REDUCE_BLOCKSIZE_COUNT) {
1500 blocksize *= REDUCE_BLOCKSIZE_FACTOR;
1501 blocksize = MAX (blocksize, src->minimum_blocksize);
1502 GST_DEBUG_OBJECT (src, "Decreased blocksize to %u", blocksize);
1503 gst_base_src_set_blocksize (GST_BASE_SRC_CAST (src), blocksize);
1504 src->reduce_blocksize_count = 0;
1507 src->reduce_blocksize_count = src->increase_blocksize_count = 0;
1512 gst_soup_http_src_update_position (GstSoupHTTPSrc * src, gint64 bytes_read)
1514 GstBaseSrc *basesrc = GST_BASE_SRC_CAST (src);
1515 guint64 new_position;
1517 new_position = src->read_position + bytes_read;
1518 if (G_LIKELY (src->request_position == src->read_position))
1519 src->request_position = new_position;
1520 src->read_position = new_position;
1522 if (src->have_size) {
1523 if (new_position > src->content_size) {
1524 GST_DEBUG_OBJECT (src, "Got position previous estimated content size "
1525 "(%" G_GINT64_FORMAT " > %" G_GINT64_FORMAT ")", new_position,
1527 src->content_size = new_position;
1528 basesrc->segment.duration = src->content_size;
1529 gst_element_post_message (GST_ELEMENT (src),
1530 gst_message_new_duration_changed (GST_OBJECT (src)));
1531 } else if (new_position == src->content_size) {
1532 GST_DEBUG_OBJECT (src, "We're EOS now");
1538 _gst_soup_http_src_data_available_callback (GObject * pollable_stream,
1541 GstSoupHTTPSrc *src = udata;
1543 src->have_data = TRUE;
1547 /* Need to wait on a gsource to know when data is available */
1549 gst_soup_http_src_wait_for_data (GstSoupHTTPSrc * src)
1551 src->have_data = FALSE;
1553 if (!src->poll_source) {
1555 g_pollable_input_stream_create_source ((GPollableInputStream *)
1556 src->input_stream, src->cancellable);
1557 g_source_set_callback (src->poll_source,
1558 (GSourceFunc) _gst_soup_http_src_data_available_callback, src, NULL);
1559 g_source_attach (src->poll_source, src->poll_context);
1562 while (!src->have_data && !g_cancellable_is_cancelled (src->cancellable)) {
1563 g_main_context_iteration (src->poll_context, TRUE);
1566 return src->have_data;
1569 static GstFlowReturn
1570 gst_soup_http_src_read_buffer (GstSoupHTTPSrc * src, GstBuffer ** outbuf)
1578 bsrc = GST_BASE_SRC_CAST (src);
1580 *outbuf = gst_soup_http_src_alloc_buffer (src);
1582 GST_WARNING_OBJECT (src, "Failed to allocate buffer");
1583 return GST_FLOW_ERROR;
1586 if (!gst_buffer_map (*outbuf, &mapinfo, GST_MAP_WRITE)) {
1587 GST_WARNING_OBJECT (src, "Failed to map buffer");
1588 return GST_FLOW_ERROR;
1591 if (src->has_pollable_interface) {
1594 g_pollable_input_stream_read_nonblocking ((GPollableInputStream *)
1595 src->input_stream, mapinfo.data, mapinfo.size, src->cancellable,
1597 if (read_bytes == -1) {
1598 if (err && g_error_matches (err, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) {
1602 /* no data yet, wait */
1603 if (gst_soup_http_src_wait_for_data (src))
1612 g_input_stream_read (src->input_stream, mapinfo.data, mapinfo.size,
1613 src->cancellable, NULL);
1619 GST_DEBUG_OBJECT (src, "Read %" G_GSSIZE_FORMAT " bytes from http input",
1622 g_mutex_lock (&src->mutex);
1623 if (g_cancellable_is_cancelled (src->cancellable)) {
1624 gst_buffer_unmap (*outbuf, &mapinfo);
1625 gst_buffer_unref (*outbuf);
1626 g_mutex_unlock (&src->mutex);
1627 return GST_FLOW_FLUSHING;
1629 g_mutex_unlock (&src->mutex);
1631 gst_buffer_unmap (*outbuf, &mapinfo);
1632 if (read_bytes > 0) {
1633 gst_buffer_set_size (*outbuf, read_bytes);
1634 GST_BUFFER_OFFSET (*outbuf) = bsrc->segment.position;
1636 gst_soup_http_src_update_position (src, read_bytes);
1638 /* Got some data, reset retry counter */
1639 src->retry_count = 0;
1641 gst_soup_http_src_check_update_blocksize (src, read_bytes);
1643 gst_buffer_unref (*outbuf);
1644 if (read_bytes < 0) {
1645 /* Maybe the server disconnected, retry */
1646 ret = GST_FLOW_CUSTOM_ERROR;
1649 src->have_body = TRUE;
1655 static GstFlowReturn
1656 gst_soup_http_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
1658 GstSoupHTTPSrc *src;
1659 GstFlowReturn ret = GST_FLOW_OK;
1660 GstEvent *http_headers_event = NULL;
1662 src = GST_SOUP_HTTP_SRC (psrc);
1665 g_mutex_lock (&src->mutex);
1667 /* Check for pending position change */
1668 if (src->request_position != src->read_position) {
1669 gst_soup_http_src_destroy_input_stream (src);
1672 if (g_cancellable_is_cancelled (src->cancellable)) {
1673 ret = GST_FLOW_FLUSHING;
1674 g_mutex_unlock (&src->mutex);
1678 /* If we have no open connection to the server, start one */
1679 if (!src->input_stream) {
1682 gst_soup_http_src_do_request (src,
1683 src->method ? src->method : SOUP_METHOD_GET);
1684 http_headers_event = src->http_headers_event;
1685 src->http_headers_event = NULL;
1687 g_mutex_unlock (&src->mutex);
1689 if (ret == GST_FLOW_OK || ret == GST_FLOW_CUSTOM_ERROR) {
1690 if (http_headers_event) {
1691 gst_pad_push_event (GST_BASE_SRC_PAD (src), http_headers_event);
1692 http_headers_event = NULL;
1696 if (ret == GST_FLOW_OK)
1697 ret = gst_soup_http_src_read_buffer (src, outbuf);
1700 GST_DEBUG_OBJECT (src, "Returning %d %s", ret, gst_flow_get_name (ret));
1701 if (ret != GST_FLOW_OK) {
1702 if (http_headers_event)
1703 gst_event_unref (http_headers_event);
1705 g_mutex_lock (&src->mutex);
1706 gst_soup_http_src_destroy_input_stream (src);
1707 g_mutex_unlock (&src->mutex);
1708 if (ret == GST_FLOW_CUSTOM_ERROR)
1715 gst_soup_http_src_start (GstBaseSrc * bsrc)
1717 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1719 GST_DEBUG_OBJECT (src, "start(\"%s\")", src->location);
1721 return gst_soup_http_src_session_open (src);
1725 gst_soup_http_src_stop (GstBaseSrc * bsrc)
1727 GstSoupHTTPSrc *src;
1729 src = GST_SOUP_HTTP_SRC (bsrc);
1730 GST_DEBUG_OBJECT (src, "stop()");
1731 if (src->keep_alive && !src->msg)
1732 gst_soup_http_src_cancel_message (src);
1734 gst_soup_http_src_session_close (src);
1736 gst_soup_http_src_reset (src);
1740 static GstStateChangeReturn
1741 gst_soup_http_src_change_state (GstElement * element, GstStateChange transition)
1743 GstStateChangeReturn ret;
1744 GstSoupHTTPSrc *src;
1746 src = GST_SOUP_HTTP_SRC (element);
1748 switch (transition) {
1749 case GST_STATE_CHANGE_READY_TO_NULL:
1750 gst_soup_http_src_session_close (src);
1756 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1761 /* Interrupt a blocking request. */
1763 gst_soup_http_src_unlock (GstBaseSrc * bsrc)
1765 GstSoupHTTPSrc *src;
1767 src = GST_SOUP_HTTP_SRC (bsrc);
1768 GST_DEBUG_OBJECT (src, "unlock()");
1770 src->ret = GST_FLOW_FLUSHING;
1771 gst_soup_http_src_cancel_message (src);
1775 /* Interrupt interrupt. */
1777 gst_soup_http_src_unlock_stop (GstBaseSrc * bsrc)
1779 GstSoupHTTPSrc *src;
1781 src = GST_SOUP_HTTP_SRC (bsrc);
1782 GST_DEBUG_OBJECT (src, "unlock_stop()");
1784 src->ret = GST_FLOW_OK;
1785 g_cancellable_reset (src->cancellable);
1790 gst_soup_http_src_get_size (GstBaseSrc * bsrc, guint64 * size)
1792 GstSoupHTTPSrc *src;
1794 src = GST_SOUP_HTTP_SRC (bsrc);
1796 if (src->have_size) {
1797 GST_DEBUG_OBJECT (src, "get_size() = %" G_GUINT64_FORMAT,
1799 *size = src->content_size;
1802 GST_DEBUG_OBJECT (src, "get_size() = FALSE");
1807 gst_soup_http_src_check_seekable (GstSoupHTTPSrc * src)
1809 GstFlowReturn ret = GST_FLOW_OK;
1811 /* Special case to check if the server allows range requests
1812 * before really starting to get data in the buffer creation
1815 if (!src->got_headers && GST_STATE (src) >= GST_STATE_PAUSED) {
1816 g_mutex_lock (&src->mutex);
1817 while (!src->got_headers && !g_cancellable_is_cancelled (src->cancellable)
1818 && ret == GST_FLOW_OK) {
1819 if ((src->msg && src->msg->method != SOUP_METHOD_HEAD)) {
1820 /* wait for the current request to finish */
1821 g_cond_wait (&src->have_headers_cond, &src->mutex);
1823 if (gst_soup_http_src_session_open (src)) {
1824 ret = gst_soup_http_src_do_request (src, SOUP_METHOD_HEAD);
1828 if (src->ret == GST_FLOW_EOS) {
1829 /* A HEAD request shouldn't lead to EOS */
1830 src->ret = GST_FLOW_OK;
1832 g_mutex_unlock (&src->mutex);
1837 gst_soup_http_src_is_seekable (GstBaseSrc * bsrc)
1839 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1841 gst_soup_http_src_check_seekable (src);
1843 return src->seekable;
1847 gst_soup_http_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
1849 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1851 GST_DEBUG_OBJECT (src, "do_seek(%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1852 ")", segment->start, segment->stop);
1853 if (src->read_position == segment->start &&
1854 src->request_position == src->read_position &&
1855 src->stop_position == segment->stop) {
1856 GST_DEBUG_OBJECT (src,
1857 "Seek to current read/end position and no seek pending");
1861 gst_soup_http_src_check_seekable (src);
1863 /* If we have no headers we don't know yet if it is seekable or not.
1864 * Store the start position and error out later if it isn't */
1865 if (src->got_headers && !src->seekable) {
1866 GST_WARNING_OBJECT (src, "Not seekable");
1870 if (segment->rate < 0.0 || segment->format != GST_FORMAT_BYTES) {
1871 GST_WARNING_OBJECT (src, "Invalid seek segment");
1875 if (src->have_size && segment->start >= src->content_size) {
1876 GST_WARNING_OBJECT (src,
1877 "Potentially seeking behind end of file, might EOS immediately");
1880 /* Wait for create() to handle the jump in offset. */
1881 src->request_position = segment->start;
1882 src->stop_position = segment->stop;
1888 gst_soup_http_src_query (GstBaseSrc * bsrc, GstQuery * query)
1890 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1892 GstSchedulingFlags flags;
1893 gint minsize, maxsize, align;
1895 switch (GST_QUERY_TYPE (query)) {
1897 gst_query_set_uri (query, src->location);
1898 if (src->redirection_uri != NULL) {
1899 gst_query_set_uri_redirection (query, src->redirection_uri);
1900 gst_query_set_uri_redirection_permanent (query,
1901 src->redirection_permanent);
1911 ret = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
1913 switch (GST_QUERY_TYPE (query)) {
1914 case GST_QUERY_SCHEDULING:
1915 gst_query_parse_scheduling (query, &flags, &minsize, &maxsize, &align);
1916 flags |= GST_SCHEDULING_FLAG_BANDWIDTH_LIMITED;
1917 gst_query_set_scheduling (query, flags, minsize, maxsize, align);
1927 gst_soup_http_src_set_location (GstSoupHTTPSrc * src, const gchar * uri,
1930 const char *alt_schemes[] = { "icy://", "icyx://" };
1933 if (src->location) {
1934 g_free (src->location);
1935 src->location = NULL;
1941 for (i = 0; i < G_N_ELEMENTS (alt_schemes); i++) {
1942 if (g_str_has_prefix (uri, alt_schemes[i])) {
1944 g_strdup_printf ("http://%s", uri + strlen (alt_schemes[i]));
1949 if (src->redirection_uri) {
1950 g_free (src->redirection_uri);
1951 src->redirection_uri = NULL;
1954 src->location = g_strdup (uri);
1960 gst_soup_http_src_set_proxy (GstSoupHTTPSrc * src, const gchar * uri)
1963 soup_uri_free (src->proxy);
1967 if (uri == NULL || *uri == '\0')
1970 if (g_str_has_prefix (uri, "http://")) {
1971 src->proxy = soup_uri_new (uri);
1973 gchar *new_uri = g_strconcat ("http://", uri, NULL);
1975 src->proxy = soup_uri_new (new_uri);
1979 return (src->proxy != NULL);
1983 gst_soup_http_src_uri_get_type (GType type)
1988 static const gchar *const *
1989 gst_soup_http_src_uri_get_protocols (GType type)
1991 static const gchar *protocols[] = { "http", "https", "icy", "icyx", NULL };
1997 gst_soup_http_src_uri_get_uri (GstURIHandler * handler)
1999 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (handler);
2001 /* FIXME: make thread-safe */
2002 return g_strdup (src->location);
2006 gst_soup_http_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
2009 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (handler);
2011 return gst_soup_http_src_set_location (src, uri, error);
2015 gst_soup_http_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
2017 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
2019 iface->get_type = gst_soup_http_src_uri_get_type;
2020 iface->get_protocols = gst_soup_http_src_uri_get_protocols;
2021 iface->get_uri = gst_soup_http_src_uri_get_uri;
2022 iface->set_uri = gst_soup_http_src_uri_set_uri;