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 #include <gst/tag/tag.h>
86 GST_DEBUG_CATEGORY_STATIC (souphttpsrc_debug);
87 #define GST_CAT_DEFAULT souphttpsrc_debug
89 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
100 PROP_AUTOMATIC_REDIRECT,
115 #define DEFAULT_USER_AGENT "GStreamer souphttpsrc "
116 #define DEFAULT_IRADIO_MODE TRUE
117 #define DEFAULT_SOUP_LOG_LEVEL SOUP_LOGGER_LOG_NONE
118 #define DEFAULT_COMPRESS FALSE
119 #define DEFAULT_KEEP_ALIVE FALSE
121 static void gst_soup_http_src_uri_handler_init (gpointer g_iface,
122 gpointer iface_data);
123 static void gst_soup_http_src_finalize (GObject * gobject);
124 static void gst_soup_http_src_dispose (GObject * gobject);
126 static void gst_soup_http_src_set_property (GObject * object, guint prop_id,
127 const GValue * value, GParamSpec * pspec);
128 static void gst_soup_http_src_get_property (GObject * object, guint prop_id,
129 GValue * value, GParamSpec * pspec);
131 static GstStateChangeReturn gst_soup_http_src_change_state (GstElement *
132 element, GstStateChange transition);
133 static GstFlowReturn gst_soup_http_src_create (GstPushSrc * psrc,
134 GstBuffer ** outbuf);
135 static gboolean gst_soup_http_src_start (GstBaseSrc * bsrc);
136 static gboolean gst_soup_http_src_stop (GstBaseSrc * bsrc);
137 static gboolean gst_soup_http_src_get_size (GstBaseSrc * bsrc, guint64 * size);
138 static gboolean gst_soup_http_src_is_seekable (GstBaseSrc * bsrc);
139 static gboolean gst_soup_http_src_do_seek (GstBaseSrc * bsrc,
140 GstSegment * segment);
141 static gboolean gst_soup_http_src_query (GstBaseSrc * bsrc, GstQuery * query);
142 static gboolean gst_soup_http_src_unlock (GstBaseSrc * bsrc);
143 static gboolean gst_soup_http_src_unlock_stop (GstBaseSrc * bsrc);
144 static gboolean gst_soup_http_src_set_location (GstSoupHTTPSrc * src,
145 const gchar * uri, GError ** error);
146 static gboolean gst_soup_http_src_set_proxy (GstSoupHTTPSrc * src,
148 static char *gst_soup_http_src_unicodify (const char *str);
149 static gboolean gst_soup_http_src_build_message (GstSoupHTTPSrc * src,
150 const gchar * method);
151 static void gst_soup_http_src_cancel_message (GstSoupHTTPSrc * src);
152 static void gst_soup_http_src_queue_message (GstSoupHTTPSrc * src);
153 static gboolean gst_soup_http_src_add_range_header (GstSoupHTTPSrc * src,
154 guint64 offset, guint64 stop_offset);
155 static void gst_soup_http_src_session_unpause_message (GstSoupHTTPSrc * src);
156 static void gst_soup_http_src_session_pause_message (GstSoupHTTPSrc * src);
157 static gboolean gst_soup_http_src_session_open (GstSoupHTTPSrc * src);
158 static void gst_soup_http_src_session_close (GstSoupHTTPSrc * src);
159 static void gst_soup_http_src_parse_status (SoupMessage * msg,
160 GstSoupHTTPSrc * src);
161 static void gst_soup_http_src_chunk_free (gpointer gstbuf);
162 static SoupBuffer *gst_soup_http_src_chunk_allocator (SoupMessage * msg,
163 gsize max_len, gpointer user_data);
164 static void gst_soup_http_src_got_chunk_cb (SoupMessage * msg,
165 SoupBuffer * chunk, GstSoupHTTPSrc * src);
166 static void gst_soup_http_src_response_cb (SoupSession * session,
167 SoupMessage * msg, GstSoupHTTPSrc * src);
168 static void gst_soup_http_src_got_headers_cb (SoupMessage * msg,
169 GstSoupHTTPSrc * src);
170 static void gst_soup_http_src_got_body_cb (SoupMessage * msg,
171 GstSoupHTTPSrc * src);
172 static void gst_soup_http_src_finished_cb (SoupMessage * msg,
173 GstSoupHTTPSrc * src);
174 static void gst_soup_http_src_authenticate_cb (SoupSession * session,
175 SoupMessage * msg, SoupAuth * auth, gboolean retrying,
176 GstSoupHTTPSrc * src);
178 #define gst_soup_http_src_parent_class parent_class
179 G_DEFINE_TYPE_WITH_CODE (GstSoupHTTPSrc, gst_soup_http_src, GST_TYPE_PUSH_SRC,
180 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
181 gst_soup_http_src_uri_handler_init));
184 gst_soup_http_src_class_init (GstSoupHTTPSrcClass * klass)
186 GObjectClass *gobject_class;
187 GstElementClass *gstelement_class;
188 GstBaseSrcClass *gstbasesrc_class;
189 GstPushSrcClass *gstpushsrc_class;
191 gobject_class = (GObjectClass *) klass;
192 gstelement_class = (GstElementClass *) klass;
193 gstbasesrc_class = (GstBaseSrcClass *) klass;
194 gstpushsrc_class = (GstPushSrcClass *) klass;
196 gobject_class->set_property = gst_soup_http_src_set_property;
197 gobject_class->get_property = gst_soup_http_src_get_property;
198 gobject_class->finalize = gst_soup_http_src_finalize;
199 gobject_class->dispose = gst_soup_http_src_dispose;
201 g_object_class_install_property (gobject_class,
203 g_param_spec_string ("location", "Location",
204 "Location to read from", "",
205 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
206 g_object_class_install_property (gobject_class,
208 g_param_spec_string ("user-agent", "User-Agent",
209 "Value of the User-Agent HTTP request header field",
210 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
211 g_object_class_install_property (gobject_class,
212 PROP_AUTOMATIC_REDIRECT,
213 g_param_spec_boolean ("automatic-redirect", "automatic-redirect",
214 "Automatically follow HTTP redirects (HTTP Status Code 3xx)",
215 TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
216 g_object_class_install_property (gobject_class,
218 g_param_spec_string ("proxy", "Proxy",
219 "HTTP proxy server URI", "",
220 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
221 g_object_class_install_property (gobject_class,
223 g_param_spec_string ("user-id", "user-id",
224 "HTTP location URI user id for authentication", "",
225 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
226 g_object_class_install_property (gobject_class, PROP_USER_PW,
227 g_param_spec_string ("user-pw", "user-pw",
228 "HTTP location URI user password for authentication", "",
229 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
230 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
231 g_param_spec_string ("proxy-id", "proxy-id",
232 "HTTP proxy URI user id for authentication", "",
233 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
234 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
235 g_param_spec_string ("proxy-pw", "proxy-pw",
236 "HTTP proxy URI user password for authentication", "",
237 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
238 g_object_class_install_property (gobject_class, PROP_COOKIES,
239 g_param_spec_boxed ("cookies", "Cookies", "HTTP request cookies",
240 G_TYPE_STRV, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
241 g_object_class_install_property (gobject_class, PROP_IS_LIVE,
242 g_param_spec_boolean ("is-live", "is-live", "Act like a live source",
243 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
244 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
245 g_param_spec_uint ("timeout", "timeout",
246 "Value in seconds to timeout a blocking I/O (0 = No timeout).", 0,
247 3600, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
248 g_object_class_install_property (gobject_class, PROP_EXTRA_HEADERS,
249 g_param_spec_boxed ("extra-headers", "Extra Headers",
250 "Extra headers to append to the HTTP request",
251 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
252 g_object_class_install_property (gobject_class, PROP_IRADIO_MODE,
253 g_param_spec_boolean ("iradio-mode", "iradio-mode",
254 "Enable internet radio mode (ask server to send shoutcast/icecast "
255 "metadata interleaved with the actual stream data)",
256 DEFAULT_IRADIO_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
259 * GstSoupHTTPSrc::http-log-level:
261 * If set and > 0, captures and dumps HTTP session data as
262 * log messages if log level >= GST_LEVEL_TRACE
266 g_object_class_install_property (gobject_class, PROP_SOUP_LOG_LEVEL,
267 g_param_spec_enum ("http-log-level", "HTTP log level",
268 "Set log level for soup's HTTP session log",
269 SOUP_TYPE_LOGGER_LOG_LEVEL, DEFAULT_SOUP_LOG_LEVEL,
270 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
273 * GstSoupHTTPSrc::compress:
275 * If set to %TRUE, souphttpsrc will automatically handle gzip
276 * and deflate Content-Encodings. This does not make much difference
277 * and causes more load for normal media files, but makes a real
278 * difference in size for plaintext files.
282 g_object_class_install_property (gobject_class, PROP_COMPRESS,
283 g_param_spec_boolean ("compress", "Compress",
284 "Allow compressed content encodings",
285 DEFAULT_COMPRESS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
288 * GstSoupHTTPSrc::keep-alive:
290 * If set to %TRUE, souphttpsrc will keep alive connections when being
291 * set to READY state and only will close connections when connecting
292 * to a different server or when going to NULL state..
296 g_object_class_install_property (gobject_class, PROP_KEEP_ALIVE,
297 g_param_spec_boolean ("keep-alive", "keep-alive",
298 "Use HTTP persistent connections", DEFAULT_KEEP_ALIVE,
299 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
301 gst_element_class_add_pad_template (gstelement_class,
302 gst_static_pad_template_get (&srctemplate));
304 gst_element_class_set_static_metadata (gstelement_class, "HTTP client source",
306 "Receive data as a client over the network via HTTP using SOUP",
307 "Wouter Cloetens <wouter@mind.be>");
308 gstelement_class->change_state =
309 GST_DEBUG_FUNCPTR (gst_soup_http_src_change_state);
311 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_soup_http_src_start);
312 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_soup_http_src_stop);
313 gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_soup_http_src_unlock);
314 gstbasesrc_class->unlock_stop =
315 GST_DEBUG_FUNCPTR (gst_soup_http_src_unlock_stop);
316 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_soup_http_src_get_size);
317 gstbasesrc_class->is_seekable =
318 GST_DEBUG_FUNCPTR (gst_soup_http_src_is_seekable);
319 gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_soup_http_src_do_seek);
320 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_soup_http_src_query);
322 gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_soup_http_src_create);
324 GST_DEBUG_CATEGORY_INIT (souphttpsrc_debug, "souphttpsrc", 0,
329 gst_soup_http_src_reset (GstSoupHTTPSrc * src)
331 src->interrupted = FALSE;
333 src->have_size = FALSE;
334 src->got_headers = FALSE;
335 src->seekable = FALSE;
336 src->read_position = 0;
337 src->request_position = 0;
338 src->stop_position = -1;
339 src->content_size = 0;
340 src->have_body = FALSE;
342 gst_caps_replace (&src->src_caps, NULL);
343 g_free (src->iradio_name);
344 src->iradio_name = NULL;
345 g_free (src->iradio_genre);
346 src->iradio_genre = NULL;
347 g_free (src->iradio_url);
348 src->iradio_url = NULL;
352 gst_soup_http_src_init (GstSoupHTTPSrc * src)
356 g_mutex_init (&src->mutex);
357 g_cond_init (&src->request_finished_cond);
358 src->location = NULL;
359 src->redirection_uri = NULL;
360 src->automatic_redirect = TRUE;
361 src->user_agent = g_strdup (DEFAULT_USER_AGENT);
364 src->proxy_id = NULL;
365 src->proxy_pw = NULL;
367 src->iradio_mode = DEFAULT_IRADIO_MODE;
372 src->log_level = DEFAULT_SOUP_LOG_LEVEL;
373 proxy = g_getenv ("http_proxy");
374 if (proxy && !gst_soup_http_src_set_proxy (src, proxy)) {
375 GST_WARNING_OBJECT (src,
376 "The proxy in the http_proxy env var (\"%s\") cannot be parsed.",
380 gst_soup_http_src_reset (src);
384 gst_soup_http_src_dispose (GObject * gobject)
386 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (gobject);
388 GST_DEBUG_OBJECT (src, "dispose");
390 gst_soup_http_src_session_close (src);
392 G_OBJECT_CLASS (parent_class)->dispose (gobject);
396 gst_soup_http_src_finalize (GObject * gobject)
398 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (gobject);
400 GST_DEBUG_OBJECT (src, "finalize");
402 g_mutex_clear (&src->mutex);
403 g_cond_clear (&src->request_finished_cond);
404 g_free (src->location);
405 if (src->redirection_uri) {
406 g_free (src->redirection_uri);
408 g_free (src->user_agent);
409 if (src->proxy != NULL) {
410 soup_uri_free (src->proxy);
412 g_free (src->user_id);
413 g_free (src->user_pw);
414 g_free (src->proxy_id);
415 g_free (src->proxy_pw);
416 g_strfreev (src->cookies);
418 G_OBJECT_CLASS (parent_class)->finalize (gobject);
422 gst_soup_http_src_set_property (GObject * object, guint prop_id,
423 const GValue * value, GParamSpec * pspec)
425 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (object);
430 const gchar *location;
432 location = g_value_get_string (value);
434 if (location == NULL) {
435 GST_WARNING ("location property cannot be NULL");
438 if (!gst_soup_http_src_set_location (src, location, NULL)) {
439 GST_WARNING ("badly formatted location");
444 case PROP_USER_AGENT:
446 g_free (src->user_agent);
447 src->user_agent = g_value_dup_string (value);
449 case PROP_IRADIO_MODE:
450 src->iradio_mode = g_value_get_boolean (value);
452 case PROP_AUTOMATIC_REDIRECT:
453 src->automatic_redirect = g_value_get_boolean (value);
459 proxy = g_value_get_string (value);
462 GST_WARNING ("proxy property cannot be NULL");
465 if (!gst_soup_http_src_set_proxy (src, proxy)) {
466 GST_WARNING ("badly formatted proxy URI");
472 g_strfreev (src->cookies);
473 src->cookies = g_strdupv (g_value_get_boxed (value));
476 gst_base_src_set_live (GST_BASE_SRC (src), g_value_get_boolean (value));
480 g_free (src->user_id);
481 src->user_id = g_value_dup_string (value);
485 g_free (src->user_pw);
486 src->user_pw = g_value_dup_string (value);
490 g_free (src->proxy_id);
491 src->proxy_id = g_value_dup_string (value);
495 g_free (src->proxy_pw);
496 src->proxy_pw = g_value_dup_string (value);
499 src->timeout = g_value_get_uint (value);
501 case PROP_EXTRA_HEADERS:{
502 const GstStructure *s = gst_value_get_structure (value);
504 if (src->extra_headers)
505 gst_structure_free (src->extra_headers);
507 src->extra_headers = s ? gst_structure_copy (s) : NULL;
510 case PROP_SOUP_LOG_LEVEL:
511 src->log_level = g_value_get_enum (value);
514 src->compress = g_value_get_boolean (value);
516 case PROP_KEEP_ALIVE:
517 src->keep_alive = g_value_get_boolean (value);
520 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
528 gst_soup_http_src_get_property (GObject * object, guint prop_id,
529 GValue * value, GParamSpec * pspec)
531 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (object);
535 g_value_set_string (value, src->location);
537 case PROP_USER_AGENT:
538 g_value_set_string (value, src->user_agent);
540 case PROP_AUTOMATIC_REDIRECT:
541 g_value_set_boolean (value, src->automatic_redirect);
544 if (src->proxy == NULL)
545 g_value_set_static_string (value, "");
547 char *proxy = soup_uri_to_string (src->proxy, FALSE);
549 g_value_set_string (value, proxy);
554 g_value_set_boxed (value, g_strdupv (src->cookies));
557 g_value_set_boolean (value, gst_base_src_is_live (GST_BASE_SRC (src)));
559 case PROP_IRADIO_MODE:
560 g_value_set_boolean (value, src->iradio_mode);
563 g_value_set_string (value, src->user_id);
566 g_value_set_string (value, src->user_pw);
569 g_value_set_string (value, src->proxy_id);
572 g_value_set_string (value, src->proxy_pw);
575 g_value_set_uint (value, src->timeout);
577 case PROP_EXTRA_HEADERS:
578 gst_value_set_structure (value, src->extra_headers);
580 case PROP_SOUP_LOG_LEVEL:
581 g_value_set_enum (value, src->log_level);
584 g_value_set_boolean (value, src->compress);
586 case PROP_KEEP_ALIVE:
587 g_value_set_boolean (value, src->keep_alive);
590 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
596 gst_soup_http_src_unicodify (const gchar * str)
598 const gchar *env_vars[] = { "GST_ICY_TAG_ENCODING",
599 "GST_TAG_ENCODING", NULL
602 return gst_tag_freeform_string_to_utf8 (str, -1, env_vars);
606 gst_soup_http_src_cancel_message (GstSoupHTTPSrc * src)
608 if (src->msg != NULL) {
609 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_CANCELLED;
610 soup_session_cancel_message (src->session, src->msg, SOUP_STATUS_CANCELLED);
612 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE;
617 gst_soup_http_src_queue_message (GstSoupHTTPSrc * src)
619 soup_session_queue_message (src->session, src->msg,
620 (SoupSessionCallback) gst_soup_http_src_response_cb, src);
621 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_QUEUED;
625 gst_soup_http_src_add_range_header (GstSoupHTTPSrc * src, guint64 offset,
632 soup_message_headers_remove (src->msg->request_headers, "Range");
633 if (offset || stop_offset != -1) {
634 if (stop_offset != -1) {
635 rc = g_snprintf (buf, sizeof (buf), "bytes=%" G_GUINT64_FORMAT "-%"
636 G_GUINT64_FORMAT, offset, stop_offset);
638 rc = g_snprintf (buf, sizeof (buf), "bytes=%" G_GUINT64_FORMAT "-",
641 if (rc > sizeof (buf) || rc < 0)
643 soup_message_headers_append (src->msg->request_headers, "Range", buf);
645 src->read_position = offset;
650 _append_extra_header (GQuark field_id, const GValue * value, gpointer user_data)
652 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (user_data);
653 const gchar *field_name = g_quark_to_string (field_id);
654 gchar *field_content = NULL;
656 if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
657 field_content = g_value_dup_string (value);
659 GValue dest = { 0, };
661 g_value_init (&dest, G_TYPE_STRING);
662 if (g_value_transform (value, &dest)) {
663 field_content = g_value_dup_string (&dest);
667 if (field_content == NULL) {
668 GST_ERROR_OBJECT (src, "extra-headers field '%s' contains no value "
669 "or can't be converted to a string", field_name);
673 GST_DEBUG_OBJECT (src, "Appending extra header: \"%s: %s\"", field_name,
675 soup_message_headers_append (src->msg->request_headers, field_name,
678 g_free (field_content);
684 _append_extra_headers (GQuark field_id, const GValue * value,
687 if (G_VALUE_TYPE (value) == GST_TYPE_ARRAY) {
688 guint n = gst_value_array_get_size (value);
691 for (i = 0; i < n; i++) {
692 const GValue *v = gst_value_array_get_value (value, i);
694 if (!_append_extra_header (field_id, v, user_data))
697 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
698 guint n = gst_value_list_get_size (value);
701 for (i = 0; i < n; i++) {
702 const GValue *v = gst_value_list_get_value (value, i);
704 if (!_append_extra_header (field_id, v, user_data))
708 return _append_extra_header (field_id, value, user_data);
716 gst_soup_http_src_add_extra_headers (GstSoupHTTPSrc * src)
718 if (!src->extra_headers)
721 return gst_structure_foreach (src->extra_headers, _append_extra_headers, src);
726 gst_soup_http_src_session_unpause_message (GstSoupHTTPSrc * src)
728 soup_session_unpause_message (src->session, src->msg);
732 gst_soup_http_src_session_pause_message (GstSoupHTTPSrc * src)
734 soup_session_pause_message (src->session, src->msg);
738 gst_soup_http_src_session_open (GstSoupHTTPSrc * src)
741 GST_DEBUG_OBJECT (src, "Session is already open");
745 if (!src->location) {
746 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (_("No URL set.")),
747 ("Missing location property"));
752 src->context = g_main_context_new ();
755 src->loop = g_main_loop_new (src->context, TRUE);
757 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
758 (NULL), ("Failed to start GMainLoop"));
759 g_main_context_unref (src->context);
764 GST_DEBUG_OBJECT (src, "Creating session");
765 if (src->proxy == NULL) {
767 soup_session_async_new_with_options (SOUP_SESSION_ASYNC_CONTEXT,
768 src->context, SOUP_SESSION_USER_AGENT, src->user_agent,
769 SOUP_SESSION_TIMEOUT, src->timeout,
770 SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_PROXY_RESOLVER_DEFAULT,
774 soup_session_async_new_with_options (SOUP_SESSION_ASYNC_CONTEXT,
775 src->context, SOUP_SESSION_PROXY_URI, src->proxy,
776 SOUP_SESSION_TIMEOUT, src->timeout,
777 SOUP_SESSION_USER_AGENT, src->user_agent, NULL);
781 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
782 (NULL), ("Failed to create async session"));
786 g_signal_connect (src->session, "authenticate",
787 G_CALLBACK (gst_soup_http_src_authenticate_cb), src);
790 gst_soup_util_log_setup (src->session, src->log_level, GST_ELEMENT (src));
792 GST_DEBUG_OBJECT (src, "Re-using session");
796 soup_session_add_feature_by_type (src->session, SOUP_TYPE_CONTENT_DECODER);
798 soup_session_remove_feature_by_type (src->session,
799 SOUP_TYPE_CONTENT_DECODER);
805 gst_soup_http_src_session_close (GstSoupHTTPSrc * src)
808 soup_session_abort (src->session); /* This unrefs the message. */
809 g_object_unref (src->session);
814 g_main_loop_unref (src->loop);
815 g_main_context_unref (src->context);
822 gst_soup_http_src_authenticate_cb (SoupSession * session, SoupMessage * msg,
823 SoupAuth * auth, gboolean retrying, GstSoupHTTPSrc * src)
826 /* First time authentication only, if we fail and are called again with retry true fall through */
827 if (msg->status_code == SOUP_STATUS_UNAUTHORIZED) {
828 if (src->user_id && src->user_pw)
829 soup_auth_authenticate (auth, src->user_id, src->user_pw);
830 } else if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
831 if (src->proxy_id && src->proxy_pw)
832 soup_auth_authenticate (auth, src->proxy_id, src->proxy_pw);
838 gst_soup_http_src_got_headers_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
841 GstTagList *tag_list;
844 GHashTable *params = NULL;
846 GST_INFO_OBJECT (src, "got headers");
848 if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED &&
849 src->proxy_id && src->proxy_pw)
852 if (src->automatic_redirect && SOUP_STATUS_IS_REDIRECTION (msg->status_code)) {
853 src->redirection_uri = g_strdup (soup_message_headers_get_one
854 (msg->response_headers, "Location"));
855 GST_DEBUG_OBJECT (src, "%u redirect to \"%s\"", msg->status_code,
856 src->redirection_uri);
860 if (msg->status_code == SOUP_STATUS_UNAUTHORIZED)
863 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING;
864 src->got_headers = TRUE;
866 /* Parse Content-Length. */
867 if (soup_message_headers_get_encoding (msg->response_headers) ==
868 SOUP_ENCODING_CONTENT_LENGTH) {
869 newsize = src->request_position +
870 soup_message_headers_get_content_length (msg->response_headers);
871 if (!src->have_size || (src->content_size != newsize)) {
872 src->content_size = newsize;
873 src->have_size = TRUE;
874 src->seekable = TRUE;
875 GST_DEBUG_OBJECT (src, "size = %" G_GUINT64_FORMAT, src->content_size);
877 basesrc = GST_BASE_SRC_CAST (src);
878 basesrc->segment.duration = src->content_size;
879 gst_element_post_message (GST_ELEMENT (src),
880 gst_message_new_duration_changed (GST_OBJECT (src)));
885 tag_list = gst_tag_list_new_empty ();
888 soup_message_headers_get_one (msg->response_headers,
889 "icy-metaint")) != NULL) {
890 gint icy_metaint = atoi (value);
892 GST_DEBUG_OBJECT (src, "icy-metaint: %s (parsed: %d)", value, icy_metaint);
893 if (icy_metaint > 0) {
895 gst_caps_unref (src->src_caps);
897 src->src_caps = gst_caps_new_simple ("application/x-icy",
898 "metadata-interval", G_TYPE_INT, icy_metaint, NULL);
900 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
904 soup_message_headers_get_content_type (msg->response_headers,
906 GST_DEBUG_OBJECT (src, "Content-Type: %s", value);
907 if (g_ascii_strcasecmp (value, "audio/L16") == 0) {
913 gst_caps_unref (src->src_caps);
915 param = g_hash_table_lookup (params, "channels");
917 channels = atol (param);
919 param = g_hash_table_lookup (params, "rate");
923 src->src_caps = gst_caps_new_simple ("audio/x-raw",
924 "format", G_TYPE_STRING, "S16BE",
925 "layout", G_TYPE_STRING, "interleaved",
926 "channels", G_TYPE_INT, channels, "rate", G_TYPE_INT, rate, NULL);
928 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
930 /* Set the Content-Type field on the caps */
932 src->src_caps = gst_caps_make_writable (src->src_caps);
933 gst_caps_set_simple (src->src_caps, "content-type", G_TYPE_STRING,
935 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
941 g_hash_table_destroy (params);
944 soup_message_headers_get_one (msg->response_headers,
945 "icy-name")) != NULL) {
946 g_free (src->iradio_name);
947 src->iradio_name = gst_soup_http_src_unicodify (value);
948 if (src->iradio_name) {
949 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_ORGANIZATION,
950 src->iradio_name, NULL);
954 soup_message_headers_get_one (msg->response_headers,
955 "icy-genre")) != NULL) {
956 g_free (src->iradio_genre);
957 src->iradio_genre = gst_soup_http_src_unicodify (value);
958 if (src->iradio_genre) {
959 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_GENRE,
960 src->iradio_genre, NULL);
963 if ((value = soup_message_headers_get_one (msg->response_headers, "icy-url"))
965 g_free (src->iradio_url);
966 src->iradio_url = gst_soup_http_src_unicodify (value);
967 if (src->iradio_url) {
968 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_LOCATION,
969 src->iradio_url, NULL);
972 if (!gst_tag_list_is_empty (tag_list)) {
973 GST_DEBUG_OBJECT (src,
974 "calling gst_element_found_tags with %" GST_PTR_FORMAT, tag_list);
975 gst_pad_push_event (GST_BASE_SRC_PAD (src), gst_event_new_tag (tag_list));
977 gst_tag_list_unref (tag_list);
980 /* Handle HTTP errors. */
981 gst_soup_http_src_parse_status (msg, src);
983 /* Check if Range header was respected. */
984 if (src->ret == GST_FLOW_CUSTOM_ERROR &&
985 src->read_position && msg->status_code != SOUP_STATUS_PARTIAL_CONTENT) {
986 src->seekable = FALSE;
987 GST_ELEMENT_ERROR (src, RESOURCE, SEEK,
988 (_("Server does not support seeking.")),
989 ("Server does not accept Range HTTP header, URL: %s", src->location));
990 src->ret = GST_FLOW_ERROR;
993 /* If we are going to error out, stop all processing right here, so we
994 * don't output any data (such as an error html page), and return
995 * GST_FLOW_ERROR from the create function instead of having
996 * got_chunk_cb overwrite src->ret with FLOW_OK again. */
997 if (src->ret == GST_FLOW_ERROR || src->ret == GST_FLOW_EOS) {
998 gst_soup_http_src_session_pause_message (src);
1001 g_main_loop_quit (src->loop);
1003 g_cond_signal (&src->request_finished_cond);
1006 /* Have body. Signal EOS. */
1008 gst_soup_http_src_got_body_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
1010 if (G_UNLIKELY (msg != src->msg)) {
1011 GST_DEBUG_OBJECT (src, "got body, but not for current message");
1014 if (G_UNLIKELY (src->session_io_status !=
1015 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
1016 /* Probably a redirect. */
1019 GST_DEBUG_OBJECT (src, "got body");
1020 src->ret = GST_FLOW_EOS;
1021 src->have_body = TRUE;
1023 /* no need to interrupt the message here, we do it on the
1024 * finished_cb anyway if needed. And getting the body might mean
1025 * that the connection was hang up before finished. This happens when
1026 * the pipeline is stalled for too long (long pauses during playback).
1027 * Best to let it continue from here and pause because it reached the
1028 * final bytes based on content_size or received an out of range error */
1031 /* Finished. Signal EOS. */
1033 gst_soup_http_src_finished_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
1035 if (G_UNLIKELY (msg != src->msg)) {
1036 GST_DEBUG_OBJECT (src, "finished, but not for current message");
1039 GST_DEBUG_OBJECT (src, "finished");
1040 src->ret = GST_FLOW_EOS;
1041 if (src->session_io_status == GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_CANCELLED) {
1042 /* gst_soup_http_src_cancel_message() triggered this; probably a seek
1043 * that occurred in the QUEUEING state; i.e. before the connection setup
1044 * was complete. Do nothing */
1045 } else if (src->session_io_status ==
1046 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING && src->read_position > 0 &&
1047 (src->have_size && src->read_position < src->content_size)) {
1048 /* The server disconnected while streaming. Reconnect and seeking to the
1051 src->ret = GST_FLOW_CUSTOM_ERROR;
1052 } else if (G_UNLIKELY (src->session_io_status !=
1053 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
1054 if (msg->method == SOUP_METHOD_HEAD) {
1055 GST_DEBUG_OBJECT (src, "Ignoring error %d:%s during HEAD request",
1056 msg->status_code, msg->reason_phrase);
1058 gst_soup_http_src_parse_status (msg, src);
1062 g_main_loop_quit (src->loop);
1063 g_cond_signal (&src->request_finished_cond);
1066 /* Buffer lifecycle management.
1068 * gst_soup_http_src_create() runs the GMainLoop for this element, to let
1069 * Soup take control.
1070 * A GstBuffer is allocated in gst_soup_http_src_chunk_allocator() and
1071 * associated with a SoupBuffer.
1072 * Soup reads HTTP data in the GstBuffer's data buffer.
1073 * The gst_soup_http_src_got_chunk_cb() is then called with the SoupBuffer.
1074 * That sets gst_soup_http_src_create()'s return argument to the GstBuffer,
1075 * increments its refcount (to 2), pauses the flow of data from the HTTP
1076 * source to prevent gst_soup_http_src_got_chunk_cb() from being called
1077 * again and breaks out of the GMainLoop.
1078 * Because the SOUP_MESSAGE_OVERWRITE_CHUNKS flag is set, Soup frees the
1079 * SoupBuffer and calls gst_soup_http_src_chunk_free(), which decrements the
1081 * gst_soup_http_src_create() returns the GstBuffer. It will be freed by a
1082 * downstream element.
1083 * If Soup fails to read HTTP data, it does not call
1084 * gst_soup_http_src_got_chunk_cb(), but still frees the SoupBuffer and
1085 * calls gst_soup_http_src_chunk_free(), which decrements the GstBuffer's
1086 * refcount to 0, freeing it.
1096 gst_soup_http_src_chunk_free (gpointer user_data)
1098 SoupGstChunk *chunk = (SoupGstChunk *) user_data;
1100 gst_buffer_unmap (chunk->buffer, &chunk->map);
1101 gst_buffer_unref (chunk->buffer);
1102 g_slice_free (SoupGstChunk, chunk);
1106 gst_soup_http_src_chunk_allocator (SoupMessage * msg, gsize max_len,
1109 GstSoupHTTPSrc *src = (GstSoupHTTPSrc *) user_data;
1110 GstBaseSrc *basesrc = GST_BASE_SRC_CAST (src);
1112 SoupBuffer *soupbuf;
1115 SoupGstChunk *chunk;
1118 length = MIN (basesrc->blocksize, max_len);
1120 length = basesrc->blocksize;
1121 GST_DEBUG_OBJECT (src, "alloc %" G_GSIZE_FORMAT " bytes <= %" G_GSIZE_FORMAT,
1124 rc = GST_BASE_SRC_CLASS (parent_class)->alloc (basesrc, -1, length, &gstbuf);
1125 if (G_UNLIKELY (rc != GST_FLOW_OK)) {
1126 /* Failed to allocate buffer. Stall SoupSession and return error code
1129 g_main_loop_quit (src->loop);
1133 chunk = g_slice_new0 (SoupGstChunk);
1134 chunk->buffer = gstbuf;
1135 gst_buffer_map (gstbuf, &chunk->map, GST_MAP_READWRITE);
1137 soupbuf = soup_buffer_new_with_owner (chunk->map.data, chunk->map.size,
1138 chunk, gst_soup_http_src_chunk_free);
1144 gst_soup_http_src_got_chunk_cb (SoupMessage * msg, SoupBuffer * chunk,
1145 GstSoupHTTPSrc * src)
1147 GstBaseSrc *basesrc;
1148 guint64 new_position;
1149 SoupGstChunk *gchunk;
1151 if (G_UNLIKELY (msg != src->msg)) {
1152 GST_DEBUG_OBJECT (src, "got chunk, but not for current message");
1155 src->have_body = FALSE;
1156 if (G_UNLIKELY (src->session_io_status !=
1157 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
1158 /* Probably a redirect. */
1161 basesrc = GST_BASE_SRC_CAST (src);
1162 GST_DEBUG_OBJECT (src, "got chunk of %" G_GSIZE_FORMAT " bytes",
1165 /* Extract the GstBuffer from the SoupBuffer and set its fields. */
1166 gchunk = (SoupGstChunk *) soup_buffer_get_owner (chunk);
1167 *src->outbuf = gchunk->buffer;
1169 gst_buffer_resize (*src->outbuf, 0, chunk->length);
1170 GST_BUFFER_OFFSET (*src->outbuf) = basesrc->segment.position;
1172 gst_buffer_ref (*src->outbuf);
1174 new_position = src->read_position + chunk->length;
1175 if (G_LIKELY (src->request_position == src->read_position))
1176 src->request_position = new_position;
1177 src->read_position = new_position;
1179 if (src->content_size != 0 && new_position > src->content_size) {
1180 GST_DEBUG_OBJECT (src, "Got position previous estimated content size "
1181 "(%" G_GINT64_FORMAT " > %" G_GINT64_FORMAT ")", new_position,
1183 src->content_size = new_position;
1184 basesrc->segment.duration = src->content_size;
1185 gst_element_post_message (GST_ELEMENT (src),
1186 gst_message_new_duration_changed (GST_OBJECT (src)));
1189 src->ret = GST_FLOW_OK;
1190 g_main_loop_quit (src->loop);
1191 gst_soup_http_src_session_pause_message (src);
1195 gst_soup_http_src_response_cb (SoupSession * session, SoupMessage * msg,
1196 GstSoupHTTPSrc * src)
1198 if (G_UNLIKELY (msg != src->msg)) {
1199 GST_DEBUG_OBJECT (src, "got response %d: %s, but not for current message",
1200 msg->status_code, msg->reason_phrase);
1203 if (G_UNLIKELY (src->session_io_status !=
1204 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)
1205 && SOUP_STATUS_IS_REDIRECTION (msg->status_code)) {
1206 /* Ignore redirections. */
1209 GST_DEBUG_OBJECT (src, "got response %d: %s", msg->status_code,
1210 msg->reason_phrase);
1211 if (src->session_io_status == GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING &&
1212 src->read_position > 0 && (src->have_size
1213 && src->read_position < src->content_size)) {
1214 /* The server disconnected while streaming. Reconnect and seeking to the
1218 gst_soup_http_src_parse_status (msg, src);
1219 /* The session's SoupMessage object expires after this callback returns. */
1221 g_main_loop_quit (src->loop);
1224 #define SOUP_HTTP_SRC_ERROR(src,soup_msg,cat,code,error_message) \
1225 GST_ELEMENT_ERROR ((src), cat, code, ("%s", error_message), \
1226 ("%s (%d), URL: %s", (soup_msg)->reason_phrase, \
1227 (soup_msg)->status_code, (src)->location));
1230 gst_soup_http_src_parse_status (SoupMessage * msg, GstSoupHTTPSrc * src)
1232 if (msg->method == SOUP_METHOD_HEAD) {
1233 if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code))
1234 GST_DEBUG_OBJECT (src, "Ignoring error %d during HEAD request",
1236 } else if (SOUP_STATUS_IS_TRANSPORT_ERROR (msg->status_code)) {
1237 switch (msg->status_code) {
1238 case SOUP_STATUS_CANT_RESOLVE:
1239 case SOUP_STATUS_CANT_RESOLVE_PROXY:
1240 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, NOT_FOUND,
1241 _("Could not resolve server name."));
1242 src->ret = GST_FLOW_ERROR;
1244 case SOUP_STATUS_CANT_CONNECT:
1245 case SOUP_STATUS_CANT_CONNECT_PROXY:
1246 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ,
1247 _("Could not establish connection to server."));
1248 src->ret = GST_FLOW_ERROR;
1250 case SOUP_STATUS_SSL_FAILED:
1251 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ,
1252 _("Secure connection setup failed."));
1253 src->ret = GST_FLOW_ERROR;
1255 case SOUP_STATUS_IO_ERROR:
1256 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, READ,
1257 _("A network error occured, or the server closed the connection "
1259 src->ret = GST_FLOW_ERROR;
1261 case SOUP_STATUS_MALFORMED:
1262 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, READ,
1263 _("Server sent bad data."));
1264 src->ret = GST_FLOW_ERROR;
1266 case SOUP_STATUS_CANCELLED:
1267 /* No error message when interrupted by program. */
1270 } else if (SOUP_STATUS_IS_CLIENT_ERROR (msg->status_code) ||
1271 SOUP_STATUS_IS_REDIRECTION (msg->status_code) ||
1272 SOUP_STATUS_IS_SERVER_ERROR (msg->status_code)) {
1273 /* Report HTTP error. */
1275 /* when content_size is unknown and we have just finished receiving
1276 * a body message, requests that go beyond the content limits will result
1277 * in an error. Here we convert those to EOS */
1278 if (msg->status_code == SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE &&
1279 src->have_body && src->have_size) {
1280 GST_DEBUG_OBJECT (src, "Requested range out of limits and received full "
1281 "body, returning EOS");
1282 src->ret = GST_FLOW_EOS;
1286 /* FIXME: reason_phrase is not translated and not suitable for user
1287 * error dialog according to libsoup documentation.
1289 if (msg->status_code == SOUP_STATUS_NOT_FOUND) {
1290 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
1291 ("%s", msg->reason_phrase),
1292 ("%s (%d), URL: %s", msg->reason_phrase, msg->status_code,
1294 } else if (msg->status_code == SOUP_STATUS_UNAUTHORIZED ||
1295 msg->status_code == SOUP_STATUS_PAYMENT_REQUIRED ||
1296 msg->status_code == SOUP_STATUS_FORBIDDEN ||
1297 msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
1298 GST_ELEMENT_ERROR (src, RESOURCE, NOT_AUTHORIZED,
1299 ("%s", msg->reason_phrase),
1300 ("%s (%d), URL: %s", msg->reason_phrase, msg->status_code,
1303 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1304 ("%s", msg->reason_phrase),
1305 ("%s (%d), URL: %s", msg->reason_phrase, msg->status_code,
1308 src->ret = GST_FLOW_ERROR;
1313 gst_soup_http_src_build_message (GstSoupHTTPSrc * src, const gchar * method)
1315 src->msg = soup_message_new (method, src->location);
1317 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1318 ("Error parsing URL."), ("URL: %s", src->location));
1321 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE;
1322 if (!src->keep_alive) {
1323 soup_message_headers_append (src->msg->request_headers, "Connection",
1326 if (src->iradio_mode) {
1327 soup_message_headers_append (src->msg->request_headers, "icy-metadata",
1333 for (cookie = src->cookies; *cookie != NULL; cookie++) {
1334 soup_message_headers_append (src->msg->request_headers, "Cookie",
1340 g_signal_connect (src->msg, "got_headers",
1341 G_CALLBACK (gst_soup_http_src_got_headers_cb), src);
1342 g_signal_connect (src->msg, "got_body",
1343 G_CALLBACK (gst_soup_http_src_got_body_cb), src);
1344 g_signal_connect (src->msg, "finished",
1345 G_CALLBACK (gst_soup_http_src_finished_cb), src);
1346 g_signal_connect (src->msg, "got_chunk",
1347 G_CALLBACK (gst_soup_http_src_got_chunk_cb), src);
1348 soup_message_set_flags (src->msg, SOUP_MESSAGE_OVERWRITE_CHUNKS |
1349 (src->automatic_redirect ? 0 : SOUP_MESSAGE_NO_REDIRECT));
1350 soup_message_set_chunk_allocator (src->msg,
1351 gst_soup_http_src_chunk_allocator, src, NULL);
1352 gst_soup_http_src_add_range_header (src, src->request_position,
1353 src->stop_position);
1355 gst_soup_http_src_add_extra_headers (src);
1360 static GstFlowReturn
1361 gst_soup_http_src_do_request (GstSoupHTTPSrc * src, const gchar * method,
1362 GstBuffer ** outbuf)
1364 GST_LOG_OBJECT (src, "Running request for method: %s", method);
1365 if (src->msg && (src->request_position != src->read_position)) {
1366 if (src->session_io_status == GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE) {
1367 gst_soup_http_src_add_range_header (src, src->request_position,
1368 src->stop_position);
1370 GST_DEBUG_OBJECT (src, "Seek from position %" G_GUINT64_FORMAT
1371 " to %" G_GUINT64_FORMAT ": requeueing connection request",
1372 src->read_position, src->request_position);
1373 gst_soup_http_src_cancel_message (src);
1377 if (!gst_soup_http_src_build_message (src, method)) {
1378 return GST_FLOW_ERROR;
1381 src->ret = GST_FLOW_CUSTOM_ERROR;
1382 src->outbuf = outbuf;
1384 if (src->interrupted) {
1385 GST_DEBUG_OBJECT (src, "interrupted");
1389 GST_DEBUG_OBJECT (src, "Reconnecting");
1390 if (!gst_soup_http_src_build_message (src, method)) {
1391 return GST_FLOW_ERROR;
1397 GST_DEBUG_OBJECT (src, "EOS reached");
1401 switch (src->session_io_status) {
1402 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE:
1403 GST_DEBUG_OBJECT (src, "Queueing connection request");
1404 gst_soup_http_src_queue_message (src);
1406 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_QUEUED:
1408 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING:
1409 gst_soup_http_src_session_unpause_message (src);
1411 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_CANCELLED:
1416 if (src->ret == GST_FLOW_CUSTOM_ERROR)
1417 g_main_loop_run (src->loop);
1418 } while (src->ret == GST_FLOW_CUSTOM_ERROR);
1420 if (src->ret == GST_FLOW_CUSTOM_ERROR)
1421 src->ret = GST_FLOW_EOS;
1422 g_cond_signal (&src->request_finished_cond);
1426 static GstFlowReturn
1427 gst_soup_http_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
1429 GstSoupHTTPSrc *src;
1432 src = GST_SOUP_HTTP_SRC (psrc);
1434 g_mutex_lock (&src->mutex);
1435 ret = gst_soup_http_src_do_request (src, SOUP_METHOD_GET, outbuf);
1436 g_mutex_unlock (&src->mutex);
1441 gst_soup_http_src_start (GstBaseSrc * bsrc)
1443 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1445 GST_DEBUG_OBJECT (src, "start(\"%s\")", src->location);
1447 return gst_soup_http_src_session_open (src);
1451 gst_soup_http_src_stop (GstBaseSrc * bsrc)
1453 GstSoupHTTPSrc *src;
1455 src = GST_SOUP_HTTP_SRC (bsrc);
1456 GST_DEBUG_OBJECT (src, "stop()");
1457 if (src->keep_alive)
1458 gst_soup_http_src_cancel_message (src);
1460 gst_soup_http_src_session_close (src);
1462 if (src->extra_headers) {
1463 gst_structure_free (src->extra_headers);
1464 src->extra_headers = NULL;
1467 gst_soup_http_src_reset (src);
1471 static GstStateChangeReturn
1472 gst_soup_http_src_change_state (GstElement * element, GstStateChange transition)
1474 GstStateChangeReturn ret;
1475 GstSoupHTTPSrc *src;
1477 src = GST_SOUP_HTTP_SRC (element);
1479 switch (transition) {
1480 case GST_STATE_CHANGE_READY_TO_NULL:
1481 gst_soup_http_src_session_close (src);
1487 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1492 /* Interrupt a blocking request. */
1494 gst_soup_http_src_unlock (GstBaseSrc * bsrc)
1496 GstSoupHTTPSrc *src;
1498 src = GST_SOUP_HTTP_SRC (bsrc);
1499 GST_DEBUG_OBJECT (src, "unlock()");
1501 src->interrupted = TRUE;
1503 g_main_loop_quit (src->loop);
1504 g_cond_signal (&src->request_finished_cond);
1508 /* Interrupt interrupt. */
1510 gst_soup_http_src_unlock_stop (GstBaseSrc * bsrc)
1512 GstSoupHTTPSrc *src;
1514 src = GST_SOUP_HTTP_SRC (bsrc);
1515 GST_DEBUG_OBJECT (src, "unlock_stop()");
1517 src->interrupted = FALSE;
1522 gst_soup_http_src_get_size (GstBaseSrc * bsrc, guint64 * size)
1524 GstSoupHTTPSrc *src;
1526 src = GST_SOUP_HTTP_SRC (bsrc);
1528 if (src->have_size) {
1529 GST_DEBUG_OBJECT (src, "get_size() = %" G_GUINT64_FORMAT,
1531 *size = src->content_size;
1534 GST_DEBUG_OBJECT (src, "get_size() = FALSE");
1539 gst_soup_http_src_check_seekable (GstSoupHTTPSrc * src)
1541 GstFlowReturn ret = GST_FLOW_OK;
1543 /* Special case to check if the server allows range requests
1544 * before really starting to get data in the buffer creation
1547 if (!src->got_headers && GST_STATE (src) >= GST_STATE_PAUSED) {
1548 g_mutex_lock (&src->mutex);
1549 while (!src->got_headers && !src->interrupted && ret == GST_FLOW_OK) {
1550 if ((src->msg && src->msg->method != SOUP_METHOD_HEAD) &&
1551 src->session_io_status != GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE) {
1552 /* wait for the current request to finish */
1553 g_cond_wait (&src->request_finished_cond, &src->mutex);
1555 if (gst_soup_http_src_session_open (src)) {
1556 ret = gst_soup_http_src_do_request (src, SOUP_METHOD_HEAD, NULL);
1560 if (src->ret == GST_FLOW_EOS) {
1561 /* A HEAD request shouldn't lead to EOS */
1562 src->ret = GST_FLOW_OK;
1564 /* resets status to idle */
1565 gst_soup_http_src_cancel_message (src);
1566 g_mutex_unlock (&src->mutex);
1572 gst_soup_http_src_is_seekable (GstBaseSrc * bsrc)
1574 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1576 gst_soup_http_src_check_seekable (src);
1578 return src->seekable;
1582 gst_soup_http_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
1584 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1586 GST_DEBUG_OBJECT (src, "do_seek(%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1587 ")", segment->start, segment->stop);
1588 if (src->read_position == segment->start &&
1589 src->request_position == src->read_position &&
1590 src->stop_position == segment->stop) {
1591 GST_DEBUG_OBJECT (src,
1592 "Seek to current read/end position and no seek pending");
1596 gst_soup_http_src_check_seekable (src);
1598 /* If we have no headers we don't know yet if it is seekable or not.
1599 * Store the start position and error out later if it isn't */
1600 if (src->got_headers && !src->seekable) {
1601 GST_WARNING_OBJECT (src, "Not seekable");
1605 if (segment->rate < 0.0 || segment->format != GST_FORMAT_BYTES) {
1606 GST_WARNING_OBJECT (src, "Invalid seek segment");
1610 if (src->content_size != 0 && segment->start >= src->content_size) {
1611 GST_WARNING_OBJECT (src,
1612 "Potentially seeking behind end of file, might EOS immediately");
1615 /* Wait for create() to handle the jump in offset. */
1616 src->request_position = segment->start;
1617 src->stop_position = segment->stop;
1622 gst_soup_http_src_query (GstBaseSrc * bsrc, GstQuery * query)
1624 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1626 GstSchedulingFlags flags;
1627 gint minsize, maxsize, align;
1629 switch (GST_QUERY_TYPE (query)) {
1631 gst_query_set_uri (query, src->location);
1632 if (src->redirection_uri != NULL)
1633 gst_query_set_uri_redirection (query, src->redirection_uri);
1642 ret = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
1644 switch (GST_QUERY_TYPE (query)) {
1645 case GST_QUERY_SCHEDULING:
1646 gst_query_parse_scheduling (query, &flags, &minsize, &maxsize, &align);
1647 flags |= GST_SCHEDULING_FLAG_BANDWIDTH_LIMITED;
1648 gst_query_set_scheduling (query, flags, minsize, maxsize, align);
1658 gst_soup_http_src_set_location (GstSoupHTTPSrc * src, const gchar * uri,
1661 const char *alt_schemes[] = { "icy://", "icyx://" };
1664 if (src->location) {
1665 g_free (src->location);
1666 src->location = NULL;
1672 for (i = 0; i < G_N_ELEMENTS (alt_schemes); i++) {
1673 if (g_str_has_prefix (uri, alt_schemes[i])) {
1675 g_strdup_printf ("http://%s", uri + strlen (alt_schemes[i]));
1680 if (src->redirection_uri) {
1681 g_free (src->redirection_uri);
1682 src->redirection_uri = NULL;
1685 src->location = g_strdup (uri);
1691 gst_soup_http_src_set_proxy (GstSoupHTTPSrc * src, const gchar * uri)
1694 soup_uri_free (src->proxy);
1697 if (g_str_has_prefix (uri, "http://")) {
1698 src->proxy = soup_uri_new (uri);
1700 gchar *new_uri = g_strconcat ("http://", uri, NULL);
1702 src->proxy = soup_uri_new (new_uri);
1710 gst_soup_http_src_uri_get_type (GType type)
1715 static const gchar *const *
1716 gst_soup_http_src_uri_get_protocols (GType type)
1718 static const gchar *protocols[] = { "http", "https", "icy", "icyx", NULL };
1724 gst_soup_http_src_uri_get_uri (GstURIHandler * handler)
1726 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (handler);
1728 /* FIXME: make thread-safe */
1729 return g_strdup (src->location);
1733 gst_soup_http_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
1736 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (handler);
1738 return gst_soup_http_src_set_location (src, uri, error);
1742 gst_soup_http_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1744 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1746 iface->get_type = gst_soup_http_src_uri_get_type;
1747 iface->get_protocols = gst_soup_http_src_uri_get_protocols;
1748 iface->get_uri = gst_soup_http_src_uri_get_uri;
1749 iface->set_uri = gst_soup_http_src_uri_set_uri;