Initialize Tizen 2.3
[framework/multimedia/gst-openmax.git] / mobile / omx / gstomx.c
1 /*
2  * Copyright (C) 2007-2009 Nokia Corporation.
3  *
4  * Author: Felipe Contreras <felipe.contreras@nokia.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation
9  * version 2.1 of the License.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #include "config.h"
23
24 #include <string.h>
25
26 #include <gst/gststructure.h>
27
28 #include "gstomx.h"
29 #include "gstomx_dummy.h"
30 #include "gstomx_mpeg4dec.h"
31 #include "gstomx_h263dec.h"
32 #include "gstomx_h264dec.h"
33 #include "gstomx_wmvdec.h"
34 #include "gstomx_mpeg4enc.h"
35 #include "gstomx_h264enc.h"
36 #include "gstomx_h263enc.h"
37 #include "gstomx_vorbisdec.h"
38 #include "gstomx_mp3dec.h"
39 #ifdef EXPERIMENTAL
40 #include "gstomx_mp2dec.h"
41 #include "gstomx_aacdec.h"
42 #include "gstomx_aacenc.h"
43 #include "gstomx_amrnbdec.h"
44 #include "gstomx_amrnbenc.h"
45 #include "gstomx_amrwbdec.h"
46 #include "gstomx_amrwbenc.h"
47 #include "gstomx_adpcmdec.h"
48 #include "gstomx_adpcmenc.h"
49 #include "gstomx_g711dec.h"
50 #include "gstomx_g711enc.h"
51 #include "gstomx_g729dec.h"
52 #include "gstomx_g729enc.h"
53 #include "gstomx_ilbcdec.h"
54 #include "gstomx_ilbcenc.h"
55 #include "gstomx_jpegenc.h"
56 #endif /* EXPERIMENTAL */
57 #include "gstomx_audiosink.h"
58 #ifdef EXPERIMENTAL
59 #include "gstomx_videosink.h"
60 #include "gstomx_filereadersrc.h"
61 #endif /* EXPERIMENTAL */
62 #include "gstomx_volume.h"
63
64 GST_DEBUG_CATEGORY (gstomx_debug);
65
66 static const GstStructure *element_table;
67 static GQuark element_name_quark;
68
69 extern const gchar *default_config;
70
71 static GType (*get_type[]) (void) = {
72   gst_omx_dummy_get_type,
73       gst_omx_mpeg4dec_get_type,
74       gst_omx_h264dec_get_type,
75       gst_omx_h263dec_get_type,
76       gst_omx_wmvdec_get_type,
77       gst_omx_mpeg4enc_get_type,
78       gst_omx_h264enc_get_type,
79       gst_omx_h263enc_get_type,
80       gst_omx_vorbisdec_get_type, gst_omx_mp3dec_get_type,
81 #ifdef EXPERIMENTAL
82       gst_omx_mp2dec_get_type,
83       gst_omx_amrnbdec_get_type,
84       gst_omx_amrnbenc_get_type,
85       gst_omx_amrwbdec_get_type,
86       gst_omx_amrwbenc_get_type,
87       gst_omx_aacdec_get_type,
88       gst_omx_aacenc_get_type,
89       gst_omx_adpcmdec_get_type,
90       gst_omx_adpcmenc_get_type,
91       gst_omx_g711dec_get_type,
92       gst_omx_g711enc_get_type,
93       gst_omx_g729dec_get_type,
94       gst_omx_g729enc_get_type,
95       gst_omx_ilbcdec_get_type,
96       gst_omx_ilbcenc_get_type, gst_omx_jpegenc_get_type,
97 #endif /* EXPERIMENTAL */
98       gst_omx_audiosink_get_type,
99 #ifdef EXPERIMENTAL
100       gst_omx_videosink_get_type, gst_omx_filereadersrc_get_type,
101 #endif /* EXPERIMENTAL */
102 gst_omx_volume_get_type,};
103
104 static gchar *
105 get_config_path (void)
106 {
107 /* MODIFICATION */
108 #if 1     /* Fix_config_path */
109   return g_build_filename (OMX_CONFIG_DIRPATH, OMX_CONFIG_FILENAME, NULL);
110 #else
111   gchar *path;
112   const gchar *const *dirs;
113   int i;
114
115   path = g_strdup (g_getenv ("OMX_CONFIG"));
116
117   if (path)
118     return path;
119
120   dirs = g_get_system_config_dirs ();
121   for (i = 0; dirs[i]; i++) {
122     path =
123         g_build_filename (dirs[i], "gstreamer-0.10", "gst-openmax.conf", NULL);
124     if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
125       return path;
126     g_free (path);
127   }
128
129   return g_build_filename (g_get_user_config_dir (), "gst-openmax.conf", NULL);
130 #endif
131 }
132
133 static void
134 fetch_element_table (GstPlugin * plugin)
135 {
136   gchar *path;
137   gchar *config, *s;
138   GstStructure *tmp, *element;
139
140   element_table = gst_plugin_get_cache_data (plugin);
141
142   if (element_table)
143     return;
144
145   path = get_config_path ();
146
147   if (!g_file_get_contents (path, &config, NULL, NULL)) {
148     g_warning ("could not find config file '%s'.. using defaults!", path);
149     config = (gchar *) default_config;
150   }
151
152   gst_plugin_add_dependency_simple (plugin, "ONX_CONFIG", path, NULL,
153       GST_PLUGIN_DEPENDENCY_FLAG_NONE);
154
155   g_free (path);
156
157   GST_DEBUG ("parsing config:\n%s", config);
158
159   tmp = gst_structure_empty_new ("element_table");
160
161   s = config;
162
163   while ((element = gst_structure_from_string (s, &s))) {
164     const gchar *element_name = gst_structure_get_name (element);
165     gst_structure_set (tmp, element_name, GST_TYPE_STRUCTURE, element, NULL);
166   }
167
168   if (config != default_config)
169     g_free (config);
170
171   GST_DEBUG ("element_table=%" GST_PTR_FORMAT, tmp);
172
173   gst_plugin_set_cache_data (plugin, tmp);
174
175   element_table = tmp;
176 }
177
178 static GstStructure *
179 get_element_entry (const gchar * element_name)
180 {
181   GstStructure *element;
182
183   if (!gst_structure_get ((GstStructure *) element_table, element_name,
184           GST_TYPE_STRUCTURE, &element, NULL)) {
185     element = NULL;
186   }
187
188   /* This assert should never fail, because plugin elements are registered
189    * based on the entries in this table.  Someone would have to manually
190    * override the type qdata for this to fail.
191    */
192   g_assert (element);
193
194   return element;
195 }
196
197 /* register a new dynamic sub-class with the name 'type_name'.. this gives us
198  * a way to use the same (for example) GstOmxMp3Dec element mapping to
199  * multiple different element names with different OMX library implementations
200  * and/or component names
201  */
202 static GType
203 create_subtype (GType parent_type, const gchar * type_name)
204 {
205   GTypeQuery q;
206   GTypeInfo i = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
207
208   if (!type_name)
209     return 0;
210
211   g_type_query (parent_type, &q);
212
213   i.class_size = q.class_size;
214   i.instance_size = q.instance_size;
215
216   return g_type_register_static (parent_type, type_name, &i, 0);
217 }
218
219 static gboolean
220 plugin_init (GstPlugin * plugin)
221 {
222   guint i, cnt;
223
224   GST_DEBUG_CATEGORY_INIT (gstomx_debug, "omx", 0, "gst-openmax");
225   GST_DEBUG_CATEGORY_INIT (gstomx_util_debug, "omx_util", 0,
226       "gst-openmax utility");
227
228   element_name_quark = g_quark_from_static_string ("element-name");
229
230   /*
231    * First, call all the _get_type() functions to ensure the types are
232    * registered.
233    */
234   for (i = 0; i < G_N_ELEMENTS (get_type); i++)
235     get_type[i] ();
236
237   fetch_element_table (plugin);
238
239   g_omx_init ();
240
241   cnt = gst_structure_n_fields (element_table);
242   for (i = 0; i < cnt; i++) {
243     const gchar *element_name = gst_structure_nth_field_name (element_table, i);
244     GstStructure *element = get_element_entry (element_name);
245     const gchar *type_name, *parent_type_name;
246     const gchar *component_name, *library_name;
247 //    const gchar *component_role;
248     GType type;
249     gint rank;
250
251     GST_DEBUG ("element_name=%s, element=%" GST_PTR_FORMAT, element_name,
252         element);
253
254     parent_type_name = gst_structure_get_string (element, "parent-type");
255     type_name = gst_structure_get_string (element, "type");
256     component_name = gst_structure_get_string (element, "component-name");
257 //    component_role = gst_structure_get_string (element, "component-role");
258     library_name = gst_structure_get_string (element, "library-name");
259
260     if (!type_name || !component_name || !library_name) {
261       g_warning ("malformed config file: missing required fields for %s",
262           element_name);
263       return FALSE;
264     }
265
266     if (parent_type_name) {
267       type = g_type_from_name (parent_type_name);
268       if (type) {
269         type = create_subtype (type, type_name);
270       } else {
271         g_warning ("malformed config file: invalid parent-type '%s' for %s",
272             parent_type_name, element_name);
273         return FALSE;
274       }
275     } else {
276       type = g_type_from_name (type_name);
277     }
278
279     if (!type) {
280       g_warning ("malformed config file: invalid type '%s' for %s",
281           type_name, element_name);
282       return FALSE;
283     }
284
285     g_type_set_qdata (type, element_name_quark, (gpointer) element_name);
286
287     if (!gst_structure_get_int (element, "rank", &rank)) {
288       /* use default rank: */
289       rank = GST_RANK_NONE;
290     }
291
292     if (!gst_element_register (plugin, element_name, rank, type)) {
293       g_warning ("failed registering '%s'", element_name);
294       return FALSE;
295     }
296   }
297
298   return TRUE;
299 }
300
301 gboolean
302 gstomx_get_component_info (void *core, GType type)
303 {
304   GOmxCore *rcore = core;
305   const gchar *element_name;
306   GstStructure *element;
307   const gchar *str;
308
309   element_name = g_type_get_qdata (type, element_name_quark);
310   element = get_element_entry (element_name);
311
312   if (!element)
313     return FALSE;
314
315   str = gst_structure_get_string (element, "library-name");
316   rcore->library_name = g_strdup (str);
317
318   str = gst_structure_get_string (element, "component-name");
319   rcore->component_name = g_strdup (str);
320
321   str = gst_structure_get_string (element, "component-role");
322   rcore->component_role = g_strdup (str);
323
324   return TRUE;
325 }
326
327 void *
328 gstomx_core_new (void *object, GType type)
329 {
330   GOmxCore *core = g_omx_core_new (object);
331   gstomx_get_component_info (core, type);
332   g_omx_core_init (core);
333   return core;
334 }
335
336 GstCaps *
337 gstomx_template_caps (GType type, const gchar * pad_name)
338 {
339   const gchar *element_name;
340   GstStructure *element;
341   const gchar *caps_str;
342
343   element_name = g_type_get_qdata (type, element_name_quark);
344   element = get_element_entry (element_name);
345
346
347   /* this shouldn't really happen.. */
348   if (!element)
349     return GST_CAPS_ANY;
350
351   caps_str = gst_structure_get_string (element, pad_name);
352
353   GST_DEBUG ("%s: %s", element_name, caps_str);
354
355   /* default to ANY.. at least for now.. maybe when everything is converted
356    * over, we should treat missing caps as a more serious condition?
357    */
358   if (!caps_str) {
359     g_warning ("%s is missing required field: %s", element_name, pad_name);
360     return GST_CAPS_ANY;
361   }
362
363   return gst_caps_from_string (caps_str);
364 }
365
366 void
367 gstomx_install_property_helper (GObjectClass * gobject_class)
368 {
369
370   g_object_class_install_property (gobject_class, ARG_COMPONENT_NAME,
371       g_param_spec_string ("component-name", "Component name",
372           "Name of the OpenMAX IL component to use",
373           NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
374
375   g_object_class_install_property (gobject_class, ARG_COMPONENT_ROLE,
376       g_param_spec_string ("component-role", "Component role",
377           "Role of the OpenMAX IL component",
378           NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
379
380   g_object_class_install_property (gobject_class, ARG_LIBRARY_NAME,
381       g_param_spec_string ("library-name", "Library name",
382           "Name of the OpenMAX IL implementation library to use",
383           NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
384 }
385
386 gboolean
387 gstomx_get_property_helper (void *core, guint prop_id, GValue * value)
388 {
389   GOmxCore *gomx = core;
390   switch (prop_id) {
391     case ARG_COMPONENT_NAME:
392       g_value_set_string (value, gomx->component_name);
393       return TRUE;
394     case ARG_COMPONENT_ROLE:
395       g_value_set_string (value, gomx->component_role);
396       return TRUE;
397     case ARG_LIBRARY_NAME:
398       g_value_set_string (value, gomx->library_name);
399       return TRUE;
400     default:
401       return FALSE;
402   }
403 }
404
405 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
406     GST_VERSION_MINOR,
407     "omx",
408     "OpenMAX IL",
409     plugin_init,
410     PACKAGE_VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)