]> git.sesse.net Git - vlc/blobdiff - modules/visualization/projectm.cpp
Merge branch 'master' of git://git.videolan.org/vlc
[vlc] / modules / visualization / projectm.cpp
index 5fa0922a5e94e9baaf1bfc28a4fcc29df73255a9..90ca9c8eefc3752ca381b641f3ee1ad55d4565c6 100644 (file)
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
+#ifndef __STDC_CONSTANT_MACROS
+# define __STDC_CONSTANT_MACROS
+#endif
 
 #include <vlc_common.h>
 #include <vlc_plugin.h>
 #include <vlc_aout.h>
 #include <vlc_vout.h>
+#include <vlc_vout_wrapper.h>
 #include <vlc_filter.h>
 
 #include <libprojectM/projectM.hpp>
@@ -44,6 +48,15 @@ static void Close        ( vlc_object_t * );
 #define CONFIG_LONGTEXT N_("File that will be used to configure the projectM " \
                            "module.")
 
+#define PRESET_PATH_TXT N_("projectM preset path")
+#define PRESET_PATH_LONGTXT N_("Path to the projectM preset directory")
+
+#define TITLE_FONT_TXT N_("Title font")
+#define TITLE_FONT_LONGTXT N_("Font used for the titles")
+
+#define MENU_FONT_TXT N_("Font menu")
+#define MENU_FONT_LONGTXT N_("Font used for the menus")
+
 #define WIDTH_TEXT N_("Video width")
 #define WIDTH_LONGTEXT N_("The width of the video window, in pixels.")
 
@@ -56,8 +69,17 @@ vlc_module_begin ()
     set_capability( "visualization2", 0 )
     set_category( CAT_AUDIO )
     set_subcategory( SUBCAT_AUDIO_VISUAL )
+#ifndef HAVE_PROJECTM2
     add_file( "projectm-config", "/usr/share/projectM/config.inp", NULL,
-                CONFIG_TEXT, CONFIG_LONGTEXT, true )
+              CONFIG_TEXT, CONFIG_LONGTEXT, true )
+#else
+    add_file( "projectm-preset-path", "/usr/share/projectM/presets", NULL,
+              PRESET_PATH_TXT, PRESET_PATH_LONGTXT, true )
+    add_file( "projectm-title-font", "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf", NULL,
+              TITLE_FONT_TXT, TITLE_FONT_LONGTXT, true )
+    add_file( "projectm-menu-font", "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf", NULL,
+              MENU_FONT_TXT, MENU_FONT_LONGTXT, true )
+#endif
     add_integer( "projectm-width", 800, NULL, WIDTH_TEXT, WIDTH_LONGTEXT,
                  false )
     add_integer( "projectm-height", 640, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT,
@@ -77,13 +99,19 @@ struct filter_sys_t
     vlc_sem_t    ready;
     bool         b_error;
 
-    /* video output module and opengl provider */
-    vout_thread_t *p_opengl;
-    module_t      *p_module;
+    /* Opengl */
+    vout_thread_t  *p_vout;
+    vout_display_t *p_vd;
 
     /* libprojectM objects */
     projectM      *p_projectm;
+#ifndef HAVE_PROJECTM2
     char          *psz_config;
+#else
+    char          *psz_preset_path;
+    char          *psz_title_font;
+    char          *psz_menu_font;
+#endif
 
     /* Window size */
     int i_width;
@@ -92,7 +120,9 @@ struct filter_sys_t
     /* audio info */
     int i_channels;
 
+    /* */
     vlc_mutex_t lock;
+    bool  b_quit;
     float *p_buffer;
     int   i_buffer_size;
     int   i_nb_samples;
@@ -102,53 +132,6 @@ struct filter_sys_t
 static block_t *DoWork( filter_t *, block_t * );
 static void *Thread( void * );
 
-
-/**
- * Init the openGL context
- * p_thread: projectm thread object
- * @return VLC_SUCCESS or vlc error codes
- */
-static int initOpenGL( filter_t *p_filter )
-{
-    filter_sys_t *p_sys = p_filter->p_sys;
-
-    p_sys->p_opengl =
-        (vout_thread_t *)vlc_object_create( p_filter, sizeof(vout_thread_t) );
-    if( !p_sys->p_opengl )
-        return VLC_ENOMEM;
-
-    vlc_object_attach( p_sys->p_opengl, p_filter );
-
-    /* Initialize the opengl object */
-    video_format_Setup( &p_sys->p_opengl->fmt_in, VLC_CODEC_RGB32,
-                        p_sys->i_width, p_sys->i_height, 1 );
-    p_sys->p_opengl->i_window_width = p_sys->i_width;
-    p_sys->p_opengl->i_window_height = p_sys->i_height;
-    p_sys->p_opengl->render.i_width = p_sys->i_width;
-    p_sys->p_opengl->render.i_height = p_sys->i_height;
-    p_sys->p_opengl->render.i_aspect = VOUT_ASPECT_FACTOR;
-    p_sys->p_opengl->b_fullscreen = false;
-    p_sys->p_opengl->b_autoscale = true;
-    p_sys->p_opengl->i_alignment = 0;
-    p_sys->p_opengl->fmt_in.i_sar_num = 1;
-    p_sys->p_opengl->fmt_in.i_sar_den = 1;
-    p_sys->p_opengl->fmt_render = p_sys->p_opengl->fmt_in;
-
-    /* Ask for the opengl provider */
-    p_sys->p_module = module_need( p_sys->p_opengl, "opengl provider",
-                                   NULL, false );
-    if( !p_sys->p_module )
-    {
-        msg_Err( p_filter, "unable to initialize OpenGL" );
-        vlc_object_detach( p_sys->p_opengl );
-        vlc_object_release( p_sys->p_opengl );
-        return VLC_EGENERIC;
-    }
-
-    return VLC_SUCCESS;
-}
-
-
 /**
  * Open the module
  * @param p_this: the filter object
@@ -181,10 +164,17 @@ static int Open( vlc_object_t * p_this )
     /* Create the object for the thread */
     vlc_sem_init( &p_sys->ready, 0 );
     p_sys->b_error  = false;
-    p_sys->i_width  = var_CreateGetInteger( p_filter, "projectm-width" );
-    p_sys->i_height = var_CreateGetInteger( p_filter, "projectm-height" );
+    p_sys->b_quit   = false;
+    p_sys->i_width  = var_InheritInteger( p_filter, "projectm-width" );
+    p_sys->i_height = var_InheritInteger( p_filter, "projectm-height" );
     p_sys->i_channels = aout_FormatNbChannels( &p_filter->fmt_in.audio );
-    p_sys->psz_config = var_CreateGetString( p_filter, "projectm-config" );
+#ifndef HAVE_PROJECTM2
+    p_sys->psz_config = var_InheritString( p_filter, "projectm-config" );
+#else
+    p_sys->psz_preset_path = var_InheritString( p_filter, "projectm-preset-path" );
+    p_sys->psz_title_font = var_InheritString( p_filter, "projectm-title-font" );
+    p_sys->psz_menu_font = var_InheritString( p_filter, "projectm-menu-font" );
+#endif
     vlc_mutex_init( &p_sys->lock );
     p_sys->p_buffer = NULL;
     p_sys->i_buffer_size = 0;
@@ -219,15 +209,26 @@ static void Close( vlc_object_t *p_this )
     filter_t     *p_filter = (filter_t *)p_this;
     filter_sys_t *p_sys = p_filter->p_sys;
 
-    /* Stop the thread */
-    vlc_cancel( p_sys->thread );
+    /* Stop the thread
+     * XXX vlc_cleanup_push does not seems to work with C++ so no
+     * vlc_cancel()... */
+    vlc_mutex_lock( &p_sys->lock );
+    p_sys->b_quit = true;
+    vlc_mutex_unlock( &p_sys->lock );
+
     vlc_join( p_sys->thread, NULL );
 
     /* Free the ressources */
     vlc_sem_destroy( &p_sys->ready );
     vlc_mutex_destroy( &p_sys->lock );
     free( p_sys->p_buffer );
+#ifndef HAVE_PROJECTM2
     free( p_sys->psz_config );
+#else
+    free( p_sys->psz_preset_path );
+    free( p_sys->psz_title_font );
+    free( p_sys->psz_menu_font );
+#endif
     free( p_sys );
 }
 
@@ -246,11 +247,17 @@ static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
     vlc_mutex_lock( &p_sys->lock );
     if( p_sys->i_buffer_size > 0 )
     {
-        p_sys->p_buffer[0] = 0;
         p_sys->i_nb_samples = __MIN( p_sys->i_buffer_size,
                                      p_in_buf->i_nb_samples );
+
+        const float *p_src = (float*)p_in_buf->p_buffer;
         for( int i = 0; i < p_sys->i_nb_samples; i++ )
-            p_sys->p_buffer[i] = p_in_buf->p_buffer[i];
+        {
+            float v = 0;
+            for( int j = 0; j < p_sys->i_channels; j++ )
+                v += p_src[p_sys->i_channels * i + j];
+            p_sys->p_buffer[i] = v / p_sys->i_channels;
+        }
     }
     vlc_mutex_unlock( &p_sys->lock );
 
@@ -258,20 +265,18 @@ static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
 }
 
 /**
- * Clean up function when Thread() is cancelled.
+ * Variable callback for the dummy vout
  */
-static void ThreadCleanup( void *p_data )
+static int VoutCallback( vlc_object_t *p_vout, char const *psz_name,
+                         vlc_value_t oldv, vlc_value_t newv, void *p_data )
 {
-    filter_t     *p_filter = (filter_t*)p_data;
-    filter_sys_t *p_sys = p_filter->p_sys;
+    vout_display_t *p_vd = (vout_display_t*)p_data;
 
-    /* Cleanup */
-    delete p_sys->p_projectm;
-
-    /* Free the openGL provider */
-    module_unneed( p_sys->p_opengl, p_sys->p_module );
-    vlc_object_detach( p_sys->p_opengl );
-    vlc_object_release( p_sys->p_opengl );
+    if( !strcmp(psz_name, "fullscreen") )
+    {
+        vout_SetDisplayFullscreen( p_vd, newv.b_bool );
+    }
+    return VLC_SUCCESS;
 }
 
 /**
@@ -283,21 +288,78 @@ static void *Thread( void *p_data )
     filter_t     *p_filter = (filter_t*)p_data;
     filter_sys_t *p_sys = p_filter->p_sys;
     int cancel = vlc_savecancel();
+    video_format_t fmt;
+    vout_opengl_t *gl;
+    int i_last_width  = 0;
+    int i_last_height = 0;
+#ifdef HAVE_PROJECTM2
+    projectM::Settings settings;
+#endif
 
     /* Create the openGL provider */
-    if( initOpenGL( p_filter ) )
+    p_sys->p_vout =
+        (vout_thread_t *)vlc_object_create( p_filter, sizeof(vout_thread_t) );
+    if( !p_sys->p_vout )
+        goto error;
+
+    vlc_object_attach( p_sys->p_vout, p_filter );
+
+    /* */
+    video_format_Init( &fmt, 0 );
+    video_format_Setup( &fmt, VLC_CODEC_RGB32,
+                        p_sys->i_width, p_sys->i_height, 0, 1 );
+    fmt.i_sar_num = 1;
+    fmt.i_sar_den = 1;
+
+    vout_display_state_t state;
+    memset( &state, 0, sizeof(state) );
+    state.cfg.display.sar.num = 1;
+    state.cfg.display.sar.den = 1;
+    state.cfg.is_display_filled = true;
+    state.cfg.zoom.num = 1;
+    state.cfg.zoom.den = 1;
+    state.sar.num = 1;
+    state.sar.den = 1;
+
+    p_sys->p_vd = vout_NewDisplay( p_sys->p_vout, &fmt, &state, "opengl",
+                                   300000, 1000000 );
+    if( !p_sys->p_vd )
     {
-        p_sys->b_error = true;
-        vlc_sem_post( &p_sys->ready );
-        return NULL;
+        vlc_object_release( p_sys->p_vout );
+        goto error;
     }
-    vlc_cleanup_push( ThreadCleanup, p_filter );
+    var_Create( p_sys->p_vout, "fullscreen", VLC_VAR_BOOL );
+    var_AddCallback( p_sys->p_vout, "fullscreen", VoutCallback, p_sys->p_vd );
 
-    /* Initialize the opengl provider for this thread */
-    p_sys->p_opengl->pf_init( p_sys->p_opengl );
+    gl = vout_GetDisplayOpengl( p_sys->p_vd );
+    if( !gl )
+    {
+        vout_DeleteDisplay( p_sys->p_vd, NULL );
+        vlc_object_release( p_sys->p_vout );
+        goto error;
+    }
 
     /* Create the projectM object */
+#ifndef HAVE_PROJECTM2
     p_sys->p_projectm = new projectM( p_sys->psz_config );
+#else
+    settings.meshX = 32;
+    settings.meshY = 24;
+    settings.fps = 35;
+    settings.textureSize = 1024;
+    settings.windowWidth = p_sys->i_width;
+    settings.windowHeight = p_sys->i_height;
+    settings.presetURL = p_sys->psz_preset_path;
+    settings.titleFontURL =  p_sys->psz_title_font;
+    settings.menuFontURL = p_sys->psz_menu_font;
+    settings.smoothPresetDuration = 5;
+    settings.presetDuration = 30;
+    settings.beatSensitivity = 10;
+    settings.aspectCorrection = 1;
+    settings.easterEgg = 1;
+    settings.shuffleEnabled = 1;
+    p_sys->p_projectm = new projectM( settings );
+#endif
     p_sys->i_buffer_size = p_sys->p_projectm->pcm()->maxsamples;
     p_sys->p_buffer = (float*)calloc( p_sys->i_buffer_size,
                                       sizeof( float ) );
@@ -307,29 +369,59 @@ static void *Thread( void *p_data )
     /* TODO: Give to projectm the name of the input
     p_sys->p_projectm->projectM_setTitle( "" ); */
 
-    /* Reset the dislay to get the right size */
-    p_sys->p_projectm->projectM_resetGL( p_sys->i_width,
-                                         p_sys->i_height );
-
+    /* */
     for( ;; )
     {
+        const mtime_t i_deadline = mdate() + CLOCK_FREQ / 50; /* 50 fps max */
         /* Manage the events */
-        p_sys->p_opengl->pf_manage( p_sys->p_opengl );
+        vout_ManageDisplay( p_sys->p_vd, true );
+        if( p_sys->p_vd->cfg->display.width  != i_last_width ||
+            p_sys->p_vd->cfg->display.height != i_last_height )
+        {
+            /* FIXME it is not perfect as we will have black bands */
+            vout_display_place_t place;
+            vout_display_PlacePicture( &place, &p_sys->p_vd->source, p_sys->p_vd->cfg, false );
+            p_sys->p_projectm->projectM_resetGL( place.width, place.height );
+
+            i_last_width  = p_sys->p_vd->cfg->display.width;
+            i_last_height = p_sys->p_vd->cfg->display.height;
+        }
+
         /* Render the image and swap the buffers */
         vlc_mutex_lock( &p_sys->lock );
         if( p_sys->i_nb_samples > 0 )
+        {
             p_sys->p_projectm->pcm()->addPCMfloat( p_sys->p_buffer,
                                                    p_sys->i_nb_samples );
+            p_sys->i_nb_samples = 0;
+        }
+        if( p_sys->b_quit )
+        {
+            vlc_mutex_unlock( &p_sys->lock );
+
+            delete p_sys->p_projectm;
+            vout_DeleteDisplay( p_sys->p_vd, NULL );
+            vlc_object_release( p_sys->p_vout );
+            return NULL;
+        }
+        vlc_mutex_unlock( &p_sys->lock );
 
         p_sys->p_projectm->renderFrame();
-        p_sys->p_opengl->pf_swap( p_sys->p_opengl );
-        vlc_mutex_unlock( &p_sys->lock );
 
-        /* TODO: use a fps limiter */
-        vlc_restorecancel( cancel );
-        msleep( 10000 );
-        cancel = vlc_savecancel();
+        /* */
+        mwait( i_deadline );
+
+        if( !vout_opengl_Lock(gl) )
+        {
+            vout_opengl_Swap( gl );
+            vout_opengl_Unlock( gl );
+        }
     }
-    vlc_cleanup_pop();
+    abort();
+
+error:
+    p_sys->b_error = true;
+    vlc_sem_post( &p_sys->ready );
+    return NULL;
 }