2 * Copyright (C) 2011 Andoni Morales Alastruey <ylatuya@gmail.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
26 #include "gstfragment.h"
27 #include "gsturidownloader.h"
28 #include "gsturidownloader_debug.h"
30 #define GST_CAT_DEFAULT uridownloader_debug
31 GST_DEBUG_CATEGORY (uridownloader_debug);
33 struct _GstUriDownloaderPrivate
35 /* Fragments fetcher */
40 GstFragment *download;
42 GMutex download_lock; /* used to restrict to one download only */
52 static void gst_uri_downloader_finalize (GObject * object);
53 static void gst_uri_downloader_dispose (GObject * object);
55 static GstFlowReturn gst_uri_downloader_chain (GstPad * pad, GstObject * parent,
57 static gboolean gst_uri_downloader_sink_event (GstPad * pad, GstObject * parent,
59 static GstBusSyncReply gst_uri_downloader_bus_handler (GstBus * bus,
60 GstMessage * message, gpointer data);
62 static gboolean gst_uri_downloader_ensure_src (GstUriDownloader * downloader,
64 static void gst_uri_downloader_destroy_src (GstUriDownloader * downloader);
66 static GstStaticPadTemplate sinkpadtemplate = GST_STATIC_PAD_TEMPLATE ("sink",
73 GST_DEBUG_CATEGORY_INIT (uridownloader_debug, "uridownloader", 0, "URI downloader"); \
76 G_DEFINE_TYPE_WITH_CODE (GstUriDownloader, gst_uri_downloader, GST_TYPE_OBJECT,
77 G_ADD_PRIVATE (GstUriDownloader)
81 gst_uri_downloader_class_init (GstUriDownloaderClass * klass)
83 GObjectClass *gobject_class;
85 gobject_class = (GObjectClass *) klass;
87 gobject_class->dispose = gst_uri_downloader_dispose;
88 gobject_class->finalize = gst_uri_downloader_finalize;
92 gst_uri_downloader_init (GstUriDownloader * downloader)
94 downloader->priv = gst_uri_downloader_get_instance_private (downloader);
96 /* Initialize the sink pad. This pad will be connected to the src pad of the
97 * element created with gst_element_make_from_uri and will handle the download */
98 downloader->priv->pad =
99 gst_pad_new_from_static_template (&sinkpadtemplate, "sink");
100 gst_pad_set_chain_function (downloader->priv->pad,
101 GST_DEBUG_FUNCPTR (gst_uri_downloader_chain));
102 gst_pad_set_event_function (downloader->priv->pad,
103 GST_DEBUG_FUNCPTR (gst_uri_downloader_sink_event));
104 gst_pad_set_element_private (downloader->priv->pad, downloader);
105 gst_pad_set_active (downloader->priv->pad, TRUE);
107 /* Create a bus to handle error and warning message from the source element */
108 downloader->priv->bus = gst_bus_new ();
110 g_mutex_init (&downloader->priv->download_lock);
111 g_cond_init (&downloader->priv->cond);
115 gst_uri_downloader_dispose (GObject * object)
117 GstUriDownloader *downloader = GST_URI_DOWNLOADER (object);
119 gst_uri_downloader_destroy_src (downloader);
121 if (downloader->priv->bus != NULL) {
122 gst_object_unref (downloader->priv->bus);
123 downloader->priv->bus = NULL;
126 if (downloader->priv->pad) {
127 gst_object_unref (downloader->priv->pad);
128 downloader->priv->pad = NULL;
131 if (downloader->priv->download) {
132 g_object_unref (downloader->priv->download);
133 downloader->priv->download = NULL;
136 g_weak_ref_clear (&downloader->priv->parent);
138 G_OBJECT_CLASS (gst_uri_downloader_parent_class)->dispose (object);
142 gst_uri_downloader_finalize (GObject * object)
144 GstUriDownloader *downloader = GST_URI_DOWNLOADER (object);
146 g_mutex_clear (&downloader->priv->download_lock);
147 g_cond_clear (&downloader->priv->cond);
149 G_OBJECT_CLASS (gst_uri_downloader_parent_class)->finalize (object);
153 gst_uri_downloader_new (void)
155 GstUriDownloader *downloader;
157 downloader = g_object_new (GST_TYPE_URI_DOWNLOADER, NULL);
158 gst_object_ref_sink (downloader);
164 * gst_uri_downloader_set_parent:
165 * @param downloader: the #GstUriDownloader
166 * @param parent: the parent #GstElement
168 * Sets an element as parent of this #GstUriDownloader so that context
169 * requests from the underlying source are proxied to the main pipeline
170 * and set back if a context was provided.
173 gst_uri_downloader_set_parent (GstUriDownloader * downloader,
176 g_weak_ref_set (&downloader->priv->parent, parent);
180 gst_uri_downloader_sink_event (GstPad * pad, GstObject * parent,
183 gboolean ret = FALSE;
184 GstUriDownloader *downloader;
186 downloader = GST_URI_DOWNLOADER (gst_pad_get_element_private (pad));
188 switch (event->type) {
190 GST_OBJECT_LOCK (downloader);
191 GST_DEBUG_OBJECT (downloader, "Got EOS on the fetcher pad");
192 if (downloader->priv->download != NULL) {
193 /* signal we have fetched the URI */
194 downloader->priv->download->completed = TRUE;
195 downloader->priv->download->download_stop_time =
196 gst_util_get_timestamp ();
197 GST_DEBUG_OBJECT (downloader, "Signaling chain funtion");
198 g_cond_signal (&downloader->priv->cond);
200 GST_OBJECT_UNLOCK (downloader);
201 gst_event_unref (event);
204 case GST_EVENT_CUSTOM_DOWNSTREAM_STICKY:{
205 const GstStructure *str;
206 str = gst_event_get_structure (event);
207 if (gst_structure_has_name (str, "http-headers")) {
208 GST_OBJECT_LOCK (downloader);
209 if (downloader->priv->download != NULL) {
210 if (downloader->priv->download->headers)
211 gst_structure_free (downloader->priv->download->headers);
212 downloader->priv->download->headers = gst_structure_copy (str);
214 GST_OBJECT_UNLOCK (downloader);
219 ret = gst_pad_event_default (pad, parent, event);
226 static GstBusSyncReply
227 gst_uri_downloader_bus_handler (GstBus * bus,
228 GstMessage * message, gpointer data)
230 GstUriDownloader *downloader = (GstUriDownloader *) (data);
232 if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR) {
234 gchar *dbg_info = NULL;
235 gchar *new_error = NULL;
237 gst_message_parse_error (message, &err, &dbg_info);
238 GST_WARNING_OBJECT (downloader,
239 "Received error: %s from %s, the download will be cancelled",
240 err->message, GST_OBJECT_NAME (message->src));
241 GST_DEBUG ("Debugging info: %s\n", (dbg_info) ? dbg_info : "none");
244 new_error = g_strdup_printf ("%s: %s\n", err->message, dbg_info);
246 g_free (err->message);
247 err->message = new_error;
250 if (!downloader->priv->err)
251 downloader->priv->err = err;
257 /* remove the sync handler to avoid duplicated messages */
258 gst_bus_set_sync_handler (downloader->priv->bus, NULL, NULL, NULL);
260 /* stop the download */
261 GST_OBJECT_LOCK (downloader);
262 if (downloader->priv->download != NULL) {
263 GST_DEBUG_OBJECT (downloader, "Stopping download");
264 g_object_unref (downloader->priv->download);
265 downloader->priv->download = NULL;
266 downloader->priv->cancelled = TRUE;
267 g_cond_signal (&downloader->priv->cond);
269 GST_OBJECT_UNLOCK (downloader);
270 } else if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_WARNING) {
272 gchar *dbg_info = NULL;
274 gst_message_parse_warning (message, &err, &dbg_info);
275 GST_WARNING_OBJECT (downloader,
276 "Received warning: %s from %s",
277 GST_OBJECT_NAME (message->src), err->message);
278 GST_DEBUG ("Debugging info: %s\n", (dbg_info) ? dbg_info : "none");
281 } else if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_NEED_CONTEXT) {
282 GstElement *parent = g_weak_ref_get (&downloader->priv->parent);
284 /* post the same need-context as if it was from the parent and then
285 * get it to our internal element that requested it */
286 if (parent && GST_IS_ELEMENT (GST_MESSAGE_SRC (message))) {
287 const gchar *context_type;
289 GstElement *msg_src = GST_ELEMENT_CAST (GST_MESSAGE_SRC (message));
291 gst_message_parse_context_type (message, &context_type);
292 context = gst_element_get_context (parent, context_type);
294 /* No context, request one */
296 GstMessage *need_context_msg =
297 gst_message_new_need_context (GST_OBJECT_CAST (parent),
299 gst_element_post_message (parent, need_context_msg);
300 context = gst_element_get_context (parent, context_type);
304 gst_element_set_context (msg_src, context);
305 gst_context_unref (context);
309 gst_object_unref (parent);
312 gst_message_unref (message);
317 gst_uri_downloader_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
319 GstUriDownloader *downloader;
321 downloader = GST_URI_DOWNLOADER (gst_pad_get_element_private (pad));
323 /* HTML errors (404, 500, etc...) are also pushed through this pad as
324 * response but the source element will also post a warning or error message
325 * in the bus, which is handled synchronously cancelling the download.
327 GST_OBJECT_LOCK (downloader);
328 if (downloader->priv->download == NULL) {
329 /* Download cancelled, quit */
330 gst_buffer_unref (buf);
331 GST_OBJECT_UNLOCK (downloader);
335 GST_LOG_OBJECT (downloader, "The uri fetcher received a new buffer "
336 "of size %" G_GSIZE_FORMAT, gst_buffer_get_size (buf));
337 downloader->priv->got_buffer = TRUE;
338 if (!gst_fragment_add_buffer (downloader->priv->download, buf)) {
339 GST_WARNING_OBJECT (downloader, "Could not add buffer to fragment");
340 gst_buffer_unref (buf);
342 GST_OBJECT_UNLOCK (downloader);
351 gst_uri_downloader_reset (GstUriDownloader * downloader)
353 g_return_if_fail (downloader != NULL);
355 GST_OBJECT_LOCK (downloader);
356 downloader->priv->cancelled = FALSE;
357 GST_OBJECT_UNLOCK (downloader);
361 gst_uri_downloader_cancel (GstUriDownloader * downloader)
363 GST_OBJECT_LOCK (downloader);
364 if (downloader->priv->download != NULL) {
365 GST_DEBUG_OBJECT (downloader, "Cancelling download");
366 g_object_unref (downloader->priv->download);
367 downloader->priv->download = NULL;
368 downloader->priv->cancelled = TRUE;
369 GST_DEBUG_OBJECT (downloader, "Signaling chain funtion");
370 g_cond_signal (&downloader->priv->cond);
374 cancelled = downloader->priv->cancelled;
375 downloader->priv->cancelled = TRUE;
377 GST_DEBUG_OBJECT (downloader,
378 "Trying to cancel a download that was alredy cancelled");
380 GST_OBJECT_UNLOCK (downloader);
384 gst_uri_downloader_set_range (GstUriDownloader * downloader,
385 gint64 range_start, gint64 range_end)
387 g_return_val_if_fail (range_start >= 0, FALSE);
388 g_return_val_if_fail (range_end >= -1, FALSE);
390 if (range_start || (range_end >= 0)) {
393 seek = gst_event_new_seek (1.0, GST_FORMAT_BYTES, GST_SEEK_FLAG_FLUSH,
394 GST_SEEK_TYPE_SET, range_start, GST_SEEK_TYPE_SET, range_end);
396 return gst_element_send_event (downloader->priv->urisrc, seek);
402 gst_uri_downloader_ensure_src (GstUriDownloader * downloader, const gchar * uri)
404 if (downloader->priv->urisrc) {
405 gchar *old_protocol, *new_protocol;
409 gst_uri_handler_get_uri (GST_URI_HANDLER (downloader->priv->urisrc));
410 old_protocol = gst_uri_get_protocol (old_uri);
411 new_protocol = gst_uri_get_protocol (uri);
413 if (!g_str_equal (old_protocol, new_protocol)) {
414 gst_uri_downloader_destroy_src (downloader);
415 GST_DEBUG_OBJECT (downloader, "Can't re-use old source element");
419 GST_DEBUG_OBJECT (downloader, "Re-using old source element");
420 if (!gst_uri_handler_set_uri
421 (GST_URI_HANDLER (downloader->priv->urisrc), uri, &err)) {
422 GST_DEBUG_OBJECT (downloader,
423 "Failed to re-use old source element: %s", err->message);
424 g_clear_error (&err);
425 gst_uri_downloader_destroy_src (downloader);
429 g_free (old_protocol);
430 g_free (new_protocol);
433 if (!downloader->priv->urisrc) {
434 GST_DEBUG_OBJECT (downloader, "Creating source element for the URI:%s",
436 downloader->priv->urisrc =
437 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
438 if (downloader->priv->urisrc) {
439 /* gst_element_make_from_uri returns a floating reference
440 * and we are not going to transfer the ownership, so we
443 gst_object_ref_sink (downloader->priv->urisrc);
447 return downloader->priv->urisrc != NULL;
451 gst_uri_downloader_destroy_src (GstUriDownloader * downloader)
453 if (!downloader->priv->urisrc)
456 gst_element_set_state (downloader->priv->urisrc, GST_STATE_NULL);
457 gst_object_unref (downloader->priv->urisrc);
458 downloader->priv->urisrc = NULL;
462 gst_uri_downloader_set_uri (GstUriDownloader * downloader, const gchar * uri,
463 const gchar * referer, gboolean compress,
464 gboolean refresh, gboolean allow_cache)
467 GObjectClass *gobject_class;
469 if (!gst_uri_is_valid (uri))
472 if (!gst_uri_downloader_ensure_src (downloader, uri))
475 gobject_class = G_OBJECT_GET_CLASS (downloader->priv->urisrc);
476 if (g_object_class_find_property (gobject_class, "compress"))
477 g_object_set (downloader->priv->urisrc, "compress", compress, NULL);
478 if (g_object_class_find_property (gobject_class, "keep-alive"))
479 g_object_set (downloader->priv->urisrc, "keep-alive", TRUE, NULL);
480 if (g_object_class_find_property (gobject_class, "extra-headers")) {
481 if (referer || refresh || !allow_cache) {
482 GstStructure *extra_headers = gst_structure_new_empty ("headers");
485 gst_structure_set (extra_headers, "Referer", G_TYPE_STRING, referer,
489 gst_structure_set (extra_headers, "Cache-Control", G_TYPE_STRING,
492 gst_structure_set (extra_headers, "Cache-Control", G_TYPE_STRING,
495 g_object_set (downloader->priv->urisrc, "extra-headers", extra_headers,
498 gst_structure_free (extra_headers);
500 g_object_set (downloader->priv->urisrc, "extra-headers", NULL, NULL);
504 /* add a sync handler for the bus messages to detect errors in the download */
505 gst_element_set_bus (GST_ELEMENT (downloader->priv->urisrc),
506 downloader->priv->bus);
507 gst_bus_set_sync_handler (downloader->priv->bus,
508 gst_uri_downloader_bus_handler, downloader, NULL);
510 pad = gst_element_get_static_pad (downloader->priv->urisrc, "src");
513 gst_pad_link (pad, downloader->priv->pad);
514 gst_object_unref (pad);
519 gst_uri_downloader_set_method (GstUriDownloader * downloader,
520 const gchar * method)
522 GObjectClass *gobject_class;
524 if (!downloader->priv->urisrc)
527 gobject_class = G_OBJECT_GET_CLASS (downloader->priv->urisrc);
528 if (g_object_class_find_property (gobject_class, "method")) {
529 g_object_set (downloader->priv->urisrc, "method", method, NULL);
536 gst_uri_downloader_fetch_uri (GstUriDownloader * downloader,
537 const gchar * uri, const gchar * referer, gboolean compress,
538 gboolean refresh, gboolean allow_cache, GError ** err)
540 return gst_uri_downloader_fetch_uri_with_range (downloader, uri,
541 referer, compress, refresh, allow_cache, 0, -1, err);
545 * gst_uri_downloader_fetch_uri_with_range:
546 * @downloader: the #GstUriDownloader
548 * @range_start: the starting byte index
549 * @range_end: the final byte index, use -1 for unspecified
551 * Returns the downloaded #GstFragment
554 gst_uri_downloader_fetch_uri_with_range (GstUriDownloader *
555 downloader, const gchar * uri, const gchar * referer, gboolean compress,
556 gboolean refresh, gboolean allow_cache,
557 gint64 range_start, gint64 range_end, GError ** err)
559 GstStateChangeReturn ret;
560 GstFragment *download = NULL;
562 GST_DEBUG_OBJECT (downloader, "Fetching URI %s", uri);
564 g_mutex_lock (&downloader->priv->download_lock);
565 downloader->priv->err = NULL;
566 downloader->priv->got_buffer = FALSE;
568 GST_OBJECT_LOCK (downloader);
569 if (downloader->priv->cancelled) {
570 GST_DEBUG_OBJECT (downloader, "Cancelled, aborting fetch");
574 if (!gst_uri_downloader_set_uri (downloader, uri, referer, compress, refresh,
576 GST_WARNING_OBJECT (downloader, "Failed to set URI");
580 gst_bus_set_flushing (downloader->priv->bus, FALSE);
581 if (downloader->priv->download)
582 g_object_unref (downloader->priv->download);
583 downloader->priv->download = gst_fragment_new ();
584 downloader->priv->download->range_start = range_start;
585 downloader->priv->download->range_end = range_end;
586 GST_OBJECT_UNLOCK (downloader);
587 ret = gst_element_set_state (downloader->priv->urisrc, GST_STATE_READY);
588 GST_OBJECT_LOCK (downloader);
589 if (ret == GST_STATE_CHANGE_FAILURE || downloader->priv->download == NULL) {
590 GST_WARNING_OBJECT (downloader, "Failed to set src to READY");
594 /* might have been cancelled because of failures in state change */
595 if (downloader->priv->cancelled) {
599 if (range_start < 0 && range_end < 0) {
600 if (!gst_uri_downloader_set_method (downloader, "HEAD")) {
601 GST_WARNING_OBJECT (downloader, "Failed to set HTTP method");
605 if (!gst_uri_downloader_set_range (downloader, range_start, range_end)) {
606 GST_WARNING_OBJECT (downloader, "Failed to set range");
611 GST_OBJECT_UNLOCK (downloader);
612 ret = gst_element_set_state (downloader->priv->urisrc, GST_STATE_PLAYING);
613 GST_OBJECT_LOCK (downloader);
614 if (ret == GST_STATE_CHANGE_FAILURE) {
615 if (downloader->priv->download) {
616 g_object_unref (downloader->priv->download);
617 downloader->priv->download = NULL;
622 /* might have been cancelled because of failures in state change */
623 if (downloader->priv->cancelled) {
628 * - the download succeed (EOS in the src pad)
629 * - the download failed (Error message on the fetcher bus)
630 * - the download was canceled
632 GST_DEBUG_OBJECT (downloader, "Waiting to fetch the URI %s", uri);
633 while (!downloader->priv->cancelled && !downloader->priv->download->completed)
634 g_cond_wait (&downloader->priv->cond, GST_OBJECT_GET_LOCK (downloader));
636 if (downloader->priv->cancelled) {
637 if (downloader->priv->download) {
638 g_object_unref (downloader->priv->download);
639 downloader->priv->download = NULL;
644 download = downloader->priv->download;
645 downloader->priv->download = NULL;
646 if (!downloader->priv->got_buffer) {
647 if (download->range_start < 0 && download->range_end < 0) {
648 /* HEAD request, so we don't expect a response */
650 g_object_unref (download);
652 GST_ERROR_OBJECT (downloader, "Didn't retrieve a buffer before EOS");
656 if (download != NULL)
657 GST_INFO_OBJECT (downloader, "URI fetched successfully");
659 GST_INFO_OBJECT (downloader, "Error fetching URI");
663 if (downloader->priv->urisrc) {
667 urisrc = downloader->priv->urisrc;
669 GST_DEBUG_OBJECT (downloader, "Stopping source element %s",
670 GST_ELEMENT_NAME (urisrc));
672 /* remove the bus' sync handler */
673 gst_bus_set_sync_handler (downloader->priv->bus, NULL, NULL, NULL);
674 gst_bus_set_flushing (downloader->priv->bus, TRUE);
676 /* set the element state to NULL */
677 GST_OBJECT_UNLOCK (downloader);
678 if (download == NULL) {
679 gst_element_set_state (urisrc, GST_STATE_NULL);
683 /* Download successful, let's query the URI */
684 query = gst_query_new_uri ();
685 if (gst_element_query (urisrc, query)) {
686 gst_query_parse_uri (query, &download->uri);
687 gst_query_parse_uri_redirection (query, &download->redirect_uri);
688 gst_query_parse_uri_redirection_permanent (query,
689 &download->redirect_permanent);
691 gst_query_unref (query);
692 gst_element_set_state (urisrc, GST_STATE_READY);
694 GST_OBJECT_LOCK (downloader);
695 gst_element_set_bus (urisrc, NULL);
697 /* unlink the source element from the internal pad */
698 pad = gst_pad_get_peer (downloader->priv->pad);
700 gst_pad_unlink (pad, downloader->priv->pad);
701 gst_object_unref (pad);
704 GST_OBJECT_UNLOCK (downloader);
706 if (download == NULL) {
707 if (!downloader->priv->err) {
708 g_set_error (err, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_OPEN_READ,
709 "Failed to download '%s'", uri);
711 g_propagate_error (err, downloader->priv->err);
712 downloader->priv->err = NULL;
716 downloader->priv->cancelled = FALSE;
718 g_mutex_unlock (&downloader->priv->download_lock);