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