2 * Copyright (C) 2006-2007 Benjamin Otte <otte@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
25 #include "swfdec_loader_internal.h"
26 #include "swfdec_buffer.h"
27 #include "swfdec_debug.h"
28 #include "swfdec_player_internal.h"
33 * SECTION:SwfdecLoader
34 * @title: SwfdecLoader
35 * @short_description: object used for input
37 * SwfdecLoader is the base class used for reading input. Since developers
38 * normally need to adapt input to the needs of their application, this class
39 * is provided to be adapted to their needs. It is used both for HTTP and
42 * Since Flash files can load new resources while operating, a #SwfdecLoader
43 * can be instructed to load another resource.
45 * For convenience, a #SwfdecLoader for file access is provided by Swfdec.
51 * This is the base object used for providing input. It is abstract, use a
52 * subclass to provide your input.
57 * @load: initialize a new loader based on a parent loader object. The new
58 * loader will already have its URL set.
60 * This is the base class used for input. If you create a subclass, you are
61 * supposed to set the function pointers listed above.
65 * SwfdecLoaderDataType:
66 * @SWFDEC_LOADER_DATA_UNKNOWN: Unidentified data or data that cannot be
68 * @SWFDEC_LOADER_DATA_SWF: Data describing a normal Flash file.
69 * @SWFDEC_LOADER_DATA_FLV: Data describing a Flash video stream.
70 * @SWFDEC_LOADER_DATA_XML: Data in XML format.
71 * @SWFDEC_LOADER_DATA_TEXT: Textual data.
72 * @SWFDEC_LOADER_DATA_JPEG: a JPEG image
73 * @SWFDEC_LOADER_DATA_PNG: a PNG image
75 * This type describes the different types of data that can be loaded inside
76 * Swfdec. Swfdec identifies its data streams and you can use the
77 * swfdec_loader_get_data_type() to acquire more information about the data
78 * inside a #SwfdecLoader.
81 /*** SwfdecLoader ***/
91 G_DEFINE_ABSTRACT_TYPE (SwfdecLoader
, swfdec_loader
, SWFDEC_TYPE_STREAM
)
94 swfdec_loader_describe (SwfdecStream
*stream
)
96 const SwfdecURL
*url
= SWFDEC_LOADER (stream
)->url
;
99 return swfdec_url_get_url (url
);
101 return "unknown url";
106 swfdec_loader_get_property (GObject
*object
, guint param_id
, GValue
*value
,
109 SwfdecLoader
*loader
= SWFDEC_LOADER (object
);
113 g_value_set_enum (value
, loader
->data_type
);
116 g_value_set_long (value
, loader
->size
);
119 g_value_set_ulong (value
, swfdec_loader_get_loaded (loader
));
122 g_value_set_boxed (value
, loader
->url
);
125 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);
131 swfdec_loader_set_property (GObject
*object
, guint param_id
, const GValue
*value
,
134 SwfdecLoader
*loader
= SWFDEC_LOADER (object
);
138 if (loader
->size
== -1 && g_value_get_long (value
) >= 0)
139 swfdec_loader_set_size (loader
, g_value_get_long (value
));
142 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);
148 swfdec_loader_dispose (GObject
*object
)
150 SwfdecLoader
*loader
= SWFDEC_LOADER (object
);
153 swfdec_url_free (loader
->url
);
157 G_OBJECT_CLASS (swfdec_loader_parent_class
)->dispose (object
);
161 swfdec_loader_class_init (SwfdecLoaderClass
*klass
)
163 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
164 SwfdecStreamClass
*stream_class
= SWFDEC_STREAM_CLASS (klass
);
166 object_class
->dispose
= swfdec_loader_dispose
;
167 object_class
->get_property
= swfdec_loader_get_property
;
168 object_class
->set_property
= swfdec_loader_set_property
;
170 g_object_class_install_property (object_class
, PROP_DATA_TYPE
,
171 g_param_spec_enum ("data-type", "data type", "the data's type as identified by Swfdec",
172 SWFDEC_TYPE_LOADER_DATA_TYPE
, SWFDEC_LOADER_DATA_UNKNOWN
, G_PARAM_READABLE
));
173 g_object_class_install_property (object_class
, PROP_SIZE
,
174 g_param_spec_long ("size", "size", "amount of bytes in loader",
175 -1, G_MAXLONG
, -1, G_PARAM_READWRITE
));
176 g_object_class_install_property (object_class
, PROP_LOADED
,
177 g_param_spec_ulong ("loaded", "loaded", "bytes already loaded",
178 0, G_MAXULONG
, 0, G_PARAM_READWRITE
));
179 g_object_class_install_property (object_class
, PROP_URL
,
180 g_param_spec_boxed ("url", "url", "URL for this file",
181 SWFDEC_TYPE_URL
, G_PARAM_READABLE
));
183 stream_class
->describe
= swfdec_loader_describe
;
187 swfdec_loader_init (SwfdecLoader
*loader
)
189 loader
->data_type
= SWFDEC_LOADER_DATA_UNKNOWN
;
197 * swfdec_loader_set_url:
198 * @loader: the loader to update
199 * @url: string specifying the new URL. The url must be a valid absolute URL.
201 * Updates the url of the given @loader to point to the new @url. This is useful
202 * whe encountering HTTP redirects, as the loader is supposed to reference the
203 * final URL after all rdirections.
204 * This function may only be called once and must have been called before
205 * calling swfdec_stream_open() on @loader.
208 swfdec_loader_set_url (SwfdecLoader
*loader
, const char *url
)
212 g_return_if_fail (SWFDEC_IS_LOADER (loader
));
213 g_return_if_fail (loader
->url
== NULL
);
214 g_return_if_fail (url
!= NULL
);
216 real
= swfdec_url_new (url
);
217 g_return_if_fail (real
!= NULL
);
222 * swfdec_loader_get_url:
223 * @loader: a #SwfdecLoader
225 * Gets the url this loader is handling. This is mostly useful for writing
226 * subclasses of #SwfdecLoader.
228 * Returns: a #SwfdecURL describing @loader or %NULL if the @url is not known
232 swfdec_loader_get_url (SwfdecLoader
*loader
)
234 g_return_val_if_fail (SWFDEC_IS_LOADER (loader
), NULL
);
240 * swfdec_loader_get_data_type:
241 * @loader: a #SwfdecLoader
243 * Queries the type of data this loader provides. The type is determined
244 * automatically by Swfdec.
246 * Returns: the type this data was identified to be in or
247 * #SWFDEC_LOADER_DATA_UNKNOWN if not identified
250 swfdec_loader_get_data_type (SwfdecLoader
*loader
)
252 g_return_val_if_fail (SWFDEC_IS_LOADER (loader
), SWFDEC_LOADER_DATA_UNKNOWN
);
254 return loader
->data_type
;
258 swfdec_loader_set_data_type (SwfdecLoader
*loader
, SwfdecLoaderDataType type
)
260 g_return_if_fail (SWFDEC_IS_LOADER (loader
));
261 g_return_if_fail (loader
->data_type
== SWFDEC_LOADER_DATA_UNKNOWN
);
262 g_return_if_fail (type
!= SWFDEC_LOADER_DATA_UNKNOWN
);
264 loader
->data_type
= type
;
265 g_object_notify (G_OBJECT (loader
), "data-type");
269 * swfdec_loader_set_size:
270 * @loader: a #SwfdecLoader
271 * @size: the amount of bytes in this loader
273 * Sets the size of bytes in this loader. This function may only be called once.
276 swfdec_loader_set_size (SwfdecLoader
*loader
, gulong size
)
278 g_return_if_fail (SWFDEC_IS_LOADER (loader
));
279 g_return_if_fail (loader
->size
== -1);
280 g_return_if_fail (size
<= G_MAXLONG
);
283 g_object_notify (G_OBJECT (loader
), "size");
287 * swfdec_loader_get_size:
288 * @loader: a #SwfdecLoader
290 * Queries the amount of bytes inside @loader. If the size is unknown, -1 is
291 * returned. Otherwise the number is greater or equal to 0.
293 * Returns: the total number of bytes for this loader or -1 if unknown
296 swfdec_loader_get_size (SwfdecLoader
*loader
)
298 g_return_val_if_fail (SWFDEC_IS_LOADER (loader
), -1);
304 * swfdec_loader_get_loaded:
305 * @loader: a #SwfdecLoader
307 * Gets the amount of bytes that have already been pushed into @loader and are
308 * available to Swfdec.
310 * Returns: Amount of bytes in @loader
313 swfdec_loader_get_loaded (SwfdecLoader
*loader
)
315 SwfdecBufferQueue
*queue
;
317 g_return_val_if_fail (SWFDEC_IS_LOADER (loader
), 0);
319 queue
= swfdec_stream_get_queue (SWFDEC_STREAM (loader
));
320 return swfdec_buffer_queue_get_depth (queue
) +
321 swfdec_buffer_queue_get_offset (queue
);
325 * swfdec_loader_data_type_get_extension:
326 * @type: a #SwfdecLoaderDataType
328 * Queries the extension to be used for data of the given @type.
330 * Returns: the typical extension for this data type or the empty string
331 * if the type has no extension
334 swfdec_loader_data_type_get_extension (SwfdecLoaderDataType type
)
337 case SWFDEC_LOADER_DATA_UNKNOWN
:
339 case SWFDEC_LOADER_DATA_SWF
:
341 case SWFDEC_LOADER_DATA_FLV
:
343 case SWFDEC_LOADER_DATA_XML
:
345 case SWFDEC_LOADER_DATA_TEXT
:
347 case SWFDEC_LOADER_DATA_JPEG
:
349 case SWFDEC_LOADER_DATA_PNG
:
352 g_warning ("unknown data type %u", type
);