[vlc-devel] [PATCH 06/12] npapi: switch VLCPlugin to vlc_player

Sergey Radionov rsatom at gmail.com
Sat Apr 21 18:07:47 CEST 2012


---
 npapi/vlcplugin_base.cpp |  134 ++-------------------------------------------
 npapi/vlcplugin_base.h   |   69 +++++++++++++-----------
 npapi/vlcplugin_win.cpp  |    5 ++-
 3 files changed, 47 insertions(+), 161 deletions(-)

diff --git a/npapi/vlcplugin_base.cpp b/npapi/vlcplugin_base.cpp
index 1d9db49..3941a76 100644
--- a/npapi/vlcplugin_base.cpp
+++ b/npapi/vlcplugin_base.cpp
@@ -351,10 +351,7 @@ VlcPluginBase::VlcPluginBase( NPP instance, NPuint16_t mode ) :
     i_npmode(mode),
     b_stream(0),
     psz_target(NULL),
-    playlist_index(-1),
     libvlc_instance(NULL),
-    libvlc_media_list(NULL),
-    libvlc_media_player(NULL),
     p_scriptClass(NULL),
     p_browser(instance),
     psz_baseURL(NULL)
@@ -496,7 +493,8 @@ NPError VlcPluginBase::init(int argc, char* const argn[], char* const argv[])
     libvlc_instance = libvlc_new(ppsz_argc, ppsz_argv);
     if( !libvlc_instance )
         return NPERR_GENERIC_ERROR;
-    libvlc_media_list = libvlc_media_list_new(libvlc_instance);
+
+    vlc_player::open(libvlc_instance);
 
     /*
     ** fetch plugin base URL, which is the URL of the page containing the plugin
@@ -557,15 +555,14 @@ VlcPluginBase::~VlcPluginBase()
     free(psz_baseURL);
     free(psz_target);
 
-    if( libvlc_media_player )
+    if( vlc_player::is_open() )
     {
+
         if( playlist_isplaying() )
             playlist_stop();
         events.unhook_manager( this );
-        libvlc_media_player_release( libvlc_media_player );
+        vlc_player::close();
     }
-    if( libvlc_media_list )
-        libvlc_media_list_release( libvlc_media_list );
     if( libvlc_instance )
         libvlc_release( libvlc_instance );
 
@@ -580,130 +577,11 @@ void VlcPluginBase::setWindow(const NPWindow &window)
 /*****************************************************************************
  * VlcPluginBase playlist replacement methods
  *****************************************************************************/
-int VlcPluginBase::playlist_add( const char *mrl )
-{
-    int item = -1;
-    libvlc_media_t *p_m = libvlc_media_new_location(libvlc_instance,mrl);
-    if( !p_m )
-        return -1;
-    assert( libvlc_media_list );
-    libvlc_media_list_lock(libvlc_media_list);
-    if( !libvlc_media_list_add_media(libvlc_media_list,p_m) )
-        item = libvlc_media_list_count(libvlc_media_list)-1;
-    libvlc_media_list_unlock(libvlc_media_list);
-
-    libvlc_media_release(p_m);
-
-    return item;
-}
-
-int VlcPluginBase::playlist_add_extended_untrusted( const char *mrl, const char *,
-                    int optc, const char **optv )
-{
-    libvlc_media_t *p_m;
-    int item = -1;
-
-    assert( libvlc_media_list );
-
-    p_m = libvlc_media_new_location(libvlc_instance, mrl);
-    if( !p_m )
-        return -1;
-
-    for( int i = 0; i < optc; ++i )
-        libvlc_media_add_option_flag(p_m, optv[i], libvlc_media_option_unique);
-
-    libvlc_media_list_lock(libvlc_media_list);
-    if( !libvlc_media_list_add_media(libvlc_media_list,p_m) )
-        item = libvlc_media_list_count(libvlc_media_list)-1;
-    libvlc_media_list_unlock(libvlc_media_list);
-    libvlc_media_release(p_m);
-
-    return item;
-}
-
-bool VlcPluginBase::playlist_select( int idx )
-{
-    libvlc_media_t *p_m = NULL;
-
-    assert( libvlc_media_list );
-
-    libvlc_media_list_lock(libvlc_media_list);
-
-    int count = libvlc_media_list_count(libvlc_media_list);
-    if( idx<0||idx>=count )
-        goto bad_unlock;
-
-    playlist_index = idx;
-
-    p_m = libvlc_media_list_item_at_index(libvlc_media_list,playlist_index);
-    libvlc_media_list_unlock(libvlc_media_list);
-
-    if( !p_m )
-        return false;
-
-    if( libvlc_media_player )
-    {
-        if( playlist_isplaying() )
-            playlist_stop();
-        events.unhook_manager( this );
-        on_media_player_release();
-        libvlc_media_player_release( libvlc_media_player );
-        libvlc_media_player = NULL;
-    }
-
-    libvlc_media_player = libvlc_media_player_new_from_media( p_m );
-    if( libvlc_media_player )
-    {
-        on_media_player_new();
-        set_player_window();
-
-        libvlc_event_manager_t *p_em;
-        p_em = libvlc_media_player_event_manager( libvlc_media_player );
-        events.hook_manager( p_em, this );
-    }
-
-    libvlc_media_release( p_m );
-    return true;
-
-bad_unlock:
-    libvlc_media_list_unlock( libvlc_media_list );
-    return false;
-}
-
-int VlcPluginBase::playlist_delete_item( int idx )
-{
-    if( !libvlc_media_list )
-        return -1;
-    libvlc_media_list_lock(libvlc_media_list);
-    int ret = libvlc_media_list_remove_index(libvlc_media_list,idx);
-    libvlc_media_list_unlock(libvlc_media_list);
-    return ret;
-}
-
-void VlcPluginBase::playlist_clear()
-{
-    if( libvlc_media_list )
-        libvlc_media_list_release(libvlc_media_list);
-    libvlc_media_list = libvlc_media_list_new(getVLC());
-}
-
-int VlcPluginBase::playlist_count()
-{
-    int items_count = 0;
-    if( !libvlc_media_list )
-        return items_count;
-    libvlc_media_list_lock(libvlc_media_list);
-    items_count = libvlc_media_list_count(libvlc_media_list);
-    libvlc_media_list_unlock(libvlc_media_list);
-    return items_count;
-}
-
-
 bool  VlcPluginBase::player_has_vout()
 {
     bool r = false;
     if( playlist_isplaying() )
-        r = libvlc_media_player_has_vout(libvlc_media_player);
+        r = libvlc_media_player_has_vout(get_player().get_mp())!=0;
     return r;
 }
 
diff --git a/npapi/vlcplugin_base.h b/npapi/vlcplugin_base.h
index 56b324e..c5daa25 100644
--- a/npapi/vlcplugin_base.h
+++ b/npapi/vlcplugin_base.h
@@ -82,6 +82,7 @@
 
 #include "control/nporuntime.h"
 #include "../common/vlc_player_options.h"
+#include "../common/vlc_player.h"
 
 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
     typedef uint16 NPuint16_t;
@@ -190,7 +191,7 @@ typedef enum vlc_toolbar_clicked_e {
     clicked_Unmute
 } vlc_toolbar_clicked_t;
 
-class VlcPluginBase: private vlc_player_options
+class VlcPluginBase: private vlc_player_options, private vlc_player
 {
 protected:
 
@@ -198,6 +199,11 @@ public:
     VlcPluginBase( NPP, NPuint16_t );
     virtual ~VlcPluginBase();
 
+    vlc_player& get_player()
+    {
+        return *static_cast<vlc_player*>(this);
+    }
+
     vlc_player_options& get_options()
         { return *static_cast<vlc_player_options*>(this); }
     const vlc_player_options& get_options() const
@@ -208,11 +214,11 @@ public:
                             { return libvlc_instance; };
     libvlc_media_player_t* getMD()
     {
-        if( !libvlc_media_player )
+        if( !get_player().is_open() )
         {
              libvlc_printerr("no mediaplayer");
         }
-        return libvlc_media_player;
+        return get_player().get_mp();
     }
     NPP                 getBrowser()
                             { return p_browser; };
@@ -232,51 +238,53 @@ public:
 
     void playlist_play()
     {
-        if( playlist_isplaying() )
-            playlist_stop();
-        if( libvlc_media_player||playlist_select(0) )
-            libvlc_media_player_play(libvlc_media_player);
+        get_player().play();
     }
     void playlist_play_item(int idx)
     {
-        if( playlist_select(idx) )
-            libvlc_media_player_play(libvlc_media_player);
+        get_player().play(idx);
     }
     void playlist_stop()
     {
-        if( libvlc_media_player )
-            libvlc_media_player_stop(libvlc_media_player);
+        get_player().stop();
     }
     void playlist_next()
     {
-        if( playlist_select(playlist_index+1) )
-            libvlc_media_player_play(libvlc_media_player);
+        get_player().next();
     }
     void playlist_prev()
     {
-        if( playlist_select(playlist_index-1) )
-            libvlc_media_player_play(libvlc_media_player);
+        get_player().prev();
     }
     void playlist_pause()
     {
-        if( libvlc_media_player )
-            libvlc_media_player_pause(libvlc_media_player);
+        get_player().pause();
     }
     int playlist_isplaying()
     {
-        int is_playing = 0;
-        if( libvlc_media_player )
-            is_playing = libvlc_media_player_is_playing(
-                                libvlc_media_player );
-        return is_playing;
+        return get_player().is_playing();
+    }
+    int playlist_add( const char * mrl)
+    {
+        return get_player().add_item(mrl);
+    }
+    int playlist_add_extended_untrusted( const char *mrl, const char *,
+                    int optc, const char **optv )
+    {
+        return get_player().add_item(mrl, optc, optv);
+    }
+    int playlist_delete_item( int idx)
+    {
+        return get_player().delete_item(idx);
+    }
+    void playlist_clear()
+    {
+        get_player().clear_items() ;
+    }
+    int  playlist_count()
+    {
+        return get_player().items_count();
     }
-
-    int playlist_add( const char * );
-    int playlist_add_extended_untrusted( const char *, const char *, int,
-                                const char ** );
-    int playlist_delete_item( int );
-    void playlist_clear();
-    int  playlist_count();
 
     void control_handler(vlc_toolbar_clicked_t);
 
@@ -311,10 +319,7 @@ protected:
     virtual void set_player_window() = 0;
 
     /* VLC reference */
-    int                 playlist_index;
     libvlc_instance_t   *libvlc_instance;
-    libvlc_media_list_t *libvlc_media_list;
-    libvlc_media_player_t *libvlc_media_player;
     NPClass             *p_scriptClass;
 
     /* browser reference */
diff --git a/npapi/vlcplugin_win.cpp b/npapi/vlcplugin_win.cpp
index e0527d8..cb78b84 100644
--- a/npapi/vlcplugin_win.cpp
+++ b/npapi/vlcplugin_win.cpp
@@ -184,6 +184,9 @@ bool VlcPluginWin::create_windows()
 
     _WindowsManager.CreateWindows(drawable);
 
+    if( get_player().is_open() )
+        _WindowsManager.LibVlcAttach(get_player().get_mp());
+
     return true;
 }
 
@@ -217,7 +220,7 @@ bool VlcPluginWin::destroy_windows()
 
 void VlcPluginWin::on_media_player_new()
 {
-    _WindowsManager.LibVlcAttach(libvlc_media_player);
+    _WindowsManager.LibVlcAttach(get_player().get_mp());
 }
 
 void VlcPluginWin::on_media_player_release()
-- 
1.7.7.1.msysgit.0




More information about the vlc-devel mailing list