2 * Copyright (C) 2007-2008 Wouter Cloetens <wouter@mind.be>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more
16 * SECTION:element-souphttpsrc
18 * This plugin reads data from a remote location specified by a URI.
19 * Supported protocols are 'http', 'https'.
21 * An HTTP proxy must be specified by its URL.
22 * If the "http_proxy" environment variable is set, its value is used.
23 * If built with libsoup's GNOME integration features, the GNOME proxy
24 * configuration will be used, or failing that, proxy autodetection.
25 * The #GstSoupHTTPSrc:proxy property can be used to override the default.
27 * In case the #GstSoupHTTPSrc:iradio-mode property is set and the location is
28 * an HTTP resource, souphttpsrc will send special Icecast HTTP headers to the
29 * server to request additional Icecast meta-information.
30 * If the server is not an Icecast server, it will behave as if the
31 * #GstSoupHTTPSrc:iradio-mode property were not set. If it is, souphttpsrc will
32 * output data with a media type of application/x-icy, in which case you will
33 * need to use the #ICYDemux element as follow-up element to extract the Icecast
34 * metadata and to determine the underlying media type.
37 * <title>Example launch line</title>
39 * gst-launch-1.0 -v souphttpsrc location=https://some.server.org/index.html
40 * ! filesink location=/home/joe/server.html
41 * ]| The above pipeline reads a web page from a server using the HTTPS protocol
42 * and writes it to a local file.
44 * gst-launch-1.0 -v souphttpsrc user-agent="FooPlayer 0.99 beta"
45 * automatic-redirect=false proxy=http://proxy.intranet.local:8080
46 * location=http://music.foobar.com/demo.mp3 ! mpgaudioparse
47 * ! mpg123audiodec ! audioconvert ! audioresample ! autoaudiosink
48 * ]| The above pipeline will read and decode and play an mp3 file from a
49 * web server using the HTTP protocol. If the server sends redirects,
50 * the request fails instead of following the redirect. The specified
51 * HTTP proxy server is used. The User-Agent HTTP request header
52 * is set to a custom string instead of "GStreamer souphttpsrc."
54 * gst-launch-1.0 -v souphttpsrc location=http://10.11.12.13/mjpeg
55 * do-timestamp=true ! multipartdemux
56 * ! image/jpeg,width=640,height=480 ! matroskamux
57 * ! filesink location=mjpeg.mkv
58 * ]| The above pipeline reads a motion JPEG stream from an IP camera
59 * using the HTTP protocol, encoded as mime/multipart image/jpeg
60 * parts, and writes a Matroska motion JPEG file. The width and
61 * height properties are set in the caps to provide the Matroska
62 * multiplexer with the information to set this in the header.
63 * Timestamps are set on the buffers as they arrive from the camera.
64 * These are used by the mime/multipart demultiplexer to emit timestamps
65 * on the JPEG-encoded video frame buffers. This allows the Matroska
66 * multiplexer to timestamp the frames in the resulting file.
76 #include <stdlib.h> /* atoi() */
78 #include <gst/gstelement.h>
79 #include <gst/gst-i18n-plugin.h>
80 #include <libsoup/soup.h>
81 #include "gstsouphttpsrc.h"
82 #include "gstsouputils.h"
84 #include <gst/tag/tag.h>
86 GST_DEBUG_CATEGORY_STATIC (souphttpsrc_debug);
87 #define GST_CAT_DEFAULT souphttpsrc_debug
89 #define GST_SOUP_SESSION_CONTEXT "gst.soup.session"
91 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
102 PROP_AUTOMATIC_REDIRECT,
117 PROP_SSL_USE_SYSTEM_CA_FILE,
121 PROP_TLS_INTERACTION,
124 #define DEFAULT_USER_AGENT "GStreamer souphttpsrc " PACKAGE_VERSION " "
125 #define DEFAULT_IRADIO_MODE TRUE
126 #define DEFAULT_SOUP_LOG_LEVEL SOUP_LOGGER_LOG_HEADERS
127 #define DEFAULT_COMPRESS FALSE
128 #define DEFAULT_KEEP_ALIVE TRUE
129 #define DEFAULT_SSL_STRICT TRUE
130 #define DEFAULT_SSL_CA_FILE NULL
131 #define DEFAULT_SSL_USE_SYSTEM_CA_FILE TRUE
132 #define DEFAULT_TLS_DATABASE NULL
133 #define DEFAULT_TLS_INTERACTION NULL
134 #define DEFAULT_TIMEOUT 15
135 #define DEFAULT_RETRIES 3
136 #define DEFAULT_SOUP_METHOD NULL
138 #define GROW_BLOCKSIZE_LIMIT 1
139 #define GROW_BLOCKSIZE_COUNT 1
140 #define GROW_BLOCKSIZE_FACTOR 2
141 #define REDUCE_BLOCKSIZE_LIMIT 0.20
142 #define REDUCE_BLOCKSIZE_COUNT 2
143 #define REDUCE_BLOCKSIZE_FACTOR 0.5
144 #define GROW_TIME_LIMIT (1 * GST_SECOND)
146 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
147 #define DLNA_OP_TIMED_SEEK 0x02
148 #define DLNA_OP_BYTE_SEEK 0x01
151 static void gst_soup_http_src_uri_handler_init (gpointer g_iface,
152 gpointer iface_data);
153 static void gst_soup_http_src_finalize (GObject * gobject);
154 static void gst_soup_http_src_dispose (GObject * gobject);
156 static void gst_soup_http_src_set_property (GObject * object, guint prop_id,
157 const GValue * value, GParamSpec * pspec);
158 static void gst_soup_http_src_get_property (GObject * object, guint prop_id,
159 GValue * value, GParamSpec * pspec);
161 static GstStateChangeReturn gst_soup_http_src_change_state (GstElement *
162 element, GstStateChange transition);
163 static void gst_soup_http_src_set_context (GstElement * element,
164 GstContext * context);
165 static GstFlowReturn gst_soup_http_src_create (GstPushSrc * psrc,
166 GstBuffer ** outbuf);
167 static gboolean gst_soup_http_src_start (GstBaseSrc * bsrc);
168 static gboolean gst_soup_http_src_stop (GstBaseSrc * bsrc);
169 static gboolean gst_soup_http_src_get_size (GstBaseSrc * bsrc, guint64 * size);
170 static gboolean gst_soup_http_src_is_seekable (GstBaseSrc * bsrc);
171 static gboolean gst_soup_http_src_do_seek (GstBaseSrc * bsrc,
172 GstSegment * segment);
173 static gboolean gst_soup_http_src_query (GstBaseSrc * bsrc, GstQuery * query);
174 static gboolean gst_soup_http_src_unlock (GstBaseSrc * bsrc);
175 static gboolean gst_soup_http_src_unlock_stop (GstBaseSrc * bsrc);
176 static gboolean gst_soup_http_src_set_location (GstSoupHTTPSrc * src,
177 const gchar * uri, GError ** error);
178 static gboolean gst_soup_http_src_set_proxy (GstSoupHTTPSrc * src,
180 static char *gst_soup_http_src_unicodify (const char *str);
181 static gboolean gst_soup_http_src_build_message (GstSoupHTTPSrc * src,
182 const gchar * method);
183 static void gst_soup_http_src_cancel_message (GstSoupHTTPSrc * src);
184 static gboolean gst_soup_http_src_add_range_header (GstSoupHTTPSrc * src,
185 guint64 offset, guint64 stop_offset);
186 static gboolean gst_soup_http_src_session_open (GstSoupHTTPSrc * src);
187 static void gst_soup_http_src_session_close (GstSoupHTTPSrc * src);
188 static GstFlowReturn gst_soup_http_src_parse_status (SoupMessage * msg,
189 GstSoupHTTPSrc * src);
190 static GstFlowReturn gst_soup_http_src_got_headers (GstSoupHTTPSrc * src,
192 static void gst_soup_http_src_authenticate_cb (SoupSession * session,
193 SoupMessage * msg, SoupAuth * auth, gboolean retrying,
194 GstSoupHTTPSrc * src);
196 #define gst_soup_http_src_parent_class parent_class
197 G_DEFINE_TYPE_WITH_CODE (GstSoupHTTPSrc, gst_soup_http_src, GST_TYPE_PUSH_SRC,
198 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
199 gst_soup_http_src_uri_handler_init));
202 gst_soup_http_src_class_init (GstSoupHTTPSrcClass * klass)
204 GObjectClass *gobject_class;
205 GstElementClass *gstelement_class;
206 GstBaseSrcClass *gstbasesrc_class;
207 GstPushSrcClass *gstpushsrc_class;
209 gobject_class = (GObjectClass *) klass;
210 gstelement_class = (GstElementClass *) klass;
211 gstbasesrc_class = (GstBaseSrcClass *) klass;
212 gstpushsrc_class = (GstPushSrcClass *) klass;
214 gobject_class->set_property = gst_soup_http_src_set_property;
215 gobject_class->get_property = gst_soup_http_src_get_property;
216 gobject_class->finalize = gst_soup_http_src_finalize;
217 gobject_class->dispose = gst_soup_http_src_dispose;
219 g_object_class_install_property (gobject_class,
221 g_param_spec_string ("location", "Location",
222 "Location to read from", "",
223 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
224 g_object_class_install_property (gobject_class,
226 g_param_spec_string ("user-agent", "User-Agent",
227 "Value of the User-Agent HTTP request header field",
228 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
229 g_object_class_install_property (gobject_class,
230 PROP_AUTOMATIC_REDIRECT,
231 g_param_spec_boolean ("automatic-redirect", "automatic-redirect",
232 "Automatically follow HTTP redirects (HTTP Status Code 3xx)",
233 TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
234 g_object_class_install_property (gobject_class,
236 g_param_spec_string ("proxy", "Proxy",
237 "HTTP proxy server URI", "",
238 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
239 g_object_class_install_property (gobject_class,
241 g_param_spec_string ("user-id", "user-id",
242 "HTTP location URI user id for authentication", "",
243 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
244 g_object_class_install_property (gobject_class, PROP_USER_PW,
245 g_param_spec_string ("user-pw", "user-pw",
246 "HTTP location URI user password for authentication", "",
247 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
248 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
249 g_param_spec_string ("proxy-id", "proxy-id",
250 "HTTP proxy URI user id for authentication", "",
251 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
252 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
253 g_param_spec_string ("proxy-pw", "proxy-pw",
254 "HTTP proxy URI user password for authentication", "",
255 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
256 g_object_class_install_property (gobject_class, PROP_COOKIES,
257 g_param_spec_boxed ("cookies", "Cookies", "HTTP request cookies",
258 G_TYPE_STRV, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
259 g_object_class_install_property (gobject_class, PROP_IS_LIVE,
260 g_param_spec_boolean ("is-live", "is-live", "Act like a live source",
261 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
262 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
263 g_param_spec_uint ("timeout", "timeout",
264 "Value in seconds to timeout a blocking I/O (0 = No timeout).", 0,
265 3600, DEFAULT_TIMEOUT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
266 g_object_class_install_property (gobject_class, PROP_EXTRA_HEADERS,
267 g_param_spec_boxed ("extra-headers", "Extra Headers",
268 "Extra headers to append to the HTTP request",
269 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
270 g_object_class_install_property (gobject_class, PROP_IRADIO_MODE,
271 g_param_spec_boolean ("iradio-mode", "iradio-mode",
272 "Enable internet radio mode (ask server to send shoutcast/icecast "
273 "metadata interleaved with the actual stream data)",
274 DEFAULT_IRADIO_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
277 * GstSoupHTTPSrc::http-log-level:
279 * If set and > 0, captures and dumps HTTP session data as
280 * log messages if log level >= GST_LEVEL_TRACE
284 g_object_class_install_property (gobject_class, PROP_SOUP_LOG_LEVEL,
285 g_param_spec_enum ("http-log-level", "HTTP log level",
286 "Set log level for soup's HTTP session log",
287 SOUP_TYPE_LOGGER_LOG_LEVEL, DEFAULT_SOUP_LOG_LEVEL,
288 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
291 * GstSoupHTTPSrc::compress:
293 * If set to %TRUE, souphttpsrc will automatically handle gzip
294 * and deflate Content-Encodings. This does not make much difference
295 * and causes more load for normal media files, but makes a real
296 * difference in size for plaintext files.
300 g_object_class_install_property (gobject_class, PROP_COMPRESS,
301 g_param_spec_boolean ("compress", "Compress",
302 "Allow compressed content encodings",
303 DEFAULT_COMPRESS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
306 * GstSoupHTTPSrc::keep-alive:
308 * If set to %TRUE, souphttpsrc will keep alive connections when being
309 * set to READY state and only will close connections when connecting
310 * to a different server or when going to NULL state..
314 g_object_class_install_property (gobject_class, PROP_KEEP_ALIVE,
315 g_param_spec_boolean ("keep-alive", "keep-alive",
316 "Use HTTP persistent connections", DEFAULT_KEEP_ALIVE,
317 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
320 * GstSoupHTTPSrc::ssl-strict:
322 * If set to %TRUE, souphttpsrc will reject all SSL certificates that
323 * are considered invalid.
327 g_object_class_install_property (gobject_class, PROP_SSL_STRICT,
328 g_param_spec_boolean ("ssl-strict", "SSL Strict",
329 "Strict SSL certificate checking", DEFAULT_SSL_STRICT,
330 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
333 * GstSoupHTTPSrc::ssl-ca-file:
335 * A SSL anchor CA file that should be used for checking certificates
336 * instead of the system CA file.
338 * If this property is non-%NULL, #GstSoupHTTPSrc::ssl-use-system-ca-file
339 * value will be ignored.
341 * Deprecated: Use #GstSoupHTTPSrc::tls-database property instead.
344 g_object_class_install_property (gobject_class, PROP_SSL_CA_FILE,
345 g_param_spec_string ("ssl-ca-file", "SSL CA File",
346 "Location of a SSL anchor CA file to use", DEFAULT_SSL_CA_FILE,
347 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
350 * GstSoupHTTPSrc::ssl-use-system-ca-file:
352 * If set to %TRUE, souphttpsrc will use the system's CA file for
353 * checking certificates, unless #GstSoupHTTPSrc::ssl-ca-file or
354 * #GstSoupHTTPSrc::tls-database are non-%NULL.
358 g_object_class_install_property (gobject_class, PROP_SSL_USE_SYSTEM_CA_FILE,
359 g_param_spec_boolean ("ssl-use-system-ca-file", "Use System CA File",
360 "Use system CA file", DEFAULT_SSL_USE_SYSTEM_CA_FILE,
361 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
364 * GstSoupHTTPSrc::tls-database:
366 * TLS database with anchor certificate authorities used to validate
367 * the server certificate.
369 * If this property is non-%NULL, #GstSoupHTTPSrc::ssl-use-system-ca-file
370 * and #GstSoupHTTPSrc::ssl-ca-file values will be ignored.
374 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
375 g_param_spec_object ("tls-database", "TLS database",
376 "TLS database with anchor certificate authorities used to validate the server certificate",
377 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
380 * GstSoupHTTPSrc::tls-interaction:
382 * A #GTlsInteraction object to be used when the connection or certificate
383 * database need to interact with the user. This will be used to prompt the
384 * user for passwords or certificate where necessary.
388 g_object_class_install_property (gobject_class, PROP_TLS_INTERACTION,
389 g_param_spec_object ("tls-interaction", "TLS interaction",
390 "A GTlsInteraction object to be used when the connection or certificate database need to interact with the user.",
391 G_TYPE_TLS_INTERACTION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
394 * GstSoupHTTPSrc::retries:
396 * Maximum number of retries until giving up.
400 g_object_class_install_property (gobject_class, PROP_RETRIES,
401 g_param_spec_int ("retries", "Retries",
402 "Maximum number of retries until giving up (-1=infinite)", -1,
403 G_MAXINT, DEFAULT_RETRIES,
404 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
407 * GstSoupHTTPSrc::method
409 * The HTTP method to use when making a request
413 g_object_class_install_property (gobject_class, PROP_METHOD,
414 g_param_spec_string ("method", "HTTP method",
415 "The HTTP method to use (GET, HEAD, OPTIONS, etc)",
416 DEFAULT_SOUP_METHOD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
418 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
420 gst_element_class_set_static_metadata (gstelement_class, "HTTP client source",
422 "Receive data as a client over the network via HTTP using SOUP",
423 "Wouter Cloetens <wouter@mind.be>");
424 gstelement_class->change_state =
425 GST_DEBUG_FUNCPTR (gst_soup_http_src_change_state);
426 gstelement_class->set_context =
427 GST_DEBUG_FUNCPTR (gst_soup_http_src_set_context);
429 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_soup_http_src_start);
430 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_soup_http_src_stop);
431 gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_soup_http_src_unlock);
432 gstbasesrc_class->unlock_stop =
433 GST_DEBUG_FUNCPTR (gst_soup_http_src_unlock_stop);
434 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_soup_http_src_get_size);
435 gstbasesrc_class->is_seekable =
436 GST_DEBUG_FUNCPTR (gst_soup_http_src_is_seekable);
437 gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_soup_http_src_do_seek);
438 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_soup_http_src_query);
440 gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_soup_http_src_create);
442 GST_DEBUG_CATEGORY_INIT (souphttpsrc_debug, "souphttpsrc", 0,
447 gst_soup_http_src_reset (GstSoupHTTPSrc * src)
449 src->retry_count = 0;
450 src->have_size = FALSE;
451 src->got_headers = FALSE;
452 src->seekable = FALSE;
453 src->read_position = 0;
454 src->request_position = 0;
455 src->stop_position = -1;
456 src->content_size = 0;
457 src->have_body = FALSE;
459 src->reduce_blocksize_count = 0;
460 src->increase_blocksize_count = 0;
461 src->last_socket_read_time = 0;
463 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
464 if (src->dash_oldest_segment) {
465 g_free (src->dash_oldest_segment);
466 src->dash_oldest_segment = NULL;
468 if (src->dash_newest_segment) {
469 g_free (src->dash_newest_segment);
470 src->dash_newest_segment = NULL;
475 g_cancellable_reset (src->cancellable);
476 g_mutex_lock (&src->mutex);
477 if (src->input_stream) {
478 g_object_unref (src->input_stream);
479 src->input_stream = NULL;
481 g_mutex_unlock (&src->mutex);
483 gst_caps_replace (&src->src_caps, NULL);
484 g_free (src->iradio_name);
485 src->iradio_name = NULL;
486 g_free (src->iradio_genre);
487 src->iradio_genre = NULL;
488 g_free (src->iradio_url);
489 src->iradio_url = NULL;
493 gst_soup_http_src_init (GstSoupHTTPSrc * src)
497 g_mutex_init (&src->mutex);
498 g_cond_init (&src->have_headers_cond);
499 src->cancellable = g_cancellable_new ();
500 src->location = NULL;
501 src->redirection_uri = NULL;
502 src->automatic_redirect = TRUE;
503 src->user_agent = g_strdup (DEFAULT_USER_AGENT);
506 src->proxy_id = NULL;
507 src->proxy_pw = NULL;
509 src->iradio_mode = DEFAULT_IRADIO_MODE;
511 src->external_session = NULL;
512 src->forced_external_session = FALSE;
514 src->timeout = DEFAULT_TIMEOUT;
515 src->log_level = DEFAULT_SOUP_LOG_LEVEL;
516 src->compress = DEFAULT_COMPRESS;
517 src->keep_alive = DEFAULT_KEEP_ALIVE;
518 src->ssl_strict = DEFAULT_SSL_STRICT;
519 src->ssl_use_system_ca_file = DEFAULT_SSL_USE_SYSTEM_CA_FILE;
520 src->tls_database = DEFAULT_TLS_DATABASE;
521 src->tls_interaction = DEFAULT_TLS_INTERACTION;
522 src->max_retries = DEFAULT_RETRIES;
523 src->method = DEFAULT_SOUP_METHOD;
524 src->minimum_blocksize = gst_base_src_get_blocksize (GST_BASE_SRC_CAST (src));
525 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
526 src->dash_oldest_segment = NULL;
527 src->dash_newest_segment = NULL;
528 src->received_total = 0;
531 proxy = g_getenv ("http_proxy");
532 if (!gst_soup_http_src_set_proxy (src, proxy)) {
533 GST_WARNING_OBJECT (src,
534 "The proxy in the http_proxy env var (\"%s\") cannot be parsed.",
537 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
538 src->cookie_jar = NULL;
540 gst_base_src_set_automatic_eos (GST_BASE_SRC (src), FALSE);
542 gst_soup_http_src_reset (src);
546 gst_soup_http_src_dispose (GObject * gobject)
548 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (gobject);
550 GST_DEBUG_OBJECT (src, "dispose");
552 gst_soup_http_src_session_close (src);
554 if (src->external_session) {
555 g_object_unref (src->external_session);
556 src->external_session = NULL;
559 G_OBJECT_CLASS (parent_class)->dispose (gobject);
563 gst_soup_http_src_finalize (GObject * gobject)
565 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (gobject);
567 GST_DEBUG_OBJECT (src, "finalize");
569 g_mutex_clear (&src->mutex);
570 g_cond_clear (&src->have_headers_cond);
571 g_object_unref (src->cancellable);
572 g_free (src->location);
573 g_free (src->redirection_uri);
574 g_free (src->user_agent);
575 if (src->proxy != NULL) {
576 soup_uri_free (src->proxy);
578 g_free (src->user_id);
579 g_free (src->user_pw);
580 g_free (src->proxy_id);
581 g_free (src->proxy_pw);
582 g_strfreev (src->cookies);
584 if (src->extra_headers) {
585 gst_structure_free (src->extra_headers);
586 src->extra_headers = NULL;
589 g_free (src->ssl_ca_file);
591 if (src->tls_database)
592 g_object_unref (src->tls_database);
593 g_free (src->method);
595 if (src->tls_interaction)
596 g_object_unref (src->tls_interaction);
598 G_OBJECT_CLASS (parent_class)->finalize (gobject);
602 gst_soup_http_src_set_property (GObject * object, guint prop_id,
603 const GValue * value, GParamSpec * pspec)
605 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (object);
610 const gchar *location;
612 location = g_value_get_string (value);
614 if (location == NULL) {
615 GST_WARNING ("location property cannot be NULL");
618 if (!gst_soup_http_src_set_location (src, location, NULL)) {
619 GST_WARNING ("badly formatted location");
624 case PROP_USER_AGENT:
625 g_free (src->user_agent);
626 src->user_agent = g_value_dup_string (value);
628 case PROP_IRADIO_MODE:
629 src->iradio_mode = g_value_get_boolean (value);
631 case PROP_AUTOMATIC_REDIRECT:
632 src->automatic_redirect = g_value_get_boolean (value);
638 proxy = g_value_get_string (value);
639 if (!gst_soup_http_src_set_proxy (src, proxy)) {
640 GST_WARNING ("badly formatted proxy URI");
646 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
650 g_strfreev (src->cookies);
651 src->cookies = g_strdupv (g_value_get_boxed (value));
653 if (src->cookie_jar && ((array = src->cookies) != NULL)) {
654 base_uri = soup_uri_new (src->location);
655 GST_INFO_OBJECT (src, "request to set cookies...");
656 while (*array != NULL) {
657 soup_cookie_jar_add_cookie (src->cookie_jar,
658 soup_cookie_parse (*array++, base_uri));
660 soup_uri_free (base_uri);
662 GST_INFO_OBJECT (src, "set cookies after session creation");
666 g_strfreev (src->cookies);
667 src->cookies = g_strdupv (g_value_get_boxed (value));
671 gst_base_src_set_live (GST_BASE_SRC (src), g_value_get_boolean (value));
674 g_free (src->user_id);
675 src->user_id = g_value_dup_string (value);
678 g_free (src->user_pw);
679 src->user_pw = g_value_dup_string (value);
682 g_free (src->proxy_id);
683 src->proxy_id = g_value_dup_string (value);
686 g_free (src->proxy_pw);
687 src->proxy_pw = g_value_dup_string (value);
690 src->timeout = g_value_get_uint (value);
692 case PROP_EXTRA_HEADERS:{
693 const GstStructure *s = gst_value_get_structure (value);
695 if (src->extra_headers)
696 gst_structure_free (src->extra_headers);
698 src->extra_headers = s ? gst_structure_copy (s) : NULL;
701 case PROP_SOUP_LOG_LEVEL:
702 src->log_level = g_value_get_enum (value);
705 src->compress = g_value_get_boolean (value);
707 case PROP_KEEP_ALIVE:
708 src->keep_alive = g_value_get_boolean (value);
710 case PROP_SSL_STRICT:
711 src->ssl_strict = g_value_get_boolean (value);
713 case PROP_SSL_CA_FILE:
714 g_free (src->ssl_ca_file);
715 src->ssl_ca_file = g_value_dup_string (value);
717 case PROP_SSL_USE_SYSTEM_CA_FILE:
718 src->ssl_use_system_ca_file = g_value_get_boolean (value);
720 case PROP_TLS_DATABASE:
721 g_clear_object (&src->tls_database);
722 src->tls_database = g_value_dup_object (value);
724 case PROP_TLS_INTERACTION:
725 g_clear_object (&src->tls_interaction);
726 src->tls_interaction = g_value_dup_object (value);
729 src->max_retries = g_value_get_int (value);
732 g_free (src->method);
733 src->method = g_value_dup_string (value);
736 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
744 gst_soup_http_src_get_property (GObject * object, guint prop_id,
745 GValue * value, GParamSpec * pspec)
747 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (object);
751 g_value_set_string (value, src->location);
753 case PROP_USER_AGENT:
754 g_value_set_string (value, src->user_agent);
756 case PROP_AUTOMATIC_REDIRECT:
757 g_value_set_boolean (value, src->automatic_redirect);
760 if (src->proxy == NULL)
761 g_value_set_static_string (value, "");
763 char *proxy = soup_uri_to_string (src->proxy, FALSE);
765 g_value_set_string (value, proxy);
770 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
772 GSList *cookie_list, *c;
773 gchar **cookies, **array;
776 if ((src->cookie_jar) &&
777 ((cookie_list = soup_cookie_jar_all_cookies (src->cookie_jar)) != NULL)) {
778 cookies = g_new0 (gchar *, g_slist_length(cookie_list) + 1);
780 for (c = cookie_list; c; c = c->next) {
781 *array++ = soup_cookie_to_set_cookie_header ((SoupCookie *)(c->data));
783 soup_cookies_free (cookie_list);
785 g_value_set_boxed (value, cookies);
788 g_value_set_boxed (value, g_strdupv (src->cookies));
792 g_value_set_boolean (value, gst_base_src_is_live (GST_BASE_SRC (src)));
794 case PROP_IRADIO_MODE:
795 g_value_set_boolean (value, src->iradio_mode);
798 g_value_set_string (value, src->user_id);
801 g_value_set_string (value, src->user_pw);
804 g_value_set_string (value, src->proxy_id);
807 g_value_set_string (value, src->proxy_pw);
810 g_value_set_uint (value, src->timeout);
812 case PROP_EXTRA_HEADERS:
813 gst_value_set_structure (value, src->extra_headers);
815 case PROP_SOUP_LOG_LEVEL:
816 g_value_set_enum (value, src->log_level);
819 g_value_set_boolean (value, src->compress);
821 case PROP_KEEP_ALIVE:
822 g_value_set_boolean (value, src->keep_alive);
824 case PROP_SSL_STRICT:
825 g_value_set_boolean (value, src->ssl_strict);
827 case PROP_SSL_CA_FILE:
828 g_value_set_string (value, src->ssl_ca_file);
830 case PROP_SSL_USE_SYSTEM_CA_FILE:
831 g_value_set_boolean (value, src->ssl_use_system_ca_file);
833 case PROP_TLS_DATABASE:
834 g_value_set_object (value, src->tls_database);
836 case PROP_TLS_INTERACTION:
837 g_value_set_object (value, src->tls_interaction);
840 g_value_set_int (value, src->max_retries);
843 g_value_set_string (value, src->method);
846 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
852 gst_soup_http_src_unicodify (const gchar * str)
854 const gchar *env_vars[] = { "GST_ICY_TAG_ENCODING",
855 "GST_TAG_ENCODING", NULL
858 return gst_tag_freeform_string_to_utf8 (str, -1, env_vars);
862 gst_soup_http_src_cancel_message (GstSoupHTTPSrc * src)
864 g_cancellable_cancel (src->cancellable);
865 g_cond_signal (&src->have_headers_cond);
869 gst_soup_http_src_add_range_header (GstSoupHTTPSrc * src, guint64 offset,
875 soup_message_headers_remove (src->msg->request_headers, "Range");
877 /* This changes are needed to enable Seekable Contents from server.
878 We have observed that , for few specific networks ( VODAFONE ) , without theabove headers ,
879 Youtube is sending non-seekable contents to the Client. */
880 #ifndef TIZEN_FEATURE_SOUP_MODIFICATION
881 if (offset || stop_offset != -1)
884 if (stop_offset != -1) {
885 g_assert (offset != stop_offset);
887 rc = g_snprintf (buf, sizeof (buf), "bytes=%" G_GUINT64_FORMAT "-%"
888 G_GUINT64_FORMAT, offset, (stop_offset > 0) ? stop_offset - 1 :
891 rc = g_snprintf (buf, sizeof (buf), "bytes=%" G_GUINT64_FORMAT "-",
894 if (rc > sizeof (buf) || rc < 0)
896 soup_message_headers_append (src->msg->request_headers, "Range", buf);
898 src->read_position = offset;
903 _append_extra_header (GQuark field_id, const GValue * value, gpointer user_data)
905 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (user_data);
906 const gchar *field_name = g_quark_to_string (field_id);
907 gchar *field_content = NULL;
909 if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
910 field_content = g_value_dup_string (value);
912 GValue dest = { 0, };
914 g_value_init (&dest, G_TYPE_STRING);
915 if (g_value_transform (value, &dest)) {
916 field_content = g_value_dup_string (&dest);
920 if (field_content == NULL) {
921 GST_ERROR_OBJECT (src, "extra-headers field '%s' contains no value "
922 "or can't be converted to a string", field_name);
926 GST_DEBUG_OBJECT (src, "Appending extra header: \"%s: %s\"", field_name,
928 soup_message_headers_append (src->msg->request_headers, field_name,
931 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
932 if (!g_ascii_strcasecmp(field_name, "Cookie")) {
934 SoupCookie *cookie_parsed = NULL;
935 gchar *saveptr = NULL;
937 if (strlen(field_content) > 0) {
938 gchar *tmp_field = NULL;
940 uri = soup_uri_new (src->location);
942 tmp_field = strtok_r (field_content, ";", &saveptr);
944 while (tmp_field != NULL) {
945 GST_DEBUG_OBJECT (src, "field_content = %s", tmp_field);
947 cookie_parsed = soup_cookie_parse(tmp_field, uri);
948 GST_DEBUG_OBJECT (src, "cookie parsed = %p", cookie_parsed);
951 soup_cookie_jar_add_cookie (src->cookie_jar, cookie_parsed);
953 tmp_field = strtok_r (NULL, ";", &saveptr);
960 g_free (field_content);
966 _append_extra_headers (GQuark field_id, const GValue * value,
969 if (G_VALUE_TYPE (value) == GST_TYPE_ARRAY) {
970 guint n = gst_value_array_get_size (value);
973 for (i = 0; i < n; i++) {
974 const GValue *v = gst_value_array_get_value (value, i);
976 if (!_append_extra_header (field_id, v, user_data))
979 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
980 guint n = gst_value_list_get_size (value);
983 for (i = 0; i < n; i++) {
984 const GValue *v = gst_value_list_get_value (value, i);
986 if (!_append_extra_header (field_id, v, user_data))
990 return _append_extra_header (field_id, value, user_data);
998 gst_soup_http_src_add_extra_headers (GstSoupHTTPSrc * src)
1000 if (!src->extra_headers)
1003 return gst_structure_foreach (src->extra_headers, _append_extra_headers, src);
1007 gst_soup_http_src_session_open (GstSoupHTTPSrc * src)
1010 GST_DEBUG_OBJECT (src, "Session is already open");
1014 if (!src->location) {
1015 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (_("No URL set.")),
1016 ("Missing location property"));
1020 if (!src->session) {
1022 gboolean can_share = (src->timeout == DEFAULT_TIMEOUT)
1023 && (src->ssl_strict == DEFAULT_SSL_STRICT)
1024 && (src->tls_interaction == NULL) && (src->proxy == NULL)
1025 && (src->tls_database == DEFAULT_TLS_DATABASE)
1026 && (src->ssl_ca_file == DEFAULT_SSL_CA_FILE)
1027 && (src->ssl_use_system_ca_file == DEFAULT_SSL_USE_SYSTEM_CA_FILE);
1029 query = gst_query_new_context (GST_SOUP_SESSION_CONTEXT);
1030 if (gst_pad_peer_query (GST_BASE_SRC_PAD (src), query)) {
1031 GstContext *context;
1033 gst_query_parse_context (query, &context);
1034 gst_element_set_context (GST_ELEMENT_CAST (src), context);
1036 GstMessage *message;
1039 gst_message_new_need_context (GST_OBJECT_CAST (src),
1040 GST_SOUP_SESSION_CONTEXT);
1041 gst_element_post_message (GST_ELEMENT_CAST (src), message);
1043 gst_query_unref (query);
1045 GST_OBJECT_LOCK (src);
1046 if (src->external_session && (can_share || src->forced_external_session)) {
1047 GST_DEBUG_OBJECT (src, "Using external session %p",
1048 src->external_session);
1049 src->session = g_object_ref (src->external_session);
1050 src->session_is_shared = TRUE;
1052 GST_DEBUG_OBJECT (src, "Creating session (can share %d)", can_share);
1054 /* We explicitly set User-Agent to NULL here and overwrite it per message
1055 * to be able to have the same session with different User-Agents per
1057 if (src->proxy == NULL) {
1059 soup_session_new_with_options (SOUP_SESSION_USER_AGENT,
1060 NULL, SOUP_SESSION_TIMEOUT, src->timeout,
1061 SOUP_SESSION_SSL_STRICT, src->ssl_strict,
1062 SOUP_SESSION_TLS_INTERACTION, src->tls_interaction, NULL);
1065 soup_session_new_with_options (SOUP_SESSION_PROXY_URI, src->proxy,
1066 SOUP_SESSION_TIMEOUT, src->timeout,
1067 SOUP_SESSION_SSL_STRICT, src->ssl_strict,
1068 SOUP_SESSION_USER_AGENT, NULL,
1069 SOUP_SESSION_TLS_INTERACTION, src->tls_interaction, NULL);
1073 gst_soup_util_log_setup (src->session, src->log_level,
1075 soup_session_add_feature_by_type (src->session,
1076 SOUP_TYPE_CONTENT_DECODER);
1077 soup_session_add_feature_by_type (src->session, SOUP_TYPE_COOKIE_JAR);
1080 GstContext *context;
1081 GstMessage *message;
1084 GST_DEBUG_OBJECT (src, "Sharing session %p", src->session);
1085 src->session_is_shared = TRUE;
1087 /* Unset the limit the number of maximum allowed connection */
1088 g_object_set (src->session, SOUP_SESSION_MAX_CONNS, G_MAXINT,
1089 SOUP_SESSION_MAX_CONNS_PER_HOST, G_MAXINT, NULL);
1091 context = gst_context_new (GST_SOUP_SESSION_CONTEXT, TRUE);
1092 s = gst_context_writable_structure (context);
1093 gst_structure_set (s, "session", SOUP_TYPE_SESSION, src->session,
1094 "force", G_TYPE_BOOLEAN, FALSE, NULL);
1096 gst_object_ref (src->session);
1097 GST_OBJECT_UNLOCK (src);
1098 gst_element_set_context (GST_ELEMENT_CAST (src), context);
1100 gst_message_new_have_context (GST_OBJECT_CAST (src), context);
1101 gst_element_post_message (GST_ELEMENT_CAST (src), message);
1102 GST_OBJECT_LOCK (src);
1103 gst_object_unref (src->session);
1105 src->session_is_shared = FALSE;
1110 if (!src->session) {
1111 GST_ELEMENT_ERROR (src, LIBRARY, INIT,
1112 (NULL), ("Failed to create session"));
1113 GST_OBJECT_UNLOCK (src);
1117 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
1119 char **array = NULL;
1121 SoupCookie *soup_cookie = NULL;
1123 soup_session_add_feature_by_type (src->session, SOUP_TYPE_COOKIE_JAR);
1124 src->cookie_jar = SOUP_COOKIE_JAR (soup_session_get_feature (src->session, SOUP_TYPE_COOKIE_JAR));
1125 if ((array = src->cookies) != NULL) {
1126 base_uri = soup_uri_new (src->location);
1127 while (*array != NULL) {
1128 soup_cookie = soup_cookie_parse (*array++, base_uri);
1129 if (soup_cookie != NULL) {
1130 GST_INFO_OBJECT (src, "adding cookies..");
1131 soup_cookie_jar_add_cookie (src->cookie_jar, soup_cookie);
1134 soup_uri_free (base_uri);
1139 g_signal_connect (src->session, "authenticate",
1140 G_CALLBACK (gst_soup_http_src_authenticate_cb), src);
1142 if (!src->session_is_shared) {
1143 if (src->tls_database)
1144 g_object_set (src->session, "tls-database", src->tls_database, NULL);
1145 else if (src->ssl_ca_file)
1146 g_object_set (src->session, "ssl-ca-file", src->ssl_ca_file, NULL);
1148 g_object_set (src->session, "ssl-use-system-ca-file",
1149 src->ssl_use_system_ca_file, NULL);
1151 GST_OBJECT_UNLOCK (src);
1153 GST_DEBUG_OBJECT (src, "Re-using session");
1160 gst_soup_http_src_session_close (GstSoupHTTPSrc * src)
1162 GST_DEBUG_OBJECT (src, "Closing session");
1164 g_mutex_lock (&src->mutex);
1166 soup_session_cancel_message (src->session, src->msg, SOUP_STATUS_CANCELLED);
1167 g_object_unref (src->msg);
1172 if (!src->session_is_shared)
1173 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
1174 /* When Playback is ongoing and Browser is moved to background ( Pressing Menu or Home Key ), The Session gets destroyed.
1175 But the cookie_jar property remains unfreed. This results in garbage pointer and causes crash.
1176 Removing the cookie_jar feature during close session of browser to handle the issue. */
1178 GST_DEBUG_OBJECT (src, "Removing Cookie Jar instance");
1179 soup_session_remove_feature_by_type(src->session, SOUP_TYPE_COOKIE_JAR);
1180 src->cookie_jar = NULL;
1181 soup_session_abort (src->session);
1184 soup_session_abort (src->session);
1186 g_signal_handlers_disconnect_by_func (src->session,
1187 G_CALLBACK (gst_soup_http_src_authenticate_cb), src);
1188 g_object_unref (src->session);
1189 src->session = NULL;
1192 g_mutex_unlock (&src->mutex);
1196 gst_soup_http_src_authenticate_cb (SoupSession * session, SoupMessage * msg,
1197 SoupAuth * auth, gboolean retrying, GstSoupHTTPSrc * src)
1199 /* Might be from another user of the shared session */
1200 if (!GST_IS_SOUP_HTTP_SRC (src) || msg != src->msg)
1204 /* First time authentication only, if we fail and are called again with retry true fall through */
1205 if (msg->status_code == SOUP_STATUS_UNAUTHORIZED) {
1206 if (src->user_id && src->user_pw)
1207 soup_auth_authenticate (auth, src->user_id, src->user_pw);
1208 } else if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
1209 if (src->proxy_id && src->proxy_pw)
1210 soup_auth_authenticate (auth, src->proxy_id, src->proxy_pw);
1216 insert_http_header (const gchar * name, const gchar * value, gpointer user_data)
1218 GstStructure *headers = user_data;
1221 if (!g_utf8_validate (name, -1, NULL) || !g_utf8_validate (value, -1, NULL))
1224 gv = gst_structure_get_value (headers, name);
1225 if (gv && GST_VALUE_HOLDS_ARRAY (gv)) {
1226 GValue v = G_VALUE_INIT;
1228 g_value_init (&v, G_TYPE_STRING);
1229 g_value_set_string (&v, value);
1230 gst_value_array_append_value ((GValue *) gv, &v);
1232 } else if (gv && G_VALUE_HOLDS_STRING (gv)) {
1233 GValue arr = G_VALUE_INIT;
1234 GValue v = G_VALUE_INIT;
1235 const gchar *old_value = g_value_get_string (gv);
1237 g_value_init (&arr, GST_TYPE_ARRAY);
1238 g_value_init (&v, G_TYPE_STRING);
1239 g_value_set_string (&v, old_value);
1240 gst_value_array_append_value (&arr, &v);
1241 g_value_set_string (&v, value);
1242 gst_value_array_append_value (&arr, &v);
1244 gst_structure_set_value (headers, name, &arr);
1246 g_value_unset (&arr);
1248 gst_structure_set (headers, name, G_TYPE_STRING, value, NULL);
1252 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
1254 gst_soup_http_src_headers_foreach (const gchar * name, const gchar * val,
1257 GST_INFO_OBJECT (src, " %s: %s", name, val);
1259 if (g_ascii_strcasecmp (name, "Set-Cookie") == 0) {
1261 gboolean bret = FALSE;
1262 GstStructure *s = NULL;
1263 GstSoupHTTPSrc * tmp = src;
1266 uri = soup_uri_new (tmp->location);
1268 /* post current bandwith & uri to application */
1269 s = gst_structure_new ("cookies",
1270 "updated-cookie", G_TYPE_STRING, val,
1271 "updated-url", G_TYPE_STRING, tmp->location, NULL);
1272 bret = gst_element_post_message (GST_ELEMENT_CAST (src), gst_message_new_element (GST_OBJECT_CAST (src), s));
1273 soup_cookie_jar_set_cookie (tmp->cookie_jar, uri, val);
1274 soup_uri_free (uri);
1276 GST_INFO_OBJECT (src, "request url [%s], posted cookies [%s] msg and returned = %d", tmp->location, val, bret);
1278 } else if (g_ascii_strcasecmp (name, "Dash-Oldest-Segment") == 0) {
1280 GstSoupHTTPSrc * tmp = src;
1281 tmp->dash_oldest_segment = g_strdup (val);
1282 GST_INFO_OBJECT (src, "Dash-Oldest-Segment set as %s ", tmp->dash_oldest_segment);
1284 } else if (g_ascii_strcasecmp (name, "Dash-Newest-Segment") == 0) {
1286 GstSoupHTTPSrc * tmp = src;
1287 tmp->dash_newest_segment = g_strdup (val);
1288 GST_INFO_OBJECT (src, "Dash-Newest-Segment set as %s ", tmp->dash_newest_segment);
1294 static GstFlowReturn
1295 gst_soup_http_src_got_headers (GstSoupHTTPSrc * src, SoupMessage * msg)
1298 GstTagList *tag_list;
1299 GstBaseSrc *basesrc;
1301 GHashTable *params = NULL;
1302 GstEvent *http_headers_event;
1303 GstStructure *http_headers, *headers;
1304 const gchar *accept_ranges;
1305 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
1306 gint64 start = 0, stop = 0, total = 0;
1309 GST_INFO_OBJECT (src, "got headers : %d", msg->status_code);
1310 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
1311 soup_message_headers_foreach (msg->response_headers,
1312 gst_soup_http_src_headers_foreach, src);
1314 if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED &&
1315 src->proxy_id && src->proxy_pw) {
1316 /* wait for authenticate callback */
1320 http_headers = gst_structure_new_empty ("http-headers");
1321 gst_structure_set (http_headers, "uri", G_TYPE_STRING, src->location,
1322 "http-status-code", G_TYPE_UINT, msg->status_code, NULL);
1323 if (src->redirection_uri)
1324 gst_structure_set (http_headers, "redirection-uri", G_TYPE_STRING,
1325 src->redirection_uri, NULL);
1326 headers = gst_structure_new_empty ("request-headers");
1327 soup_message_headers_foreach (msg->request_headers, insert_http_header,
1329 gst_structure_set (http_headers, "request-headers", GST_TYPE_STRUCTURE,
1331 gst_structure_free (headers);
1332 headers = gst_structure_new_empty ("response-headers");
1333 soup_message_headers_foreach (msg->response_headers, insert_http_header,
1335 gst_structure_set (http_headers, "response-headers", GST_TYPE_STRUCTURE,
1337 gst_structure_free (headers);
1339 gst_element_post_message (GST_ELEMENT_CAST (src),
1340 gst_message_new_element (GST_OBJECT_CAST (src),
1341 gst_structure_copy (http_headers)));
1343 if (msg->status_code == SOUP_STATUS_UNAUTHORIZED) {
1344 /* force an error */
1345 gst_structure_free (http_headers);
1346 return gst_soup_http_src_parse_status (msg, src);
1349 src->got_headers = TRUE;
1350 g_cond_broadcast (&src->have_headers_cond);
1352 http_headers_event =
1353 gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_STICKY, http_headers);
1354 gst_event_replace (&src->http_headers_event, http_headers_event);
1355 gst_event_unref (http_headers_event);
1357 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
1358 /* Parse DLNA OP CODE */
1359 if ((value = soup_message_headers_get_one
1360 (msg->response_headers, "contentFeatures.dlna.org")) != NULL) {
1361 gchar **token = NULL;
1364 GST_DEBUG_OBJECT (src, "DLNA server response");
1366 token = g_strsplit (value, ";", 0);
1367 for (ptr = token ; *ptr ; ptr++) {
1369 gchar *op_code = NULL;
1374 tmp = g_ascii_strup (*ptr, strlen (*ptr));
1375 if (!strstr (tmp, "DLNA.ORG_OP")) {
1382 op_code = strchr (*ptr, '=');
1386 src->dlna_opt = (atoi (op_code) / 10 << 1) | (atoi (op_code) % 10);
1387 GST_DEBUG_OBJECT (src, "dlna op code: %s (0x%X)", op_code, src->dlna_opt);
1395 /* Parse Content-Length. */
1396 if (soup_message_headers_get_encoding (msg->response_headers) ==
1397 SOUP_ENCODING_CONTENT_LENGTH) {
1398 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
1399 if (msg->status_code == SOUP_STATUS_PARTIAL_CONTENT) {
1400 newsize = src->request_position +
1401 soup_message_headers_get_content_length (msg->response_headers);
1403 if (soup_message_headers_get_content_range(msg->response_headers, &start, &stop, &total) && (total > 0)) {
1404 GST_DEBUG_OBJECT (src, "get range header : %" G_GINT64_FORMAT
1405 "~%" G_GINT64_FORMAT"/%"G_GINT64_FORMAT, start, stop, total);
1406 newsize = (guint64)total;
1408 if ((src->have_size) && (src->content_size <= src->request_position)) {
1409 newsize = src->content_size;
1411 newsize = soup_message_headers_get_content_length (msg->response_headers);
1416 newsize = src->request_position +
1417 soup_message_headers_get_content_length (msg->response_headers);
1419 if (!src->have_size || (src->content_size != newsize)) {
1420 src->content_size = newsize;
1421 src->have_size = TRUE;
1422 src->seekable = TRUE;
1423 GST_DEBUG_OBJECT (src, "size = %" G_GUINT64_FORMAT, src->content_size);
1425 basesrc = GST_BASE_SRC_CAST (src);
1426 basesrc->segment.duration = src->content_size;
1427 gst_element_post_message (GST_ELEMENT (src),
1428 gst_message_new_duration_changed (GST_OBJECT (src)));
1432 /* If the server reports Accept-Ranges: none we don't have to try
1433 * doing range requests at all
1435 if ((accept_ranges =
1436 soup_message_headers_get_one (msg->response_headers,
1437 "Accept-Ranges"))) {
1438 if (g_ascii_strcasecmp (accept_ranges, "none") == 0)
1439 src->seekable = FALSE;
1441 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
1442 else if (src->dlna_opt & DLNA_OP_BYTE_SEEK) {
1443 if (src->have_size) {
1444 GST_DEBUG_OBJECT (src, "DLNA server is seekable");
1445 src->seekable = TRUE;
1448 /* The Range request header is always included.
1449 * @ref gst_soup_http_src_add_range_header() */
1450 else if ((msg->status_code == SOUP_STATUS_OK) &&
1451 (soup_message_headers_get_content_range (msg->response_headers, &start, &stop, &total) == FALSE)) {
1452 GST_DEBUG_OBJECT (src, "there is no accept range header");
1453 src->seekable = FALSE;
1458 tag_list = gst_tag_list_new_empty ();
1461 soup_message_headers_get_one (msg->response_headers,
1462 "icy-metaint")) != NULL) {
1465 if (g_utf8_validate (value, -1, NULL)) {
1466 icy_metaint = atoi (value);
1468 GST_DEBUG_OBJECT (src, "icy-metaint: %s (parsed: %d)", value,
1470 if (icy_metaint > 0) {
1472 gst_caps_unref (src->src_caps);
1474 src->src_caps = gst_caps_new_simple ("application/x-icy",
1475 "metadata-interval", G_TYPE_INT, icy_metaint, NULL);
1477 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
1482 soup_message_headers_get_content_type (msg->response_headers,
1483 ¶ms)) != NULL) {
1484 if (!g_utf8_validate (value, -1, NULL)) {
1485 GST_WARNING_OBJECT (src, "Content-Type is invalid UTF-8");
1486 } else if (g_ascii_strcasecmp (value, "audio/L16") == 0) {
1491 GST_DEBUG_OBJECT (src, "Content-Type: %s", value);
1493 if (src->src_caps) {
1494 gst_caps_unref (src->src_caps);
1495 src->src_caps = NULL;
1498 param = g_hash_table_lookup (params, "channels");
1499 if (param != NULL) {
1500 guint64 val = g_ascii_strtoull (param, NULL, 10);
1507 param = g_hash_table_lookup (params, "rate");
1508 if (param != NULL) {
1509 guint64 val = g_ascii_strtoull (param, NULL, 10);
1516 if (rate > 0 && channels > 0) {
1517 src->src_caps = gst_caps_new_simple ("audio/x-unaligned-raw",
1518 "format", G_TYPE_STRING, "S16BE",
1519 "layout", G_TYPE_STRING, "interleaved",
1520 "channels", G_TYPE_INT, channels, "rate", G_TYPE_INT, rate, NULL);
1522 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
1525 GST_DEBUG_OBJECT (src, "Content-Type: %s", value);
1527 /* Set the Content-Type field on the caps */
1528 if (src->src_caps) {
1529 src->src_caps = gst_caps_make_writable (src->src_caps);
1530 gst_caps_set_simple (src->src_caps, "content-type", G_TYPE_STRING,
1532 gst_base_src_set_caps (GST_BASE_SRC (src), src->src_caps);
1538 g_hash_table_destroy (params);
1541 soup_message_headers_get_one (msg->response_headers,
1542 "icy-name")) != NULL) {
1543 if (g_utf8_validate (value, -1, NULL)) {
1544 g_free (src->iradio_name);
1545 src->iradio_name = gst_soup_http_src_unicodify (value);
1546 if (src->iradio_name) {
1547 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_ORGANIZATION,
1548 src->iradio_name, NULL);
1553 soup_message_headers_get_one (msg->response_headers,
1554 "icy-genre")) != NULL) {
1555 if (g_utf8_validate (value, -1, NULL)) {
1556 g_free (src->iradio_genre);
1557 src->iradio_genre = gst_soup_http_src_unicodify (value);
1558 if (src->iradio_genre) {
1559 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_GENRE,
1560 src->iradio_genre, NULL);
1564 if ((value = soup_message_headers_get_one (msg->response_headers, "icy-url"))
1566 if (g_utf8_validate (value, -1, NULL)) {
1567 g_free (src->iradio_url);
1568 src->iradio_url = gst_soup_http_src_unicodify (value);
1569 if (src->iradio_url) {
1570 gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, GST_TAG_LOCATION,
1571 src->iradio_url, NULL);
1575 if (!gst_tag_list_is_empty (tag_list)) {
1576 GST_DEBUG_OBJECT (src,
1577 "calling gst_element_found_tags with %" GST_PTR_FORMAT, tag_list);
1578 gst_pad_push_event (GST_BASE_SRC_PAD (src), gst_event_new_tag (tag_list));
1580 gst_tag_list_unref (tag_list);
1583 /* Handle HTTP errors. */
1584 return gst_soup_http_src_parse_status (msg, src);
1588 gst_soup_http_src_alloc_buffer (GstSoupHTTPSrc * src)
1590 GstBaseSrc *basesrc = GST_BASE_SRC_CAST (src);
1594 rc = GST_BASE_SRC_CLASS (parent_class)->alloc (basesrc, -1,
1595 basesrc->blocksize, &gstbuf);
1596 if (G_UNLIKELY (rc != GST_FLOW_OK)) {
1603 #define SOUP_HTTP_SRC_ERROR(src,soup_msg,cat,code,error_message) \
1605 GST_ELEMENT_ERROR_WITH_DETAILS ((src), cat, code, ("%s", error_message), \
1606 ("%s (%d), URL: %s, Redirect to: %s", (soup_msg)->reason_phrase, \
1607 (soup_msg)->status_code, (src)->location, GST_STR_NULL ((src)->redirection_uri)), \
1608 ("http-status-code", G_TYPE_UINT, (soup_msg)->status_code, \
1609 "http-redirect-uri", G_TYPE_STRING, GST_STR_NULL ((src)->redirection_uri), NULL)); \
1612 static GstFlowReturn
1613 gst_soup_http_src_parse_status (SoupMessage * msg, GstSoupHTTPSrc * src)
1615 if (msg->method == SOUP_METHOD_HEAD) {
1616 if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code))
1617 GST_DEBUG_OBJECT (src, "Ignoring error %d during HEAD request",
1622 if (SOUP_STATUS_IS_TRANSPORT_ERROR (msg->status_code)) {
1623 switch (msg->status_code) {
1624 case SOUP_STATUS_CANT_RESOLVE:
1625 case SOUP_STATUS_CANT_RESOLVE_PROXY:
1626 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, NOT_FOUND,
1627 _("Could not resolve server name."));
1628 return GST_FLOW_ERROR;
1629 case SOUP_STATUS_CANT_CONNECT:
1630 case SOUP_STATUS_CANT_CONNECT_PROXY:
1631 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ,
1632 _("Could not establish connection to server."));
1633 return GST_FLOW_ERROR;
1634 case SOUP_STATUS_SSL_FAILED:
1635 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ,
1636 _("Secure connection setup failed."));
1637 return GST_FLOW_ERROR;
1638 case SOUP_STATUS_IO_ERROR:
1639 if (src->max_retries == -1 || src->retry_count < src->max_retries)
1640 return GST_FLOW_CUSTOM_ERROR;
1641 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, READ,
1642 _("A network error occurred, or the server closed the connection "
1644 return GST_FLOW_ERROR;
1645 case SOUP_STATUS_MALFORMED:
1646 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, READ,
1647 _("Server sent bad data."));
1648 return GST_FLOW_ERROR;
1649 case SOUP_STATUS_CANCELLED:
1650 /* No error message when interrupted by program. */
1656 if (SOUP_STATUS_IS_CLIENT_ERROR (msg->status_code) ||
1657 SOUP_STATUS_IS_REDIRECTION (msg->status_code) ||
1658 SOUP_STATUS_IS_SERVER_ERROR (msg->status_code)) {
1659 const gchar *reason_phrase;
1661 reason_phrase = msg->reason_phrase;
1662 if (reason_phrase && !g_utf8_validate (reason_phrase, -1, NULL)) {
1663 GST_ERROR_OBJECT (src, "Invalid UTF-8 in reason");
1664 reason_phrase = "(invalid)";
1667 /* Report HTTP error. */
1669 /* when content_size is unknown and we have just finished receiving
1670 * a body message, requests that go beyond the content limits will result
1671 * in an error. Here we convert those to EOS */
1672 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
1673 if (msg->status_code == SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE &&
1674 ((src->have_body && !src->have_size) ||
1675 (src->have_size && src->request_position >= src->content_size))) {
1676 GST_DEBUG_OBJECT (src, "Requested range out of limits and received full "
1677 "body, returning EOS");
1678 return GST_FLOW_EOS;
1681 if (msg->status_code == SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE &&
1682 src->have_body && !src->have_size) {
1683 GST_DEBUG_OBJECT (src, "Requested range out of limits and received full "
1684 "body, returning EOS");
1685 return GST_FLOW_EOS;
1689 /* FIXME: reason_phrase is not translated and not suitable for user
1690 * error dialog according to libsoup documentation.
1692 if (msg->status_code == SOUP_STATUS_NOT_FOUND) {
1693 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, NOT_FOUND, (reason_phrase));
1694 } else if (msg->status_code == SOUP_STATUS_UNAUTHORIZED
1695 || msg->status_code == SOUP_STATUS_PAYMENT_REQUIRED
1696 || msg->status_code == SOUP_STATUS_FORBIDDEN
1697 || msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
1698 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, NOT_AUTHORIZED, (reason_phrase));
1700 SOUP_HTTP_SRC_ERROR (src, msg, RESOURCE, OPEN_READ, (reason_phrase));
1702 return GST_FLOW_ERROR;
1709 gst_soup_http_src_restarted_cb (SoupMessage * msg, GstSoupHTTPSrc * src)
1711 if (soup_session_would_redirect (src->session, msg)) {
1712 src->redirection_uri =
1713 soup_uri_to_string (soup_message_get_uri (msg), FALSE);
1714 src->redirection_permanent =
1715 (msg->status_code == SOUP_STATUS_MOVED_PERMANENTLY);
1716 GST_DEBUG_OBJECT (src, "%u redirect to \"%s\" (permanent %d)",
1717 msg->status_code, src->redirection_uri, src->redirection_permanent);
1722 gst_soup_http_src_build_message (GstSoupHTTPSrc * src, const gchar * method)
1724 g_return_val_if_fail (src->msg == NULL, FALSE);
1726 src->msg = soup_message_new (method, src->location);
1728 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
1729 ("Error parsing URL."), ("URL: %s", src->location));
1733 /* Duplicating the defaults of libsoup here. We don't want to set a
1734 * User-Agent in the session as each source might have its own User-Agent
1736 if (!src->user_agent || !*src->user_agent) {
1738 g_strdup_printf ("libsoup/%u.%u.%u", soup_get_major_version (),
1739 soup_get_minor_version (), soup_get_micro_version ());
1740 soup_message_headers_append (src->msg->request_headers, "User-Agent",
1742 g_free (user_agent);
1743 } else if (g_str_has_suffix (src->user_agent, " ")) {
1744 gchar *user_agent = g_strdup_printf ("%slibsoup/%u.%u.%u", src->user_agent,
1745 soup_get_major_version (),
1746 soup_get_minor_version (), soup_get_micro_version ());
1747 soup_message_headers_append (src->msg->request_headers, "User-Agent",
1749 g_free (user_agent);
1751 soup_message_headers_append (src->msg->request_headers, "User-Agent",
1755 if (!src->keep_alive) {
1756 soup_message_headers_append (src->msg->request_headers, "Connection",
1759 if (src->iradio_mode) {
1760 soup_message_headers_append (src->msg->request_headers, "icy-metadata",
1764 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
1765 /* This changes are needed to enable Seekable Contents from server.
1766 We have observed that , for few specific networks ( VODAFONE ) , without theabove headers ,
1767 Youtube is sending non-seekable contents to the Client. */
1768 soup_message_headers_append (src->msg->request_headers, "Accept-Ranges","bytes");
1770 if (src->cookie_jar) {
1771 GSList *cookie_list, *c;
1774 SoupURI *uri = NULL;
1776 uri = soup_uri_new (src->location);
1778 if ((cookie_list = soup_cookie_jar_all_cookies (src->cookie_jar)) != NULL) {
1779 for (c = cookie_list; c; c = c->next) {
1780 cookie = (SoupCookie *)c->data;
1781 if (soup_cookie_applies_to_uri(cookie, uri)) {
1782 header = soup_cookie_to_cookie_header (cookie);
1783 soup_message_headers_append (src->msg->request_headers, "Cookie", header);
1788 soup_cookies_free (cookie_list);
1789 soup_uri_free (uri);
1795 for (cookie = src->cookies; *cookie != NULL; cookie++) {
1796 soup_message_headers_append (src->msg->request_headers, "Cookie",
1803 soup_message_disable_feature (src->msg, SOUP_TYPE_CONTENT_DECODER);
1805 soup_message_set_flags (src->msg, SOUP_MESSAGE_OVERWRITE_CHUNKS |
1806 (src->automatic_redirect ? 0 : SOUP_MESSAGE_NO_REDIRECT));
1808 if (src->automatic_redirect) {
1809 g_signal_connect (src->msg, "restarted",
1810 G_CALLBACK (gst_soup_http_src_restarted_cb), src);
1813 gst_soup_http_src_add_range_header (src, src->request_position,
1814 src->stop_position);
1816 gst_soup_http_src_add_extra_headers (src);
1818 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
1819 soup_message_headers_foreach (src->msg->request_headers,
1820 gst_soup_http_src_headers_foreach, src);
1827 static GstFlowReturn
1828 gst_soup_http_src_send_message (GstSoupHTTPSrc * src)
1831 GError *error = NULL;
1833 g_return_val_if_fail (src->msg != NULL, GST_FLOW_ERROR);
1834 g_assert (src->input_stream == NULL);
1837 soup_session_send (src->session, src->msg, src->cancellable, &error);
1839 if (g_cancellable_is_cancelled (src->cancellable)) {
1840 ret = GST_FLOW_FLUSHING;
1844 ret = gst_soup_http_src_got_headers (src, src->msg);
1845 if (ret != GST_FLOW_OK) {
1849 if (!src->input_stream) {
1850 GST_DEBUG_OBJECT (src, "Didn't get an input stream: %s", error->message);
1851 ret = GST_FLOW_ERROR;
1855 if (SOUP_STATUS_IS_SUCCESSFUL (src->msg->status_code)) {
1856 GST_DEBUG_OBJECT (src, "Successfully got a reply");
1858 /* FIXME - be more helpful to people debugging */
1859 ret = GST_FLOW_ERROR;
1864 g_error_free (error);
1868 static GstFlowReturn
1869 gst_soup_http_src_do_request (GstSoupHTTPSrc * src, const gchar * method)
1873 if (src->max_retries != -1 && src->retry_count > src->max_retries) {
1874 GST_DEBUG_OBJECT (src, "Max retries reached");
1875 return GST_FLOW_ERROR;
1879 /* EOS immediately if we have an empty segment */
1880 if (src->request_position == src->stop_position)
1881 return GST_FLOW_EOS;
1883 GST_LOG_OBJECT (src, "Running request for method: %s", method);
1885 /* Update the position if we are retrying */
1886 if (src->msg && src->request_position > 0) {
1887 gst_soup_http_src_add_range_header (src, src->request_position,
1888 src->stop_position);
1889 } else if (src->msg && src->request_position == 0)
1890 soup_message_headers_remove (src->msg->request_headers, "Range");
1892 /* add_range_header() has the side effect of setting read_position to
1893 * the requested position. This *needs* to be set regardless of having
1894 * a message or not. Failure to do so would result in calculation being
1895 * done with stale/wrong read position */
1896 src->read_position = src->request_position;
1899 if (!gst_soup_http_src_build_message (src, method)) {
1900 return GST_FLOW_ERROR;
1904 if (g_cancellable_is_cancelled (src->cancellable)) {
1905 GST_INFO_OBJECT (src, "interrupted");
1906 return GST_FLOW_FLUSHING;
1909 ret = gst_soup_http_src_send_message (src);
1911 /* Check if Range header was respected. */
1912 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
1913 if (ret == GST_FLOW_OK && src->request_position > 0 &&
1914 (src->msg->status_code != SOUP_STATUS_PARTIAL_CONTENT) &&
1915 (src->request_position < src->content_size)) {
1917 if (ret == GST_FLOW_OK && src->request_position > 0 &&
1918 src->msg->status_code != SOUP_STATUS_PARTIAL_CONTENT) {
1920 src->seekable = FALSE;
1921 GST_ELEMENT_ERROR_WITH_DETAILS (src, RESOURCE, SEEK,
1922 (_("Server does not support seeking.")),
1923 ("Server does not accept Range HTTP header, URL: %s, Redirect to: %s",
1924 src->location, GST_STR_NULL (src->redirection_uri)),
1925 ("http-status-code", G_TYPE_UINT, src->msg->status_code,
1926 "http-redirection-uri", G_TYPE_STRING,
1927 GST_STR_NULL (src->redirection_uri), NULL));
1928 ret = GST_FLOW_ERROR;
1935 * Check if the bytes_read is above a certain threshold of the blocksize, if
1936 * that happens a few times in a row, increase the blocksize; Do the same in
1937 * the opposite direction to reduce the blocksize.
1940 gst_soup_http_src_check_update_blocksize (GstSoupHTTPSrc * src,
1943 guint blocksize = gst_base_src_get_blocksize (GST_BASE_SRC_CAST (src));
1945 gint64 time_since_last_read =
1946 g_get_monotonic_time () * GST_USECOND - src->last_socket_read_time;
1948 GST_LOG_OBJECT (src, "Checking to update blocksize. Read: %" G_GINT64_FORMAT
1949 " bytes, blocksize: %u bytes, time since last read: %" GST_TIME_FORMAT,
1950 bytes_read, blocksize, GST_TIME_ARGS (time_since_last_read));
1952 if (bytes_read >= blocksize * GROW_BLOCKSIZE_LIMIT
1953 && time_since_last_read <= GROW_TIME_LIMIT) {
1954 src->reduce_blocksize_count = 0;
1955 src->increase_blocksize_count++;
1957 if (src->increase_blocksize_count >= GROW_BLOCKSIZE_COUNT) {
1958 blocksize *= GROW_BLOCKSIZE_FACTOR;
1959 GST_DEBUG_OBJECT (src, "Increased blocksize to %u", blocksize);
1960 gst_base_src_set_blocksize (GST_BASE_SRC_CAST (src), blocksize);
1961 src->increase_blocksize_count = 0;
1963 } else if (bytes_read < blocksize * REDUCE_BLOCKSIZE_LIMIT
1964 || time_since_last_read > GROW_TIME_LIMIT) {
1965 src->reduce_blocksize_count++;
1966 src->increase_blocksize_count = 0;
1968 if (src->reduce_blocksize_count >= REDUCE_BLOCKSIZE_COUNT) {
1969 blocksize *= REDUCE_BLOCKSIZE_FACTOR;
1970 blocksize = MAX (blocksize, src->minimum_blocksize);
1971 GST_DEBUG_OBJECT (src, "Decreased blocksize to %u", blocksize);
1972 gst_base_src_set_blocksize (GST_BASE_SRC_CAST (src), blocksize);
1973 src->reduce_blocksize_count = 0;
1976 src->reduce_blocksize_count = src->increase_blocksize_count = 0;
1981 gst_soup_http_src_update_position (GstSoupHTTPSrc * src, gint64 bytes_read)
1983 GstBaseSrc *basesrc = GST_BASE_SRC_CAST (src);
1984 guint64 new_position;
1986 new_position = src->read_position + bytes_read;
1987 if (G_LIKELY (src->request_position == src->read_position))
1988 src->request_position = new_position;
1989 src->read_position = new_position;
1991 if (src->have_size) {
1992 if (new_position > src->content_size) {
1993 GST_DEBUG_OBJECT (src, "Got position previous estimated content size "
1994 "(%" G_GINT64_FORMAT " > %" G_GINT64_FORMAT ")", new_position,
1996 src->content_size = new_position;
1997 basesrc->segment.duration = src->content_size;
1998 gst_element_post_message (GST_ELEMENT (src),
1999 gst_message_new_duration_changed (GST_OBJECT (src)));
2000 } else if (new_position == src->content_size) {
2001 GST_DEBUG_OBJECT (src, "We're EOS now");
2006 static GstFlowReturn
2007 gst_soup_http_src_read_buffer (GstSoupHTTPSrc * src, GstBuffer ** outbuf)
2014 bsrc = GST_BASE_SRC_CAST (src);
2016 *outbuf = gst_soup_http_src_alloc_buffer (src);
2018 GST_WARNING_OBJECT (src, "Failed to allocate buffer");
2019 return GST_FLOW_ERROR;
2022 if (!gst_buffer_map (*outbuf, &mapinfo, GST_MAP_WRITE)) {
2023 GST_WARNING_OBJECT (src, "Failed to map buffer");
2024 return GST_FLOW_ERROR;
2028 g_input_stream_read (src->input_stream, mapinfo.data, mapinfo.size,
2029 src->cancellable, NULL);
2030 GST_DEBUG_OBJECT (src, "Read %" G_GSSIZE_FORMAT " bytes from http input",
2033 g_mutex_lock (&src->mutex);
2034 if (g_cancellable_is_cancelled (src->cancellable)) {
2035 gst_buffer_unmap (*outbuf, &mapinfo);
2036 gst_buffer_unref (*outbuf);
2037 g_mutex_unlock (&src->mutex);
2038 return GST_FLOW_FLUSHING;
2041 gst_buffer_unmap (*outbuf, &mapinfo);
2042 if (read_bytes > 0) {
2043 gst_buffer_set_size (*outbuf, read_bytes);
2044 GST_BUFFER_OFFSET (*outbuf) = bsrc->segment.position;
2046 gst_soup_http_src_update_position (src, read_bytes);
2047 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
2048 src->received_total += read_bytes;
2051 /* Got some data, reset retry counter */
2052 src->retry_count = 0;
2054 gst_soup_http_src_check_update_blocksize (src, read_bytes);
2056 src->last_socket_read_time = g_get_monotonic_time () * GST_USECOND;
2058 /* If we're at the end of a range request, read again to let libsoup
2059 * finalize the request. This allows to reuse the connection again later,
2060 * otherwise we would have to cancel the message and close the connection
2062 if (bsrc->segment.stop != -1
2063 && bsrc->segment.position + read_bytes >= bsrc->segment.stop) {
2066 g_object_unref (src->msg);
2068 src->have_body = TRUE;
2070 /* This should return immediately as we're at the end of the range */
2072 g_input_stream_read (src->input_stream, tmp, sizeof (tmp),
2073 src->cancellable, NULL);
2075 GST_ERROR_OBJECT (src,
2076 "Read %" G_GSIZE_FORMAT " bytes after end of range", read_bytes);
2079 gst_buffer_unref (*outbuf);
2080 if (read_bytes < 0 ||
2081 (src->have_size && src->read_position < src->content_size)) {
2082 /* Maybe the server disconnected, retry */
2083 ret = GST_FLOW_CUSTOM_ERROR;
2085 g_object_unref (src->msg);
2088 src->have_body = TRUE;
2091 g_mutex_unlock (&src->mutex);
2096 static GstFlowReturn
2097 gst_soup_http_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
2099 GstSoupHTTPSrc *src;
2100 GstFlowReturn ret = GST_FLOW_OK;
2101 GstEvent *http_headers_event = NULL;
2103 src = GST_SOUP_HTTP_SRC (psrc);
2106 g_mutex_lock (&src->mutex);
2108 /* Check for pending position change */
2109 if (src->request_position != src->read_position) {
2110 if (src->input_stream) {
2111 g_input_stream_close (src->input_stream, src->cancellable, NULL);
2112 g_object_unref (src->input_stream);
2113 src->input_stream = NULL;
2117 if (g_cancellable_is_cancelled (src->cancellable)) {
2118 ret = GST_FLOW_FLUSHING;
2119 g_mutex_unlock (&src->mutex);
2123 /* If we have no open connection to the server, start one */
2124 if (!src->input_stream) {
2127 gst_soup_http_src_do_request (src,
2128 src->method ? src->method : SOUP_METHOD_GET);
2129 http_headers_event = src->http_headers_event;
2130 src->http_headers_event = NULL;
2132 g_mutex_unlock (&src->mutex);
2134 if (ret == GST_FLOW_OK || ret == GST_FLOW_CUSTOM_ERROR) {
2135 if (http_headers_event) {
2136 gst_pad_push_event (GST_BASE_SRC_PAD (src), http_headers_event);
2137 http_headers_event = NULL;
2141 if (ret == GST_FLOW_OK)
2142 ret = gst_soup_http_src_read_buffer (src, outbuf);
2145 GST_DEBUG_OBJECT (src, "Returning %d %s", ret, gst_flow_get_name (ret));
2146 if (ret != GST_FLOW_OK) {
2147 if (http_headers_event)
2148 gst_event_unref (http_headers_event);
2150 g_mutex_lock (&src->mutex);
2151 if (src->input_stream) {
2152 g_object_unref (src->input_stream);
2153 src->input_stream = NULL;
2155 g_mutex_unlock (&src->mutex);
2156 if (ret == GST_FLOW_CUSTOM_ERROR) {
2162 if (ret == GST_FLOW_FLUSHING) {
2163 g_mutex_lock (&src->mutex);
2164 src->retry_count = 0;
2165 g_mutex_unlock (&src->mutex);
2172 gst_soup_http_src_start (GstBaseSrc * bsrc)
2174 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
2176 GST_DEBUG_OBJECT (src, "start(\"%s\")", src->location);
2178 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
2179 if (src->dash_oldest_segment) {
2180 g_free (src->dash_oldest_segment);
2181 src->dash_oldest_segment = NULL;
2183 if (src->dash_newest_segment) {
2184 g_free (src->dash_newest_segment);
2185 src->dash_newest_segment = NULL;
2188 return gst_soup_http_src_session_open (src);
2192 gst_soup_http_src_stop (GstBaseSrc * bsrc)
2194 GstSoupHTTPSrc *src;
2196 src = GST_SOUP_HTTP_SRC (bsrc);
2197 GST_DEBUG_OBJECT (src, "stop()");
2198 if (src->keep_alive && !src->msg && !src->session_is_shared)
2199 gst_soup_http_src_cancel_message (src);
2201 gst_soup_http_src_session_close (src);
2203 gst_soup_http_src_reset (src);
2207 static GstStateChangeReturn
2208 gst_soup_http_src_change_state (GstElement * element, GstStateChange transition)
2210 GstStateChangeReturn ret;
2211 GstSoupHTTPSrc *src;
2213 src = GST_SOUP_HTTP_SRC (element);
2215 switch (transition) {
2216 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
2217 case GST_STATE_CHANGE_PAUSED_TO_READY:
2218 GST_WARNING_OBJECT (src, "Last read pos"
2219 ": %" G_GINT64_FORMAT ", received total : %" G_GINT64_FORMAT,
2220 src->read_position, src->received_total);
2223 case GST_STATE_CHANGE_READY_TO_NULL:
2224 gst_soup_http_src_session_close (src);
2230 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2236 gst_soup_http_src_set_context (GstElement * element, GstContext * context)
2238 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (element);
2240 if (g_strcmp0 (gst_context_get_context_type (context),
2241 GST_SOUP_SESSION_CONTEXT) == 0) {
2242 const GstStructure *s = gst_context_get_structure (context);
2244 GST_OBJECT_LOCK (src);
2245 if (src->external_session)
2246 g_object_unref (src->external_session);
2247 src->external_session = NULL;
2248 gst_structure_get (s, "session", SOUP_TYPE_SESSION, &src->external_session,
2250 src->forced_external_session = FALSE;
2251 gst_structure_get (s, "force", G_TYPE_BOOLEAN,
2252 &src->forced_external_session, NULL);
2254 GST_DEBUG_OBJECT (src, "Setting external session %p (force: %d)",
2255 src->external_session, src->forced_external_session);
2256 GST_OBJECT_UNLOCK (src);
2259 GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
2262 /* Interrupt a blocking request. */
2264 gst_soup_http_src_unlock (GstBaseSrc * bsrc)
2266 GstSoupHTTPSrc *src;
2268 src = GST_SOUP_HTTP_SRC (bsrc);
2269 GST_DEBUG_OBJECT (src, "unlock()");
2271 gst_soup_http_src_cancel_message (src);
2275 /* Interrupt interrupt. */
2277 gst_soup_http_src_unlock_stop (GstBaseSrc * bsrc)
2279 GstSoupHTTPSrc *src;
2281 src = GST_SOUP_HTTP_SRC (bsrc);
2282 GST_DEBUG_OBJECT (src, "unlock_stop()");
2284 g_cancellable_reset (src->cancellable);
2289 gst_soup_http_src_get_size (GstBaseSrc * bsrc, guint64 * size)
2291 GstSoupHTTPSrc *src;
2293 src = GST_SOUP_HTTP_SRC (bsrc);
2295 if (src->have_size) {
2296 GST_DEBUG_OBJECT (src, "get_size() = %" G_GUINT64_FORMAT,
2298 *size = src->content_size;
2301 GST_DEBUG_OBJECT (src, "get_size() = FALSE");
2306 gst_soup_http_src_check_seekable (GstSoupHTTPSrc * src)
2308 GstFlowReturn ret = GST_FLOW_OK;
2310 /* Special case to check if the server allows range requests
2311 * before really starting to get data in the buffer creation
2314 if (!src->got_headers && GST_STATE (src) >= GST_STATE_PAUSED) {
2315 g_mutex_lock (&src->mutex);
2316 while (!src->got_headers && !g_cancellable_is_cancelled (src->cancellable)
2317 && ret == GST_FLOW_OK) {
2318 if ((src->msg && src->msg->method != SOUP_METHOD_HEAD)) {
2319 /* wait for the current request to finish */
2320 g_cond_wait (&src->have_headers_cond, &src->mutex);
2322 if (gst_soup_http_src_session_open (src)) {
2323 ret = gst_soup_http_src_do_request (src, SOUP_METHOD_HEAD);
2327 g_mutex_unlock (&src->mutex);
2332 gst_soup_http_src_is_seekable (GstBaseSrc * bsrc)
2334 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
2336 gst_soup_http_src_check_seekable (src);
2338 return src->seekable;
2342 gst_soup_http_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
2344 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
2346 GST_DEBUG_OBJECT (src, "do_seek(%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
2347 ")", segment->start, segment->stop);
2348 if (src->read_position == segment->start &&
2349 src->request_position == src->read_position &&
2350 src->stop_position == segment->stop) {
2351 GST_DEBUG_OBJECT (src,
2352 "Seek to current read/end position and no seek pending");
2356 gst_soup_http_src_check_seekable (src);
2358 /* If we have no headers we don't know yet if it is seekable or not.
2359 * Store the start position and error out later if it isn't */
2360 if (src->got_headers && !src->seekable) {
2361 GST_WARNING_OBJECT (src, "Not seekable");
2365 if (segment->rate < 0.0 || segment->format != GST_FORMAT_BYTES) {
2366 GST_WARNING_OBJECT (src, "Invalid seek segment");
2370 if (src->have_size && segment->start >= src->content_size) {
2371 GST_WARNING_OBJECT (src,
2372 "Potentially seeking behind end of file, might EOS immediately");
2375 /* Wait for create() to handle the jump in offset. */
2376 src->request_position = segment->start;
2377 src->stop_position = segment->stop;
2383 gst_soup_http_src_query (GstBaseSrc * bsrc, GstQuery * query)
2385 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (bsrc);
2387 GstSchedulingFlags flags;
2388 gint minsize, maxsize, align;
2390 switch (GST_QUERY_TYPE (query)) {
2392 gst_query_set_uri (query, src->location);
2393 if (src->redirection_uri != NULL) {
2394 gst_query_set_uri_redirection (query, src->redirection_uri);
2395 gst_query_set_uri_redirection_permanent (query,
2396 src->redirection_permanent);
2406 ret = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
2408 switch (GST_QUERY_TYPE (query)) {
2409 case GST_QUERY_SCHEDULING:
2410 gst_query_parse_scheduling (query, &flags, &minsize, &maxsize, &align);
2411 flags |= GST_SCHEDULING_FLAG_BANDWIDTH_LIMITED;
2413 #ifdef TIZEN_FEATURE_SOUP_MODIFICATION
2414 if (gst_soup_http_src_is_seekable(bsrc)) {
2415 GST_DEBUG_OBJECT (src, "set seekable flag");
2416 flags |= GST_SCHEDULING_FLAG_SEEKABLE;
2419 gst_query_set_scheduling (query, flags, minsize, maxsize, align);
2429 gst_soup_http_src_set_location (GstSoupHTTPSrc * src, const gchar * uri,
2432 const char *alt_schemes[] = { "icy://", "icyx://" };
2435 if (src->location) {
2436 g_free (src->location);
2437 src->location = NULL;
2443 for (i = 0; i < G_N_ELEMENTS (alt_schemes); i++) {
2444 if (g_str_has_prefix (uri, alt_schemes[i])) {
2446 g_strdup_printf ("http://%s", uri + strlen (alt_schemes[i]));
2451 if (src->redirection_uri) {
2452 g_free (src->redirection_uri);
2453 src->redirection_uri = NULL;
2456 src->location = g_strdup (uri);
2462 gst_soup_http_src_set_proxy (GstSoupHTTPSrc * src, const gchar * uri)
2465 soup_uri_free (src->proxy);
2469 if (uri == NULL || *uri == '\0')
2472 if (g_strstr_len (uri, -1, "://")) {
2473 src->proxy = soup_uri_new (uri);
2475 gchar *new_uri = g_strconcat ("http://", uri, NULL);
2477 src->proxy = soup_uri_new (new_uri);
2481 return (src->proxy != NULL);
2485 gst_soup_http_src_uri_get_type (GType type)
2490 static const gchar *const *
2491 gst_soup_http_src_uri_get_protocols (GType type)
2493 static const gchar *protocols[] = { "http", "https", "icy", "icyx", NULL };
2499 gst_soup_http_src_uri_get_uri (GstURIHandler * handler)
2501 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (handler);
2503 /* FIXME: make thread-safe */
2504 return g_strdup (src->location);
2508 gst_soup_http_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
2511 GstSoupHTTPSrc *src = GST_SOUP_HTTP_SRC (handler);
2513 return gst_soup_http_src_set_location (src, uri, error);
2517 gst_soup_http_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
2519 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
2521 iface->get_type = gst_soup_http_src_uri_get_type;
2522 iface->get_protocols = gst_soup_http_src_uri_get_protocols;
2523 iface->get_uri = gst_soup_http_src_uri_get_uri;
2524 iface->set_uri = gst_soup_http_src_uri_set_uri;