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