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 -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 -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 -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 #ifdef HAVE_LIBSOUP_GNOME
81 #include <libsoup/soup-gnome.h>
83 #include <libsoup/soup.h>
85 #include "gstsouphttpsrc.h"
87 #include <gst/tag/tag.h>
90 GST_DEBUG_CATEGORY_STATIC (souphttpsrc_debug);
91 #define GST_CAT_DEFAULT souphttpsrc_debug
93 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
104 PROP_AUTOMATIC_REDIRECT,
125 #define DEFAULT_USER_AGENT "GStreamer souphttpsrc "
127 static void gst_soup_http_src_uri_handler_init (gpointer g_iface,
128 gpointer iface_data);
129 static void gst_soup_http_src_finalize (GObject * gobject);
131 static void gst_soup_http_src_set_property (GObject * object, guint prop_id,
132 const GValue * value, GParamSpec * pspec);
133 static void gst_soup_http_src_get_property (GObject * object, guint prop_id,
134 GValue * value, GParamSpec * pspec);
136 static GstFlowReturn gst_soup_http_src_create (GstPushSrc * psrc,
137 GstBuffer ** outbuf);
138 static gboolean gst_soup_http_src_start (GstBaseSrc * bsrc);
139 static gboolean gst_soup_http_src_stop (GstBaseSrc * bsrc);
140 static gboolean gst_soup_http_src_get_size (GstBaseSrc * bsrc, guint64 * size);
141 static gboolean gst_soup_http_src_is_seekable (GstBaseSrc * bsrc);
142 static gboolean gst_soup_http_src_do_seek (GstBaseSrc * bsrc,
143 GstSegment * segment);
144 static gboolean gst_soup_http_src_query (GstBaseSrc * bsrc, GstQuery * query);
145 static gboolean gst_soup_http_src_unlock (GstBaseSrc * bsrc);
146 static gboolean gst_soup_http_src_unlock_stop (GstBaseSrc * bsrc);
147 static gboolean gst_soup_http_src_set_location (GstSoupHTTPSrc * src,
149 static gboolean gst_soup_http_src_set_proxy (GstSoupHTTPSrc * src,
151 static char *gst_soup_http_src_unicodify (const char *str);
152 static gboolean gst_soup_http_src_build_message (GstSoupHTTPSrc * src);
153 static void gst_soup_http_src_cancel_message (GstSoupHTTPSrc * src);
154 static void gst_soup_http_src_queue_message (GstSoupHTTPSrc * src);
155 static gboolean gst_soup_http_src_add_range_header (GstSoupHTTPSrc * src,
157 static void gst_soup_http_src_session_unpause_message (GstSoupHTTPSrc * src);
158 static void gst_soup_http_src_session_pause_message (GstSoupHTTPSrc * src);
159 static void gst_soup_http_src_session_close (GstSoupHTTPSrc * src);
160 static void gst_soup_http_src_parse_status (SoupMessage * msg,
161 GstSoupHTTPSrc * src);
162 static void gst_soup_http_src_chunk_free (gpointer gstbuf);
163 static SoupBuffer *gst_soup_http_src_chunk_allocator (SoupMessage * msg,
164 gsize max_len, gpointer user_data);
165 static void gst_soup_http_src_got_chunk_cb (SoupMessage * msg,
166 SoupBuffer * chunk, GstSoupHTTPSrc * src);
167 static void gst_soup_http_src_response_cb (SoupSession * session,
168 SoupMessage * msg, GstSoupHTTPSrc * src);
169 static void gst_soup_http_src_got_headers_cb (SoupMessage * msg,
170 GstSoupHTTPSrc * src);
171 static void gst_soup_http_src_got_body_cb (SoupMessage * msg,
172 GstSoupHTTPSrc * src);
173 static void gst_soup_http_src_finished_cb (SoupMessage * msg,
174 GstSoupHTTPSrc * src);
175 static void gst_soup_http_src_authenticate_cb (SoupSession * session,
176 SoupMessage * msg, SoupAuth * auth, gboolean retrying,
177 GstSoupHTTPSrc * src);
180 _do_init (GType type)
182 static const GInterfaceInfo urihandler_info = {
183 gst_soup_http_src_uri_handler_init,
188 g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
190 GST_DEBUG_CATEGORY_INIT (souphttpsrc_debug, "souphttpsrc", 0,
194 GST_BOILERPLATE_FULL (GstSoupHTTPSrc, gst_soup_http_src, GstPushSrc,
195 GST_TYPE_PUSH_SRC, _do_init);
198 gst_soup_http_src_base_init (gpointer g_class)
200 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
202 gst_element_class_add_static_pad_template (element_class, &srctemplate);
204 gst_element_class_set_details_simple (element_class, "HTTP client source",
206 "Receive data as a client over the network via HTTP using SOUP",
207 "Wouter Cloetens <wouter@mind.be>");
211 gst_soup_http_src_class_init (GstSoupHTTPSrcClass * klass)
213 GObjectClass *gobject_class;
214 GstBaseSrcClass *gstbasesrc_class;
215 GstPushSrcClass *gstpushsrc_class;
217 gobject_class = (GObjectClass *) klass;
218 gstbasesrc_class = (GstBaseSrcClass *) klass;
219 gstpushsrc_class = (GstPushSrcClass *) klass;
221 gobject_class->set_property = gst_soup_http_src_set_property;
222 gobject_class->get_property = gst_soup_http_src_get_property;
223 gobject_class->finalize = gst_soup_http_src_finalize;
225 g_object_class_install_property (gobject_class,
227 g_param_spec_string ("location", "Location",
228 "Location to read from", "",
229 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
230 g_object_class_install_property (gobject_class,
232 g_param_spec_string ("user-agent", "User-Agent",
233 "Value of the User-Agent HTTP request header field",
234 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
235 g_object_class_install_property (gobject_class,
236 PROP_AUTOMATIC_REDIRECT,
237 g_param_spec_boolean ("automatic-redirect", "automatic-redirect",
238 "Automatically follow HTTP redirects (HTTP Status Code 3xx)",
239 TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
240 g_object_class_install_property (gobject_class,
242 g_param_spec_string ("proxy", "Proxy",
243 "HTTP proxy server URI", "",
244 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
245 g_object_class_install_property (gobject_class,
247 g_param_spec_string ("user-id", "user-id",
248 "HTTP location URI user id for authentication", "",
249 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
250 g_object_class_install_property (gobject_class, PROP_USER_PW,
251 g_param_spec_string ("user-pw", "user-pw",
252 "HTTP location URI user password for authentication", "",
253 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
254 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
255 g_param_spec_string ("proxy-id", "proxy-id",
256 "HTTP proxy URI user id for authentication", "",
257 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
258 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
259 g_param_spec_string ("proxy-pw", "proxy-pw",
260 "HTTP proxy URI user password for authentication", "",
261 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
262 g_object_class_install_property (gobject_class, PROP_COOKIES,
263 g_param_spec_boxed ("cookies", "Cookies", "HTTP request cookies",
264 G_TYPE_STRV, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
265 g_object_class_install_property (gobject_class, PROP_IS_LIVE,
266 g_param_spec_boolean ("is-live", "is-live", "Act like a live source",
267 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
268 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
269 g_param_spec_uint ("timeout", "timeout",
270 "Value in seconds to timeout a blocking I/O (0 = No timeout).", 0,
271 3600, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
272 g_object_class_install_property (gobject_class, PROP_EXTRA_HEADERS,
273 g_param_spec_boxed ("extra-headers", "Extra Headers",
274 "Extra headers to append to the HTTP request",
275 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
278 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
279 g_param_spec_int64 ("blocksize", "blocksize",
280 "Size of each buffer downloaded from libsoup",
281 -1, G_MAXUINT, 4096, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
284 g_object_class_install_property (gobject_class,
286 g_param_spec_boolean ("iradio-mode",
288 "Enable internet radio mode (extraction of shoutcast/icecast metadata)",
289 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
290 g_object_class_install_property (gobject_class,
292 g_param_spec_string ("iradio-name",
293 "iradio-name", "Name of the stream", NULL,
294 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
295 g_object_class_install_property (gobject_class,
297 g_param_spec_string ("iradio-genre",
298 "iradio-genre", "Genre of the stream", NULL,
299 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
300 g_object_class_install_property (gobject_class,
302 g_param_spec_string ("iradio-url",
304 "Homepage URL for radio stream", NULL,
305 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
306 g_object_class_install_property (gobject_class,
308 g_param_spec_string ("iradio-title",
310 "Name of currently playing song", NULL,
311 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
313 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_soup_http_src_start);
314 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_soup_http_src_stop);
315 gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_soup_http_src_unlock);
316 gstbasesrc_class->unlock_stop =
317 GST_DEBUG_FUNCPTR (gst_soup_http_src_unlock_stop);
318 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_soup_http_src_get_size);
319 gstbasesrc_class->is_seekable =
320 GST_DEBUG_FUNCPTR (gst_soup_http_src_is_seekable);
321 gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_soup_http_src_do_seek);
322 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_soup_http_src_query);
324 gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_soup_http_src_create);
328 gst_soup_http_src_reset (GstSoupHTTPSrc * src)
330 src->interrupted = FALSE;
332 src->have_size = FALSE;
333 src->seekable = FALSE;
334 src->read_position = 0;
335 src->request_position = 0;
336 src->content_size = 0;
341 gst_caps_replace (&src->src_caps, NULL);
342 g_free (src->iradio_name);
343 src->iradio_name = NULL;
344 g_free (src->iradio_genre);
345 src->iradio_genre = NULL;
346 g_free (src->iradio_url);
347 src->iradio_url = NULL;
348 g_free (src->iradio_title);
349 src->iradio_title = NULL;
353 gst_soup_http_src_init (GstSoupHTTPSrc * src, GstSoupHTTPSrcClass * g_class)
357 src->location = NULL;
358 src->automatic_redirect = TRUE;
359 src->user_agent = g_strdup (DEFAULT_USER_AGENT);
362 src->proxy_id = NULL;
363 src->proxy_pw = NULL;
365 src->iradio_mode = FALSE;
374 proxy = g_getenv ("http_proxy");
375 if (proxy && !gst_soup_http_src_set_proxy (src, proxy)) {
376 GST_WARNING_OBJECT (src,
377 "The proxy in the http_proxy env var (\"%s\") cannot be parsed.",
381 gst_soup_http_src_reset (src);
385 gst_soup_http_src_finalize (GObject * gobject)
387 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (gobject);
389 GST_DEBUG_OBJECT (src, "finalize");
391 g_free (src->location);
392 g_free (src->user_agent);
393 if (src->proxy != NULL) {
394 soup_uri_free (src->proxy);
396 g_free (src->user_id);
397 g_free (src->user_pw);
398 g_free (src->proxy_id);
399 g_free (src->proxy_pw);
400 g_strfreev (src->cookies);
402 G_OBJECT_CLASS (parent_class)->finalize (gobject);
406 gst_soup_http_src_set_property (GObject * object, guint prop_id,
407 const GValue * value, GParamSpec * pspec)
409 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (object);
414 const gchar *location;
416 location = g_value_get_string (value);
418 if (location == NULL) {
419 GST_WARNING ("location property cannot be NULL");
422 if (!gst_soup_http_src_set_location (src, location)) {
423 GST_WARNING ("badly formatted location");
428 case PROP_USER_AGENT:
430 g_free (src->user_agent);
431 src->user_agent = g_value_dup_string (value);
433 case PROP_IRADIO_MODE:
434 src->iradio_mode = g_value_get_boolean (value);
436 case PROP_AUTOMATIC_REDIRECT:
437 src->automatic_redirect = g_value_get_boolean (value);
443 proxy = g_value_get_string (value);
446 GST_WARNING ("proxy property cannot be NULL");
449 if (!gst_soup_http_src_set_proxy (src, proxy)) {
450 GST_WARNING ("badly formatted proxy URI");
457 #ifdef GST_EXT_SOUP_MODIFICATION
460 g_strfreev (src->cookies);
461 src->cookies = g_strdupv (g_value_get_boxed (value));
462 #ifdef GST_EXT_SOUP_MODIFICATION
463 if ((array = src->cookies) != NULL) {
464 while (*array != NULL) {
465 soup_cookie_jar_add_cookie (src->cookie_jar,
466 soup_cookie_parse (*array++, NULL));
473 gst_base_src_set_live (GST_BASE_SRC (src), g_value_get_boolean (value));
477 g_free (src->user_id);
478 src->user_id = g_value_dup_string (value);
482 g_free (src->user_pw);
483 src->user_pw = g_value_dup_string (value);
487 g_free (src->proxy_id);
488 src->proxy_id = g_value_dup_string (value);
492 g_free (src->proxy_pw);
493 src->proxy_pw = g_value_dup_string (value);
496 src->timeout = g_value_get_uint (value);
498 case PROP_EXTRA_HEADERS:{
499 const GstStructure *s = gst_value_get_structure (value);
501 if (src->extra_headers)
502 gst_structure_free (src->extra_headers);
504 src->extra_headers = s ? gst_structure_copy (s) : NULL;
508 case PROP_BLOCKSIZE:{
509 src->range_size = g_value_get_int64 (value);
514 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
522 gst_soup_http_src_get_property (GObject * object, guint prop_id,
523 GValue * value, GParamSpec * pspec)
525 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (object);
529 g_value_set_string (value, src->location);
531 case PROP_USER_AGENT:
532 g_value_set_string (value, src->user_agent);
534 case PROP_AUTOMATIC_REDIRECT:
535 g_value_set_boolean (value, src->automatic_redirect);
538 if (src->proxy == NULL)
539 g_value_set_static_string (value, "");
541 char *proxy = soup_uri_to_string (src->proxy, FALSE);
543 g_value_set_string (value, proxy);
549 #ifdef GST_EXT_SOUP_MODIFICATION
550 GSList *cookie_list, *c;
551 gchar **cookies, **array;
554 if ((cookie_list = soup_cookie_jar_all_cookies (src->cookie_jar)) != NULL) {
555 cookies = g_new0 (gchar *, g_slist_length(cookie_list) + 1);
557 for (c = cookie_list; c; c = c->next) {
558 *array++ = soup_cookie_to_set_cookie_header ((SoupCookie *)(c->data));
560 soup_cookies_free (cookie_list);
562 g_value_set_boxed (value, cookies);
564 g_value_set_boxed (value, g_strdupv (src->cookies));
569 g_value_set_boolean (value, gst_base_src_is_live (GST_BASE_SRC (src)));
571 case PROP_IRADIO_MODE:
572 g_value_set_boolean (value, src->iradio_mode);
574 case PROP_IRADIO_NAME:
575 g_value_set_string (value, src->iradio_name);
577 case PROP_IRADIO_GENRE:
578 g_value_set_string (value, src->iradio_genre);
580 case PROP_IRADIO_URL:
581 g_value_set_string (value, src->iradio_url);
583 case PROP_IRADIO_TITLE:
584 g_value_set_string (value, src->iradio_title);
587 g_value_set_string (value, src->user_id);
590 g_value_set_string (value, src->user_pw);
593 g_value_set_string (value, src->proxy_id);
596 g_value_set_string (value, src->proxy_pw);
599 g_value_set_uint (value, src->timeout);
601 case PROP_EXTRA_HEADERS:
602 gst_value_set_structure (value, src->extra_headers);
606 g_value_set_int64 (value, src->range_size);
610 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
616 gst_soup_http_src_unicodify (const gchar * str)
618 const gchar *env_vars[] = { "GST_ICY_TAG_ENCODING",
619 "GST_TAG_ENCODING", NULL
622 return gst_tag_freeform_string_to_utf8 (str, -1, env_vars);
626 gst_soup_http_src_cancel_message (GstSoupHTTPSrc * src)
628 if (src->msg != NULL) {
629 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_CANCELLED;
630 soup_session_cancel_message (src->session, src->msg, SOUP_STATUS_CANCELLED);
632 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE;
637 gst_soup_http_src_queue_message (GstSoupHTTPSrc * src)
639 soup_session_queue_message (src->session, src->msg,
640 (SoupSessionCallback) gst_soup_http_src_response_cb, src);
641 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_QUEUED;
645 gst_soup_http_src_add_range_header (GstSoupHTTPSrc * src, guint64 offset)
651 soup_message_headers_remove (src->msg->request_headers, "Range");
653 #ifdef GST_EXT_SOUP_MODIFICATION
654 /* Note : Some http server could not handle Range header in the middle of playing.
655 * Need to add Range header at first for seeking properly.
657 rc = g_snprintf (buf, sizeof (buf), "bytes=%" G_GUINT64_FORMAT "-", offset);
658 if (rc > sizeof (buf) || rc < 0)
660 soup_message_headers_append (src->msg->request_headers, "Range", buf);
663 rc = g_snprintf (buf, sizeof (buf), "bytes=%" G_GUINT64_FORMAT "-", offset);
664 if (rc > sizeof (buf) || rc < 0)
666 soup_message_headers_append (src->msg->request_headers, "Range", buf);
669 src->read_position = offset;
674 _append_extra_header (GQuark field_id, const GValue * value, gpointer user_data)
676 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (user_data);
677 const gchar *field_name = g_quark_to_string (field_id);
678 gchar *field_content = NULL;
680 if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
681 field_content = g_value_dup_string (value);
683 GValue dest = { 0, };
685 g_value_init (&dest, G_TYPE_STRING);
686 if (g_value_transform (value, &dest)) {
687 field_content = g_value_dup_string (&dest);
691 if (field_content == NULL) {
692 GST_ERROR_OBJECT (src, "extra-headers field '%s' contains no value "
693 "or can't be converted to a string", field_name);
697 GST_DEBUG_OBJECT (src, "Appending extra header: \"%s: %s\"", field_name,
699 soup_message_headers_append (src->msg->request_headers, field_name,
702 g_free (field_content);
708 _append_extra_headers (GQuark field_id, const GValue * value,
711 if (G_VALUE_TYPE (value) == GST_TYPE_ARRAY) {
712 guint n = gst_value_array_get_size (value);
715 for (i = 0; i < n; i++) {
716 const GValue *v = gst_value_array_get_value (value, i);
718 if (!_append_extra_header (field_id, v, user_data))
721 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
722 guint n = gst_value_list_get_size (value);
725 for (i = 0; i < n; i++) {
726 const GValue *v = gst_value_list_get_value (value, i);
728 if (!_append_extra_header (field_id, v, user_data))
732 return _append_extra_header (field_id, value, user_data);
740 gst_soup_http_src_add_extra_headers (GstSoupHTTPSrc * src)
742 if (!src->extra_headers)
745 return gst_structure_foreach (src->extra_headers, _append_extra_headers, src);
750 gst_soup_http_src_session_unpause_message (GstSoupHTTPSrc * src)
752 soup_session_unpause_message (src->session, src->msg);
756 gst_soup_http_src_session_pause_message (GstSoupHTTPSrc * src)
758 soup_session_pause_message (src->session, src->msg);
762 gst_soup_http_src_session_close (GstSoupHTTPSrc * src)
765 soup_session_abort (src->session); /* This unrefs the message. */
766 g_object_unref (src->session);
773 gst_soup_http_src_authenticate_cb (SoupSession * session, SoupMessage * msg,
774 SoupAuth * auth, gboolean retrying, GstSoupHTTPSrc * src)
777 /* First time authentication only, if we fail and are called again with retry true fall through */
778 if (msg->status_code == SOUP_STATUS_UNAUTHORIZED) {
779 if (src->user_id && src->user_pw)
780 soup_auth_authenticate (auth, src->user_id, src->user_pw);
781 } else if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
782 if (src->proxy_id && src->proxy_pw)
783 soup_auth_authenticate (auth, src->proxy_id, src->proxy_pw);
789 gst_soup_http_src_headers_foreach (const gchar * name, const gchar * val,
792 GST_DEBUG_OBJECT (src, " %s: %s", name, val);
796 gst_soup_http_src_got_headers_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
799 GstTagList *tag_list;
803 goffset start = 0, end = 0, total_length = 0;
805 GHashTable *params = NULL;
806 #ifdef GST_EXT_SOUP_MODIFICATION
808 const char* blackTypes[] = {"application/xml", "text/html"};
811 GST_DEBUG_OBJECT (src, "got headers:");
812 soup_message_headers_foreach (msg->response_headers,
813 gst_soup_http_src_headers_foreach, src);
815 if (msg->status_code == 407 && src->proxy_id && src->proxy_pw)
818 if (src->automatic_redirect && SOUP_STATUS_IS_REDIRECTION (msg->status_code)) {
819 #ifdef GST_EXT_SOUP_MODIFICATION
820 value = soup_message_headers_get (msg->response_headers, "Location");
821 gst_soup_http_src_set_location (src, value);
822 GST_DEBUG_OBJECT (src, "%u redirect to \"%s\"", msg->status_code, value);
824 GST_DEBUG_OBJECT (src, "%u redirect to \"%s\"", msg->status_code,
825 soup_message_headers_get (msg->response_headers, "Location"));
830 if (msg->status_code == SOUP_STATUS_UNAUTHORIZED)
833 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING;
835 /* Parse Content-Length. */
836 if (soup_message_headers_get_encoding (msg->response_headers) ==
837 SOUP_ENCODING_CONTENT_LENGTH) {
838 newsize = src->request_position +
839 soup_message_headers_get_content_length (msg->response_headers);
840 if (!src->have_size || (src->content_size != newsize)) {
841 src->content_size = newsize;
844 src->file_size = newsize;
846 src->have_size = TRUE;
847 src->seekable = TRUE;
848 GST_DEBUG_OBJECT (src, "size = %" G_GUINT64_FORMAT, src->content_size);
850 basesrc = GST_BASE_SRC_CAST (src);
851 gst_segment_set_duration (&basesrc->segment, GST_FORMAT_BYTES,
853 gst_element_post_message (GST_ELEMENT (src),
854 gst_message_new_duration (GST_OBJECT (src), GST_FORMAT_BYTES,
858 soup_message_headers_get_content_range(msg->response_headers, &start, &end, &total_length);
861 src->file_size = total_length;
862 GST_DEBUG_OBJECT (src, "size = %" G_GUINT64_FORMAT, src->file_size);
863 basesrc = GST_BASE_SRC_CAST (src);
864 gst_segment_set_duration (&basesrc->segment, GST_FORMAT_BYTES,
866 gst_element_post_message (GST_ELEMENT (src),
867 gst_message_new_duration (GST_OBJECT (src), GST_FORMAT_BYTES,
874 tag_list = gst_tag_list_new ();
877 soup_message_headers_get (msg->response_headers,
878 "icy-metaint")) != NULL) {
879 gint icy_metaint = atoi (value);
881 GST_DEBUG_OBJECT (src, "icy-metaint: %s (parsed: %d)", value, icy_metaint);
882 if (icy_metaint > 0) {
884 gst_caps_unref (src->src_caps);
886 src->src_caps = gst_caps_new_simple ("application/x-icy",
887 "metadata-interval", G_TYPE_INT, icy_metaint, NULL);
891 soup_message_headers_get_content_type (msg->response_headers,
893 GST_DEBUG_OBJECT (src, "Content-Type: %s", value);
895 #ifdef GST_EXT_SOUP_MODIFICATION
896 for (idx = 0; idx < (sizeof(blackTypes) / sizeof(char *)); idx++) {
897 if (!g_ascii_strcasecmp(value, blackTypes[idx])) {
898 GST_DEBUG_OBJECT (src, "blackType: %s", blackTypes[idx]);
899 GST_ELEMENT_ERROR(src, STREAM, WRONG_TYPE, (0), (0));
900 src->ret = GST_FLOW_ERROR;
905 if (g_ascii_strcasecmp (value, "audio/L16") == 0) {
911 gst_caps_unref (src->src_caps);
913 param = g_hash_table_lookup (params, "channels");
915 channels = atol (param);
917 param = g_hash_table_lookup (params, "rate");
921 src->src_caps = gst_caps_new_simple ("audio/x-raw-int",
922 "channels", G_TYPE_INT, channels,
923 "rate", G_TYPE_INT, rate,
924 "width", G_TYPE_INT, 16,
925 "depth", G_TYPE_INT, 16,
926 "signed", G_TYPE_BOOLEAN, TRUE,
927 "endianness", G_TYPE_INT, G_BIG_ENDIAN, NULL);
929 /* Set the Content-Type field on the caps */
931 gst_caps_set_simple (src->src_caps, "content-type", G_TYPE_STRING,
937 g_hash_table_destroy (params);
940 soup_message_headers_get (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 g_object_notify (G_OBJECT (src), "iradio-name");
946 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_ORGANIZATION,
947 src->iradio_name, NULL);
951 soup_message_headers_get (msg->response_headers,
952 "icy-genre")) != NULL) {
953 g_free (src->iradio_genre);
954 src->iradio_genre = gst_soup_http_src_unicodify (value);
955 if (src->iradio_genre) {
956 g_object_notify (G_OBJECT (src), "iradio-genre");
957 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_GENRE,
958 src->iradio_genre, NULL);
961 if ((value = soup_message_headers_get (msg->response_headers, "icy-url"))
963 g_free (src->iradio_url);
964 src->iradio_url = gst_soup_http_src_unicodify (value);
965 if (src->iradio_url) {
966 g_object_notify (G_OBJECT (src), "iradio-url");
967 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_LOCATION,
968 src->iradio_url, NULL);
971 if (!gst_tag_list_is_empty (tag_list)) {
972 GST_DEBUG_OBJECT (src,
973 "calling gst_element_found_tags with %" GST_PTR_FORMAT, tag_list);
974 gst_element_found_tags (GST_ELEMENT_CAST (src), tag_list);
976 gst_tag_list_free (tag_list);
979 /* Handle HTTP errors. */
980 gst_soup_http_src_parse_status (msg, src);
982 /* Check if Range header was respected. */
983 if (src->ret == GST_FLOW_CUSTOM_ERROR &&
984 src->read_position && msg->status_code != SOUP_STATUS_PARTIAL_CONTENT) {
985 src->seekable = FALSE;
986 GST_ELEMENT_ERROR (src, RESOURCE, SEEK,
987 (_("Server does not support seeking.")),
988 ("Server does not accept Range HTTP header, URL: %s", src->location));
989 src->ret = GST_FLOW_ERROR;
993 /* Have body. Signal EOS. */
995 gst_soup_http_src_got_body_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
997 if (G_UNLIKELY (msg != src->msg)) {
998 GST_DEBUG_OBJECT (src, "got body, but not for current message");
1001 if (G_UNLIKELY (src->session_io_status !=
1002 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
1003 /* Probably a redirect. */
1006 GST_DEBUG_OBJECT (src, "got body");
1007 src->ret = GST_FLOW_UNEXPECTED;
1009 g_main_loop_quit (src->loop);
1010 gst_soup_http_src_session_pause_message (src);
1013 /* Finished. Signal EOS. */
1015 gst_soup_http_src_finished_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
1017 if (G_UNLIKELY (msg != src->msg)) {
1018 GST_DEBUG_OBJECT (src, "finished, but not for current message");
1021 GST_DEBUG_OBJECT (src, "finished");
1022 src->ret = GST_FLOW_UNEXPECTED;
1023 if (src->session_io_status == GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_CANCELLED) {
1024 /* gst_soup_http_src_cancel_message() triggered this; probably a seek
1025 * that occurred in the QUEUEING state; i.e. before the connection setup
1026 * was complete. Do nothing */
1027 } else if (src->session_io_status ==
1028 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING && src->read_position > 0) {
1029 /* The server disconnected while streaming. Reconnect and seeking to the
1032 src->ret = GST_FLOW_CUSTOM_ERROR;
1033 } else if (G_UNLIKELY (src->session_io_status !=
1034 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
1035 /* FIXME: reason_phrase is not translated, add proper error message */
1036 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
1037 ("%s", msg->reason_phrase),
1038 ("libsoup status code %d", msg->status_code));
1041 g_main_loop_quit (src->loop);
1044 /* Buffer lifecycle management.
1046 * gst_soup_http_src_create() runs the GMainLoop for this element, to let
1047 * Soup take control.
1048 * A GstBuffer is allocated in gst_soup_http_src_chunk_allocator() and
1049 * associated with a SoupBuffer.
1050 * Soup reads HTTP data in the GstBuffer's data buffer.
1051 * The gst_soup_http_src_got_chunk_cb() is then called with the SoupBuffer.
1052 * That sets gst_soup_http_src_create()'s return argument to the GstBuffer,
1053 * increments its refcount (to 2), pauses the flow of data from the HTTP
1054 * source to prevent gst_soup_http_src_got_chunk_cb() from being called
1055 * again and breaks out of the GMainLoop.
1056 * Because the SOUP_MESSAGE_OVERWRITE_CHUNKS flag is set, Soup frees the
1057 * SoupBuffer and calls gst_soup_http_src_chunk_free(), which decrements the
1059 * gst_soup_http_src_create() returns the GstBuffer. It will be freed by a
1060 * downstream element.
1061 * If Soup fails to read HTTP data, it does not call
1062 * gst_soup_http_src_got_chunk_cb(), but still frees the SoupBuffer and
1063 * calls gst_soup_http_src_chunk_free(), which decrements the GstBuffer's
1064 * refcount to 0, freeing it.
1068 gst_soup_http_src_chunk_free (gpointer gstbuf)
1070 gst_buffer_unref (GST_BUFFER_CAST (gstbuf));
1074 gst_soup_http_src_chunk_allocator (SoupMessage * msg, gsize max_len,
1077 GstSoupHTTPSrc *src = (GstSoupHTTPSrc *) user_data;
1078 GstBaseSrc *basesrc = GST_BASE_SRC_CAST (src);
1080 SoupBuffer *soupbuf;
1085 length = MIN (basesrc->blocksize, max_len);
1087 length = basesrc->blocksize;
1088 GST_DEBUG_OBJECT (src, "alloc %" G_GSIZE_FORMAT " bytes <= %" G_GSIZE_FORMAT,
1092 rc = gst_pad_alloc_buffer (GST_BASE_SRC_PAD (basesrc),
1093 GST_BUFFER_OFFSET_NONE, length,
1094 src->src_caps ? src->src_caps :
1095 GST_PAD_CAPS (GST_BASE_SRC_PAD (basesrc)), &gstbuf);
1096 if (G_UNLIKELY (rc != GST_FLOW_OK)) {
1097 /* Failed to allocate buffer. Stall SoupSession and return error code
1100 g_main_loop_quit (src->loop);
1104 soupbuf = soup_buffer_new_with_owner (GST_BUFFER_DATA (gstbuf), length,
1105 gstbuf, gst_soup_http_src_chunk_free);
1111 gst_soup_http_src_got_chunk_cb (SoupMessage * msg, SoupBuffer * chunk,
1112 GstSoupHTTPSrc * src)
1114 GstBaseSrc *basesrc;
1115 guint64 new_position;
1117 if (G_UNLIKELY (msg != src->msg)) {
1118 GST_DEBUG_OBJECT (src, "got chunk, but not for current message");
1121 if (G_UNLIKELY (src->session_io_status !=
1122 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
1123 /* Probably a redirect. */
1126 basesrc = GST_BASE_SRC_CAST (src);
1127 GST_DEBUG_OBJECT (src, "got chunk of %" G_GSIZE_FORMAT " bytes",
1130 /* Extract the GstBuffer from the SoupBuffer and set its fields. */
1131 *src->outbuf = GST_BUFFER_CAST (soup_buffer_get_owner (chunk));
1133 GST_BUFFER_SIZE (*src->outbuf) = chunk->length;
1134 GST_BUFFER_OFFSET (*src->outbuf) = basesrc->segment.last_stop;
1136 gst_buffer_set_caps (*src->outbuf,
1137 (src->src_caps) ? src->src_caps :
1138 GST_PAD_CAPS (GST_BASE_SRC_PAD (basesrc)));
1140 gst_buffer_ref (*src->outbuf);
1142 new_position = src->read_position + chunk->length;
1143 if (G_LIKELY (src->request_position == src->read_position))
1144 src->request_position = new_position;
1145 src->read_position = new_position;
1147 src->ret = GST_FLOW_OK;
1148 g_main_loop_quit (src->loop);
1149 gst_soup_http_src_session_pause_message (src);
1153 gst_soup_http_src_response_cb (SoupSession * session, SoupMessage * msg,
1154 GstSoupHTTPSrc * src)
1156 if (G_UNLIKELY (msg != src->msg)) {
1157 GST_DEBUG_OBJECT (src, "got response %d: %s, but not for current message",
1158 msg->status_code, msg->reason_phrase);
1161 if (G_UNLIKELY (src->session_io_status !=
1162 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)
1163 && SOUP_STATUS_IS_REDIRECTION (msg->status_code)) {
1164 /* Ignore redirections. */
1167 GST_DEBUG_OBJECT (src, "got response %d: %s", msg->status_code,
1168 msg->reason_phrase);
1169 if (src->session_io_status == GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING &&
1170 src->read_position > 0) {
1171 /* The server disconnected while streaming. Reconnect and seeking to the
1175 gst_soup_http_src_parse_status (msg, src);
1176 /* The session's SoupMessage object expires after this callback returns. */
1178 g_main_loop_quit (src->loop);
1181 #define SOUP_HTTP_SRC_ERROR(src,soup_msg,cat,code,error_message) \
1182 GST_ELEMENT_ERROR ((src), cat, code, ("%s", error_message), \
1183 ("%s (%d), URL: %s", (soup_msg)->reason_phrase, \
1184 (soup_msg)->status_code, (src)->location));
1187 gst_soup_http_src_parse_status (SoupMessage * msg, GstSoupHTTPSrc * src)
1189 if (SOUP_STATUS_IS_TRANSPORT_ERROR (msg->status_code)) {
1190 switch (msg->status_code) {
1191 case SOUP_STATUS_CANT_RESOLVE:
1192 case SOUP_STATUS_CANT_RESOLVE_PROXY:
1193 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, NOT_FOUND,
1194 _("Could not resolve server name."));
1195 src->ret = GST_FLOW_ERROR;
1197 case SOUP_STATUS_CANT_CONNECT:
1198 case SOUP_STATUS_CANT_CONNECT_PROXY:
1199 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ,
1200 _("Could not establish connection to server."));
1201 src->ret = GST_FLOW_ERROR;
1203 case SOUP_STATUS_SSL_FAILED:
1204 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ,
1205 _("Secure connection setup failed."));
1206 src->ret = GST_FLOW_ERROR;
1208 case SOUP_STATUS_IO_ERROR:
1209 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, READ,
1210 _("A network error occured, or the server closed the connection "
1212 src->ret = GST_FLOW_ERROR;
1214 case SOUP_STATUS_MALFORMED:
1215 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, READ,
1216 _("Server sent bad data."));
1217 src->ret = GST_FLOW_ERROR;
1219 case SOUP_STATUS_CANCELLED:
1220 /* No error message when interrupted by program. */
1223 } else if (SOUP_STATUS_IS_CLIENT_ERROR (msg->status_code) ||
1224 SOUP_STATUS_IS_REDIRECTION (msg->status_code) ||
1225 SOUP_STATUS_IS_SERVER_ERROR (msg->status_code)) {
1226 /* Report HTTP error. */
1227 /* FIXME: reason_phrase is not translated and not suitable for user
1228 * error dialog according to libsoup documentation.
1229 * FIXME: error code (OPEN_READ vs. READ) should depend on http status? */
1230 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1231 ("%s", msg->reason_phrase),
1232 ("%s (%d), URL: %s", msg->reason_phrase, msg->status_code,
1234 src->ret = GST_FLOW_ERROR;
1239 gst_soup_http_src_build_message (GstSoupHTTPSrc * src)
1241 src->msg = soup_message_new (SOUP_METHOD_GET, src->location);
1243 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1244 ("Error parsing URL."), ("URL: %s", src->location));
1247 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE;
1248 soup_message_headers_append (src->msg->request_headers, "Connection",
1250 if (src->iradio_mode) {
1251 soup_message_headers_append (src->msg->request_headers, "icy-metadata",
1256 #ifdef GST_EXT_SOUP_MODIFICATION
1258 SoupCookie *cookie_parsed;
1261 uri = soup_uri_new (src->location);
1262 for (cookie = src->cookies; *cookie != NULL; cookie++) {
1263 if ((cookie_parsed = soup_cookie_parse (*cookie, uri)) != NULL) {
1264 header = soup_cookie_to_cookie_header (cookie_parsed);
1265 soup_message_headers_append (src->msg->request_headers, "Cookie",
1268 soup_cookie_free (cookie_parsed);
1271 soup_uri_free (uri);
1273 for (cookie = src->cookies; *cookie != NULL; cookie++) {
1274 soup_message_headers_append (src->msg->request_headers, "Cookie",
1279 soup_message_headers_append (src->msg->request_headers,
1280 "transferMode.dlna.org", "Streaming");
1283 g_signal_connect (src->msg, "got_headers",
1284 G_CALLBACK (gst_soup_http_src_got_headers_cb), src);
1285 g_signal_connect (src->msg, "got_body",
1286 G_CALLBACK (gst_soup_http_src_got_body_cb), src);
1287 g_signal_connect (src->msg, "finished",
1288 G_CALLBACK (gst_soup_http_src_finished_cb), src);
1289 g_signal_connect (src->msg, "got_chunk",
1290 G_CALLBACK (gst_soup_http_src_got_chunk_cb), src);
1291 soup_message_set_flags (src->msg, SOUP_MESSAGE_OVERWRITE_CHUNKS |
1292 (src->automatic_redirect ? 0 : SOUP_MESSAGE_NO_REDIRECT));
1293 soup_message_set_chunk_allocator (src->msg,
1294 gst_soup_http_src_chunk_allocator, src, NULL);
1296 //gst_soup_http_src_add_range_header (src, src->request_position);
1297 if(src->range_size > 0)
1298 soup_message_headers_set_range(src->msg->request_headers, src->request_position, (src->request_position+src->range_size-1));
1300 gst_soup_http_src_add_range_header (src, src->request_position);
1301 gst_soup_http_src_add_extra_headers (src);
1302 GST_DEBUG_OBJECT (src, "request headers:");
1303 soup_message_headers_foreach (src->msg->request_headers,gst_soup_http_src_headers_foreach, src);
1306 gst_soup_http_src_add_range_header (src, src->request_position);
1308 gst_soup_http_src_add_extra_headers (src);
1310 GST_DEBUG_OBJECT (src, "request headers:");
1311 soup_message_headers_foreach (src->msg->request_headers,
1312 gst_soup_http_src_headers_foreach, src);
1318 static GstFlowReturn
1319 gst_soup_http_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
1321 GstSoupHTTPSrc *src;
1323 src = GST_SOUP_HTTP_SRC (psrc);
1325 if (src->msg && (src->request_position != src->read_position)) {
1327 if (src->file_size != 0 && src->request_position >= src->file_size) {
1329 if (src->content_size != 0 && src->request_position >= src->content_size) {
1331 GST_WARNING_OBJECT (src, "Seeking behind the end of file -- EOS");
1332 return GST_FLOW_UNEXPECTED;
1333 } else if (src->session_io_status ==
1334 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE) {
1335 gst_soup_http_src_add_range_header (src, src->request_position);
1337 GST_DEBUG_OBJECT (src, "Seek from position %" G_GUINT64_FORMAT
1338 " to %" G_GUINT64_FORMAT ": requeueing connection request",
1339 src->read_position, src->request_position);
1340 #ifndef SEEK_CHANGES
1341 gst_soup_http_src_cancel_message (src);
1346 if(src->msg && src->seeked) {
1347 GST_DEBUG_OBJECT (src, "seeking to offset start %llu end %llu", src->request_position, (src->request_position+src->range_size-1));
1349 soup_session_cancel_message (src->session, src->msg, SOUP_STATUS_OK);
1351 if (!gst_soup_http_src_build_message (src))
1352 return GST_FLOW_ERROR;
1354 soup_session_queue_message (src->session, src->msg, (SoupSessionCallback) gst_soup_http_src_response_cb, src);
1355 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_QUEUED;
1356 src->read_position = src->request_position;
1357 if(src->range_size > 0) src->content_size = src->request_position+src->range_size-1;
1358 else src->content_size = src->file_size;
1360 src->seeked = FALSE;
1363 if (!gst_soup_http_src_build_message (src))
1364 return GST_FLOW_ERROR;
1366 src->ret = GST_FLOW_CUSTOM_ERROR;
1367 src->outbuf = outbuf;
1369 if (src->interrupted) {
1370 GST_DEBUG_OBJECT (src, "interrupted");
1374 GST_DEBUG_OBJECT (src, "Reconnecting");
1375 if (!gst_soup_http_src_build_message (src))
1376 return GST_FLOW_ERROR;
1381 GST_DEBUG_OBJECT (src, "EOS reached");
1385 switch (src->session_io_status) {
1386 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE:
1387 GST_DEBUG_OBJECT (src, "Queueing connection request");
1388 gst_soup_http_src_queue_message (src);
1390 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_QUEUED:
1392 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING:
1393 gst_soup_http_src_session_unpause_message (src);
1395 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_CANCELLED:
1400 if (src->ret == GST_FLOW_CUSTOM_ERROR)
1401 g_main_loop_run (src->loop);
1402 } while (src->ret == GST_FLOW_CUSTOM_ERROR);
1404 if (src->ret == GST_FLOW_CUSTOM_ERROR)
1405 src->ret = GST_FLOW_UNEXPECTED;
1410 gst_soup_http_src_start (GstBaseSrc * bsrc)
1412 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1414 GST_DEBUG_OBJECT (src, "start(\"%s\")", src->location);
1416 if (!src->location) {
1417 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (_("No URL set.")),
1418 ("Missing location property"));
1422 src->context = g_main_context_new ();
1424 src->loop = g_main_loop_new (src->context, TRUE);
1426 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
1427 (NULL), ("Failed to start GMainLoop"));
1428 g_main_context_unref (src->context);
1432 if (src->proxy == NULL) {
1434 soup_session_async_new_with_options (SOUP_SESSION_ASYNC_CONTEXT,
1435 src->context, SOUP_SESSION_USER_AGENT, src->user_agent,
1436 SOUP_SESSION_TIMEOUT, src->timeout,
1437 #ifdef HAVE_LIBSOUP_GNOME
1438 SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_PROXY_RESOLVER_GNOME,
1443 soup_session_async_new_with_options (SOUP_SESSION_ASYNC_CONTEXT,
1444 src->context, SOUP_SESSION_PROXY_URI, src->proxy,
1445 SOUP_SESSION_TIMEOUT, src->timeout,
1446 SOUP_SESSION_USER_AGENT, src->user_agent, NULL);
1449 if (!src->session) {
1450 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
1451 (NULL), ("Failed to create async session"));
1455 #ifdef GST_EXT_SOUP_MODIFICATION
1456 soup_session_add_feature_by_type (src->session, SOUP_TYPE_COOKIE_JAR);
1457 src->cookie_jar = SOUP_COOKIE_JAR (soup_session_get_feature (src->session, SOUP_TYPE_COOKIE_JAR));
1460 g_signal_connect (src->session, "authenticate",
1461 G_CALLBACK (gst_soup_http_src_authenticate_cb), src);
1466 gst_soup_http_src_stop (GstBaseSrc * bsrc)
1468 GstSoupHTTPSrc *src;
1470 src = GST_SOUP_HTTP_SRC (bsrc);
1471 GST_DEBUG_OBJECT (src, "stop()");
1472 gst_soup_http_src_session_close (src);
1474 g_main_loop_unref (src->loop);
1475 g_main_context_unref (src->context);
1477 src->context = NULL;
1479 if (src->extra_headers) {
1480 gst_structure_free (src->extra_headers);
1481 src->extra_headers = NULL;
1484 gst_soup_http_src_reset (src);
1488 /* Interrupt a blocking request. */
1490 gst_soup_http_src_unlock (GstBaseSrc * bsrc)
1492 GstSoupHTTPSrc *src;
1494 src = GST_SOUP_HTTP_SRC (bsrc);
1495 GST_DEBUG_OBJECT (src, "unlock()");
1497 src->interrupted = TRUE;
1499 g_main_loop_quit (src->loop);
1503 /* Interrupt interrupt. */
1505 gst_soup_http_src_unlock_stop (GstBaseSrc * bsrc)
1507 GstSoupHTTPSrc *src;
1509 src = GST_SOUP_HTTP_SRC (bsrc);
1510 GST_DEBUG_OBJECT (src, "unlock_stop()");
1512 src->interrupted = FALSE;
1517 gst_soup_http_src_get_size (GstBaseSrc * bsrc, guint64 * size)
1519 GstSoupHTTPSrc *src;
1521 src = GST_SOUP_HTTP_SRC (bsrc);
1523 if (src->have_size) {
1525 GST_DEBUG_OBJECT (src, "get_size() = %" G_GUINT64_FORMAT,
1527 *size = src->file_size;
1529 GST_DEBUG_OBJECT (src, "get_size() = %" G_GUINT64_FORMAT,
1531 *size = src->content_size;
1535 GST_DEBUG_OBJECT (src, "get_size() = FALSE");
1540 gst_soup_http_src_is_seekable (GstBaseSrc * bsrc)
1542 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1544 return src->seekable;
1548 gst_soup_http_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
1550 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1552 GST_DEBUG_OBJECT (src, "do_seek(%" G_GUINT64_FORMAT ")", segment->start);
1557 if (src->read_position == segment->start) {
1558 GST_DEBUG_OBJECT (src, "Seeking to current read position");
1562 if (!src->seekable) {
1563 GST_WARNING_OBJECT (src, "Not seekable");
1567 if (segment->rate < 0.0 || segment->format != GST_FORMAT_BYTES) {
1568 GST_WARNING_OBJECT (src, "Invalid seek segment");
1573 if (src->content_size != 0 && segment->start >= src->file_size) {
1575 if (src->content_size != 0 && segment->start >= src->content_size) {
1577 GST_WARNING_OBJECT (src, "Seeking behind end of file, will go to EOS soon");
1580 /* Wait for create() to handle the jump in offset. */
1581 src->request_position = segment->start;
1586 gst_soup_http_src_query (GstBaseSrc * bsrc, GstQuery * query)
1588 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1591 switch (GST_QUERY_TYPE (query)) {
1593 gst_query_set_uri (query, src->location);
1602 ret = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
1608 gst_soup_http_src_set_location (GstSoupHTTPSrc * src, const gchar * uri)
1610 if (src->location) {
1611 g_free (src->location);
1612 src->location = NULL;
1614 src->location = g_strdup (uri);
1620 gst_soup_http_src_set_proxy (GstSoupHTTPSrc * src, const gchar * uri)
1623 soup_uri_free (src->proxy);
1626 if (g_str_has_prefix (uri, "http://")) {
1627 src->proxy = soup_uri_new (uri);
1629 gchar *new_uri = g_strconcat ("http://", uri, NULL);
1631 src->proxy = soup_uri_new (new_uri);
1639 gst_soup_http_src_uri_get_type (void)
1645 gst_soup_http_src_uri_get_protocols (void)
1647 static const gchar *protocols[] = { "http", "https", NULL };
1648 return (gchar **) protocols;
1651 static const gchar *
1652 gst_soup_http_src_uri_get_uri (GstURIHandler * handler)
1654 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (handler);
1656 return src->location;
1660 gst_soup_http_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1662 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (handler);
1664 return gst_soup_http_src_set_location (src, uri);
1668 gst_soup_http_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1670 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1672 iface->get_type = gst_soup_http_src_uri_get_type;
1673 iface->get_protocols = gst_soup_http_src_uri_get_protocols;
1674 iface->get_uri = gst_soup_http_src_uri_get_uri;
1675 iface->set_uri = gst_soup_http_src_uri_set_uri;