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"
83 #include <gst/tag/tag.h>
85 GST_DEBUG_CATEGORY_STATIC (souphttpsrc_debug);
86 #define GST_CAT_DEFAULT souphttpsrc_debug
88 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
99 PROP_AUTOMATIC_REDIRECT,
111 #define DEFAULT_USER_AGENT "GStreamer souphttpsrc "
112 #define DEFAULT_IRADIO_MODE TRUE
114 static void gst_soup_http_src_uri_handler_init (gpointer g_iface,
115 gpointer iface_data);
116 static void gst_soup_http_src_finalize (GObject * gobject);
118 static void gst_soup_http_src_set_property (GObject * object, guint prop_id,
119 const GValue * value, GParamSpec * pspec);
120 static void gst_soup_http_src_get_property (GObject * object, guint prop_id,
121 GValue * value, GParamSpec * pspec);
123 static GstFlowReturn gst_soup_http_src_create (GstPushSrc * psrc,
124 GstBuffer ** outbuf);
125 static gboolean gst_soup_http_src_start (GstBaseSrc * bsrc);
126 static gboolean gst_soup_http_src_stop (GstBaseSrc * bsrc);
127 static gboolean gst_soup_http_src_get_size (GstBaseSrc * bsrc, guint64 * size);
128 static gboolean gst_soup_http_src_is_seekable (GstBaseSrc * bsrc);
129 static gboolean gst_soup_http_src_do_seek (GstBaseSrc * bsrc,
130 GstSegment * segment);
131 static gboolean gst_soup_http_src_query (GstBaseSrc * bsrc, GstQuery * query);
132 static gboolean gst_soup_http_src_unlock (GstBaseSrc * bsrc);
133 static gboolean gst_soup_http_src_unlock_stop (GstBaseSrc * bsrc);
134 static gboolean gst_soup_http_src_set_location (GstSoupHTTPSrc * src,
135 const gchar * uri, GError ** error);
136 static gboolean gst_soup_http_src_set_proxy (GstSoupHTTPSrc * src,
138 static char *gst_soup_http_src_unicodify (const char *str);
139 static gboolean gst_soup_http_src_build_message (GstSoupHTTPSrc * src,
140 const gchar * method);
141 static void gst_soup_http_src_cancel_message (GstSoupHTTPSrc * src);
142 static void gst_soup_http_src_queue_message (GstSoupHTTPSrc * src);
143 static gboolean gst_soup_http_src_add_range_header (GstSoupHTTPSrc * src,
145 static void gst_soup_http_src_session_unpause_message (GstSoupHTTPSrc * src);
146 static void gst_soup_http_src_session_pause_message (GstSoupHTTPSrc * src);
147 static gboolean gst_soup_http_src_session_open (GstSoupHTTPSrc * src);
148 static void gst_soup_http_src_session_close (GstSoupHTTPSrc * src);
149 static void gst_soup_http_src_parse_status (SoupMessage * msg,
150 GstSoupHTTPSrc * src);
151 static void gst_soup_http_src_chunk_free (gpointer gstbuf);
152 static SoupBuffer *gst_soup_http_src_chunk_allocator (SoupMessage * msg,
153 gsize max_len, gpointer user_data);
154 static void gst_soup_http_src_got_chunk_cb (SoupMessage * msg,
155 SoupBuffer * chunk, GstSoupHTTPSrc * src);
156 static void gst_soup_http_src_response_cb (SoupSession * session,
157 SoupMessage * msg, GstSoupHTTPSrc * src);
158 static void gst_soup_http_src_got_headers_cb (SoupMessage * msg,
159 GstSoupHTTPSrc * src);
160 static void gst_soup_http_src_got_body_cb (SoupMessage * msg,
161 GstSoupHTTPSrc * src);
162 static void gst_soup_http_src_finished_cb (SoupMessage * msg,
163 GstSoupHTTPSrc * src);
164 static void gst_soup_http_src_authenticate_cb (SoupSession * session,
165 SoupMessage * msg, SoupAuth * auth, gboolean retrying,
166 GstSoupHTTPSrc * src);
168 #define gst_soup_http_src_parent_class parent_class
169 G_DEFINE_TYPE_WITH_CODE (GstSoupHTTPSrc, gst_soup_http_src, GST_TYPE_PUSH_SRC,
170 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
171 gst_soup_http_src_uri_handler_init));
174 gst_soup_http_src_class_init (GstSoupHTTPSrcClass * klass)
176 GObjectClass *gobject_class;
177 GstElementClass *gstelement_class;
178 GstBaseSrcClass *gstbasesrc_class;
179 GstPushSrcClass *gstpushsrc_class;
181 gobject_class = (GObjectClass *) klass;
182 gstelement_class = (GstElementClass *) klass;
183 gstbasesrc_class = (GstBaseSrcClass *) klass;
184 gstpushsrc_class = (GstPushSrcClass *) klass;
186 gobject_class->set_property = gst_soup_http_src_set_property;
187 gobject_class->get_property = gst_soup_http_src_get_property;
188 gobject_class->finalize = gst_soup_http_src_finalize;
190 g_object_class_install_property (gobject_class,
192 g_param_spec_string ("location", "Location",
193 "Location to read from", "",
194 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
195 g_object_class_install_property (gobject_class,
197 g_param_spec_string ("user-agent", "User-Agent",
198 "Value of the User-Agent HTTP request header field",
199 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
200 g_object_class_install_property (gobject_class,
201 PROP_AUTOMATIC_REDIRECT,
202 g_param_spec_boolean ("automatic-redirect", "automatic-redirect",
203 "Automatically follow HTTP redirects (HTTP Status Code 3xx)",
204 TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
205 g_object_class_install_property (gobject_class,
207 g_param_spec_string ("proxy", "Proxy",
208 "HTTP proxy server URI", "",
209 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
210 g_object_class_install_property (gobject_class,
212 g_param_spec_string ("user-id", "user-id",
213 "HTTP location URI user id for authentication", "",
214 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
215 g_object_class_install_property (gobject_class, PROP_USER_PW,
216 g_param_spec_string ("user-pw", "user-pw",
217 "HTTP location URI user password for authentication", "",
218 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
219 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
220 g_param_spec_string ("proxy-id", "proxy-id",
221 "HTTP proxy URI user id for authentication", "",
222 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
223 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
224 g_param_spec_string ("proxy-pw", "proxy-pw",
225 "HTTP proxy URI user password for authentication", "",
226 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227 g_object_class_install_property (gobject_class, PROP_COOKIES,
228 g_param_spec_boxed ("cookies", "Cookies", "HTTP request cookies",
229 G_TYPE_STRV, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
230 g_object_class_install_property (gobject_class, PROP_IS_LIVE,
231 g_param_spec_boolean ("is-live", "is-live", "Act like a live source",
232 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
233 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
234 g_param_spec_uint ("timeout", "timeout",
235 "Value in seconds to timeout a blocking I/O (0 = No timeout).", 0,
236 3600, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
237 g_object_class_install_property (gobject_class, PROP_EXTRA_HEADERS,
238 g_param_spec_boxed ("extra-headers", "Extra Headers",
239 "Extra headers to append to the HTTP request",
240 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
241 g_object_class_install_property (gobject_class, PROP_IRADIO_MODE,
242 g_param_spec_boolean ("iradio-mode", "iradio-mode",
243 "Enable internet radio mode (ask server to send shoutcast/icecast "
244 "metadata interleaved with the actual stream data)",
245 DEFAULT_IRADIO_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
247 gst_element_class_add_pad_template (gstelement_class,
248 gst_static_pad_template_get (&srctemplate));
250 gst_element_class_set_static_metadata (gstelement_class, "HTTP client source",
252 "Receive data as a client over the network via HTTP using SOUP",
253 "Wouter Cloetens <wouter@mind.be>");
255 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_soup_http_src_start);
256 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_soup_http_src_stop);
257 gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_soup_http_src_unlock);
258 gstbasesrc_class->unlock_stop =
259 GST_DEBUG_FUNCPTR (gst_soup_http_src_unlock_stop);
260 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_soup_http_src_get_size);
261 gstbasesrc_class->is_seekable =
262 GST_DEBUG_FUNCPTR (gst_soup_http_src_is_seekable);
263 gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_soup_http_src_do_seek);
264 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_soup_http_src_query);
266 gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_soup_http_src_create);
268 GST_DEBUG_CATEGORY_INIT (souphttpsrc_debug, "souphttpsrc", 0,
273 gst_soup_http_src_reset (GstSoupHTTPSrc * src)
275 src->interrupted = FALSE;
277 src->have_size = FALSE;
278 src->got_headers = FALSE;
279 src->seekable = FALSE;
280 src->read_position = 0;
281 src->request_position = 0;
282 src->content_size = 0;
284 gst_caps_replace (&src->src_caps, NULL);
285 g_free (src->iradio_name);
286 src->iradio_name = NULL;
287 g_free (src->iradio_genre);
288 src->iradio_genre = NULL;
289 g_free (src->iradio_url);
290 src->iradio_url = NULL;
294 gst_soup_http_src_init (GstSoupHTTPSrc * src)
298 g_mutex_init (&src->mutex);
299 g_cond_init (&src->request_finished_cond);
300 src->location = NULL;
301 src->automatic_redirect = TRUE;
302 src->user_agent = g_strdup (DEFAULT_USER_AGENT);
305 src->proxy_id = NULL;
306 src->proxy_pw = NULL;
308 src->iradio_mode = DEFAULT_IRADIO_MODE;
313 proxy = g_getenv ("http_proxy");
314 if (proxy && !gst_soup_http_src_set_proxy (src, proxy)) {
315 GST_WARNING_OBJECT (src,
316 "The proxy in the http_proxy env var (\"%s\") cannot be parsed.",
320 gst_soup_http_src_reset (src);
324 gst_soup_http_src_finalize (GObject * gobject)
326 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (gobject);
328 GST_DEBUG_OBJECT (src, "finalize");
330 g_mutex_clear (&src->mutex);
331 g_cond_clear (&src->request_finished_cond);
332 g_free (src->location);
333 g_free (src->user_agent);
334 if (src->proxy != NULL) {
335 soup_uri_free (src->proxy);
337 g_free (src->user_id);
338 g_free (src->user_pw);
339 g_free (src->proxy_id);
340 g_free (src->proxy_pw);
341 g_strfreev (src->cookies);
343 G_OBJECT_CLASS (parent_class)->finalize (gobject);
347 gst_soup_http_src_set_property (GObject * object, guint prop_id,
348 const GValue * value, GParamSpec * pspec)
350 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (object);
355 const gchar *location;
357 location = g_value_get_string (value);
359 if (location == NULL) {
360 GST_WARNING ("location property cannot be NULL");
363 if (!gst_soup_http_src_set_location (src, location, NULL)) {
364 GST_WARNING ("badly formatted location");
369 case PROP_USER_AGENT:
371 g_free (src->user_agent);
372 src->user_agent = g_value_dup_string (value);
374 case PROP_IRADIO_MODE:
375 src->iradio_mode = g_value_get_boolean (value);
377 case PROP_AUTOMATIC_REDIRECT:
378 src->automatic_redirect = g_value_get_boolean (value);
384 proxy = g_value_get_string (value);
387 GST_WARNING ("proxy property cannot be NULL");
390 if (!gst_soup_http_src_set_proxy (src, proxy)) {
391 GST_WARNING ("badly formatted proxy URI");
397 g_strfreev (src->cookies);
398 src->cookies = g_strdupv (g_value_get_boxed (value));
401 gst_base_src_set_live (GST_BASE_SRC (src), g_value_get_boolean (value));
405 g_free (src->user_id);
406 src->user_id = g_value_dup_string (value);
410 g_free (src->user_pw);
411 src->user_pw = g_value_dup_string (value);
415 g_free (src->proxy_id);
416 src->proxy_id = g_value_dup_string (value);
420 g_free (src->proxy_pw);
421 src->proxy_pw = g_value_dup_string (value);
424 src->timeout = g_value_get_uint (value);
426 case PROP_EXTRA_HEADERS:{
427 const GstStructure *s = gst_value_get_structure (value);
429 if (src->extra_headers)
430 gst_structure_free (src->extra_headers);
432 src->extra_headers = s ? gst_structure_copy (s) : NULL;
436 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
444 gst_soup_http_src_get_property (GObject * object, guint prop_id,
445 GValue * value, GParamSpec * pspec)
447 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (object);
451 g_value_set_string (value, src->location);
453 case PROP_USER_AGENT:
454 g_value_set_string (value, src->user_agent);
456 case PROP_AUTOMATIC_REDIRECT:
457 g_value_set_boolean (value, src->automatic_redirect);
460 if (src->proxy == NULL)
461 g_value_set_static_string (value, "");
463 char *proxy = soup_uri_to_string (src->proxy, FALSE);
465 g_value_set_string (value, proxy);
470 g_value_set_boxed (value, g_strdupv (src->cookies));
473 g_value_set_boolean (value, gst_base_src_is_live (GST_BASE_SRC (src)));
475 case PROP_IRADIO_MODE:
476 g_value_set_boolean (value, src->iradio_mode);
479 g_value_set_string (value, src->user_id);
482 g_value_set_string (value, src->user_pw);
485 g_value_set_string (value, src->proxy_id);
488 g_value_set_string (value, src->proxy_pw);
491 g_value_set_uint (value, src->timeout);
493 case PROP_EXTRA_HEADERS:
494 gst_value_set_structure (value, src->extra_headers);
497 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
503 gst_soup_http_src_unicodify (const gchar * str)
505 const gchar *env_vars[] = { "GST_ICY_TAG_ENCODING",
506 "GST_TAG_ENCODING", NULL
509 return gst_tag_freeform_string_to_utf8 (str, -1, env_vars);
513 gst_soup_http_src_cancel_message (GstSoupHTTPSrc * src)
515 if (src->msg != NULL) {
516 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_CANCELLED;
517 soup_session_cancel_message (src->session, src->msg, SOUP_STATUS_CANCELLED);
519 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE;
524 gst_soup_http_src_queue_message (GstSoupHTTPSrc * src)
526 soup_session_queue_message (src->session, src->msg,
527 (SoupSessionCallback) gst_soup_http_src_response_cb, src);
528 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_QUEUED;
532 gst_soup_http_src_add_range_header (GstSoupHTTPSrc * src, guint64 offset)
538 soup_message_headers_remove (src->msg->request_headers, "Range");
540 rc = g_snprintf (buf, sizeof (buf), "bytes=%" G_GUINT64_FORMAT "-", offset);
541 if (rc > sizeof (buf) || rc < 0)
543 soup_message_headers_append (src->msg->request_headers, "Range", buf);
545 src->read_position = offset;
550 _append_extra_header (GQuark field_id, const GValue * value, gpointer user_data)
552 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (user_data);
553 const gchar *field_name = g_quark_to_string (field_id);
554 gchar *field_content = NULL;
556 if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
557 field_content = g_value_dup_string (value);
559 GValue dest = { 0, };
561 g_value_init (&dest, G_TYPE_STRING);
562 if (g_value_transform (value, &dest)) {
563 field_content = g_value_dup_string (&dest);
567 if (field_content == NULL) {
568 GST_ERROR_OBJECT (src, "extra-headers field '%s' contains no value "
569 "or can't be converted to a string", field_name);
573 GST_DEBUG_OBJECT (src, "Appending extra header: \"%s: %s\"", field_name,
575 soup_message_headers_append (src->msg->request_headers, field_name,
578 g_free (field_content);
584 _append_extra_headers (GQuark field_id, const GValue * value,
587 if (G_VALUE_TYPE (value) == GST_TYPE_ARRAY) {
588 guint n = gst_value_array_get_size (value);
591 for (i = 0; i < n; i++) {
592 const GValue *v = gst_value_array_get_value (value, i);
594 if (!_append_extra_header (field_id, v, user_data))
597 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
598 guint n = gst_value_list_get_size (value);
601 for (i = 0; i < n; i++) {
602 const GValue *v = gst_value_list_get_value (value, i);
604 if (!_append_extra_header (field_id, v, user_data))
608 return _append_extra_header (field_id, value, user_data);
616 gst_soup_http_src_add_extra_headers (GstSoupHTTPSrc * src)
618 if (!src->extra_headers)
621 return gst_structure_foreach (src->extra_headers, _append_extra_headers, src);
626 gst_soup_http_src_session_unpause_message (GstSoupHTTPSrc * src)
628 soup_session_unpause_message (src->session, src->msg);
632 gst_soup_http_src_session_pause_message (GstSoupHTTPSrc * src)
634 soup_session_pause_message (src->session, src->msg);
638 gst_soup_http_src_session_open (GstSoupHTTPSrc * src)
641 GST_DEBUG_OBJECT (src, "Session is already open");
645 if (!src->location) {
646 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (_("No URL set.")),
647 ("Missing location property"));
651 src->context = g_main_context_new ();
653 src->loop = g_main_loop_new (src->context, TRUE);
655 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
656 (NULL), ("Failed to start GMainLoop"));
657 g_main_context_unref (src->context);
661 GST_DEBUG_OBJECT (src, "Creating session");
662 if (src->proxy == NULL) {
664 soup_session_async_new_with_options (SOUP_SESSION_ASYNC_CONTEXT,
665 src->context, SOUP_SESSION_USER_AGENT, src->user_agent,
666 SOUP_SESSION_TIMEOUT, src->timeout,
667 SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_PROXY_RESOLVER_DEFAULT,
671 soup_session_async_new_with_options (SOUP_SESSION_ASYNC_CONTEXT,
672 src->context, SOUP_SESSION_PROXY_URI, src->proxy,
673 SOUP_SESSION_TIMEOUT, src->timeout,
674 SOUP_SESSION_USER_AGENT, src->user_agent, NULL);
678 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
679 (NULL), ("Failed to create async session"));
683 g_signal_connect (src->session, "authenticate",
684 G_CALLBACK (gst_soup_http_src_authenticate_cb), src);
689 gst_soup_http_src_session_close (GstSoupHTTPSrc * src)
692 soup_session_abort (src->session); /* This unrefs the message. */
693 g_object_unref (src->session);
700 gst_soup_http_src_authenticate_cb (SoupSession * session, SoupMessage * msg,
701 SoupAuth * auth, gboolean retrying, GstSoupHTTPSrc * src)
704 /* First time authentication only, if we fail and are called again with retry true fall through */
705 if (msg->status_code == SOUP_STATUS_UNAUTHORIZED) {
706 if (src->user_id && src->user_pw)
707 soup_auth_authenticate (auth, src->user_id, src->user_pw);
708 } else if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
709 if (src->proxy_id && src->proxy_pw)
710 soup_auth_authenticate (auth, src->proxy_id, src->proxy_pw);
716 gst_soup_http_src_headers_foreach (const gchar * name, const gchar * val,
719 GST_DEBUG_OBJECT (src, " %s: %s", name, val);
723 gst_soup_http_src_got_headers_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
726 GstTagList *tag_list;
729 GHashTable *params = NULL;
731 GST_DEBUG_OBJECT (src, "got headers:");
732 soup_message_headers_foreach (msg->response_headers,
733 gst_soup_http_src_headers_foreach, src);
735 if (msg->status_code == 407 && src->proxy_id && src->proxy_pw)
738 if (src->automatic_redirect && SOUP_STATUS_IS_REDIRECTION (msg->status_code)) {
739 GST_DEBUG_OBJECT (src, "%u redirect to \"%s\"", msg->status_code,
740 soup_message_headers_get_one (msg->response_headers, "Location"));
744 if (msg->status_code == SOUP_STATUS_UNAUTHORIZED)
747 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING;
748 src->got_headers = TRUE;
750 /* Parse Content-Length. */
751 if (soup_message_headers_get_encoding (msg->response_headers) ==
752 SOUP_ENCODING_CONTENT_LENGTH) {
753 newsize = src->request_position +
754 soup_message_headers_get_content_length (msg->response_headers);
755 if (!src->have_size || (src->content_size != newsize)) {
756 src->content_size = newsize;
757 src->have_size = TRUE;
758 src->seekable = TRUE;
759 GST_DEBUG_OBJECT (src, "size = %" G_GUINT64_FORMAT, src->content_size);
761 basesrc = GST_BASE_SRC_CAST (src);
762 basesrc->segment.duration = src->content_size;
763 gst_element_post_message (GST_ELEMENT (src),
764 gst_message_new_duration_changed (GST_OBJECT (src)));
769 tag_list = gst_tag_list_new_empty ();
772 soup_message_headers_get_one (msg->response_headers,
773 "icy-metaint")) != NULL) {
774 gint icy_metaint = atoi (value);
776 GST_DEBUG_OBJECT (src, "icy-metaint: %s (parsed: %d)", value, icy_metaint);
777 if (icy_metaint > 0) {
779 gst_caps_unref (src->src_caps);
781 src->src_caps = gst_caps_new_simple ("application/x-icy",
782 "metadata-interval", G_TYPE_INT, icy_metaint, NULL);
784 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
788 soup_message_headers_get_content_type (msg->response_headers,
790 GST_DEBUG_OBJECT (src, "Content-Type: %s", value);
791 if (g_ascii_strcasecmp (value, "audio/L16") == 0) {
797 gst_caps_unref (src->src_caps);
799 param = g_hash_table_lookup (params, "channels");
801 channels = atol (param);
803 param = g_hash_table_lookup (params, "rate");
807 src->src_caps = gst_caps_new_simple ("audio/x-raw",
808 "format", G_TYPE_STRING, "S16BE",
809 "layout", G_TYPE_STRING, "interleaved",
810 "channels", G_TYPE_INT, channels, "rate", G_TYPE_INT, rate, NULL);
812 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
814 /* Set the Content-Type field on the caps */
816 src->src_caps = gst_caps_make_writable (src->src_caps);
817 gst_caps_set_simple (src->src_caps, "content-type", G_TYPE_STRING,
819 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
825 g_hash_table_destroy (params);
828 soup_message_headers_get_one (msg->response_headers,
829 "icy-name")) != NULL) {
830 g_free (src->iradio_name);
831 src->iradio_name = gst_soup_http_src_unicodify (value);
832 if (src->iradio_name) {
833 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_ORGANIZATION,
834 src->iradio_name, NULL);
838 soup_message_headers_get_one (msg->response_headers,
839 "icy-genre")) != NULL) {
840 g_free (src->iradio_genre);
841 src->iradio_genre = gst_soup_http_src_unicodify (value);
842 if (src->iradio_genre) {
843 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_GENRE,
844 src->iradio_genre, NULL);
847 if ((value = soup_message_headers_get_one (msg->response_headers, "icy-url"))
849 g_free (src->iradio_url);
850 src->iradio_url = gst_soup_http_src_unicodify (value);
851 if (src->iradio_url) {
852 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_LOCATION,
853 src->iradio_url, NULL);
856 if (!gst_tag_list_is_empty (tag_list)) {
857 GST_DEBUG_OBJECT (src,
858 "calling gst_element_found_tags with %" GST_PTR_FORMAT, tag_list);
859 gst_pad_push_event (GST_BASE_SRC_PAD (src), gst_event_new_tag (tag_list));
861 gst_tag_list_unref (tag_list);
864 /* Handle HTTP errors. */
865 gst_soup_http_src_parse_status (msg, src);
867 /* Check if Range header was respected. */
868 if (src->ret == GST_FLOW_CUSTOM_ERROR &&
869 src->read_position && msg->status_code != SOUP_STATUS_PARTIAL_CONTENT) {
870 src->seekable = FALSE;
871 GST_ELEMENT_ERROR (src, RESOURCE, SEEK,
872 (_("Server does not support seeking.")),
873 ("Server does not accept Range HTTP header, URL: %s", src->location));
874 src->ret = GST_FLOW_ERROR;
877 /* If we are going to error out, stop all processing right here, so we
878 * don't output any data (such as an error html page), and return
879 * GST_FLOW_ERROR from the create function instead of having
880 * got_chunk_cb overwrite src->ret with FLOW_OK again. */
881 if (src->ret == GST_FLOW_ERROR) {
882 gst_soup_http_src_session_pause_message (src);
885 g_main_loop_quit (src->loop);
887 g_cond_signal (&src->request_finished_cond);
890 /* Have body. Signal EOS. */
892 gst_soup_http_src_got_body_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
894 if (G_UNLIKELY (msg != src->msg)) {
895 GST_DEBUG_OBJECT (src, "got body, but not for current message");
898 if (G_UNLIKELY (src->session_io_status !=
899 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
900 /* Probably a redirect. */
903 GST_DEBUG_OBJECT (src, "got body");
904 src->ret = GST_FLOW_EOS;
906 g_main_loop_quit (src->loop);
907 gst_soup_http_src_session_pause_message (src);
910 /* Finished. Signal EOS. */
912 gst_soup_http_src_finished_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
914 if (G_UNLIKELY (msg != src->msg)) {
915 GST_DEBUG_OBJECT (src, "finished, but not for current message");
918 GST_DEBUG_OBJECT (src, "finished");
919 src->ret = GST_FLOW_EOS;
920 if (src->session_io_status == GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_CANCELLED) {
921 /* gst_soup_http_src_cancel_message() triggered this; probably a seek
922 * that occurred in the QUEUEING state; i.e. before the connection setup
923 * was complete. Do nothing */
924 } else if (src->session_io_status ==
925 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING && src->read_position > 0) {
926 /* The server disconnected while streaming. Reconnect and seeking to the
929 src->ret = GST_FLOW_CUSTOM_ERROR;
930 } else if (G_UNLIKELY (src->session_io_status !=
931 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
932 /* FIXME: reason_phrase is not translated, add proper error message */
933 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
934 ("%s", msg->reason_phrase),
935 ("libsoup status code %d", msg->status_code));
938 g_main_loop_quit (src->loop);
939 g_cond_signal (&src->request_finished_cond);
942 /* Buffer lifecycle management.
944 * gst_soup_http_src_create() runs the GMainLoop for this element, to let
946 * A GstBuffer is allocated in gst_soup_http_src_chunk_allocator() and
947 * associated with a SoupBuffer.
948 * Soup reads HTTP data in the GstBuffer's data buffer.
949 * The gst_soup_http_src_got_chunk_cb() is then called with the SoupBuffer.
950 * That sets gst_soup_http_src_create()'s return argument to the GstBuffer,
951 * increments its refcount (to 2), pauses the flow of data from the HTTP
952 * source to prevent gst_soup_http_src_got_chunk_cb() from being called
953 * again and breaks out of the GMainLoop.
954 * Because the SOUP_MESSAGE_OVERWRITE_CHUNKS flag is set, Soup frees the
955 * SoupBuffer and calls gst_soup_http_src_chunk_free(), which decrements the
957 * gst_soup_http_src_create() returns the GstBuffer. It will be freed by a
958 * downstream element.
959 * If Soup fails to read HTTP data, it does not call
960 * gst_soup_http_src_got_chunk_cb(), but still frees the SoupBuffer and
961 * calls gst_soup_http_src_chunk_free(), which decrements the GstBuffer's
962 * refcount to 0, freeing it.
972 gst_soup_http_src_chunk_free (gpointer user_data)
974 SoupGstChunk *chunk = (SoupGstChunk *) user_data;
976 gst_buffer_unmap (chunk->buffer, &chunk->map);
977 gst_buffer_unref (chunk->buffer);
978 g_slice_free (SoupGstChunk, chunk);
982 gst_soup_http_src_chunk_allocator (SoupMessage * msg, gsize max_len,
985 GstSoupHTTPSrc *src = (GstSoupHTTPSrc *) user_data;
986 GstBaseSrc *basesrc = GST_BASE_SRC_CAST (src);
994 length = MIN (basesrc->blocksize, max_len);
996 length = basesrc->blocksize;
997 GST_DEBUG_OBJECT (src, "alloc %" G_GSIZE_FORMAT " bytes <= %" G_GSIZE_FORMAT,
1000 rc = GST_BASE_SRC_CLASS (parent_class)->alloc (basesrc, -1, length, &gstbuf);
1001 if (G_UNLIKELY (rc != GST_FLOW_OK)) {
1002 /* Failed to allocate buffer. Stall SoupSession and return error code
1005 g_main_loop_quit (src->loop);
1009 chunk = g_slice_new0 (SoupGstChunk);
1010 chunk->buffer = gstbuf;
1011 gst_buffer_map (gstbuf, &chunk->map, GST_MAP_READWRITE);
1013 soupbuf = soup_buffer_new_with_owner (chunk->map.data, chunk->map.size,
1014 chunk, gst_soup_http_src_chunk_free);
1020 gst_soup_http_src_got_chunk_cb (SoupMessage * msg, SoupBuffer * chunk,
1021 GstSoupHTTPSrc * src)
1023 GstBaseSrc *basesrc;
1024 guint64 new_position;
1025 SoupGstChunk *gchunk;
1027 if (G_UNLIKELY (msg != src->msg)) {
1028 GST_DEBUG_OBJECT (src, "got chunk, but not for current message");
1031 if (G_UNLIKELY (src->session_io_status !=
1032 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
1033 /* Probably a redirect. */
1036 basesrc = GST_BASE_SRC_CAST (src);
1037 GST_DEBUG_OBJECT (src, "got chunk of %" G_GSIZE_FORMAT " bytes",
1040 /* Extract the GstBuffer from the SoupBuffer and set its fields. */
1041 gchunk = (SoupGstChunk *) soup_buffer_get_owner (chunk);
1042 *src->outbuf = gchunk->buffer;
1044 gst_buffer_resize (*src->outbuf, 0, chunk->length);
1045 GST_BUFFER_OFFSET (*src->outbuf) = basesrc->segment.position;
1047 gst_buffer_ref (*src->outbuf);
1049 new_position = src->read_position + chunk->length;
1050 if (G_LIKELY (src->request_position == src->read_position))
1051 src->request_position = new_position;
1052 src->read_position = new_position;
1054 if (src->content_size != 0 && new_position > src->content_size) {
1055 GST_DEBUG_OBJECT (src, "Got position previous estimated content size "
1056 "(%" G_GINT64_FORMAT " > %" G_GINT64_FORMAT ")", new_position,
1058 src->content_size = new_position;
1059 basesrc->segment.duration = src->content_size;
1060 gst_element_post_message (GST_ELEMENT (src),
1061 gst_message_new_duration_changed (GST_OBJECT (src)));
1064 src->ret = GST_FLOW_OK;
1065 g_main_loop_quit (src->loop);
1066 gst_soup_http_src_session_pause_message (src);
1070 gst_soup_http_src_response_cb (SoupSession * session, SoupMessage * msg,
1071 GstSoupHTTPSrc * src)
1073 if (G_UNLIKELY (msg != src->msg)) {
1074 GST_DEBUG_OBJECT (src, "got response %d: %s, but not for current message",
1075 msg->status_code, msg->reason_phrase);
1078 if (G_UNLIKELY (src->session_io_status !=
1079 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)
1080 && SOUP_STATUS_IS_REDIRECTION (msg->status_code)) {
1081 /* Ignore redirections. */
1084 GST_DEBUG_OBJECT (src, "got response %d: %s", msg->status_code,
1085 msg->reason_phrase);
1086 if (src->session_io_status == GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING &&
1087 src->read_position > 0) {
1088 /* The server disconnected while streaming. Reconnect and seeking to the
1092 gst_soup_http_src_parse_status (msg, src);
1093 /* The session's SoupMessage object expires after this callback returns. */
1095 g_main_loop_quit (src->loop);
1098 #define SOUP_HTTP_SRC_ERROR(src,soup_msg,cat,code,error_message) \
1099 GST_ELEMENT_ERROR ((src), cat, code, ("%s", error_message), \
1100 ("%s (%d), URL: %s", (soup_msg)->reason_phrase, \
1101 (soup_msg)->status_code, (src)->location));
1104 gst_soup_http_src_parse_status (SoupMessage * msg, GstSoupHTTPSrc * src)
1106 if (SOUP_STATUS_IS_TRANSPORT_ERROR (msg->status_code)) {
1107 switch (msg->status_code) {
1108 case SOUP_STATUS_CANT_RESOLVE:
1109 case SOUP_STATUS_CANT_RESOLVE_PROXY:
1110 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, NOT_FOUND,
1111 _("Could not resolve server name."));
1112 src->ret = GST_FLOW_ERROR;
1114 case SOUP_STATUS_CANT_CONNECT:
1115 case SOUP_STATUS_CANT_CONNECT_PROXY:
1116 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ,
1117 _("Could not establish connection to server."));
1118 src->ret = GST_FLOW_ERROR;
1120 case SOUP_STATUS_SSL_FAILED:
1121 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ,
1122 _("Secure connection setup failed."));
1123 src->ret = GST_FLOW_ERROR;
1125 case SOUP_STATUS_IO_ERROR:
1126 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, READ,
1127 _("A network error occured, or the server closed the connection "
1129 src->ret = GST_FLOW_ERROR;
1131 case SOUP_STATUS_MALFORMED:
1132 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, READ,
1133 _("Server sent bad data."));
1134 src->ret = GST_FLOW_ERROR;
1136 case SOUP_STATUS_CANCELLED:
1137 /* No error message when interrupted by program. */
1140 } else if (SOUP_STATUS_IS_CLIENT_ERROR (msg->status_code) ||
1141 SOUP_STATUS_IS_REDIRECTION (msg->status_code) ||
1142 SOUP_STATUS_IS_SERVER_ERROR (msg->status_code)) {
1143 /* Report HTTP error. */
1144 /* FIXME: reason_phrase is not translated and not suitable for user
1145 * error dialog according to libsoup documentation.
1146 * FIXME: error code (OPEN_READ vs. READ) should depend on http status? */
1147 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1148 ("%s", msg->reason_phrase),
1149 ("%s (%d), URL: %s", msg->reason_phrase, msg->status_code,
1151 src->ret = GST_FLOW_ERROR;
1156 gst_soup_http_src_build_message (GstSoupHTTPSrc * src, const gchar * method)
1158 src->msg = soup_message_new (method, src->location);
1160 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1161 ("Error parsing URL."), ("URL: %s", src->location));
1164 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE;
1165 soup_message_headers_append (src->msg->request_headers, "Connection",
1167 if (src->iradio_mode) {
1168 soup_message_headers_append (src->msg->request_headers, "icy-metadata",
1174 for (cookie = src->cookies; *cookie != NULL; cookie++) {
1175 soup_message_headers_append (src->msg->request_headers, "Cookie",
1181 g_signal_connect (src->msg, "got_headers",
1182 G_CALLBACK (gst_soup_http_src_got_headers_cb), src);
1183 g_signal_connect (src->msg, "got_body",
1184 G_CALLBACK (gst_soup_http_src_got_body_cb), src);
1185 g_signal_connect (src->msg, "finished",
1186 G_CALLBACK (gst_soup_http_src_finished_cb), src);
1187 g_signal_connect (src->msg, "got_chunk",
1188 G_CALLBACK (gst_soup_http_src_got_chunk_cb), src);
1189 soup_message_set_flags (src->msg, SOUP_MESSAGE_OVERWRITE_CHUNKS |
1190 (src->automatic_redirect ? 0 : SOUP_MESSAGE_NO_REDIRECT));
1191 soup_message_set_chunk_allocator (src->msg,
1192 gst_soup_http_src_chunk_allocator, src, NULL);
1193 gst_soup_http_src_add_range_header (src, src->request_position);
1195 gst_soup_http_src_add_extra_headers (src);
1197 GST_DEBUG_OBJECT (src, "request headers:");
1198 soup_message_headers_foreach (src->msg->request_headers,
1199 gst_soup_http_src_headers_foreach, src);
1204 static GstFlowReturn
1205 gst_soup_http_src_do_request (GstSoupHTTPSrc * src, const gchar * method,
1206 GstBuffer ** outbuf)
1208 GST_LOG_OBJECT (src, "Running request for method: %s", method);
1209 if (src->msg && (src->request_position != src->read_position)) {
1210 if (src->session_io_status == GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE) {
1211 gst_soup_http_src_add_range_header (src, src->request_position);
1213 GST_DEBUG_OBJECT (src, "Seek from position %" G_GUINT64_FORMAT
1214 " to %" G_GUINT64_FORMAT ": requeueing connection request",
1215 src->read_position, src->request_position);
1216 gst_soup_http_src_cancel_message (src);
1220 if (!gst_soup_http_src_build_message (src, method)) {
1221 return GST_FLOW_ERROR;
1224 src->ret = GST_FLOW_CUSTOM_ERROR;
1225 src->outbuf = outbuf;
1227 if (src->interrupted) {
1228 GST_DEBUG_OBJECT (src, "interrupted");
1232 GST_DEBUG_OBJECT (src, "Reconnecting");
1233 if (!gst_soup_http_src_build_message (src, method)) {
1234 return GST_FLOW_ERROR;
1240 GST_DEBUG_OBJECT (src, "EOS reached");
1244 switch (src->session_io_status) {
1245 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE:
1246 GST_DEBUG_OBJECT (src, "Queueing connection request");
1247 gst_soup_http_src_queue_message (src);
1249 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_QUEUED:
1251 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING:
1252 gst_soup_http_src_session_unpause_message (src);
1254 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_CANCELLED:
1259 if (src->ret == GST_FLOW_CUSTOM_ERROR)
1260 g_main_loop_run (src->loop);
1261 } while (src->ret == GST_FLOW_CUSTOM_ERROR);
1263 if (src->ret == GST_FLOW_CUSTOM_ERROR)
1264 src->ret = GST_FLOW_EOS;
1265 g_cond_signal (&src->request_finished_cond);
1269 static GstFlowReturn
1270 gst_soup_http_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
1272 GstSoupHTTPSrc *src;
1275 src = GST_SOUP_HTTP_SRC (psrc);
1277 g_mutex_lock (&src->mutex);
1278 ret = gst_soup_http_src_do_request (src, SOUP_METHOD_GET, outbuf);
1279 g_mutex_unlock (&src->mutex);
1284 gst_soup_http_src_start (GstBaseSrc * bsrc)
1286 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1288 GST_DEBUG_OBJECT (src, "start(\"%s\")", src->location);
1290 return gst_soup_http_src_session_open (src);
1294 gst_soup_http_src_stop (GstBaseSrc * bsrc)
1296 GstSoupHTTPSrc *src;
1298 src = GST_SOUP_HTTP_SRC (bsrc);
1299 GST_DEBUG_OBJECT (src, "stop()");
1300 gst_soup_http_src_session_close (src);
1302 g_main_loop_unref (src->loop);
1303 g_main_context_unref (src->context);
1305 src->context = NULL;
1307 if (src->extra_headers) {
1308 gst_structure_free (src->extra_headers);
1309 src->extra_headers = NULL;
1312 gst_soup_http_src_reset (src);
1316 /* Interrupt a blocking request. */
1318 gst_soup_http_src_unlock (GstBaseSrc * bsrc)
1320 GstSoupHTTPSrc *src;
1322 src = GST_SOUP_HTTP_SRC (bsrc);
1323 GST_DEBUG_OBJECT (src, "unlock()");
1325 src->interrupted = TRUE;
1327 g_main_loop_quit (src->loop);
1328 g_cond_signal (&src->request_finished_cond);
1332 /* Interrupt interrupt. */
1334 gst_soup_http_src_unlock_stop (GstBaseSrc * bsrc)
1336 GstSoupHTTPSrc *src;
1338 src = GST_SOUP_HTTP_SRC (bsrc);
1339 GST_DEBUG_OBJECT (src, "unlock_stop()");
1341 src->interrupted = FALSE;
1346 gst_soup_http_src_get_size (GstBaseSrc * bsrc, guint64 * size)
1348 GstSoupHTTPSrc *src;
1350 src = GST_SOUP_HTTP_SRC (bsrc);
1352 if (src->have_size) {
1353 GST_DEBUG_OBJECT (src, "get_size() = %" G_GUINT64_FORMAT,
1355 *size = src->content_size;
1358 GST_DEBUG_OBJECT (src, "get_size() = FALSE");
1363 gst_soup_http_src_is_seekable (GstBaseSrc * bsrc)
1365 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1366 GstFlowReturn ret = GST_FLOW_OK;
1368 /* Special case to check if the server allows range requests
1369 * before really starting to get data in the buffer creation
1372 if (!src->got_headers && GST_STATE (src) != GST_STATE_NULL) {
1373 g_mutex_lock (&src->mutex);
1374 while (!src->got_headers && !src->interrupted && ret == GST_FLOW_OK) {
1375 if ((src->msg && src->msg->method != SOUP_METHOD_HEAD) &&
1376 src->session_io_status != GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE) {
1377 /* wait for the current request to finish */
1378 g_cond_wait (&src->request_finished_cond, &src->mutex);
1380 if (gst_soup_http_src_session_open (src)) {
1381 ret = gst_soup_http_src_do_request (src, SOUP_METHOD_HEAD, NULL);
1385 if (src->ret == GST_FLOW_EOS) {
1386 /* A HEAD request shouldn't lead to EOS */
1387 src->ret = GST_FLOW_OK;
1389 /* resets status to idle */
1390 gst_soup_http_src_cancel_message (src);
1391 g_mutex_unlock (&src->mutex);
1394 return src->seekable;
1398 gst_soup_http_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
1400 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1402 GST_DEBUG_OBJECT (src, "do_seek(%" G_GUINT64_FORMAT ")", segment->start);
1404 if (src->read_position == segment->start &&
1405 src->request_position == src->read_position) {
1406 GST_DEBUG_OBJECT (src, "Seek to current read position and no seek pending");
1410 if (!src->seekable) {
1411 GST_WARNING_OBJECT (src, "Not seekable");
1415 if (segment->rate < 0.0 || segment->format != GST_FORMAT_BYTES) {
1416 GST_WARNING_OBJECT (src, "Invalid seek segment");
1420 if (src->content_size != 0 && segment->start >= src->content_size) {
1421 GST_WARNING_OBJECT (src,
1422 "Potentially seeking behind end of file, might EOS immediately");
1425 /* Wait for create() to handle the jump in offset. */
1426 src->request_position = segment->start;
1431 gst_soup_http_src_query (GstBaseSrc * bsrc, GstQuery * query)
1433 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1435 GstSchedulingFlags flags;
1436 gint minsize, maxsize, align;
1438 switch (GST_QUERY_TYPE (query)) {
1440 gst_query_set_uri (query, src->location);
1449 ret = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
1451 switch (GST_QUERY_TYPE (query)) {
1452 case GST_QUERY_SCHEDULING:
1453 gst_query_parse_scheduling (query, &flags, &minsize, &maxsize, &align);
1454 flags |= GST_SCHEDULING_FLAG_BANDWIDTH_LIMITED;
1455 gst_query_set_scheduling (query, flags, minsize, maxsize, align);
1465 gst_soup_http_src_set_location (GstSoupHTTPSrc * src, const gchar * uri,
1468 const char *alt_schemes[] = { "icy://", "icyx://" };
1471 if (src->location) {
1472 g_free (src->location);
1473 src->location = NULL;
1479 for (i = 0; i < G_N_ELEMENTS (alt_schemes); i++) {
1480 if (g_str_has_prefix (uri, alt_schemes[i])) {
1482 g_strdup_printf ("http://%s", uri + strlen (alt_schemes[i]));
1487 src->location = g_strdup (uri);
1493 gst_soup_http_src_set_proxy (GstSoupHTTPSrc * src, const gchar * uri)
1496 soup_uri_free (src->proxy);
1499 if (g_str_has_prefix (uri, "http://")) {
1500 src->proxy = soup_uri_new (uri);
1502 gchar *new_uri = g_strconcat ("http://", uri, NULL);
1504 src->proxy = soup_uri_new (new_uri);
1512 gst_soup_http_src_uri_get_type (GType type)
1517 static const gchar *const *
1518 gst_soup_http_src_uri_get_protocols (GType type)
1520 static const gchar *protocols[] = { "http", "https", "icy", "icyx", NULL };
1526 gst_soup_http_src_uri_get_uri (GstURIHandler * handler)
1528 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (handler);
1530 /* FIXME: make thread-safe */
1531 return g_strdup (src->location);
1535 gst_soup_http_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
1538 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (handler);
1540 return gst_soup_http_src_set_location (src, uri, error);
1544 gst_soup_http_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1546 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1548 iface->get_type = gst_soup_http_src_uri_get_type;
1549 iface->get_protocols = gst_soup_http_src_uri_get_protocols;
1550 iface->get_uri = gst_soup_http_src_uri_get_uri;
1551 iface->set_uri = gst_soup_http_src_uri_set_uri;