]> git.sesse.net Git - vlc/blob - modules/visualization/vsxu.cpp
skins2: remove useless event
[vlc] / modules / visualization / vsxu.cpp
1 /*****************************************************************************
2  * vsxu.cpp: visualization module wrapper for Vovoid VSXu
3  *****************************************************************************
4  * Copyright © 2009-2012 the VideoLAN team, Vovoid Media Technologies
5  * $Id$
6  *
7  * Authors: Rémi Duraffort <ivoire@videolan.org>
8  *          Laurent Aimar
9  *          Jonatan "jaw" Wallmander
10  *
11  * Used the projectM implementation as reference for this file.
12  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License as published by the Free
15  * Software Foundation; either version 2 of the License, or (at your option)
16  * any later version.
17  *
18  * This program is distributed in the hope that it will be useful, but WITHOUT
19  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
21  * more details.
22  *
23  * You should have received a copy of the GNU General Public License along with
24  * this program; if not, write to the Free Software Foundation, Inc., 51
25  * Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31 #ifndef __STDC_CONSTANT_MACROS
32 # define __STDC_CONSTANT_MACROS
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_aout.h>
38 #include <vlc_vout.h>
39 #include <vlc_vout_wrapper.h>
40 #include <vlc_opengl.h>
41
42 // vsxu manager include
43 #include <vsx_manager.h>
44 #include <logo_intro.h>
45
46 // class to handle cyclic buffer
47 #include "cyclic_buffer.h"
48
49 /*****************************************************************************
50  * Module descriptor
51  *****************************************************************************/
52 static int  Open         ( vlc_object_t * );
53 static void Close        ( vlc_object_t * );
54
55 #define WIDTH_TEXT N_("Video width")
56 #define WIDTH_LONGTEXT N_("The width of the video window, in pixels.")
57
58 #define HEIGHT_TEXT N_("Video height")
59 #define HEIGHT_LONGTEXT N_("The height of the video window, in pixels.")
60
61 vlc_module_begin ()
62     set_shortname( N_("vsxu"))
63     set_description( N_("vsxu") )
64     set_capability( "visualization2", 0 )
65     set_category( CAT_AUDIO )
66     set_subcategory( SUBCAT_AUDIO_VISUAL )
67     add_integer( "vsxu-width", 1280, WIDTH_TEXT, WIDTH_LONGTEXT,
68                  false )
69     add_integer( "vsxu-height", 800, HEIGHT_TEXT, HEIGHT_LONGTEXT,
70                  false )
71     add_shortcut( "vsxu" )
72     set_callbacks( Open, Close )
73 vlc_module_end ()
74
75
76 /*****************************************************************************
77  * Local prototypes
78  *****************************************************************************/
79
80 struct filter_sys_t
81 {
82     vlc_thread_t thread;
83
84     vlc_sem_t    ready;
85
86     vlc_mutex_t lock;
87
88     // mutex around the cyclic block
89     vlc_mutex_t cyclic_block_mutex;
90
91     // cyclic buffer to cache sound frames in
92     cyclic_block_queue* vsxu_cyclic_buffer;
93
94     int i_width;
95     int i_height;
96     int i_channels;
97
98     bool b_error;
99     bool b_quit;
100 };
101
102 static block_t *DoWork( filter_t *, block_t * );
103 static void *Thread( void * );
104
105 /**
106  * Open the module
107  * @param p_this: the filter object
108  * @return VLC_SUCCESS or vlc error codes
109  */
110 static int Open( vlc_object_t * p_this )
111 {
112     filter_t     *p_filter = (filter_t *)p_this;
113     filter_sys_t *p_sys;
114
115     /* Test the audio format */
116     if( p_filter->fmt_in.audio.i_format != VLC_CODEC_FL32 )
117     {
118         msg_Warn( p_filter, "bad input format" );
119         return VLC_EGENERIC;
120     }
121
122     p_sys = p_filter->p_sys = (filter_sys_t*)malloc( sizeof( *p_sys ) );
123     if( unlikely( !p_sys ) )
124     {
125         return VLC_ENOMEM;
126     }
127
128     /* Create the object for the thread */
129     vlc_sem_init( &p_sys->ready, 0 );
130     p_sys->b_error       = false;
131     p_sys->b_quit        = false;
132     p_sys->i_width       = var_InheritInteger( p_filter, "vsxu-width" );
133     p_sys->i_height      = var_InheritInteger( p_filter, "vsxu-height" );
134     p_sys->i_channels    = aout_FormatNbChannels( &p_filter->fmt_in.audio );
135     vlc_mutex_init( &p_sys->lock );
136     vlc_mutex_init( &p_sys->cyclic_block_mutex );
137     p_sys->vsxu_cyclic_buffer = new cyclic_block_queue();
138
139
140     /* Create the thread */
141     if( vlc_clone( &p_sys->thread, Thread, p_filter, VLC_THREAD_PRIORITY_LOW ) )
142         goto error;
143
144
145     vlc_sem_wait( &p_sys->ready );
146     if( p_sys->b_error )
147     {
148         vlc_join( p_sys->thread, NULL );
149         goto error;
150     }
151
152     p_filter->fmt_out.audio = p_filter->fmt_in.audio;
153     p_filter->pf_audio_filter = DoWork;
154
155     return VLC_SUCCESS;
156
157 error:
158     vlc_sem_destroy( &p_sys->ready );
159     free (p_sys );
160     return VLC_EGENERIC;
161 }
162
163 /**
164  * Close the module
165  * @param p_this: the filter object
166  */
167 static void Close( vlc_object_t *p_this )
168 {
169     filter_t  *p_filter = (filter_t *)p_this;
170     filter_sys_t *p_sys = p_filter->p_sys;
171
172     vlc_mutex_lock( &p_sys->lock );
173     p_sys->b_quit = true;
174     vlc_mutex_unlock( &p_sys->lock );
175
176     vlc_join( p_sys->thread, NULL );
177
178     /* Free the ressources */
179     vlc_sem_destroy( &p_sys->ready );
180     vlc_mutex_destroy( &p_sys->lock );
181     delete p_sys->vsxu_cyclic_buffer;
182     free( p_sys );
183 }
184
185 /**
186  * Do the actual work with the new sample
187  * @param p_filter: filter object
188  * @param p_in_buf: input buffer
189  */
190 static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
191 {
192     filter_sys_t *p_sys = p_filter->p_sys;
193
194     vlc_mutex_lock( &p_sys->lock );
195     vlc_mutex_lock( &p_sys->cyclic_block_mutex );
196
197     unsigned i_nb_samples = __MIN( 1024,
198                                  p_in_buf->i_nb_samples );
199
200     const float *p_src = (float*)p_in_buf->p_buffer;
201     // iterate block holder
202     size_t i_bh_data_iter = 0;
203
204     // calc 512-byte-aligned sample count to grab, we don't need more
205     unsigned i_num_samples = i_nb_samples - i_nb_samples % 512;
206
207     // muls are cheaper than divs
208     float f_onedivchannels = 1.0f / (float)p_sys->i_channels;
209
210     block_holder* p_block_holder = p_sys->vsxu_cyclic_buffer->get_insertion_object();
211     p_block_holder->pts = p_in_buf->i_pts;
212     for( unsigned i = 0; i < i_num_samples; i++ )
213     {
214         float f_v = 0;
215         for( int j = 0; j < p_sys->i_channels; j++ )
216         {
217             f_v += p_src[p_sys->i_channels * i + j];
218         }
219
220         // insert into our little cyclic buffer
221         p_block_holder->data[i_bh_data_iter] = f_v * f_onedivchannels;
222         i_bh_data_iter++;
223         if (i_bh_data_iter == 512 && i < i_num_samples-256)
224         {
225             p_block_holder = p_sys->vsxu_cyclic_buffer->get_insertion_object();
226             p_block_holder->pts = p_in_buf->i_pts + 11609;
227
228             i_bh_data_iter = 0;
229         }
230     }
231
232     vlc_mutex_unlock( &p_sys->cyclic_block_mutex );
233     vlc_mutex_unlock( &p_sys->lock );
234     return p_in_buf;
235 }
236
237 /**
238  * Variable callback for the dummy vout
239  */
240 static int VoutCallback( vlc_object_t *p_vout, char const *psz_name,
241                          vlc_value_t oldv, vlc_value_t newv, void *p_data )
242 {
243     VLC_UNUSED( p_vout ); VLC_UNUSED( oldv );
244     vout_display_t *p_vd = (vout_display_t*)p_data;
245
246     if( !strcmp(psz_name, "fullscreen") )
247     {
248         vout_SetDisplayFullscreen( p_vd, newv.b_bool );
249     }
250     return VLC_SUCCESS;
251 }
252
253 /**
254  * VSXu update thread which do the rendering
255  * @param p_this: the p_thread object
256  */
257 static void *Thread( void *p_data )
258 {
259     filter_t  *p_filter = (filter_t*)p_data;
260     filter_sys_t *p_sys = p_filter->p_sys;
261
262     // our abstract manager holder
263     vsx_manager_abs* manager = 0;
264
265     // temp audio buffer for sending to vsxu through manager
266     float f_sample_buf[512];
267
268     // vsxu logo intro
269     vsx_logo_intro* intro = 0;
270
271     vout_display_t *p_vd;
272
273     video_format_t fmt;
274     vlc_gl_t *gl;
275
276     unsigned int i_last_width  = 0;
277     unsigned int i_last_height = 0;
278     bool first = true;
279     bool run = true;
280
281     /* Create the openGL provider */
282     vout_thread_t  *p_vout;
283
284     p_vout = (vout_thread_t *)vlc_object_create( p_filter, sizeof(vout_thread_t) );
285     if( !p_vout )
286         goto error;
287
288     video_format_Init( &fmt, 0 );
289     video_format_Setup( &fmt, VLC_CODEC_RGB32,
290                         p_sys->i_width, p_sys->i_height, 0, 1 );
291     fmt.i_sar_num = 1;
292     fmt.i_sar_den = 1;
293
294     vout_display_state_t state;
295     memset( &state, 0, sizeof(state) );
296     state.cfg.display.sar.num = 1;
297     state.cfg.display.sar.den = 1;
298     state.cfg.is_display_filled = true;
299     state.cfg.zoom.num = 1;
300     state.cfg.zoom.den = 1;
301     state.sar.num = 1;
302     state.sar.den = 1;
303
304     p_vd = vout_NewDisplay( p_vout, &fmt, &state, "opengl", 300000, 1000000 );
305     if( !p_vd )
306     {
307         vlc_object_release( p_vout );
308         goto error;
309     }
310     var_Create( p_vout, "fullscreen", VLC_VAR_BOOL );
311     var_AddCallback( p_vout, "fullscreen", VoutCallback, p_vd );
312
313     gl = vout_GetDisplayOpengl( p_vd );
314     if( !gl )
315     {
316         var_DelCallback( p_vout, "fullscreen", VoutCallback, p_vd );
317         vout_DeleteDisplay( p_vd, NULL );
318         vlc_object_release( p_vout );
319         goto error;
320     }
321
322     // tell main thread we are ready
323     vlc_sem_post( &p_sys->ready );
324
325     while ( run )
326     {
327         /* Manage the events */
328         vout_ManageDisplay( p_vd, true );
329         if( p_vd->cfg->display.width  != i_last_width ||
330             p_vd->cfg->display.height != i_last_height )
331         {
332             /* FIXME it is not perfect as we will have black bands */
333             vout_display_place_t place;
334             vout_display_PlacePicture( &place, &p_vd->source, p_vd->cfg, false );
335
336             i_last_width  = p_vd->cfg->display.width;
337             i_last_height = p_vd->cfg->display.height;
338         }
339
340         // look for control commands from outside the thread
341         vlc_mutex_lock( &p_sys->lock );
342             if( p_sys->b_quit )
343             {
344                 run = false;
345             }
346         vlc_mutex_unlock( &p_sys->lock );
347
348         if (first)
349         {
350             // only run this once
351             first = false;
352
353             // create a new manager
354             manager = manager_factory();
355
356             // init manager with the shared path and sound input type.
357             manager->init( 0, "media_player" );
358
359             // only show logo once
360             // keep track of iterations
361             static int i_iterations = 0;
362             if ( i_iterations++ < 1 )
363             {
364                 intro = new vsx_logo_intro();
365                 intro->set_destroy_textures( false );
366             }
367         }
368
369         // lock cyclic buffer mutex and copy floats
370         vlc_mutex_lock( &p_sys->cyclic_block_mutex );
371             block_holder* bh = p_sys->vsxu_cyclic_buffer->consume();
372             memcpy( &f_sample_buf[0], (void*)(&bh->data[0]), sizeof(float) * 512 );
373         vlc_mutex_unlock( &p_sys->cyclic_block_mutex );
374
375         // send sound pointer to vsxu
376         manager->set_sound_wave( &f_sample_buf[0] );
377
378         // render vsxu engine
379         if (manager) manager->render();
380
381         // render intro
382         if (intro) intro->draw();
383
384         // swap buffers etc.
385         if( !vlc_gl_Lock(gl) )
386         {
387             vlc_gl_Swap( gl );
388             vlc_gl_Unlock( gl );
389         }
390     }
391
392     // stop vsxu nicely (unloads textures and frees memory)
393     if (manager) manager->stop();
394
395     // call manager factory to destruct our manager object
396     if (manager) manager_destroy( manager );
397
398     // delete the intro (if ever allocated)
399     if (intro) delete intro;
400
401     var_DelCallback( p_vout, "fullscreen", VoutCallback, p_vd );
402
403     // clean out vlc opengl stuff
404     vout_DeleteDisplay( p_vd, NULL );
405     vlc_object_release( p_vout );
406
407     // clean up the cyclic buffer
408     vlc_mutex_lock( &p_sys->cyclic_block_mutex );
409         p_sys->vsxu_cyclic_buffer->reset();
410     vlc_mutex_unlock( &p_sys->cyclic_block_mutex );
411
412     // die
413     return NULL;
414
415 error:
416     p_sys->b_error = true;
417     vlc_sem_post( &p_sys->ready );
418     return NULL;
419 }
420