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 GstFlowReturn gst_soup_http_src_create (GstPushSrc * psrc,
132 GstBuffer ** outbuf);
133 static gboolean gst_soup_http_src_start (GstBaseSrc * bsrc);
134 static gboolean gst_soup_http_src_stop (GstBaseSrc * bsrc);
135 static gboolean gst_soup_http_src_get_size (GstBaseSrc * bsrc, guint64 * size);
136 static gboolean gst_soup_http_src_is_seekable (GstBaseSrc * bsrc);
137 static gboolean gst_soup_http_src_do_seek (GstBaseSrc * bsrc,
138 GstSegment * segment);
139 static gboolean gst_soup_http_src_query (GstBaseSrc * bsrc, GstQuery * query);
140 static gboolean gst_soup_http_src_unlock (GstBaseSrc * bsrc);
141 static gboolean gst_soup_http_src_unlock_stop (GstBaseSrc * bsrc);
142 static gboolean gst_soup_http_src_set_location (GstSoupHTTPSrc * src,
143 const gchar * uri, GError ** error);
144 static gboolean gst_soup_http_src_set_proxy (GstSoupHTTPSrc * src,
146 static char *gst_soup_http_src_unicodify (const char *str);
147 static gboolean gst_soup_http_src_build_message (GstSoupHTTPSrc * src,
148 const gchar * method);
149 static void gst_soup_http_src_cancel_message (GstSoupHTTPSrc * src);
150 static void gst_soup_http_src_queue_message (GstSoupHTTPSrc * src);
151 static gboolean gst_soup_http_src_add_range_header (GstSoupHTTPSrc * src,
152 guint64 offset, guint64 stop_offset);
153 static void gst_soup_http_src_session_unpause_message (GstSoupHTTPSrc * src);
154 static void gst_soup_http_src_session_pause_message (GstSoupHTTPSrc * src);
155 static gboolean gst_soup_http_src_session_open (GstSoupHTTPSrc * src);
156 static void gst_soup_http_src_session_close (GstSoupHTTPSrc * src);
157 static void gst_soup_http_src_parse_status (SoupMessage * msg,
158 GstSoupHTTPSrc * src);
159 static void gst_soup_http_src_chunk_free (gpointer gstbuf);
160 static SoupBuffer *gst_soup_http_src_chunk_allocator (SoupMessage * msg,
161 gsize max_len, gpointer user_data);
162 static void gst_soup_http_src_got_chunk_cb (SoupMessage * msg,
163 SoupBuffer * chunk, GstSoupHTTPSrc * src);
164 static void gst_soup_http_src_response_cb (SoupSession * session,
165 SoupMessage * msg, GstSoupHTTPSrc * src);
166 static void gst_soup_http_src_got_headers_cb (SoupMessage * msg,
167 GstSoupHTTPSrc * src);
168 static void gst_soup_http_src_got_body_cb (SoupMessage * msg,
169 GstSoupHTTPSrc * src);
170 static void gst_soup_http_src_finished_cb (SoupMessage * msg,
171 GstSoupHTTPSrc * src);
172 static void gst_soup_http_src_authenticate_cb (SoupSession * session,
173 SoupMessage * msg, SoupAuth * auth, gboolean retrying,
174 GstSoupHTTPSrc * src);
176 #define gst_soup_http_src_parent_class parent_class
177 G_DEFINE_TYPE_WITH_CODE (GstSoupHTTPSrc, gst_soup_http_src, GST_TYPE_PUSH_SRC,
178 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
179 gst_soup_http_src_uri_handler_init));
182 gst_soup_http_src_class_init (GstSoupHTTPSrcClass * klass)
184 GObjectClass *gobject_class;
185 GstElementClass *gstelement_class;
186 GstBaseSrcClass *gstbasesrc_class;
187 GstPushSrcClass *gstpushsrc_class;
189 gobject_class = (GObjectClass *) klass;
190 gstelement_class = (GstElementClass *) klass;
191 gstbasesrc_class = (GstBaseSrcClass *) klass;
192 gstpushsrc_class = (GstPushSrcClass *) klass;
194 gobject_class->set_property = gst_soup_http_src_set_property;
195 gobject_class->get_property = gst_soup_http_src_get_property;
196 gobject_class->finalize = gst_soup_http_src_finalize;
197 gobject_class->dispose = gst_soup_http_src_dispose;
199 g_object_class_install_property (gobject_class,
201 g_param_spec_string ("location", "Location",
202 "Location to read from", "",
203 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
204 g_object_class_install_property (gobject_class,
206 g_param_spec_string ("user-agent", "User-Agent",
207 "Value of the User-Agent HTTP request header field",
208 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
209 g_object_class_install_property (gobject_class,
210 PROP_AUTOMATIC_REDIRECT,
211 g_param_spec_boolean ("automatic-redirect", "automatic-redirect",
212 "Automatically follow HTTP redirects (HTTP Status Code 3xx)",
213 TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
214 g_object_class_install_property (gobject_class,
216 g_param_spec_string ("proxy", "Proxy",
217 "HTTP proxy server URI", "",
218 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
219 g_object_class_install_property (gobject_class,
221 g_param_spec_string ("user-id", "user-id",
222 "HTTP location URI user id for authentication", "",
223 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
224 g_object_class_install_property (gobject_class, PROP_USER_PW,
225 g_param_spec_string ("user-pw", "user-pw",
226 "HTTP location URI user password for authentication", "",
227 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
228 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
229 g_param_spec_string ("proxy-id", "proxy-id",
230 "HTTP proxy URI user id for authentication", "",
231 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
232 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
233 g_param_spec_string ("proxy-pw", "proxy-pw",
234 "HTTP proxy URI user password for authentication", "",
235 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
236 g_object_class_install_property (gobject_class, PROP_COOKIES,
237 g_param_spec_boxed ("cookies", "Cookies", "HTTP request cookies",
238 G_TYPE_STRV, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
239 g_object_class_install_property (gobject_class, PROP_IS_LIVE,
240 g_param_spec_boolean ("is-live", "is-live", "Act like a live source",
241 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
242 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
243 g_param_spec_uint ("timeout", "timeout",
244 "Value in seconds to timeout a blocking I/O (0 = No timeout).", 0,
245 3600, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
246 g_object_class_install_property (gobject_class, PROP_EXTRA_HEADERS,
247 g_param_spec_boxed ("extra-headers", "Extra Headers",
248 "Extra headers to append to the HTTP request",
249 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
250 g_object_class_install_property (gobject_class, PROP_IRADIO_MODE,
251 g_param_spec_boolean ("iradio-mode", "iradio-mode",
252 "Enable internet radio mode (ask server to send shoutcast/icecast "
253 "metadata interleaved with the actual stream data)",
254 DEFAULT_IRADIO_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
257 * GstSoupHTTPSrc::http-log-level:
259 * If set and > 0, captures and dumps HTTP session data as
260 * log messages if log level >= GST_LEVEL_TRACE
264 g_object_class_install_property (gobject_class, PROP_SOUP_LOG_LEVEL,
265 g_param_spec_enum ("http-log-level", "HTTP log level",
266 "Set log level for soup's HTTP session log",
267 SOUP_TYPE_LOGGER_LOG_LEVEL, DEFAULT_SOUP_LOG_LEVEL,
268 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
271 * GstSoupHTTPSrc::compress:
273 * If set to %TRUE, souphttpsrc will automatically handle gzip
274 * and deflate Content-Encodings. This does not make much difference
275 * and causes more load for normal media files, but makes a real
276 * difference in size for plaintext files.
280 g_object_class_install_property (gobject_class, PROP_COMPRESS,
281 g_param_spec_boolean ("compress", "Compress",
282 "Allow compressed content encodings",
283 DEFAULT_COMPRESS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
286 * GstSoupHTTPSrc::keep-alive:
288 * If set to %TRUE, souphttpsrc will keep alive connections when being
289 * set to READY state and only will close connections when connecting
290 * to a different server or when going to NULL state..
294 g_object_class_install_property (gobject_class, PROP_KEEP_ALIVE,
295 g_param_spec_boolean ("keep-alive", "keep-alive",
296 "Use HTTP persistent connections", DEFAULT_KEEP_ALIVE,
297 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
299 gst_element_class_add_pad_template (gstelement_class,
300 gst_static_pad_template_get (&srctemplate));
302 gst_element_class_set_static_metadata (gstelement_class, "HTTP client source",
304 "Receive data as a client over the network via HTTP using SOUP",
305 "Wouter Cloetens <wouter@mind.be>");
307 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_soup_http_src_start);
308 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_soup_http_src_stop);
309 gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_soup_http_src_unlock);
310 gstbasesrc_class->unlock_stop =
311 GST_DEBUG_FUNCPTR (gst_soup_http_src_unlock_stop);
312 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_soup_http_src_get_size);
313 gstbasesrc_class->is_seekable =
314 GST_DEBUG_FUNCPTR (gst_soup_http_src_is_seekable);
315 gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_soup_http_src_do_seek);
316 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_soup_http_src_query);
318 gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_soup_http_src_create);
320 GST_DEBUG_CATEGORY_INIT (souphttpsrc_debug, "souphttpsrc", 0,
325 gst_soup_http_src_reset (GstSoupHTTPSrc * src)
327 src->interrupted = FALSE;
329 src->have_size = FALSE;
330 src->got_headers = FALSE;
331 src->seekable = FALSE;
332 src->read_position = 0;
333 src->request_position = 0;
334 src->stop_position = -1;
335 src->content_size = 0;
336 src->have_body = FALSE;
338 gst_caps_replace (&src->src_caps, NULL);
339 g_free (src->iradio_name);
340 src->iradio_name = NULL;
341 g_free (src->iradio_genre);
342 src->iradio_genre = NULL;
343 g_free (src->iradio_url);
344 src->iradio_url = NULL;
348 gst_soup_http_src_init (GstSoupHTTPSrc * src)
352 g_mutex_init (&src->mutex);
353 g_cond_init (&src->request_finished_cond);
354 src->location = NULL;
355 src->redirection_uri = NULL;
356 src->automatic_redirect = TRUE;
357 src->user_agent = g_strdup (DEFAULT_USER_AGENT);
360 src->proxy_id = NULL;
361 src->proxy_pw = NULL;
363 src->iradio_mode = DEFAULT_IRADIO_MODE;
368 src->log_level = DEFAULT_SOUP_LOG_LEVEL;
369 proxy = g_getenv ("http_proxy");
370 if (proxy && !gst_soup_http_src_set_proxy (src, proxy)) {
371 GST_WARNING_OBJECT (src,
372 "The proxy in the http_proxy env var (\"%s\") cannot be parsed.",
376 gst_soup_http_src_reset (src);
380 gst_soup_http_src_dispose (GObject * gobject)
382 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (gobject);
384 GST_DEBUG_OBJECT (src, "dispose");
386 gst_soup_http_src_session_close (src);
388 G_OBJECT_CLASS (parent_class)->dispose (gobject);
392 gst_soup_http_src_finalize (GObject * gobject)
394 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (gobject);
396 GST_DEBUG_OBJECT (src, "finalize");
398 g_mutex_clear (&src->mutex);
399 g_cond_clear (&src->request_finished_cond);
400 g_free (src->location);
401 if (src->redirection_uri) {
402 g_free (src->redirection_uri);
404 g_free (src->user_agent);
405 if (src->proxy != NULL) {
406 soup_uri_free (src->proxy);
408 g_free (src->user_id);
409 g_free (src->user_pw);
410 g_free (src->proxy_id);
411 g_free (src->proxy_pw);
412 g_strfreev (src->cookies);
414 G_OBJECT_CLASS (parent_class)->finalize (gobject);
418 gst_soup_http_src_set_property (GObject * object, guint prop_id,
419 const GValue * value, GParamSpec * pspec)
421 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (object);
426 const gchar *location;
428 location = g_value_get_string (value);
430 if (location == NULL) {
431 GST_WARNING ("location property cannot be NULL");
434 if (!gst_soup_http_src_set_location (src, location, NULL)) {
435 GST_WARNING ("badly formatted location");
440 case PROP_USER_AGENT:
442 g_free (src->user_agent);
443 src->user_agent = g_value_dup_string (value);
445 case PROP_IRADIO_MODE:
446 src->iradio_mode = g_value_get_boolean (value);
448 case PROP_AUTOMATIC_REDIRECT:
449 src->automatic_redirect = g_value_get_boolean (value);
455 proxy = g_value_get_string (value);
458 GST_WARNING ("proxy property cannot be NULL");
461 if (!gst_soup_http_src_set_proxy (src, proxy)) {
462 GST_WARNING ("badly formatted proxy URI");
468 g_strfreev (src->cookies);
469 src->cookies = g_strdupv (g_value_get_boxed (value));
472 gst_base_src_set_live (GST_BASE_SRC (src), g_value_get_boolean (value));
476 g_free (src->user_id);
477 src->user_id = g_value_dup_string (value);
481 g_free (src->user_pw);
482 src->user_pw = g_value_dup_string (value);
486 g_free (src->proxy_id);
487 src->proxy_id = g_value_dup_string (value);
491 g_free (src->proxy_pw);
492 src->proxy_pw = g_value_dup_string (value);
495 src->timeout = g_value_get_uint (value);
497 case PROP_EXTRA_HEADERS:{
498 const GstStructure *s = gst_value_get_structure (value);
500 if (src->extra_headers)
501 gst_structure_free (src->extra_headers);
503 src->extra_headers = s ? gst_structure_copy (s) : NULL;
506 case PROP_SOUP_LOG_LEVEL:
507 src->log_level = g_value_get_enum (value);
510 src->compress = g_value_get_boolean (value);
512 case PROP_KEEP_ALIVE:
513 src->keep_alive = g_value_get_boolean (value);
516 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
524 gst_soup_http_src_get_property (GObject * object, guint prop_id,
525 GValue * value, GParamSpec * pspec)
527 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (object);
531 g_value_set_string (value, src->location);
533 case PROP_USER_AGENT:
534 g_value_set_string (value, src->user_agent);
536 case PROP_AUTOMATIC_REDIRECT:
537 g_value_set_boolean (value, src->automatic_redirect);
540 if (src->proxy == NULL)
541 g_value_set_static_string (value, "");
543 char *proxy = soup_uri_to_string (src->proxy, FALSE);
545 g_value_set_string (value, proxy);
550 g_value_set_boxed (value, g_strdupv (src->cookies));
553 g_value_set_boolean (value, gst_base_src_is_live (GST_BASE_SRC (src)));
555 case PROP_IRADIO_MODE:
556 g_value_set_boolean (value, src->iradio_mode);
559 g_value_set_string (value, src->user_id);
562 g_value_set_string (value, src->user_pw);
565 g_value_set_string (value, src->proxy_id);
568 g_value_set_string (value, src->proxy_pw);
571 g_value_set_uint (value, src->timeout);
573 case PROP_EXTRA_HEADERS:
574 gst_value_set_structure (value, src->extra_headers);
576 case PROP_SOUP_LOG_LEVEL:
577 g_value_set_enum (value, src->log_level);
580 g_value_set_boolean (value, src->compress);
582 case PROP_KEEP_ALIVE:
583 g_value_set_boolean (value, src->keep_alive);
586 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
592 gst_soup_http_src_unicodify (const gchar * str)
594 const gchar *env_vars[] = { "GST_ICY_TAG_ENCODING",
595 "GST_TAG_ENCODING", NULL
598 return gst_tag_freeform_string_to_utf8 (str, -1, env_vars);
602 gst_soup_http_src_cancel_message (GstSoupHTTPSrc * src)
604 if (src->msg != NULL) {
605 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_CANCELLED;
606 soup_session_cancel_message (src->session, src->msg, SOUP_STATUS_CANCELLED);
608 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE;
613 gst_soup_http_src_queue_message (GstSoupHTTPSrc * src)
615 soup_session_queue_message (src->session, src->msg,
616 (SoupSessionCallback) gst_soup_http_src_response_cb, src);
617 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_QUEUED;
621 gst_soup_http_src_add_range_header (GstSoupHTTPSrc * src, guint64 offset,
628 soup_message_headers_remove (src->msg->request_headers, "Range");
629 if (offset || stop_offset != -1) {
630 if (stop_offset != -1) {
631 rc = g_snprintf (buf, sizeof (buf), "bytes=%" G_GUINT64_FORMAT "-%"
632 G_GUINT64_FORMAT, offset, stop_offset);
634 rc = g_snprintf (buf, sizeof (buf), "bytes=%" G_GUINT64_FORMAT "-",
637 if (rc > sizeof (buf) || rc < 0)
639 soup_message_headers_append (src->msg->request_headers, "Range", buf);
641 src->read_position = offset;
646 _append_extra_header (GQuark field_id, const GValue * value, gpointer user_data)
648 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (user_data);
649 const gchar *field_name = g_quark_to_string (field_id);
650 gchar *field_content = NULL;
652 if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
653 field_content = g_value_dup_string (value);
655 GValue dest = { 0, };
657 g_value_init (&dest, G_TYPE_STRING);
658 if (g_value_transform (value, &dest)) {
659 field_content = g_value_dup_string (&dest);
663 if (field_content == NULL) {
664 GST_ERROR_OBJECT (src, "extra-headers field '%s' contains no value "
665 "or can't be converted to a string", field_name);
669 GST_DEBUG_OBJECT (src, "Appending extra header: \"%s: %s\"", field_name,
671 soup_message_headers_append (src->msg->request_headers, field_name,
674 g_free (field_content);
680 _append_extra_headers (GQuark field_id, const GValue * value,
683 if (G_VALUE_TYPE (value) == GST_TYPE_ARRAY) {
684 guint n = gst_value_array_get_size (value);
687 for (i = 0; i < n; i++) {
688 const GValue *v = gst_value_array_get_value (value, i);
690 if (!_append_extra_header (field_id, v, user_data))
693 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
694 guint n = gst_value_list_get_size (value);
697 for (i = 0; i < n; i++) {
698 const GValue *v = gst_value_list_get_value (value, i);
700 if (!_append_extra_header (field_id, v, user_data))
704 return _append_extra_header (field_id, value, user_data);
712 gst_soup_http_src_add_extra_headers (GstSoupHTTPSrc * src)
714 if (!src->extra_headers)
717 return gst_structure_foreach (src->extra_headers, _append_extra_headers, src);
722 gst_soup_http_src_session_unpause_message (GstSoupHTTPSrc * src)
724 soup_session_unpause_message (src->session, src->msg);
728 gst_soup_http_src_session_pause_message (GstSoupHTTPSrc * src)
730 soup_session_pause_message (src->session, src->msg);
734 gst_soup_http_src_session_open (GstSoupHTTPSrc * src)
737 GST_DEBUG_OBJECT (src, "Session is already open");
741 if (!src->location) {
742 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (_("No URL set.")),
743 ("Missing location property"));
748 src->context = g_main_context_new ();
751 src->loop = g_main_loop_new (src->context, TRUE);
753 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
754 (NULL), ("Failed to start GMainLoop"));
755 g_main_context_unref (src->context);
760 GST_DEBUG_OBJECT (src, "Creating session");
761 if (src->proxy == NULL) {
763 soup_session_async_new_with_options (SOUP_SESSION_ASYNC_CONTEXT,
764 src->context, SOUP_SESSION_USER_AGENT, src->user_agent,
765 SOUP_SESSION_TIMEOUT, src->timeout,
766 SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_PROXY_RESOLVER_DEFAULT,
770 soup_session_async_new_with_options (SOUP_SESSION_ASYNC_CONTEXT,
771 src->context, SOUP_SESSION_PROXY_URI, src->proxy,
772 SOUP_SESSION_TIMEOUT, src->timeout,
773 SOUP_SESSION_USER_AGENT, src->user_agent, NULL);
777 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
778 (NULL), ("Failed to create async session"));
782 g_signal_connect (src->session, "authenticate",
783 G_CALLBACK (gst_soup_http_src_authenticate_cb), src);
786 gst_soup_util_log_setup (src->session, src->log_level, GST_ELEMENT (src));
788 GST_DEBUG_OBJECT (src, "Re-using session");
792 soup_session_add_feature_by_type (src->session, SOUP_TYPE_CONTENT_DECODER);
794 soup_session_remove_feature_by_type (src->session,
795 SOUP_TYPE_CONTENT_DECODER);
801 gst_soup_http_src_session_close (GstSoupHTTPSrc * src)
804 soup_session_abort (src->session); /* This unrefs the message. */
805 g_object_unref (src->session);
810 g_main_loop_unref (src->loop);
811 g_main_context_unref (src->context);
818 gst_soup_http_src_authenticate_cb (SoupSession * session, SoupMessage * msg,
819 SoupAuth * auth, gboolean retrying, GstSoupHTTPSrc * src)
822 /* First time authentication only, if we fail and are called again with retry true fall through */
823 if (msg->status_code == SOUP_STATUS_UNAUTHORIZED) {
824 if (src->user_id && src->user_pw)
825 soup_auth_authenticate (auth, src->user_id, src->user_pw);
826 } else if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
827 if (src->proxy_id && src->proxy_pw)
828 soup_auth_authenticate (auth, src->proxy_id, src->proxy_pw);
834 gst_soup_http_src_got_headers_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
837 GstTagList *tag_list;
840 GHashTable *params = NULL;
842 GST_INFO_OBJECT (src, "got headers");
844 if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED &&
845 src->proxy_id && src->proxy_pw)
848 if (src->automatic_redirect && SOUP_STATUS_IS_REDIRECTION (msg->status_code)) {
849 src->redirection_uri = g_strdup (soup_message_headers_get_one
850 (msg->response_headers, "Location"));
851 GST_DEBUG_OBJECT (src, "%u redirect to \"%s\"", msg->status_code,
852 src->redirection_uri);
856 if (msg->status_code == SOUP_STATUS_UNAUTHORIZED)
859 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING;
860 src->got_headers = TRUE;
862 /* Parse Content-Length. */
863 if (soup_message_headers_get_encoding (msg->response_headers) ==
864 SOUP_ENCODING_CONTENT_LENGTH) {
865 newsize = src->request_position +
866 soup_message_headers_get_content_length (msg->response_headers);
867 if (!src->have_size || (src->content_size != newsize)) {
868 src->content_size = newsize;
869 src->have_size = TRUE;
870 src->seekable = TRUE;
871 GST_DEBUG_OBJECT (src, "size = %" G_GUINT64_FORMAT, src->content_size);
873 basesrc = GST_BASE_SRC_CAST (src);
874 basesrc->segment.duration = src->content_size;
875 gst_element_post_message (GST_ELEMENT (src),
876 gst_message_new_duration_changed (GST_OBJECT (src)));
881 tag_list = gst_tag_list_new_empty ();
884 soup_message_headers_get_one (msg->response_headers,
885 "icy-metaint")) != NULL) {
886 gint icy_metaint = atoi (value);
888 GST_DEBUG_OBJECT (src, "icy-metaint: %s (parsed: %d)", value, icy_metaint);
889 if (icy_metaint > 0) {
891 gst_caps_unref (src->src_caps);
893 src->src_caps = gst_caps_new_simple ("application/x-icy",
894 "metadata-interval", G_TYPE_INT, icy_metaint, NULL);
896 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
900 soup_message_headers_get_content_type (msg->response_headers,
902 GST_DEBUG_OBJECT (src, "Content-Type: %s", value);
903 if (g_ascii_strcasecmp (value, "audio/L16") == 0) {
909 gst_caps_unref (src->src_caps);
911 param = g_hash_table_lookup (params, "channels");
913 channels = atol (param);
915 param = g_hash_table_lookup (params, "rate");
919 src->src_caps = gst_caps_new_simple ("audio/x-raw",
920 "format", G_TYPE_STRING, "S16BE",
921 "layout", G_TYPE_STRING, "interleaved",
922 "channels", G_TYPE_INT, channels, "rate", G_TYPE_INT, rate, NULL);
924 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
926 /* Set the Content-Type field on the caps */
928 src->src_caps = gst_caps_make_writable (src->src_caps);
929 gst_caps_set_simple (src->src_caps, "content-type", G_TYPE_STRING,
931 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
937 g_hash_table_destroy (params);
940 soup_message_headers_get_one (msg->response_headers,
941 "icy-name")) != NULL) {
942 g_free (src->iradio_name);
943 src->iradio_name = gst_soup_http_src_unicodify (value);
944 if (src->iradio_name) {
945 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_ORGANIZATION,
946 src->iradio_name, NULL);
950 soup_message_headers_get_one (msg->response_headers,
951 "icy-genre")) != NULL) {
952 g_free (src->iradio_genre);
953 src->iradio_genre = gst_soup_http_src_unicodify (value);
954 if (src->iradio_genre) {
955 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_GENRE,
956 src->iradio_genre, NULL);
959 if ((value = soup_message_headers_get_one (msg->response_headers, "icy-url"))
961 g_free (src->iradio_url);
962 src->iradio_url = gst_soup_http_src_unicodify (value);
963 if (src->iradio_url) {
964 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_LOCATION,
965 src->iradio_url, NULL);
968 if (!gst_tag_list_is_empty (tag_list)) {
969 GST_DEBUG_OBJECT (src,
970 "calling gst_element_found_tags with %" GST_PTR_FORMAT, tag_list);
971 gst_pad_push_event (GST_BASE_SRC_PAD (src), gst_event_new_tag (tag_list));
973 gst_tag_list_unref (tag_list);
976 /* Handle HTTP errors. */
977 gst_soup_http_src_parse_status (msg, src);
979 /* Check if Range header was respected. */
980 if (src->ret == GST_FLOW_CUSTOM_ERROR &&
981 src->read_position && msg->status_code != SOUP_STATUS_PARTIAL_CONTENT) {
982 src->seekable = FALSE;
983 GST_ELEMENT_ERROR (src, RESOURCE, SEEK,
984 (_("Server does not support seeking.")),
985 ("Server does not accept Range HTTP header, URL: %s", src->location));
986 src->ret = GST_FLOW_ERROR;
989 /* If we are going to error out, stop all processing right here, so we
990 * don't output any data (such as an error html page), and return
991 * GST_FLOW_ERROR from the create function instead of having
992 * got_chunk_cb overwrite src->ret with FLOW_OK again. */
993 if (src->ret == GST_FLOW_ERROR || src->ret == GST_FLOW_EOS) {
994 gst_soup_http_src_session_pause_message (src);
997 g_main_loop_quit (src->loop);
999 g_cond_signal (&src->request_finished_cond);
1002 /* Have body. Signal EOS. */
1004 gst_soup_http_src_got_body_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
1006 if (G_UNLIKELY (msg != src->msg)) {
1007 GST_DEBUG_OBJECT (src, "got body, but not for current message");
1010 if (G_UNLIKELY (src->session_io_status !=
1011 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
1012 /* Probably a redirect. */
1015 GST_DEBUG_OBJECT (src, "got body");
1016 src->ret = GST_FLOW_EOS;
1017 src->have_body = TRUE;
1019 /* no need to interrupt the message here, we do it on the
1020 * finished_cb anyway if needed. And getting the body might mean
1021 * that the connection was hang up before finished. This happens when
1022 * the pipeline is stalled for too long (long pauses during playback).
1023 * Best to let it continue from here and pause because it reached the
1024 * final bytes based on content_size or received an out of range error */
1027 /* Finished. Signal EOS. */
1029 gst_soup_http_src_finished_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
1031 if (G_UNLIKELY (msg != src->msg)) {
1032 GST_DEBUG_OBJECT (src, "finished, but not for current message");
1035 GST_DEBUG_OBJECT (src, "finished");
1036 src->ret = GST_FLOW_EOS;
1037 if (src->session_io_status == GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_CANCELLED) {
1038 /* gst_soup_http_src_cancel_message() triggered this; probably a seek
1039 * that occurred in the QUEUEING state; i.e. before the connection setup
1040 * was complete. Do nothing */
1041 } else if (src->session_io_status ==
1042 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING && src->read_position > 0 &&
1043 (src->have_size && src->read_position < src->content_size)) {
1044 /* The server disconnected while streaming. Reconnect and seeking to the
1047 src->ret = GST_FLOW_CUSTOM_ERROR;
1048 } else if (G_UNLIKELY (src->session_io_status !=
1049 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
1050 if (msg->method == SOUP_METHOD_HEAD) {
1051 GST_DEBUG_OBJECT (src, "Ignoring error %d:%s during HEAD request",
1052 msg->status_code, msg->reason_phrase);
1054 gst_soup_http_src_parse_status (msg, src);
1058 g_main_loop_quit (src->loop);
1059 g_cond_signal (&src->request_finished_cond);
1062 /* Buffer lifecycle management.
1064 * gst_soup_http_src_create() runs the GMainLoop for this element, to let
1065 * Soup take control.
1066 * A GstBuffer is allocated in gst_soup_http_src_chunk_allocator() and
1067 * associated with a SoupBuffer.
1068 * Soup reads HTTP data in the GstBuffer's data buffer.
1069 * The gst_soup_http_src_got_chunk_cb() is then called with the SoupBuffer.
1070 * That sets gst_soup_http_src_create()'s return argument to the GstBuffer,
1071 * increments its refcount (to 2), pauses the flow of data from the HTTP
1072 * source to prevent gst_soup_http_src_got_chunk_cb() from being called
1073 * again and breaks out of the GMainLoop.
1074 * Because the SOUP_MESSAGE_OVERWRITE_CHUNKS flag is set, Soup frees the
1075 * SoupBuffer and calls gst_soup_http_src_chunk_free(), which decrements the
1077 * gst_soup_http_src_create() returns the GstBuffer. It will be freed by a
1078 * downstream element.
1079 * If Soup fails to read HTTP data, it does not call
1080 * gst_soup_http_src_got_chunk_cb(), but still frees the SoupBuffer and
1081 * calls gst_soup_http_src_chunk_free(), which decrements the GstBuffer's
1082 * refcount to 0, freeing it.
1092 gst_soup_http_src_chunk_free (gpointer user_data)
1094 SoupGstChunk *chunk = (SoupGstChunk *) user_data;
1096 gst_buffer_unmap (chunk->buffer, &chunk->map);
1097 gst_buffer_unref (chunk->buffer);
1098 g_slice_free (SoupGstChunk, chunk);
1102 gst_soup_http_src_chunk_allocator (SoupMessage * msg, gsize max_len,
1105 GstSoupHTTPSrc *src = (GstSoupHTTPSrc *) user_data;
1106 GstBaseSrc *basesrc = GST_BASE_SRC_CAST (src);
1108 SoupBuffer *soupbuf;
1111 SoupGstChunk *chunk;
1114 length = MIN (basesrc->blocksize, max_len);
1116 length = basesrc->blocksize;
1117 GST_DEBUG_OBJECT (src, "alloc %" G_GSIZE_FORMAT " bytes <= %" G_GSIZE_FORMAT,
1120 rc = GST_BASE_SRC_CLASS (parent_class)->alloc (basesrc, -1, length, &gstbuf);
1121 if (G_UNLIKELY (rc != GST_FLOW_OK)) {
1122 /* Failed to allocate buffer. Stall SoupSession and return error code
1125 g_main_loop_quit (src->loop);
1129 chunk = g_slice_new0 (SoupGstChunk);
1130 chunk->buffer = gstbuf;
1131 gst_buffer_map (gstbuf, &chunk->map, GST_MAP_READWRITE);
1133 soupbuf = soup_buffer_new_with_owner (chunk->map.data, chunk->map.size,
1134 chunk, gst_soup_http_src_chunk_free);
1140 gst_soup_http_src_got_chunk_cb (SoupMessage * msg, SoupBuffer * chunk,
1141 GstSoupHTTPSrc * src)
1143 GstBaseSrc *basesrc;
1144 guint64 new_position;
1145 SoupGstChunk *gchunk;
1147 if (G_UNLIKELY (msg != src->msg)) {
1148 GST_DEBUG_OBJECT (src, "got chunk, but not for current message");
1151 src->have_body = FALSE;
1152 if (G_UNLIKELY (src->session_io_status !=
1153 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
1154 /* Probably a redirect. */
1157 basesrc = GST_BASE_SRC_CAST (src);
1158 GST_DEBUG_OBJECT (src, "got chunk of %" G_GSIZE_FORMAT " bytes",
1161 /* Extract the GstBuffer from the SoupBuffer and set its fields. */
1162 gchunk = (SoupGstChunk *) soup_buffer_get_owner (chunk);
1163 *src->outbuf = gchunk->buffer;
1165 gst_buffer_resize (*src->outbuf, 0, chunk->length);
1166 GST_BUFFER_OFFSET (*src->outbuf) = basesrc->segment.position;
1168 gst_buffer_ref (*src->outbuf);
1170 new_position = src->read_position + chunk->length;
1171 if (G_LIKELY (src->request_position == src->read_position))
1172 src->request_position = new_position;
1173 src->read_position = new_position;
1175 if (src->content_size != 0 && new_position > src->content_size) {
1176 GST_DEBUG_OBJECT (src, "Got position previous estimated content size "
1177 "(%" G_GINT64_FORMAT " > %" G_GINT64_FORMAT ")", new_position,
1179 src->content_size = new_position;
1180 basesrc->segment.duration = src->content_size;
1181 gst_element_post_message (GST_ELEMENT (src),
1182 gst_message_new_duration_changed (GST_OBJECT (src)));
1185 src->ret = GST_FLOW_OK;
1186 g_main_loop_quit (src->loop);
1187 gst_soup_http_src_session_pause_message (src);
1191 gst_soup_http_src_response_cb (SoupSession * session, SoupMessage * msg,
1192 GstSoupHTTPSrc * src)
1194 if (G_UNLIKELY (msg != src->msg)) {
1195 GST_DEBUG_OBJECT (src, "got response %d: %s, but not for current message",
1196 msg->status_code, msg->reason_phrase);
1199 if (G_UNLIKELY (src->session_io_status !=
1200 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)
1201 && SOUP_STATUS_IS_REDIRECTION (msg->status_code)) {
1202 /* Ignore redirections. */
1205 GST_DEBUG_OBJECT (src, "got response %d: %s", msg->status_code,
1206 msg->reason_phrase);
1207 if (src->session_io_status == GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING &&
1208 src->read_position > 0 && (src->have_size
1209 && src->read_position < src->content_size)) {
1210 /* The server disconnected while streaming. Reconnect and seeking to the
1214 gst_soup_http_src_parse_status (msg, src);
1215 /* The session's SoupMessage object expires after this callback returns. */
1217 g_main_loop_quit (src->loop);
1220 #define SOUP_HTTP_SRC_ERROR(src,soup_msg,cat,code,error_message) \
1221 GST_ELEMENT_ERROR ((src), cat, code, ("%s", error_message), \
1222 ("%s (%d), URL: %s", (soup_msg)->reason_phrase, \
1223 (soup_msg)->status_code, (src)->location));
1226 gst_soup_http_src_parse_status (SoupMessage * msg, GstSoupHTTPSrc * src)
1228 if (msg->method == SOUP_METHOD_HEAD) {
1229 if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code))
1230 GST_DEBUG_OBJECT (src, "Ignoring error %d during HEAD request",
1232 } else if (SOUP_STATUS_IS_TRANSPORT_ERROR (msg->status_code)) {
1233 switch (msg->status_code) {
1234 case SOUP_STATUS_CANT_RESOLVE:
1235 case SOUP_STATUS_CANT_RESOLVE_PROXY:
1236 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, NOT_FOUND,
1237 _("Could not resolve server name."));
1238 src->ret = GST_FLOW_ERROR;
1240 case SOUP_STATUS_CANT_CONNECT:
1241 case SOUP_STATUS_CANT_CONNECT_PROXY:
1242 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ,
1243 _("Could not establish connection to server."));
1244 src->ret = GST_FLOW_ERROR;
1246 case SOUP_STATUS_SSL_FAILED:
1247 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ,
1248 _("Secure connection setup failed."));
1249 src->ret = GST_FLOW_ERROR;
1251 case SOUP_STATUS_IO_ERROR:
1252 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, READ,
1253 _("A network error occured, or the server closed the connection "
1255 src->ret = GST_FLOW_ERROR;
1257 case SOUP_STATUS_MALFORMED:
1258 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, READ,
1259 _("Server sent bad data."));
1260 src->ret = GST_FLOW_ERROR;
1262 case SOUP_STATUS_CANCELLED:
1263 /* No error message when interrupted by program. */
1266 } else if (SOUP_STATUS_IS_CLIENT_ERROR (msg->status_code) ||
1267 SOUP_STATUS_IS_REDIRECTION (msg->status_code) ||
1268 SOUP_STATUS_IS_SERVER_ERROR (msg->status_code)) {
1269 /* Report HTTP error. */
1271 /* when content_size is unknown and we have just finished receiving
1272 * a body message, requests that go beyond the content limits will result
1273 * in an error. Here we convert those to EOS */
1274 if (msg->status_code == SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE &&
1275 src->have_body && src->have_size) {
1276 GST_DEBUG_OBJECT (src, "Requested range out of limits and received full "
1277 "body, returning EOS");
1278 src->ret = GST_FLOW_EOS;
1282 /* FIXME: reason_phrase is not translated and not suitable for user
1283 * error dialog according to libsoup documentation.
1285 if (msg->status_code == SOUP_STATUS_NOT_FOUND) {
1286 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
1287 ("%s", msg->reason_phrase),
1288 ("%s (%d), URL: %s", msg->reason_phrase, msg->status_code,
1290 } else if (msg->status_code == SOUP_STATUS_UNAUTHORIZED ||
1291 msg->status_code == SOUP_STATUS_PAYMENT_REQUIRED ||
1292 msg->status_code == SOUP_STATUS_FORBIDDEN ||
1293 msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
1294 GST_ELEMENT_ERROR (src, RESOURCE, NOT_AUTHORIZED,
1295 ("%s", msg->reason_phrase),
1296 ("%s (%d), URL: %s", msg->reason_phrase, msg->status_code,
1299 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1300 ("%s", msg->reason_phrase),
1301 ("%s (%d), URL: %s", msg->reason_phrase, msg->status_code,
1304 src->ret = GST_FLOW_ERROR;
1309 gst_soup_http_src_build_message (GstSoupHTTPSrc * src, const gchar * method)
1311 src->msg = soup_message_new (method, src->location);
1313 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1314 ("Error parsing URL."), ("URL: %s", src->location));
1317 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE;
1318 if (!src->keep_alive) {
1319 soup_message_headers_append (src->msg->request_headers, "Connection",
1322 if (src->iradio_mode) {
1323 soup_message_headers_append (src->msg->request_headers, "icy-metadata",
1329 for (cookie = src->cookies; *cookie != NULL; cookie++) {
1330 soup_message_headers_append (src->msg->request_headers, "Cookie",
1336 g_signal_connect (src->msg, "got_headers",
1337 G_CALLBACK (gst_soup_http_src_got_headers_cb), src);
1338 g_signal_connect (src->msg, "got_body",
1339 G_CALLBACK (gst_soup_http_src_got_body_cb), src);
1340 g_signal_connect (src->msg, "finished",
1341 G_CALLBACK (gst_soup_http_src_finished_cb), src);
1342 g_signal_connect (src->msg, "got_chunk",
1343 G_CALLBACK (gst_soup_http_src_got_chunk_cb), src);
1344 soup_message_set_flags (src->msg, SOUP_MESSAGE_OVERWRITE_CHUNKS |
1345 (src->automatic_redirect ? 0 : SOUP_MESSAGE_NO_REDIRECT));
1346 soup_message_set_chunk_allocator (src->msg,
1347 gst_soup_http_src_chunk_allocator, src, NULL);
1348 gst_soup_http_src_add_range_header (src, src->request_position,
1349 src->stop_position);
1351 gst_soup_http_src_add_extra_headers (src);
1356 static GstFlowReturn
1357 gst_soup_http_src_do_request (GstSoupHTTPSrc * src, const gchar * method,
1358 GstBuffer ** outbuf)
1360 GST_LOG_OBJECT (src, "Running request for method: %s", method);
1361 if (src->msg && (src->request_position != src->read_position)) {
1362 if (src->session_io_status == GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE) {
1363 gst_soup_http_src_add_range_header (src, src->request_position,
1364 src->stop_position);
1366 GST_DEBUG_OBJECT (src, "Seek from position %" G_GUINT64_FORMAT
1367 " to %" G_GUINT64_FORMAT ": requeueing connection request",
1368 src->read_position, src->request_position);
1369 gst_soup_http_src_cancel_message (src);
1373 if (!gst_soup_http_src_build_message (src, method)) {
1374 return GST_FLOW_ERROR;
1377 src->ret = GST_FLOW_CUSTOM_ERROR;
1378 src->outbuf = outbuf;
1380 if (src->interrupted) {
1381 GST_DEBUG_OBJECT (src, "interrupted");
1385 GST_DEBUG_OBJECT (src, "Reconnecting");
1386 if (!gst_soup_http_src_build_message (src, method)) {
1387 return GST_FLOW_ERROR;
1393 GST_DEBUG_OBJECT (src, "EOS reached");
1397 switch (src->session_io_status) {
1398 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE:
1399 GST_DEBUG_OBJECT (src, "Queueing connection request");
1400 gst_soup_http_src_queue_message (src);
1402 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_QUEUED:
1404 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING:
1405 gst_soup_http_src_session_unpause_message (src);
1407 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_CANCELLED:
1412 if (src->ret == GST_FLOW_CUSTOM_ERROR)
1413 g_main_loop_run (src->loop);
1414 } while (src->ret == GST_FLOW_CUSTOM_ERROR);
1416 if (src->ret == GST_FLOW_CUSTOM_ERROR)
1417 src->ret = GST_FLOW_EOS;
1418 g_cond_signal (&src->request_finished_cond);
1422 static GstFlowReturn
1423 gst_soup_http_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
1425 GstSoupHTTPSrc *src;
1428 src = GST_SOUP_HTTP_SRC (psrc);
1430 g_mutex_lock (&src->mutex);
1431 ret = gst_soup_http_src_do_request (src, SOUP_METHOD_GET, outbuf);
1432 g_mutex_unlock (&src->mutex);
1437 gst_soup_http_src_start (GstBaseSrc * bsrc)
1439 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1441 GST_DEBUG_OBJECT (src, "start(\"%s\")", src->location);
1443 return gst_soup_http_src_session_open (src);
1447 gst_soup_http_src_stop (GstBaseSrc * bsrc)
1449 GstSoupHTTPSrc *src;
1451 src = GST_SOUP_HTTP_SRC (bsrc);
1452 GST_DEBUG_OBJECT (src, "stop()");
1453 if (src->keep_alive)
1454 gst_soup_http_src_cancel_message (src);
1456 gst_soup_http_src_session_close (src);
1458 if (src->extra_headers) {
1459 gst_structure_free (src->extra_headers);
1460 src->extra_headers = NULL;
1463 gst_soup_http_src_reset (src);
1467 /* Interrupt a blocking request. */
1469 gst_soup_http_src_unlock (GstBaseSrc * bsrc)
1471 GstSoupHTTPSrc *src;
1473 src = GST_SOUP_HTTP_SRC (bsrc);
1474 GST_DEBUG_OBJECT (src, "unlock()");
1476 src->interrupted = TRUE;
1478 g_main_loop_quit (src->loop);
1479 g_cond_signal (&src->request_finished_cond);
1483 /* Interrupt interrupt. */
1485 gst_soup_http_src_unlock_stop (GstBaseSrc * bsrc)
1487 GstSoupHTTPSrc *src;
1489 src = GST_SOUP_HTTP_SRC (bsrc);
1490 GST_DEBUG_OBJECT (src, "unlock_stop()");
1492 src->interrupted = FALSE;
1497 gst_soup_http_src_get_size (GstBaseSrc * bsrc, guint64 * size)
1499 GstSoupHTTPSrc *src;
1501 src = GST_SOUP_HTTP_SRC (bsrc);
1503 if (src->have_size) {
1504 GST_DEBUG_OBJECT (src, "get_size() = %" G_GUINT64_FORMAT,
1506 *size = src->content_size;
1509 GST_DEBUG_OBJECT (src, "get_size() = FALSE");
1514 gst_soup_http_src_check_seekable (GstSoupHTTPSrc * src)
1516 GstFlowReturn ret = GST_FLOW_OK;
1518 /* Special case to check if the server allows range requests
1519 * before really starting to get data in the buffer creation
1522 if (!src->got_headers && GST_STATE (src) >= GST_STATE_PAUSED) {
1523 g_mutex_lock (&src->mutex);
1524 while (!src->got_headers && !src->interrupted && ret == GST_FLOW_OK) {
1525 if ((src->msg && src->msg->method != SOUP_METHOD_HEAD) &&
1526 src->session_io_status != GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE) {
1527 /* wait for the current request to finish */
1528 g_cond_wait (&src->request_finished_cond, &src->mutex);
1530 if (gst_soup_http_src_session_open (src)) {
1531 ret = gst_soup_http_src_do_request (src, SOUP_METHOD_HEAD, NULL);
1535 if (src->ret == GST_FLOW_EOS) {
1536 /* A HEAD request shouldn't lead to EOS */
1537 src->ret = GST_FLOW_OK;
1539 /* resets status to idle */
1540 gst_soup_http_src_cancel_message (src);
1541 g_mutex_unlock (&src->mutex);
1547 gst_soup_http_src_is_seekable (GstBaseSrc * bsrc)
1549 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1551 gst_soup_http_src_check_seekable (src);
1553 return src->seekable;
1557 gst_soup_http_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
1559 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1561 GST_DEBUG_OBJECT (src, "do_seek(%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1562 ")", segment->start, segment->stop);
1563 if (src->read_position == segment->start &&
1564 src->request_position == src->read_position &&
1565 src->stop_position == segment->stop) {
1566 GST_DEBUG_OBJECT (src,
1567 "Seek to current read/end position and no seek pending");
1571 gst_soup_http_src_check_seekable (src);
1573 /* If we have no headers we don't know yet if it is seekable or not.
1574 * Store the start position and error out later if it isn't */
1575 if (src->got_headers && !src->seekable) {
1576 GST_WARNING_OBJECT (src, "Not seekable");
1580 if (segment->rate < 0.0 || segment->format != GST_FORMAT_BYTES) {
1581 GST_WARNING_OBJECT (src, "Invalid seek segment");
1585 if (src->content_size != 0 && segment->start >= src->content_size) {
1586 GST_WARNING_OBJECT (src,
1587 "Potentially seeking behind end of file, might EOS immediately");
1590 /* Wait for create() to handle the jump in offset. */
1591 src->request_position = segment->start;
1592 src->stop_position = segment->stop;
1597 gst_soup_http_src_query (GstBaseSrc * bsrc, GstQuery * query)
1599 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1601 GstSchedulingFlags flags;
1602 gint minsize, maxsize, align;
1604 switch (GST_QUERY_TYPE (query)) {
1606 gst_query_set_uri (query, src->location);
1607 if (src->redirection_uri != NULL)
1608 gst_query_set_uri_redirection (query, src->redirection_uri);
1617 ret = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
1619 switch (GST_QUERY_TYPE (query)) {
1620 case GST_QUERY_SCHEDULING:
1621 gst_query_parse_scheduling (query, &flags, &minsize, &maxsize, &align);
1622 flags |= GST_SCHEDULING_FLAG_BANDWIDTH_LIMITED;
1623 gst_query_set_scheduling (query, flags, minsize, maxsize, align);
1633 gst_soup_http_src_set_location (GstSoupHTTPSrc * src, const gchar * uri,
1636 const char *alt_schemes[] = { "icy://", "icyx://" };
1639 if (src->location) {
1640 g_free (src->location);
1641 src->location = NULL;
1647 for (i = 0; i < G_N_ELEMENTS (alt_schemes); i++) {
1648 if (g_str_has_prefix (uri, alt_schemes[i])) {
1650 g_strdup_printf ("http://%s", uri + strlen (alt_schemes[i]));
1655 if (src->redirection_uri) {
1656 g_free (src->redirection_uri);
1657 src->redirection_uri = NULL;
1660 src->location = g_strdup (uri);
1666 gst_soup_http_src_set_proxy (GstSoupHTTPSrc * src, const gchar * uri)
1669 soup_uri_free (src->proxy);
1672 if (g_str_has_prefix (uri, "http://")) {
1673 src->proxy = soup_uri_new (uri);
1675 gchar *new_uri = g_strconcat ("http://", uri, NULL);
1677 src->proxy = soup_uri_new (new_uri);
1685 gst_soup_http_src_uri_get_type (GType type)
1690 static const gchar *const *
1691 gst_soup_http_src_uri_get_protocols (GType type)
1693 static const gchar *protocols[] = { "http", "https", "icy", "icyx", NULL };
1699 gst_soup_http_src_uri_get_uri (GstURIHandler * handler)
1701 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (handler);
1703 /* FIXME: make thread-safe */
1704 return g_strdup (src->location);
1708 gst_soup_http_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
1711 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (handler);
1713 return gst_soup_http_src_set_location (src, uri, error);
1717 gst_soup_http_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1719 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1721 iface->get_type = gst_soup_http_src_uri_get_type;
1722 iface->get_protocols = gst_soup_http_src_uri_get_protocols;
1723 iface->get_uri = gst_soup_http_src_uri_get_uri;
1724 iface->set_uri = gst_soup_http_src_uri_set_uri;