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,
130 #define DEFAULT_USER_AGENT "GStreamer souphttpsrc "
131 #define DEFAULT_IRADIO_MODE TRUE
132 #define DEFAULT_SOUP_LOG_LEVEL SOUP_LOGGER_LOG_HEADERS
133 #define DEFAULT_COMPRESS FALSE
134 #define DEFAULT_KEEP_ALIVE FALSE
135 #define DEFAULT_SSL_STRICT TRUE
136 #define DEFAULT_SSL_CA_FILE NULL
137 #define DEFAULT_SSL_USE_SYSTEM_CA_FILE TRUE
138 #define DEFAULT_TLS_DATABASE NULL
139 #define DEFAULT_TIMEOUT 15
140 #define DEFAULT_RETRIES 3
141 #define DEFAULT_SOUP_METHOD NULL
143 static void gst_soup_http_src_uri_handler_init (gpointer g_iface,
144 gpointer iface_data);
145 static void gst_soup_http_src_finalize (GObject * gobject);
146 static void gst_soup_http_src_dispose (GObject * gobject);
148 static void gst_soup_http_src_set_property (GObject * object, guint prop_id,
149 const GValue * value, GParamSpec * pspec);
150 static void gst_soup_http_src_get_property (GObject * object, guint prop_id,
151 GValue * value, GParamSpec * pspec);
153 static GstStateChangeReturn gst_soup_http_src_change_state (GstElement *
154 element, GstStateChange transition);
155 static GstFlowReturn gst_soup_http_src_create (GstPushSrc * psrc,
156 GstBuffer ** outbuf);
157 static gboolean gst_soup_http_src_start (GstBaseSrc * bsrc);
158 static gboolean gst_soup_http_src_stop (GstBaseSrc * bsrc);
159 static gboolean gst_soup_http_src_get_size (GstBaseSrc * bsrc, guint64 * size);
160 static gboolean gst_soup_http_src_is_seekable (GstBaseSrc * bsrc);
161 static gboolean gst_soup_http_src_do_seek (GstBaseSrc * bsrc,
162 GstSegment * segment);
163 static gboolean gst_soup_http_src_query (GstBaseSrc * bsrc, GstQuery * query);
164 static gboolean gst_soup_http_src_unlock (GstBaseSrc * bsrc);
165 static gboolean gst_soup_http_src_unlock_stop (GstBaseSrc * bsrc);
166 static gboolean gst_soup_http_src_set_location (GstSoupHTTPSrc * src,
167 const gchar * uri, GError ** error);
168 static gboolean gst_soup_http_src_set_proxy (GstSoupHTTPSrc * src,
170 static char *gst_soup_http_src_unicodify (const char *str);
171 static gboolean gst_soup_http_src_build_message (GstSoupHTTPSrc * src,
172 const gchar * method);
173 static void gst_soup_http_src_cancel_message (GstSoupHTTPSrc * src);
174 static void gst_soup_http_src_queue_message (GstSoupHTTPSrc * src);
175 static gboolean gst_soup_http_src_add_range_header (GstSoupHTTPSrc * src,
176 guint64 offset, guint64 stop_offset);
177 static void gst_soup_http_src_session_unpause_message (GstSoupHTTPSrc * src);
178 static void gst_soup_http_src_session_pause_message (GstSoupHTTPSrc * src);
179 static gboolean gst_soup_http_src_session_open (GstSoupHTTPSrc * src);
180 static void gst_soup_http_src_session_close (GstSoupHTTPSrc * src);
181 static void gst_soup_http_src_parse_status (SoupMessage * msg,
182 GstSoupHTTPSrc * src);
183 static void gst_soup_http_src_chunk_free (gpointer gstbuf);
184 static SoupBuffer *gst_soup_http_src_chunk_allocator (SoupMessage * msg,
185 gsize max_len, gpointer user_data);
186 static void gst_soup_http_src_got_chunk_cb (SoupMessage * msg,
187 SoupBuffer * chunk, GstSoupHTTPSrc * src);
188 static void gst_soup_http_src_response_cb (SoupSession * session,
189 SoupMessage * msg, GstSoupHTTPSrc * src);
190 static void gst_soup_http_src_got_headers_cb (SoupMessage * msg,
191 GstSoupHTTPSrc * src);
192 static void gst_soup_http_src_got_body_cb (SoupMessage * msg,
193 GstSoupHTTPSrc * src);
194 static void gst_soup_http_src_finished_cb (SoupMessage * msg,
195 GstSoupHTTPSrc * src);
196 static void gst_soup_http_src_authenticate_cb (SoupSession * session,
197 SoupMessage * msg, SoupAuth * auth, gboolean retrying,
198 GstSoupHTTPSrc * src);
200 #define gst_soup_http_src_parent_class parent_class
201 G_DEFINE_TYPE_WITH_CODE (GstSoupHTTPSrc, gst_soup_http_src, GST_TYPE_PUSH_SRC,
202 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
203 gst_soup_http_src_uri_handler_init));
206 gst_soup_http_src_class_init (GstSoupHTTPSrcClass * klass)
208 GObjectClass *gobject_class;
209 GstElementClass *gstelement_class;
210 GstBaseSrcClass *gstbasesrc_class;
211 GstPushSrcClass *gstpushsrc_class;
213 gobject_class = (GObjectClass *) klass;
214 gstelement_class = (GstElementClass *) klass;
215 gstbasesrc_class = (GstBaseSrcClass *) klass;
216 gstpushsrc_class = (GstPushSrcClass *) klass;
218 gobject_class->set_property = gst_soup_http_src_set_property;
219 gobject_class->get_property = gst_soup_http_src_get_property;
220 gobject_class->finalize = gst_soup_http_src_finalize;
221 gobject_class->dispose = gst_soup_http_src_dispose;
223 g_object_class_install_property (gobject_class,
225 g_param_spec_string ("location", "Location",
226 "Location to read from", "",
227 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
228 g_object_class_install_property (gobject_class,
230 g_param_spec_string ("user-agent", "User-Agent",
231 "Value of the User-Agent HTTP request header field",
232 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
233 g_object_class_install_property (gobject_class,
234 PROP_AUTOMATIC_REDIRECT,
235 g_param_spec_boolean ("automatic-redirect", "automatic-redirect",
236 "Automatically follow HTTP redirects (HTTP Status Code 3xx)",
237 TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
238 g_object_class_install_property (gobject_class,
240 g_param_spec_string ("proxy", "Proxy",
241 "HTTP proxy server URI", "",
242 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
243 g_object_class_install_property (gobject_class,
245 g_param_spec_string ("user-id", "user-id",
246 "HTTP location URI user id for authentication", "",
247 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
248 g_object_class_install_property (gobject_class, PROP_USER_PW,
249 g_param_spec_string ("user-pw", "user-pw",
250 "HTTP location URI user password for authentication", "",
251 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
252 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
253 g_param_spec_string ("proxy-id", "proxy-id",
254 "HTTP proxy URI user id for authentication", "",
255 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
256 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
257 g_param_spec_string ("proxy-pw", "proxy-pw",
258 "HTTP proxy URI user password for authentication", "",
259 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
260 g_object_class_install_property (gobject_class, PROP_COOKIES,
261 g_param_spec_boxed ("cookies", "Cookies", "HTTP request cookies",
262 G_TYPE_STRV, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
263 g_object_class_install_property (gobject_class, PROP_IS_LIVE,
264 g_param_spec_boolean ("is-live", "is-live", "Act like a live source",
265 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
266 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
267 g_param_spec_uint ("timeout", "timeout",
268 "Value in seconds to timeout a blocking I/O (0 = No timeout).", 0,
269 3600, DEFAULT_TIMEOUT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
270 g_object_class_install_property (gobject_class, PROP_EXTRA_HEADERS,
271 g_param_spec_boxed ("extra-headers", "Extra Headers",
272 "Extra headers to append to the HTTP request",
273 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
274 g_object_class_install_property (gobject_class, PROP_IRADIO_MODE,
275 g_param_spec_boolean ("iradio-mode", "iradio-mode",
276 "Enable internet radio mode (ask server to send shoutcast/icecast "
277 "metadata interleaved with the actual stream data)",
278 DEFAULT_IRADIO_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
281 * GstSoupHTTPSrc::http-log-level:
283 * If set and > 0, captures and dumps HTTP session data as
284 * log messages if log level >= GST_LEVEL_TRACE
288 g_object_class_install_property (gobject_class, PROP_SOUP_LOG_LEVEL,
289 g_param_spec_enum ("http-log-level", "HTTP log level",
290 "Set log level for soup's HTTP session log",
291 SOUP_TYPE_LOGGER_LOG_LEVEL, DEFAULT_SOUP_LOG_LEVEL,
292 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
295 * GstSoupHTTPSrc::compress:
297 * If set to %TRUE, souphttpsrc will automatically handle gzip
298 * and deflate Content-Encodings. This does not make much difference
299 * and causes more load for normal media files, but makes a real
300 * difference in size for plaintext files.
304 g_object_class_install_property (gobject_class, PROP_COMPRESS,
305 g_param_spec_boolean ("compress", "Compress",
306 "Allow compressed content encodings",
307 DEFAULT_COMPRESS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
310 * GstSoupHTTPSrc::keep-alive:
312 * If set to %TRUE, souphttpsrc will keep alive connections when being
313 * set to READY state and only will close connections when connecting
314 * to a different server or when going to NULL state..
318 g_object_class_install_property (gobject_class, PROP_KEEP_ALIVE,
319 g_param_spec_boolean ("keep-alive", "keep-alive",
320 "Use HTTP persistent connections", DEFAULT_KEEP_ALIVE,
321 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
324 * GstSoupHTTPSrc::ssl-strict:
326 * If set to %TRUE, souphttpsrc will reject all SSL certificates that
327 * are considered invalid.
331 g_object_class_install_property (gobject_class, PROP_SSL_STRICT,
332 g_param_spec_boolean ("ssl-strict", "SSL Strict",
333 "Strict SSL certificate checking", DEFAULT_SSL_STRICT,
334 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
337 * GstSoupHTTPSrc::ssl-ca-file:
339 * A SSL anchor CA file that should be used for checking certificates
340 * instead of the system CA file.
342 * If this property is non-%NULL, #GstSoupHTTPSrc::ssl-use-system-ca-file
343 * value will be ignored.
345 * Deprecated: Use #GstSoupHTTPSrc::tls-database property instead.
348 g_object_class_install_property (gobject_class, PROP_SSL_CA_FILE,
349 g_param_spec_string ("ssl-ca-file", "SSL CA File",
350 "Location of a SSL anchor CA file to use", DEFAULT_SSL_CA_FILE,
351 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
354 * GstSoupHTTPSrc::ssl-use-system-ca-file:
356 * If set to %TRUE, souphttpsrc will use the system's CA file for
357 * checking certificates, unless #GstSoupHTTPSrc::ssl-ca-file or
358 * #GstSoupHTTPSrc::tls-database are non-%NULL.
362 g_object_class_install_property (gobject_class, PROP_SSL_USE_SYSTEM_CA_FILE,
363 g_param_spec_boolean ("ssl-use-system-ca-file", "Use System CA File",
364 "Use system CA file", DEFAULT_SSL_USE_SYSTEM_CA_FILE,
365 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
368 * GstSoupHTTPSrc::tls-database:
370 * TLS database with anchor certificate authorities used to validate
371 * the server certificate.
373 * If this property is non-%NULL, #GstSoupHTTPSrc::ssl-use-system-ca-file
374 * and #GstSoupHTTPSrc::ssl-ca-file values will be ignored.
378 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
379 g_param_spec_object ("tls-database", "TLS database",
380 "TLS database with anchor certificate authorities used to validate the server certificate",
381 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
384 * GstSoupHTTPSrc::retries:
386 * Maximum number of retries until giving up.
390 g_object_class_install_property (gobject_class, PROP_RETRIES,
391 g_param_spec_int ("retries", "Retries",
392 "Maximum number of retries until giving up (-1=infinite)", -1,
393 G_MAXINT, DEFAULT_RETRIES,
394 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
397 * GstSoupHTTPSrc::method
399 * The HTTP method to use when making a request
403 g_object_class_install_property (gobject_class, PROP_METHOD,
404 g_param_spec_string ("method", "HTTP method",
405 "The HTTP method to use (GET, HEAD, OPTIONS, etc)",
406 DEFAULT_SOUP_METHOD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
408 gst_element_class_add_pad_template (gstelement_class,
409 gst_static_pad_template_get (&srctemplate));
411 gst_element_class_set_static_metadata (gstelement_class, "HTTP client source",
413 "Receive data as a client over the network via HTTP using SOUP",
414 "Wouter Cloetens <wouter@mind.be>");
415 gstelement_class->change_state =
416 GST_DEBUG_FUNCPTR (gst_soup_http_src_change_state);
418 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_soup_http_src_start);
419 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_soup_http_src_stop);
420 gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_soup_http_src_unlock);
421 gstbasesrc_class->unlock_stop =
422 GST_DEBUG_FUNCPTR (gst_soup_http_src_unlock_stop);
423 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_soup_http_src_get_size);
424 gstbasesrc_class->is_seekable =
425 GST_DEBUG_FUNCPTR (gst_soup_http_src_is_seekable);
426 gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_soup_http_src_do_seek);
427 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_soup_http_src_query);
429 gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_soup_http_src_create);
431 GST_DEBUG_CATEGORY_INIT (souphttpsrc_debug, "souphttpsrc", 0,
436 gst_soup_http_src_reset (GstSoupHTTPSrc * src)
438 src->interrupted = FALSE;
440 src->retry_count = 0;
441 src->have_size = FALSE;
442 src->got_headers = FALSE;
443 src->seekable = FALSE;
444 src->read_position = 0;
445 src->request_position = 0;
446 src->stop_position = -1;
447 src->content_size = 0;
448 src->have_body = FALSE;
450 src->ret = GST_FLOW_OK;
452 gst_caps_replace (&src->src_caps, NULL);
453 g_free (src->iradio_name);
454 src->iradio_name = NULL;
455 g_free (src->iradio_genre);
456 src->iradio_genre = NULL;
457 g_free (src->iradio_url);
458 src->iradio_url = NULL;
462 gst_soup_http_src_init (GstSoupHTTPSrc * src)
466 g_mutex_init (&src->mutex);
467 g_cond_init (&src->request_finished_cond);
468 src->location = NULL;
469 src->redirection_uri = NULL;
470 src->automatic_redirect = TRUE;
471 src->user_agent = g_strdup (DEFAULT_USER_AGENT);
474 src->proxy_id = NULL;
475 src->proxy_pw = NULL;
477 src->iradio_mode = DEFAULT_IRADIO_MODE;
482 src->timeout = DEFAULT_TIMEOUT;
483 src->log_level = DEFAULT_SOUP_LOG_LEVEL;
484 src->ssl_strict = DEFAULT_SSL_STRICT;
485 src->ssl_use_system_ca_file = DEFAULT_SSL_USE_SYSTEM_CA_FILE;
486 src->tls_database = DEFAULT_TLS_DATABASE;
487 src->max_retries = DEFAULT_RETRIES;
488 src->method = DEFAULT_SOUP_METHOD;
489 proxy = g_getenv ("http_proxy");
490 if (!gst_soup_http_src_set_proxy (src, proxy)) {
491 GST_WARNING_OBJECT (src,
492 "The proxy in the http_proxy env var (\"%s\") cannot be parsed.",
496 gst_base_src_set_automatic_eos (GST_BASE_SRC (src), FALSE);
498 gst_soup_http_src_reset (src);
502 gst_soup_http_src_dispose (GObject * gobject)
504 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (gobject);
506 GST_DEBUG_OBJECT (src, "dispose");
508 gst_soup_http_src_session_close (src);
510 G_OBJECT_CLASS (parent_class)->dispose (gobject);
514 gst_soup_http_src_finalize (GObject * gobject)
516 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (gobject);
518 GST_DEBUG_OBJECT (src, "finalize");
520 g_mutex_clear (&src->mutex);
521 g_cond_clear (&src->request_finished_cond);
522 g_free (src->location);
523 if (src->redirection_uri) {
524 g_free (src->redirection_uri);
526 g_free (src->user_agent);
527 if (src->proxy != NULL) {
528 soup_uri_free (src->proxy);
530 g_free (src->user_id);
531 g_free (src->user_pw);
532 g_free (src->proxy_id);
533 g_free (src->proxy_pw);
534 g_strfreev (src->cookies);
536 if (src->extra_headers) {
537 gst_structure_free (src->extra_headers);
538 src->extra_headers = NULL;
541 g_free (src->ssl_ca_file);
543 if (src->tls_database)
544 g_object_unref (src->tls_database);
545 g_free (src->method);
547 G_OBJECT_CLASS (parent_class)->finalize (gobject);
551 gst_soup_http_src_set_property (GObject * object, guint prop_id,
552 const GValue * value, GParamSpec * pspec)
554 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (object);
559 const gchar *location;
561 location = g_value_get_string (value);
563 if (location == NULL) {
564 GST_WARNING ("location property cannot be NULL");
567 if (!gst_soup_http_src_set_location (src, location, NULL)) {
568 GST_WARNING ("badly formatted location");
573 case PROP_USER_AGENT:
575 g_free (src->user_agent);
576 src->user_agent = g_value_dup_string (value);
578 case PROP_IRADIO_MODE:
579 src->iradio_mode = g_value_get_boolean (value);
581 case PROP_AUTOMATIC_REDIRECT:
582 src->automatic_redirect = g_value_get_boolean (value);
588 proxy = g_value_get_string (value);
589 if (!gst_soup_http_src_set_proxy (src, proxy)) {
590 GST_WARNING ("badly formatted proxy URI");
596 g_strfreev (src->cookies);
597 src->cookies = g_strdupv (g_value_get_boxed (value));
600 gst_base_src_set_live (GST_BASE_SRC (src), g_value_get_boolean (value));
604 g_free (src->user_id);
605 src->user_id = g_value_dup_string (value);
609 g_free (src->user_pw);
610 src->user_pw = g_value_dup_string (value);
614 g_free (src->proxy_id);
615 src->proxy_id = g_value_dup_string (value);
619 g_free (src->proxy_pw);
620 src->proxy_pw = g_value_dup_string (value);
623 src->timeout = g_value_get_uint (value);
625 case PROP_EXTRA_HEADERS:{
626 const GstStructure *s = gst_value_get_structure (value);
628 if (src->extra_headers)
629 gst_structure_free (src->extra_headers);
631 src->extra_headers = s ? gst_structure_copy (s) : NULL;
634 case PROP_SOUP_LOG_LEVEL:
635 src->log_level = g_value_get_enum (value);
638 src->compress = g_value_get_boolean (value);
640 case PROP_KEEP_ALIVE:
641 src->keep_alive = g_value_get_boolean (value);
643 case PROP_SSL_STRICT:
644 src->ssl_strict = g_value_get_boolean (value);
646 case PROP_SSL_CA_FILE:
647 if (src->ssl_ca_file)
648 g_free (src->ssl_ca_file);
649 src->ssl_ca_file = g_value_dup_string (value);
651 case PROP_SSL_USE_SYSTEM_CA_FILE:
652 src->ssl_use_system_ca_file = g_value_get_boolean (value);
654 case PROP_TLS_DATABASE:
655 g_clear_object (&src->tls_database);
656 src->tls_database = g_value_dup_object (value);
659 src->max_retries = g_value_get_int (value);
662 g_free (src->method);
663 src->method = g_value_dup_string (value);
666 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
674 gst_soup_http_src_get_property (GObject * object, guint prop_id,
675 GValue * value, GParamSpec * pspec)
677 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (object);
681 g_value_set_string (value, src->location);
683 case PROP_USER_AGENT:
684 g_value_set_string (value, src->user_agent);
686 case PROP_AUTOMATIC_REDIRECT:
687 g_value_set_boolean (value, src->automatic_redirect);
690 if (src->proxy == NULL)
691 g_value_set_static_string (value, "");
693 char *proxy = soup_uri_to_string (src->proxy, FALSE);
695 g_value_set_string (value, proxy);
700 g_value_set_boxed (value, g_strdupv (src->cookies));
703 g_value_set_boolean (value, gst_base_src_is_live (GST_BASE_SRC (src)));
705 case PROP_IRADIO_MODE:
706 g_value_set_boolean (value, src->iradio_mode);
709 g_value_set_string (value, src->user_id);
712 g_value_set_string (value, src->user_pw);
715 g_value_set_string (value, src->proxy_id);
718 g_value_set_string (value, src->proxy_pw);
721 g_value_set_uint (value, src->timeout);
723 case PROP_EXTRA_HEADERS:
724 gst_value_set_structure (value, src->extra_headers);
726 case PROP_SOUP_LOG_LEVEL:
727 g_value_set_enum (value, src->log_level);
730 g_value_set_boolean (value, src->compress);
732 case PROP_KEEP_ALIVE:
733 g_value_set_boolean (value, src->keep_alive);
735 case PROP_SSL_STRICT:
736 g_value_set_boolean (value, src->ssl_strict);
738 case PROP_SSL_CA_FILE:
739 g_value_set_string (value, src->ssl_ca_file);
741 case PROP_SSL_USE_SYSTEM_CA_FILE:
742 g_value_set_boolean (value, src->ssl_use_system_ca_file);
744 case PROP_TLS_DATABASE:
745 g_value_set_object (value, src->tls_database);
748 g_value_set_int (value, src->max_retries);
751 g_value_set_string (value, src->method);
754 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
760 gst_soup_http_src_unicodify (const gchar * str)
762 const gchar *env_vars[] = { "GST_ICY_TAG_ENCODING",
763 "GST_TAG_ENCODING", NULL
766 return gst_tag_freeform_string_to_utf8 (str, -1, env_vars);
770 gst_soup_http_src_cancel_message (GstSoupHTTPSrc * src)
772 if (src->msg != NULL) {
773 GST_INFO_OBJECT (src, "Cancelling message");
774 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_CANCELLED;
775 soup_session_cancel_message (src->session, src->msg, SOUP_STATUS_CANCELLED);
777 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE;
782 gst_soup_http_src_queue_message (GstSoupHTTPSrc * src)
784 soup_session_queue_message (src->session, src->msg,
785 (SoupSessionCallback) gst_soup_http_src_response_cb, src);
786 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_QUEUED;
790 gst_soup_http_src_add_range_header (GstSoupHTTPSrc * src, guint64 offset,
797 soup_message_headers_remove (src->msg->request_headers, "Range");
798 if (offset || stop_offset != -1) {
799 if (stop_offset != -1) {
800 /* FIXME: If stop_offset == 0, this will still download a single byte */
801 rc = g_snprintf (buf, sizeof (buf), "bytes=%" G_GUINT64_FORMAT "-%"
802 G_GUINT64_FORMAT, offset, (stop_offset > 0) ? stop_offset - 1 :
805 rc = g_snprintf (buf, sizeof (buf), "bytes=%" G_GUINT64_FORMAT "-",
808 if (rc > sizeof (buf) || rc < 0)
810 soup_message_headers_append (src->msg->request_headers, "Range", buf);
812 src->read_position = offset;
817 _append_extra_header (GQuark field_id, const GValue * value, gpointer user_data)
819 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (user_data);
820 const gchar *field_name = g_quark_to_string (field_id);
821 gchar *field_content = NULL;
823 if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
824 field_content = g_value_dup_string (value);
826 GValue dest = { 0, };
828 g_value_init (&dest, G_TYPE_STRING);
829 if (g_value_transform (value, &dest)) {
830 field_content = g_value_dup_string (&dest);
834 if (field_content == NULL) {
835 GST_ERROR_OBJECT (src, "extra-headers field '%s' contains no value "
836 "or can't be converted to a string", field_name);
840 GST_DEBUG_OBJECT (src, "Appending extra header: \"%s: %s\"", field_name,
842 soup_message_headers_append (src->msg->request_headers, field_name,
845 g_free (field_content);
851 _append_extra_headers (GQuark field_id, const GValue * value,
854 if (G_VALUE_TYPE (value) == GST_TYPE_ARRAY) {
855 guint n = gst_value_array_get_size (value);
858 for (i = 0; i < n; i++) {
859 const GValue *v = gst_value_array_get_value (value, i);
861 if (!_append_extra_header (field_id, v, user_data))
864 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
865 guint n = gst_value_list_get_size (value);
868 for (i = 0; i < n; i++) {
869 const GValue *v = gst_value_list_get_value (value, i);
871 if (!_append_extra_header (field_id, v, user_data))
875 return _append_extra_header (field_id, value, user_data);
883 gst_soup_http_src_add_extra_headers (GstSoupHTTPSrc * src)
885 if (!src->extra_headers)
888 return gst_structure_foreach (src->extra_headers, _append_extra_headers, src);
893 gst_soup_http_src_session_unpause_message (GstSoupHTTPSrc * src)
895 soup_session_unpause_message (src->session, src->msg);
899 gst_soup_http_src_session_pause_message (GstSoupHTTPSrc * src)
901 soup_session_pause_message (src->session, src->msg);
905 gst_soup_http_src_session_open (GstSoupHTTPSrc * src)
908 GST_DEBUG_OBJECT (src, "Session is already open");
912 if (!src->location) {
913 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (_("No URL set.")),
914 ("Missing location property"));
919 src->context = g_main_context_new ();
922 src->loop = g_main_loop_new (src->context, TRUE);
924 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
925 (NULL), ("Failed to start GMainLoop"));
926 g_main_context_unref (src->context);
931 GST_DEBUG_OBJECT (src, "Creating session");
932 if (src->proxy == NULL) {
934 soup_session_async_new_with_options (SOUP_SESSION_ASYNC_CONTEXT,
935 src->context, SOUP_SESSION_USER_AGENT, src->user_agent,
936 SOUP_SESSION_TIMEOUT, src->timeout,
937 SOUP_SESSION_SSL_STRICT, src->ssl_strict,
938 SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_PROXY_RESOLVER_DEFAULT,
942 soup_session_async_new_with_options (SOUP_SESSION_ASYNC_CONTEXT,
943 src->context, SOUP_SESSION_PROXY_URI, src->proxy,
944 SOUP_SESSION_TIMEOUT, src->timeout,
945 SOUP_SESSION_SSL_STRICT, src->ssl_strict,
946 SOUP_SESSION_USER_AGENT, src->user_agent, NULL);
950 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
951 (NULL), ("Failed to create async session"));
955 g_signal_connect (src->session, "authenticate",
956 G_CALLBACK (gst_soup_http_src_authenticate_cb), src);
959 gst_soup_util_log_setup (src->session, src->log_level, GST_ELEMENT (src));
960 if (src->tls_database)
961 g_object_set (src->session, "tls-database", src->tls_database, NULL);
962 else if (src->ssl_ca_file)
963 g_object_set (src->session, "ssl-ca-file", src->ssl_ca_file, NULL);
965 g_object_set (src->session, "ssl-use-system-ca-file",
966 src->ssl_use_system_ca_file, NULL);
968 GST_DEBUG_OBJECT (src, "Re-using session");
972 soup_session_add_feature_by_type (src->session, SOUP_TYPE_CONTENT_DECODER);
974 soup_session_remove_feature_by_type (src->session,
975 SOUP_TYPE_CONTENT_DECODER);
980 #ifdef LIBSOUP_DOES_NOT_STEAL_OUR_CONTEXT
982 dummy_idle_cb (gpointer data)
984 return FALSE /* Idle source is removed */ ;
989 gst_soup_http_src_session_close (GstSoupHTTPSrc * src)
991 GST_DEBUG_OBJECT (src, "Closing session");
994 g_main_loop_quit (src->loop);
996 g_mutex_lock (&src->mutex);
998 soup_session_abort (src->session); /* This unrefs the message. */
999 g_object_unref (src->session);
1000 src->session = NULL;
1004 #ifdef LIBSOUP_DOES_NOT_STEAL_OUR_CONTEXT
1005 GSource *idle_source;
1007 /* Iterating the main context to give GIO cancellables a chance
1008 * to initiate cleanups. Wihout this, resources allocated by
1009 * libsoup for the connection are not released and socket fd is
1011 idle_source = g_idle_source_new ();
1012 /* Suppressing "idle souce without callback" warning */
1013 g_source_set_callback (idle_source, dummy_idle_cb, NULL, NULL);
1014 g_source_set_priority (idle_source, G_PRIORITY_LOW);
1015 g_source_attach (idle_source, src->context);
1016 /* Acquiring the context. Idle source guarantees that we'll not block. */
1017 g_main_context_push_thread_default (src->context);
1018 g_main_context_iteration (src->context, TRUE);
1019 /* Ensuring that there's no unhandled pending events left. */
1020 while (g_main_context_iteration (src->context, FALSE));
1021 g_main_context_pop_thread_default (src->context);
1022 g_source_unref (idle_source);
1025 g_main_loop_unref (src->loop);
1026 g_main_context_unref (src->context);
1028 src->context = NULL;
1030 g_mutex_unlock (&src->mutex);
1034 gst_soup_http_src_authenticate_cb (SoupSession * session, SoupMessage * msg,
1035 SoupAuth * auth, gboolean retrying, GstSoupHTTPSrc * src)
1038 /* First time authentication only, if we fail and are called again with retry true fall through */
1039 if (msg->status_code == SOUP_STATUS_UNAUTHORIZED) {
1040 if (src->user_id && src->user_pw)
1041 soup_auth_authenticate (auth, src->user_id, src->user_pw);
1042 } else if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
1043 if (src->proxy_id && src->proxy_pw)
1044 soup_auth_authenticate (auth, src->proxy_id, src->proxy_pw);
1050 insert_http_header (const gchar * name, const gchar * value, gpointer user_data)
1052 GstStructure *headers = user_data;
1055 gv = gst_structure_get_value (headers, name);
1056 if (gv && GST_VALUE_HOLDS_ARRAY (gv)) {
1057 GValue v = G_VALUE_INIT;
1059 g_value_init (&v, G_TYPE_STRING);
1060 g_value_set_string (&v, value);
1061 gst_value_array_append_value ((GValue *) gv, &v);
1063 } else if (gv && G_VALUE_HOLDS_STRING (gv)) {
1064 GValue arr = G_VALUE_INIT;
1065 GValue v = G_VALUE_INIT;
1066 const gchar *old_value = g_value_get_string (gv);
1068 g_value_init (&arr, GST_TYPE_ARRAY);
1069 g_value_init (&v, G_TYPE_STRING);
1070 g_value_set_string (&v, old_value);
1071 gst_value_array_append_value (&arr, &v);
1072 g_value_set_string (&v, value);
1073 gst_value_array_append_value (&arr, &v);
1075 gst_structure_set_value (headers, name, &arr);
1077 g_value_unset (&arr);
1079 gst_structure_set (headers, name, G_TYPE_STRING, value, NULL);
1084 gst_soup_http_src_got_headers_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
1087 GstTagList *tag_list;
1088 GstBaseSrc *basesrc;
1090 GHashTable *params = NULL;
1091 GstEvent *http_headers_event;
1092 GstStructure *http_headers, *headers;
1093 const gchar *accept_ranges;
1095 GST_INFO_OBJECT (src, "got headers");
1097 if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED &&
1098 src->proxy_id && src->proxy_pw)
1101 if (src->automatic_redirect && SOUP_STATUS_IS_REDIRECTION (msg->status_code)) {
1102 src->redirection_uri = g_strdup (soup_message_headers_get_one
1103 (msg->response_headers, "Location"));
1104 src->redirection_permanent =
1105 (msg->status_code == SOUP_STATUS_MOVED_PERMANENTLY);
1106 GST_DEBUG_OBJECT (src, "%u redirect to \"%s\" (permanent %d)",
1107 msg->status_code, src->redirection_uri, src->redirection_permanent);
1111 if (msg->status_code == SOUP_STATUS_UNAUTHORIZED)
1114 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING;
1115 src->got_headers = TRUE;
1117 http_headers = gst_structure_new_empty ("http-headers");
1118 gst_structure_set (http_headers, "uri", G_TYPE_STRING, src->location, NULL);
1119 if (src->redirection_uri)
1120 gst_structure_set (http_headers, "redirection-uri", G_TYPE_STRING,
1121 src->redirection_uri, NULL);
1122 headers = gst_structure_new_empty ("request-headers");
1123 soup_message_headers_foreach (msg->request_headers, insert_http_header,
1125 gst_structure_set (http_headers, "request-headers", GST_TYPE_STRUCTURE,
1127 gst_structure_free (headers);
1128 headers = gst_structure_new_empty ("response-headers");
1129 soup_message_headers_foreach (msg->response_headers, insert_http_header,
1131 gst_structure_set (http_headers, "response-headers", GST_TYPE_STRUCTURE,
1133 gst_structure_free (headers);
1135 http_headers_event =
1136 gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_STICKY, http_headers);
1137 gst_event_replace (&src->http_headers_event, http_headers_event);
1138 gst_event_unref (http_headers_event);
1140 /* Parse Content-Length. */
1141 if (soup_message_headers_get_encoding (msg->response_headers) ==
1142 SOUP_ENCODING_CONTENT_LENGTH) {
1143 newsize = src->request_position +
1144 soup_message_headers_get_content_length (msg->response_headers);
1145 if (!src->have_size || (src->content_size != newsize)) {
1146 src->content_size = newsize;
1147 src->have_size = TRUE;
1148 src->seekable = TRUE;
1149 GST_DEBUG_OBJECT (src, "size = %" G_GUINT64_FORMAT, src->content_size);
1151 basesrc = GST_BASE_SRC_CAST (src);
1152 basesrc->segment.duration = src->content_size;
1153 gst_element_post_message (GST_ELEMENT (src),
1154 gst_message_new_duration_changed (GST_OBJECT (src)));
1158 /* If the server reports Accept-Ranges: none we don't have to try
1159 * doing range requests at all
1161 if ((accept_ranges =
1162 soup_message_headers_get_one (msg->response_headers,
1163 "Accept-Ranges"))) {
1164 if (g_ascii_strcasecmp (accept_ranges, "none") == 0)
1165 src->seekable = FALSE;
1169 tag_list = gst_tag_list_new_empty ();
1172 soup_message_headers_get_one (msg->response_headers,
1173 "icy-metaint")) != NULL) {
1174 gint icy_metaint = atoi (value);
1176 GST_DEBUG_OBJECT (src, "icy-metaint: %s (parsed: %d)", value, icy_metaint);
1177 if (icy_metaint > 0) {
1179 gst_caps_unref (src->src_caps);
1181 src->src_caps = gst_caps_new_simple ("application/x-icy",
1182 "metadata-interval", G_TYPE_INT, icy_metaint, NULL);
1184 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
1188 soup_message_headers_get_content_type (msg->response_headers,
1189 ¶ms)) != NULL) {
1190 GST_DEBUG_OBJECT (src, "Content-Type: %s", value);
1191 if (g_ascii_strcasecmp (value, "audio/L16") == 0) {
1197 gst_caps_unref (src->src_caps);
1199 param = g_hash_table_lookup (params, "channels");
1201 channels = atol (param);
1203 param = g_hash_table_lookup (params, "rate");
1205 rate = atol (param);
1207 src->src_caps = gst_caps_new_simple ("audio/x-raw",
1208 "format", G_TYPE_STRING, "S16BE",
1209 "layout", G_TYPE_STRING, "interleaved",
1210 "channels", G_TYPE_INT, channels, "rate", G_TYPE_INT, rate, NULL);
1212 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
1214 /* Set the Content-Type field on the caps */
1215 if (src->src_caps) {
1216 src->src_caps = gst_caps_make_writable (src->src_caps);
1217 gst_caps_set_simple (src->src_caps, "content-type", G_TYPE_STRING,
1219 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
1225 g_hash_table_destroy (params);
1228 soup_message_headers_get_one (msg->response_headers,
1229 "icy-name")) != NULL) {
1230 g_free (src->iradio_name);
1231 src->iradio_name = gst_soup_http_src_unicodify (value);
1232 if (src->iradio_name) {
1233 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_ORGANIZATION,
1234 src->iradio_name, NULL);
1238 soup_message_headers_get_one (msg->response_headers,
1239 "icy-genre")) != NULL) {
1240 g_free (src->iradio_genre);
1241 src->iradio_genre = gst_soup_http_src_unicodify (value);
1242 if (src->iradio_genre) {
1243 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_GENRE,
1244 src->iradio_genre, NULL);
1247 if ((value = soup_message_headers_get_one (msg->response_headers, "icy-url"))
1249 g_free (src->iradio_url);
1250 src->iradio_url = gst_soup_http_src_unicodify (value);
1251 if (src->iradio_url) {
1252 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_LOCATION,
1253 src->iradio_url, NULL);
1256 if (!gst_tag_list_is_empty (tag_list)) {
1257 GST_DEBUG_OBJECT (src,
1258 "calling gst_element_found_tags with %" GST_PTR_FORMAT, tag_list);
1259 gst_pad_push_event (GST_BASE_SRC_PAD (src), gst_event_new_tag (tag_list));
1261 gst_tag_list_unref (tag_list);
1264 /* Handle HTTP errors. */
1265 gst_soup_http_src_parse_status (msg, src);
1267 /* Check if Range header was respected. */
1268 if (src->ret == GST_FLOW_CUSTOM_ERROR &&
1269 src->read_position && msg->status_code != SOUP_STATUS_PARTIAL_CONTENT) {
1270 src->seekable = FALSE;
1271 GST_ELEMENT_ERROR (src, RESOURCE, SEEK,
1272 (_("Server does not support seeking.")),
1273 ("Server does not accept Range HTTP header, URL: %s, Redirect to: %s",
1274 src->location, GST_STR_NULL (src->redirection_uri)));
1275 src->ret = GST_FLOW_ERROR;
1278 /* If we are going to error out, stop all processing right here, so we
1279 * don't output any data (such as an error html page), and return
1280 * GST_FLOW_ERROR from the create function instead of having
1281 * got_chunk_cb overwrite src->ret with FLOW_OK again. */
1282 if (src->ret == GST_FLOW_ERROR || src->ret == GST_FLOW_EOS) {
1283 gst_soup_http_src_session_pause_message (src);
1286 g_main_loop_quit (src->loop);
1288 g_cond_signal (&src->request_finished_cond);
1291 /* Have body. Signal EOS. */
1293 gst_soup_http_src_got_body_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
1295 if (G_UNLIKELY (msg != src->msg)) {
1296 GST_DEBUG_OBJECT (src, "got body, but not for current message");
1299 if (G_UNLIKELY (src->session_io_status !=
1300 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
1301 /* Probably a redirect. */
1304 GST_DEBUG_OBJECT (src, "got body");
1305 src->ret = GST_FLOW_EOS;
1306 src->have_body = TRUE;
1308 /* no need to interrupt the message here, we do it on the
1309 * finished_cb anyway if needed. And getting the body might mean
1310 * that the connection was hang up before finished. This happens when
1311 * the pipeline is stalled for too long (long pauses during playback).
1312 * Best to let it continue from here and pause because it reached the
1313 * final bytes based on content_size or received an out of range error */
1316 /* Finished. Signal EOS. */
1318 gst_soup_http_src_finished_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
1320 if (G_UNLIKELY (msg != src->msg)) {
1321 GST_DEBUG_OBJECT (src, "finished, but not for current message");
1324 GST_INFO_OBJECT (src, "finished, io status: %d", src->session_io_status);
1325 src->ret = GST_FLOW_EOS;
1326 if (src->session_io_status == GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_CANCELLED) {
1327 /* gst_soup_http_src_cancel_message() triggered this; probably a seek
1328 * that occurred in the QUEUEING state; i.e. before the connection setup
1329 * was complete. Do nothing */
1330 GST_DEBUG_OBJECT (src, "cancelled");
1331 } else if (src->session_io_status ==
1332 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING && src->read_position > 0 &&
1333 (src->have_size && src->read_position < src->content_size) &&
1334 (src->max_retries == -1 || src->retry_count < src->max_retries)) {
1335 /* The server disconnected while streaming. Reconnect and seeking to the
1339 src->ret = GST_FLOW_CUSTOM_ERROR;
1340 } else if (G_UNLIKELY (src->session_io_status !=
1341 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
1342 if (msg->method == SOUP_METHOD_HEAD) {
1343 GST_DEBUG_OBJECT (src, "Ignoring error %d:%s during HEAD request",
1344 msg->status_code, msg->reason_phrase);
1346 gst_soup_http_src_parse_status (msg, src);
1350 g_main_loop_quit (src->loop);
1351 g_cond_signal (&src->request_finished_cond);
1354 /* Buffer lifecycle management.
1356 * gst_soup_http_src_create() runs the GMainLoop for this element, to let
1357 * Soup take control.
1358 * A GstBuffer is allocated in gst_soup_http_src_chunk_allocator() and
1359 * associated with a SoupBuffer.
1360 * Soup reads HTTP data in the GstBuffer's data buffer.
1361 * The gst_soup_http_src_got_chunk_cb() is then called with the SoupBuffer.
1362 * That sets gst_soup_http_src_create()'s return argument to the GstBuffer,
1363 * increments its refcount (to 2), pauses the flow of data from the HTTP
1364 * source to prevent gst_soup_http_src_got_chunk_cb() from being called
1365 * again and breaks out of the GMainLoop.
1366 * Because the SOUP_MESSAGE_OVERWRITE_CHUNKS flag is set, Soup frees the
1367 * SoupBuffer and calls gst_soup_http_src_chunk_free(), which decrements the
1369 * gst_soup_http_src_create() returns the GstBuffer. It will be freed by a
1370 * downstream element.
1371 * If Soup fails to read HTTP data, it does not call
1372 * gst_soup_http_src_got_chunk_cb(), but still frees the SoupBuffer and
1373 * calls gst_soup_http_src_chunk_free(), which decrements the GstBuffer's
1374 * refcount to 0, freeing it.
1384 gst_soup_http_src_chunk_free (gpointer user_data)
1386 SoupGstChunk *chunk = (SoupGstChunk *) user_data;
1388 gst_buffer_unmap (chunk->buffer, &chunk->map);
1389 gst_buffer_unref (chunk->buffer);
1390 g_slice_free (SoupGstChunk, chunk);
1394 gst_soup_http_src_chunk_allocator (SoupMessage * msg, gsize max_len,
1397 GstSoupHTTPSrc *src = (GstSoupHTTPSrc *) user_data;
1398 GstBaseSrc *basesrc = GST_BASE_SRC_CAST (src);
1400 SoupBuffer *soupbuf;
1403 SoupGstChunk *chunk;
1406 length = MIN (basesrc->blocksize, max_len);
1408 length = basesrc->blocksize;
1409 GST_DEBUG_OBJECT (src, "alloc %" G_GSIZE_FORMAT " bytes <= %" G_GSIZE_FORMAT,
1412 rc = GST_BASE_SRC_CLASS (parent_class)->alloc (basesrc, -1, length, &gstbuf);
1413 if (G_UNLIKELY (rc != GST_FLOW_OK)) {
1414 /* Failed to allocate buffer. Stall SoupSession and return error code
1417 g_main_loop_quit (src->loop);
1421 chunk = g_slice_new0 (SoupGstChunk);
1422 chunk->buffer = gstbuf;
1423 gst_buffer_map (gstbuf, &chunk->map, GST_MAP_READWRITE);
1425 soupbuf = soup_buffer_new_with_owner (chunk->map.data, chunk->map.size,
1426 chunk, gst_soup_http_src_chunk_free);
1432 gst_soup_http_src_got_chunk_cb (SoupMessage * msg, SoupBuffer * chunk,
1433 GstSoupHTTPSrc * src)
1435 GstBaseSrc *basesrc;
1436 guint64 new_position;
1437 SoupGstChunk *gchunk;
1439 if (G_UNLIKELY (msg != src->msg)) {
1440 GST_DEBUG_OBJECT (src, "got chunk, but not for current message");
1443 if (G_UNLIKELY (!src->outbuf)) {
1444 GST_DEBUG_OBJECT (src, "got chunk but we're not expecting one");
1445 src->ret = GST_FLOW_OK;
1446 gst_soup_http_src_cancel_message (src);
1447 g_main_loop_quit (src->loop);
1451 /* We got data, reset the retry counter */
1452 src->retry_count = 0;
1454 src->have_body = FALSE;
1455 if (G_UNLIKELY (src->session_io_status !=
1456 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
1457 /* Probably a redirect. */
1460 basesrc = GST_BASE_SRC_CAST (src);
1461 GST_DEBUG_OBJECT (src, "got chunk of %" G_GSIZE_FORMAT " bytes",
1464 /* Extract the GstBuffer from the SoupBuffer and set its fields. */
1465 gchunk = (SoupGstChunk *) soup_buffer_get_owner (chunk);
1466 *src->outbuf = gchunk->buffer;
1468 gst_buffer_resize (*src->outbuf, 0, chunk->length);
1469 GST_BUFFER_OFFSET (*src->outbuf) = basesrc->segment.position;
1471 gst_buffer_ref (*src->outbuf);
1473 new_position = src->read_position + chunk->length;
1474 if (G_LIKELY (src->request_position == src->read_position))
1475 src->request_position = new_position;
1476 src->read_position = new_position;
1478 if (src->have_size) {
1479 if (new_position > src->content_size) {
1480 GST_DEBUG_OBJECT (src, "Got position previous estimated content size "
1481 "(%" G_GINT64_FORMAT " > %" G_GINT64_FORMAT ")", new_position,
1483 src->content_size = new_position;
1484 basesrc->segment.duration = src->content_size;
1485 gst_element_post_message (GST_ELEMENT (src),
1486 gst_message_new_duration_changed (GST_OBJECT (src)));
1487 } else if (new_position == src->content_size) {
1488 GST_DEBUG_OBJECT (src, "We're EOS now");
1492 src->ret = GST_FLOW_OK;
1493 g_main_loop_quit (src->loop);
1494 gst_soup_http_src_session_pause_message (src);
1498 gst_soup_http_src_response_cb (SoupSession * session, SoupMessage * msg,
1499 GstSoupHTTPSrc * src)
1501 if (G_UNLIKELY (msg != src->msg)) {
1502 GST_DEBUG_OBJECT (src, "got response %d: %s, but not for current message",
1503 msg->status_code, msg->reason_phrase);
1506 if (G_UNLIKELY (src->session_io_status !=
1507 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)
1508 && SOUP_STATUS_IS_REDIRECTION (msg->status_code)) {
1509 /* Ignore redirections. */
1512 GST_INFO_OBJECT (src, "got response %d: %s", msg->status_code,
1513 msg->reason_phrase);
1514 if (src->session_io_status == GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING &&
1515 src->read_position > 0 && (src->have_size
1516 && src->read_position < src->content_size) &&
1517 (src->max_retries == -1 || src->retry_count < src->max_retries)) {
1518 /* The server disconnected while streaming. Reconnect and seeking to the
1523 gst_soup_http_src_parse_status (msg, src);
1525 /* The session's SoupMessage object expires after this callback returns. */
1527 g_main_loop_quit (src->loop);
1530 #define SOUP_HTTP_SRC_ERROR(src,soup_msg,cat,code,error_message) \
1531 GST_ELEMENT_ERROR ((src), cat, code, ("%s", error_message), \
1532 ("%s (%d), URL: %s, Redirect to: %s", (soup_msg)->reason_phrase, \
1533 (soup_msg)->status_code, (src)->location, GST_STR_NULL ((src)->redirection_uri)));
1536 gst_soup_http_src_parse_status (SoupMessage * msg, GstSoupHTTPSrc * src)
1538 if (msg->method == SOUP_METHOD_HEAD) {
1539 if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code))
1540 GST_DEBUG_OBJECT (src, "Ignoring error %d during HEAD request",
1542 } else if (SOUP_STATUS_IS_TRANSPORT_ERROR (msg->status_code)) {
1543 switch (msg->status_code) {
1544 case SOUP_STATUS_CANT_RESOLVE:
1545 case SOUP_STATUS_CANT_RESOLVE_PROXY:
1546 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, NOT_FOUND,
1547 _("Could not resolve server name."));
1548 src->ret = GST_FLOW_ERROR;
1550 case SOUP_STATUS_CANT_CONNECT:
1551 case SOUP_STATUS_CANT_CONNECT_PROXY:
1552 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ,
1553 _("Could not establish connection to server."));
1554 src->ret = GST_FLOW_ERROR;
1556 case SOUP_STATUS_SSL_FAILED:
1557 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ,
1558 _("Secure connection setup failed."));
1559 src->ret = GST_FLOW_ERROR;
1561 case SOUP_STATUS_IO_ERROR:
1562 if (src->max_retries == -1 || src->retry_count < src->max_retries) {
1565 src->ret = GST_FLOW_CUSTOM_ERROR;
1567 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, READ,
1568 _("A network error occurred, or the server closed the connection "
1570 src->ret = GST_FLOW_ERROR;
1573 case SOUP_STATUS_MALFORMED:
1574 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, READ,
1575 _("Server sent bad data."));
1576 src->ret = GST_FLOW_ERROR;
1578 case SOUP_STATUS_CANCELLED:
1579 /* No error message when interrupted by program. */
1582 } else if (SOUP_STATUS_IS_CLIENT_ERROR (msg->status_code) ||
1583 SOUP_STATUS_IS_REDIRECTION (msg->status_code) ||
1584 SOUP_STATUS_IS_SERVER_ERROR (msg->status_code)) {
1585 /* Report HTTP error. */
1587 /* when content_size is unknown and we have just finished receiving
1588 * a body message, requests that go beyond the content limits will result
1589 * in an error. Here we convert those to EOS */
1590 if (msg->status_code == SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE &&
1591 src->have_body && !src->have_size) {
1592 GST_DEBUG_OBJECT (src, "Requested range out of limits and received full "
1593 "body, returning EOS");
1594 src->ret = GST_FLOW_EOS;
1598 /* FIXME: reason_phrase is not translated and not suitable for user
1599 * error dialog according to libsoup documentation.
1601 if (msg->status_code == SOUP_STATUS_NOT_FOUND) {
1602 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
1603 ("%s", msg->reason_phrase),
1604 ("%s (%d), URL: %s, Redirect to: %s", msg->reason_phrase,
1605 msg->status_code, src->location,
1606 GST_STR_NULL (src->redirection_uri)));
1607 } else if (msg->status_code == SOUP_STATUS_UNAUTHORIZED
1608 || msg->status_code == SOUP_STATUS_PAYMENT_REQUIRED
1609 || msg->status_code == SOUP_STATUS_FORBIDDEN
1610 || msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
1611 GST_ELEMENT_ERROR (src, RESOURCE, NOT_AUTHORIZED, ("%s",
1612 msg->reason_phrase), ("%s (%d), URL: %s, Redirect to: %s",
1613 msg->reason_phrase, msg->status_code, src->location,
1614 GST_STR_NULL (src->redirection_uri)));
1616 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1617 ("%s", msg->reason_phrase),
1618 ("%s (%d), URL: %s, Redirect to: %s", msg->reason_phrase,
1619 msg->status_code, src->location,
1620 GST_STR_NULL (src->redirection_uri)));
1622 src->ret = GST_FLOW_ERROR;
1627 gst_soup_http_src_build_message (GstSoupHTTPSrc * src, const gchar * method)
1629 g_return_val_if_fail (src->msg == NULL, FALSE);
1631 src->msg = soup_message_new (method, src->location);
1633 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1634 ("Error parsing URL."), ("URL: %s", src->location));
1637 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE;
1638 if (!src->keep_alive) {
1639 soup_message_headers_append (src->msg->request_headers, "Connection",
1642 if (src->iradio_mode) {
1643 soup_message_headers_append (src->msg->request_headers, "icy-metadata",
1649 for (cookie = src->cookies; *cookie != NULL; cookie++) {
1650 soup_message_headers_append (src->msg->request_headers, "Cookie",
1656 g_signal_connect (src->msg, "got_headers",
1657 G_CALLBACK (gst_soup_http_src_got_headers_cb), src);
1658 g_signal_connect (src->msg, "got_body",
1659 G_CALLBACK (gst_soup_http_src_got_body_cb), src);
1660 g_signal_connect (src->msg, "finished",
1661 G_CALLBACK (gst_soup_http_src_finished_cb), src);
1662 g_signal_connect (src->msg, "got_chunk",
1663 G_CALLBACK (gst_soup_http_src_got_chunk_cb), src);
1664 soup_message_set_flags (src->msg, SOUP_MESSAGE_OVERWRITE_CHUNKS |
1665 (src->automatic_redirect ? 0 : SOUP_MESSAGE_NO_REDIRECT));
1666 soup_message_set_chunk_allocator (src->msg,
1667 gst_soup_http_src_chunk_allocator, src, NULL);
1668 gst_soup_http_src_add_range_header (src, src->request_position,
1669 src->stop_position);
1671 gst_soup_http_src_add_extra_headers (src);
1676 static GstFlowReturn
1677 gst_soup_http_src_do_request (GstSoupHTTPSrc * src, const gchar * method,
1678 GstBuffer ** outbuf)
1680 /* If we're not OK, just go out of here */
1681 if (src->ret != GST_FLOW_OK) {
1682 GST_DEBUG_OBJECT (src, "Previous flow return not OK: %s",
1683 gst_flow_get_name (src->ret));
1687 GST_LOG_OBJECT (src, "Running request for method: %s", method);
1688 if (src->msg && (src->request_position != src->read_position)) {
1689 if (src->session_io_status == GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE) {
1690 gst_soup_http_src_add_range_header (src, src->request_position,
1691 src->stop_position);
1693 GST_DEBUG_OBJECT (src, "Seek from position %" G_GUINT64_FORMAT
1694 " to %" G_GUINT64_FORMAT ": requeueing connection request",
1695 src->read_position, src->request_position);
1696 gst_soup_http_src_cancel_message (src);
1700 if (!gst_soup_http_src_build_message (src, method)) {
1701 return GST_FLOW_ERROR;
1704 src->ret = GST_FLOW_CUSTOM_ERROR;
1705 src->outbuf = outbuf;
1707 if (src->interrupted) {
1708 GST_INFO_OBJECT (src, "interrupted");
1709 src->ret = GST_FLOW_FLUSHING;
1713 GST_INFO_OBJECT (src, "Reconnecting");
1714 if (!gst_soup_http_src_build_message (src, method)) {
1715 return GST_FLOW_ERROR;
1721 GST_DEBUG_OBJECT (src, "EOS reached");
1725 switch (src->session_io_status) {
1726 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE:
1727 GST_INFO_OBJECT (src, "Queueing connection request");
1728 gst_soup_http_src_queue_message (src);
1730 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_QUEUED:
1732 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING:
1733 gst_soup_http_src_session_unpause_message (src);
1735 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_CANCELLED:
1740 if (src->ret == GST_FLOW_CUSTOM_ERROR) {
1741 g_main_context_push_thread_default (src->context);
1742 g_main_loop_run (src->loop);
1743 g_main_context_pop_thread_default (src->context);
1746 } while (src->ret == GST_FLOW_CUSTOM_ERROR);
1748 /* Let the request finish if we had a stop position and are there */
1749 if (src->ret == GST_FLOW_OK && src->stop_position != -1
1750 && src->read_position >= src->stop_position) {
1752 gst_soup_http_src_session_unpause_message (src);
1753 g_main_context_push_thread_default (src->context);
1754 g_main_loop_run (src->loop);
1755 g_main_context_pop_thread_default (src->context);
1757 g_cond_signal (&src->request_finished_cond);
1758 /* Return OK unconditionally here, src->ret will
1759 * be most likely be EOS now but we want to
1760 * consume the buffer we got above */
1764 if (src->ret == GST_FLOW_CUSTOM_ERROR)
1765 src->ret = GST_FLOW_EOS;
1766 g_cond_signal (&src->request_finished_cond);
1768 /* basesrc assumes that we don't return a buffer if
1769 * something else than OK is returned. It will just
1770 * leak any buffer we might accidentially provide
1773 * This can potentially happen during flushing.
1775 if (src->ret != GST_FLOW_OK && outbuf && *outbuf) {
1776 gst_buffer_unref (*outbuf);
1783 static GstFlowReturn
1784 gst_soup_http_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
1786 GstSoupHTTPSrc *src;
1788 GstEvent *http_headers_event;
1790 src = GST_SOUP_HTTP_SRC (psrc);
1792 g_mutex_lock (&src->mutex);
1795 gst_soup_http_src_do_request (src,
1796 src->method ? src->method : SOUP_METHOD_GET, outbuf);
1797 http_headers_event = src->http_headers_event;
1798 src->http_headers_event = NULL;
1799 g_mutex_unlock (&src->mutex);
1801 if (http_headers_event)
1802 gst_pad_push_event (GST_BASE_SRC_PAD (src), http_headers_event);
1808 gst_soup_http_src_start (GstBaseSrc * bsrc)
1810 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1812 GST_DEBUG_OBJECT (src, "start(\"%s\")", src->location);
1814 return gst_soup_http_src_session_open (src);
1818 gst_soup_http_src_stop (GstBaseSrc * bsrc)
1820 GstSoupHTTPSrc *src;
1822 src = GST_SOUP_HTTP_SRC (bsrc);
1823 GST_DEBUG_OBJECT (src, "stop()");
1824 if (src->keep_alive && !src->msg)
1825 gst_soup_http_src_cancel_message (src);
1827 gst_soup_http_src_session_close (src);
1829 gst_soup_http_src_reset (src);
1833 static GstStateChangeReturn
1834 gst_soup_http_src_change_state (GstElement * element, GstStateChange transition)
1836 GstStateChangeReturn ret;
1837 GstSoupHTTPSrc *src;
1839 src = GST_SOUP_HTTP_SRC (element);
1841 switch (transition) {
1842 case GST_STATE_CHANGE_READY_TO_NULL:
1843 gst_soup_http_src_session_close (src);
1849 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1854 /* Interrupt a blocking request. */
1856 gst_soup_http_src_unlock (GstBaseSrc * bsrc)
1858 GstSoupHTTPSrc *src;
1860 src = GST_SOUP_HTTP_SRC (bsrc);
1861 GST_DEBUG_OBJECT (src, "unlock()");
1863 src->interrupted = TRUE;
1864 src->ret = GST_FLOW_FLUSHING;
1866 g_main_loop_quit (src->loop);
1867 g_cond_signal (&src->request_finished_cond);
1871 /* Interrupt interrupt. */
1873 gst_soup_http_src_unlock_stop (GstBaseSrc * bsrc)
1875 GstSoupHTTPSrc *src;
1877 src = GST_SOUP_HTTP_SRC (bsrc);
1878 GST_DEBUG_OBJECT (src, "unlock_stop()");
1880 src->interrupted = FALSE;
1881 src->ret = GST_FLOW_OK;
1886 gst_soup_http_src_get_size (GstBaseSrc * bsrc, guint64 * size)
1888 GstSoupHTTPSrc *src;
1890 src = GST_SOUP_HTTP_SRC (bsrc);
1892 if (src->have_size) {
1893 GST_DEBUG_OBJECT (src, "get_size() = %" G_GUINT64_FORMAT,
1895 *size = src->content_size;
1898 GST_DEBUG_OBJECT (src, "get_size() = FALSE");
1903 gst_soup_http_src_check_seekable (GstSoupHTTPSrc * src)
1905 GstFlowReturn ret = GST_FLOW_OK;
1907 /* Special case to check if the server allows range requests
1908 * before really starting to get data in the buffer creation
1911 if (!src->got_headers && GST_STATE (src) >= GST_STATE_PAUSED) {
1912 g_mutex_lock (&src->mutex);
1913 while (!src->got_headers && !src->interrupted && ret == GST_FLOW_OK) {
1914 if ((src->msg && src->msg->method != SOUP_METHOD_HEAD) &&
1915 src->session_io_status != GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE) {
1916 /* wait for the current request to finish */
1917 g_cond_wait (&src->request_finished_cond, &src->mutex);
1919 if (gst_soup_http_src_session_open (src)) {
1920 ret = gst_soup_http_src_do_request (src, SOUP_METHOD_HEAD, NULL);
1924 if (src->ret == GST_FLOW_EOS) {
1925 /* A HEAD request shouldn't lead to EOS */
1926 src->ret = GST_FLOW_OK;
1928 /* resets status to idle */
1929 gst_soup_http_src_cancel_message (src);
1930 g_mutex_unlock (&src->mutex);
1935 gst_soup_http_src_is_seekable (GstBaseSrc * bsrc)
1937 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1939 gst_soup_http_src_check_seekable (src);
1941 return src->seekable;
1945 gst_soup_http_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
1947 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1949 GST_DEBUG_OBJECT (src, "do_seek(%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1950 ")", segment->start, segment->stop);
1951 if (src->read_position == segment->start &&
1952 src->request_position == src->read_position &&
1953 src->stop_position == segment->stop) {
1954 GST_DEBUG_OBJECT (src,
1955 "Seek to current read/end position and no seek pending");
1959 gst_soup_http_src_check_seekable (src);
1961 /* If we have no headers we don't know yet if it is seekable or not.
1962 * Store the start position and error out later if it isn't */
1963 if (src->got_headers && !src->seekable) {
1964 GST_WARNING_OBJECT (src, "Not seekable");
1968 if (segment->rate < 0.0 || segment->format != GST_FORMAT_BYTES) {
1969 GST_WARNING_OBJECT (src, "Invalid seek segment");
1973 if (src->have_size && segment->start >= src->content_size) {
1974 GST_WARNING_OBJECT (src,
1975 "Potentially seeking behind end of file, might EOS immediately");
1978 /* Wait for create() to handle the jump in offset. */
1979 src->request_position = segment->start;
1980 src->stop_position = segment->stop;
1986 gst_soup_http_src_query (GstBaseSrc * bsrc, GstQuery * query)
1988 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1990 GstSchedulingFlags flags;
1991 gint minsize, maxsize, align;
1993 switch (GST_QUERY_TYPE (query)) {
1995 gst_query_set_uri (query, src->location);
1996 if (src->redirection_uri != NULL) {
1997 gst_query_set_uri_redirection (query, src->redirection_uri);
1998 gst_query_set_uri_redirection_permanent (query,
1999 src->redirection_permanent);
2009 ret = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
2011 switch (GST_QUERY_TYPE (query)) {
2012 case GST_QUERY_SCHEDULING:
2013 gst_query_parse_scheduling (query, &flags, &minsize, &maxsize, &align);
2014 flags |= GST_SCHEDULING_FLAG_BANDWIDTH_LIMITED;
2015 gst_query_set_scheduling (query, flags, minsize, maxsize, align);
2025 gst_soup_http_src_set_location (GstSoupHTTPSrc * src, const gchar * uri,
2028 const char *alt_schemes[] = { "icy://", "icyx://" };
2031 if (src->location) {
2032 g_free (src->location);
2033 src->location = NULL;
2039 for (i = 0; i < G_N_ELEMENTS (alt_schemes); i++) {
2040 if (g_str_has_prefix (uri, alt_schemes[i])) {
2042 g_strdup_printf ("http://%s", uri + strlen (alt_schemes[i]));
2047 if (src->redirection_uri) {
2048 g_free (src->redirection_uri);
2049 src->redirection_uri = NULL;
2052 src->location = g_strdup (uri);
2058 gst_soup_http_src_set_proxy (GstSoupHTTPSrc * src, const gchar * uri)
2061 soup_uri_free (src->proxy);
2065 if (uri == NULL || *uri == '\0')
2068 if (g_str_has_prefix (uri, "http://")) {
2069 src->proxy = soup_uri_new (uri);
2071 gchar *new_uri = g_strconcat ("http://", uri, NULL);
2073 src->proxy = soup_uri_new (new_uri);
2077 return (src->proxy != NULL);
2081 gst_soup_http_src_uri_get_type (GType type)
2086 static const gchar *const *
2087 gst_soup_http_src_uri_get_protocols (GType type)
2089 static const gchar *protocols[] = { "http", "https", "icy", "icyx", NULL };
2095 gst_soup_http_src_uri_get_uri (GstURIHandler * handler)
2097 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (handler);
2099 /* FIXME: make thread-safe */
2100 return g_strdup (src->location);
2104 gst_soup_http_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
2107 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (handler);
2109 return gst_soup_http_src_set_location (src, uri, error);
2113 gst_soup_http_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
2115 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
2117 iface->get_type = gst_soup_http_src_uri_get_type;
2118 iface->get_protocols = gst_soup_http_src_uri_get_protocols;
2119 iface->get_uri = gst_soup_http_src_uri_get_uri;
2120 iface->set_uri = gst_soup_http_src_uri_set_uri;