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.
23 #include "gstfragment.h"
24 #include "gsturidownloader.h"
25 #include "gsturidownloader_debug.h"
27 #define GST_CAT_DEFAULT uridownloader_debug
28 GST_DEBUG_CATEGORY (uridownloader_debug);
30 #define GST_URI_DOWNLOADER_GET_PRIVATE(obj) \
31 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
32 GST_TYPE_URI_DOWNLOADER, GstUriDownloaderPrivate))
34 struct _GstUriDownloaderPrivate
36 /* Fragments fetcher */
41 GstFragment *download;
46 static void gst_uri_downloader_finalize (GObject * object);
47 static void gst_uri_downloader_dispose (GObject * object);
49 static GstFlowReturn gst_uri_downloader_chain (GstPad * pad, GstObject * parent,
51 static gboolean gst_uri_downloader_sink_event (GstPad * pad, GstObject * parent,
53 static GstBusSyncReply gst_uri_downloader_bus_handler (GstBus * bus,
54 GstMessage * message, gpointer data);
56 static GstStaticPadTemplate sinkpadtemplate = GST_STATIC_PAD_TEMPLATE ("sink",
63 GST_DEBUG_CATEGORY_INIT (uridownloader_debug, "uridownloader", 0, "URI downloader"); \
66 G_DEFINE_TYPE_WITH_CODE (GstUriDownloader, gst_uri_downloader, GST_TYPE_OBJECT,
70 gst_uri_downloader_class_init (GstUriDownloaderClass * klass)
72 GObjectClass *gobject_class;
74 gobject_class = (GObjectClass *) klass;
76 g_type_class_add_private (klass, sizeof (GstUriDownloaderPrivate));
78 gobject_class->dispose = gst_uri_downloader_dispose;
79 gobject_class->finalize = gst_uri_downloader_finalize;
83 gst_uri_downloader_init (GstUriDownloader * downloader)
85 downloader->priv = GST_URI_DOWNLOADER_GET_PRIVATE (downloader);
87 /* Initialize the sink pad. This pad will be connected to the src pad of the
88 * element created with gst_element_make_from_uri and will handle the download */
89 downloader->priv->pad =
90 gst_pad_new_from_static_template (&sinkpadtemplate, "sink");
91 gst_pad_set_chain_function (downloader->priv->pad,
92 GST_DEBUG_FUNCPTR (gst_uri_downloader_chain));
93 gst_pad_set_event_function (downloader->priv->pad,
94 GST_DEBUG_FUNCPTR (gst_uri_downloader_sink_event));
95 gst_pad_set_element_private (downloader->priv->pad, downloader);
96 gst_pad_set_active (downloader->priv->pad, TRUE);
98 /* Create a bus to handle error and warning message from the source element */
99 downloader->priv->bus = gst_bus_new ();
101 g_mutex_init (&downloader->priv->lock);
102 g_cond_init (&downloader->priv->cond);
106 gst_uri_downloader_dispose (GObject * object)
108 GstUriDownloader *downloader = GST_URI_DOWNLOADER (object);
110 if (downloader->priv->urisrc != NULL) {
111 gst_object_unref (downloader->priv->urisrc);
112 downloader->priv->urisrc = NULL;
115 if (downloader->priv->bus != NULL) {
116 gst_object_unref (downloader->priv->bus);
117 downloader->priv->bus = NULL;
120 if (downloader->priv->pad) {
121 gst_object_unref (downloader->priv->pad);
122 downloader->priv->pad = NULL;
125 if (downloader->priv->download) {
126 g_object_unref (downloader->priv->download);
127 downloader->priv->download = NULL;
130 G_OBJECT_CLASS (gst_uri_downloader_parent_class)->dispose (object);
134 gst_uri_downloader_finalize (GObject * object)
136 GstUriDownloader *downloader = GST_URI_DOWNLOADER (object);
138 g_mutex_clear (&downloader->priv->lock);
139 g_cond_clear (&downloader->priv->cond);
141 G_OBJECT_CLASS (gst_uri_downloader_parent_class)->finalize (object);
145 gst_uri_downloader_new (void)
147 return g_object_new (GST_TYPE_URI_DOWNLOADER, NULL);
151 gst_uri_downloader_sink_event (GstPad * pad, GstObject * parent,
154 gboolean ret = FALSE;
155 GstUriDownloader *downloader;
157 downloader = GST_URI_DOWNLOADER (gst_pad_get_element_private (pad));
159 switch (event->type) {
161 GST_OBJECT_LOCK (downloader);
162 GST_DEBUG_OBJECT (downloader, "Got EOS on the fetcher pad");
163 if (downloader->priv->download != NULL) {
164 /* signal we have fetched the URI */
165 downloader->priv->download->completed = TRUE;
166 downloader->priv->download->download_stop_time =
167 gst_util_get_timestamp ();
168 GST_OBJECT_UNLOCK (downloader);
169 GST_DEBUG_OBJECT (downloader, "Signaling chain funtion");
170 g_mutex_lock (&downloader->priv->lock);
171 g_cond_signal (&downloader->priv->cond);
172 g_mutex_unlock (&downloader->priv->lock);
174 GST_OBJECT_UNLOCK (downloader);
176 gst_event_unref (event);
180 ret = gst_pad_event_default (pad, parent, event);
187 static GstBusSyncReply
188 gst_uri_downloader_bus_handler (GstBus * bus,
189 GstMessage * message, gpointer data)
191 GstUriDownloader *downloader = (GstUriDownloader *) (data);
193 if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR ||
194 GST_MESSAGE_TYPE (message) == GST_MESSAGE_WARNING) {
196 gchar *dbg_info = NULL;
198 gst_message_parse_error (message, &err, &dbg_info);
199 GST_WARNING_OBJECT (downloader,
200 "Received error: %s from %s, the download will be cancelled",
201 GST_OBJECT_NAME (message->src), err->message);
202 GST_DEBUG ("Debugging info: %s\n", (dbg_info) ? dbg_info : "none");
206 /* remove the sync handler to avoid duplicated messages */
207 gst_bus_set_sync_handler (downloader->priv->bus, NULL, NULL, NULL);
208 gst_uri_downloader_cancel (downloader);
211 gst_message_unref (message);
216 gst_uri_downloader_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
218 GstUriDownloader *downloader;
220 downloader = GST_URI_DOWNLOADER (gst_pad_get_element_private (pad));
222 /* HTML errors (404, 500, etc...) are also pushed through this pad as
223 * response but the source element will also post a warning or error message
224 * in the bus, which is handled synchronously cancelling the download.
226 GST_OBJECT_LOCK (downloader);
227 if (downloader->priv->download == NULL) {
228 /* Download cancelled, quit */
229 GST_OBJECT_UNLOCK (downloader);
233 GST_LOG_OBJECT (downloader, "The uri fetcher received a new buffer "
234 "of size %" G_GSIZE_FORMAT, gst_buffer_get_size (buf));
235 if (!gst_fragment_add_buffer (downloader->priv->download, buf))
236 GST_WARNING_OBJECT (downloader, "Could not add buffer to fragment");
237 GST_OBJECT_UNLOCK (downloader);
246 gst_uri_downloader_stop (GstUriDownloader * downloader)
250 GST_DEBUG_OBJECT (downloader, "Stopping source element");
252 /* remove the bus' sync handler */
253 gst_bus_set_sync_handler (downloader->priv->bus, NULL, NULL, NULL);
254 /* unlink the source element from the internal pad */
255 pad = gst_pad_get_peer (downloader->priv->pad);
257 gst_pad_unlink (pad, downloader->priv->pad);
258 gst_object_unref (pad);
260 /* set the element state to NULL */
261 gst_element_set_state (downloader->priv->urisrc, GST_STATE_NULL);
262 gst_element_get_state (downloader->priv->urisrc, NULL, NULL,
263 GST_CLOCK_TIME_NONE);
267 gst_uri_downloader_cancel (GstUriDownloader * downloader)
269 GST_OBJECT_LOCK (downloader);
270 if (downloader->priv->download != NULL) {
271 GST_DEBUG_OBJECT (downloader, "Cancelling download");
272 g_object_unref (downloader->priv->download);
273 downloader->priv->download = NULL;
274 GST_OBJECT_UNLOCK (downloader);
275 GST_DEBUG_OBJECT (downloader, "Signaling chain funtion");
276 g_mutex_lock (&downloader->priv->lock);
277 g_cond_signal (&downloader->priv->cond);
278 g_mutex_unlock (&downloader->priv->lock);
280 GST_OBJECT_UNLOCK (downloader);
281 GST_DEBUG_OBJECT (downloader,
282 "Trying to cancell a download that was alredy cancelled");
287 gst_uri_downloader_set_uri (GstUriDownloader * downloader, const gchar * uri)
291 if (!gst_uri_is_valid (uri))
294 GST_DEBUG_OBJECT (downloader, "Creating source element for the URI:%s", uri);
295 downloader->priv->urisrc =
296 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
297 if (!downloader->priv->urisrc)
300 /* add a sync handler for the bus messages to detect errors in the download */
301 gst_element_set_bus (GST_ELEMENT (downloader->priv->urisrc),
302 downloader->priv->bus);
303 gst_bus_set_sync_handler (downloader->priv->bus,
304 gst_uri_downloader_bus_handler, downloader, NULL);
306 pad = gst_element_get_static_pad (downloader->priv->urisrc, "src");
309 gst_pad_link (pad, downloader->priv->pad);
310 gst_object_unref (pad);
315 gst_uri_downloader_fetch_uri (GstUriDownloader * downloader, const gchar * uri)
317 GstStateChangeReturn ret;
318 GstFragment *download = NULL;
320 g_mutex_lock (&downloader->priv->lock);
322 if (!gst_uri_downloader_set_uri (downloader, uri)) {
326 downloader->priv->download = gst_fragment_new ();
328 ret = gst_element_set_state (downloader->priv->urisrc, GST_STATE_PLAYING);
329 if (ret == GST_STATE_CHANGE_FAILURE) {
330 g_object_unref (downloader->priv->download);
331 downloader->priv->download = NULL;
336 * - the download succeed (EOS in the src pad)
337 * - the download failed (Error message on the fetcher bus)
338 * - the download was canceled
340 GST_DEBUG_OBJECT (downloader, "Waiting to fetch the URI");
341 g_cond_wait (&downloader->priv->cond, &downloader->priv->lock);
343 GST_OBJECT_LOCK (downloader);
344 download = downloader->priv->download;
345 downloader->priv->download = NULL;
346 GST_OBJECT_UNLOCK (downloader);
348 if (download != NULL)
349 GST_INFO_OBJECT (downloader, "URI fetched successfully");
351 GST_INFO_OBJECT (downloader, "Error fetching URI");
355 gst_uri_downloader_stop (downloader);
356 g_mutex_unlock (&downloader->priv->lock);