]> git.sesse.net Git - vlc/blob - modules/visualization/projectm.cpp
opensles: implement mute/volume set
[vlc] / modules / visualization / projectm.cpp
1 /*****************************************************************************
2  * projectm.cpp: visualization module based on libprojectM
3  *****************************************************************************
4  * Copyright © 2009-2011 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Rémi Duraffort <ivoire@videolan.org>
8  *          Laurent Aimar
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 2 of the License, or (at your option)
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
18  * more details.
19  *
20  * You should have received a copy of the GNU General Public License along with
21  * this program; if not, write to the Free Software Foundation, Inc., 51
22  * Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28 #ifndef __STDC_CONSTANT_MACROS
29 # define __STDC_CONSTANT_MACROS
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_aout.h>
35 #include <vlc_vout.h>
36 #include <vlc_vout_wrapper.h>
37 #include <vlc_opengl.h>
38 #include <vlc_filter.h>
39 #include <vlc_rand.h>
40
41 #include <libprojectM/projectM.hpp>
42
43 /*****************************************************************************
44  * Module descriptor
45  *****************************************************************************/
46 static int  Open         ( vlc_object_t * );
47 static void Close        ( vlc_object_t * );
48
49 #define CONFIG_TEXT N_("projectM configuration file")
50 #define CONFIG_LONGTEXT N_("File that will be used to configure the projectM " \
51                            "module.")
52
53 #define PRESET_PATH_TXT N_("projectM preset path")
54 #define PRESET_PATH_LONGTXT N_("Path to the projectM preset directory")
55
56 #define TITLE_FONT_TXT N_("Title font")
57 #define TITLE_FONT_LONGTXT N_("Font used for the titles")
58
59 #define MENU_FONT_TXT N_("Font menu")
60 #define MENU_FONT_LONGTXT N_("Font used for the menus")
61
62 #define WIDTH_TEXT N_("Video width")
63 #define WIDTH_LONGTEXT N_("The width of the video window, in pixels.")
64
65 #define HEIGHT_TEXT N_("Video height")
66 #define HEIGHT_LONGTEXT N_("The height of the video window, in pixels.")
67
68 #define MESHX_TEXT N_("Mesh width")
69 #define MESHX_LONGTEXT N_("The width of the mesh, in pixels.")
70
71 #define MESHY_TEXT N_("Mesh height")
72 #define MESHY_LONGTEXT N_("The height of the mesh, in pixels.")
73
74 #define TEXTURE_TEXT N_("Texture size")
75 #define TEXTURE_LONGTEXT N_("The size of the texture, in pixels.")
76
77 #ifdef WIN32
78 # define FONT_PATH      "C:\\WINDOWS\\Fonts\\arial.ttf"
79 # define FONT_PATH_MENU "C:\\WINDOWS\\Fonts\\arial.ttf"
80 # define PRESET_PATH    NULL
81 #else
82 # define FONT_PATH      "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf"
83 # define FONT_PATH_MENU "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf"
84 # define PRESET_PATH    "/usr/share/projectM/presets"
85 #endif
86
87 vlc_module_begin ()
88     set_shortname( N_("projectM"))
89     set_description( N_("libprojectM effect") )
90     set_capability( "visualization2", 0 )
91     set_category( CAT_AUDIO )
92     set_subcategory( SUBCAT_AUDIO_VISUAL )
93 #ifndef HAVE_PROJECTM2
94     add_loadfile( "projectm-config", "/usr/share/projectM/config.inp",
95                   CONFIG_TEXT, CONFIG_LONGTEXT, true )
96 #else
97     add_directory( "projectm-preset-path", PRESET_PATH,
98                   PRESET_PATH_TXT, PRESET_PATH_LONGTXT, true )
99     add_loadfile( "projectm-title-font", FONT_PATH,
100                   TITLE_FONT_TXT, TITLE_FONT_LONGTXT, true )
101     add_loadfile( "projectm-menu-font", FONT_PATH_MENU,
102                   MENU_FONT_TXT, MENU_FONT_LONGTXT, true )
103 #endif
104     add_integer( "projectm-width", 800, WIDTH_TEXT, WIDTH_LONGTEXT,
105                  false )
106     add_integer( "projectm-height", 500, HEIGHT_TEXT, HEIGHT_LONGTEXT,
107                  false )
108     add_integer( "projectm-meshx", 32, MESHX_TEXT, MESHX_LONGTEXT,
109                  false )
110     add_integer( "projectm-meshy", 24, MESHY_TEXT, MESHY_LONGTEXT,
111                  false )
112     add_integer( "projectm-texture-size", 1024, TEXTURE_TEXT, TEXTURE_LONGTEXT,
113                  false )
114     add_shortcut( "projectm" )
115     set_callbacks( Open, Close )
116 vlc_module_end ()
117
118
119 /*****************************************************************************
120  * Local prototypes
121  *****************************************************************************/
122 struct filter_sys_t
123 {
124     /* */
125     vlc_thread_t thread;
126     vlc_sem_t    ready;
127     bool         b_error;
128
129     /* Opengl */
130     vout_thread_t  *p_vout;
131     vout_display_t *p_vd;
132
133     /* Window size */
134     int i_width;
135     int i_height;
136
137     /* audio info */
138     int i_channels;
139
140     /* */
141     vlc_mutex_t lock;
142     bool  b_quit;
143     float *p_buffer;
144     unsigned i_buffer_size;
145     unsigned i_nb_samples;
146 };
147
148
149 static block_t *DoWork( filter_t *, block_t * );
150 static void *Thread( void * );
151
152 /**
153  * Open the module
154  * @param p_this: the filter object
155  * @return VLC_SUCCESS or vlc error codes
156  */
157 static int Open( vlc_object_t * p_this )
158 {
159     filter_t     *p_filter = (filter_t *)p_this;
160     filter_sys_t *p_sys;
161
162     /* Test the audio format */
163     if( p_filter->fmt_in.audio.i_format != VLC_CODEC_FL32 ||
164         p_filter->fmt_out.audio.i_format != VLC_CODEC_FL32 )
165     {
166         msg_Warn( p_filter, "bad input or output format" );
167         return VLC_EGENERIC;
168     }
169     if( !AOUT_FMTS_SIMILAR( &p_filter->fmt_in.audio, &p_filter->fmt_out.audio ) )
170     {
171         msg_Warn( p_filter, "input and outut are not similar" );
172         return VLC_EGENERIC;
173     }
174
175     p_filter->pf_audio_filter = DoWork;
176
177     p_sys = p_filter->p_sys = (filter_sys_t*)malloc( sizeof( *p_sys ) );
178     if( !p_sys )
179         return VLC_ENOMEM;
180
181     /* Create the object for the thread */
182     vlc_sem_init( &p_sys->ready, 0 );
183     p_sys->b_error       = false;
184     p_sys->b_quit        = false;
185     p_sys->i_width       = var_InheritInteger( p_filter, "projectm-width" );
186     p_sys->i_height      = var_InheritInteger( p_filter, "projectm-height" );
187     p_sys->i_channels    = aout_FormatNbChannels( &p_filter->fmt_in.audio );
188     vlc_mutex_init( &p_sys->lock );
189     p_sys->p_buffer      = NULL;
190     p_sys->i_buffer_size = 0;
191     p_sys->i_nb_samples  = 0;
192
193     /* Create the thread */
194     if( vlc_clone( &p_sys->thread, Thread, p_filter, VLC_THREAD_PRIORITY_LOW ) )
195         goto error;
196
197     vlc_sem_wait( &p_sys->ready );
198     if( p_sys->b_error )
199     {
200         vlc_join( p_sys->thread, NULL );
201         goto error;
202     }
203
204     return VLC_SUCCESS;
205
206 error:
207     vlc_sem_destroy( &p_sys->ready );
208     free (p_sys );
209     return VLC_EGENERIC;
210 }
211
212
213 /**
214  * Close the module
215  * @param p_this: the filter object
216  */
217 static void Close( vlc_object_t *p_this )
218 {
219     filter_t  *p_filter = (filter_t *)p_this;
220     filter_sys_t *p_sys = p_filter->p_sys;
221
222     /* Stop the thread
223      * XXX vlc_cleanup_push does not seems to work with C++ so no
224      * vlc_cancel()... */
225     vlc_mutex_lock( &p_sys->lock );
226     p_sys->b_quit = true;
227     vlc_mutex_unlock( &p_sys->lock );
228
229     vlc_join( p_sys->thread, NULL );
230
231     /* Free the ressources */
232     vlc_sem_destroy( &p_sys->ready );
233     vlc_mutex_destroy( &p_sys->lock );
234     free( p_sys->p_buffer );
235     free( p_sys );
236 }
237
238
239 /**
240  * Do the actual work with the new sample
241  * @param p_aout: audio output object
242  * @param p_filter: filter object
243  * @param p_in_buf: input buffer
244  * @param p_out_buf: output buffer
245  */
246 static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
247 {
248     filter_sys_t *p_sys = p_filter->p_sys;
249
250     vlc_mutex_lock( &p_sys->lock );
251     if( p_sys->i_buffer_size > 0 )
252     {
253         p_sys->i_nb_samples = __MIN( p_sys->i_buffer_size,
254                                      p_in_buf->i_nb_samples );
255
256         const float *p_src = (float*)p_in_buf->p_buffer;
257         for( unsigned i = 0; i < p_sys->i_nb_samples; i++ )
258         {
259             float v = 0;
260             for( int j = 0; j < p_sys->i_channels; j++ )
261                 v += p_src[p_sys->i_channels * i + j];
262             p_sys->p_buffer[i] = v / p_sys->i_channels;
263         }
264     }
265     vlc_mutex_unlock( &p_sys->lock );
266
267     return p_in_buf;
268 }
269
270 /**
271  * Variable callback for the dummy vout
272  */
273 static int VoutCallback( vlc_object_t *p_vout, char const *psz_name,
274                          vlc_value_t oldv, vlc_value_t newv, void *p_data )
275 {
276     VLC_UNUSED( p_vout ); VLC_UNUSED( oldv );
277     vout_display_t *p_vd = (vout_display_t*)p_data;
278
279     if( !strcmp(psz_name, "fullscreen") )
280     {
281         vout_SetDisplayFullscreen( p_vd, newv.b_bool );
282     }
283     return VLC_SUCCESS;
284 }
285
286 /**
287  * ProjectM update thread which do the rendering
288  * @param p_this: the p_thread object
289  */
290 static void *Thread( void *p_data )
291 {
292     filter_t  *p_filter = (filter_t*)p_data;
293     filter_sys_t *p_sys = p_filter->p_sys;
294
295     video_format_t fmt;
296     vlc_gl_t *gl;
297     unsigned int i_last_width  = 0;
298     unsigned int i_last_height = 0;
299     locale_t loc;
300     locale_t oldloc;
301
302     projectM *p_projectm;
303 #ifndef HAVE_PROJECTM2
304     char *psz_config;
305 #else
306     char *psz_preset_path;
307     char *psz_title_font;
308     char *psz_menu_font;
309     projectM::Settings settings;
310 #endif
311
312     vlc_savecancel();
313
314     /* Create the openGL provider */
315     p_sys->p_vout =
316         (vout_thread_t *)vlc_object_create( p_filter, sizeof(vout_thread_t) );
317     if( !p_sys->p_vout )
318         goto error;
319
320     /* */
321     video_format_Init( &fmt, 0 );
322     video_format_Setup( &fmt, VLC_CODEC_RGB32,
323                         p_sys->i_width, p_sys->i_height, 0, 1 );
324     fmt.i_sar_num = 1;
325     fmt.i_sar_den = 1;
326
327     vout_display_state_t state;
328     memset( &state, 0, sizeof(state) );
329     state.cfg.display.sar.num = 1;
330     state.cfg.display.sar.den = 1;
331     state.cfg.is_display_filled = true;
332     state.cfg.zoom.num = 1;
333     state.cfg.zoom.den = 1;
334     state.sar.num = 1;
335     state.sar.den = 1;
336
337     p_sys->p_vd = vout_NewDisplay( p_sys->p_vout, &fmt, &state, "opengl",
338                                    300000, 1000000 );
339     if( !p_sys->p_vd )
340     {
341         vlc_object_release( p_sys->p_vout );
342         goto error;
343     }
344     var_Create( p_sys->p_vout, "fullscreen", VLC_VAR_BOOL );
345     var_AddCallback( p_sys->p_vout, "fullscreen", VoutCallback, p_sys->p_vd );
346
347     gl = vout_GetDisplayOpengl( p_sys->p_vd );
348     if( !gl )
349     {
350         var_DelCallback( p_sys->p_vout, "fullscreen", VoutCallback, p_sys->p_vd );
351         vout_DeleteDisplay( p_sys->p_vd, NULL );
352         vlc_object_release( p_sys->p_vout );
353         goto error;
354     }
355
356     /* Work-around the projectM locale bug */
357     loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
358     oldloc = uselocale (loc);
359
360     /* Create the projectM object */
361 #ifndef HAVE_PROJECTM2
362     psz_config = var_InheritString( p_filter, "projectm-config" );
363     p_projectm = new projectM( psz_config );
364     free( psz_config );
365 #else
366     psz_preset_path = var_InheritString( p_filter, "projectm-preset-path" );
367 #ifdef WIN32
368     if ( psz_preset_path == NULL )
369     {
370         char *psz_data_path = config_GetDataDir();
371         asprintf( &psz_preset_path, "%s" DIR_SEP "visualization", psz_data_path );
372         free( psz_data_path );
373     }
374 #endif
375
376     psz_title_font                = var_InheritString( p_filter, "projectm-title-font" );
377     psz_menu_font                 = var_InheritString( p_filter, "projectm-menu-font" );
378
379     settings.meshX                = var_InheritInteger( p_filter, "projectm-meshx" );
380     settings.meshY                = var_InheritInteger( p_filter, "projectm-meshy" );
381     settings.fps                  = 35;
382     settings.textureSize          = var_InheritInteger( p_filter, "projectm-texture-size" );
383     settings.windowWidth          = p_sys->i_width;
384     settings.windowHeight         = p_sys->i_height;
385     settings.presetURL            = psz_preset_path;
386     settings.titleFontURL         = psz_title_font;
387     settings.menuFontURL          = psz_menu_font;
388     settings.smoothPresetDuration = 5;
389     settings.presetDuration       = 30;
390     settings.beatSensitivity      = 10;
391     settings.aspectCorrection     = 1;
392     settings.easterEgg            = 1;
393     settings.shuffleEnabled       = 1;
394
395     p_projectm = new projectM( settings );
396
397     free( psz_menu_font );
398     free( psz_title_font );
399     free( psz_preset_path );
400 #endif /* HAVE_PROJECTM2 */
401
402     p_sys->i_buffer_size = p_projectm->pcm()->maxsamples;
403     p_sys->p_buffer = (float*)calloc( p_sys->i_buffer_size,
404                                       sizeof( float ) );
405
406     vlc_sem_post( &p_sys->ready );
407
408     /* Choose a preset randomly or projectM will always show the first one */
409     if ( p_projectm->getPlaylistSize() > 0 )
410         p_projectm->selectPreset( (unsigned)vlc_mrand48() % p_projectm->getPlaylistSize() );
411
412     /* */
413     for( ;; )
414     {
415         const mtime_t i_deadline = mdate() + CLOCK_FREQ / 50; /* 50 fps max */
416         /* Manage the events */
417         vout_ManageDisplay( p_sys->p_vd, true );
418         if( p_sys->p_vd->cfg->display.width  != i_last_width ||
419             p_sys->p_vd->cfg->display.height != i_last_height )
420         {
421             /* FIXME it is not perfect as we will have black bands */
422             vout_display_place_t place;
423             vout_display_PlacePicture( &place, &p_sys->p_vd->source, p_sys->p_vd->cfg, false );
424             p_projectm->projectM_resetGL( place.width, place.height );
425
426             i_last_width  = p_sys->p_vd->cfg->display.width;
427             i_last_height = p_sys->p_vd->cfg->display.height;
428         }
429
430         /* Render the image and swap the buffers */
431         vlc_mutex_lock( &p_sys->lock );
432         if( p_sys->i_nb_samples > 0 )
433         {
434             p_projectm->pcm()->addPCMfloat( p_sys->p_buffer,
435                                             p_sys->i_nb_samples );
436             p_sys->i_nb_samples = 0;
437         }
438         if( p_sys->b_quit )
439         {
440             vlc_mutex_unlock( &p_sys->lock );
441
442             delete p_projectm;
443             var_DelCallback( p_sys->p_vout, "fullscreen", VoutCallback, p_sys->p_vd );
444             vout_DeleteDisplay( p_sys->p_vd, NULL );
445             vlc_object_release( p_sys->p_vout );
446             if (loc != (locale_t)0)
447             {
448                 uselocale (oldloc);
449                 freelocale (loc);
450             }
451             return NULL;
452         }
453         vlc_mutex_unlock( &p_sys->lock );
454
455         p_projectm->renderFrame();
456
457         /* */
458         mwait( i_deadline );
459
460         if( !vlc_gl_Lock(gl) )
461         {
462             vlc_gl_Swap( gl );
463             vlc_gl_Unlock( gl );
464         }
465     }
466     abort();
467
468 error:
469     p_sys->b_error = true;
470     vlc_sem_post( &p_sys->ready );
471     return NULL;
472 }
473