nexmon – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /* codecs.c
2 * codecs interface 2007 Tomas Kukosa
3 *
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22  
23 #include "config.h"
24  
25 #include <glib.h>
26 #include "codecs.h"
27  
28 #include "G711a/G711adecode.h"
29 #include "G711u/G711udecode.h"
30  
31 #ifdef HAVE_SBC
32 #include "sbc/sbc_private.h"
33 #endif
34  
35 #ifdef HAVE_PLUGINS
36  
37 #include <gmodule.h>
38  
39 #include <wsutil/plugins.h>
40  
41 /*
42 * List of codec plugins.
43 */
44 typedef struct {
45 void (*register_codec_module)(void); /* routine to call to register a codec */
46 } codec_plugin;
47  
48 static GSList *codec_plugins = NULL;
49  
50 /*
51 * Callback for each plugin found.
52 */
53 static gboolean
54 check_for_codec_plugin(GModule *handle)
55 {
56 gpointer gp;
57 void (*register_codec_module)(void);
58 codec_plugin *plugin;
59  
60 /*
61 * Do we have a register_codec_module routine?
62 */
63 if (!g_module_symbol(handle, "register_codec_module", &gp)) {
64 /* No, so this isn't a codec plugin. */
65 return FALSE;
66 }
67  
68 /*
69 * Yes - this plugin includes one or more codecs.
70 */
71 register_codec_module = (void (*)(void))gp;
72  
73 /*
74 * Add this one to the list of codec plugins.
75 */
76 plugin = (codec_plugin *)g_malloc(sizeof (codec_plugin));
77 plugin->register_codec_module = register_codec_module;
78 codec_plugins = g_slist_append(codec_plugins, plugin);
79 return TRUE;
80 }
81  
82 void
83 codec_register_plugin_types(void)
84 {
85 add_plugin_type("codec", check_for_codec_plugin);
86 }
87  
88 static void
89 register_codec_plugin(gpointer data, gpointer user_data _U_)
90 {
91 codec_plugin *plugin = (codec_plugin *)data;
92  
93 (plugin->register_codec_module)();
94 }
95  
96  
97 /*
98 * For all codec plugins, call their register routines.
99 */
100 void
101 register_all_codecs(void)
102 {
103 register_codec("g711U", codec_g711u_init, codec_g711u_release,
104 codec_g711u_get_channels, codec_g711u_get_frequency, codec_g711u_decode);
105 register_codec("g711A", codec_g711a_init, codec_g711a_release,
106 codec_g711a_get_channels, codec_g711a_get_frequency, codec_g711a_decode);
107 #ifdef HAVE_SPANDSP
108 register_codec("g722", codec_g722_init, codec_g722_release,
109 codec_g722_get_channels, codec_g722_get_frequency, codec_g722_decode);
110 register_codec("g726", codec_g726_init, codec_g726_release,
111 codec_g726_get_channels, codec_g726_get_frequency, codec_g726_decode);
112 #endif
113 #ifdef HAVE_SBC
114 register_codec("SBC", codec_sbc_init, codec_sbc_release,
115 codec_sbc_get_channels, codec_sbc_get_frequency, codec_sbc_decode);
116 #endif
117  
118 g_slist_foreach(codec_plugins, register_codec_plugin, NULL);
119 }
120 #endif /* HAVE_PLUGINS */
121  
122 struct codec_handle {
123 const char *name;
124 codec_init_fn init_fn;
125 codec_release_fn release_fn;
126 codec_get_channels_fn channels_fn;
127 codec_get_frequency_fn frequency_fn;
128 codec_decode_fn decode_fn;
129 };
130  
131 /*
132 * List of registered codecs.
133 */
134 static GHashTable *registered_codecs = NULL;
135  
136  
137 /* Find a registered codec by name. */
138 codec_handle_t
139 find_codec(const char *name)
140 {
141 return (registered_codecs) ? (codec_handle_t)g_hash_table_lookup(registered_codecs, name) : NULL;
142 }
143  
144 /* Register a codec by name. */
145 gboolean
146 register_codec(const char *name, codec_init_fn init_fn, codec_release_fn release_fn,
147 codec_get_channels_fn channels_fn, codec_get_frequency_fn frequency_fn,
148 codec_decode_fn decode_fn)
149 {
150 struct codec_handle *handle;
151  
152 /* Create our hash table if it doesn't already exist */
153 if (registered_codecs == NULL)
154 registered_codecs = g_hash_table_new(g_str_hash, g_str_equal);
155  
156 /* Make sure the registration is unique */
157 if (g_hash_table_lookup(registered_codecs, name) != NULL)
158 return FALSE; /* report an error, or have our caller do it? */
159  
160 handle = (struct codec_handle *)g_malloc(sizeof (struct codec_handle));
161 handle->name = name;
162 handle->init_fn = init_fn;
163 handle->release_fn = release_fn;
164 handle->channels_fn = channels_fn;
165 handle->frequency_fn = frequency_fn;
166 handle->decode_fn = decode_fn;
167  
168 g_hash_table_insert(registered_codecs, (gpointer)name, (gpointer) handle);
169 return TRUE;
170 }
171  
172 /* Deregister a codec by name. */
173 gboolean
174 deregister_codec(const char *name)
175 {
176 gpointer key, value;
177  
178 if (registered_codecs && g_hash_table_lookup_extended(registered_codecs, name, &key, &value)) {
179 g_hash_table_remove(registered_codecs, name);
180 g_free(value);
181 return TRUE;
182 }
183 return FALSE;
184 }
185  
186 void *codec_init(codec_handle_t codec)
187 {
188 if (!codec) return NULL;
189 return (codec->init_fn)();
190 }
191  
192 void codec_release(codec_handle_t codec, void *context)
193 {
194 if (!codec) return;
195 (codec->release_fn)(context);
196 }
197  
198 unsigned codec_get_channels(codec_handle_t codec, void *context)
199 {
200 if (!codec) return 0;
201 return (codec->channels_fn)(context);
202 }
203  
204 unsigned codec_get_frequency(codec_handle_t codec, void *context)
205 {
206 if (!codec) return 0;
207 return (codec->frequency_fn)(context);
208 }
209  
210 size_t codec_decode(codec_handle_t codec, void *context, const void *input, size_t inputSizeBytes, void *output, size_t *outputSizeBytes)
211 {
212 if (!codec) return 0;
213 return (codec->decode_fn)(context, input, inputSizeBytes, output, outputSizeBytes);
214 }
215  
216 /*
217 * Editor modelines - http://www.wireshark.org/tools/modelines.html
218 *
219 * Local variables:
220 * c-basic-offset: 4
221 * tab-width: 8
222 * indent-tabs-mode: nil
223 * End:
224 *
225 * vi: set shiftwidth=4 tabstop=8 expandtab:
226 * :indentSize=4:tabSize=8:noTabs=true:
227 */