1 /*****************************************************************************
2 * vsxu.cpp: visualization module wrapper for Vovoid VSXu
3 *****************************************************************************
4 * Copyright © 2009-2012 the VideoLAN team, Vovoid Media Technologies
7 * Authors: Rémi Duraffort <ivoire@videolan.org>
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 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.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 /*****************************************************************************
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.")
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
,
66 add_integer( "vsxu-height", 800, HEIGHT_TEXT
, HEIGHT_LONGTEXT
,
68 add_shortcut( "vsxu" )
69 set_callbacks( Open
, Close
)
73 /*****************************************************************************
75 *****************************************************************************/
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
;
95 static block_t
*DoWork( filter_t
*, block_t
* );
96 static void *Thread( void * );
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
;
108 p_sys
= p_filter
->p_sys
= (filter_sys_t
*)malloc( sizeof( *p_sys
) );
109 if( unlikely( !p_sys
) )
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
)
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
);
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
;
147 vlc_mutex_destroy( &p_sys
->cyclic_block_mutex
);
148 vlc_mutex_destroy( &p_sys
->lock
);
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
;
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
++ )
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
;
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;
223 vlc_mutex_unlock( &p_sys
->cyclic_block_mutex
);
224 vlc_mutex_unlock( &p_sys
->lock
);
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];
245 vsx_logo_intro
* intro
= 0;
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" );
259 /* Manage the events */
260 unsigned width
, height
;
262 if( vlc_gl_surface_CheckSize( p_sys
->gl
, &width
, &height
) )
267 // look for control commands from outside the thread
268 vlc_mutex_lock( &p_sys
->lock
);
273 vlc_mutex_unlock( &p_sys
->lock
);
277 // only run this once
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();
309 if (intro
) intro
->draw();
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
);