documentation: fixed a heap o' typos
[platform/upstream/gstreamer.git] / gst-libs / gst / uridownloader / gsturidownloader.c
1 /* GStreamer
2  * Copyright (C) 2011 Andoni Morales Alastruey <ylatuya@gmail.com>
3  *
4  * gstfragment.c:
5  *
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.
10  *
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.
15  *
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.
20  */
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <glib.h>
26 #include "gstfragment.h"
27 #include "gsturidownloader.h"
28 #include "gsturidownloader_debug.h"
29
30 #define GST_CAT_DEFAULT uridownloader_debug
31 GST_DEBUG_CATEGORY (uridownloader_debug);
32
33 struct _GstUriDownloaderPrivate
34 {
35   /* Fragments fetcher */
36   GstElement *urisrc;
37   GstBus *bus;
38   GstPad *pad;
39   GTimeVal *timeout;
40   GstFragment *download;
41   gboolean got_buffer;
42   GMutex download_lock;         /* used to restrict to one download only */
43
44   GWeakRef parent;
45
46   GError *err;
47
48   GCond cond;
49   gboolean cancelled;
50 };
51
52 static void gst_uri_downloader_finalize (GObject * object);
53 static void gst_uri_downloader_dispose (GObject * object);
54
55 static GstFlowReturn gst_uri_downloader_chain (GstPad * pad, GstObject * parent,
56     GstBuffer * buf);
57 static gboolean gst_uri_downloader_sink_event (GstPad * pad, GstObject * parent,
58     GstEvent * event);
59 static GstBusSyncReply gst_uri_downloader_bus_handler (GstBus * bus,
60     GstMessage * message, gpointer data);
61
62 static gboolean gst_uri_downloader_ensure_src (GstUriDownloader * downloader,
63     const gchar * uri);
64 static void gst_uri_downloader_destroy_src (GstUriDownloader * downloader);
65
66 static GstStaticPadTemplate sinkpadtemplate = GST_STATIC_PAD_TEMPLATE ("sink",
67     GST_PAD_SINK,
68     GST_PAD_ALWAYS,
69     GST_STATIC_CAPS_ANY);
70
71 #define _do_init \
72 { \
73   GST_DEBUG_CATEGORY_INIT (uridownloader_debug, "uridownloader", 0, "URI downloader"); \
74 }
75
76 G_DEFINE_TYPE_WITH_CODE (GstUriDownloader, gst_uri_downloader, GST_TYPE_OBJECT,
77     G_ADD_PRIVATE (GstUriDownloader)
78     _do_init);
79
80 static void
81 gst_uri_downloader_class_init (GstUriDownloaderClass * klass)
82 {
83   GObjectClass *gobject_class;
84
85   gobject_class = (GObjectClass *) klass;
86
87   gobject_class->dispose = gst_uri_downloader_dispose;
88   gobject_class->finalize = gst_uri_downloader_finalize;
89 }
90
91 static void
92 gst_uri_downloader_init (GstUriDownloader * downloader)
93 {
94   downloader->priv = gst_uri_downloader_get_instance_private (downloader);
95
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);
106
107   /* Create a bus to handle error and warning message from the source element */
108   downloader->priv->bus = gst_bus_new ();
109
110   g_mutex_init (&downloader->priv->download_lock);
111   g_cond_init (&downloader->priv->cond);
112 }
113
114 static void
115 gst_uri_downloader_dispose (GObject * object)
116 {
117   GstUriDownloader *downloader = GST_URI_DOWNLOADER (object);
118
119   gst_uri_downloader_destroy_src (downloader);
120
121   if (downloader->priv->bus != NULL) {
122     gst_object_unref (downloader->priv->bus);
123     downloader->priv->bus = NULL;
124   }
125
126   if (downloader->priv->pad) {
127     gst_object_unref (downloader->priv->pad);
128     downloader->priv->pad = NULL;
129   }
130
131   if (downloader->priv->download) {
132     g_object_unref (downloader->priv->download);
133     downloader->priv->download = NULL;
134   }
135
136   g_weak_ref_clear (&downloader->priv->parent);
137
138   G_OBJECT_CLASS (gst_uri_downloader_parent_class)->dispose (object);
139 }
140
141 static void
142 gst_uri_downloader_finalize (GObject * object)
143 {
144   GstUriDownloader *downloader = GST_URI_DOWNLOADER (object);
145
146   g_mutex_clear (&downloader->priv->download_lock);
147   g_cond_clear (&downloader->priv->cond);
148
149   G_OBJECT_CLASS (gst_uri_downloader_parent_class)->finalize (object);
150 }
151
152 GstUriDownloader *
153 gst_uri_downloader_new (void)
154 {
155   GstUriDownloader *downloader;
156
157   downloader = g_object_new (GST_TYPE_URI_DOWNLOADER, NULL);
158   gst_object_ref_sink (downloader);
159
160   return downloader;
161 }
162
163 /**
164  * gst_uri_downloader_set_parent:
165  * @param downloader: the #GstUriDownloader
166  * @param parent: the parent #GstElement
167  *
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.
171  */
172 void
173 gst_uri_downloader_set_parent (GstUriDownloader * downloader,
174     GstElement * parent)
175 {
176   g_weak_ref_set (&downloader->priv->parent, parent);
177 }
178
179 static gboolean
180 gst_uri_downloader_sink_event (GstPad * pad, GstObject * parent,
181     GstEvent * event)
182 {
183   gboolean ret = FALSE;
184   GstUriDownloader *downloader;
185
186   downloader = GST_URI_DOWNLOADER (gst_pad_get_element_private (pad));
187
188   switch (event->type) {
189     case GST_EVENT_EOS:{
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);
199       }
200       GST_OBJECT_UNLOCK (downloader);
201       gst_event_unref (event);
202       break;
203     }
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);
213         }
214         GST_OBJECT_UNLOCK (downloader);
215       }
216     }
217       /* falls through */
218     default:
219       ret = gst_pad_event_default (pad, parent, event);
220       break;
221   }
222
223   return ret;
224 }
225
226 static GstBusSyncReply
227 gst_uri_downloader_bus_handler (GstBus * bus,
228     GstMessage * message, gpointer data)
229 {
230   GstUriDownloader *downloader = (GstUriDownloader *) (data);
231
232   if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR) {
233     GError *err = NULL;
234     gchar *dbg_info = NULL;
235     gchar *new_error = NULL;
236
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");
242
243     if (dbg_info)
244       new_error = g_strdup_printf ("%s: %s\n", err->message, dbg_info);
245     if (new_error) {
246       g_free (err->message);
247       err->message = new_error;
248     }
249
250     if (!downloader->priv->err)
251       downloader->priv->err = err;
252     else
253       g_error_free (err);
254
255     g_free (dbg_info);
256
257     /* remove the sync handler to avoid duplicated messages */
258     gst_bus_set_sync_handler (downloader->priv->bus, NULL, NULL, NULL);
259
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);
268     }
269     GST_OBJECT_UNLOCK (downloader);
270   } else if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_WARNING) {
271     GError *err = NULL;
272     gchar *dbg_info = NULL;
273
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");
279     g_error_free (err);
280     g_free (dbg_info);
281   } else if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_NEED_CONTEXT) {
282     GstElement *parent = g_weak_ref_get (&downloader->priv->parent);
283
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;
288       GstContext *context;
289       GstElement *msg_src = GST_ELEMENT_CAST (GST_MESSAGE_SRC (message));
290
291       gst_message_parse_context_type (message, &context_type);
292       context = gst_element_get_context (parent, context_type);
293
294       /* No context, request one */
295       if (!context) {
296         GstMessage *need_context_msg =
297             gst_message_new_need_context (GST_OBJECT_CAST (parent),
298             context_type);
299         gst_element_post_message (parent, need_context_msg);
300         context = gst_element_get_context (parent, context_type);
301       }
302
303       if (context) {
304         gst_element_set_context (msg_src, context);
305         gst_context_unref (context);
306       }
307     }
308     if (parent)
309       gst_object_unref (parent);
310   }
311
312   gst_message_unref (message);
313   return GST_BUS_DROP;
314 }
315
316 static GstFlowReturn
317 gst_uri_downloader_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
318 {
319   GstUriDownloader *downloader;
320
321   downloader = GST_URI_DOWNLOADER (gst_pad_get_element_private (pad));
322
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.
326    */
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);
332     goto done;
333   }
334
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);
341   }
342   GST_OBJECT_UNLOCK (downloader);
343
344 done:
345   {
346     return GST_FLOW_OK;
347   }
348 }
349
350 void
351 gst_uri_downloader_reset (GstUriDownloader * downloader)
352 {
353   g_return_if_fail (downloader != NULL);
354
355   GST_OBJECT_LOCK (downloader);
356   downloader->priv->cancelled = FALSE;
357   GST_OBJECT_UNLOCK (downloader);
358 }
359
360 void
361 gst_uri_downloader_cancel (GstUriDownloader * downloader)
362 {
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);
371   } else {
372     gboolean cancelled;
373
374     cancelled = downloader->priv->cancelled;
375     downloader->priv->cancelled = TRUE;
376     if (cancelled)
377       GST_DEBUG_OBJECT (downloader,
378           "Trying to cancel a download that was alredy cancelled");
379   }
380   GST_OBJECT_UNLOCK (downloader);
381 }
382
383 static gboolean
384 gst_uri_downloader_set_range (GstUriDownloader * downloader,
385     gint64 range_start, gint64 range_end)
386 {
387   g_return_val_if_fail (range_start >= 0, FALSE);
388   g_return_val_if_fail (range_end >= -1, FALSE);
389
390   if (range_start || (range_end >= 0)) {
391     GstEvent *seek;
392
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);
395
396     return gst_element_send_event (downloader->priv->urisrc, seek);
397   }
398   return TRUE;
399 }
400
401 static gboolean
402 gst_uri_downloader_ensure_src (GstUriDownloader * downloader, const gchar * uri)
403 {
404   if (downloader->priv->urisrc) {
405     gchar *old_protocol, *new_protocol;
406     gchar *old_uri;
407
408     old_uri =
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);
412
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");
416     } else {
417       GError *err = NULL;
418
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);
426       }
427     }
428     g_free (old_uri);
429     g_free (old_protocol);
430     g_free (new_protocol);
431   }
432
433   if (!downloader->priv->urisrc) {
434     GST_DEBUG_OBJECT (downloader, "Creating source element for the URI:%s",
435         uri);
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
441        * should take it.
442        */
443       gst_object_ref_sink (downloader->priv->urisrc);
444     }
445   }
446
447   return downloader->priv->urisrc != NULL;
448 }
449
450 static void
451 gst_uri_downloader_destroy_src (GstUriDownloader * downloader)
452 {
453   if (!downloader->priv->urisrc)
454     return;
455
456   gst_element_set_state (downloader->priv->urisrc, GST_STATE_NULL);
457   gst_object_unref (downloader->priv->urisrc);
458   downloader->priv->urisrc = NULL;
459 }
460
461 static gboolean
462 gst_uri_downloader_set_uri (GstUriDownloader * downloader, const gchar * uri,
463     const gchar * referer, gboolean compress,
464     gboolean refresh, gboolean allow_cache)
465 {
466   GstPad *pad;
467   GObjectClass *gobject_class;
468
469   if (!gst_uri_is_valid (uri))
470     return FALSE;
471
472   if (!gst_uri_downloader_ensure_src (downloader, uri))
473     return FALSE;
474
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");
483
484       if (referer)
485         gst_structure_set (extra_headers, "Referer", G_TYPE_STRING, referer,
486             NULL);
487
488       if (!allow_cache)
489         gst_structure_set (extra_headers, "Cache-Control", G_TYPE_STRING,
490             "no-cache", NULL);
491       else if (refresh)
492         gst_structure_set (extra_headers, "Cache-Control", G_TYPE_STRING,
493             "max-age=0", NULL);
494
495       g_object_set (downloader->priv->urisrc, "extra-headers", extra_headers,
496           NULL);
497
498       gst_structure_free (extra_headers);
499     } else {
500       g_object_set (downloader->priv->urisrc, "extra-headers", NULL, NULL);
501     }
502   }
503
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);
509
510   pad = gst_element_get_static_pad (downloader->priv->urisrc, "src");
511   if (!pad)
512     return FALSE;
513   gst_pad_link (pad, downloader->priv->pad);
514   gst_object_unref (pad);
515   return TRUE;
516 }
517
518 static gboolean
519 gst_uri_downloader_set_method (GstUriDownloader * downloader,
520     const gchar * method)
521 {
522   GObjectClass *gobject_class;
523
524   if (!downloader->priv->urisrc)
525     return FALSE;
526
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);
530     return TRUE;
531   }
532   return FALSE;
533 }
534
535 GstFragment *
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)
539 {
540   return gst_uri_downloader_fetch_uri_with_range (downloader, uri,
541       referer, compress, refresh, allow_cache, 0, -1, err);
542 }
543
544 /**
545  * gst_uri_downloader_fetch_uri_with_range:
546  * @downloader: the #GstUriDownloader
547  * @uri: the uri
548  * @range_start: the starting byte index
549  * @range_end: the final byte index, use -1 for unspecified
550  *
551  * Returns the downloaded #GstFragment
552  */
553 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)
558 {
559   GstStateChangeReturn ret;
560   GstFragment *download = NULL;
561
562   GST_DEBUG_OBJECT (downloader, "Fetching URI %s", uri);
563
564   g_mutex_lock (&downloader->priv->download_lock);
565   downloader->priv->err = NULL;
566   downloader->priv->got_buffer = FALSE;
567
568   GST_OBJECT_LOCK (downloader);
569   if (downloader->priv->cancelled) {
570     GST_DEBUG_OBJECT (downloader, "Cancelled, aborting fetch");
571     goto quit;
572   }
573
574   if (!gst_uri_downloader_set_uri (downloader, uri, referer, compress, refresh,
575           allow_cache)) {
576     GST_WARNING_OBJECT (downloader, "Failed to set URI");
577     goto quit;
578   }
579
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");
591     goto quit;
592   }
593
594   /* might have been cancelled because of failures in state change */
595   if (downloader->priv->cancelled) {
596     goto quit;
597   }
598
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");
602       goto quit;
603     }
604   } else {
605     if (!gst_uri_downloader_set_range (downloader, range_start, range_end)) {
606       GST_WARNING_OBJECT (downloader, "Failed to set range");
607       goto quit;
608     }
609   }
610
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;
618     }
619     goto quit;
620   }
621
622   /* might have been cancelled because of failures in state change */
623   if (downloader->priv->cancelled) {
624     goto quit;
625   }
626
627   /* wait until:
628    *   - the download succeed (EOS in the src pad)
629    *   - the download failed (Error message on the fetcher bus)
630    *   - the download was canceled
631    */
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));
635
636   if (downloader->priv->cancelled) {
637     if (downloader->priv->download) {
638       g_object_unref (downloader->priv->download);
639       downloader->priv->download = NULL;
640     }
641     goto quit;
642   }
643
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 */
649     } else {
650       g_object_unref (download);
651       download = NULL;
652       GST_ERROR_OBJECT (downloader, "Didn't retrieve a buffer before EOS");
653     }
654   }
655
656   if (download != NULL)
657     GST_INFO_OBJECT (downloader, "URI fetched successfully");
658   else
659     GST_INFO_OBJECT (downloader, "Error fetching URI");
660
661 quit:
662   {
663     if (downloader->priv->urisrc) {
664       GstPad *pad;
665       GstElement *urisrc;
666
667       urisrc = downloader->priv->urisrc;
668
669       GST_DEBUG_OBJECT (downloader, "Stopping source element %s",
670           GST_ELEMENT_NAME (urisrc));
671
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);
675
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);
680       } else {
681         GstQuery *query;
682
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);
690         }
691         gst_query_unref (query);
692         gst_element_set_state (urisrc, GST_STATE_READY);
693       }
694       GST_OBJECT_LOCK (downloader);
695       gst_element_set_bus (urisrc, NULL);
696
697       /* unlink the source element from the internal pad */
698       pad = gst_pad_get_peer (downloader->priv->pad);
699       if (pad) {
700         gst_pad_unlink (pad, downloader->priv->pad);
701         gst_object_unref (pad);
702       }
703     }
704     GST_OBJECT_UNLOCK (downloader);
705
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);
710       } else {
711         g_propagate_error (err, downloader->priv->err);
712         downloader->priv->err = NULL;
713       }
714     }
715
716     downloader->priv->cancelled = FALSE;
717
718     g_mutex_unlock (&downloader->priv->download_lock);
719     return download;
720   }
721 }