[vlc-commits] npapi: Fix track operations

Hugo Beauzée-Luyssen git at videolan.org
Tue Apr 21 10:15:55 CEST 2015


npapi-vlc | branch: master | Hugo Beauzée-Luyssen <hugo at beauzee.fr> | Wed Mar 25 16:40:13 2015 +0100| [3c08e147fc1ea205896748c56139f48e314eed7c] | committer: Hugo Beauzée-Luyssen

npapi: Fix track operations

> http://git.videolan.org/gitweb.cgi/npapi-vlc.git/?a=commit;h=3c08e147fc1ea205896748c56139f48e314eed7c
---

 common/vlc_player.cpp         |   10 ++------
 npapi/npruntime/npolibvlc.cpp |   52 +++++++++++++----------------------------
 npapi/npruntime/nporuntime.h  |    2 +-
 3 files changed, 19 insertions(+), 45 deletions(-)

diff --git a/common/vlc_player.cpp b/common/vlc_player.cpp
index 984b0b4..101b343 100644
--- a/common/vlc_player.cpp
+++ b/common/vlc_player.cpp
@@ -104,26 +104,20 @@ void vlc_player::play()
 
 int vlc_player::currentAudioTrack()
 {
-    auto current = _mp.spu();
-    if ( current < 0 )
-        return -1;
-    auto tracks = _mp.spuDescription();
+    auto current = _mp.audioTrack();
+    auto tracks = _mp.audioTrackDescription();
     return getTrack( current, tracks );
 }
 
 int vlc_player::currentSubtitleTrack()
 {
     auto current = _mp.spu();
-    if ( current < 0 )
-        return -1;
     auto tracks = _mp.spuDescription();
     return getTrack( current, tracks );
 }
 
 int vlc_player::getTrack( int currentId, const std::vector<VLC::TrackDescription>& tracks )
 {
-    if ( currentId < 0 )
-        return -1;
     if ( tracks.empty() )
         return -1;
 
diff --git a/npapi/npruntime/npolibvlc.cpp b/npapi/npruntime/npolibvlc.cpp
index a22de5c..a16de9a 100644
--- a/npapi/npruntime/npolibvlc.cpp
+++ b/npapi/npruntime/npolibvlc.cpp
@@ -252,23 +252,8 @@ LibvlcAudioNPObject::getProperty(int index, NPVariant &result)
             }
             case ID_audio_track:
             {
-                /* get the current internal audio track ID */
-                int actualTrack = mp.audioTrack();
-
-                int audioTrackCount = mp.audioTrackCount();
-                if (audioTrackCount < 0) {
-                    INT32_TO_NPVARIANT(actualTrack, result);
-                    return INVOKERESULT_NO_ERROR;
-                }
-
-                auto tracks = mp.audioTrackDescription();
-                auto t = std::find_if(begin(tracks), end(tracks), [actualTrack](const VLC::TrackDescription& td) {
-                    return td.id() == actualTrack;
-                });
-                if ( t == end( tracks ) )
-                    INT32_TO_NPVARIANT(tracks.size(), result);
-                else
-                    INT32_TO_NPVARIANT(actualTrack, result);
+                int track = p_plugin->player().currentAudioTrack();
+                INT32_TO_NPVARIANT(track, result);
                 return INVOKERESULT_NO_ERROR;
             }
             case ID_audio_count:
@@ -324,8 +309,10 @@ LibvlcAudioNPObject::setProperty(int index, const NPVariant &value)
             case ID_audio_track:
                 if( v.is<int>() )
                 {
-                    int trackIdx = v;
-                    if ( mp.setAudioTrack( trackIdx ) )
+                    auto tracks = mp.audioTrackDescription();
+                    if ( v >= tracks.size() )
+                        return INVOKERESULT_INVALID_VALUE;
+                    if ( mp.setAudioTrack( tracks[v].id() ) )
                         return INVOKERESULT_NO_ERROR;
                 }
                 return INVOKERESULT_INVALID_VALUE;
@@ -383,18 +370,13 @@ LibvlcAudioNPObject::invoke(int index, const NPVariant *args,
                 if ( argCount < 1 )
                     return INVOKERESULT_INVALID_ARGS;
                 auto v = npapi::Variant( args[0] );
-                if( !v.is<int>() )
+                if( v.is<int>() )
                 {
-                    int fakeTrackIndex = v;
                     auto tracks = mp.audioTrackDescription();
-                    auto track = std::find_if( begin( tracks ), end( tracks ), [fakeTrackIndex](const VLC::TrackDescription& t) {
-                        return t.id() == fakeTrackIndex;
-                    });
-                    if (track == end( tracks ) )
+                    if ( v >= tracks.size() )
                         return INVOKERESULT_INVALID_VALUE;
-
                     /* display the name of the track chosen */
-                    return invokeResultString( (*track).name().c_str(), result );
+                    return invokeResultString( tracks[v].name().c_str(), result );
                 }
                 return INVOKERESULT_NO_SUCH_METHOD;
             }
@@ -1217,7 +1199,7 @@ LibvlcSubtitleNPObject::getProperty(int index, NPVariant &result)
             case ID_subtitle_track:
             {
                 /* get the current internal subtitles track ID */
-                int actualTrack = mp.spu();
+                int actualTrack = p_plugin->player().currentSubtitleTrack();
                 INT32_TO_NPVARIANT(actualTrack, result);
                 return INVOKERESULT_NO_ERROR;
             }
@@ -1252,7 +1234,10 @@ LibvlcSubtitleNPObject::setProperty(int index, const NPVariant &value)
                 auto v = npapi::Variant( value );
                 if( v.is<int>() )
                 {
-                    if ( mp.setSpu( v ) )
+                    auto tracks = mp.spuDescription();
+                    if ( v >= tracks.size() )
+                        return INVOKERESULT_INVALID_ARGS;
+                    if ( mp.setSpu( tracks[ v ].id() ) )
                         return INVOKERESULT_NO_ERROR;
                 }
                 return INVOKERESULT_INVALID_VALUE;
@@ -1294,16 +1279,11 @@ LibvlcSubtitleNPObject::invoke(int index, const NPVariant *args,
                 auto v = npapi::Variant( args[0] );
                 if ( v.is<int>() )
                 {
-                    int fakeTrackIndex = v;
                     auto tracks = mp.spuDescription();
-                    auto track = std::find_if( begin( tracks ), end( tracks ), [fakeTrackIndex](const VLC::TrackDescription& t) {
-                        return t.id() == fakeTrackIndex;
-                    });
-                    if (track == end( tracks ) )
+                    if ( v >= tracks.size() )
                         return INVOKERESULT_INVALID_VALUE;
-
                     /* display the name of the track chosen */
-                    return invokeResultString( (*track).name().c_str(), result );
+                    return invokeResultString( tracks[v].name().c_str(), result );
                 }
                 return INVOKERESULT_NO_SUCH_METHOD;
             }
diff --git a/npapi/npruntime/nporuntime.h b/npapi/npruntime/nporuntime.h
index f56b4a4..969a549 100644
--- a/npapi/npruntime/nporuntime.h
+++ b/npapi/npruntime/nporuntime.h
@@ -97,7 +97,7 @@ protected:
         _class = const_cast<NPClass *>(aClass);
         referenceCount = 1;
     };
-    virtual ~RuntimeNPObject() {};
+    virtual ~RuntimeNPObject() = default;
 
     friend void RuntimeNPClassDeallocate(NPObject *npobj);
     friend void RuntimeNPClassInvalidate(NPObject *npobj);



More information about the vlc-commits mailing list