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,
144 guint64 offset, guint64 stop_offset);
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->stop_position = -1;
283 src->content_size = 0;
284 src->have_body = FALSE;
286 gst_caps_replace (&src->src_caps, NULL);
287 g_free (src->iradio_name);
288 src->iradio_name = NULL;
289 g_free (src->iradio_genre);
290 src->iradio_genre = NULL;
291 g_free (src->iradio_url);
292 src->iradio_url = NULL;
296 gst_soup_http_src_init (GstSoupHTTPSrc * src)
300 g_mutex_init (&src->mutex);
301 g_cond_init (&src->request_finished_cond);
302 src->location = NULL;
303 src->redirection_uri = NULL;
304 src->automatic_redirect = TRUE;
305 src->user_agent = g_strdup (DEFAULT_USER_AGENT);
308 src->proxy_id = NULL;
309 src->proxy_pw = NULL;
311 src->iradio_mode = DEFAULT_IRADIO_MODE;
316 proxy = g_getenv ("http_proxy");
317 if (proxy && !gst_soup_http_src_set_proxy (src, proxy)) {
318 GST_WARNING_OBJECT (src,
319 "The proxy in the http_proxy env var (\"%s\") cannot be parsed.",
323 gst_soup_http_src_reset (src);
327 gst_soup_http_src_finalize (GObject * gobject)
329 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (gobject);
331 GST_DEBUG_OBJECT (src, "finalize");
333 g_mutex_clear (&src->mutex);
334 g_cond_clear (&src->request_finished_cond);
335 g_free (src->location);
336 if (src->redirection_uri) {
337 g_free (src->redirection_uri);
339 g_free (src->user_agent);
340 if (src->proxy != NULL) {
341 soup_uri_free (src->proxy);
343 g_free (src->user_id);
344 g_free (src->user_pw);
345 g_free (src->proxy_id);
346 g_free (src->proxy_pw);
347 g_strfreev (src->cookies);
349 G_OBJECT_CLASS (parent_class)->finalize (gobject);
353 gst_soup_http_src_set_property (GObject * object, guint prop_id,
354 const GValue * value, GParamSpec * pspec)
356 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (object);
361 const gchar *location;
363 location = g_value_get_string (value);
365 if (location == NULL) {
366 GST_WARNING ("location property cannot be NULL");
369 if (!gst_soup_http_src_set_location (src, location, NULL)) {
370 GST_WARNING ("badly formatted location");
375 case PROP_USER_AGENT:
377 g_free (src->user_agent);
378 src->user_agent = g_value_dup_string (value);
380 case PROP_IRADIO_MODE:
381 src->iradio_mode = g_value_get_boolean (value);
383 case PROP_AUTOMATIC_REDIRECT:
384 src->automatic_redirect = g_value_get_boolean (value);
390 proxy = g_value_get_string (value);
393 GST_WARNING ("proxy property cannot be NULL");
396 if (!gst_soup_http_src_set_proxy (src, proxy)) {
397 GST_WARNING ("badly formatted proxy URI");
403 g_strfreev (src->cookies);
404 src->cookies = g_strdupv (g_value_get_boxed (value));
407 gst_base_src_set_live (GST_BASE_SRC (src), g_value_get_boolean (value));
411 g_free (src->user_id);
412 src->user_id = g_value_dup_string (value);
416 g_free (src->user_pw);
417 src->user_pw = g_value_dup_string (value);
421 g_free (src->proxy_id);
422 src->proxy_id = g_value_dup_string (value);
426 g_free (src->proxy_pw);
427 src->proxy_pw = g_value_dup_string (value);
430 src->timeout = g_value_get_uint (value);
432 case PROP_EXTRA_HEADERS:{
433 const GstStructure *s = gst_value_get_structure (value);
435 if (src->extra_headers)
436 gst_structure_free (src->extra_headers);
438 src->extra_headers = s ? gst_structure_copy (s) : NULL;
442 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
450 gst_soup_http_src_get_property (GObject * object, guint prop_id,
451 GValue * value, GParamSpec * pspec)
453 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (object);
457 g_value_set_string (value, src->location);
459 case PROP_USER_AGENT:
460 g_value_set_string (value, src->user_agent);
462 case PROP_AUTOMATIC_REDIRECT:
463 g_value_set_boolean (value, src->automatic_redirect);
466 if (src->proxy == NULL)
467 g_value_set_static_string (value, "");
469 char *proxy = soup_uri_to_string (src->proxy, FALSE);
471 g_value_set_string (value, proxy);
476 g_value_set_boxed (value, g_strdupv (src->cookies));
479 g_value_set_boolean (value, gst_base_src_is_live (GST_BASE_SRC (src)));
481 case PROP_IRADIO_MODE:
482 g_value_set_boolean (value, src->iradio_mode);
485 g_value_set_string (value, src->user_id);
488 g_value_set_string (value, src->user_pw);
491 g_value_set_string (value, src->proxy_id);
494 g_value_set_string (value, src->proxy_pw);
497 g_value_set_uint (value, src->timeout);
499 case PROP_EXTRA_HEADERS:
500 gst_value_set_structure (value, src->extra_headers);
503 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
509 gst_soup_http_src_unicodify (const gchar * str)
511 const gchar *env_vars[] = { "GST_ICY_TAG_ENCODING",
512 "GST_TAG_ENCODING", NULL
515 return gst_tag_freeform_string_to_utf8 (str, -1, env_vars);
519 gst_soup_http_src_cancel_message (GstSoupHTTPSrc * src)
521 if (src->msg != NULL) {
522 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_CANCELLED;
523 soup_session_cancel_message (src->session, src->msg, SOUP_STATUS_CANCELLED);
525 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE;
530 gst_soup_http_src_queue_message (GstSoupHTTPSrc * src)
532 soup_session_queue_message (src->session, src->msg,
533 (SoupSessionCallback) gst_soup_http_src_response_cb, src);
534 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_QUEUED;
538 gst_soup_http_src_add_range_header (GstSoupHTTPSrc * src, guint64 offset,
545 soup_message_headers_remove (src->msg->request_headers, "Range");
546 if (offset || stop_offset != -1) {
547 if (stop_offset != -1) {
548 rc = g_snprintf (buf, sizeof (buf), "bytes=%" G_GUINT64_FORMAT "-%"
549 G_GUINT64_FORMAT, offset, stop_offset);
551 rc = g_snprintf (buf, sizeof (buf), "bytes=%" G_GUINT64_FORMAT "-",
554 if (rc > sizeof (buf) || rc < 0)
556 soup_message_headers_append (src->msg->request_headers, "Range", buf);
558 src->read_position = offset;
563 _append_extra_header (GQuark field_id, const GValue * value, gpointer user_data)
565 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (user_data);
566 const gchar *field_name = g_quark_to_string (field_id);
567 gchar *field_content = NULL;
569 if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
570 field_content = g_value_dup_string (value);
572 GValue dest = { 0, };
574 g_value_init (&dest, G_TYPE_STRING);
575 if (g_value_transform (value, &dest)) {
576 field_content = g_value_dup_string (&dest);
580 if (field_content == NULL) {
581 GST_ERROR_OBJECT (src, "extra-headers field '%s' contains no value "
582 "or can't be converted to a string", field_name);
586 GST_DEBUG_OBJECT (src, "Appending extra header: \"%s: %s\"", field_name,
588 soup_message_headers_append (src->msg->request_headers, field_name,
591 g_free (field_content);
597 _append_extra_headers (GQuark field_id, const GValue * value,
600 if (G_VALUE_TYPE (value) == GST_TYPE_ARRAY) {
601 guint n = gst_value_array_get_size (value);
604 for (i = 0; i < n; i++) {
605 const GValue *v = gst_value_array_get_value (value, i);
607 if (!_append_extra_header (field_id, v, user_data))
610 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
611 guint n = gst_value_list_get_size (value);
614 for (i = 0; i < n; i++) {
615 const GValue *v = gst_value_list_get_value (value, i);
617 if (!_append_extra_header (field_id, v, user_data))
621 return _append_extra_header (field_id, value, user_data);
629 gst_soup_http_src_add_extra_headers (GstSoupHTTPSrc * src)
631 if (!src->extra_headers)
634 return gst_structure_foreach (src->extra_headers, _append_extra_headers, src);
639 gst_soup_http_src_session_unpause_message (GstSoupHTTPSrc * src)
641 soup_session_unpause_message (src->session, src->msg);
645 gst_soup_http_src_session_pause_message (GstSoupHTTPSrc * src)
647 soup_session_pause_message (src->session, src->msg);
651 gst_soup_http_src_session_open (GstSoupHTTPSrc * src)
654 GST_DEBUG_OBJECT (src, "Session is already open");
658 if (!src->location) {
659 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (_("No URL set.")),
660 ("Missing location property"));
664 src->context = g_main_context_new ();
666 src->loop = g_main_loop_new (src->context, TRUE);
668 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
669 (NULL), ("Failed to start GMainLoop"));
670 g_main_context_unref (src->context);
674 GST_DEBUG_OBJECT (src, "Creating session");
675 if (src->proxy == NULL) {
677 soup_session_async_new_with_options (SOUP_SESSION_ASYNC_CONTEXT,
678 src->context, SOUP_SESSION_USER_AGENT, src->user_agent,
679 SOUP_SESSION_TIMEOUT, src->timeout,
680 SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_PROXY_RESOLVER_DEFAULT,
684 soup_session_async_new_with_options (SOUP_SESSION_ASYNC_CONTEXT,
685 src->context, SOUP_SESSION_PROXY_URI, src->proxy,
686 SOUP_SESSION_TIMEOUT, src->timeout,
687 SOUP_SESSION_USER_AGENT, src->user_agent, NULL);
691 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
692 (NULL), ("Failed to create async session"));
696 g_signal_connect (src->session, "authenticate",
697 G_CALLBACK (gst_soup_http_src_authenticate_cb), src);
702 gst_soup_http_src_session_close (GstSoupHTTPSrc * src)
705 soup_session_abort (src->session); /* This unrefs the message. */
706 g_object_unref (src->session);
713 gst_soup_http_src_authenticate_cb (SoupSession * session, SoupMessage * msg,
714 SoupAuth * auth, gboolean retrying, GstSoupHTTPSrc * src)
717 /* First time authentication only, if we fail and are called again with retry true fall through */
718 if (msg->status_code == SOUP_STATUS_UNAUTHORIZED) {
719 if (src->user_id && src->user_pw)
720 soup_auth_authenticate (auth, src->user_id, src->user_pw);
721 } else if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
722 if (src->proxy_id && src->proxy_pw)
723 soup_auth_authenticate (auth, src->proxy_id, src->proxy_pw);
729 gst_soup_http_src_headers_foreach (const gchar * name, const gchar * val,
732 GST_DEBUG_OBJECT (src, " %s: %s", name, val);
736 gst_soup_http_src_got_headers_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
739 GstTagList *tag_list;
742 GHashTable *params = NULL;
744 GST_DEBUG_OBJECT (src, "got headers:");
745 soup_message_headers_foreach (msg->response_headers,
746 gst_soup_http_src_headers_foreach, src);
748 if (msg->status_code == 407 && src->proxy_id && src->proxy_pw)
751 if (src->automatic_redirect && SOUP_STATUS_IS_REDIRECTION (msg->status_code)) {
752 src->redirection_uri = g_strdup (soup_message_headers_get_one
753 (msg->response_headers, "Location"));
754 GST_DEBUG_OBJECT (src, "%u redirect to \"%s\"", msg->status_code,
755 src->redirection_uri);
759 if (msg->status_code == SOUP_STATUS_UNAUTHORIZED)
762 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING;
763 src->got_headers = TRUE;
765 /* Parse Content-Length. */
766 if (soup_message_headers_get_encoding (msg->response_headers) ==
767 SOUP_ENCODING_CONTENT_LENGTH) {
768 newsize = src->request_position +
769 soup_message_headers_get_content_length (msg->response_headers);
770 if (!src->have_size || (src->content_size != newsize)) {
771 src->content_size = newsize;
772 src->have_size = TRUE;
773 src->seekable = TRUE;
774 GST_DEBUG_OBJECT (src, "size = %" G_GUINT64_FORMAT, src->content_size);
776 basesrc = GST_BASE_SRC_CAST (src);
777 basesrc->segment.duration = src->content_size;
778 gst_element_post_message (GST_ELEMENT (src),
779 gst_message_new_duration_changed (GST_OBJECT (src)));
784 tag_list = gst_tag_list_new_empty ();
787 soup_message_headers_get_one (msg->response_headers,
788 "icy-metaint")) != NULL) {
789 gint icy_metaint = atoi (value);
791 GST_DEBUG_OBJECT (src, "icy-metaint: %s (parsed: %d)", value, icy_metaint);
792 if (icy_metaint > 0) {
794 gst_caps_unref (src->src_caps);
796 src->src_caps = gst_caps_new_simple ("application/x-icy",
797 "metadata-interval", G_TYPE_INT, icy_metaint, NULL);
799 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
803 soup_message_headers_get_content_type (msg->response_headers,
805 GST_DEBUG_OBJECT (src, "Content-Type: %s", value);
806 if (g_ascii_strcasecmp (value, "audio/L16") == 0) {
812 gst_caps_unref (src->src_caps);
814 param = g_hash_table_lookup (params, "channels");
816 channels = atol (param);
818 param = g_hash_table_lookup (params, "rate");
822 src->src_caps = gst_caps_new_simple ("audio/x-raw",
823 "format", G_TYPE_STRING, "S16BE",
824 "layout", G_TYPE_STRING, "interleaved",
825 "channels", G_TYPE_INT, channels, "rate", G_TYPE_INT, rate, NULL);
827 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
829 /* Set the Content-Type field on the caps */
831 src->src_caps = gst_caps_make_writable (src->src_caps);
832 gst_caps_set_simple (src->src_caps, "content-type", G_TYPE_STRING,
834 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
840 g_hash_table_destroy (params);
843 soup_message_headers_get_one (msg->response_headers,
844 "icy-name")) != NULL) {
845 g_free (src->iradio_name);
846 src->iradio_name = gst_soup_http_src_unicodify (value);
847 if (src->iradio_name) {
848 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_ORGANIZATION,
849 src->iradio_name, NULL);
853 soup_message_headers_get_one (msg->response_headers,
854 "icy-genre")) != NULL) {
855 g_free (src->iradio_genre);
856 src->iradio_genre = gst_soup_http_src_unicodify (value);
857 if (src->iradio_genre) {
858 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_GENRE,
859 src->iradio_genre, NULL);
862 if ((value = soup_message_headers_get_one (msg->response_headers, "icy-url"))
864 g_free (src->iradio_url);
865 src->iradio_url = gst_soup_http_src_unicodify (value);
866 if (src->iradio_url) {
867 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_LOCATION,
868 src->iradio_url, NULL);
871 if (!gst_tag_list_is_empty (tag_list)) {
872 GST_DEBUG_OBJECT (src,
873 "calling gst_element_found_tags with %" GST_PTR_FORMAT, tag_list);
874 gst_pad_push_event (GST_BASE_SRC_PAD (src), gst_event_new_tag (tag_list));
876 gst_tag_list_unref (tag_list);
879 /* Handle HTTP errors. */
880 gst_soup_http_src_parse_status (msg, src);
882 /* Check if Range header was respected. */
883 if (src->ret == GST_FLOW_CUSTOM_ERROR &&
884 src->read_position && msg->status_code != SOUP_STATUS_PARTIAL_CONTENT) {
885 src->seekable = FALSE;
886 GST_ELEMENT_ERROR (src, RESOURCE, SEEK,
887 (_("Server does not support seeking.")),
888 ("Server does not accept Range HTTP header, URL: %s", src->location));
889 src->ret = GST_FLOW_ERROR;
892 /* If we are going to error out, stop all processing right here, so we
893 * don't output any data (such as an error html page), and return
894 * GST_FLOW_ERROR from the create function instead of having
895 * got_chunk_cb overwrite src->ret with FLOW_OK again. */
896 if (src->ret == GST_FLOW_ERROR || src->ret == GST_FLOW_EOS) {
897 gst_soup_http_src_session_pause_message (src);
900 g_main_loop_quit (src->loop);
902 g_cond_signal (&src->request_finished_cond);
905 /* Have body. Signal EOS. */
907 gst_soup_http_src_got_body_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
909 if (G_UNLIKELY (msg != src->msg)) {
910 GST_DEBUG_OBJECT (src, "got body, but not for current message");
913 if (G_UNLIKELY (src->session_io_status !=
914 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
915 /* Probably a redirect. */
918 GST_DEBUG_OBJECT (src, "got body");
919 src->ret = GST_FLOW_EOS;
920 src->have_body = TRUE;
922 /* no need to interrupt the message here, we do it on the
923 * finished_cb anyway if needed. And getting the body might mean
924 * that the connection was hang up before finished. This happens when
925 * the pipeline is stalled for too long (long pauses during playback).
926 * Best to let it continue from here and pause because it reached the
927 * final bytes based on content_size or received an out of range error */
930 /* Finished. Signal EOS. */
932 gst_soup_http_src_finished_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
934 if (G_UNLIKELY (msg != src->msg)) {
935 GST_DEBUG_OBJECT (src, "finished, but not for current message");
938 GST_DEBUG_OBJECT (src, "finished");
939 src->ret = GST_FLOW_EOS;
940 if (src->session_io_status == GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_CANCELLED) {
941 /* gst_soup_http_src_cancel_message() triggered this; probably a seek
942 * that occurred in the QUEUEING state; i.e. before the connection setup
943 * was complete. Do nothing */
944 } else if (src->session_io_status ==
945 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING && src->read_position > 0 &&
946 (!src->have_size || src->read_position < src->content_size)) {
947 /* The server disconnected while streaming. Reconnect and seeking to the
950 src->ret = GST_FLOW_CUSTOM_ERROR;
951 } else if (G_UNLIKELY (src->session_io_status !=
952 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
953 if (msg->method == SOUP_METHOD_HEAD) {
954 GST_DEBUG_OBJECT (src, "Ignoring error %d:%s during HEAD request",
955 msg->status_code, msg->reason_phrase);
957 /* FIXME: reason_phrase is not translated, add proper error message */
958 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
959 ("%s", msg->reason_phrase),
960 ("libsoup status code %d", msg->status_code));
964 g_main_loop_quit (src->loop);
965 g_cond_signal (&src->request_finished_cond);
968 /* Buffer lifecycle management.
970 * gst_soup_http_src_create() runs the GMainLoop for this element, to let
972 * A GstBuffer is allocated in gst_soup_http_src_chunk_allocator() and
973 * associated with a SoupBuffer.
974 * Soup reads HTTP data in the GstBuffer's data buffer.
975 * The gst_soup_http_src_got_chunk_cb() is then called with the SoupBuffer.
976 * That sets gst_soup_http_src_create()'s return argument to the GstBuffer,
977 * increments its refcount (to 2), pauses the flow of data from the HTTP
978 * source to prevent gst_soup_http_src_got_chunk_cb() from being called
979 * again and breaks out of the GMainLoop.
980 * Because the SOUP_MESSAGE_OVERWRITE_CHUNKS flag is set, Soup frees the
981 * SoupBuffer and calls gst_soup_http_src_chunk_free(), which decrements the
983 * gst_soup_http_src_create() returns the GstBuffer. It will be freed by a
984 * downstream element.
985 * If Soup fails to read HTTP data, it does not call
986 * gst_soup_http_src_got_chunk_cb(), but still frees the SoupBuffer and
987 * calls gst_soup_http_src_chunk_free(), which decrements the GstBuffer's
988 * refcount to 0, freeing it.
998 gst_soup_http_src_chunk_free (gpointer user_data)
1000 SoupGstChunk *chunk = (SoupGstChunk *) user_data;
1002 gst_buffer_unmap (chunk->buffer, &chunk->map);
1003 gst_buffer_unref (chunk->buffer);
1004 g_slice_free (SoupGstChunk, chunk);
1008 gst_soup_http_src_chunk_allocator (SoupMessage * msg, gsize max_len,
1011 GstSoupHTTPSrc *src = (GstSoupHTTPSrc *) user_data;
1012 GstBaseSrc *basesrc = GST_BASE_SRC_CAST (src);
1014 SoupBuffer *soupbuf;
1017 SoupGstChunk *chunk;
1020 length = MIN (basesrc->blocksize, max_len);
1022 length = basesrc->blocksize;
1023 GST_DEBUG_OBJECT (src, "alloc %" G_GSIZE_FORMAT " bytes <= %" G_GSIZE_FORMAT,
1026 rc = GST_BASE_SRC_CLASS (parent_class)->alloc (basesrc, -1, length, &gstbuf);
1027 if (G_UNLIKELY (rc != GST_FLOW_OK)) {
1028 /* Failed to allocate buffer. Stall SoupSession and return error code
1031 g_main_loop_quit (src->loop);
1035 chunk = g_slice_new0 (SoupGstChunk);
1036 chunk->buffer = gstbuf;
1037 gst_buffer_map (gstbuf, &chunk->map, GST_MAP_READWRITE);
1039 soupbuf = soup_buffer_new_with_owner (chunk->map.data, chunk->map.size,
1040 chunk, gst_soup_http_src_chunk_free);
1046 gst_soup_http_src_got_chunk_cb (SoupMessage * msg, SoupBuffer * chunk,
1047 GstSoupHTTPSrc * src)
1049 GstBaseSrc *basesrc;
1050 guint64 new_position;
1051 SoupGstChunk *gchunk;
1053 if (G_UNLIKELY (msg != src->msg)) {
1054 GST_DEBUG_OBJECT (src, "got chunk, but not for current message");
1057 src->have_body = FALSE;
1058 if (G_UNLIKELY (src->session_io_status !=
1059 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
1060 /* Probably a redirect. */
1063 basesrc = GST_BASE_SRC_CAST (src);
1064 GST_DEBUG_OBJECT (src, "got chunk of %" G_GSIZE_FORMAT " bytes",
1067 /* Extract the GstBuffer from the SoupBuffer and set its fields. */
1068 gchunk = (SoupGstChunk *) soup_buffer_get_owner (chunk);
1069 *src->outbuf = gchunk->buffer;
1071 gst_buffer_resize (*src->outbuf, 0, chunk->length);
1072 GST_BUFFER_OFFSET (*src->outbuf) = basesrc->segment.position;
1074 gst_buffer_ref (*src->outbuf);
1076 new_position = src->read_position + chunk->length;
1077 if (G_LIKELY (src->request_position == src->read_position))
1078 src->request_position = new_position;
1079 src->read_position = new_position;
1081 if (src->content_size != 0 && new_position > src->content_size) {
1082 GST_DEBUG_OBJECT (src, "Got position previous estimated content size "
1083 "(%" G_GINT64_FORMAT " > %" G_GINT64_FORMAT ")", new_position,
1085 src->content_size = new_position;
1086 basesrc->segment.duration = src->content_size;
1087 gst_element_post_message (GST_ELEMENT (src),
1088 gst_message_new_duration_changed (GST_OBJECT (src)));
1091 src->ret = GST_FLOW_OK;
1092 g_main_loop_quit (src->loop);
1093 gst_soup_http_src_session_pause_message (src);
1097 gst_soup_http_src_response_cb (SoupSession * session, SoupMessage * msg,
1098 GstSoupHTTPSrc * src)
1100 if (G_UNLIKELY (msg != src->msg)) {
1101 GST_DEBUG_OBJECT (src, "got response %d: %s, but not for current message",
1102 msg->status_code, msg->reason_phrase);
1105 if (G_UNLIKELY (src->session_io_status !=
1106 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)
1107 && SOUP_STATUS_IS_REDIRECTION (msg->status_code)) {
1108 /* Ignore redirections. */
1111 GST_DEBUG_OBJECT (src, "got response %d: %s", msg->status_code,
1112 msg->reason_phrase);
1113 if (src->session_io_status == GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING &&
1114 src->read_position > 0) {
1115 /* The server disconnected while streaming. Reconnect and seeking to the
1119 gst_soup_http_src_parse_status (msg, src);
1120 /* The session's SoupMessage object expires after this callback returns. */
1122 g_main_loop_quit (src->loop);
1125 #define SOUP_HTTP_SRC_ERROR(src,soup_msg,cat,code,error_message) \
1126 GST_ELEMENT_ERROR ((src), cat, code, ("%s", error_message), \
1127 ("%s (%d), URL: %s", (soup_msg)->reason_phrase, \
1128 (soup_msg)->status_code, (src)->location));
1131 gst_soup_http_src_parse_status (SoupMessage * msg, GstSoupHTTPSrc * src)
1133 if (msg->method == SOUP_METHOD_HEAD) {
1134 if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code))
1135 GST_DEBUG_OBJECT (src, "Ignoring error %d during HEAD request",
1137 } else if (SOUP_STATUS_IS_TRANSPORT_ERROR (msg->status_code)) {
1138 switch (msg->status_code) {
1139 case SOUP_STATUS_CANT_RESOLVE:
1140 case SOUP_STATUS_CANT_RESOLVE_PROXY:
1141 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, NOT_FOUND,
1142 _("Could not resolve server name."));
1143 src->ret = GST_FLOW_ERROR;
1145 case SOUP_STATUS_CANT_CONNECT:
1146 case SOUP_STATUS_CANT_CONNECT_PROXY:
1147 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ,
1148 _("Could not establish connection to server."));
1149 src->ret = GST_FLOW_ERROR;
1151 case SOUP_STATUS_SSL_FAILED:
1152 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ,
1153 _("Secure connection setup failed."));
1154 src->ret = GST_FLOW_ERROR;
1156 case SOUP_STATUS_IO_ERROR:
1157 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, READ,
1158 _("A network error occured, or the server closed the connection "
1160 src->ret = GST_FLOW_ERROR;
1162 case SOUP_STATUS_MALFORMED:
1163 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, READ,
1164 _("Server sent bad data."));
1165 src->ret = GST_FLOW_ERROR;
1167 case SOUP_STATUS_CANCELLED:
1168 /* No error message when interrupted by program. */
1171 } else if (SOUP_STATUS_IS_CLIENT_ERROR (msg->status_code) ||
1172 SOUP_STATUS_IS_REDIRECTION (msg->status_code) ||
1173 SOUP_STATUS_IS_SERVER_ERROR (msg->status_code)) {
1174 /* Report HTTP error. */
1176 /* when content_size is unknown and we have just finished receiving
1177 * a body message, requests that go beyond the content limits will result
1178 * in an error. Here we convert those to EOS */
1179 if (msg->status_code == SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE &&
1180 src->have_body && src->have_size) {
1181 GST_DEBUG_OBJECT (src, "Requested range out of limits and received full "
1182 "body, returning EOS");
1183 src->ret = GST_FLOW_EOS;
1187 /* FIXME: reason_phrase is not translated and not suitable for user
1188 * error dialog according to libsoup documentation.
1189 * FIXME: error code (OPEN_READ vs. READ) should depend on http status? */
1190 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1191 ("%s", msg->reason_phrase),
1192 ("%s (%d), URL: %s", msg->reason_phrase, msg->status_code,
1194 src->ret = GST_FLOW_ERROR;
1199 gst_soup_http_src_build_message (GstSoupHTTPSrc * src, const gchar * method)
1201 src->msg = soup_message_new (method, src->location);
1203 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1204 ("Error parsing URL."), ("URL: %s", src->location));
1207 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE;
1208 soup_message_headers_append (src->msg->request_headers, "Connection",
1210 if (src->iradio_mode) {
1211 soup_message_headers_append (src->msg->request_headers, "icy-metadata",
1217 for (cookie = src->cookies; *cookie != NULL; cookie++) {
1218 soup_message_headers_append (src->msg->request_headers, "Cookie",
1224 g_signal_connect (src->msg, "got_headers",
1225 G_CALLBACK (gst_soup_http_src_got_headers_cb), src);
1226 g_signal_connect (src->msg, "got_body",
1227 G_CALLBACK (gst_soup_http_src_got_body_cb), src);
1228 g_signal_connect (src->msg, "finished",
1229 G_CALLBACK (gst_soup_http_src_finished_cb), src);
1230 g_signal_connect (src->msg, "got_chunk",
1231 G_CALLBACK (gst_soup_http_src_got_chunk_cb), src);
1232 soup_message_set_flags (src->msg, SOUP_MESSAGE_OVERWRITE_CHUNKS |
1233 (src->automatic_redirect ? 0 : SOUP_MESSAGE_NO_REDIRECT));
1234 soup_message_set_chunk_allocator (src->msg,
1235 gst_soup_http_src_chunk_allocator, src, NULL);
1236 gst_soup_http_src_add_range_header (src, src->request_position,
1237 src->stop_position);
1239 gst_soup_http_src_add_extra_headers (src);
1241 GST_DEBUG_OBJECT (src, "request headers:");
1242 soup_message_headers_foreach (src->msg->request_headers,
1243 gst_soup_http_src_headers_foreach, src);
1248 static GstFlowReturn
1249 gst_soup_http_src_do_request (GstSoupHTTPSrc * src, const gchar * method,
1250 GstBuffer ** outbuf)
1252 GST_LOG_OBJECT (src, "Running request for method: %s", method);
1253 if (src->msg && (src->request_position != src->read_position)) {
1254 if (src->session_io_status == GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE) {
1255 gst_soup_http_src_add_range_header (src, src->request_position,
1256 src->stop_position);
1258 GST_DEBUG_OBJECT (src, "Seek from position %" G_GUINT64_FORMAT
1259 " to %" G_GUINT64_FORMAT ": requeueing connection request",
1260 src->read_position, src->request_position);
1261 gst_soup_http_src_cancel_message (src);
1265 if (!gst_soup_http_src_build_message (src, method)) {
1266 return GST_FLOW_ERROR;
1269 src->ret = GST_FLOW_CUSTOM_ERROR;
1270 src->outbuf = outbuf;
1272 if (src->interrupted) {
1273 GST_DEBUG_OBJECT (src, "interrupted");
1277 GST_DEBUG_OBJECT (src, "Reconnecting");
1278 if (!gst_soup_http_src_build_message (src, method)) {
1279 return GST_FLOW_ERROR;
1285 GST_DEBUG_OBJECT (src, "EOS reached");
1289 switch (src->session_io_status) {
1290 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE:
1291 GST_DEBUG_OBJECT (src, "Queueing connection request");
1292 gst_soup_http_src_queue_message (src);
1294 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_QUEUED:
1296 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING:
1297 gst_soup_http_src_session_unpause_message (src);
1299 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_CANCELLED:
1304 if (src->ret == GST_FLOW_CUSTOM_ERROR)
1305 g_main_loop_run (src->loop);
1306 } while (src->ret == GST_FLOW_CUSTOM_ERROR);
1308 if (src->ret == GST_FLOW_CUSTOM_ERROR)
1309 src->ret = GST_FLOW_EOS;
1310 g_cond_signal (&src->request_finished_cond);
1314 static GstFlowReturn
1315 gst_soup_http_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
1317 GstSoupHTTPSrc *src;
1320 src = GST_SOUP_HTTP_SRC (psrc);
1322 g_mutex_lock (&src->mutex);
1323 ret = gst_soup_http_src_do_request (src, SOUP_METHOD_GET, outbuf);
1324 g_mutex_unlock (&src->mutex);
1329 gst_soup_http_src_start (GstBaseSrc * bsrc)
1331 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1333 GST_DEBUG_OBJECT (src, "start(\"%s\")", src->location);
1335 return gst_soup_http_src_session_open (src);
1339 gst_soup_http_src_stop (GstBaseSrc * bsrc)
1341 GstSoupHTTPSrc *src;
1343 src = GST_SOUP_HTTP_SRC (bsrc);
1344 GST_DEBUG_OBJECT (src, "stop()");
1345 gst_soup_http_src_session_close (src);
1347 g_main_loop_unref (src->loop);
1348 g_main_context_unref (src->context);
1350 src->context = NULL;
1352 if (src->extra_headers) {
1353 gst_structure_free (src->extra_headers);
1354 src->extra_headers = NULL;
1357 gst_soup_http_src_reset (src);
1361 /* Interrupt a blocking request. */
1363 gst_soup_http_src_unlock (GstBaseSrc * bsrc)
1365 GstSoupHTTPSrc *src;
1367 src = GST_SOUP_HTTP_SRC (bsrc);
1368 GST_DEBUG_OBJECT (src, "unlock()");
1370 src->interrupted = TRUE;
1372 g_main_loop_quit (src->loop);
1373 g_cond_signal (&src->request_finished_cond);
1377 /* Interrupt interrupt. */
1379 gst_soup_http_src_unlock_stop (GstBaseSrc * bsrc)
1381 GstSoupHTTPSrc *src;
1383 src = GST_SOUP_HTTP_SRC (bsrc);
1384 GST_DEBUG_OBJECT (src, "unlock_stop()");
1386 src->interrupted = FALSE;
1391 gst_soup_http_src_get_size (GstBaseSrc * bsrc, guint64 * size)
1393 GstSoupHTTPSrc *src;
1395 src = GST_SOUP_HTTP_SRC (bsrc);
1397 if (src->have_size) {
1398 GST_DEBUG_OBJECT (src, "get_size() = %" G_GUINT64_FORMAT,
1400 *size = src->content_size;
1403 GST_DEBUG_OBJECT (src, "get_size() = FALSE");
1408 gst_soup_http_src_check_seekable (GstSoupHTTPSrc * src)
1410 GstFlowReturn ret = GST_FLOW_OK;
1412 /* Special case to check if the server allows range requests
1413 * before really starting to get data in the buffer creation
1416 if (!src->got_headers && GST_STATE (src) >= GST_STATE_PAUSED) {
1417 g_mutex_lock (&src->mutex);
1418 while (!src->got_headers && !src->interrupted && ret == GST_FLOW_OK) {
1419 if ((src->msg && src->msg->method != SOUP_METHOD_HEAD) &&
1420 src->session_io_status != GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE) {
1421 /* wait for the current request to finish */
1422 g_cond_wait (&src->request_finished_cond, &src->mutex);
1424 if (gst_soup_http_src_session_open (src)) {
1425 ret = gst_soup_http_src_do_request (src, SOUP_METHOD_HEAD, NULL);
1429 if (src->ret == GST_FLOW_EOS) {
1430 /* A HEAD request shouldn't lead to EOS */
1431 src->ret = GST_FLOW_OK;
1433 /* resets status to idle */
1434 gst_soup_http_src_cancel_message (src);
1435 g_mutex_unlock (&src->mutex);
1441 gst_soup_http_src_is_seekable (GstBaseSrc * bsrc)
1443 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1445 gst_soup_http_src_check_seekable (src);
1447 return src->seekable;
1451 gst_soup_http_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
1453 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1455 GST_DEBUG_OBJECT (src, "do_seek(%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1456 ")", segment->start, segment->stop);
1457 if (src->read_position == segment->start &&
1458 src->request_position == src->read_position &&
1459 src->stop_position == segment->stop) {
1460 GST_DEBUG_OBJECT (src,
1461 "Seek to current read/end position and no seek pending");
1465 gst_soup_http_src_check_seekable (src);
1467 /* If we have no headers we don't know yet if it is seekable or not.
1468 * Store the start position and error out later if it isn't */
1469 if (src->got_headers && !src->seekable) {
1470 GST_WARNING_OBJECT (src, "Not seekable");
1474 if (segment->rate < 0.0 || segment->format != GST_FORMAT_BYTES) {
1475 GST_WARNING_OBJECT (src, "Invalid seek segment");
1479 if (src->content_size != 0 && segment->start >= src->content_size) {
1480 GST_WARNING_OBJECT (src,
1481 "Potentially seeking behind end of file, might EOS immediately");
1484 /* Wait for create() to handle the jump in offset. */
1485 src->request_position = segment->start;
1486 src->stop_position = segment->stop;
1491 gst_soup_http_src_query (GstBaseSrc * bsrc, GstQuery * query)
1493 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1495 GstSchedulingFlags flags;
1496 gint minsize, maxsize, align;
1498 switch (GST_QUERY_TYPE (query)) {
1500 gst_query_set_uri (query, src->location);
1501 if (src->redirection_uri != NULL)
1502 gst_query_set_uri_redirection (query, src->redirection_uri);
1511 ret = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
1513 switch (GST_QUERY_TYPE (query)) {
1514 case GST_QUERY_SCHEDULING:
1515 gst_query_parse_scheduling (query, &flags, &minsize, &maxsize, &align);
1516 flags |= GST_SCHEDULING_FLAG_BANDWIDTH_LIMITED;
1517 gst_query_set_scheduling (query, flags, minsize, maxsize, align);
1527 gst_soup_http_src_set_location (GstSoupHTTPSrc * src, const gchar * uri,
1530 const char *alt_schemes[] = { "icy://", "icyx://" };
1533 if (src->location) {
1534 g_free (src->location);
1535 src->location = NULL;
1541 for (i = 0; i < G_N_ELEMENTS (alt_schemes); i++) {
1542 if (g_str_has_prefix (uri, alt_schemes[i])) {
1544 g_strdup_printf ("http://%s", uri + strlen (alt_schemes[i]));
1549 if (src->redirection_uri) {
1550 g_free (src->redirection_uri);
1551 src->redirection_uri = NULL;
1554 src->location = g_strdup (uri);
1560 gst_soup_http_src_set_proxy (GstSoupHTTPSrc * src, const gchar * uri)
1563 soup_uri_free (src->proxy);
1566 if (g_str_has_prefix (uri, "http://")) {
1567 src->proxy = soup_uri_new (uri);
1569 gchar *new_uri = g_strconcat ("http://", uri, NULL);
1571 src->proxy = soup_uri_new (new_uri);
1579 gst_soup_http_src_uri_get_type (GType type)
1584 static const gchar *const *
1585 gst_soup_http_src_uri_get_protocols (GType type)
1587 static const gchar *protocols[] = { "http", "https", "icy", "icyx", NULL };
1593 gst_soup_http_src_uri_get_uri (GstURIHandler * handler)
1595 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (handler);
1597 /* FIXME: make thread-safe */
1598 return g_strdup (src->location);
1602 gst_soup_http_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
1605 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (handler);
1607 return gst_soup_http_src_set_location (src, uri, error);
1611 gst_soup_http_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1613 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1615 iface->get_type = gst_soup_http_src_uri_get_type;
1616 iface->get_protocols = gst_soup_http_src_uri_get_protocols;
1617 iface->get_uri = gst_soup_http_src_uri_get_uri;
1618 iface->set_uri = gst_soup_http_src_uri_set_uri;