Initialize Tizen 2.3
[framework/multimedia/gstreamer0.10.git] / mobile / gst / gstregistrychunks.c
1 /* GStreamer
2  * Copyright (C) 2006 Josep Torra <josep@fluendo.com>
3  *               2006 Mathieu Garcia <matthieu@fluendo.com>
4  *               2006,2007 Stefan Kost <ensonic@users.sf.net>
5  *               2008 Sebastian Dröge <slomo@circular-chaos.org>
6  *               2008 Jan Schmidt <jan.schmidt@sun.com>
7  *
8  * gstregistrychunks.c: GstRegistryChunk helper for serialising/deserialising
9  * plugin entries and features.
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public
22  * License along with this library; if not, write to the
23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #  include "config.h"
29 #endif
30
31 #include <gst/gst_private.h>
32 #include <gst/gstconfig.h>
33 #include <gst/gstelement.h>
34 #include <gst/gsttypefind.h>
35 #include <gst/gsttypefindfactory.h>
36 #include <gst/gsturi.h>
37 #include <gst/gstinfo.h>
38 #include <gst/gstenumtypes.h>
39 #include <gst/gstpadtemplate.h>
40
41 #include <gst/gstregistrychunks.h>
42
43 #define GST_CAT_DEFAULT GST_CAT_REGISTRY
44
45 /* count string length, but return -1 if we hit the eof */
46 static gint
47 _strnlen (const gchar * str, gint maxlen)
48 {
49   gint len = 0;
50
51   while (G_LIKELY (len < maxlen)) {
52     if (G_UNLIKELY (str[len] == '\0'))
53       return len;
54     len++;
55   }
56   return -1;
57 }
58
59 /* Macros */
60 #define unpack_element(inptr, outptr, element, endptr, error_label) G_STMT_START{ \
61   if (inptr + sizeof(element) > endptr) { \
62     GST_ERROR ("Failed reading element " G_STRINGIFY (element)  \
63         ". Have %d bytes need %" G_GSSIZE_FORMAT, \
64         (int) (endptr - inptr), sizeof(element)); \
65     goto error_label; \
66   } \
67   outptr = (element *) inptr; \
68   inptr += sizeof (element); \
69 }G_STMT_END
70
71 #define unpack_const_string(inptr, outptr, endptr, error_label) G_STMT_START{\
72   gint _len = _strnlen (inptr, (endptr-inptr)); \
73   if (_len == -1) \
74     goto error_label; \
75   outptr = g_intern_string ((const gchar *)inptr); \
76   inptr += _len + 1; \
77 }G_STMT_END
78
79 #define unpack_string(inptr, outptr, endptr, error_label)  G_STMT_START{\
80   gint _len = _strnlen (inptr, (endptr-inptr)); \
81   if (_len == -1) \
82     goto error_label; \
83   outptr = g_memdup ((gconstpointer)inptr, _len + 1); \
84   inptr += _len + 1; \
85 }G_STMT_END
86
87 #define unpack_string_nocopy(inptr, outptr, endptr, error_label)  G_STMT_START{\
88   gint _len = _strnlen (inptr, (endptr-inptr)); \
89   if (_len == -1) \
90     goto error_label; \
91   outptr = (const gchar *)inptr; \
92   inptr += _len + 1; \
93 }G_STMT_END
94
95 #define ALIGNMENT            (sizeof (void *))
96 #define alignment(_address)  (gsize)_address%ALIGNMENT
97 #define align(_ptr)          _ptr += (( alignment(_ptr) == 0) ? 0 : ALIGNMENT-alignment(_ptr))
98
99 void
100 _priv_gst_registry_chunk_free (GstRegistryChunk * chunk)
101 {
102   if (!(chunk->flags & GST_REGISTRY_CHUNK_FLAG_CONST)) {
103     if ((chunk->flags & GST_REGISTRY_CHUNK_FLAG_MALLOC))
104       g_free (chunk->data);
105     else
106       g_slice_free1 (chunk->size, chunk->data);
107   }
108   g_slice_free (GstRegistryChunk, chunk);
109 }
110
111 /*
112  * gst_registry_chunks_save_const_string:
113  *
114  * Store a const string in a binary chunk.
115  *
116  * Returns: %TRUE for success
117  */
118 inline static gboolean
119 gst_registry_chunks_save_const_string (GList ** list, const gchar * str)
120 {
121   GstRegistryChunk *chunk;
122
123   if (G_UNLIKELY (str == NULL)) {
124     GST_ERROR ("unexpected NULL string in plugin or plugin feature data");
125     str = "";
126   }
127
128   chunk = g_slice_new (GstRegistryChunk);
129   chunk->data = (gpointer) str;
130   chunk->size = strlen ((gchar *) chunk->data) + 1;
131   chunk->flags = GST_REGISTRY_CHUNK_FLAG_CONST;
132   chunk->align = FALSE;
133   *list = g_list_prepend (*list, chunk);
134   return TRUE;
135 }
136
137 /*
138  * gst_registry_chunks_save_string:
139  *
140  * Store a string in a binary chunk.
141  *
142  * Returns: %TRUE for success
143  */
144 inline static gboolean
145 gst_registry_chunks_save_string (GList ** list, gchar * str)
146 {
147   GstRegistryChunk *chunk;
148
149   chunk = g_slice_new (GstRegistryChunk);
150   chunk->data = str;
151   chunk->size = strlen ((gchar *) chunk->data) + 1;
152   chunk->flags = GST_REGISTRY_CHUNK_FLAG_MALLOC;
153   chunk->align = FALSE;
154   *list = g_list_prepend (*list, chunk);
155   return TRUE;
156 }
157
158 /*
159  * gst_registry_chunks_save_data:
160  *
161  * Store some data in a binary chunk.
162  *
163  * Returns: the initialized chunk
164  */
165 inline static GstRegistryChunk *
166 gst_registry_chunks_make_data (gpointer data, gulong size)
167 {
168   GstRegistryChunk *chunk;
169
170   chunk = g_slice_new (GstRegistryChunk);
171   chunk->data = data;
172   chunk->size = size;
173   chunk->flags = GST_REGISTRY_CHUNK_FLAG_NONE;
174   chunk->align = TRUE;
175   return chunk;
176 }
177
178
179 /*
180  * gst_registry_chunks_save_pad_template:
181  *
182  * Store pad_templates in binary chunks.
183  *
184  * Returns: %TRUE for success
185  */
186 static gboolean
187 gst_registry_chunks_save_pad_template (GList ** list,
188     GstStaticPadTemplate * template)
189 {
190   GstRegistryChunkPadTemplate *pt;
191   GstRegistryChunk *chk;
192
193   pt = g_slice_new (GstRegistryChunkPadTemplate);
194   chk =
195       gst_registry_chunks_make_data (pt, sizeof (GstRegistryChunkPadTemplate));
196
197   pt->presence = template->presence;
198   pt->direction = template->direction;
199
200   /* pack pad template strings */
201   gst_registry_chunks_save_const_string (list,
202       (gchar *) (template->static_caps.string));
203   gst_registry_chunks_save_const_string (list, template->name_template);
204
205   *list = g_list_prepend (*list, chk);
206
207   return TRUE;
208 }
209
210 #define VALIDATE_UTF8(__details, __entry)                                   \
211 G_STMT_START {                                                              \
212   if (!g_utf8_validate (__details->__entry, -1, NULL)) {                    \
213     g_warning ("Invalid UTF-8 in " G_STRINGIFY (__entry) ": %s",            \
214         __details->__entry);                                                \
215     g_free (__details->__entry);                                            \
216     __details->__entry = g_strdup ("[ERROR: invalid UTF-8]");               \
217   }                                                                         \
218 } G_STMT_END
219
220 /*
221  * gst_registry_chunks_save_feature:
222  *
223  * Store features in binary chunks.
224  *
225  * Returns: %TRUE for success
226  */
227 static gboolean
228 gst_registry_chunks_save_feature (GList ** list, GstPluginFeature * feature)
229 {
230   const gchar *type_name = g_type_name (G_OBJECT_TYPE (feature));
231   GstRegistryChunkPluginFeature *pf = NULL;
232   GstRegistryChunk *chk = NULL;
233   GList *walk;
234
235   if (!type_name) {
236     GST_ERROR ("NULL feature type_name, aborting.");
237     return FALSE;
238   }
239
240   if (GST_IS_ELEMENT_FACTORY (feature)) {
241     GstRegistryChunkElementFactory *ef;
242     GstElementFactory *factory = GST_ELEMENT_FACTORY (feature);
243     GstElementDetails *details = &factory->details;
244
245     /* do the utf-8 validation of the element factory details here to avoid
246      * doing it every time we load */
247     VALIDATE_UTF8 (details, longname);
248     VALIDATE_UTF8 (details, klass);
249     VALIDATE_UTF8 (details, description);
250     VALIDATE_UTF8 (details, author);
251
252     /* Initialize with zeroes because of struct padding and
253      * valgrind complaining about copying unitialized memory
254      */
255     ef = g_slice_new0 (GstRegistryChunkElementFactory);
256     chk =
257         gst_registry_chunks_make_data (ef,
258         sizeof (GstRegistryChunkElementFactory));
259     ef->npadtemplates = ef->ninterfaces = ef->nuriprotocols = 0;
260     pf = (GstRegistryChunkPluginFeature *) ef;
261
262     /* save interfaces */
263     for (walk = factory->interfaces; walk;
264         walk = g_list_next (walk), ef->ninterfaces++) {
265       gst_registry_chunks_save_const_string (list, (gchar *) walk->data);
266     }
267     GST_DEBUG ("Feature %s: saved %d interfaces %d pad templates",
268         feature->name, ef->ninterfaces, ef->npadtemplates);
269
270     /* save uritypes */
271     if (GST_URI_TYPE_IS_VALID (factory->uri_type)) {
272       if (factory->uri_protocols && *factory->uri_protocols) {
273         GstRegistryChunk *subchk;
274         gchar **protocol;
275
276         subchk =
277             gst_registry_chunks_make_data (&factory->uri_type,
278             sizeof (factory->uri_type));
279         subchk->flags = GST_REGISTRY_CHUNK_FLAG_CONST;
280
281         protocol = factory->uri_protocols;
282         while (*protocol) {
283           gst_registry_chunks_save_const_string (list, *protocol++);
284           ef->nuriprotocols++;
285         }
286         *list = g_list_prepend (*list, subchk);
287         GST_DEBUG ("Saved %d UriTypes", ef->nuriprotocols);
288       } else {
289         g_warning ("GStreamer feature '%s' is URI handler but does not provide"
290             " any protocols it can handle", feature->name);
291       }
292     }
293
294     /* save pad-templates */
295     for (walk = factory->staticpadtemplates; walk;
296         walk = g_list_next (walk), ef->npadtemplates++) {
297       GstStaticPadTemplate *template = walk->data;
298
299       if (!gst_registry_chunks_save_pad_template (list, template)) {
300         GST_ERROR ("Can't fill pad template, aborting.");
301         goto fail;
302       }
303     }
304
305     /* pack element factory strings */
306     gst_registry_chunks_save_const_string (list, details->author);
307     gst_registry_chunks_save_const_string (list, details->description);
308     gst_registry_chunks_save_const_string (list, details->klass);
309     gst_registry_chunks_save_const_string (list, details->longname);
310     if (factory->meta_data) {
311       gst_registry_chunks_save_string (list,
312           gst_structure_to_string (factory->meta_data));
313     } else {
314       gst_registry_chunks_save_const_string (list, "");
315     }
316   } else if (GST_IS_TYPE_FIND_FACTORY (feature)) {
317     GstRegistryChunkTypeFindFactory *tff;
318     GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (feature);
319     gchar *str;
320
321     /* Initialize with zeroes because of struct padding and
322      * valgrind complaining about copying unitialized memory
323      */
324     tff = g_slice_new0 (GstRegistryChunkTypeFindFactory);
325     chk =
326         gst_registry_chunks_make_data (tff,
327         sizeof (GstRegistryChunkTypeFindFactory));
328     tff->nextensions = 0;
329     pf = (GstRegistryChunkPluginFeature *) tff;
330
331     /* save extensions */
332     if (factory->extensions) {
333       while (factory->extensions[tff->nextensions]) {
334         gst_registry_chunks_save_const_string (list,
335             factory->extensions[tff->nextensions++]);
336       }
337     }
338     /* save caps */
339     if (factory->caps) {
340       /* we copy the caps here so we can simplify them before saving. This
341        * is a lot faster when loading them later on */
342       GstCaps *copy = gst_caps_copy (factory->caps);
343
344       gst_caps_do_simplify (copy);
345       str = gst_caps_to_string (copy);
346       gst_caps_unref (copy);
347       gst_registry_chunks_save_string (list, str);
348     } else {
349       gst_registry_chunks_save_const_string (list, "");
350     }
351   } else if (GST_IS_INDEX_FACTORY (feature)) {
352     GstIndexFactory *factory = GST_INDEX_FACTORY (feature);
353
354     pf = g_slice_new (GstRegistryChunkPluginFeature);
355     chk =
356         gst_registry_chunks_make_data (pf,
357         sizeof (GstRegistryChunkPluginFeature));
358
359     /* pack element factory strings */
360     gst_registry_chunks_save_const_string (list, factory->longdesc);
361   } else {
362     GST_WARNING ("unhandled feature type '%s'", type_name);
363   }
364
365   if (pf) {
366     pf->rank = feature->rank;
367     *list = g_list_prepend (*list, chk);
368
369     /* pack plugin feature strings */
370     gst_registry_chunks_save_const_string (list, feature->name);
371     gst_registry_chunks_save_const_string (list, (gchar *) type_name);
372
373     return TRUE;
374   }
375
376   /* Errors */
377 fail:
378   g_free (chk);
379   g_free (pf);
380   return FALSE;
381 }
382
383 static gboolean
384 gst_registry_chunks_save_plugin_dep (GList ** list, GstPluginDep * dep)
385 {
386   GstRegistryChunkDep *ed;
387   GstRegistryChunk *chk;
388   gchar **s;
389
390   ed = g_slice_new (GstRegistryChunkDep);
391   chk = gst_registry_chunks_make_data (ed, sizeof (GstRegistryChunkDep));
392
393   ed->flags = dep->flags;
394   ed->n_env_vars = 0;
395   ed->n_paths = 0;
396   ed->n_names = 0;
397
398   ed->env_hash = dep->env_hash;
399   ed->stat_hash = dep->stat_hash;
400
401   for (s = dep->env_vars; s != NULL && *s != NULL; ++s, ++ed->n_env_vars)
402     gst_registry_chunks_save_string (list, g_strdup (*s));
403
404   for (s = dep->paths; s != NULL && *s != NULL; ++s, ++ed->n_paths)
405     gst_registry_chunks_save_string (list, g_strdup (*s));
406
407   for (s = dep->names; s != NULL && *s != NULL; ++s, ++ed->n_names)
408     gst_registry_chunks_save_string (list, g_strdup (*s));
409
410   *list = g_list_prepend (*list, chk);
411
412   GST_LOG ("Saved external plugin dependency");
413   return TRUE;
414 }
415
416 /*
417  * _priv_gst_registry_chunks_save_plugin:
418  *
419  * Adapt a GstPlugin to our GstRegistryChunkPluginElement structure, and
420  * prepend it as a GstRegistryChunk in the provided list.
421  *
422  */
423 gboolean
424 _priv_gst_registry_chunks_save_plugin (GList ** list, GstRegistry * registry,
425     GstPlugin * plugin)
426 {
427   GstRegistryChunkPluginElement *pe;
428   GstRegistryChunk *chk;
429   GList *plugin_features = NULL;
430   GList *walk;
431
432   pe = g_slice_new (GstRegistryChunkPluginElement);
433   chk =
434       gst_registry_chunks_make_data (pe,
435       sizeof (GstRegistryChunkPluginElement));
436
437   pe->file_size = plugin->file_size;
438   pe->file_mtime = plugin->file_mtime;
439   pe->nfeatures = 0;
440   pe->n_deps = 0;
441
442   /* pack external deps */
443   for (walk = plugin->priv->deps; walk != NULL; walk = walk->next) {
444     if (!gst_registry_chunks_save_plugin_dep (list, walk->data)) {
445       GST_ERROR ("Could not save external plugin dependency, aborting.");
446       goto fail;
447     }
448     ++pe->n_deps;
449   }
450
451   /* pack plugin features */
452   plugin_features =
453       gst_registry_get_feature_list_by_plugin (registry, plugin->desc.name);
454   for (walk = plugin_features; walk; walk = g_list_next (walk), pe->nfeatures++) {
455     GstPluginFeature *feature = GST_PLUGIN_FEATURE (walk->data);
456
457     if (!gst_registry_chunks_save_feature (list, feature)) {
458       GST_ERROR ("Can't fill plugin feature, aborting.");
459       goto fail;
460     }
461   }
462
463   gst_plugin_feature_list_free (plugin_features);
464
465   /* pack cache data */
466   if (plugin->priv->cache_data) {
467     gchar *cache_str = gst_structure_to_string (plugin->priv->cache_data);
468     gst_registry_chunks_save_string (list, cache_str);
469   } else {
470     gst_registry_chunks_save_const_string (list, "");
471   }
472
473   /* pack plugin element strings */
474   gst_registry_chunks_save_const_string (list,
475       (plugin->desc.release_datetime) ? plugin->desc.release_datetime : "");
476   gst_registry_chunks_save_const_string (list, plugin->desc.origin);
477   gst_registry_chunks_save_const_string (list, plugin->desc.package);
478   gst_registry_chunks_save_const_string (list, plugin->desc.source);
479   gst_registry_chunks_save_const_string (list, plugin->desc.license);
480   gst_registry_chunks_save_const_string (list, plugin->desc.version);
481   gst_registry_chunks_save_const_string (list, plugin->filename);
482   gst_registry_chunks_save_const_string (list, plugin->desc.description);
483   gst_registry_chunks_save_const_string (list, plugin->desc.name);
484
485   *list = g_list_prepend (*list, chk);
486
487   GST_DEBUG ("Found %d features in plugin \"%s\"", pe->nfeatures,
488       plugin->desc.name);
489   return TRUE;
490
491   /* Errors */
492 fail:
493   gst_plugin_feature_list_free (plugin_features);
494   g_free (chk);
495   g_free (pe);
496   return FALSE;
497 }
498
499 /*
500  * gst_registry_chunks_load_pad_template:
501  *
502  * Make a new GstStaticPadTemplate from current GstRegistryChunkPadTemplate
503  * structure.
504  *
505  * Returns: new GstStaticPadTemplate
506  */
507 static gboolean
508 gst_registry_chunks_load_pad_template (GstElementFactory * factory, gchar ** in,
509     gchar * end)
510 {
511   GstRegistryChunkPadTemplate *pt;
512   GstStaticPadTemplate *template = NULL;
513
514   align (*in);
515   GST_DEBUG ("Reading/casting for GstRegistryChunkPadTemplate at address %p",
516       *in);
517   unpack_element (*in, pt, GstRegistryChunkPadTemplate, end, fail);
518
519   template = g_slice_new (GstStaticPadTemplate);
520   template->presence = pt->presence;
521   template->direction = (GstPadDirection) pt->direction;
522   template->static_caps.caps.refcount = 0;
523
524   /* unpack pad template strings */
525   unpack_const_string (*in, template->name_template, end, fail);
526   unpack_const_string (*in, template->static_caps.string, end, fail);
527
528   __gst_element_factory_add_static_pad_template (factory, template);
529   GST_DEBUG ("Added pad_template %s", template->name_template);
530
531   return TRUE;
532 fail:
533   GST_INFO ("Reading pad template failed");
534   if (template)
535     g_slice_free (GstStaticPadTemplate, template);
536   return FALSE;
537 }
538
539 /*
540  * gst_registry_chunks_load_feature:
541  *
542  * Make a new GstPluginFeature from current binary plugin feature structure
543  *
544  * Returns: new GstPluginFeature
545  */
546 static gboolean
547 gst_registry_chunks_load_feature (GstRegistry * registry, gchar ** in,
548     gchar * end, GstPlugin * plugin)
549 {
550   GstRegistryChunkPluginFeature *pf = NULL;
551   GstPluginFeature *feature = NULL;
552   const gchar *const_str, *type_name;
553   const gchar *feature_name;
554   const gchar *plugin_name;
555   gchar *str;
556   GType type;
557   guint i;
558
559   plugin_name = plugin->desc.name;
560
561   /* unpack plugin feature strings */
562   unpack_string_nocopy (*in, type_name, end, fail);
563
564   if (G_UNLIKELY (!type_name)) {
565     GST_ERROR ("No feature type name");
566     return FALSE;
567   }
568
569   /* unpack more plugin feature strings */
570   unpack_string_nocopy (*in, feature_name, end, fail);
571
572   GST_DEBUG ("Plugin '%s' feature '%s' typename : '%s'", plugin_name,
573       feature_name, type_name);
574
575   if (G_UNLIKELY (!(type = g_type_from_name (type_name)))) {
576     GST_ERROR ("Unknown type from typename '%s' for plugin '%s'", type_name,
577         plugin_name);
578     return FALSE;
579   }
580   if (G_UNLIKELY ((feature = g_object_newv (type, 0, NULL)) == NULL)) {
581     GST_ERROR ("Can't create feature from type");
582     return FALSE;
583   }
584   gst_plugin_feature_set_name (feature, feature_name);
585
586   if (G_UNLIKELY (!GST_IS_PLUGIN_FEATURE (feature))) {
587     GST_ERROR ("typename : '%s' is not a plugin feature", type_name);
588     goto fail;
589   }
590
591   if (GST_IS_ELEMENT_FACTORY (feature)) {
592     GstRegistryChunkElementFactory *ef;
593     guint n;
594     GstElementFactory *factory = GST_ELEMENT_FACTORY_CAST (feature);
595     gchar *str;
596     const gchar *meta_data_str;
597
598     align (*in);
599     GST_LOG ("Reading/casting for GstRegistryChunkElementFactory at address %p",
600         *in);
601     unpack_element (*in, ef, GstRegistryChunkElementFactory, end, fail);
602     pf = (GstRegistryChunkPluginFeature *) ef;
603
604     /* unpack element factory strings */
605     unpack_string_nocopy (*in, meta_data_str, end, fail);
606     if (meta_data_str && *meta_data_str) {
607       factory->meta_data = gst_structure_from_string (meta_data_str, NULL);
608       if (!factory->meta_data) {
609         GST_ERROR
610             ("Error when trying to deserialize structure for metadata '%s'",
611             meta_data_str);
612         goto fail;
613       }
614     }
615     unpack_string (*in, factory->details.longname, end, fail);
616     unpack_string (*in, factory->details.klass, end, fail);
617     unpack_string (*in, factory->details.description, end, fail);
618     unpack_string (*in, factory->details.author, end, fail);
619     n = ef->npadtemplates;
620     GST_DEBUG ("Element factory : '%s' with npadtemplates=%d",
621         factory->details.longname, n);
622
623     /* load pad templates */
624     for (i = 0; i < n; i++) {
625       if (G_UNLIKELY (!gst_registry_chunks_load_pad_template (factory, in,
626                   end))) {
627         GST_ERROR ("Error while loading binary pad template");
628         goto fail;
629       }
630     }
631
632     /* load uritypes */
633     if (G_UNLIKELY ((n = ef->nuriprotocols))) {
634       GST_DEBUG ("Reading %d UriTypes at address %p", n, *in);
635
636       align (*in);
637       factory->uri_type = *((guint *) * in);
638       *in += sizeof (factory->uri_type);
639       /*unpack_element(*in, &factory->uri_type, factory->uri_type, end, fail); */
640
641       factory->uri_protocols = g_new0 (gchar *, n + 1);
642       for (i = 0; i < n; i++) {
643         unpack_string (*in, str, end, fail);
644         factory->uri_protocols[i] = str;
645       }
646     }
647     /* load interfaces */
648     if (G_UNLIKELY ((n = ef->ninterfaces))) {
649       GST_DEBUG ("Reading %d Interfaces at address %p", n, *in);
650       for (i = 0; i < n; i++) {
651         unpack_string_nocopy (*in, const_str, end, fail);
652         __gst_element_factory_add_interface (factory, const_str);
653       }
654     }
655   } else if (GST_IS_TYPE_FIND_FACTORY (feature)) {
656     GstRegistryChunkTypeFindFactory *tff;
657     GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (feature);
658
659     align (*in);
660     GST_DEBUG
661         ("Reading/casting for GstRegistryChunkPluginFeature at address %p",
662         *in);
663     unpack_element (*in, tff, GstRegistryChunkTypeFindFactory, end, fail);
664     pf = (GstRegistryChunkPluginFeature *) tff;
665
666     /* load typefinder caps */
667     unpack_string_nocopy (*in, const_str, end, fail);
668     if (const_str != NULL && *const_str != '\0')
669       factory->caps = gst_caps_from_string (const_str);
670     else
671       factory->caps = NULL;
672
673     /* load extensions */
674     if (tff->nextensions) {
675       GST_DEBUG ("Reading %d Typefind extensions at address %p",
676           tff->nextensions, *in);
677       factory->extensions = g_new0 (gchar *, tff->nextensions + 1);
678       /* unpack in reverse order to maintain the correct order */
679       for (i = tff->nextensions; i > 0; i--) {
680         unpack_string (*in, str, end, fail);
681         factory->extensions[i - 1] = str;
682       }
683     }
684   } else if (GST_IS_INDEX_FACTORY (feature)) {
685     GstIndexFactory *factory = GST_INDEX_FACTORY (feature);
686
687     align (*in);
688     GST_DEBUG
689         ("Reading/casting for GstRegistryChunkPluginFeature at address %p",
690         *in);
691     unpack_element (*in, pf, GstRegistryChunkPluginFeature, end, fail);
692
693     /* unpack index factory strings */
694     unpack_string (*in, factory->longdesc, end, fail);
695   } else {
696     GST_WARNING ("unhandled factory type : %s", G_OBJECT_TYPE_NAME (feature));
697     goto fail;
698   }
699
700   feature->rank = pf->rank;
701
702   feature->plugin_name = plugin_name;
703   feature->plugin = plugin;
704   g_object_add_weak_pointer ((GObject *) plugin,
705       (gpointer *) & feature->plugin);
706
707   gst_registry_add_feature (registry, feature);
708   GST_DEBUG ("Added feature %s, plugin %p %s", feature->name, plugin,
709       plugin_name);
710
711   return TRUE;
712
713   /* Errors */
714 fail:
715   GST_INFO ("Reading plugin feature failed");
716   if (feature) {
717     if (GST_IS_OBJECT (feature))
718       gst_object_unref (feature);
719     else
720       g_object_unref (feature);
721   }
722   return FALSE;
723 }
724
725 static gchar **
726 gst_registry_chunks_load_plugin_dep_strv (gchar ** in, gchar * end, guint n)
727 {
728   gchar **arr;
729
730   if (n == 0)
731     return NULL;
732
733   arr = g_new0 (gchar *, n + 1);
734   while (n > 0) {
735     unpack_string (*in, arr[n - 1], end, fail);
736     --n;
737   }
738   return arr;
739 fail:
740   GST_INFO ("Reading plugin dependency strings failed");
741   return NULL;
742 }
743
744 static gboolean
745 gst_registry_chunks_load_plugin_dep (GstPlugin * plugin, gchar ** in,
746     gchar * end)
747 {
748   GstPluginDep *dep;
749   GstRegistryChunkDep *d;
750   gchar **s;
751
752   align (*in);
753   GST_LOG_OBJECT (plugin, "Unpacking GstRegistryChunkDep from %p", *in);
754   unpack_element (*in, d, GstRegistryChunkDep, end, fail);
755
756   dep = g_slice_new (GstPluginDep);
757
758   dep->env_hash = d->env_hash;
759   dep->stat_hash = d->stat_hash;
760
761   dep->flags = (GstPluginDependencyFlags) d->flags;
762
763   dep->names = gst_registry_chunks_load_plugin_dep_strv (in, end, d->n_names);
764   dep->paths = gst_registry_chunks_load_plugin_dep_strv (in, end, d->n_paths);
765   dep->env_vars =
766       gst_registry_chunks_load_plugin_dep_strv (in, end, d->n_env_vars);
767
768   plugin->priv->deps = g_list_append (plugin->priv->deps, dep);
769
770   GST_DEBUG_OBJECT (plugin, "Loaded external plugin dependency from registry: "
771       "env_hash: %08x, stat_hash: %08x", dep->env_hash, dep->stat_hash);
772   for (s = dep->env_vars; s != NULL && *s != NULL; ++s)
773     GST_LOG_OBJECT (plugin, " evar: %s", *s);
774   for (s = dep->paths; s != NULL && *s != NULL; ++s)
775     GST_LOG_OBJECT (plugin, " path: %s", *s);
776   for (s = dep->names; s != NULL && *s != NULL; ++s)
777     GST_LOG_OBJECT (plugin, " name: %s", *s);
778
779   return TRUE;
780 fail:
781   GST_INFO ("Reading plugin dependency failed");
782   return FALSE;
783 }
784
785
786 /*
787  * _priv_gst_registry_chunks_load_plugin:
788  *
789  * Make a new GstPlugin from current GstRegistryChunkPluginElement structure
790  * and add it to the GstRegistry. Return an offset to the next
791  * GstRegistryChunkPluginElement structure.
792  */
793 gboolean
794 _priv_gst_registry_chunks_load_plugin (GstRegistry * registry, gchar ** in,
795     gchar * end, GstPlugin ** out_plugin)
796 {
797 #ifndef GST_DISABLE_GST_DEBUG
798   gchar *start = *in;
799 #endif
800   GstRegistryChunkPluginElement *pe;
801   const gchar *cache_str = NULL;
802   GstPlugin *plugin = NULL;
803   guint i, n;
804
805   align (*in);
806   GST_LOG ("Reading/casting for GstRegistryChunkPluginElement at address %p",
807       *in);
808   unpack_element (*in, pe, GstRegistryChunkPluginElement, end, fail);
809
810   plugin = g_object_newv (GST_TYPE_PLUGIN, 0, NULL);
811
812   /* TODO: also set GST_PLUGIN_FLAG_CONST */
813   plugin->flags |= GST_PLUGIN_FLAG_CACHED;
814   plugin->file_mtime = pe->file_mtime;
815   plugin->file_size = pe->file_size;
816
817   /* unpack plugin element strings */
818   unpack_const_string (*in, plugin->desc.name, end, fail);
819   unpack_const_string (*in, plugin->desc.description, end, fail);
820   unpack_string (*in, plugin->filename, end, fail);
821   unpack_const_string (*in, plugin->desc.version, end, fail);
822   unpack_const_string (*in, plugin->desc.license, end, fail);
823   unpack_const_string (*in, plugin->desc.source, end, fail);
824   unpack_const_string (*in, plugin->desc.package, end, fail);
825   unpack_const_string (*in, plugin->desc.origin, end, fail);
826   unpack_const_string (*in, plugin->desc.release_datetime, end, fail);
827
828   GST_LOG ("read strings for name='%s'", plugin->desc.name);
829   GST_LOG ("  desc.description='%s'", plugin->desc.description);
830   GST_LOG ("  filename='%s'", plugin->filename);
831   GST_LOG ("  desc.version='%s'", plugin->desc.version);
832   GST_LOG ("  desc.license='%s'", plugin->desc.license);
833   GST_LOG ("  desc.source='%s'", plugin->desc.source);
834   GST_LOG ("  desc.package='%s'", plugin->desc.package);
835   GST_LOG ("  desc.origin='%s'", plugin->desc.origin);
836   GST_LOG ("  desc.datetime=%s", plugin->desc.release_datetime);
837
838   if (plugin->desc.release_datetime[0] == '\0')
839     plugin->desc.release_datetime = NULL;
840
841   /* unpack cache data */
842   unpack_string_nocopy (*in, cache_str, end, fail);
843   if (cache_str != NULL && *cache_str != '\0')
844     plugin->priv->cache_data = gst_structure_from_string (cache_str, NULL);
845
846   /* If the license string is 'BLACKLIST', mark this as a blacklisted
847    * plugin */
848   if (strcmp (plugin->desc.license, "BLACKLIST") == 0)
849     plugin->flags |= GST_PLUGIN_FLAG_BLACKLISTED;
850
851   plugin->basename = g_path_get_basename (plugin->filename);
852
853   /* Takes ownership of plugin */
854   gst_registry_add_plugin (registry, plugin);
855   n = pe->nfeatures;
856   GST_DEBUG ("Added plugin '%s' plugin with %d features from binary registry",
857       plugin->desc.name, n);
858
859   /* Load plugin features */
860   for (i = 0; i < n; i++) {
861     if (G_UNLIKELY (!gst_registry_chunks_load_feature (registry, in, end,
862                 plugin))) {
863       GST_ERROR ("Error while loading binary feature for plugin '%s'",
864           GST_STR_NULL (plugin->desc.name));
865       gst_registry_remove_plugin (registry, plugin);
866       goto fail;
867     }
868   }
869
870   /* Load external plugin dependencies */
871   for (i = 0; i < pe->n_deps; ++i) {
872     if (G_UNLIKELY (!gst_registry_chunks_load_plugin_dep (plugin, in, end))) {
873       GST_ERROR_OBJECT (plugin, "Could not read external plugin dependency "
874           "for plugin '%s'", GST_STR_NULL (plugin->desc.name));
875       gst_registry_remove_plugin (registry, plugin);
876       goto fail;
877     }
878   }
879
880   if (out_plugin)
881     *out_plugin = plugin;
882
883   return TRUE;
884
885   /* Errors */
886 fail:
887   GST_INFO ("Reading plugin failed after %u bytes", (guint) (end - start));
888   return FALSE;
889 }
890
891 void
892 _priv_gst_registry_chunks_save_global_header (GList ** list,
893     GstRegistry * registry, guint32 filter_env_hash)
894 {
895   GstRegistryChunkGlobalHeader *hdr;
896   GstRegistryChunk *chk;
897
898   hdr = g_slice_new (GstRegistryChunkGlobalHeader);
899   chk = gst_registry_chunks_make_data (hdr,
900       sizeof (GstRegistryChunkGlobalHeader));
901
902   hdr->filter_env_hash = filter_env_hash;
903
904   *list = g_list_prepend (*list, chk);
905
906   GST_LOG ("Saved global header (filter_env_hash=0x%08x)", filter_env_hash);
907 }
908
909 gboolean
910 _priv_gst_registry_chunks_load_global_header (GstRegistry * registry,
911     gchar ** in, gchar * end, guint32 * filter_env_hash)
912 {
913   GstRegistryChunkGlobalHeader *hdr;
914
915   align (*in);
916   GST_LOG ("Reading/casting for GstRegistryChunkGlobalHeader at %p", *in);
917   unpack_element (*in, hdr, GstRegistryChunkGlobalHeader, end, fail);
918   *filter_env_hash = hdr->filter_env_hash;
919   return TRUE;
920
921   /* Errors */
922 fail:
923   GST_WARNING ("Reading global header failed");
924   return FALSE;
925 }