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 /* libsoup before 2.47.0 was stealing our main context from us,
85 * so we can't reliable use it to clean up all pending resources
86 * once we're done... let's just continue leaking on old versions.
87 * https://bugzilla.gnome.org/show_bug.cgi?id=663944
89 #if defined(SOUP_MINOR_VERSION) && SOUP_MINOR_VERSION >= 47
90 #define LIBSOUP_DOES_NOT_STEAL_OUR_CONTEXT 1
93 #include <gst/tag/tag.h>
95 GST_DEBUG_CATEGORY_STATIC (souphttpsrc_debug);
96 #define GST_CAT_DEFAULT souphttpsrc_debug
98 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
101 GST_STATIC_CAPS_ANY);
109 PROP_AUTOMATIC_REDIRECT,
124 PROP_SSL_USE_SYSTEM_CA_FILE,
128 #define DEFAULT_USER_AGENT "GStreamer souphttpsrc "
129 #define DEFAULT_IRADIO_MODE TRUE
130 #define DEFAULT_SOUP_LOG_LEVEL SOUP_LOGGER_LOG_HEADERS
131 #define DEFAULT_COMPRESS FALSE
132 #define DEFAULT_KEEP_ALIVE FALSE
133 #define DEFAULT_SSL_STRICT TRUE
134 #define DEFAULT_SSL_CA_FILE NULL
135 #define DEFAULT_SSL_USE_SYSTEM_CA_FILE TRUE
136 #define DEFAULT_TIMEOUT 15
137 #define DEFAULT_RETRIES 3
139 static void gst_soup_http_src_uri_handler_init (gpointer g_iface,
140 gpointer iface_data);
141 static void gst_soup_http_src_finalize (GObject * gobject);
142 static void gst_soup_http_src_dispose (GObject * gobject);
144 static void gst_soup_http_src_set_property (GObject * object, guint prop_id,
145 const GValue * value, GParamSpec * pspec);
146 static void gst_soup_http_src_get_property (GObject * object, guint prop_id,
147 GValue * value, GParamSpec * pspec);
149 static GstStateChangeReturn gst_soup_http_src_change_state (GstElement *
150 element, GstStateChange transition);
151 static GstFlowReturn gst_soup_http_src_create (GstPushSrc * psrc,
152 GstBuffer ** outbuf);
153 static gboolean gst_soup_http_src_start (GstBaseSrc * bsrc);
154 static gboolean gst_soup_http_src_stop (GstBaseSrc * bsrc);
155 static gboolean gst_soup_http_src_get_size (GstBaseSrc * bsrc, guint64 * size);
156 static gboolean gst_soup_http_src_is_seekable (GstBaseSrc * bsrc);
157 static gboolean gst_soup_http_src_do_seek (GstBaseSrc * bsrc,
158 GstSegment * segment);
159 static gboolean gst_soup_http_src_query (GstBaseSrc * bsrc, GstQuery * query);
160 static gboolean gst_soup_http_src_unlock (GstBaseSrc * bsrc);
161 static gboolean gst_soup_http_src_unlock_stop (GstBaseSrc * bsrc);
162 static gboolean gst_soup_http_src_set_location (GstSoupHTTPSrc * src,
163 const gchar * uri, GError ** error);
164 static gboolean gst_soup_http_src_set_proxy (GstSoupHTTPSrc * src,
166 static char *gst_soup_http_src_unicodify (const char *str);
167 static gboolean gst_soup_http_src_build_message (GstSoupHTTPSrc * src,
168 const gchar * method);
169 static void gst_soup_http_src_cancel_message (GstSoupHTTPSrc * src);
170 static void gst_soup_http_src_queue_message (GstSoupHTTPSrc * src);
171 static gboolean gst_soup_http_src_add_range_header (GstSoupHTTPSrc * src,
172 guint64 offset, guint64 stop_offset);
173 static void gst_soup_http_src_session_unpause_message (GstSoupHTTPSrc * src);
174 static void gst_soup_http_src_session_pause_message (GstSoupHTTPSrc * src);
175 static gboolean gst_soup_http_src_session_open (GstSoupHTTPSrc * src);
176 static void gst_soup_http_src_session_close (GstSoupHTTPSrc * src);
177 static void gst_soup_http_src_parse_status (SoupMessage * msg,
178 GstSoupHTTPSrc * src);
179 static void gst_soup_http_src_chunk_free (gpointer gstbuf);
180 static SoupBuffer *gst_soup_http_src_chunk_allocator (SoupMessage * msg,
181 gsize max_len, gpointer user_data);
182 static void gst_soup_http_src_got_chunk_cb (SoupMessage * msg,
183 SoupBuffer * chunk, GstSoupHTTPSrc * src);
184 static void gst_soup_http_src_response_cb (SoupSession * session,
185 SoupMessage * msg, GstSoupHTTPSrc * src);
186 static void gst_soup_http_src_got_headers_cb (SoupMessage * msg,
187 GstSoupHTTPSrc * src);
188 static void gst_soup_http_src_got_body_cb (SoupMessage * msg,
189 GstSoupHTTPSrc * src);
190 static void gst_soup_http_src_finished_cb (SoupMessage * msg,
191 GstSoupHTTPSrc * src);
192 static void gst_soup_http_src_authenticate_cb (SoupSession * session,
193 SoupMessage * msg, SoupAuth * auth, gboolean retrying,
194 GstSoupHTTPSrc * src);
196 #define gst_soup_http_src_parent_class parent_class
197 G_DEFINE_TYPE_WITH_CODE (GstSoupHTTPSrc, gst_soup_http_src, GST_TYPE_PUSH_SRC,
198 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
199 gst_soup_http_src_uri_handler_init));
202 gst_soup_http_src_class_init (GstSoupHTTPSrcClass * klass)
204 GObjectClass *gobject_class;
205 GstElementClass *gstelement_class;
206 GstBaseSrcClass *gstbasesrc_class;
207 GstPushSrcClass *gstpushsrc_class;
209 gobject_class = (GObjectClass *) klass;
210 gstelement_class = (GstElementClass *) klass;
211 gstbasesrc_class = (GstBaseSrcClass *) klass;
212 gstpushsrc_class = (GstPushSrcClass *) klass;
214 gobject_class->set_property = gst_soup_http_src_set_property;
215 gobject_class->get_property = gst_soup_http_src_get_property;
216 gobject_class->finalize = gst_soup_http_src_finalize;
217 gobject_class->dispose = gst_soup_http_src_dispose;
219 g_object_class_install_property (gobject_class,
221 g_param_spec_string ("location", "Location",
222 "Location to read from", "",
223 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
224 g_object_class_install_property (gobject_class,
226 g_param_spec_string ("user-agent", "User-Agent",
227 "Value of the User-Agent HTTP request header field",
228 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
229 g_object_class_install_property (gobject_class,
230 PROP_AUTOMATIC_REDIRECT,
231 g_param_spec_boolean ("automatic-redirect", "automatic-redirect",
232 "Automatically follow HTTP redirects (HTTP Status Code 3xx)",
233 TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
234 g_object_class_install_property (gobject_class,
236 g_param_spec_string ("proxy", "Proxy",
237 "HTTP proxy server URI", "",
238 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
239 g_object_class_install_property (gobject_class,
241 g_param_spec_string ("user-id", "user-id",
242 "HTTP location URI user id for authentication", "",
243 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
244 g_object_class_install_property (gobject_class, PROP_USER_PW,
245 g_param_spec_string ("user-pw", "user-pw",
246 "HTTP location URI user password for authentication", "",
247 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
248 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
249 g_param_spec_string ("proxy-id", "proxy-id",
250 "HTTP proxy URI user id for authentication", "",
251 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
252 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
253 g_param_spec_string ("proxy-pw", "proxy-pw",
254 "HTTP proxy URI user password for authentication", "",
255 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
256 g_object_class_install_property (gobject_class, PROP_COOKIES,
257 g_param_spec_boxed ("cookies", "Cookies", "HTTP request cookies",
258 G_TYPE_STRV, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
259 g_object_class_install_property (gobject_class, PROP_IS_LIVE,
260 g_param_spec_boolean ("is-live", "is-live", "Act like a live source",
261 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
262 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
263 g_param_spec_uint ("timeout", "timeout",
264 "Value in seconds to timeout a blocking I/O (0 = No timeout).", 0,
265 3600, DEFAULT_TIMEOUT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
266 g_object_class_install_property (gobject_class, PROP_EXTRA_HEADERS,
267 g_param_spec_boxed ("extra-headers", "Extra Headers",
268 "Extra headers to append to the HTTP request",
269 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
270 g_object_class_install_property (gobject_class, PROP_IRADIO_MODE,
271 g_param_spec_boolean ("iradio-mode", "iradio-mode",
272 "Enable internet radio mode (ask server to send shoutcast/icecast "
273 "metadata interleaved with the actual stream data)",
274 DEFAULT_IRADIO_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
277 * GstSoupHTTPSrc::http-log-level:
279 * If set and > 0, captures and dumps HTTP session data as
280 * log messages if log level >= GST_LEVEL_TRACE
284 g_object_class_install_property (gobject_class, PROP_SOUP_LOG_LEVEL,
285 g_param_spec_enum ("http-log-level", "HTTP log level",
286 "Set log level for soup's HTTP session log",
287 SOUP_TYPE_LOGGER_LOG_LEVEL, DEFAULT_SOUP_LOG_LEVEL,
288 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
291 * GstSoupHTTPSrc::compress:
293 * If set to %TRUE, souphttpsrc will automatically handle gzip
294 * and deflate Content-Encodings. This does not make much difference
295 * and causes more load for normal media files, but makes a real
296 * difference in size for plaintext files.
300 g_object_class_install_property (gobject_class, PROP_COMPRESS,
301 g_param_spec_boolean ("compress", "Compress",
302 "Allow compressed content encodings",
303 DEFAULT_COMPRESS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
306 * GstSoupHTTPSrc::keep-alive:
308 * If set to %TRUE, souphttpsrc will keep alive connections when being
309 * set to READY state and only will close connections when connecting
310 * to a different server or when going to NULL state..
314 g_object_class_install_property (gobject_class, PROP_KEEP_ALIVE,
315 g_param_spec_boolean ("keep-alive", "keep-alive",
316 "Use HTTP persistent connections", DEFAULT_KEEP_ALIVE,
317 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
320 * GstSoupHTTPSrc::ssl-strict:
322 * If set to %TRUE, souphttpsrc will reject all SSL certificates that
323 * are considered invalid.
327 g_object_class_install_property (gobject_class, PROP_SSL_STRICT,
328 g_param_spec_boolean ("ssl-strict", "SSL Strict",
329 "Strict SSL certificate checking", DEFAULT_SSL_STRICT,
330 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
333 * GstSoupHTTPSrc::ssl-ca-file:
335 * A SSL anchor CA file that should be used for checking certificates
336 * instead of the system CA file.
340 g_object_class_install_property (gobject_class, PROP_SSL_CA_FILE,
341 g_param_spec_string ("ssl-ca-file", "SSL CA File",
342 "Location of a SSL anchor CA file to use", DEFAULT_SSL_CA_FILE,
343 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
346 * GstSoupHTTPSrc::ssl-use-system-ca-file:
348 * If set to %TRUE, souphttpsrc will use the system's CA file for
349 * checking certificates.
353 g_object_class_install_property (gobject_class, PROP_SSL_USE_SYSTEM_CA_FILE,
354 g_param_spec_boolean ("ssl-use-system-ca-file", "Use System CA File",
355 "Use system CA file", DEFAULT_SSL_USE_SYSTEM_CA_FILE,
356 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
359 * GstSoupHTTPSrc::retries:
361 * Maximum number of retries until giving up.
365 g_object_class_install_property (gobject_class, PROP_RETRIES,
366 g_param_spec_int ("retries", "Retries",
367 "Maximum number of retries until giving up (-1=infinite)", -1,
368 G_MAXINT, DEFAULT_RETRIES,
369 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
371 gst_element_class_add_pad_template (gstelement_class,
372 gst_static_pad_template_get (&srctemplate));
374 gst_element_class_set_static_metadata (gstelement_class, "HTTP client source",
376 "Receive data as a client over the network via HTTP using SOUP",
377 "Wouter Cloetens <wouter@mind.be>");
378 gstelement_class->change_state =
379 GST_DEBUG_FUNCPTR (gst_soup_http_src_change_state);
381 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_soup_http_src_start);
382 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_soup_http_src_stop);
383 gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_soup_http_src_unlock);
384 gstbasesrc_class->unlock_stop =
385 GST_DEBUG_FUNCPTR (gst_soup_http_src_unlock_stop);
386 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_soup_http_src_get_size);
387 gstbasesrc_class->is_seekable =
388 GST_DEBUG_FUNCPTR (gst_soup_http_src_is_seekable);
389 gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_soup_http_src_do_seek);
390 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_soup_http_src_query);
392 gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_soup_http_src_create);
394 GST_DEBUG_CATEGORY_INIT (souphttpsrc_debug, "souphttpsrc", 0,
399 gst_soup_http_src_reset (GstSoupHTTPSrc * src)
401 src->interrupted = FALSE;
403 src->retry_count = 0;
404 src->have_size = FALSE;
405 src->got_headers = FALSE;
406 src->seekable = FALSE;
407 src->read_position = 0;
408 src->request_position = 0;
409 src->stop_position = -1;
410 src->content_size = 0;
411 src->have_body = FALSE;
413 src->ret = GST_FLOW_OK;
415 gst_caps_replace (&src->src_caps, NULL);
416 g_free (src->iradio_name);
417 src->iradio_name = NULL;
418 g_free (src->iradio_genre);
419 src->iradio_genre = NULL;
420 g_free (src->iradio_url);
421 src->iradio_url = NULL;
425 gst_soup_http_src_init (GstSoupHTTPSrc * src)
429 g_mutex_init (&src->mutex);
430 g_cond_init (&src->request_finished_cond);
431 src->location = NULL;
432 src->redirection_uri = NULL;
433 src->automatic_redirect = TRUE;
434 src->user_agent = g_strdup (DEFAULT_USER_AGENT);
437 src->proxy_id = NULL;
438 src->proxy_pw = NULL;
440 src->iradio_mode = DEFAULT_IRADIO_MODE;
445 src->timeout = DEFAULT_TIMEOUT;
446 src->log_level = DEFAULT_SOUP_LOG_LEVEL;
447 src->ssl_strict = DEFAULT_SSL_STRICT;
448 src->ssl_use_system_ca_file = DEFAULT_SSL_USE_SYSTEM_CA_FILE;
449 src->max_retries = DEFAULT_RETRIES;
450 proxy = g_getenv ("http_proxy");
451 if (proxy && !gst_soup_http_src_set_proxy (src, proxy)) {
452 GST_WARNING_OBJECT (src,
453 "The proxy in the http_proxy env var (\"%s\") cannot be parsed.",
457 gst_base_src_set_automatic_eos (GST_BASE_SRC (src), FALSE);
459 gst_soup_http_src_reset (src);
463 gst_soup_http_src_dispose (GObject * gobject)
465 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (gobject);
467 GST_DEBUG_OBJECT (src, "dispose");
469 gst_soup_http_src_session_close (src);
471 G_OBJECT_CLASS (parent_class)->dispose (gobject);
475 gst_soup_http_src_finalize (GObject * gobject)
477 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (gobject);
479 GST_DEBUG_OBJECT (src, "finalize");
481 g_mutex_clear (&src->mutex);
482 g_cond_clear (&src->request_finished_cond);
483 g_free (src->location);
484 if (src->redirection_uri) {
485 g_free (src->redirection_uri);
487 g_free (src->user_agent);
488 if (src->proxy != NULL) {
489 soup_uri_free (src->proxy);
491 g_free (src->user_id);
492 g_free (src->user_pw);
493 g_free (src->proxy_id);
494 g_free (src->proxy_pw);
495 g_strfreev (src->cookies);
497 if (src->extra_headers) {
498 gst_structure_free (src->extra_headers);
499 src->extra_headers = NULL;
502 g_free (src->ssl_ca_file);
504 G_OBJECT_CLASS (parent_class)->finalize (gobject);
508 gst_soup_http_src_set_property (GObject * object, guint prop_id,
509 const GValue * value, GParamSpec * pspec)
511 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (object);
516 const gchar *location;
518 location = g_value_get_string (value);
520 if (location == NULL) {
521 GST_WARNING ("location property cannot be NULL");
524 if (!gst_soup_http_src_set_location (src, location, NULL)) {
525 GST_WARNING ("badly formatted location");
530 case PROP_USER_AGENT:
532 g_free (src->user_agent);
533 src->user_agent = g_value_dup_string (value);
535 case PROP_IRADIO_MODE:
536 src->iradio_mode = g_value_get_boolean (value);
538 case PROP_AUTOMATIC_REDIRECT:
539 src->automatic_redirect = g_value_get_boolean (value);
545 proxy = g_value_get_string (value);
548 GST_WARNING ("proxy property cannot be NULL");
551 if (!gst_soup_http_src_set_proxy (src, proxy)) {
552 GST_WARNING ("badly formatted proxy URI");
558 g_strfreev (src->cookies);
559 src->cookies = g_strdupv (g_value_get_boxed (value));
562 gst_base_src_set_live (GST_BASE_SRC (src), g_value_get_boolean (value));
566 g_free (src->user_id);
567 src->user_id = g_value_dup_string (value);
571 g_free (src->user_pw);
572 src->user_pw = g_value_dup_string (value);
576 g_free (src->proxy_id);
577 src->proxy_id = g_value_dup_string (value);
581 g_free (src->proxy_pw);
582 src->proxy_pw = g_value_dup_string (value);
585 src->timeout = g_value_get_uint (value);
587 case PROP_EXTRA_HEADERS:{
588 const GstStructure *s = gst_value_get_structure (value);
590 if (src->extra_headers)
591 gst_structure_free (src->extra_headers);
593 src->extra_headers = s ? gst_structure_copy (s) : NULL;
596 case PROP_SOUP_LOG_LEVEL:
597 src->log_level = g_value_get_enum (value);
600 src->compress = g_value_get_boolean (value);
602 case PROP_KEEP_ALIVE:
603 src->keep_alive = g_value_get_boolean (value);
605 case PROP_SSL_STRICT:
606 src->ssl_strict = g_value_get_boolean (value);
608 case PROP_SSL_CA_FILE:
609 if (src->ssl_ca_file)
610 g_free (src->ssl_ca_file);
611 src->ssl_ca_file = g_value_dup_string (value);
613 case PROP_SSL_USE_SYSTEM_CA_FILE:
614 src->ssl_use_system_ca_file = g_value_get_boolean (value);
617 src->max_retries = g_value_get_int (value);
620 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
628 gst_soup_http_src_get_property (GObject * object, guint prop_id,
629 GValue * value, GParamSpec * pspec)
631 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (object);
635 g_value_set_string (value, src->location);
637 case PROP_USER_AGENT:
638 g_value_set_string (value, src->user_agent);
640 case PROP_AUTOMATIC_REDIRECT:
641 g_value_set_boolean (value, src->automatic_redirect);
644 if (src->proxy == NULL)
645 g_value_set_static_string (value, "");
647 char *proxy = soup_uri_to_string (src->proxy, FALSE);
649 g_value_set_string (value, proxy);
654 g_value_set_boxed (value, g_strdupv (src->cookies));
657 g_value_set_boolean (value, gst_base_src_is_live (GST_BASE_SRC (src)));
659 case PROP_IRADIO_MODE:
660 g_value_set_boolean (value, src->iradio_mode);
663 g_value_set_string (value, src->user_id);
666 g_value_set_string (value, src->user_pw);
669 g_value_set_string (value, src->proxy_id);
672 g_value_set_string (value, src->proxy_pw);
675 g_value_set_uint (value, src->timeout);
677 case PROP_EXTRA_HEADERS:
678 gst_value_set_structure (value, src->extra_headers);
680 case PROP_SOUP_LOG_LEVEL:
681 g_value_set_enum (value, src->log_level);
684 g_value_set_boolean (value, src->compress);
686 case PROP_KEEP_ALIVE:
687 g_value_set_boolean (value, src->keep_alive);
689 case PROP_SSL_STRICT:
690 g_value_set_boolean (value, src->ssl_strict);
692 case PROP_SSL_CA_FILE:
693 g_value_set_string (value, src->ssl_ca_file);
695 case PROP_SSL_USE_SYSTEM_CA_FILE:
696 g_value_set_boolean (value, src->ssl_strict);
699 g_value_set_int (value, src->max_retries);
702 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
708 gst_soup_http_src_unicodify (const gchar * str)
710 const gchar *env_vars[] = { "GST_ICY_TAG_ENCODING",
711 "GST_TAG_ENCODING", NULL
714 return gst_tag_freeform_string_to_utf8 (str, -1, env_vars);
718 gst_soup_http_src_cancel_message (GstSoupHTTPSrc * src)
720 if (src->msg != NULL) {
721 GST_INFO_OBJECT (src, "Cancelling message");
722 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_CANCELLED;
723 soup_session_cancel_message (src->session, src->msg, SOUP_STATUS_CANCELLED);
725 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE;
730 gst_soup_http_src_queue_message (GstSoupHTTPSrc * src)
732 soup_session_queue_message (src->session, src->msg,
733 (SoupSessionCallback) gst_soup_http_src_response_cb, src);
734 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_QUEUED;
738 gst_soup_http_src_add_range_header (GstSoupHTTPSrc * src, guint64 offset,
745 soup_message_headers_remove (src->msg->request_headers, "Range");
746 if (offset || stop_offset != -1) {
747 if (stop_offset != -1) {
748 rc = g_snprintf (buf, sizeof (buf), "bytes=%" G_GUINT64_FORMAT "-%"
749 G_GUINT64_FORMAT, offset, stop_offset);
751 rc = g_snprintf (buf, sizeof (buf), "bytes=%" G_GUINT64_FORMAT "-",
754 if (rc > sizeof (buf) || rc < 0)
756 soup_message_headers_append (src->msg->request_headers, "Range", buf);
758 src->read_position = offset;
763 _append_extra_header (GQuark field_id, const GValue * value, gpointer user_data)
765 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (user_data);
766 const gchar *field_name = g_quark_to_string (field_id);
767 gchar *field_content = NULL;
769 if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
770 field_content = g_value_dup_string (value);
772 GValue dest = { 0, };
774 g_value_init (&dest, G_TYPE_STRING);
775 if (g_value_transform (value, &dest)) {
776 field_content = g_value_dup_string (&dest);
780 if (field_content == NULL) {
781 GST_ERROR_OBJECT (src, "extra-headers field '%s' contains no value "
782 "or can't be converted to a string", field_name);
786 GST_DEBUG_OBJECT (src, "Appending extra header: \"%s: %s\"", field_name,
788 soup_message_headers_append (src->msg->request_headers, field_name,
791 g_free (field_content);
797 _append_extra_headers (GQuark field_id, const GValue * value,
800 if (G_VALUE_TYPE (value) == GST_TYPE_ARRAY) {
801 guint n = gst_value_array_get_size (value);
804 for (i = 0; i < n; i++) {
805 const GValue *v = gst_value_array_get_value (value, i);
807 if (!_append_extra_header (field_id, v, user_data))
810 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
811 guint n = gst_value_list_get_size (value);
814 for (i = 0; i < n; i++) {
815 const GValue *v = gst_value_list_get_value (value, i);
817 if (!_append_extra_header (field_id, v, user_data))
821 return _append_extra_header (field_id, value, user_data);
829 gst_soup_http_src_add_extra_headers (GstSoupHTTPSrc * src)
831 if (!src->extra_headers)
834 return gst_structure_foreach (src->extra_headers, _append_extra_headers, src);
839 gst_soup_http_src_session_unpause_message (GstSoupHTTPSrc * src)
841 soup_session_unpause_message (src->session, src->msg);
845 gst_soup_http_src_session_pause_message (GstSoupHTTPSrc * src)
847 soup_session_pause_message (src->session, src->msg);
851 gst_soup_http_src_session_open (GstSoupHTTPSrc * src)
854 GST_DEBUG_OBJECT (src, "Session is already open");
858 if (!src->location) {
859 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (_("No URL set.")),
860 ("Missing location property"));
865 src->context = g_main_context_new ();
868 src->loop = g_main_loop_new (src->context, TRUE);
870 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
871 (NULL), ("Failed to start GMainLoop"));
872 g_main_context_unref (src->context);
877 GST_DEBUG_OBJECT (src, "Creating session");
878 if (src->proxy == NULL) {
880 soup_session_async_new_with_options (SOUP_SESSION_ASYNC_CONTEXT,
881 src->context, SOUP_SESSION_USER_AGENT, src->user_agent,
882 SOUP_SESSION_TIMEOUT, src->timeout,
883 SOUP_SESSION_SSL_STRICT, src->ssl_strict,
884 SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_PROXY_RESOLVER_DEFAULT,
888 soup_session_async_new_with_options (SOUP_SESSION_ASYNC_CONTEXT,
889 src->context, SOUP_SESSION_PROXY_URI, src->proxy,
890 SOUP_SESSION_TIMEOUT, src->timeout,
891 SOUP_SESSION_SSL_STRICT, src->ssl_strict,
892 SOUP_SESSION_USER_AGENT, src->user_agent, NULL);
896 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
897 (NULL), ("Failed to create async session"));
901 g_signal_connect (src->session, "authenticate",
902 G_CALLBACK (gst_soup_http_src_authenticate_cb), src);
905 gst_soup_util_log_setup (src->session, src->log_level, GST_ELEMENT (src));
906 if (src->ssl_ca_file)
907 g_object_set (src->session, "ssl-ca-file", src->ssl_ca_file, NULL);
909 g_object_set (src->session, "ssl-use-system-ca-file",
910 src->ssl_use_system_ca_file, NULL);
912 GST_DEBUG_OBJECT (src, "Re-using session");
916 soup_session_add_feature_by_type (src->session, SOUP_TYPE_CONTENT_DECODER);
918 soup_session_remove_feature_by_type (src->session,
919 SOUP_TYPE_CONTENT_DECODER);
924 #ifdef LIBSOUP_DOES_NOT_STEAL_OUR_CONTEXT
926 dummy_idle_cb (gpointer data)
928 return FALSE /* Idle source is removed */ ;
933 gst_soup_http_src_session_close (GstSoupHTTPSrc * src)
935 GST_DEBUG_OBJECT (src, "Closing session");
938 g_main_loop_quit (src->loop);
940 g_mutex_lock (&src->mutex);
942 soup_session_abort (src->session); /* This unrefs the message. */
943 g_object_unref (src->session);
948 #ifdef LIBSOUP_DOES_NOT_STEAL_OUR_CONTEXT
949 GSource *idle_source;
951 /* Iterating the main context to give GIO cancellables a chance
952 * to initiate cleanups. Wihout this, resources allocated by
953 * libsoup for the connection are not released and socket fd is
955 idle_source = g_idle_source_new ();
956 /* Suppressing "idle souce without callback" warning */
957 g_source_set_callback (idle_source, dummy_idle_cb, NULL, NULL);
958 g_source_set_priority (idle_source, G_PRIORITY_LOW);
959 g_source_attach (idle_source, src->context);
960 /* Acquiring the context. Idle source guarantees that we'll not block. */
961 g_main_context_push_thread_default (src->context);
962 g_main_context_iteration (src->context, TRUE);
963 /* Ensuring that there's no unhandled pending events left. */
964 while (g_main_context_iteration (src->context, FALSE));
965 g_main_context_pop_thread_default (src->context);
966 g_source_unref (idle_source);
969 g_main_loop_unref (src->loop);
970 g_main_context_unref (src->context);
974 g_mutex_unlock (&src->mutex);
978 gst_soup_http_src_authenticate_cb (SoupSession * session, SoupMessage * msg,
979 SoupAuth * auth, gboolean retrying, GstSoupHTTPSrc * src)
982 /* First time authentication only, if we fail and are called again with retry true fall through */
983 if (msg->status_code == SOUP_STATUS_UNAUTHORIZED) {
984 if (src->user_id && src->user_pw)
985 soup_auth_authenticate (auth, src->user_id, src->user_pw);
986 } else if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
987 if (src->proxy_id && src->proxy_pw)
988 soup_auth_authenticate (auth, src->proxy_id, src->proxy_pw);
994 insert_http_header (const gchar * name, const gchar * value, gpointer user_data)
996 GstStructure *headers = user_data;
999 gv = gst_structure_get_value (headers, name);
1000 if (gv && GST_VALUE_HOLDS_ARRAY (gv)) {
1001 GValue v = G_VALUE_INIT;
1003 g_value_init (&v, G_TYPE_STRING);
1004 g_value_set_string (&v, value);
1005 gst_value_array_append_value ((GValue *) gv, &v);
1007 } else if (gv && G_VALUE_HOLDS_STRING (gv)) {
1008 GValue arr = G_VALUE_INIT;
1009 GValue v = G_VALUE_INIT;
1010 const gchar *old_value = g_value_get_string (gv);
1012 g_value_init (&arr, GST_TYPE_ARRAY);
1013 g_value_init (&v, G_TYPE_STRING);
1014 g_value_set_string (&v, old_value);
1015 gst_value_array_append_value (&arr, &v);
1016 g_value_set_string (&v, value);
1017 gst_value_array_append_value (&arr, &v);
1019 gst_structure_set_value (headers, name, &arr);
1021 g_value_unset (&arr);
1023 gst_structure_set (headers, name, G_TYPE_STRING, value, NULL);
1028 gst_soup_http_src_got_headers_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
1031 GstTagList *tag_list;
1032 GstBaseSrc *basesrc;
1034 GHashTable *params = NULL;
1035 GstEvent *http_headers_event;
1036 GstStructure *http_headers, *headers;
1037 const gchar *accept_ranges;
1039 GST_INFO_OBJECT (src, "got headers");
1041 if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED &&
1042 src->proxy_id && src->proxy_pw)
1045 if (src->automatic_redirect && SOUP_STATUS_IS_REDIRECTION (msg->status_code)) {
1046 src->redirection_uri = g_strdup (soup_message_headers_get_one
1047 (msg->response_headers, "Location"));
1048 src->redirection_permanent =
1049 (msg->status_code == SOUP_STATUS_MOVED_PERMANENTLY);
1050 GST_DEBUG_OBJECT (src, "%u redirect to \"%s\" (permanent %d)",
1051 msg->status_code, src->redirection_uri, src->redirection_permanent);
1055 if (msg->status_code == SOUP_STATUS_UNAUTHORIZED)
1058 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING;
1059 src->got_headers = TRUE;
1061 http_headers = gst_structure_new_empty ("http-headers");
1062 gst_structure_set (http_headers, "uri", G_TYPE_STRING, src->location, NULL);
1063 if (src->redirection_uri)
1064 gst_structure_set (http_headers, "redirection-uri", G_TYPE_STRING,
1065 src->redirection_uri, NULL);
1066 headers = gst_structure_new_empty ("request-headers");
1067 soup_message_headers_foreach (msg->request_headers, insert_http_header,
1069 gst_structure_set (http_headers, "request-headers", GST_TYPE_STRUCTURE,
1071 gst_structure_free (headers);
1072 headers = gst_structure_new_empty ("response-headers");
1073 soup_message_headers_foreach (msg->response_headers, insert_http_header,
1075 gst_structure_set (http_headers, "response-headers", GST_TYPE_STRUCTURE,
1077 gst_structure_free (headers);
1079 http_headers_event =
1080 gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_STICKY, http_headers);
1081 gst_event_replace (&src->http_headers_event, http_headers_event);
1082 gst_event_unref (http_headers_event);
1084 /* Parse Content-Length. */
1085 if (soup_message_headers_get_encoding (msg->response_headers) ==
1086 SOUP_ENCODING_CONTENT_LENGTH) {
1087 newsize = src->request_position +
1088 soup_message_headers_get_content_length (msg->response_headers);
1089 if (!src->have_size || (src->content_size != newsize)) {
1090 src->content_size = newsize;
1091 src->have_size = TRUE;
1092 src->seekable = TRUE;
1093 GST_DEBUG_OBJECT (src, "size = %" G_GUINT64_FORMAT, src->content_size);
1095 basesrc = GST_BASE_SRC_CAST (src);
1096 basesrc->segment.duration = src->content_size;
1097 gst_element_post_message (GST_ELEMENT (src),
1098 gst_message_new_duration_changed (GST_OBJECT (src)));
1102 /* If the server reports Accept-Ranges: none we don't have to try
1103 * doing range requests at all
1105 if ((accept_ranges =
1106 soup_message_headers_get_one (msg->response_headers,
1107 "Accept-Ranges"))) {
1108 if (g_ascii_strcasecmp (accept_ranges, "none") == 0)
1109 src->seekable = FALSE;
1113 tag_list = gst_tag_list_new_empty ();
1116 soup_message_headers_get_one (msg->response_headers,
1117 "icy-metaint")) != NULL) {
1118 gint icy_metaint = atoi (value);
1120 GST_DEBUG_OBJECT (src, "icy-metaint: %s (parsed: %d)", value, icy_metaint);
1121 if (icy_metaint > 0) {
1123 gst_caps_unref (src->src_caps);
1125 src->src_caps = gst_caps_new_simple ("application/x-icy",
1126 "metadata-interval", G_TYPE_INT, icy_metaint, NULL);
1128 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
1132 soup_message_headers_get_content_type (msg->response_headers,
1133 ¶ms)) != NULL) {
1134 GST_DEBUG_OBJECT (src, "Content-Type: %s", value);
1135 if (g_ascii_strcasecmp (value, "audio/L16") == 0) {
1141 gst_caps_unref (src->src_caps);
1143 param = g_hash_table_lookup (params, "channels");
1145 channels = atol (param);
1147 param = g_hash_table_lookup (params, "rate");
1149 rate = atol (param);
1151 src->src_caps = gst_caps_new_simple ("audio/x-raw",
1152 "format", G_TYPE_STRING, "S16BE",
1153 "layout", G_TYPE_STRING, "interleaved",
1154 "channels", G_TYPE_INT, channels, "rate", G_TYPE_INT, rate, NULL);
1156 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
1158 /* Set the Content-Type field on the caps */
1159 if (src->src_caps) {
1160 src->src_caps = gst_caps_make_writable (src->src_caps);
1161 gst_caps_set_simple (src->src_caps, "content-type", G_TYPE_STRING,
1163 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
1169 g_hash_table_destroy (params);
1172 soup_message_headers_get_one (msg->response_headers,
1173 "icy-name")) != NULL) {
1174 g_free (src->iradio_name);
1175 src->iradio_name = gst_soup_http_src_unicodify (value);
1176 if (src->iradio_name) {
1177 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_ORGANIZATION,
1178 src->iradio_name, NULL);
1182 soup_message_headers_get_one (msg->response_headers,
1183 "icy-genre")) != NULL) {
1184 g_free (src->iradio_genre);
1185 src->iradio_genre = gst_soup_http_src_unicodify (value);
1186 if (src->iradio_genre) {
1187 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_GENRE,
1188 src->iradio_genre, NULL);
1191 if ((value = soup_message_headers_get_one (msg->response_headers, "icy-url"))
1193 g_free (src->iradio_url);
1194 src->iradio_url = gst_soup_http_src_unicodify (value);
1195 if (src->iradio_url) {
1196 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_LOCATION,
1197 src->iradio_url, NULL);
1200 if (!gst_tag_list_is_empty (tag_list)) {
1201 GST_DEBUG_OBJECT (src,
1202 "calling gst_element_found_tags with %" GST_PTR_FORMAT, tag_list);
1203 gst_pad_push_event (GST_BASE_SRC_PAD (src), gst_event_new_tag (tag_list));
1205 gst_tag_list_unref (tag_list);
1208 /* Handle HTTP errors. */
1209 gst_soup_http_src_parse_status (msg, src);
1211 /* Check if Range header was respected. */
1212 if (src->ret == GST_FLOW_CUSTOM_ERROR &&
1213 src->read_position && msg->status_code != SOUP_STATUS_PARTIAL_CONTENT) {
1214 src->seekable = FALSE;
1215 GST_ELEMENT_ERROR (src, RESOURCE, SEEK,
1216 (_("Server does not support seeking.")),
1217 ("Server does not accept Range HTTP header, URL: %s, Redirect to: %s",
1218 src->location, GST_STR_NULL (src->redirection_uri)));
1219 src->ret = GST_FLOW_ERROR;
1222 /* If we are going to error out, stop all processing right here, so we
1223 * don't output any data (such as an error html page), and return
1224 * GST_FLOW_ERROR from the create function instead of having
1225 * got_chunk_cb overwrite src->ret with FLOW_OK again. */
1226 if (src->ret == GST_FLOW_ERROR || src->ret == GST_FLOW_EOS) {
1227 gst_soup_http_src_session_pause_message (src);
1230 g_main_loop_quit (src->loop);
1232 g_cond_signal (&src->request_finished_cond);
1235 /* Have body. Signal EOS. */
1237 gst_soup_http_src_got_body_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
1239 if (G_UNLIKELY (msg != src->msg)) {
1240 GST_DEBUG_OBJECT (src, "got body, but not for current message");
1243 if (G_UNLIKELY (src->session_io_status !=
1244 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
1245 /* Probably a redirect. */
1248 GST_DEBUG_OBJECT (src, "got body");
1249 src->ret = GST_FLOW_EOS;
1250 src->have_body = TRUE;
1252 /* no need to interrupt the message here, we do it on the
1253 * finished_cb anyway if needed. And getting the body might mean
1254 * that the connection was hang up before finished. This happens when
1255 * the pipeline is stalled for too long (long pauses during playback).
1256 * Best to let it continue from here and pause because it reached the
1257 * final bytes based on content_size or received an out of range error */
1260 /* Finished. Signal EOS. */
1262 gst_soup_http_src_finished_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
1264 if (G_UNLIKELY (msg != src->msg)) {
1265 GST_DEBUG_OBJECT (src, "finished, but not for current message");
1268 GST_INFO_OBJECT (src, "finished, io status: %d", src->session_io_status);
1269 src->ret = GST_FLOW_EOS;
1270 if (src->session_io_status == GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_CANCELLED) {
1271 /* gst_soup_http_src_cancel_message() triggered this; probably a seek
1272 * that occurred in the QUEUEING state; i.e. before the connection setup
1273 * was complete. Do nothing */
1274 GST_DEBUG_OBJECT (src, "cancelled");
1275 } else if (src->session_io_status ==
1276 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING && src->read_position > 0 &&
1277 (src->have_size && src->read_position < src->content_size) &&
1278 (src->max_retries == -1 || src->retry_count < src->max_retries)) {
1279 /* The server disconnected while streaming. Reconnect and seeking to the
1283 src->ret = GST_FLOW_CUSTOM_ERROR;
1284 } else if (G_UNLIKELY (src->session_io_status !=
1285 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
1286 if (msg->method == SOUP_METHOD_HEAD) {
1287 GST_DEBUG_OBJECT (src, "Ignoring error %d:%s during HEAD request",
1288 msg->status_code, msg->reason_phrase);
1290 gst_soup_http_src_parse_status (msg, src);
1294 g_main_loop_quit (src->loop);
1295 g_cond_signal (&src->request_finished_cond);
1298 /* Buffer lifecycle management.
1300 * gst_soup_http_src_create() runs the GMainLoop for this element, to let
1301 * Soup take control.
1302 * A GstBuffer is allocated in gst_soup_http_src_chunk_allocator() and
1303 * associated with a SoupBuffer.
1304 * Soup reads HTTP data in the GstBuffer's data buffer.
1305 * The gst_soup_http_src_got_chunk_cb() is then called with the SoupBuffer.
1306 * That sets gst_soup_http_src_create()'s return argument to the GstBuffer,
1307 * increments its refcount (to 2), pauses the flow of data from the HTTP
1308 * source to prevent gst_soup_http_src_got_chunk_cb() from being called
1309 * again and breaks out of the GMainLoop.
1310 * Because the SOUP_MESSAGE_OVERWRITE_CHUNKS flag is set, Soup frees the
1311 * SoupBuffer and calls gst_soup_http_src_chunk_free(), which decrements the
1313 * gst_soup_http_src_create() returns the GstBuffer. It will be freed by a
1314 * downstream element.
1315 * If Soup fails to read HTTP data, it does not call
1316 * gst_soup_http_src_got_chunk_cb(), but still frees the SoupBuffer and
1317 * calls gst_soup_http_src_chunk_free(), which decrements the GstBuffer's
1318 * refcount to 0, freeing it.
1328 gst_soup_http_src_chunk_free (gpointer user_data)
1330 SoupGstChunk *chunk = (SoupGstChunk *) user_data;
1332 gst_buffer_unmap (chunk->buffer, &chunk->map);
1333 gst_buffer_unref (chunk->buffer);
1334 g_slice_free (SoupGstChunk, chunk);
1338 gst_soup_http_src_chunk_allocator (SoupMessage * msg, gsize max_len,
1341 GstSoupHTTPSrc *src = (GstSoupHTTPSrc *) user_data;
1342 GstBaseSrc *basesrc = GST_BASE_SRC_CAST (src);
1344 SoupBuffer *soupbuf;
1347 SoupGstChunk *chunk;
1350 length = MIN (basesrc->blocksize, max_len);
1352 length = basesrc->blocksize;
1353 GST_DEBUG_OBJECT (src, "alloc %" G_GSIZE_FORMAT " bytes <= %" G_GSIZE_FORMAT,
1356 rc = GST_BASE_SRC_CLASS (parent_class)->alloc (basesrc, -1, length, &gstbuf);
1357 if (G_UNLIKELY (rc != GST_FLOW_OK)) {
1358 /* Failed to allocate buffer. Stall SoupSession and return error code
1361 g_main_loop_quit (src->loop);
1365 chunk = g_slice_new0 (SoupGstChunk);
1366 chunk->buffer = gstbuf;
1367 gst_buffer_map (gstbuf, &chunk->map, GST_MAP_READWRITE);
1369 soupbuf = soup_buffer_new_with_owner (chunk->map.data, chunk->map.size,
1370 chunk, gst_soup_http_src_chunk_free);
1376 gst_soup_http_src_got_chunk_cb (SoupMessage * msg, SoupBuffer * chunk,
1377 GstSoupHTTPSrc * src)
1379 GstBaseSrc *basesrc;
1380 guint64 new_position;
1381 SoupGstChunk *gchunk;
1383 if (G_UNLIKELY (msg != src->msg)) {
1384 GST_DEBUG_OBJECT (src, "got chunk, but not for current message");
1387 if (G_UNLIKELY (!src->outbuf)) {
1388 GST_DEBUG_OBJECT (src, "got chunk but we're not expecting one");
1389 src->ret = GST_FLOW_OK;
1390 gst_soup_http_src_cancel_message (src);
1391 g_main_loop_quit (src->loop);
1395 /* We got data, reset the retry counter */
1396 src->retry_count = 0;
1398 src->have_body = FALSE;
1399 if (G_UNLIKELY (src->session_io_status !=
1400 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
1401 /* Probably a redirect. */
1404 basesrc = GST_BASE_SRC_CAST (src);
1405 GST_DEBUG_OBJECT (src, "got chunk of %" G_GSIZE_FORMAT " bytes",
1408 /* Extract the GstBuffer from the SoupBuffer and set its fields. */
1409 gchunk = (SoupGstChunk *) soup_buffer_get_owner (chunk);
1410 *src->outbuf = gchunk->buffer;
1412 gst_buffer_resize (*src->outbuf, 0, chunk->length);
1413 GST_BUFFER_OFFSET (*src->outbuf) = basesrc->segment.position;
1415 gst_buffer_ref (*src->outbuf);
1417 new_position = src->read_position + chunk->length;
1418 if (G_LIKELY (src->request_position == src->read_position))
1419 src->request_position = new_position;
1420 src->read_position = new_position;
1422 if (src->have_size) {
1423 if (new_position > src->content_size) {
1424 GST_DEBUG_OBJECT (src, "Got position previous estimated content size "
1425 "(%" G_GINT64_FORMAT " > %" G_GINT64_FORMAT ")", new_position,
1427 src->content_size = new_position;
1428 basesrc->segment.duration = src->content_size;
1429 gst_element_post_message (GST_ELEMENT (src),
1430 gst_message_new_duration_changed (GST_OBJECT (src)));
1431 } else if (new_position == src->content_size) {
1432 GST_DEBUG_OBJECT (src, "We're EOS now");
1436 src->ret = GST_FLOW_OK;
1437 g_main_loop_quit (src->loop);
1438 gst_soup_http_src_session_pause_message (src);
1442 gst_soup_http_src_response_cb (SoupSession * session, SoupMessage * msg,
1443 GstSoupHTTPSrc * src)
1445 if (G_UNLIKELY (msg != src->msg)) {
1446 GST_DEBUG_OBJECT (src, "got response %d: %s, but not for current message",
1447 msg->status_code, msg->reason_phrase);
1450 if (G_UNLIKELY (src->session_io_status !=
1451 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)
1452 && SOUP_STATUS_IS_REDIRECTION (msg->status_code)) {
1453 /* Ignore redirections. */
1456 GST_INFO_OBJECT (src, "got response %d: %s", msg->status_code,
1457 msg->reason_phrase);
1458 if (src->session_io_status == GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING &&
1459 src->read_position > 0 && (src->have_size
1460 && src->read_position < src->content_size) &&
1461 (src->max_retries == -1 || src->retry_count < src->max_retries)) {
1462 /* The server disconnected while streaming. Reconnect and seeking to the
1467 gst_soup_http_src_parse_status (msg, src);
1469 /* The session's SoupMessage object expires after this callback returns. */
1471 g_main_loop_quit (src->loop);
1474 #define SOUP_HTTP_SRC_ERROR(src,soup_msg,cat,code,error_message) \
1475 GST_ELEMENT_ERROR ((src), cat, code, ("%s", error_message), \
1476 ("%s (%d), URL: %s, Redirect to: %s", (soup_msg)->reason_phrase, \
1477 (soup_msg)->status_code, (src)->location, GST_STR_NULL ((src)->redirection_uri)));
1480 gst_soup_http_src_parse_status (SoupMessage * msg, GstSoupHTTPSrc * src)
1482 if (msg->method == SOUP_METHOD_HEAD) {
1483 if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code))
1484 GST_DEBUG_OBJECT (src, "Ignoring error %d during HEAD request",
1486 } else if (SOUP_STATUS_IS_TRANSPORT_ERROR (msg->status_code)) {
1487 switch (msg->status_code) {
1488 case SOUP_STATUS_CANT_RESOLVE:
1489 case SOUP_STATUS_CANT_RESOLVE_PROXY:
1490 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, NOT_FOUND,
1491 _("Could not resolve server name."));
1492 src->ret = GST_FLOW_ERROR;
1494 case SOUP_STATUS_CANT_CONNECT:
1495 case SOUP_STATUS_CANT_CONNECT_PROXY:
1496 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ,
1497 _("Could not establish connection to server."));
1498 src->ret = GST_FLOW_ERROR;
1500 case SOUP_STATUS_SSL_FAILED:
1501 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ,
1502 _("Secure connection setup failed."));
1503 src->ret = GST_FLOW_ERROR;
1505 case SOUP_STATUS_IO_ERROR:
1506 if (src->max_retries == -1 || src->retry_count < src->max_retries) {
1509 src->ret = GST_FLOW_CUSTOM_ERROR;
1511 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, READ,
1512 _("A network error occured, or the server closed the connection "
1514 src->ret = GST_FLOW_ERROR;
1517 case SOUP_STATUS_MALFORMED:
1518 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, READ,
1519 _("Server sent bad data."));
1520 src->ret = GST_FLOW_ERROR;
1522 case SOUP_STATUS_CANCELLED:
1523 /* No error message when interrupted by program. */
1526 } else if (SOUP_STATUS_IS_CLIENT_ERROR (msg->status_code) ||
1527 SOUP_STATUS_IS_REDIRECTION (msg->status_code) ||
1528 SOUP_STATUS_IS_SERVER_ERROR (msg->status_code)) {
1529 /* Report HTTP error. */
1531 /* when content_size is unknown and we have just finished receiving
1532 * a body message, requests that go beyond the content limits will result
1533 * in an error. Here we convert those to EOS */
1534 if (msg->status_code == SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE &&
1535 src->have_body && !src->have_size) {
1536 GST_DEBUG_OBJECT (src, "Requested range out of limits and received full "
1537 "body, returning EOS");
1538 src->ret = GST_FLOW_EOS;
1542 /* FIXME: reason_phrase is not translated and not suitable for user
1543 * error dialog according to libsoup documentation.
1545 if (msg->status_code == SOUP_STATUS_NOT_FOUND) {
1546 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
1547 ("%s", msg->reason_phrase),
1548 ("%s (%d), URL: %s, Redirect to: %s", msg->reason_phrase,
1549 msg->status_code, src->location,
1550 GST_STR_NULL (src->redirection_uri)));
1551 } else if (msg->status_code == SOUP_STATUS_UNAUTHORIZED
1552 || msg->status_code == SOUP_STATUS_PAYMENT_REQUIRED
1553 || msg->status_code == SOUP_STATUS_FORBIDDEN
1554 || msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
1555 GST_ELEMENT_ERROR (src, RESOURCE, NOT_AUTHORIZED, ("%s",
1556 msg->reason_phrase), ("%s (%d), URL: %s, Redirect to: %s",
1557 msg->reason_phrase, msg->status_code, src->location,
1558 GST_STR_NULL (src->redirection_uri)));
1560 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1561 ("%s", msg->reason_phrase),
1562 ("%s (%d), URL: %s, Redirect to: %s", msg->reason_phrase,
1563 msg->status_code, src->location,
1564 GST_STR_NULL (src->redirection_uri)));
1566 src->ret = GST_FLOW_ERROR;
1571 gst_soup_http_src_build_message (GstSoupHTTPSrc * src, const gchar * method)
1573 g_return_val_if_fail (src->msg == NULL, FALSE);
1575 src->msg = soup_message_new (method, src->location);
1577 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1578 ("Error parsing URL."), ("URL: %s", src->location));
1581 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE;
1582 if (!src->keep_alive) {
1583 soup_message_headers_append (src->msg->request_headers, "Connection",
1586 if (src->iradio_mode) {
1587 soup_message_headers_append (src->msg->request_headers, "icy-metadata",
1593 for (cookie = src->cookies; *cookie != NULL; cookie++) {
1594 soup_message_headers_append (src->msg->request_headers, "Cookie",
1600 g_signal_connect (src->msg, "got_headers",
1601 G_CALLBACK (gst_soup_http_src_got_headers_cb), src);
1602 g_signal_connect (src->msg, "got_body",
1603 G_CALLBACK (gst_soup_http_src_got_body_cb), src);
1604 g_signal_connect (src->msg, "finished",
1605 G_CALLBACK (gst_soup_http_src_finished_cb), src);
1606 g_signal_connect (src->msg, "got_chunk",
1607 G_CALLBACK (gst_soup_http_src_got_chunk_cb), src);
1608 soup_message_set_flags (src->msg, SOUP_MESSAGE_OVERWRITE_CHUNKS |
1609 (src->automatic_redirect ? 0 : SOUP_MESSAGE_NO_REDIRECT));
1610 soup_message_set_chunk_allocator (src->msg,
1611 gst_soup_http_src_chunk_allocator, src, NULL);
1612 gst_soup_http_src_add_range_header (src, src->request_position,
1613 src->stop_position);
1615 gst_soup_http_src_add_extra_headers (src);
1620 static GstFlowReturn
1621 gst_soup_http_src_do_request (GstSoupHTTPSrc * src, const gchar * method,
1622 GstBuffer ** outbuf)
1624 /* If we're not OK, just go out of here */
1625 if (src->ret != GST_FLOW_OK) {
1626 GST_DEBUG_OBJECT (src, "Previous flow return not OK: %s",
1627 gst_flow_get_name (src->ret));
1631 GST_LOG_OBJECT (src, "Running request for method: %s", method);
1632 if (src->msg && (src->request_position != src->read_position)) {
1633 if (src->session_io_status == GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE) {
1634 gst_soup_http_src_add_range_header (src, src->request_position,
1635 src->stop_position);
1637 GST_DEBUG_OBJECT (src, "Seek from position %" G_GUINT64_FORMAT
1638 " to %" G_GUINT64_FORMAT ": requeueing connection request",
1639 src->read_position, src->request_position);
1640 gst_soup_http_src_cancel_message (src);
1644 if (!gst_soup_http_src_build_message (src, method)) {
1645 return GST_FLOW_ERROR;
1648 src->ret = GST_FLOW_CUSTOM_ERROR;
1649 src->outbuf = outbuf;
1651 if (src->interrupted) {
1652 GST_INFO_OBJECT (src, "interrupted");
1653 src->ret = GST_FLOW_FLUSHING;
1657 GST_INFO_OBJECT (src, "Reconnecting");
1658 if (!gst_soup_http_src_build_message (src, method)) {
1659 return GST_FLOW_ERROR;
1665 GST_DEBUG_OBJECT (src, "EOS reached");
1669 switch (src->session_io_status) {
1670 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE:
1671 GST_INFO_OBJECT (src, "Queueing connection request");
1672 gst_soup_http_src_queue_message (src);
1674 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_QUEUED:
1676 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING:
1677 gst_soup_http_src_session_unpause_message (src);
1679 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_CANCELLED:
1684 if (src->ret == GST_FLOW_CUSTOM_ERROR) {
1685 g_main_context_push_thread_default (src->context);
1686 g_main_loop_run (src->loop);
1687 g_main_context_pop_thread_default (src->context);
1690 } while (src->ret == GST_FLOW_CUSTOM_ERROR);
1692 /* Let the request finish if we had a stop position and are there */
1693 if (src->ret == GST_FLOW_OK && src->stop_position != -1
1694 && src->read_position >= src->stop_position) {
1696 gst_soup_http_src_session_unpause_message (src);
1697 g_main_context_push_thread_default (src->context);
1698 g_main_loop_run (src->loop);
1699 g_main_context_pop_thread_default (src->context);
1701 g_cond_signal (&src->request_finished_cond);
1702 /* Return OK unconditionally here, src->ret will
1703 * be most likely be EOS now but we want to
1704 * consume the buffer we got above */
1708 if (src->ret == GST_FLOW_CUSTOM_ERROR)
1709 src->ret = GST_FLOW_EOS;
1710 g_cond_signal (&src->request_finished_cond);
1712 /* basesrc assumes that we don't return a buffer if
1713 * something else than OK is returned. It will just
1714 * leak any buffer we might accidentially provide
1717 * This can potentially happen during flushing.
1719 if (src->ret != GST_FLOW_OK && outbuf && *outbuf) {
1720 gst_buffer_unref (*outbuf);
1727 static GstFlowReturn
1728 gst_soup_http_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
1730 GstSoupHTTPSrc *src;
1732 GstEvent *http_headers_event;
1734 src = GST_SOUP_HTTP_SRC (psrc);
1736 g_mutex_lock (&src->mutex);
1738 ret = gst_soup_http_src_do_request (src, SOUP_METHOD_GET, outbuf);
1739 http_headers_event = src->http_headers_event;
1740 src->http_headers_event = NULL;
1741 g_mutex_unlock (&src->mutex);
1743 if (http_headers_event)
1744 gst_pad_push_event (GST_BASE_SRC_PAD (src), http_headers_event);
1750 gst_soup_http_src_start (GstBaseSrc * bsrc)
1752 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1754 GST_DEBUG_OBJECT (src, "start(\"%s\")", src->location);
1756 return gst_soup_http_src_session_open (src);
1760 gst_soup_http_src_stop (GstBaseSrc * bsrc)
1762 GstSoupHTTPSrc *src;
1764 src = GST_SOUP_HTTP_SRC (bsrc);
1765 GST_DEBUG_OBJECT (src, "stop()");
1766 if (src->keep_alive && !src->msg)
1767 gst_soup_http_src_cancel_message (src);
1769 gst_soup_http_src_session_close (src);
1771 gst_soup_http_src_reset (src);
1775 static GstStateChangeReturn
1776 gst_soup_http_src_change_state (GstElement * element, GstStateChange transition)
1778 GstStateChangeReturn ret;
1779 GstSoupHTTPSrc *src;
1781 src = GST_SOUP_HTTP_SRC (element);
1783 switch (transition) {
1784 case GST_STATE_CHANGE_READY_TO_NULL:
1785 gst_soup_http_src_session_close (src);
1791 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1796 /* Interrupt a blocking request. */
1798 gst_soup_http_src_unlock (GstBaseSrc * bsrc)
1800 GstSoupHTTPSrc *src;
1802 src = GST_SOUP_HTTP_SRC (bsrc);
1803 GST_DEBUG_OBJECT (src, "unlock()");
1805 src->interrupted = TRUE;
1806 src->ret = GST_FLOW_FLUSHING;
1808 g_main_loop_quit (src->loop);
1809 g_cond_signal (&src->request_finished_cond);
1813 /* Interrupt interrupt. */
1815 gst_soup_http_src_unlock_stop (GstBaseSrc * bsrc)
1817 GstSoupHTTPSrc *src;
1819 src = GST_SOUP_HTTP_SRC (bsrc);
1820 GST_DEBUG_OBJECT (src, "unlock_stop()");
1822 src->interrupted = FALSE;
1823 src->ret = GST_FLOW_OK;
1828 gst_soup_http_src_get_size (GstBaseSrc * bsrc, guint64 * size)
1830 GstSoupHTTPSrc *src;
1832 src = GST_SOUP_HTTP_SRC (bsrc);
1834 if (src->have_size) {
1835 GST_DEBUG_OBJECT (src, "get_size() = %" G_GUINT64_FORMAT,
1837 *size = src->content_size;
1840 GST_DEBUG_OBJECT (src, "get_size() = FALSE");
1845 gst_soup_http_src_check_seekable (GstSoupHTTPSrc * src)
1847 GstFlowReturn ret = GST_FLOW_OK;
1849 /* Special case to check if the server allows range requests
1850 * before really starting to get data in the buffer creation
1853 if (!src->got_headers && GST_STATE (src) >= GST_STATE_PAUSED) {
1854 g_mutex_lock (&src->mutex);
1855 while (!src->got_headers && !src->interrupted && ret == GST_FLOW_OK) {
1856 if ((src->msg && src->msg->method != SOUP_METHOD_HEAD) &&
1857 src->session_io_status != GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE) {
1858 /* wait for the current request to finish */
1859 g_cond_wait (&src->request_finished_cond, &src->mutex);
1861 if (gst_soup_http_src_session_open (src)) {
1862 ret = gst_soup_http_src_do_request (src, SOUP_METHOD_HEAD, NULL);
1866 if (src->ret == GST_FLOW_EOS) {
1867 /* A HEAD request shouldn't lead to EOS */
1868 src->ret = GST_FLOW_OK;
1870 /* resets status to idle */
1871 gst_soup_http_src_cancel_message (src);
1872 g_mutex_unlock (&src->mutex);
1877 gst_soup_http_src_is_seekable (GstBaseSrc * bsrc)
1879 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1881 gst_soup_http_src_check_seekable (src);
1883 return src->seekable;
1887 gst_soup_http_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
1889 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1891 GST_DEBUG_OBJECT (src, "do_seek(%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1892 ")", segment->start, segment->stop);
1893 if (src->read_position == segment->start &&
1894 src->request_position == src->read_position &&
1895 src->stop_position == segment->stop) {
1896 GST_DEBUG_OBJECT (src,
1897 "Seek to current read/end position and no seek pending");
1901 gst_soup_http_src_check_seekable (src);
1903 /* If we have no headers we don't know yet if it is seekable or not.
1904 * Store the start position and error out later if it isn't */
1905 if (src->got_headers && !src->seekable) {
1906 GST_WARNING_OBJECT (src, "Not seekable");
1910 if (segment->rate < 0.0 || segment->format != GST_FORMAT_BYTES) {
1911 GST_WARNING_OBJECT (src, "Invalid seek segment");
1915 if (src->have_size && segment->start >= src->content_size) {
1916 GST_WARNING_OBJECT (src,
1917 "Potentially seeking behind end of file, might EOS immediately");
1920 /* Wait for create() to handle the jump in offset. */
1921 src->request_position = segment->start;
1922 src->stop_position = segment->stop;
1928 gst_soup_http_src_query (GstBaseSrc * bsrc, GstQuery * query)
1930 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1932 GstSchedulingFlags flags;
1933 gint minsize, maxsize, align;
1935 switch (GST_QUERY_TYPE (query)) {
1937 gst_query_set_uri (query, src->location);
1938 if (src->redirection_uri != NULL) {
1939 gst_query_set_uri_redirection (query, src->redirection_uri);
1940 gst_query_set_uri_redirection_permanent (query,
1941 src->redirection_permanent);
1951 ret = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
1953 switch (GST_QUERY_TYPE (query)) {
1954 case GST_QUERY_SCHEDULING:
1955 gst_query_parse_scheduling (query, &flags, &minsize, &maxsize, &align);
1956 flags |= GST_SCHEDULING_FLAG_BANDWIDTH_LIMITED;
1957 gst_query_set_scheduling (query, flags, minsize, maxsize, align);
1967 gst_soup_http_src_set_location (GstSoupHTTPSrc * src, const gchar * uri,
1970 const char *alt_schemes[] = { "icy://", "icyx://" };
1973 if (src->location) {
1974 g_free (src->location);
1975 src->location = NULL;
1981 for (i = 0; i < G_N_ELEMENTS (alt_schemes); i++) {
1982 if (g_str_has_prefix (uri, alt_schemes[i])) {
1984 g_strdup_printf ("http://%s", uri + strlen (alt_schemes[i]));
1989 if (src->redirection_uri) {
1990 g_free (src->redirection_uri);
1991 src->redirection_uri = NULL;
1994 src->location = g_strdup (uri);
2000 gst_soup_http_src_set_proxy (GstSoupHTTPSrc * src, const gchar * uri)
2003 soup_uri_free (src->proxy);
2006 if (g_str_has_prefix (uri, "http://")) {
2007 src->proxy = soup_uri_new (uri);
2009 gchar *new_uri = g_strconcat ("http://", uri, NULL);
2011 src->proxy = soup_uri_new (new_uri);
2019 gst_soup_http_src_uri_get_type (GType type)
2024 static const gchar *const *
2025 gst_soup_http_src_uri_get_protocols (GType type)
2027 static const gchar *protocols[] = { "http", "https", "icy", "icyx", NULL };
2033 gst_soup_http_src_uri_get_uri (GstURIHandler * handler)
2035 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (handler);
2037 /* FIXME: make thread-safe */
2038 return g_strdup (src->location);
2042 gst_soup_http_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
2045 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (handler);
2047 return gst_soup_http_src_set_location (src, uri, error);
2051 gst_soup_http_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
2053 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
2055 iface->get_type = gst_soup_http_src_uri_get_type;
2056 iface->get_protocols = gst_soup_http_src_uri_get_protocols;
2057 iface->get_uri = gst_soup_http_src_uri_get_uri;
2058 iface->set_uri = gst_soup_http_src_uri_set_uri;