]> git.sesse.net Git - vlc/blob - modules/visualization/projectm.cpp
aout_buffer_t.start_data -> aout_buffer_t.i_pts
[vlc] / modules / visualization / projectm.cpp
1 /*****************************************************************************
2  * projectm: visualization module based on libprojectM
3  *****************************************************************************
4  * Copyright (C) 2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: RĂ©mi Duraffort <ivoire@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the Free
11  * Software Foundation; either version 2 of the License, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17  * more details.
18  *
19  * You should have received a copy of the GNU General Public License along with
20  * this program; if not, write to the Free Software Foundation, Inc., 51
21  * Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include <vlc_plugin.h>
30 #include <vlc_aout.h>
31 #include <vlc_vout.h>
32
33 #include <libprojectM/projectM.hpp>
34
35
36 /*****************************************************************************
37  * Module descriptor
38  *****************************************************************************/
39 static int  Open         ( vlc_object_t * );
40 static void Close        ( vlc_object_t * );
41
42 #define CONFIG_TEXT N_("projectM configuration file")
43 #define CONFIG_LONGTEXT N_("File that will be used to configure the projectM " \
44                            "module.")
45
46 #define WIDTH_TEXT N_("Video width")
47 #define WIDTH_LONGTEXT N_("The width of the video window, in pixels.")
48
49 #define HEIGHT_TEXT N_("Video height")
50 #define HEIGHT_LONGTEXT N_("The height of the video window, in pixels.")
51
52 vlc_module_begin ()
53     set_shortname( N_("projectM"))
54     set_description( N_("libprojectM effect") )
55     set_capability( "visualization", 0 )
56     set_category( CAT_AUDIO )
57     set_subcategory( SUBCAT_AUDIO_VISUAL )
58     add_file( "projectm-config", "/usr/share/projectM/config.inp", NULL,
59                 CONFIG_TEXT, CONFIG_LONGTEXT, true )
60     add_integer( "projectm-width", 800, NULL, WIDTH_TEXT, WIDTH_LONGTEXT,
61                  false )
62     add_integer( "projectm-height", 640, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT,
63                  false )
64     add_shortcut( "projectm" )
65     set_callbacks( Open, Close )
66 vlc_module_end ()
67
68
69 /*****************************************************************************
70  * Local prototypes
71  *****************************************************************************/
72 typedef struct
73 {
74     VLC_COMMON_MEMBERS
75
76     /* video output module and opengl provider */
77     vout_thread_t *p_opengl;
78     module_t      *p_module;
79
80     /* libprojectM objects */
81     projectM      *p_projectm;
82     char          *psz_config;
83
84     /* Window size */
85     int i_width;
86     int i_height;
87
88     /* audio info */
89     int i_channels;
90     float *p_buffer;
91     int   i_buffer_size;
92     int   i_nb_samples;
93
94     vlc_mutex_t lock;
95 } projectm_thread_t;
96
97
98 struct aout_filter_sys_t
99 {
100     projectm_thread_t *p_thread;
101 };
102
103
104 static void DoWork( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
105                     aout_buffer_t * );
106 static void* Thread( vlc_object_t * );
107
108
109 /**
110  * Init the openGL context
111  * p_thread: projectm thread object
112  * @return VLC_SUCCESS or vlc error codes
113  */
114 static int initOpenGL( projectm_thread_t *p_thread )
115 {
116     p_thread->p_opengl = (vout_thread_t *)vlc_object_create( p_thread,
117                                                     sizeof( vout_thread_t ) );
118     if( !p_thread->p_opengl )
119         return VLC_ENOMEM;
120
121     vlc_object_attach( p_thread->p_opengl, p_thread );
122
123     /* Initialize the opengl object */
124     video_format_Setup( &p_thread->p_opengl->fmt_in, VLC_CODEC_RGB32,
125                         p_thread->i_width, p_thread->i_height, 1 );
126     p_thread->p_opengl->i_window_width = p_thread->i_width;
127     p_thread->p_opengl->i_window_height = p_thread->i_height;
128     p_thread->p_opengl->render.i_width = p_thread->i_width;
129     p_thread->p_opengl->render.i_height = p_thread->i_height;
130     p_thread->p_opengl->render.i_aspect = VOUT_ASPECT_FACTOR;
131     p_thread->p_opengl->b_fullscreen = false;
132     p_thread->p_opengl->b_autoscale = true;
133     p_thread->p_opengl->i_alignment = 0;
134     p_thread->p_opengl->fmt_in.i_sar_num = 1;
135     p_thread->p_opengl->fmt_in.i_sar_den = 1;
136     p_thread->p_opengl->fmt_render = p_thread->p_opengl->fmt_in;
137
138     /* Ask for the opengl provider */
139     p_thread->p_module = module_need( p_thread->p_opengl, "opengl provider",
140                                       NULL, false );
141     if( !p_thread->p_module )
142     {
143         msg_Err( p_thread, "unable to initialize OpenGL" );
144         vlc_object_detach( p_thread->p_opengl );
145         vlc_object_release( p_thread->p_opengl );
146         return VLC_EGENERIC;
147     }
148
149     return VLC_SUCCESS;
150 }
151
152
153 /**
154  * Open the module
155  * @param p_this: the filter object
156  * @return VLC_SUCCESS or vlc error codes
157  */
158 static int Open( vlc_object_t * p_this )
159 {
160     aout_filter_t       *p_filter = (aout_filter_t *)p_this;
161     aout_filter_sys_t   *p_sys;
162     projectm_thread_t   *p_thread;
163
164     /* Test the audio format */
165     if( p_filter->input.i_format != VLC_CODEC_FL32 ||
166         p_filter->output.i_format != VLC_CODEC_FL32 )
167     {
168         msg_Warn( p_filter, "bad input or output format" );
169         return VLC_EGENERIC;
170     }
171     if( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
172     {
173         msg_Warn( p_filter, "input and outut are not similar" );
174         return VLC_EGENERIC;
175     }
176
177     p_filter->pf_do_work = DoWork;
178     p_filter->b_in_place = true;
179
180     p_sys = p_filter->p_sys = (aout_filter_sys_t*)malloc( sizeof( *p_sys ) );
181     if( !p_sys )
182         return VLC_ENOMEM;
183
184     /* Create the object for the thread */
185     p_sys->p_thread = p_thread = (projectm_thread_t *)
186                     vlc_object_create( p_filter, sizeof( projectm_thread_t ) );
187     vlc_object_attach( p_sys->p_thread, p_filter );
188     p_thread->i_width  = var_CreateGetInteger( p_filter, "projectm-width" );
189     p_thread->i_height = var_CreateGetInteger( p_filter, "projectm-height" );
190
191     /* Create the openGL provider */
192     int i_ret = initOpenGL( p_sys->p_thread );
193     if( i_ret != VLC_SUCCESS )
194     {
195         vlc_object_detach( p_sys->p_thread );
196         vlc_object_release( p_sys->p_thread );
197         free( p_sys );
198         return i_ret;
199     }
200
201     p_thread->i_channels = aout_FormatNbChannels( &p_filter->input );
202     p_thread->psz_config = var_CreateGetString( p_filter, "projectm-config" );
203     vlc_mutex_init( &p_thread->lock );
204     p_thread->p_buffer = NULL;
205     p_thread->i_buffer_size = 0;
206     p_thread->i_nb_samples = 0;
207
208     /* Create the thread */
209     if( vlc_thread_create( p_thread, "projectm update thread", Thread,
210                            VLC_THREAD_PRIORITY_LOW ) )
211     {
212         msg_Err( p_filter, "cannot launch the projectm thread" );
213         vlc_object_detach( p_thread );
214         vlc_object_release( p_thread );
215         free (p_sys );
216         return VLC_EGENERIC;
217     }
218
219     return VLC_SUCCESS;
220 }
221
222
223 /**
224  * Close the module
225  * @param p_this: the filter object
226  */
227 static void Close( vlc_object_t *p_this )
228 {
229     aout_filter_t     *p_filter = (aout_filter_t *)p_this;
230     aout_filter_sys_t *p_sys = p_filter->p_sys;
231     projectm_thread_t *p_thread = p_sys->p_thread;
232
233     /* Stop the thread */
234     vlc_object_kill( p_thread );
235     vlc_thread_join( p_thread );
236
237     /* Free the ressources */
238     vlc_mutex_destroy( &p_thread->lock );
239     free( p_thread->p_buffer );
240     free( p_thread->psz_config );
241
242     vlc_object_detach( p_thread );
243     vlc_object_release( p_thread );
244
245     free( p_sys );
246 }
247
248
249 /**
250  * Do the actual work with the new sample
251  * @param p_aout: audio output object
252  * @param p_filter: filter object
253  * @param p_in_buf: input buffer
254  * @param p_out_buf: output buffer
255  */
256 static void DoWork( aout_instance_t *p_aout, aout_filter_t *p_filter,
257                     aout_buffer_t *p_in_buf, aout_buffer_t *p_out_buf )
258 {
259     projectm_thread_t *p_thread = p_filter->p_sys->p_thread;
260
261     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
262     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes;
263
264     vlc_mutex_lock( &p_thread->lock );
265     if( p_thread->i_buffer_size > 0 )
266     {
267         p_thread->p_buffer[0] = 0;
268         p_thread->i_nb_samples = __MIN( p_thread->i_buffer_size,
269                                         p_in_buf->i_nb_samples );
270         for( int i = 0; i < p_thread->i_nb_samples; i++ )
271             p_thread->p_buffer[i] = p_in_buf->p_buffer[i];
272     }
273
274     vlc_mutex_unlock( &p_thread->lock );
275
276     return;
277 }
278
279
280 /**
281  * ProjectM update thread which do the rendering
282  * @param p_this: the p_thread object
283  */
284 static void* Thread( vlc_object_t *p_this )
285 {
286     /* we don't want to be interupted in this thread */
287     int cancel = vlc_savecancel();
288     projectm_thread_t *p_thread = (projectm_thread_t *)p_this;
289
290     /* Initialize the opengl provider for this thread */
291     p_thread->p_opengl->pf_init( p_thread->p_opengl );
292
293     /* Create the projectM object */
294     p_thread->p_projectm = new projectM( p_thread->psz_config );
295     p_thread->i_buffer_size = p_thread->p_projectm->pcm()->maxsamples;
296     p_thread->p_buffer = (float*)malloc( p_thread->i_buffer_size *
297                                          sizeof( float ) );
298
299     /* TODO: Give to projectm the name of the input
300     p_thread->p_projectm->projectM_setTitle( "" ); */
301
302     /* Reset the dislay to get the right size */
303     p_thread->p_projectm->projectM_resetGL( p_thread->i_width,
304                                             p_thread->i_height );
305
306     while( vlc_object_alive( p_thread ) )
307     {
308         /* Manage the events */
309         p_thread->p_opengl->pf_manage( p_thread->p_opengl );
310         /* Render the image and swap the buffers */
311         vlc_mutex_lock( &p_thread->lock );
312         if( p_thread->i_nb_samples > 0 )
313             p_thread->p_projectm->pcm()->addPCMfloat( p_thread->p_buffer,
314                                                       p_thread->i_nb_samples );
315
316         p_thread->p_projectm->renderFrame();
317         p_thread->p_opengl->pf_swap( p_thread->p_opengl );
318         vlc_mutex_unlock( &p_thread->lock );
319
320         /* TODO: use a fps limiter */
321         msleep( 10000 );
322     }
323
324
325     /* Cleanup */
326     delete p_thread->p_projectm;
327
328     /* Free the openGL provider */
329     module_unneed( p_thread->p_opengl, p_thread->p_module );
330     vlc_object_detach( p_thread->p_opengl );
331     vlc_object_release( p_thread->p_opengl );
332
333
334     vlc_restorecancel( cancel );
335     return NULL;
336 }