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;
807 GST_DEBUG_OBJECT (src, "got headers:");
808 soup_message_headers_foreach (msg->response_headers,
809 gst_soup_http_src_headers_foreach, src);
811 if (msg->status_code == 407 && src->proxy_id && src->proxy_pw)
814 if (src->automatic_redirect && SOUP_STATUS_IS_REDIRECTION (msg->status_code)) {
815 #ifdef GST_EXT_SOUP_MODIFICATION
816 value = soup_message_headers_get (msg->response_headers, "Location");
817 gst_soup_http_src_set_location (src, value);
818 GST_DEBUG_OBJECT (src, "%u redirect to \"%s\"", msg->status_code, value);
820 GST_DEBUG_OBJECT (src, "%u redirect to \"%s\"", msg->status_code,
821 soup_message_headers_get (msg->response_headers, "Location"));
826 if (msg->status_code == SOUP_STATUS_UNAUTHORIZED)
829 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING;
831 /* Parse Content-Length. */
832 if (soup_message_headers_get_encoding (msg->response_headers) ==
833 SOUP_ENCODING_CONTENT_LENGTH) {
834 newsize = src->request_position +
835 soup_message_headers_get_content_length (msg->response_headers);
836 if (!src->have_size || (src->content_size != newsize)) {
837 src->content_size = newsize;
840 src->file_size = newsize;
842 src->have_size = TRUE;
843 src->seekable = TRUE;
844 GST_DEBUG_OBJECT (src, "size = %" G_GUINT64_FORMAT, src->content_size);
846 basesrc = GST_BASE_SRC_CAST (src);
847 gst_segment_set_duration (&basesrc->segment, GST_FORMAT_BYTES,
849 gst_element_post_message (GST_ELEMENT (src),
850 gst_message_new_duration (GST_OBJECT (src), GST_FORMAT_BYTES,
854 soup_message_headers_get_content_range(msg->response_headers, &start, &end, &total_length);
857 src->file_size = total_length;
858 GST_DEBUG_OBJECT (src, "size = %" G_GUINT64_FORMAT, src->file_size);
859 basesrc = GST_BASE_SRC_CAST (src);
860 gst_segment_set_duration (&basesrc->segment, GST_FORMAT_BYTES,
862 gst_element_post_message (GST_ELEMENT (src),
863 gst_message_new_duration (GST_OBJECT (src), GST_FORMAT_BYTES,
870 tag_list = gst_tag_list_new ();
873 soup_message_headers_get (msg->response_headers,
874 "icy-metaint")) != NULL) {
875 gint icy_metaint = atoi (value);
877 GST_DEBUG_OBJECT (src, "icy-metaint: %s (parsed: %d)", value, icy_metaint);
878 if (icy_metaint > 0) {
880 gst_caps_unref (src->src_caps);
882 src->src_caps = gst_caps_new_simple ("application/x-icy",
883 "metadata-interval", G_TYPE_INT, icy_metaint, NULL);
887 soup_message_headers_get_content_type (msg->response_headers,
889 GST_DEBUG_OBJECT (src, "Content-Type: %s", value);
890 if (g_ascii_strcasecmp (value, "audio/L16") == 0) {
896 gst_caps_unref (src->src_caps);
898 param = g_hash_table_lookup (params, "channels");
900 channels = atol (param);
902 param = g_hash_table_lookup (params, "rate");
906 src->src_caps = gst_caps_new_simple ("audio/x-raw-int",
907 "channels", G_TYPE_INT, channels,
908 "rate", G_TYPE_INT, rate,
909 "width", G_TYPE_INT, 16,
910 "depth", G_TYPE_INT, 16,
911 "signed", G_TYPE_BOOLEAN, TRUE,
912 "endianness", G_TYPE_INT, G_BIG_ENDIAN, NULL);
914 /* Set the Content-Type field on the caps */
916 gst_caps_set_simple (src->src_caps, "content-type", G_TYPE_STRING,
922 g_hash_table_destroy (params);
925 soup_message_headers_get (msg->response_headers,
926 "icy-name")) != NULL) {
927 g_free (src->iradio_name);
928 src->iradio_name = gst_soup_http_src_unicodify (value);
929 if (src->iradio_name) {
930 g_object_notify (G_OBJECT (src), "iradio-name");
931 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_ORGANIZATION,
932 src->iradio_name, NULL);
936 soup_message_headers_get (msg->response_headers,
937 "icy-genre")) != NULL) {
938 g_free (src->iradio_genre);
939 src->iradio_genre = gst_soup_http_src_unicodify (value);
940 if (src->iradio_genre) {
941 g_object_notify (G_OBJECT (src), "iradio-genre");
942 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_GENRE,
943 src->iradio_genre, NULL);
946 if ((value = soup_message_headers_get (msg->response_headers, "icy-url"))
948 g_free (src->iradio_url);
949 src->iradio_url = gst_soup_http_src_unicodify (value);
950 if (src->iradio_url) {
951 g_object_notify (G_OBJECT (src), "iradio-url");
952 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_LOCATION,
953 src->iradio_url, NULL);
956 if (!gst_tag_list_is_empty (tag_list)) {
957 GST_DEBUG_OBJECT (src,
958 "calling gst_element_found_tags with %" GST_PTR_FORMAT, tag_list);
959 gst_element_found_tags (GST_ELEMENT_CAST (src), tag_list);
961 gst_tag_list_free (tag_list);
964 /* Handle HTTP errors. */
965 gst_soup_http_src_parse_status (msg, src);
967 /* Check if Range header was respected. */
968 if (src->ret == GST_FLOW_CUSTOM_ERROR &&
969 src->read_position && msg->status_code != SOUP_STATUS_PARTIAL_CONTENT) {
970 src->seekable = FALSE;
971 GST_ELEMENT_ERROR (src, RESOURCE, SEEK,
972 (_("Server does not support seeking.")),
973 ("Server does not accept Range HTTP header, URL: %s", src->location));
974 src->ret = GST_FLOW_ERROR;
978 /* Have body. Signal EOS. */
980 gst_soup_http_src_got_body_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
982 if (G_UNLIKELY (msg != src->msg)) {
983 GST_DEBUG_OBJECT (src, "got body, but not for current message");
986 if (G_UNLIKELY (src->session_io_status !=
987 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
988 /* Probably a redirect. */
991 GST_DEBUG_OBJECT (src, "got body");
992 src->ret = GST_FLOW_UNEXPECTED;
994 g_main_loop_quit (src->loop);
995 gst_soup_http_src_session_pause_message (src);
998 /* Finished. Signal EOS. */
1000 gst_soup_http_src_finished_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
1002 if (G_UNLIKELY (msg != src->msg)) {
1003 GST_DEBUG_OBJECT (src, "finished, but not for current message");
1006 GST_DEBUG_OBJECT (src, "finished");
1007 src->ret = GST_FLOW_UNEXPECTED;
1008 if (src->session_io_status == GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_CANCELLED) {
1009 /* gst_soup_http_src_cancel_message() triggered this; probably a seek
1010 * that occurred in the QUEUEING state; i.e. before the connection setup
1011 * was complete. Do nothing */
1012 } else if (src->session_io_status ==
1013 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING && src->read_position > 0) {
1014 /* The server disconnected while streaming. Reconnect and seeking to the
1017 src->ret = GST_FLOW_CUSTOM_ERROR;
1018 } else if (G_UNLIKELY (src->session_io_status !=
1019 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
1020 /* FIXME: reason_phrase is not translated, add proper error message */
1021 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
1022 ("%s", msg->reason_phrase),
1023 ("libsoup status code %d", msg->status_code));
1026 g_main_loop_quit (src->loop);
1029 /* Buffer lifecycle management.
1031 * gst_soup_http_src_create() runs the GMainLoop for this element, to let
1032 * Soup take control.
1033 * A GstBuffer is allocated in gst_soup_http_src_chunk_allocator() and
1034 * associated with a SoupBuffer.
1035 * Soup reads HTTP data in the GstBuffer's data buffer.
1036 * The gst_soup_http_src_got_chunk_cb() is then called with the SoupBuffer.
1037 * That sets gst_soup_http_src_create()'s return argument to the GstBuffer,
1038 * increments its refcount (to 2), pauses the flow of data from the HTTP
1039 * source to prevent gst_soup_http_src_got_chunk_cb() from being called
1040 * again and breaks out of the GMainLoop.
1041 * Because the SOUP_MESSAGE_OVERWRITE_CHUNKS flag is set, Soup frees the
1042 * SoupBuffer and calls gst_soup_http_src_chunk_free(), which decrements the
1044 * gst_soup_http_src_create() returns the GstBuffer. It will be freed by a
1045 * downstream element.
1046 * If Soup fails to read HTTP data, it does not call
1047 * gst_soup_http_src_got_chunk_cb(), but still frees the SoupBuffer and
1048 * calls gst_soup_http_src_chunk_free(), which decrements the GstBuffer's
1049 * refcount to 0, freeing it.
1053 gst_soup_http_src_chunk_free (gpointer gstbuf)
1055 gst_buffer_unref (GST_BUFFER_CAST (gstbuf));
1059 gst_soup_http_src_chunk_allocator (SoupMessage * msg, gsize max_len,
1062 GstSoupHTTPSrc *src = (GstSoupHTTPSrc *) user_data;
1063 GstBaseSrc *basesrc = GST_BASE_SRC_CAST (src);
1065 SoupBuffer *soupbuf;
1070 length = MIN (basesrc->blocksize, max_len);
1072 length = basesrc->blocksize;
1073 GST_DEBUG_OBJECT (src, "alloc %" G_GSIZE_FORMAT " bytes <= %" G_GSIZE_FORMAT,
1077 rc = gst_pad_alloc_buffer (GST_BASE_SRC_PAD (basesrc),
1078 GST_BUFFER_OFFSET_NONE, length,
1079 src->src_caps ? src->src_caps :
1080 GST_PAD_CAPS (GST_BASE_SRC_PAD (basesrc)), &gstbuf);
1081 if (G_UNLIKELY (rc != GST_FLOW_OK)) {
1082 /* Failed to allocate buffer. Stall SoupSession and return error code
1085 g_main_loop_quit (src->loop);
1089 soupbuf = soup_buffer_new_with_owner (GST_BUFFER_DATA (gstbuf), length,
1090 gstbuf, gst_soup_http_src_chunk_free);
1096 gst_soup_http_src_got_chunk_cb (SoupMessage * msg, SoupBuffer * chunk,
1097 GstSoupHTTPSrc * src)
1099 GstBaseSrc *basesrc;
1100 guint64 new_position;
1102 if (G_UNLIKELY (msg != src->msg)) {
1103 GST_DEBUG_OBJECT (src, "got chunk, but not for current message");
1106 if (G_UNLIKELY (src->session_io_status !=
1107 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)) {
1108 /* Probably a redirect. */
1111 basesrc = GST_BASE_SRC_CAST (src);
1112 GST_DEBUG_OBJECT (src, "got chunk of %" G_GSIZE_FORMAT " bytes",
1115 /* Extract the GstBuffer from the SoupBuffer and set its fields. */
1116 *src->outbuf = GST_BUFFER_CAST (soup_buffer_get_owner (chunk));
1118 GST_BUFFER_SIZE (*src->outbuf) = chunk->length;
1119 GST_BUFFER_OFFSET (*src->outbuf) = basesrc->segment.last_stop;
1121 gst_buffer_set_caps (*src->outbuf,
1122 (src->src_caps) ? src->src_caps :
1123 GST_PAD_CAPS (GST_BASE_SRC_PAD (basesrc)));
1125 gst_buffer_ref (*src->outbuf);
1127 new_position = src->read_position + chunk->length;
1128 if (G_LIKELY (src->request_position == src->read_position))
1129 src->request_position = new_position;
1130 src->read_position = new_position;
1132 src->ret = GST_FLOW_OK;
1133 g_main_loop_quit (src->loop);
1134 gst_soup_http_src_session_pause_message (src);
1138 gst_soup_http_src_response_cb (SoupSession * session, SoupMessage * msg,
1139 GstSoupHTTPSrc * src)
1141 if (G_UNLIKELY (msg != src->msg)) {
1142 GST_DEBUG_OBJECT (src, "got response %d: %s, but not for current message",
1143 msg->status_code, msg->reason_phrase);
1146 if (G_UNLIKELY (src->session_io_status !=
1147 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING)
1148 && SOUP_STATUS_IS_REDIRECTION (msg->status_code)) {
1149 /* Ignore redirections. */
1152 GST_DEBUG_OBJECT (src, "got response %d: %s", msg->status_code,
1153 msg->reason_phrase);
1154 if (src->session_io_status == GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING &&
1155 src->read_position > 0) {
1156 /* The server disconnected while streaming. Reconnect and seeking to the
1160 gst_soup_http_src_parse_status (msg, src);
1161 /* The session's SoupMessage object expires after this callback returns. */
1163 g_main_loop_quit (src->loop);
1166 #define SOUP_HTTP_SRC_ERROR(src,soup_msg,cat,code,error_message) \
1167 GST_ELEMENT_ERROR ((src), cat, code, ("%s", error_message), \
1168 ("%s (%d), URL: %s", (soup_msg)->reason_phrase, \
1169 (soup_msg)->status_code, (src)->location));
1172 gst_soup_http_src_parse_status (SoupMessage * msg, GstSoupHTTPSrc * src)
1174 if (SOUP_STATUS_IS_TRANSPORT_ERROR (msg->status_code)) {
1175 switch (msg->status_code) {
1176 case SOUP_STATUS_CANT_RESOLVE:
1177 case SOUP_STATUS_CANT_RESOLVE_PROXY:
1178 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, NOT_FOUND,
1179 _("Could not resolve server name."));
1180 src->ret = GST_FLOW_ERROR;
1182 case SOUP_STATUS_CANT_CONNECT:
1183 case SOUP_STATUS_CANT_CONNECT_PROXY:
1184 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ,
1185 _("Could not establish connection to server."));
1186 src->ret = GST_FLOW_ERROR;
1188 case SOUP_STATUS_SSL_FAILED:
1189 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ,
1190 _("Secure connection setup failed."));
1191 src->ret = GST_FLOW_ERROR;
1193 case SOUP_STATUS_IO_ERROR:
1194 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, READ,
1195 _("A network error occured, or the server closed the connection "
1197 src->ret = GST_FLOW_ERROR;
1199 case SOUP_STATUS_MALFORMED:
1200 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, READ,
1201 _("Server sent bad data."));
1202 src->ret = GST_FLOW_ERROR;
1204 case SOUP_STATUS_CANCELLED:
1205 /* No error message when interrupted by program. */
1208 } else if (SOUP_STATUS_IS_CLIENT_ERROR (msg->status_code) ||
1209 SOUP_STATUS_IS_REDIRECTION (msg->status_code) ||
1210 SOUP_STATUS_IS_SERVER_ERROR (msg->status_code)) {
1211 /* Report HTTP error. */
1212 /* FIXME: reason_phrase is not translated and not suitable for user
1213 * error dialog according to libsoup documentation.
1214 * FIXME: error code (OPEN_READ vs. READ) should depend on http status? */
1215 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1216 ("%s", msg->reason_phrase),
1217 ("%s (%d), URL: %s", msg->reason_phrase, msg->status_code,
1219 src->ret = GST_FLOW_ERROR;
1224 gst_soup_http_src_build_message (GstSoupHTTPSrc * src)
1226 src->msg = soup_message_new (SOUP_METHOD_GET, src->location);
1228 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1229 ("Error parsing URL."), ("URL: %s", src->location));
1232 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE;
1233 soup_message_headers_append (src->msg->request_headers, "Connection",
1235 if (src->iradio_mode) {
1236 soup_message_headers_append (src->msg->request_headers, "icy-metadata",
1241 #ifdef GST_EXT_SOUP_MODIFICATION
1243 SoupCookie *cookie_parsed;
1246 uri = soup_uri_new (src->location);
1247 for (cookie = src->cookies; *cookie != NULL; cookie++) {
1248 if ((cookie_parsed = soup_cookie_parse (*cookie, uri)) != NULL) {
1249 header = soup_cookie_to_cookie_header (cookie_parsed);
1250 soup_message_headers_append (src->msg->request_headers, "Cookie",
1253 soup_cookie_free (cookie_parsed);
1256 soup_uri_free (uri);
1258 for (cookie = src->cookies; *cookie != NULL; cookie++) {
1259 soup_message_headers_append (src->msg->request_headers, "Cookie",
1264 soup_message_headers_append (src->msg->request_headers,
1265 "transferMode.dlna.org", "Streaming");
1268 g_signal_connect (src->msg, "got_headers",
1269 G_CALLBACK (gst_soup_http_src_got_headers_cb), src);
1270 g_signal_connect (src->msg, "got_body",
1271 G_CALLBACK (gst_soup_http_src_got_body_cb), src);
1272 g_signal_connect (src->msg, "finished",
1273 G_CALLBACK (gst_soup_http_src_finished_cb), src);
1274 g_signal_connect (src->msg, "got_chunk",
1275 G_CALLBACK (gst_soup_http_src_got_chunk_cb), src);
1276 soup_message_set_flags (src->msg, SOUP_MESSAGE_OVERWRITE_CHUNKS |
1277 (src->automatic_redirect ? 0 : SOUP_MESSAGE_NO_REDIRECT));
1278 soup_message_set_chunk_allocator (src->msg,
1279 gst_soup_http_src_chunk_allocator, src, NULL);
1281 //gst_soup_http_src_add_range_header (src, src->request_position);
1282 if(src->range_size > 0)
1283 soup_message_headers_set_range(src->msg->request_headers, src->request_position, (src->request_position+src->range_size-1));
1285 gst_soup_http_src_add_range_header (src, src->request_position);
1286 gst_soup_http_src_add_extra_headers (src);
1287 GST_DEBUG_OBJECT (src, "request headers:");
1288 soup_message_headers_foreach (src->msg->request_headers,gst_soup_http_src_headers_foreach, src);
1291 gst_soup_http_src_add_range_header (src, src->request_position);
1293 gst_soup_http_src_add_extra_headers (src);
1295 GST_DEBUG_OBJECT (src, "request headers:");
1296 soup_message_headers_foreach (src->msg->request_headers,
1297 gst_soup_http_src_headers_foreach, src);
1303 static GstFlowReturn
1304 gst_soup_http_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
1306 GstSoupHTTPSrc *src;
1308 src = GST_SOUP_HTTP_SRC (psrc);
1310 if (src->msg && (src->request_position != src->read_position)) {
1312 if (src->file_size != 0 && src->request_position >= src->file_size) {
1314 if (src->content_size != 0 && src->request_position >= src->content_size) {
1316 GST_WARNING_OBJECT (src, "Seeking behind the end of file -- EOS");
1317 return GST_FLOW_UNEXPECTED;
1318 } else if (src->session_io_status ==
1319 GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE) {
1320 gst_soup_http_src_add_range_header (src, src->request_position);
1322 GST_DEBUG_OBJECT (src, "Seek from position %" G_GUINT64_FORMAT
1323 " to %" G_GUINT64_FORMAT ": requeueing connection request",
1324 src->read_position, src->request_position);
1325 #ifndef SEEK_CHANGES
1326 gst_soup_http_src_cancel_message (src);
1331 if(src->msg && src->seeked) {
1332 GST_DEBUG_OBJECT (src, "seeking to offset start %llu end %llu", src->request_position, (src->request_position+src->range_size-1));
1334 soup_session_cancel_message (src->session, src->msg, SOUP_STATUS_OK);
1336 if (!gst_soup_http_src_build_message (src))
1337 return GST_FLOW_ERROR;
1339 soup_session_queue_message (src->session, src->msg, (SoupSessionCallback) gst_soup_http_src_response_cb, src);
1340 src->session_io_status = GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_QUEUED;
1341 src->read_position = src->request_position;
1342 if(src->range_size > 0) src->content_size = src->request_position+src->range_size-1;
1343 else src->content_size = src->file_size;
1345 src->seeked = FALSE;
1348 if (!gst_soup_http_src_build_message (src))
1349 return GST_FLOW_ERROR;
1351 src->ret = GST_FLOW_CUSTOM_ERROR;
1352 src->outbuf = outbuf;
1354 if (src->interrupted) {
1355 GST_DEBUG_OBJECT (src, "interrupted");
1359 GST_DEBUG_OBJECT (src, "Reconnecting");
1360 if (!gst_soup_http_src_build_message (src))
1361 return GST_FLOW_ERROR;
1366 GST_DEBUG_OBJECT (src, "EOS reached");
1370 switch (src->session_io_status) {
1371 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_IDLE:
1372 GST_DEBUG_OBJECT (src, "Queueing connection request");
1373 gst_soup_http_src_queue_message (src);
1375 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_QUEUED:
1377 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_RUNNING:
1378 gst_soup_http_src_session_unpause_message (src);
1380 case GST_SOUP_HTTP_SRC_SESSION_IO_STATUS_CANCELLED:
1385 if (src->ret == GST_FLOW_CUSTOM_ERROR)
1386 g_main_loop_run (src->loop);
1387 } while (src->ret == GST_FLOW_CUSTOM_ERROR);
1389 if (src->ret == GST_FLOW_CUSTOM_ERROR)
1390 src->ret = GST_FLOW_UNEXPECTED;
1395 gst_soup_http_src_start (GstBaseSrc * bsrc)
1397 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1399 GST_DEBUG_OBJECT (src, "start(\"%s\")", src->location);
1401 if (!src->location) {
1402 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (_("No URL set.")),
1403 ("Missing location property"));
1407 src->context = g_main_context_new ();
1409 src->loop = g_main_loop_new (src->context, TRUE);
1411 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
1412 (NULL), ("Failed to start GMainLoop"));
1413 g_main_context_unref (src->context);
1417 if (src->proxy == NULL) {
1419 soup_session_async_new_with_options (SOUP_SESSION_ASYNC_CONTEXT,
1420 src->context, SOUP_SESSION_USER_AGENT, src->user_agent,
1421 SOUP_SESSION_TIMEOUT, src->timeout,
1422 #ifdef HAVE_LIBSOUP_GNOME
1423 SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_PROXY_RESOLVER_GNOME,
1428 soup_session_async_new_with_options (SOUP_SESSION_ASYNC_CONTEXT,
1429 src->context, SOUP_SESSION_PROXY_URI, src->proxy,
1430 SOUP_SESSION_TIMEOUT, src->timeout,
1431 SOUP_SESSION_USER_AGENT, src->user_agent, NULL);
1434 if (!src->session) {
1435 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
1436 (NULL), ("Failed to create async session"));
1440 #ifdef GST_EXT_SOUP_MODIFICATION
1441 soup_session_add_feature_by_type (src->session, SOUP_TYPE_COOKIE_JAR);
1442 src->cookie_jar = SOUP_COOKIE_JAR (soup_session_get_feature (src->session, SOUP_TYPE_COOKIE_JAR));
1445 g_signal_connect (src->session, "authenticate",
1446 G_CALLBACK (gst_soup_http_src_authenticate_cb), src);
1451 gst_soup_http_src_stop (GstBaseSrc * bsrc)
1453 GstSoupHTTPSrc *src;
1455 src = GST_SOUP_HTTP_SRC (bsrc);
1456 GST_DEBUG_OBJECT (src, "stop()");
1457 gst_soup_http_src_session_close (src);
1459 g_main_loop_unref (src->loop);
1460 g_main_context_unref (src->context);
1462 src->context = NULL;
1464 if (src->extra_headers) {
1465 gst_structure_free (src->extra_headers);
1466 src->extra_headers = NULL;
1469 gst_soup_http_src_reset (src);
1473 /* Interrupt a blocking request. */
1475 gst_soup_http_src_unlock (GstBaseSrc * bsrc)
1477 GstSoupHTTPSrc *src;
1479 src = GST_SOUP_HTTP_SRC (bsrc);
1480 GST_DEBUG_OBJECT (src, "unlock()");
1482 src->interrupted = TRUE;
1484 g_main_loop_quit (src->loop);
1488 /* Interrupt interrupt. */
1490 gst_soup_http_src_unlock_stop (GstBaseSrc * bsrc)
1492 GstSoupHTTPSrc *src;
1494 src = GST_SOUP_HTTP_SRC (bsrc);
1495 GST_DEBUG_OBJECT (src, "unlock_stop()");
1497 src->interrupted = FALSE;
1502 gst_soup_http_src_get_size (GstBaseSrc * bsrc, guint64 * size)
1504 GstSoupHTTPSrc *src;
1506 src = GST_SOUP_HTTP_SRC (bsrc);
1508 if (src->have_size) {
1510 GST_DEBUG_OBJECT (src, "get_size() = %" G_GUINT64_FORMAT,
1512 *size = src->file_size;
1514 GST_DEBUG_OBJECT (src, "get_size() = %" G_GUINT64_FORMAT,
1516 *size = src->content_size;
1520 GST_DEBUG_OBJECT (src, "get_size() = FALSE");
1525 gst_soup_http_src_is_seekable (GstBaseSrc * bsrc)
1527 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1529 return src->seekable;
1533 gst_soup_http_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
1535 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1537 GST_DEBUG_OBJECT (src, "do_seek(%" G_GUINT64_FORMAT ")", segment->start);
1542 if (src->read_position == segment->start) {
1543 GST_DEBUG_OBJECT (src, "Seeking to current read position");
1547 if (!src->seekable) {
1548 GST_WARNING_OBJECT (src, "Not seekable");
1552 if (segment->rate < 0.0 || segment->format != GST_FORMAT_BYTES) {
1553 GST_WARNING_OBJECT (src, "Invalid seek segment");
1558 if (src->content_size != 0 && segment->start >= src->file_size) {
1560 if (src->content_size != 0 && segment->start >= src->content_size) {
1562 GST_WARNING_OBJECT (src, "Seeking behind end of file, will go to EOS soon");
1565 /* Wait for create() to handle the jump in offset. */
1566 src->request_position = segment->start;
1571 gst_soup_http_src_query (GstBaseSrc * bsrc, GstQuery * query)
1573 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
1576 switch (GST_QUERY_TYPE (query)) {
1578 gst_query_set_uri (query, src->location);
1587 ret = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
1593 gst_soup_http_src_set_location (GstSoupHTTPSrc * src, const gchar * uri)
1595 if (src->location) {
1596 g_free (src->location);
1597 src->location = NULL;
1599 src->location = g_strdup (uri);
1605 gst_soup_http_src_set_proxy (GstSoupHTTPSrc * src, const gchar * uri)
1608 soup_uri_free (src->proxy);
1611 if (g_str_has_prefix (uri, "http://")) {
1612 src->proxy = soup_uri_new (uri);
1614 gchar *new_uri = g_strconcat ("http://", uri, NULL);
1616 src->proxy = soup_uri_new (new_uri);
1624 gst_soup_http_src_uri_get_type (void)
1630 gst_soup_http_src_uri_get_protocols (void)
1632 static const gchar *protocols[] = { "http", "https", NULL };
1633 return (gchar **) protocols;
1636 static const gchar *
1637 gst_soup_http_src_uri_get_uri (GstURIHandler * handler)
1639 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (handler);
1641 return src->location;
1645 gst_soup_http_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1647 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (handler);
1649 return gst_soup_http_src_set_location (src, uri);
1653 gst_soup_http_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1655 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1657 iface->get_type = gst_soup_http_src_uri_get_type;
1658 iface->get_protocols = gst_soup_http_src_uri_get_protocols;
1659 iface->get_uri = gst_soup_http_src_uri_get_uri;
1660 iface->set_uri = gst_soup_http_src_uri_set_uri;