]> git.sesse.net Git - vlc/blob - modules/visualization/vsxu.cpp
Upnp: remove trailing space
[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 #include <vlc_filter.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( "visualization", 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     vlc_gl_t *gl;
84
85     vlc_mutex_t lock;
86
87     // mutex around the cyclic block
88     vlc_mutex_t cyclic_block_mutex;
89
90     // cyclic buffer to cache sound frames in
91     cyclic_block_queue* vsxu_cyclic_buffer;
92
93     int i_channels;
94
95     bool b_quit;
96 };
97
98 static block_t *DoWork( filter_t *, block_t * );
99 static void *Thread( void * );
100
101 /**
102  * Open the module
103  * @param p_this: the filter object
104  * @return VLC_SUCCESS or vlc error codes
105  */
106 static int Open( vlc_object_t * p_this )
107 {
108     filter_t     *p_filter = (filter_t *)p_this;
109     filter_sys_t *p_sys;
110
111     p_sys = p_filter->p_sys = (filter_sys_t*)malloc( sizeof( *p_sys ) );
112     if( unlikely( !p_sys ) )
113     {
114         return VLC_ENOMEM;
115     }
116
117     /* Create the object for the thread */
118     p_sys->b_quit        = false;
119     p_sys->i_channels    = aout_FormatNbChannels( &p_filter->fmt_in.audio );
120     vlc_mutex_init( &p_sys->lock );
121     vlc_mutex_init( &p_sys->cyclic_block_mutex );
122     p_sys->vsxu_cyclic_buffer = new cyclic_block_queue();
123
124     /* Create the openGL provider */
125     vout_window_cfg_t cfg;
126
127     memset( &cfg, 0, sizeof(cfg) );
128     cfg.width = var_InheritInteger( p_filter, "vsxu-width" );
129     cfg.height = var_InheritInteger( p_filter, "vsxu-height" );
130
131     p_sys->gl = vlc_gl_surface_Create( VLC_OBJECT(p_filter), &cfg, NULL );
132     if( p_sys->gl == NULL )
133         goto error;
134
135     /* Create the thread */
136     if( vlc_clone( &p_sys->thread, Thread, p_filter,
137                    VLC_THREAD_PRIORITY_LOW ) )
138     {
139         vlc_gl_surface_Destroy( p_sys->gl );
140         goto error;
141     }
142
143     p_filter->fmt_in.audio.i_format = VLC_CODEC_FL32;
144     p_filter->fmt_out.audio = p_filter->fmt_in.audio;
145     p_filter->pf_audio_filter = DoWork;
146
147     return VLC_SUCCESS;
148
149 error:
150     vlc_mutex_destroy( &p_sys->cyclic_block_mutex );
151     vlc_mutex_destroy( &p_sys->lock );
152     free( p_sys );
153     return VLC_EGENERIC;
154 }
155
156 /**
157  * Close the module
158  * @param p_this: the filter object
159  */
160 static void Close( vlc_object_t *p_this )
161 {
162     filter_t  *p_filter = (filter_t *)p_this;
163     filter_sys_t *p_sys = p_filter->p_sys;
164
165     vlc_mutex_lock( &p_sys->lock );
166     p_sys->b_quit = true;
167     vlc_mutex_unlock( &p_sys->lock );
168
169     vlc_join( p_sys->thread, NULL );
170
171     /* Free the ressources */
172     vlc_gl_surface_Destroy( p_sys->gl );
173     vlc_mutex_destroy( &p_sys->cyclic_block_mutex );
174     vlc_mutex_destroy( &p_sys->lock );
175     delete p_sys->vsxu_cyclic_buffer;
176     free( p_sys );
177 }
178
179 /**
180  * Do the actual work with the new sample
181  * @param p_filter: filter object
182  * @param p_in_buf: input buffer
183  */
184 static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
185 {
186     filter_sys_t *p_sys = p_filter->p_sys;
187
188     vlc_mutex_lock( &p_sys->lock );
189     vlc_mutex_lock( &p_sys->cyclic_block_mutex );
190
191     unsigned i_nb_samples = __MIN( 1024,
192                                  p_in_buf->i_nb_samples );
193
194     const float *p_src = (float*)p_in_buf->p_buffer;
195     // iterate block holder
196     size_t i_bh_data_iter = 0;
197
198     // calc 512-byte-aligned sample count to grab, we don't need more
199     unsigned i_num_samples = i_nb_samples - i_nb_samples % 512;
200
201     // muls are cheaper than divs
202     float f_onedivchannels = 1.0f / (float)p_sys->i_channels;
203
204     block_holder* p_block_holder = p_sys->vsxu_cyclic_buffer->get_insertion_object();
205     p_block_holder->pts = p_in_buf->i_pts;
206     for( unsigned i = 0; i < i_num_samples; i++ )
207     {
208         float f_v = 0;
209         for( int j = 0; j < p_sys->i_channels; j++ )
210         {
211             f_v += p_src[p_sys->i_channels * i + j];
212         }
213
214         // insert into our little cyclic buffer
215         p_block_holder->data[i_bh_data_iter] = f_v * f_onedivchannels;
216         i_bh_data_iter++;
217         if (i_bh_data_iter == 512 && i < i_num_samples-256)
218         {
219             p_block_holder = p_sys->vsxu_cyclic_buffer->get_insertion_object();
220             p_block_holder->pts = p_in_buf->i_pts + 11609;
221
222             i_bh_data_iter = 0;
223         }
224     }
225
226     vlc_mutex_unlock( &p_sys->cyclic_block_mutex );
227     vlc_mutex_unlock( &p_sys->lock );
228     return p_in_buf;
229 }
230
231 /**
232  * VSXu update thread which do the rendering
233  * @param p_this: the p_thread object
234  */
235 static void *Thread( void *p_data )
236 {
237     filter_t  *p_filter = (filter_t*)p_data;
238     filter_sys_t *p_sys = p_filter->p_sys;
239     vlc_gl_t *gl = p_sys->gl;
240
241     // our abstract manager holder
242     vsx_manager_abs* manager = 0;
243
244     // temp audio buffer for sending to vsxu through manager
245     float f_sample_buf[512];
246
247     // vsxu logo intro
248     vsx_logo_intro* intro = 0;
249
250     bool first = true;
251     bool run = true;
252
253     // tell main thread we are ready
254     vlc_gl_MakeCurrent( gl );
255
256     while ( run )
257     {
258         /* Manage the events */
259         unsigned width, height;
260
261         if( vlc_gl_surface_CheckSize( p_sys->gl, &width, &height ) )
262         {
263             /* ??? */
264         }
265
266         // look for control commands from outside the thread
267         vlc_mutex_lock( &p_sys->lock );
268             if( p_sys->b_quit )
269             {
270                 run = false;
271             }
272         vlc_mutex_unlock( &p_sys->lock );
273
274         if (first)
275         {
276             // only run this once
277             first = false;
278
279             // create a new manager
280             manager = manager_factory();
281
282             // init manager with the shared path and sound input type.
283             manager->init( 0, "media_player" );
284
285             // only show logo once
286             // keep track of iterations
287             static int i_iterations = 0;
288             if ( i_iterations++ < 1 )
289             {
290                 intro = new vsx_logo_intro();
291                 intro->set_destroy_textures( false );
292             }
293         }
294
295         // lock cyclic buffer mutex and copy floats
296         vlc_mutex_lock( &p_sys->cyclic_block_mutex );
297             block_holder* bh = p_sys->vsxu_cyclic_buffer->consume();
298             memcpy( &f_sample_buf[0], (void*)(&bh->data[0]), sizeof(float) * 512 );
299         vlc_mutex_unlock( &p_sys->cyclic_block_mutex );
300
301         // send sound pointer to vsxu
302         manager->set_sound_wave( &f_sample_buf[0] );
303
304         // render vsxu engine
305         if (manager) manager->render();
306
307         // render intro
308         if (intro) intro->draw();
309
310         // swap buffers etc.
311         if( !vlc_gl_Lock(gl) )
312         {
313             vlc_gl_Swap( gl );
314             vlc_gl_Unlock( gl );
315         }
316     }
317
318     // stop vsxu nicely (unloads textures and frees memory)
319     if (manager) manager->stop();
320
321     // call manager factory to destruct our manager object
322     if (manager) manager_destroy( manager );
323
324     // delete the intro (if ever allocated)
325     if (intro) delete intro;
326
327     vlc_gl_ReleaseCurrent( gl );
328
329     // clean up the cyclic buffer
330     vlc_mutex_lock( &p_sys->cyclic_block_mutex );
331         p_sys->vsxu_cyclic_buffer->reset();
332     vlc_mutex_unlock( &p_sys->cyclic_block_mutex );
333
334     // die
335     return NULL;
336 }
337