50d53df2ccb2411183ded8c27f7d853d30b4dfb6
[platform/upstream/gstreamer.git] / gst / gstelementfactory.c
1 /* Gnome-Streamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <gst/gstelement.h>
21 #include <gst/gstplugin.h>
22
23
24 /* global list of registered elementfactories */
25 GList* _gst_elementfactories;
26
27 void _gst_elementfactory_initialize() {
28   _gst_elementfactories = NULL;
29 }
30
31 /**
32  * gst_elementfactory_register:
33  * @elementfactory: factory to register
34  *
35  * Adds the elementfactory to the global list, so it can be retrieved by
36  * name.
37  */
38 void gst_elementfactory_register(GstElementFactory *elementfactory) {
39   g_return_if_fail(elementfactory != NULL);
40
41   _gst_elementfactories = g_list_prepend(_gst_elementfactories,elementfactory);
42 }
43
44 /**
45  * gst_elementfactory_find:
46  * @name: name of factory to find
47  *
48  * Search for an elementfactory of the given name.
49  *
50  * Returns: #GstElementFactory if found, NULL otherwise
51  */
52 GstElementFactory *gst_elementfactory_find(gchar *name) {
53   GList *walk = _gst_elementfactories;
54   GstElementFactory *factory;
55
56   while (walk) {
57     factory = (GstElementFactory *)(walk->data);
58     if (!strcmp(name,factory->name))
59       return factory;
60     walk = g_list_next(walk);
61   }
62
63   return NULL;
64 }
65
66 /**
67  * gst_elementfactory_get_list:
68  *
69  * Get the global list of elementfactories.
70  *
71  * Returns: <type>GList</type> of type #GstElementFactory
72  */
73 GList *gst_elementfactory_get_list() {
74   return _gst_elementfactories;
75 }
76
77
78 /**
79  * gst_elementfactory_new:
80  * @name: name of new elementfactory
81  * @type: GtkType of new element
82  * @details: #GstElementDetails structure with element details
83  *
84  * Create a new elementfactory capable of insantiating objects of the
85  * given type.
86  *
87  * Returns: new elementfactory
88  */
89 GstElementFactory *gst_elementfactory_new(gchar *name,GtkType type,
90                                           GstElementDetails *details) {
91   GstElementFactory *factory = g_new0(GstElementFactory, 1);
92   factory->name = g_strdup(name);
93   factory->type = type;
94   factory->details = details;
95   factory->src_types = NULL;
96   factory->sink_types = NULL;
97   return factory;
98 }
99
100 /**
101  * gst_elementfactory_create:
102  * @factory: factory to instantiate
103  * @name: name of new element
104  *
105  * Create a new element of the type defined by the given elementfactory.
106  * It wll be given the name supplied, since all elements require a name as
107  * their first argument.
108  *
109  * Returns: new #GstElement
110  */
111 GstElement *gst_elementfactory_create(GstElementFactory *factory,
112                                       gchar *name) {
113   GstElement *element;
114   GstElementClass *oclass;
115
116   g_return_val_if_fail(factory != NULL, NULL);
117
118   factory = gst_plugin_load_elementfactory(factory->name);
119
120   g_return_val_if_fail(factory->type != 0, NULL);
121
122   // create an instance of the element
123   element = GST_ELEMENT(gtk_type_new(factory->type));
124   g_assert(element != NULL);
125
126   // attempt to set the elemenfactory class pointer if necessary
127   oclass = GST_ELEMENT_CLASS(GTK_OBJECT(element)->klass);
128   if (oclass->elementfactory == NULL)
129     oclass->elementfactory = factory;
130
131   gst_element_set_name(GST_ELEMENT(element),name);
132
133   return element;
134 }
135
136 GstElement *gst_elementfactory_make(gchar *factoryname,gchar *name) {
137   GstElementFactory *factory;
138   GstElement *element;
139
140   gst_plugin_load_elementfactory(factoryname);
141   factory = gst_elementfactory_find(factoryname);
142   if (factory == NULL) return NULL;
143   element = gst_elementfactory_create(factory,name);
144   return element;
145 }
146
147 void gst_elementfactory_add_src(GstElementFactory *elementfactory, guint16 id) {
148   guint type = id;
149
150   elementfactory->src_types = g_list_prepend(elementfactory->src_types, GUINT_TO_POINTER(type));
151 }
152
153 void gst_elementfactory_add_sink(GstElementFactory *elementfactory, guint16 id) {
154   guint type = id;
155
156   elementfactory->sink_types = g_list_prepend(elementfactory->sink_types, GUINT_TO_POINTER(type));
157 }
158
159 xmlNodePtr gst_elementfactory_save_thyself(GstElementFactory *factory, xmlNodePtr parent) {
160   GList *types;
161   xmlNodePtr subtree;
162
163   xmlNewChild(parent,NULL,"name",factory->name);
164   xmlNewChild(parent,NULL,"longname", factory->details->longname);
165   xmlNewChild(parent,NULL,"class", factory->details->class);
166   xmlNewChild(parent,NULL,"description", factory->details->description);
167   xmlNewChild(parent,NULL,"version", factory->details->version);
168   xmlNewChild(parent,NULL,"author", factory->details->author);
169   xmlNewChild(parent,NULL,"copyright", factory->details->copyright);
170
171   types = factory->src_types;
172   if (types) {
173     subtree = xmlNewChild(parent,NULL,"sources",NULL);
174     while (types) {
175       guint16 typeid = GPOINTER_TO_UINT(types->data);
176       GstType *type = gst_type_find_by_id(typeid);
177
178       gst_type_save_thyself(type, subtree);
179
180       types = g_list_next(types);
181     }
182   }
183   types = factory->sink_types;
184   if (types) {
185     subtree = xmlNewChild(parent,NULL,"sinks",NULL);
186     while (types) {
187       guint16 typeid = GPOINTER_TO_UINT(types->data);
188       GstType *type = gst_type_find_by_id(typeid);
189
190       gst_type_save_thyself(type, subtree);
191
192       types = g_list_next(types);
193     }
194   }
195
196   return parent;
197 }
198
199 GstElementFactory *gst_elementfactory_load_thyself(xmlNodePtr parent) {
200   GstElementFactory *factory = g_new0(GstElementFactory, 1);
201   xmlNodePtr children = parent->childs;
202   factory->details = g_new0(GstElementDetails, 1);
203   factory->sink_types = NULL;
204   factory->src_types = NULL;
205
206   while (children) {
207     if (!strcmp(children->name, "name")) {
208       factory->name = g_strdup(xmlNodeGetContent(children));
209     }
210     if (!strcmp(children->name, "longname")) {
211       factory->details->longname = g_strdup(xmlNodeGetContent(children));
212     }
213     if (!strcmp(children->name, "class")) {
214       factory->details->class = g_strdup(xmlNodeGetContent(children));
215     }
216     if (!strcmp(children->name, "description")) {
217       factory->details->description = g_strdup(xmlNodeGetContent(children));
218     }
219     if (!strcmp(children->name, "version")) {
220       factory->details->version = g_strdup(xmlNodeGetContent(children));
221     }
222     if (!strcmp(children->name, "author")) {
223       factory->details->author = g_strdup(xmlNodeGetContent(children));
224     }
225     if (!strcmp(children->name, "copyright")) {
226       factory->details->copyright = g_strdup(xmlNodeGetContent(children));
227     }
228     if (!strcmp(children->name, "sources")) {
229       guint16 typeid = gst_type_load_thyself(children);
230
231       gst_type_add_src(typeid, factory);
232     }
233     if (!strcmp(children->name, "sinks")) {
234       guint16 typeid = gst_type_load_thyself(children);
235
236       gst_type_add_sink(typeid, factory);
237     }
238
239     children = children->next;
240   }
241
242   return factory;
243 }
244