]> git.sesse.net Git - vlc/blob - modules/visualization/vsxu.cpp
vsxu: use vlc_gl_surface_* helpers
[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 Lesser General Public License as published by
15  * the Free Software Foundation; either version 2.1 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 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_window.h>
39 #include <vlc_opengl.h>
40
41 // vsxu manager include
42 #include <vsx_manager.h>
43 #include <logo_intro.h>
44
45 // class to handle cyclic buffer
46 #include "cyclic_buffer.h"
47
48 /*****************************************************************************
49  * Module descriptor
50  *****************************************************************************/
51 static int  Open         ( vlc_object_t * );
52 static void Close        ( vlc_object_t * );
53
54 #define WIDTH_TEXT N_("Video width")
55 #define WIDTH_LONGTEXT N_("The width of the video window, in pixels.")
56
57 #define HEIGHT_TEXT N_("Video height")
58 #define HEIGHT_LONGTEXT N_("The height of the video window, in pixels.")
59
60 vlc_module_begin ()
61     set_shortname( N_("vsxu"))
62     set_description( N_("vsxu") )
63     set_capability( "visualization", 0 )
64     set_category( CAT_AUDIO )
65     set_subcategory( SUBCAT_AUDIO_VISUAL )
66     add_integer( "vsxu-width", 1280, WIDTH_TEXT, WIDTH_LONGTEXT,
67                  false )
68     add_integer( "vsxu-height", 800, HEIGHT_TEXT, HEIGHT_LONGTEXT,
69                  false )
70     add_shortcut( "vsxu" )
71     set_callbacks( Open, Close )
72 vlc_module_end ()
73
74
75 /*****************************************************************************
76  * Local prototypes
77  *****************************************************************************/
78
79 struct filter_sys_t
80 {
81     vlc_thread_t thread;
82     vlc_gl_t *gl;
83
84     vlc_mutex_t lock;
85
86     // mutex around the cyclic block
87     vlc_mutex_t cyclic_block_mutex;
88
89     // cyclic buffer to cache sound frames in
90     cyclic_block_queue* vsxu_cyclic_buffer;
91
92     int i_channels;
93
94     bool b_quit;
95 };
96
97 static block_t *DoWork( filter_t *, block_t * );
98 static void *Thread( void * );
99
100 /**
101  * Open the module
102  * @param p_this: the filter object
103  * @return VLC_SUCCESS or vlc error codes
104  */
105 static int Open( vlc_object_t * p_this )
106 {
107     filter_t     *p_filter = (filter_t *)p_this;
108     filter_sys_t *p_sys;
109
110     p_sys = p_filter->p_sys = (filter_sys_t*)malloc( sizeof( *p_sys ) );
111     if( unlikely( !p_sys ) )
112     {
113         return VLC_ENOMEM;
114     }
115
116     /* Create the object for the thread */
117     p_sys->b_quit        = false;
118     p_sys->i_channels    = aout_FormatNbChannels( &p_filter->fmt_in.audio );
119     vlc_mutex_init( &p_sys->lock );
120     vlc_mutex_init( &p_sys->cyclic_block_mutex );
121     p_sys->vsxu_cyclic_buffer = new cyclic_block_queue();
122
123     /* Create the openGL provider */
124     vout_window_cfg_t cfg;
125
126     memset( &cfg, 0, sizeof(cfg) );
127     cfg.width = var_InheritInteger( p_filter, "vsxu-width" );
128     cfg.height = var_InheritInteger( p_filter, "vsxu-height" );
129
130     p_sys->gl = vlc_gl_surface_Create( VLC_OBJECT(p_filter), &cfg, NULL );
131     if( p_sys->gl == NULL )
132         goto error;
133
134     /* Create the thread */
135     if( vlc_clone( &p_sys->thread, Thread, p_filter,
136                    VLC_THREAD_PRIORITY_LOW ) )
137     {
138         vlc_gl_surface_Destroy( p_sys->gl );
139         goto error;
140     }
141
142     p_filter->fmt_in.audio.i_format = VLC_CODEC_FL32;
143     p_filter->fmt_out.audio = p_filter->fmt_in.audio;
144     p_filter->pf_audio_filter = DoWork;
145
146     return VLC_SUCCESS;
147
148 error:
149     vlc_mutex_destroy( &p_sys->cyclic_block_mutex );
150     vlc_mutex_destroy( &p_sys->lock );
151     free( p_sys );
152     return VLC_EGENERIC;
153 }
154
155 /**
156  * Close the module
157  * @param p_this: the filter object
158  */
159 static void Close( vlc_object_t *p_this )
160 {
161     filter_t  *p_filter = (filter_t *)p_this;
162     filter_sys_t *p_sys = p_filter->p_sys;
163
164     vlc_mutex_lock( &p_sys->lock );
165     p_sys->b_quit = true;
166     vlc_mutex_unlock( &p_sys->lock );
167
168     vlc_join( p_sys->thread, NULL );
169
170     /* Free the ressources */
171     vlc_gl_surface_Destroy( p_sys->gl );
172     vlc_mutex_destroy( &p_sys->cyclic_block_mutex );
173     vlc_mutex_destroy( &p_sys->lock );
174     delete p_sys->vsxu_cyclic_buffer;
175     free( p_sys );
176 }
177
178 /**
179  * Do the actual work with the new sample
180  * @param p_filter: filter object
181  * @param p_in_buf: input buffer
182  */
183 static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
184 {
185     filter_sys_t *p_sys = p_filter->p_sys;
186
187     vlc_mutex_lock( &p_sys->lock );
188     vlc_mutex_lock( &p_sys->cyclic_block_mutex );
189
190     unsigned i_nb_samples = __MIN( 1024,
191                                  p_in_buf->i_nb_samples );
192
193     const float *p_src = (float*)p_in_buf->p_buffer;
194     // iterate block holder
195     size_t i_bh_data_iter = 0;
196
197     // calc 512-byte-aligned sample count to grab, we don't need more
198     unsigned i_num_samples = i_nb_samples - i_nb_samples % 512;
199
200     // muls are cheaper than divs
201     float f_onedivchannels = 1.0f / (float)p_sys->i_channels;
202
203     block_holder* p_block_holder = p_sys->vsxu_cyclic_buffer->get_insertion_object();
204     p_block_holder->pts = p_in_buf->i_pts;
205     for( unsigned i = 0; i < i_num_samples; i++ )
206     {
207         float f_v = 0;
208         for( int j = 0; j < p_sys->i_channels; j++ )
209         {
210             f_v += p_src[p_sys->i_channels * i + j];
211         }
212
213         // insert into our little cyclic buffer
214         p_block_holder->data[i_bh_data_iter] = f_v * f_onedivchannels;
215         i_bh_data_iter++;
216         if (i_bh_data_iter == 512 && i < i_num_samples-256)
217         {
218             p_block_holder = p_sys->vsxu_cyclic_buffer->get_insertion_object();
219             p_block_holder->pts = p_in_buf->i_pts + 11609;
220
221             i_bh_data_iter = 0;
222         }
223     }
224
225     vlc_mutex_unlock( &p_sys->cyclic_block_mutex );
226     vlc_mutex_unlock( &p_sys->lock );
227     return p_in_buf;
228 }
229
230 /**
231  * VSXu update thread which do the rendering
232  * @param p_this: the p_thread object
233  */
234 static void *Thread( void *p_data )
235 {
236     filter_t  *p_filter = (filter_t*)p_data;
237     filter_sys_t *p_sys = p_filter->p_sys;
238     vlc_gl_t *gl = p_sys->gl;
239
240     // our abstract manager holder
241     vsx_manager_abs* manager = 0;
242
243     // temp audio buffer for sending to vsxu through manager
244     float f_sample_buf[512];
245
246     // vsxu logo intro
247     vsx_logo_intro* intro = 0;
248
249     bool first = true;
250     bool run = true;
251
252     // tell main thread we are ready
253     vlc_gl_MakeCurrent( gl );
254
255     while ( run )
256     {
257         /* Manage the events */
258         unsigned width, height;
259
260         if( vlc_gl_surface_CheckSize( p_sys->gl, &width, &height ) )
261         {
262             /* ??? */
263         }
264
265         // look for control commands from outside the thread
266         vlc_mutex_lock( &p_sys->lock );
267             if( p_sys->b_quit )
268             {
269                 run = false;
270             }
271         vlc_mutex_unlock( &p_sys->lock );
272
273         if (first)
274         {
275             // only run this once
276             first = false;
277
278             // create a new manager
279             manager = manager_factory();
280
281             // init manager with the shared path and sound input type.
282             manager->init( 0, "media_player" );
283
284             // only show logo once
285             // keep track of iterations
286             static int i_iterations = 0;
287             if ( i_iterations++ < 1 )
288             {
289                 intro = new vsx_logo_intro();
290                 intro->set_destroy_textures( false );
291             }
292         }
293
294         // lock cyclic buffer mutex and copy floats
295         vlc_mutex_lock( &p_sys->cyclic_block_mutex );
296             block_holder* bh = p_sys->vsxu_cyclic_buffer->consume();
297             memcpy( &f_sample_buf[0], (void*)(&bh->data[0]), sizeof(float) * 512 );
298         vlc_mutex_unlock( &p_sys->cyclic_block_mutex );
299
300         // send sound pointer to vsxu
301         manager->set_sound_wave( &f_sample_buf[0] );
302
303         // render vsxu engine
304         if (manager) manager->render();
305
306         // render intro
307         if (intro) intro->draw();
308
309         // swap buffers etc.
310         if( !vlc_gl_Lock(gl) )
311         {
312             vlc_gl_Swap( gl );
313             vlc_gl_Unlock( gl );
314         }
315     }
316
317     // stop vsxu nicely (unloads textures and frees memory)
318     if (manager) manager->stop();
319
320     // call manager factory to destruct our manager object
321     if (manager) manager_destroy( manager );
322
323     // delete the intro (if ever allocated)
324     if (intro) delete intro;
325
326     vlc_gl_ReleaseCurrent( gl );
327
328     // clean up the cyclic buffer
329     vlc_mutex_lock( &p_sys->cyclic_block_mutex );
330         p_sys->vsxu_cyclic_buffer->reset();
331     vlc_mutex_unlock( &p_sys->cyclic_block_mutex );
332
333     // die
334     return NULL;
335 }
336