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