macOS vout: fix forgotten ';'
[vlc.git] / modules / visualization / vsxu.cpp
blob8be523e6faad114aef2291a0f696c61cfef55747
1 /*****************************************************************************
2 * vsxu.cpp: visualization module wrapper for Vovoid VSXu
3 *****************************************************************************
4 * Copyright © 2009-2012 the VideoLAN team, Vovoid Media Technologies
5 * $Id$
7 * Authors: Rémi Duraffort <ivoire@videolan.org>
8 * Laurent Aimar
9 * Jonatan "jaw" Wallmander
11 * Used the projectM implementation as reference for this file.
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.
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.
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 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
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>
39 // vsxu manager include
40 #include <vsx_manager.h>
41 #include <logo_intro.h>
43 // class to handle cyclic buffer
44 #include "cyclic_buffer.h"
46 /*****************************************************************************
47 * Module descriptor
48 *****************************************************************************/
49 static int Open ( vlc_object_t * );
50 static void Close ( vlc_object_t * );
52 #define WIDTH_TEXT N_("Video width")
53 #define WIDTH_LONGTEXT N_("The width of the video window, in pixels.")
55 #define HEIGHT_TEXT N_("Video height")
56 #define HEIGHT_LONGTEXT N_("The height of the video window, in pixels.")
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 ()
73 /*****************************************************************************
74 * Local prototypes
75 *****************************************************************************/
77 struct filter_sys_t
79 vlc_thread_t thread;
80 vlc_gl_t *gl;
82 vlc_mutex_t lock;
84 // mutex around the cyclic block
85 vlc_mutex_t cyclic_block_mutex;
87 // cyclic buffer to cache sound frames in
88 cyclic_block_queue* vsxu_cyclic_buffer;
90 int i_channels;
92 bool b_quit;
95 static block_t *DoWork( filter_t *, block_t * );
96 static void *Thread( void * );
98 /**
99 * Open the module
100 * @param p_this: the filter object
101 * @return VLC_SUCCESS or vlc error codes
103 static int Open( vlc_object_t * p_this )
105 filter_t *p_filter = (filter_t *)p_this;
106 filter_sys_t *p_sys;
108 p_sys = p_filter->p_sys = (filter_sys_t*)malloc( sizeof( *p_sys ) );
109 if( unlikely( !p_sys ) )
111 return VLC_ENOMEM;
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();
121 /* Create the openGL provider */
122 vout_window_cfg_t cfg;
124 memset( &cfg, 0, sizeof(cfg) );
125 cfg.width = var_InheritInteger( p_filter, "vsxu-width" );
126 cfg.height = var_InheritInteger( p_filter, "vsxu-height" );
128 p_sys->gl = vlc_gl_surface_Create( VLC_OBJECT(p_filter), &cfg, NULL );
129 if( p_sys->gl == NULL )
130 goto error;
132 /* Create the thread */
133 if( vlc_clone( &p_sys->thread, Thread, p_filter,
134 VLC_THREAD_PRIORITY_LOW ) )
136 vlc_gl_surface_Destroy( p_sys->gl );
137 goto error;
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;
144 return VLC_SUCCESS;
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;
154 * Close the module
155 * @param p_this: the filter object
157 static void Close( vlc_object_t *p_this )
159 filter_t *p_filter = (filter_t *)p_this;
160 filter_sys_t *p_sys = p_filter->p_sys;
162 vlc_mutex_lock( &p_sys->lock );
163 p_sys->b_quit = true;
164 vlc_mutex_unlock( &p_sys->lock );
166 vlc_join( p_sys->thread, NULL );
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 );
177 * Do the actual work with the new sample
178 * @param p_filter: filter object
179 * @param p_in_buf: input buffer
181 static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
183 filter_sys_t *p_sys = p_filter->p_sys;
185 vlc_mutex_lock( &p_sys->lock );
186 vlc_mutex_lock( &p_sys->cyclic_block_mutex );
188 unsigned i_nb_samples = __MIN( 1024,
189 p_in_buf->i_nb_samples );
191 const float *p_src = (float*)p_in_buf->p_buffer;
192 // iterate block holder
193 size_t i_bh_data_iter = 0;
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;
198 // muls are cheaper than divs
199 float f_onedivchannels = 1.0f / (float)p_sys->i_channels;
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++ )
205 float f_v = 0;
206 for( int j = 0; j < p_sys->i_channels; j++ )
208 f_v += p_src[p_sys->i_channels * i + j];
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)
216 p_block_holder = p_sys->vsxu_cyclic_buffer->get_insertion_object();
217 p_block_holder->pts = p_in_buf->i_pts + 11609;
219 i_bh_data_iter = 0;
223 vlc_mutex_unlock( &p_sys->cyclic_block_mutex );
224 vlc_mutex_unlock( &p_sys->lock );
225 return p_in_buf;
229 * VSXu update thread which do the rendering
230 * @param p_this: the p_thread object
232 static void *Thread( void *p_data )
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;
238 // our abstract manager holder
239 vsx_manager_abs* manager = 0;
241 // temp audio buffer for sending to vsxu through manager
242 float f_sample_buf[512];
244 // vsxu logo intro
245 vsx_logo_intro* intro = 0;
247 bool first = true;
248 bool run = true;
250 // tell main thread we are ready
251 if( vlc_gl_MakeCurrent( gl ) != VLC_SUCCESS )
253 msg_Err( p_filter, "Can't attach gl context" );
254 return NULL;
257 while ( run )
259 /* Manage the events */
260 unsigned width, height;
262 if( vlc_gl_surface_CheckSize( p_sys->gl, &width, &height ) )
264 /* ??? */
267 // look for control commands from outside the thread
268 vlc_mutex_lock( &p_sys->lock );
269 if( p_sys->b_quit )
271 run = false;
273 vlc_mutex_unlock( &p_sys->lock );
275 if (first)
277 // only run this once
278 first = false;
280 // create a new manager
281 manager = manager_factory();
283 // init manager with the shared path and sound input type.
284 manager->init( 0, "media_player" );
286 // only show logo once
287 // keep track of iterations
288 static int i_iterations = 0;
289 if ( i_iterations++ < 1 )
291 intro = new vsx_logo_intro();
292 intro->set_destroy_textures( false );
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 );
302 // send sound pointer to vsxu
303 manager->set_sound_wave( &f_sample_buf[0] );
305 // render vsxu engine
306 if (manager) manager->render();
308 // render intro
309 if (intro) intro->draw();
311 // swap buffers etc.
312 vlc_gl_Swap( gl );
315 // stop vsxu nicely (unloads textures and frees memory)
316 if (manager) manager->stop();
318 // call manager factory to destruct our manager object
319 if (manager) manager_destroy( manager );
321 // delete the intro (if ever allocated)
322 if (intro) delete intro;
324 vlc_gl_ReleaseCurrent( gl );
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 );
331 // die
332 return NULL;