Commit 909b96ec authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

npapi: Start replacing libvlc by libvlcpp

parent 7cf66b81
AM_CPPFLAGS = $(LIBVLC_CFLAGS)
AM_CPPFLAGS = $(LIBVLC_CFLAGS) -I$(top_srcdir)/vlcpp
libvlcplugin_common_la_SOURCES = \
position.h \
......
/*****************************************************************************
* Copyright 2002-2011 VideoLAN and VLC authors
* Copyright 2002-2011 VideoLAN and VLC authors
* $Id$
*
* Authors: Sergey Radionov <rsatom_gmail.com>
......@@ -22,35 +22,25 @@
#include "vlc_player.h"
vlc_player::vlc_player()
:_libvlc_instance(0), _mp(0), _ml(0), _ml_p(0)
{
}
vlc_player::~vlc_player(void)
{
close();
}
bool vlc_player::open(libvlc_instance_t* inst)
bool vlc_player::open(VLC::Instance& inst)
{
if( !inst )
return false;
if( is_open() )
close();
_libvlc_instance = inst;
_mp = libvlc_media_player_new(inst);
_ml = libvlc_media_list_new(inst);
_ml_p = libvlc_media_list_player_new(inst);
try {
_mp = VLC::MediaPlayer(inst);
_ml = VLC::MediaList(inst);
_ml_p = VLC::MediaListPlayer(inst);
if( _mp && _ml && _ml_p ) {
libvlc_media_list_player_set_media_list(_ml_p, _ml);
libvlc_media_list_player_set_media_player(_ml_p, _mp);
_ml_p.setMediaList( _ml );
_ml_p.setMediaPlayer( _mp );
}
else{
close();
catch (std::runtime_error&) {
return false;
}
......@@ -59,7 +49,7 @@ bool vlc_player::open(libvlc_instance_t* inst)
bool vlc_player::is_playing()
{
return _ml_p && libvlc_media_list_player_is_playing(_ml_p) != 0;
return _ml_p && _ml_p.isPlaying() != 0;
}
libvlc_state_t vlc_player::get_state()
......@@ -67,27 +57,7 @@ libvlc_state_t vlc_player::get_state()
if( !is_open() )
return libvlc_NothingSpecial;
return libvlc_media_list_player_get_state(_ml_p);
}
void vlc_player::close()
{
if(_ml_p) {
libvlc_media_list_player_release(_ml_p);
_ml_p = 0;
}
if(_ml) {
libvlc_media_list_release(_ml);
_ml = 0;
}
if(_mp) {
libvlc_media_player_release(_mp);
_mp = 0;
}
_libvlc_instance = 0;
return _ml_p.state();
}
int vlc_player::add_item(const char * mrl, unsigned int optc, const char **optv)
......@@ -95,23 +65,21 @@ int vlc_player::add_item(const char * mrl, unsigned int optc, const char **optv)
if( !is_open() )
return -1;
int item = -1;
libvlc_media_t* media = libvlc_media_new_location(_libvlc_instance, mrl);
if( !media )
VLC::Media media;
try {
media = VLC::Media( _libvlc_instance, mrl, VLC::Media::FromLocation );
}
catch ( std::runtime_error& ) {
return -1;
}
for( unsigned int i = 0; i < optc; ++i )
libvlc_media_add_option_flag(media, optv[i], libvlc_media_option_unique);
libvlc_media_list_lock(_ml);
if( 0 == libvlc_media_list_add_media(_ml, media) )
item = libvlc_media_list_count(_ml) - 1;
libvlc_media_list_unlock(_ml);
media.addOptionFlag( optv[i], libvlc_media_option_unique );
libvlc_media_release(media);
return item;
VLC::MediaList::Lock lock( _ml );
if( _ml.addMedia( media ) )
return _ml.count() - 1;
return -1;
}
int vlc_player::current_item()
......@@ -119,12 +87,11 @@ int vlc_player::current_item()
if( !is_open() )
return -1;
libvlc_media_t* media = libvlc_media_player_get_media(_mp);
auto media = _mp.media();
if( !media )
return -1;
return libvlc_media_list_index_of_item(_ml, media);
return _ml.indexOfItem( *media );
}
int vlc_player::items_count()
......@@ -132,10 +99,8 @@ int vlc_player::items_count()
if( !is_open() )
return 0;
libvlc_media_list_lock(_ml);
int icnt = libvlc_media_list_count(_ml);
libvlc_media_list_unlock(_ml);
return icnt;
VLC::MediaList::Lock lock( _ml );
return _ml.count();
}
bool vlc_player::delete_item(unsigned int idx)
......@@ -143,11 +108,8 @@ bool vlc_player::delete_item(unsigned int idx)
if( !is_open() )
return false;
libvlc_media_list_lock(_ml);
bool ret = libvlc_media_list_remove_index(_ml, idx) == 0;
libvlc_media_list_unlock(_ml);
return ret;
VLC::MediaList::Lock lock( _ml );
return _ml.removeIndex( idx );
}
void vlc_player::clear_items()
......@@ -155,11 +117,10 @@ void vlc_player::clear_items()
if( !is_open() )
return;
libvlc_media_list_lock(_ml);
for( int i = libvlc_media_list_count(_ml); i > 0; --i) {
libvlc_media_list_remove_index(_ml, i - 1);
VLC::MediaList::Lock lock( _ml );
for( int i = _ml.count(); i > 0; --i) {
_ml.removeIndex( i - 1 );
}
libvlc_media_list_unlock(_ml);
}
void vlc_player::play()
......@@ -173,7 +134,7 @@ void vlc_player::play()
play(0);
}
else {
libvlc_media_list_player_play(_ml_p);
_ml_p.play();
on_player_action(pa_play);
}
}
......@@ -183,19 +144,17 @@ bool vlc_player::play(unsigned int idx)
if( !is_open() )
return false;
const int r = libvlc_media_list_player_play_item_at_index(_ml_p, idx);
if( 0 == r ) {
if( _ml_p.playItemAtIndex( idx ) ) {
on_player_action(pa_play);
return true;
}
return false;
}
void vlc_player::pause()
{
if( is_open() ) {
libvlc_media_player_set_pause(_mp, true);
_mp.setPause( true );
on_player_action(pa_pause);
}
}
......@@ -203,7 +162,7 @@ void vlc_player::pause()
void vlc_player::togglePause()
{
if( is_open() ) {
libvlc_media_list_player_pause(_ml_p);
_ml_p.pause();
on_player_action(pa_pause);
}
}
......@@ -221,12 +180,10 @@ bool vlc_player::next()
if( !is_open() )
return false;
const int r = libvlc_media_list_player_next(_ml_p);
if( 0 == r ) {
if( _ml_p.next() ) {
on_player_action(pa_next);
return true;
}
return false;
}
......@@ -235,12 +192,11 @@ bool vlc_player::prev()
if( !is_open() )
return false;
const int r = libvlc_media_list_player_previous(_ml_p);
if( 0 == r ) {
if( _ml_p.previous() ) {
on_player_action(pa_prev);
return true;
}
return false;
}
......@@ -249,7 +205,7 @@ float vlc_player::get_rate()
if( !is_open() )
return 1.f;
return libvlc_media_player_get_rate(_mp);
return _mp.rate();
}
void vlc_player::set_rate(float rate)
......@@ -257,7 +213,7 @@ void vlc_player::set_rate(float rate)
if( !is_open() )
return;
libvlc_media_player_set_rate(_mp, rate);
_mp.setRate( rate );
}
float vlc_player::get_fps()
......@@ -265,7 +221,7 @@ float vlc_player::get_fps()
if( !is_open() )
return 0;
return libvlc_media_player_get_fps(_mp);
return _mp.fps();
}
bool vlc_player::has_vout()
......@@ -273,7 +229,7 @@ bool vlc_player::has_vout()
if( !is_open() )
return false;
return libvlc_media_player_has_vout(_mp) > 0;
return _mp.hasVout() > 0;
}
float vlc_player::get_position()
......@@ -281,9 +237,8 @@ float vlc_player::get_position()
if( !is_open() )
return 0.f;
float p = libvlc_media_player_get_position(_mp);
return p<0 ? 0 : p;
float p = _mp.position();
return p < 0 ? 0.f : p;
}
void vlc_player::set_position(float p)
......@@ -291,7 +246,7 @@ void vlc_player::set_position(float p)
if( !is_open() )
return;
libvlc_media_player_set_position(_mp, p);
_mp.setPosition( p );
}
libvlc_time_t vlc_player::get_time()
......@@ -299,9 +254,9 @@ libvlc_time_t vlc_player::get_time()
if( !is_open() )
return 0;
libvlc_time_t t = libvlc_media_player_get_time(_mp);
auto t = _mp.time();
return t<0 ? 0 : t ;
return t < 0 ? 0 : t;
}
void vlc_player::set_time(libvlc_time_t t)
......@@ -309,7 +264,7 @@ void vlc_player::set_time(libvlc_time_t t)
if( !is_open() )
return;
libvlc_media_player_set_time(_mp, t);
_mp.setTime( t );
}
libvlc_time_t vlc_player::get_length()
......@@ -317,15 +272,14 @@ libvlc_time_t vlc_player::get_length()
if( !is_open() )
return 0;
libvlc_time_t t = libvlc_media_player_get_length(_mp);
return t<0 ? 0 : t ;
auto t = _mp.length();
return t < 0 ? 0 : t;
}
void vlc_player::set_mode(libvlc_playback_mode_t mode)
{
if( is_open() )
libvlc_media_list_player_set_playback_mode(_ml_p, mode);
_ml_p.setPlaybackMode( mode );
}
bool vlc_player::is_muted()
......@@ -333,19 +287,19 @@ bool vlc_player::is_muted()
if( !is_open() )
return false;
return libvlc_audio_get_mute(_mp) != 0;
return _mp.mute();
}
void vlc_player::toggle_mute()
{
if( is_open() )
libvlc_audio_toggle_mute(_mp);
_mp.toggleMute();
}
void vlc_player::set_mute(bool mute)
{
if( is_open() )
libvlc_audio_set_mute(_mp, mute);
_mp.setMute( mute );
}
unsigned int vlc_player::get_volume()
......@@ -353,15 +307,14 @@ unsigned int vlc_player::get_volume()
if( !is_open() )
return 0;
int v = libvlc_audio_get_volume(_mp);
return v<0 ? 0 : v;
int v = _mp.volume();
return v < 0 ? 0 : v;
}
void vlc_player::set_volume(unsigned int volume)
{
if( is_open() )
libvlc_audio_set_volume(_mp, volume);
_mp.setVolume( volume );
}
unsigned int vlc_player::track_count()
......@@ -369,7 +322,7 @@ unsigned int vlc_player::track_count()
if( !is_open() )
return 0;
int tc = libvlc_audio_get_track_count(_mp);
int tc = _mp.audioTrackCount();
return tc<0 ? 0 : tc ;
}
......@@ -381,13 +334,13 @@ unsigned int vlc_player::get_track()
int t = libvlc_audio_get_track(_mp);
return t<0 ? 0 : t ;
return t < 0 ? 0 : t;
}
void vlc_player::set_track(unsigned int track)
{
if( is_open() )
libvlc_audio_set_track(_mp, track);
_mp.setAudioTrack( track );
}
unsigned int vlc_player::get_channel()
......@@ -395,13 +348,11 @@ unsigned int vlc_player::get_channel()
if( !is_open() )
return 0;
int c =libvlc_audio_get_channel(_mp);
return c<0 ? 0 : c ;
return _mp.channel();
}
void vlc_player::set_channel(unsigned int channel)
{
if( is_open() )
libvlc_audio_set_channel(_mp, channel);
_mp.setChannel( channel );
}
......@@ -21,7 +21,8 @@
#pragma once
#include <vlc/vlc.h>
#include <vlcpp/vlc.hpp>
#include <memory>
enum vlc_player_action_e
{
......@@ -36,12 +37,10 @@ class vlc_player
{
public:
vlc_player();
~vlc_player(void);
bool open(libvlc_instance_t* inst);
void close();
bool open(VLC::Instance& inst);
bool is_open() const { return _ml_p != 0; }
bool is_open() const { return _ml_p.isValid(); }
bool is_playing();
libvlc_state_t get_state();
bool is_stopped() { return libvlc_Stopped == get_state(); }
......@@ -95,15 +94,15 @@ public:
unsigned int get_channel();
void set_channel(unsigned int);
libvlc_media_player_t* get_mp() const
VLC::MediaPlayer& get_mp()
{ return _mp; }
protected:
virtual void on_player_action( vlc_player_action_e ){};
virtual void on_player_action( vlc_player_action_e ){}
private:
libvlc_instance_t * _libvlc_instance;
libvlc_media_player_t* _mp;
libvlc_media_list_t* _ml;
libvlc_media_list_player_t* _ml_p;
VLC::Instance _libvlc_instance;
VLC::MediaPlayer _mp;
VLC::MediaList _ml;
VLC::MediaListPlayer _ml_p;
};
......@@ -13,7 +13,7 @@ CLEANFILES = $(BUILT_SOURCES) \
npvlcdir = $(libdir)/mozilla/plugins
AM_CPPFLAGS = $(LIBVLC_CFLAGS) -Inpapi-sdk $(MOZILLA_CFLAGS)
AM_CPPFLAGS = $(LIBVLC_CFLAGS) -Inpapi-sdk $(MOZILLA_CFLAGS) -I$(top_srcdir)/vlcpp
libvlcplugin_la_SOURCES = \
common.h \
......
......@@ -260,60 +260,56 @@ LibvlcAudioNPObject::getProperty(int index, NPVariant &result)
{
VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
libvlc_media_player_t *p_md = p_plugin->getMD();
if( !p_md )
auto& mp = p_plugin->getMD();
if( !mp )
RETURN_ON_ERROR;
switch( index )
{
case ID_audio_mute:
{
bool muted = libvlc_audio_get_mute(p_md);
bool muted = mp.mute();
BOOLEAN_TO_NPVARIANT(muted, result);
return INVOKERESULT_NO_ERROR;
}
case ID_audio_volume:
{
int volume = libvlc_audio_get_volume(p_md);
int volume = mp.volume();
INT32_TO_NPVARIANT(volume, result);
return INVOKERESULT_NO_ERROR;
}
case ID_audio_track:
{
/* get the current internal audio track ID */
int actualTrack = libvlc_audio_get_track(p_md);
int actualTrack = mp.audioTrack();
int audioTrackCount = libvlc_audio_get_track_count(p_md);
int audioTrackCount = mp.audioTrackCount();
if (audioTrackCount < 0) {
INT32_TO_NPVARIANT(actualTrack, result);
return INVOKERESULT_NO_ERROR;
}
libvlc_track_description_t *currentTrack = libvlc_audio_get_track_description(p_md);
int fakeTrackIndex = 0;
while (currentTrack) {
if (actualTrack == currentTrack->i_id)
break;
currentTrack = currentTrack->p_next;
fakeTrackIndex++;
}
libvlc_track_description_list_release(currentTrack);
INT32_TO_NPVARIANT(fakeTrackIndex, result);
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);
return INVOKERESULT_NO_ERROR;
}
case ID_audio_count:
{
// get the number of audio track available
int i_track = libvlc_audio_get_track_count(p_md);
int i_track = mp.audioTrackCount();
// return it
INT32_TO_NPVARIANT(i_track, result);
return INVOKERESULT_NO_ERROR;
}
case ID_audio_channel:
{
int channel = libvlc_audio_get_channel(p_md);
int channel = mp.channel();
INT32_TO_NPVARIANT(channel, result);
return INVOKERESULT_NO_ERROR;
}
......@@ -332,8 +328,8 @@ LibvlcAudioNPObject::setProperty(int index, const NPVariant &value)
{
VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
libvlc_media_player_t *p_md = p_plugin->getMD();
if( !p_md )
auto& mp = p_plugin->getMD();
if( !mp )
RETURN_ON_ERROR;
switch( index )
......@@ -341,53 +337,30 @@ LibvlcAudioNPObject::setProperty(int index, const NPVariant &value)
case ID_audio_mute:
if( isBoolValue(value) )
{
libvlc_audio_set_mute(p_md,
boolValue(value));
mp.setMute( boolValue( value ) );
return INVOKERESULT_NO_ERROR;
}
return INVOKERESULT_INVALID_VALUE;
case ID_audio_volume:
if( isNumberValue(value) )
{
libvlc_audio_set_volume(p_md, intValue(value));
mp.setVolume( intValue( value ) );
return INVOKERESULT_NO_ERROR;
}
return INVOKERESULT_INVALID_VALUE;
case ID_audio_track:
if( isNumberValue(value) )
{
int fakeTrackIndex = intValue(value);
/* bounds checking */
int count = libvlc_audio_get_track_count(p_md);
if (fakeTrackIndex >= count || count == 0)
return INVOKERESULT_INVALID_VALUE;
libvlc_track_description_t *currentTrack = libvlc_audio_get_track_description(p_md);
for (unsigned int x = 0; x < fakeTrackIndex+1; x++) {
if (x == fakeTrackIndex)
break;
if (currentTrack->p_next)
currentTrack = currentTrack->p_next;
else {
libvlc_track_description_list_release(currentTrack);
return INVOKERESULT_INVALID_VALUE;
}
}
int actualTrack = currentTrack->i_id;
libvlc_track_description_list_release(currentTrack);
libvlc_audio_set_track(p_md, actualTrack);
return INVOKERESULT_NO_ERROR;
int trackIdx = intValue(value);
if ( mp.setAudioTrack( trackIdx ) )
return INVOKERESULT_NO_ERROR;
}
return INVOKERESULT_INVALID_VALUE;
case ID_audio_channel:
if( isNumberValue(value) )
{
libvlc_audio_set_channel(p_md, intValue(value));
return INVOKERESULT_NO_ERROR;
if ( mp.setChannel( intValue( value ) ) )
return INVOKERESULT_NO_ERROR;
}
return INVOKERESULT_INVALID_VALUE;
default:
......@@ -418,8 +391,8 @@ LibvlcAudioNPObject::invoke(int index, const NPVariant *args,
if( isPluginRunning() )
{
VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
libvlc_media_player_t *p_md = p_plugin->getMD();
if( !p_md )
auto& mp = p_plugin->getMD();
if( !mp )
RETURN_ON_ERROR;
switch( index )
......@@ -427,7 +400,7 @@ LibvlcAudioNPObject::invoke(int index, const NPVariant *args,
case ID_audio_togglemute:
if( argCount == 0 )
{
libvlc_audio_toggle_mute(p_md);
mp.toggleMute();
VOID_TO_NPVARIANT(result);
return INVOKERESULT_NO_ERROR;
}
......@@ -437,29 +410,15 @@ LibvlcAudioNPObject::invoke(int index, const NPVariant *args,
if( argCount == 1 && isNumberValue(args[0]))
{
int fakeTrackIndex = intValue(args[0]);
char *psz_name;
/* bounds checking */
int count = libvlc_audio_get_track_count(p_md);
if (fakeTrackIndex >= count || count == 0 || fakeTrackIndex < 0)
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 ) )
return INVOKERESULT_INVALID_VALUE;
libvlc_track_description_t *currentTrack = libvlc_audio_get_track_description(p_md);
for (unsigned int x = 0; x < fakeTrackIndex+1; x++) {
if (x == fakeTrackIndex)
break;
currentTrack = currentTrack->p_next;
}
psz_name = strdup(currentTrack->psz_name);
libvlc_track_description_list_release(currentTrack);
/* display the name of the track chosen */
if (psz_name != NULL)
return invokeResultString( psz_name, result );
else
return INVOKERESULT_GENERIC_ERROR;
return invokeResultString( (*track).name().c_str(), result );
}
return INVOKERESULT_NO_SUCH_METHOD;
}
......@@ -504,8 +463,8 @@ LibvlcInputNPObject::getProperty(int index, NPVariant &result)
if( isPluginRunning() )
{
VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
libvlc_media_player_t *p_md = p_plugin->getMD();
if( !p_md )
auto& mp = p_plugin->getMD();
if( !mp )
{
if( index != ID_input_state )
RETURN_ON_ERROR;
......@@ -521,37 +480,37 @@ LibvlcInputNPObject::getProperty(int index, NPVariant &result)
{
case ID_input_length:
{
double val = (double)libvlc_media_player_get_length(p_md);
double val = mp.length();
DOUBLE_TO_NPVARIANT(val, result);
return INVOKERESULT_NO_ERROR;
}
case ID_input_position:
{
double val = libvlc_media_player_get_position(p_md);
double val = mp.position();
DOUBLE_TO_NPVARIANT(val, result);
return INVOKERESULT_NO_ERROR;
}
case ID_input_time:
{
double val = (double)libvlc_media_player_get_time(p_md);
double val = (double)mp.time();
DOUBLE_TO_NPVARIANT(val, result);
return INVOKERESULT_NO_ERROR;
}
case ID_input_state:
{
int val = libvlc_media_player_get_state(p_md);
int val = mp.state();
INT32_TO_NPVARIANT(val, result);
return INVOKERESULT_NO_ERROR;
}
case ID_input_rate:
{
float val = libvlc_media_player_get_rate(p_md);
float val = mp.rate();
DOUBLE_TO_NPVARIANT(val, result);
return INVOKERESULT_NO_ERROR;
}
case ID_input_fps:
{
double val = libvlc_media_player_get_fps(p_md);
double val = mp.fps();
DOUBLE_TO_NPVARIANT(val, result);
return INVOKERESULT_NO_ERROR;
}
......@@ -575,8 +534,8 @@ LibvlcInputNPObject::setProperty(int index, const NPVariant &value)
if( isPluginRunning() )
{
VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
libvlc_media_player_t *p_md = p_plugin->getMD();
if( !p_md )
auto& mp = p_plugin->getMD();