From ead0ecc80873a92e8efe4520c4a66ae3a854b8fd Mon Sep 17 00:00:00 2001 From: dridri Date: Sat, 18 Oct 2014 20:02:27 +0200 Subject: [PATCH] clean code, use boost::function for callbacks --- CMakeModules/FindLIBVLC.cmake | 11 ++- src/libtomahawk/CMakeLists.txt | 5 +- src/libtomahawk/audio/AudioEngine.cpp | 10 +-- src/libtomahawk/audio/AudioEngine.h | 3 +- src/libtomahawk/audio/AudioEngine_p.h | 2 - src/libtomahawk/audio/AudioOutput.cpp | 20 +++-- src/libtomahawk/audio/AudioOutput.h | 12 +-- src/libtomahawk/audio/VlcDsp.cpp | 38 +++++++++ src/libtomahawk/audio/VlcDsp.h | 47 +++++++++++ src/libtomahawk/audio/VlcDspHack.cpp | 24 ------ src/libtomahawk/audio/VlcDspHack.h | 28 ------- src/libtomahawk/audio/VlcDspHack_p.h | 107 -------------------------- src/libtomahawk/audio/VlcDsp_p.h | 62 +++++++++++++++ 13 files changed, 185 insertions(+), 184 deletions(-) create mode 100644 src/libtomahawk/audio/VlcDsp.cpp create mode 100644 src/libtomahawk/audio/VlcDsp.h delete mode 100644 src/libtomahawk/audio/VlcDspHack.cpp delete mode 100644 src/libtomahawk/audio/VlcDspHack.h delete mode 100644 src/libtomahawk/audio/VlcDspHack_p.h create mode 100644 src/libtomahawk/audio/VlcDsp_p.h diff --git a/CMakeModules/FindLIBVLC.cmake b/CMakeModules/FindLIBVLC.cmake index 2a31e9128..cd597f04b 100644 --- a/CMakeModules/FindLIBVLC.cmake +++ b/CMakeModules/FindLIBVLC.cmake @@ -10,7 +10,7 @@ # LIBVLC_VERSION if(NOT LIBVLC_MIN_VERSION) - set(LIBVLC_MIN_VERSION "0.0") + set(LIBVLC_MIN_VERSION "2.1") endif(NOT LIBVLC_MIN_VERSION) # find_path and find_library normally search standard locations @@ -43,6 +43,15 @@ PATHS find_path(LIBVLC_INCLUDE_DIR PATHS "${CMAKE_INCLUDE_PATH}/vlc" NAMES vlc.h HINTS ${PC_LIBVLC_INCLUDEDIR} ${PC_LIBVLC_INCLUDE_DIRS}) +find_path(LIBVLCCORE_INCLUDE_DIR vlc_plugin.h +PATHS + "$ENV{LIB_DIR}/include/vlc/plugins" + "/usr/include/vlc/plugins" + "/usr/local/include/vlc/plugins" + #mingw + c:/msys/local/include/vlc/plugins +) + #Put here path to custom location #example: /home/user/vlc/lib etc.. find_library(LIBVLC_LIBRARY NAMES vlc libvlc diff --git a/src/libtomahawk/CMakeLists.txt b/src/libtomahawk/CMakeLists.txt index ccb03f607..a110b024d 100644 --- a/src/libtomahawk/CMakeLists.txt +++ b/src/libtomahawk/CMakeLists.txt @@ -215,7 +215,7 @@ list(APPEND libSources audio/AudioEngine.cpp audio/AudioOutput.cpp - audio/VlcDspHack.cpp + audio/VlcDsp.cpp collection/Collection.cpp collection/ArtistsRequest.cpp @@ -393,7 +393,8 @@ include_directories( ${ECHONEST_INCLUDE_DIR} ${LUCENEPP_INCLUDE_DIRS} ${PHONON_INCLUDES} - ${LIBVLC_INCLUDES} + ${LIBVLC_INCLUDE_DIR} + ${LIBVLCCORE_INCLUDE_DIR} ${Boost_INCLUDE_DIR} ${LIBPORTFWD_INCLUDE_DIR} diff --git a/src/libtomahawk/audio/AudioEngine.cpp b/src/libtomahawk/audio/AudioEngine.cpp index 649ca150d..97e1838f1 100644 --- a/src/libtomahawk/audio/AudioEngine.cpp +++ b/src/libtomahawk/audio/AudioEngine.cpp @@ -76,7 +76,7 @@ AudioEnginePrivate::onStateChanged( AudioOutput::AudioState newState, AudioOutpu { q_ptr->stop( AudioEngine::UnknownError ); -//TODO tDebug() << "Phonon Error:" << audioOutput->errorString() << audioOutput->errorType(); +//TODO tDebug() << "AudioOutput Error:" << audioOutput->errorString() << audioOutput->errorType(); emit q_ptr->error( AudioEngine::UnknownError ); q_ptr->setState( AudioEngine::Error ); @@ -97,7 +97,6 @@ AudioEnginePrivate::onStateChanged( AudioOutput::AudioState newState, AudioOutpu } if ( newState == AudioOutput::Stopped && oldState == AudioOutput::Paused ) { - // GStreamer backend hack: instead of going from PlayingState to StoppedState, it traverses PausedState q_ptr->setState( AudioEngine::Stopped ); } @@ -508,7 +507,6 @@ bool AudioEngine::isMuted() const { return d_func()->audioOutput->isMuted(); - return 0; } @@ -1248,8 +1246,8 @@ AudioEngine::currentTrackTotalTime() const { // TODO : This is too hacky. The problem is that I don't know why // libVLC doesn't report total duration for stream data (imem://) - // But it's not a real problem for playback, since - // EndOfStream is emitted by libVLC itself + // But it's not a real problem for playback, since EndOfStream is emitted by libVLC itself + // This value is only used by AudioOutput to evaluate if it's close to end of stream if ( d_func()->audioOutput->totalTime() <= 0 && d_func()->currentTrack && d_func()->currentTrack->track() ) { return d_func()->currentTrack->track()->duration() * 1000 + 1000; } @@ -1341,7 +1339,7 @@ AudioEngine::setCurrentTrackPlaylist( const playlistinterface_ptr& playlist ) void -AudioEngine::setDspCallback( void ( *cb ) ( float*, int, int ) ) +AudioEngine::setDspCallback( boost::function< void( int state, int frameNumber, float* samples, int nb_channels, int nb_samples ) > cb ) { Q_D( AudioEngine ); diff --git a/src/libtomahawk/audio/AudioEngine.h b/src/libtomahawk/audio/AudioEngine.h index f00ea04a7..28570cc23 100644 --- a/src/libtomahawk/audio/AudioEngine.h +++ b/src/libtomahawk/audio/AudioEngine.h @@ -24,6 +24,7 @@ #include "../Typedefs.h" #include +#include #include "DllMacro.h" @@ -109,7 +110,7 @@ public: */ qint64 currentTrackTotalTime() const; - void setDspCallback( void ( *cb ) ( float* samples, int nb_channels, int nb_samples ) ); + void setDspCallback( boost::function< void( int state, int frameNumber, float* samples, int nb_channels, int nb_samples ) > cb ); public slots: void playPause(); diff --git a/src/libtomahawk/audio/AudioEngine_p.h b/src/libtomahawk/audio/AudioEngine_p.h index b7c736626..c2e5edf76 100644 --- a/src/libtomahawk/audio/AudioEngine_p.h +++ b/src/libtomahawk/audio/AudioEngine_p.h @@ -59,7 +59,5 @@ private: QTemporaryFile* coverTempFile; - void (* dspPluginCallback )( signed short* samples, int nb_channels, int nb_samples ); - static AudioEngine* s_instance; }; diff --git a/src/libtomahawk/audio/AudioOutput.cpp b/src/libtomahawk/audio/AudioOutput.cpp index e893524d5..3b8b82c11 100644 --- a/src/libtomahawk/audio/AudioOutput.cpp +++ b/src/libtomahawk/audio/AudioOutput.cpp @@ -2,7 +2,7 @@ * * Copyright 2010-2014, Christian Muehlhaeuser * Copyright 2010-2012, Jeff Mitchell - * Copyright 2013, Teo Mrnjavac + * Copyright 2014, Adrien Aubry * * Tomahawk is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,7 +20,7 @@ #include "AudioEngine.h" #include "AudioOutput.h" -#include "VlcDspHack.h" +#include "VlcDsp.h" #include "utils/Logger.h" @@ -61,6 +61,7 @@ AudioOutput::AudioOutput( QObject* parent ) , m_currentTime( 0 ) , m_totalTime( 0 ) , m_aboutToFinish( false ) + , m_justSeeked( false ) , dspPluginCallback( 0 ) , vlcInstance( 0 ) , vlcPlayer( 0 ) @@ -177,6 +178,7 @@ AudioOutput::setCurrentSource(MediaStream* stream) currentStream = stream; m_totalTime = 0; m_currentTime = 0; + m_justSeeked = false; seekable = true; QByteArray url; @@ -223,8 +225,7 @@ AudioOutput::setCurrentSource(MediaStream* stream) libvlc_media_player_set_media( vlcPlayer, vlcMedia ); #ifdef VLC_DSP_PLUGIN_ENABLED - // This is very, very tricky - VlcDspHackInstall( vlcPlayer ); + VlcDspSetup( vlcPlayer ); #endif if ( stream->type() == MediaStream::Url ) { @@ -378,8 +379,9 @@ AudioOutput::seek( qint64 milliseconds ) return; } - tDebug() << "AudioOutput:: seeking" << milliseconds << "msec"; +// tDebug() << "AudioOutput:: seeking" << milliseconds << "msec"; + m_justSeeked = true; libvlc_media_player_set_time ( vlcPlayer, milliseconds ); setCurrentTime( milliseconds ); } @@ -487,18 +489,20 @@ AudioOutput::vlcEventCallback( const libvlc_event_t* event, void* opaque ) void -AudioOutput::s_dspCallback( float* samples, int nb_channels, int nb_samples ) +AudioOutput::s_dspCallback( int frameNumber, float* samples, int nb_channels, int nb_samples ) { // tDebug() << Q_FUNC_INFO; + int state = AudioOutput::instance()->m_justSeeked ? 1 : 0; + AudioOutput::instance()->m_justSeeked = false; if ( AudioOutput::instance()->dspPluginCallback ) { - AudioOutput::instance()->dspPluginCallback( samples, nb_channels, nb_samples ); + AudioOutput::instance()->dspPluginCallback( state, frameNumber, samples, nb_channels, nb_samples ); } } void -AudioOutput::setDspCallback( void ( *cb ) ( float*, int, int ) ) +AudioOutput::setDspCallback( boost::function< void( int, int, float*, int, int ) > cb ) { dspPluginCallback = cb; } diff --git a/src/libtomahawk/audio/AudioOutput.h b/src/libtomahawk/audio/AudioOutput.h index 3d2388031..91df31c57 100644 --- a/src/libtomahawk/audio/AudioOutput.h +++ b/src/libtomahawk/audio/AudioOutput.h @@ -2,7 +2,7 @@ * * Copyright 2010-2014, Christian Muehlhaeuser * Copyright 2010-2012, Jeff Mitchell - * Copyright 2013, Teo Mrnjavac + * Copyright 2014, Adrien Aubry * * Tomahawk is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,9 +24,10 @@ #include "../Typedefs.h" #include "DllMacro.h" - #include "utils/MediaStream.h" +#include + struct libvlc_instance_t; struct libvlc_media_player_t; struct libvlc_media_t; @@ -61,7 +62,7 @@ public: qint64 totalTime(); void setAutoDelete ( bool ad ); - void setDspCallback( void ( *cb ) ( float*, int, int ) ); + void setDspCallback( boost::function< void( int, int, float*, int, int ) > cb ); static AudioOutput* instance(); @@ -78,7 +79,7 @@ private: void setTotalTime( qint64 time ); static void vlcEventCallback( const libvlc_event_t *event, void *opaque ); - static void s_dspCallback( float* samples, int nb_channels, int nb_samples ); + static void s_dspCallback( int frameNumber, float* samples, int nb_channels, int nb_samples ); static AudioOutput* s_instance; AudioState currentState; @@ -90,8 +91,9 @@ private: qint64 m_currentTime; qint64 m_totalTime; bool m_aboutToFinish; + bool m_justSeeked; - void ( *dspPluginCallback ) ( float* samples, int nb_channels, int nb_samples ); + boost::function< void( int state, int frameNumber, float* samples, int nb_channels, int nb_samples ) > dspPluginCallback; libvlc_instance_t* vlcInstance; libvlc_media_player_t* vlcPlayer; diff --git a/src/libtomahawk/audio/VlcDsp.cpp b/src/libtomahawk/audio/VlcDsp.cpp new file mode 100644 index 000000000..b5ebeaf6f --- /dev/null +++ b/src/libtomahawk/audio/VlcDsp.cpp @@ -0,0 +1,38 @@ +/* === This file is part of Tomahawk Player - === + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ + +#include "VlcDsp_p.h" + +#ifdef VLC_DSP_PLUGIN_ENABLED + +void +VlcDspSetup( libvlc_media_player_t* vlcPlayer ) +{ + vlc_value_t val; + + if ( vlcPlayer->input.p_resource != 0 ) { + audio_output_t *aout = input_resource_GetAout( vlcPlayer->input.p_resource ); + if ( aout != 0 ) { + var_Create( ( vlc_object_t* )aout, "audio-filter", VLC_VAR_STRING ); + val.psz_string = (char*)"dsp"; + var_SetChecked( ( vlc_object_t* )aout, "audio-filter", VLC_VAR_STRING, val ); + aout->event.restart_request( aout, AOUT_RESTART_FILTERS ); + input_resource_PutAout( vlcPlayer->input.p_resource, aout ); + } + } +} + +#endif // VLC_DSP_PLUGIN_ENABLED diff --git a/src/libtomahawk/audio/VlcDsp.h b/src/libtomahawk/audio/VlcDsp.h new file mode 100644 index 000000000..2b2d6da0c --- /dev/null +++ b/src/libtomahawk/audio/VlcDsp.h @@ -0,0 +1,47 @@ +/* === This file is part of Tomahawk Player - === + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ + +#ifndef VLCDSP_H +#define VLCDSP_H + +/* + * Very tricky technique to enable DSP plugin support + * There is no other way to do this with libvlc for the moment + * + * A VLC audio filter plugin specificaly made for Tomahawk + * is necessary to get it functionning + * + * TODO : Check thoroughly if libvlc_media_player_t + * structure is identical to the one in running libvlccore.so + * (checking if libVLC version is >= than the one used to compile Tomahawk + * and verifying pointers integrity should be enough. + * + check if audio filter "dsp" is present in libVLC plugins list) + * + * The define 'VLC_DSP_PLUGIN_ENABLED' better should be a CMake option +*/ + +// Maybe later :) +//#define VLC_DSP_PLUGIN_ENABLED + +#ifdef VLC_DSP_PLUGIN_ENABLED + +struct libvlc_media_player_t; + +void VlcDspSetup( libvlc_media_player_t* vlcPlayer ); + +#endif // VLC_DSP_PLUGIN_ENABLED + +#endif // VLCDSP_H \ No newline at end of file diff --git a/src/libtomahawk/audio/VlcDspHack.cpp b/src/libtomahawk/audio/VlcDspHack.cpp deleted file mode 100644 index 56f2952b8..000000000 --- a/src/libtomahawk/audio/VlcDspHack.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "VlcDspHack_p.h" - -#ifdef VLC_DSP_PLUGIN_ENABLED - -void -VlcDspHackInstall( libvlc_media_player_t* vlcPlayer ) -{ - if ( vlcPlayer->input.p_resource != 0 ) { - audio_output_t *aout = input_resource_GetAout( vlcPlayer->input.p_resource ); - if ( aout != 0 ) { - var_Create( (vlc_object_t*)aout, "audio-filter", 0x0040 /*VLC_VAR_STRING*/ ); - - vlc_value_t val; - val.psz_string = (char*)"dsp"; - var_SetChecked( (vlc_object_t*)aout, "audio-filter", 0x0040 /*VLC_VAR_STRING*/, val ); - - aout->event.restart_request( aout, 1 /*AOUT_RESTART_FILTERS*/ ); - - input_resource_PutAout( vlcPlayer->input.p_resource, aout ); - } - } -} - -#endif // VLC_DSP_PLUGIN_ENABLED diff --git a/src/libtomahawk/audio/VlcDspHack.h b/src/libtomahawk/audio/VlcDspHack.h deleted file mode 100644 index 1c8a05cc2..000000000 --- a/src/libtomahawk/audio/VlcDspHack.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef VLCDSPHACK_H -#define VLCDSPHACK_H - -/* - * Very tricky technique to enable DSP plugin support - * There is no other way to do this with libvlc for the moment - * - * A VLC audio filter plugin specificaly made for Tomahawk - * is necessary to get it functionning - * - * TODO : Check thoroughly if both libvlc_media_player_t and audio_output - * structures are identical to those in the running libvlccore.so - * (checking if libVLC version is >= than the one used to compile Tomahawk - * and verifying pointers integrity should be enough. - * + check if audio filter "dsp" is present in libVLC plugins list) -*/ - -//#define VLC_DSP_PLUGIN_ENABLED - -#ifdef VLC_DSP_PLUGIN_ENABLED - -struct libvlc_media_player_t; - -void VlcDspHackInstall( libvlc_media_player_t* vlcPlayer ); - -#endif // VLC_DSP_PLUGIN_ENABLED - -#endif // VLCDSPHACK_H \ No newline at end of file diff --git a/src/libtomahawk/audio/VlcDspHack_p.h b/src/libtomahawk/audio/VlcDspHack_p.h deleted file mode 100644 index 0a54f152d..000000000 --- a/src/libtomahawk/audio/VlcDspHack_p.h +++ /dev/null @@ -1,107 +0,0 @@ -#ifndef VLCDSPHACK_P_H -#define VLCDSPHACK_P_H - -#include "VlcDspHack.h" - -#ifdef VLC_DSP_PLUGIN_ENABLED - -#include - -#include -#include -#include -#include -#include - -// TODO : Replace all these copy-pasted strutures by vlc/plugins/?.h includes - -typedef struct audio_output audio_output_t; -typedef struct input_resource_t input_resource_t; -typedef struct vlc_object_t vlc_object_t; -typedef struct vlc_list_t vlc_list_t; -typedef int64_t mtime_t; -typedef pthread_mutex_t vlc_mutex_t; - -typedef union -{ - int64_t i_int; - bool b_bool; - float f_float; - char * psz_string; - void * p_address; - vlc_object_t * p_object; - vlc_list_t * p_list; - mtime_t i_time; - struct { int32_t x; int32_t y; } coords; - -} vlc_value_t; - -struct audio_output -{ - const char *psz_object_type; - char *psz_header; - int i_flags; - bool b_force; - void *p_libvlc; - void * p_parent; - - void *sys; - int (*start)(audio_output_t *, void *fmt); - void (*stop)(audio_output_t *); - int (*time_get)(audio_output_t *, void *delay); - void (*play)(audio_output_t *, void *); - void (*pause)( audio_output_t *, bool pause, mtime_t date); - void (*flush)( audio_output_t *, bool wait); - int (*volume_set)(audio_output_t *, float volume); - int (*mute_set)(audio_output_t *, bool mute); - int (*device_select)(audio_output_t *, const char *id); - struct { - void (*volume_report)(audio_output_t *, float); - void (*mute_report)(audio_output_t *, bool); - void (*policy_report)(audio_output_t *, bool); - void (*device_report)(audio_output_t *, const char *); - void (*hotplug_report)(audio_output_t *, const char *, const char *); - int (*gain_request)(audio_output_t *, float); - void (*restart_request)(audio_output_t *, unsigned); - } event; -}; - - -struct libvlc_media_player_t -{ - const char *psz_object_type; - char *psz_header; - int i_flags; - bool b_force; - void *p_libvlc; - void *p_parent; - - int i_refcount; - vlc_mutex_t object_lock; - - struct - { - void *p_thread; - input_resource_t *p_resource; - vlc_mutex_t lock; - } input; - - void *p_libvlc_instance; - void *p_md; - void *p_event_manager; - int state; -}; - -extern "C" { - audio_output_t* input_resource_GetAout(input_resource_t*); - void input_resource_PutAout(input_resource_t*, audio_output_t*); - int var_Create(vlc_object_t *, const char *, int ); - int var_SetChecked( vlc_object_t *, const char *, int, vlc_value_t ); - static inline void aout_RestartRequest(audio_output_t *aout, unsigned mode){ - aout->event.restart_request(aout, mode); - } -}; - -#endif // VLC_DSP_PLUGIN_ENABLED - -#endif // VLCDSPHACK_P_H diff --git a/src/libtomahawk/audio/VlcDsp_p.h b/src/libtomahawk/audio/VlcDsp_p.h new file mode 100644 index 000000000..67e8a6482 --- /dev/null +++ b/src/libtomahawk/audio/VlcDsp_p.h @@ -0,0 +1,62 @@ +/* === This file is part of Tomahawk Player - === + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ + +#ifndef VLCDSP_P_H +#define VLCDSP_P_H + +#include "VlcDsp.h" + +#ifdef VLC_DSP_PLUGIN_ENABLED + +#include + +#include +#include +#include +#include +#include + +#define restrict +#define _(s) s + +#include +#include + +typedef struct libvlc_media_player_t +{ + const char *psz_object_type; + char *psz_header; + int i_flags; + bool b_force; + void *p_libvlc; + void *p_parent; + int i_refcount; + vlc_mutex_t object_lock; + struct + { + void *p_thread; + input_resource_t *p_resource; + vlc_mutex_t lock; + } input; + void *p_libvlc_instance; + void *p_md; + void *p_event_manager; + int state; +} libvlc_media_player_t; + +#endif // VLC_DSP_PLUGIN_ENABLED + +#endif // VLCDSP_P_H