1 /* Libvisual - The audio visualisation framework.
2  *
3  * Copyright (C) 2004, 2005 Dennis Smit <ds@nerds-incorporated.org>
4  *
5  * Authors: Dennis Smit <ds@nerds-incorporated.org>
6  *
7  * $Id:
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as
11  * published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 
29 #include "lv_list.h"
30 #include "lv_input.h"
31 
32 extern VisList *__lv_plugins_input;
33 
34 static int input_dtor (VisObject *object);
35 
36 static VisInputPlugin *get_input_plugin (VisInput *input);
37 
input_dtor(VisObject * object)38 static int input_dtor (VisObject *object)
39 {
40 	VisInput *input = VISUAL_INPUT (object);
41 
42 	if (input->plugin != NULL)
43 		visual_plugin_unload (input->plugin);
44 
45 	if (input->audio != NULL)
46 		visual_object_unref (VISUAL_OBJECT (input->audio));
47 
48 	input->plugin = NULL;
49 	input->audio = NULL;
50 
51 	return VISUAL_OK;
52 }
53 
get_input_plugin(VisInput * input)54 static VisInputPlugin *get_input_plugin (VisInput *input)
55 {
56 	VisInputPlugin *inplugin;
57 
58 	visual_log_return_val_if_fail (input != NULL, NULL);
59 	visual_log_return_val_if_fail (input->plugin != NULL, NULL);
60 
61 	inplugin = VISUAL_PLUGIN_INPUT (input->plugin->info->plugin);
62 
63 	return inplugin;
64 }
65 
66 /**
67  * @defgroup VisInput VisInput
68  * @{
69  */
70 
71 /**
72  * Gives the encapsulated VisPluginData from a VisInput.
73  *
74  * @param input Pointer of a VisInput of which the VisPluginData needs to be returned.
75  *
76  * @return VisPluginData that is encapsulated in the VisInput, possibly NULL.
77  */
visual_input_get_plugin(VisInput * input)78 VisPluginData *visual_input_get_plugin (VisInput *input)
79 {
80 	        return input->plugin;
81 }
82 
83 /**
84  * Gives a list of input plugins in the current plugin registry.
85  *
86  * @return An VisList of VisPluginRef's containing the input plugins in the plugin registry.
87  */
visual_input_get_list()88 VisList *visual_input_get_list ()
89 {
90 	return __lv_plugins_input;
91 }
92 
93 /**
94  * Gives the next input plugin based on the name of a plugin.
95  *
96  * @see visual_input_get_prev_by_name
97  *
98  * @param name The name of the current plugin, or NULL to get the first.
99  *
100  * @return The name of the next plugin within the list.
101  */
visual_input_get_next_by_name(const char * name)102 const char *visual_input_get_next_by_name (const char *name)
103 {
104 	return visual_plugin_get_next_by_name (visual_input_get_list (), name);
105 }
106 
107 /**
108  * Gives the previous input plugin based on the name of a plugin.
109  *
110  * @see visual_input_get_next_by_name
111  *
112  * @param name The name of the current plugin. or NULL to get the last.
113  *
114  * @return The name of the previous plugin within the list.
115  */
visual_input_get_prev_by_name(const char * name)116 const char *visual_input_get_prev_by_name (const char *name)
117 {
118 	return visual_plugin_get_prev_by_name (visual_input_get_list (), name);
119 }
120 
121 
122 /**
123  * Checks if the input plugin is in the registry, based on it's name.
124  *
125  * @param name The name of the plugin that needs to be checked.
126  *
127  * @return TRUE if found, else FALSE.
128  */
visual_input_valid_by_name(const char * name)129 int visual_input_valid_by_name (const char *name)
130 {
131 	if (visual_plugin_find (visual_input_get_list (), name) == NULL)
132 		return FALSE;
133 	else
134 		return TRUE;
135 }
136 
137 /**
138  * Creates a new VisInput from name, the plugin will be loaded but won't be realized.
139  *
140  * @param inputname
141  * 	The name of the plugin to load, or NULL to simply allocate a new
142  * 	input.
143  *
144  * @return A newly allocated VisInput, optionally containing a loaded plugin. Or NULL on failure.
145  */
visual_input_new(const char * inputname)146 VisInput *visual_input_new (const char *inputname)
147 {
148 	VisInput *input;
149 	VisPluginRef *ref;
150 
151 //	visual_log_return_val_if_fail (__lv_plugins_input != NULL && inputname == NULL, NULL);
152 
153 	if (__lv_plugins_input == NULL && inputname != NULL) {
154 		visual_log (VISUAL_LOG_CRITICAL, "the plugin list is NULL");
155 		return NULL;
156 	}
157 
158 	input = visual_mem_new0 (VisInput, 1);
159 
160 	input->audio = visual_audio_new ();
161 
162 	/* Do the VisObject initialization */
163 	visual_object_initialize (VISUAL_OBJECT (input), TRUE, input_dtor);
164 
165 	if (inputname == NULL)
166 		return input;
167 
168 	ref = visual_plugin_find (__lv_plugins_input, inputname);
169 
170 	input->plugin = visual_plugin_load (ref);
171 
172 	return input;
173 }
174 
175 /**
176  * Realize the VisInput. This also calls the plugin init function.
177  *
178  * @param input Pointer to a VisInput that needs to be realized.
179  *
180  * @return VISUAL_OK on succes, -VISUAL_ERROR_INPUT_NULL or error values returned by
181  *	visual_plugin_realize () on failure.
182  */
visual_input_realize(VisInput * input)183 int visual_input_realize (VisInput *input)
184 {
185 	visual_log_return_val_if_fail (input != NULL, -VISUAL_ERROR_INPUT_NULL);
186 
187 	if (input->plugin != NULL && input->callback == NULL)
188 		return visual_plugin_realize (input->plugin);
189 
190 	return VISUAL_OK;
191 }
192 
193 /**
194  * Sets a callback function for VisInput. Callback functions can be used instead of plugins. Using
195  * a callback function you can implement an in app PCM data upload function which is like the
196  * upload callback that is used for input plugins.
197  *
198  * @param input Pointer to a VisInput that to which a callback needs to be set.
199  * @param callback The in app callback function that should be used instead of a plugin.
200  * @param priv A private that can be read within the callback function.
201  *
202  * @return VISUAL_OK on succes, -VISUAL_ERROR_INPUT_NULL on failure.
203  */
visual_input_set_callback(VisInput * input,VisInputUploadCallbackFunc callback,void * priv)204 int visual_input_set_callback (VisInput *input, VisInputUploadCallbackFunc callback, void *priv)
205 {
206 	visual_log_return_val_if_fail (input != NULL, -VISUAL_ERROR_INPUT_NULL);
207 
208 	input->callback = callback;
209 	visual_object_set_private (VISUAL_OBJECT (input), priv);
210 
211 	return VISUAL_OK;
212 }
213 
214 /**
215  * This is called to run a VisInput. This function will call the plugin to upload it's samples and run it
216  * through the visual_audio_analyze function. If a callback is set it will use the callback instead of
217  * the plugin.
218  *
219  * @param input A pointer to a VisInput that needs to be runned.
220  *
221  * @return VISUAL_OK on succes, -VISUAL_ERROR_INPUT_NULL or -VISUAL_ERROR_INPUT_PLUGIN_NULL on failure.
222  */
visual_input_run(VisInput * input)223 int visual_input_run (VisInput *input)
224 {
225 	VisInputPlugin *inplugin;
226 
227 	visual_log_return_val_if_fail (input != NULL, -VISUAL_ERROR_INPUT_NULL);
228 
229 	if (input->callback == NULL) {
230 		inplugin = get_input_plugin (input);
231 
232 		if (inplugin == NULL) {
233 			visual_log (VISUAL_LOG_CRITICAL, "The input plugin is not loaded correctly.");
234 
235 			return -VISUAL_ERROR_INPUT_PLUGIN_NULL;
236 		}
237 
238 		inplugin->upload (input->plugin, input->audio);
239 	} else
240 		input->callback (input, input->audio, visual_object_get_private (VISUAL_OBJECT (input)));
241 
242 	visual_audio_analyze (input->audio);
243 
244 	return VISUAL_OK;
245 }
246 
247 /**
248  * @}
249  */
250 
251