]> git.sesse.net Git - vlc/blob - modules/visualization/projectm.cpp
Do not use vlc_cancel with vlc_cleanup* uses in projectM.
[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 #ifndef __STDC_CONSTANT_MACROS
28 # define __STDC_CONSTANT_MACROS
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_aout.h>
34 #include <vlc_vout.h>
35 #include <vlc_vout_wrapper.h>
36 #include <vlc_filter.h>
37
38 #include <libprojectM/projectM.hpp>
39
40
41 /*****************************************************************************
42  * Module descriptor
43  *****************************************************************************/
44 static int  Open         ( vlc_object_t * );
45 static void Close        ( vlc_object_t * );
46
47 #define CONFIG_TEXT N_("projectM configuration file")
48 #define CONFIG_LONGTEXT N_("File that will be used to configure the projectM " \
49                            "module.")
50
51 #define WIDTH_TEXT N_("Video width")
52 #define WIDTH_LONGTEXT N_("The width of the video window, in pixels.")
53
54 #define HEIGHT_TEXT N_("Video height")
55 #define HEIGHT_LONGTEXT N_("The height of the video window, in pixels.")
56
57 vlc_module_begin ()
58     set_shortname( N_("projectM"))
59     set_description( N_("libprojectM effect") )
60     set_capability( "visualization2", 0 )
61     set_category( CAT_AUDIO )
62     set_subcategory( SUBCAT_AUDIO_VISUAL )
63     add_file( "projectm-config", "/usr/share/projectM/config.inp", NULL,
64                 CONFIG_TEXT, CONFIG_LONGTEXT, true )
65     add_integer( "projectm-width", 800, NULL, WIDTH_TEXT, WIDTH_LONGTEXT,
66                  false )
67     add_integer( "projectm-height", 640, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT,
68                  false )
69     add_shortcut( "projectm" )
70     set_callbacks( Open, Close )
71 vlc_module_end ()
72
73
74 /*****************************************************************************
75  * Local prototypes
76  *****************************************************************************/
77 struct filter_sys_t
78 {
79     /* */
80     vlc_thread_t thread;
81     vlc_sem_t    ready;
82     bool         b_error;
83
84     /* Opengl */
85     vout_thread_t  *p_vout;
86     vout_display_t *p_vd;
87
88     /* libprojectM objects */
89     projectM      *p_projectm;
90     char          *psz_config;
91
92     /* Window size */
93     int i_width;
94     int i_height;
95
96     /* audio info */
97     int i_channels;
98
99     /* */
100     vlc_mutex_t lock;
101     bool  b_quit;
102     float *p_buffer;
103     int   i_buffer_size;
104     int   i_nb_samples;
105 };
106
107
108 static block_t *DoWork( filter_t *, block_t * );
109 static void *Thread( void * );
110
111 /**
112  * Open the module
113  * @param p_this: the filter object
114  * @return VLC_SUCCESS or vlc error codes
115  */
116 static int Open( vlc_object_t * p_this )
117 {
118     filter_t     *p_filter = (filter_t *)p_this;
119     filter_sys_t *p_sys;
120
121     /* Test the audio format */
122     if( p_filter->fmt_in.audio.i_format != VLC_CODEC_FL32 ||
123         p_filter->fmt_out.audio.i_format != VLC_CODEC_FL32 )
124     {
125         msg_Warn( p_filter, "bad input or output format" );
126         return VLC_EGENERIC;
127     }
128     if( !AOUT_FMTS_SIMILAR( &p_filter->fmt_in.audio, &p_filter->fmt_out.audio ) )
129     {
130         msg_Warn( p_filter, "input and outut are not similar" );
131         return VLC_EGENERIC;
132     }
133
134     p_filter->pf_audio_filter = DoWork;
135
136     p_sys = p_filter->p_sys = (filter_sys_t*)malloc( sizeof( *p_sys ) );
137     if( !p_sys )
138         return VLC_ENOMEM;
139
140     /* Create the object for the thread */
141     vlc_sem_init( &p_sys->ready, 0 );
142     p_sys->b_error  = false;
143     p_sys->b_quit   = false;
144     p_sys->i_width  = var_CreateGetInteger( p_filter, "projectm-width" );
145     p_sys->i_height = var_CreateGetInteger( p_filter, "projectm-height" );
146     p_sys->i_channels = aout_FormatNbChannels( &p_filter->fmt_in.audio );
147     p_sys->psz_config = var_CreateGetString( p_filter, "projectm-config" );
148     vlc_mutex_init( &p_sys->lock );
149     p_sys->p_buffer = NULL;
150     p_sys->i_buffer_size = 0;
151     p_sys->i_nb_samples = 0;
152
153     /* Create the thread */
154     if( vlc_clone( &p_sys->thread, Thread, p_filter, VLC_THREAD_PRIORITY_LOW ) )
155         goto error;
156
157     vlc_sem_wait( &p_sys->ready );
158     if( p_sys->b_error )
159     {
160         vlc_join( p_sys->thread, NULL );
161         goto error;
162     }
163
164     return VLC_SUCCESS;
165
166 error:
167     vlc_sem_destroy( &p_sys->ready );
168     free (p_sys );
169     return VLC_EGENERIC;
170 }
171
172
173 /**
174  * Close the module
175  * @param p_this: the filter object
176  */
177 static void Close( vlc_object_t *p_this )
178 {
179     filter_t     *p_filter = (filter_t *)p_this;
180     filter_sys_t *p_sys = p_filter->p_sys;
181
182     /* Stop the thread
183      * XXX vlc_cleanup_push does not seems to work with C++ so no
184      * vlc_cancel()... */
185     vlc_mutex_lock( &p_sys->lock );
186     p_sys->b_quit = true;
187     vlc_mutex_unlock( &p_sys->lock );
188
189     vlc_join( p_sys->thread, NULL );
190
191     /* Free the ressources */
192     vlc_sem_destroy( &p_sys->ready );
193     vlc_mutex_destroy( &p_sys->lock );
194     free( p_sys->p_buffer );
195     free( p_sys->psz_config );
196     free( p_sys );
197 }
198
199
200 /**
201  * Do the actual work with the new sample
202  * @param p_aout: audio output object
203  * @param p_filter: filter object
204  * @param p_in_buf: input buffer
205  * @param p_out_buf: output buffer
206  */
207 static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
208 {
209     filter_sys_t *p_sys = p_filter->p_sys;
210
211     vlc_mutex_lock( &p_sys->lock );
212     if( p_sys->i_buffer_size > 0 )
213     {
214         p_sys->i_nb_samples = __MIN( p_sys->i_buffer_size,
215                                      p_in_buf->i_nb_samples );
216
217         const float *p_src = (float*)p_in_buf->p_buffer;
218         for( int i = 0; i < p_sys->i_nb_samples; i++ )
219         {
220             float v = 0;
221             for( int j = 0; j < p_sys->i_channels; j++ )
222                 v += p_src[p_sys->i_channels * i + j];
223             p_sys->p_buffer[i] = v / p_sys->i_channels;
224         }
225     }
226     vlc_mutex_unlock( &p_sys->lock );
227
228     return p_in_buf;
229 }
230
231 /**
232  * Variable callback for the dummy vout
233  */
234 static int VoutCallback( vlc_object_t *p_vout, char const *psz_name,
235                          vlc_value_t oldv, vlc_value_t newv, void *p_data )
236 {
237     vout_display_t *p_vd = (vout_display_t*)p_data;
238
239     if( !strcmp(psz_name, "fullscreen") )
240     {
241         vout_SetDisplayFullscreen( p_vd, newv.b_bool );
242     }
243 }
244
245 /**
246  * ProjectM update thread which do the rendering
247  * @param p_this: the p_thread object
248  */
249 static void *Thread( void *p_data )
250 {
251     filter_t     *p_filter = (filter_t*)p_data;
252     filter_sys_t *p_sys = p_filter->p_sys;
253     int cancel = vlc_savecancel();
254     video_format_t fmt;
255     vout_opengl_t *gl;
256     int i_last_width  = 0;
257     int i_last_height = 0;
258
259     /* Create the openGL provider */
260     p_sys->p_vout =
261         (vout_thread_t *)vlc_object_create( p_filter, sizeof(vout_thread_t) );
262     if( !p_sys->p_vout )
263         goto error;
264
265     /* */
266     video_format_Init( &fmt, 0 );
267     video_format_Setup( &fmt, VLC_CODEC_RGB32,
268                         p_sys->i_width, p_sys->i_height, 0, 1 );
269     fmt.i_sar_num = 1;
270     fmt.i_sar_den = 1;
271
272     vout_display_state_t state;
273     memset( &state, 0, sizeof(state) );
274     state.cfg.display.sar.num = 1;
275     state.cfg.display.sar.den = 1;
276     state.cfg.is_display_filled = true;
277     state.cfg.zoom.num = 1;
278     state.cfg.zoom.den = 1;
279     state.sar.num = 1;
280     state.sar.den = 1;
281
282     p_sys->p_vd = vout_NewDisplay( p_sys->p_vout, &fmt, &state, "opengl",
283                                    300000, 1000000 );
284     if( !p_sys->p_vd )
285     {
286         vlc_object_release( p_sys->p_vout );
287         goto error;
288     }
289     var_Create( p_sys->p_vout, "fullscreen", VLC_VAR_BOOL );
290     var_AddCallback( p_sys->p_vout, "fullscreen", VoutCallback, p_sys->p_vd );
291
292     gl = vout_GetDisplayOpengl( p_sys->p_vd );
293     if( !gl )
294     {
295         vout_DeleteDisplay( p_sys->p_vd, NULL );
296         vlc_object_release( p_sys->p_vout );
297         goto error;
298     }
299
300     /* Create the projectM object */
301     p_sys->p_projectm = new projectM( p_sys->psz_config );
302     p_sys->i_buffer_size = p_sys->p_projectm->pcm()->maxsamples;
303     p_sys->p_buffer = (float*)calloc( p_sys->i_buffer_size,
304                                       sizeof( float ) );
305
306     vlc_sem_post( &p_sys->ready );
307
308     /* TODO: Give to projectm the name of the input
309     p_sys->p_projectm->projectM_setTitle( "" ); */
310
311     /* */
312     for( ;; )
313     {
314         const mtime_t i_deadline = mdate() + CLOCK_FREQ / 50; /* 50 fps max */
315         /* Manage the events */
316         vout_ManageDisplay( p_sys->p_vd, true );
317         if( p_sys->p_vd->cfg->display.width  != i_last_width ||
318             p_sys->p_vd->cfg->display.height != i_last_height )
319         {
320             /* FIXME it is not perfect as we will have black bands */
321             vout_display_place_t place;
322             vout_display_PlacePicture( &place, &p_sys->p_vd->source, p_sys->p_vd->cfg, false );
323             p_sys->p_projectm->projectM_resetGL( place.width, place.height );
324
325             i_last_width  = p_sys->p_vd->cfg->display.width;
326             i_last_height = p_sys->p_vd->cfg->display.height;
327         }
328
329         /* Render the image and swap the buffers */
330         vlc_mutex_lock( &p_sys->lock );
331         if( p_sys->i_nb_samples > 0 )
332         {
333             p_sys->p_projectm->pcm()->addPCMfloat( p_sys->p_buffer,
334                                                    p_sys->i_nb_samples );
335             p_sys->i_nb_samples = 0;
336         }
337         if( p_sys->b_quit )
338         {
339             vlc_mutex_unlock( &p_sys->lock );
340
341             delete p_sys->p_projectm;
342             vout_DeleteDisplay( p_sys->p_vd, NULL );
343             vlc_object_release( p_sys->p_vout );
344             return NULL;
345         }
346         vlc_mutex_unlock( &p_sys->lock );
347
348         p_sys->p_projectm->renderFrame();
349
350         /* */
351         mwait( i_deadline );
352
353         if( !vout_opengl_Lock(gl) )
354         {
355             vout_opengl_Swap( gl );
356             vout_opengl_Unlock( gl );
357         }
358     }
359     abort();
360
361 error:
362     p_sys->b_error = true;
363     vlc_sem_post( &p_sys->ready );
364     return NULL;
365 }
366