📄 npolibvlc.cpp
字号:
/*****************************************************************************
* npolibvlc.cpp: official Javascript APIs
*****************************************************************************
* Copyright (C) 2002-2006 the VideoLAN team
*
* Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Mozilla stuff */
#ifdef HAVE_MOZILLA_CONFIG_H
# include <mozilla-config.h>
#endif
#include "npolibvlc.h"
#include "vlcplugin.h"
/*
** implementation of libvlc root object
*/
LibvlcRootNPObject::~LibvlcRootNPObject()
{
/*
** when plugin is destroyed, firefox takes upon itself to destroy all 'live' script objects
** and ignores refcounting. Therefore we cannot safely assume that refcounting will control
** lifespan of objects. Hence they are only lazily created on request, so that firefox can
** take ownership, and are not released when plugin is being destroyed.
*/
if( isValid() )
{
if( audioObj ) NPN_ReleaseObject(audioObj);
if( inputObj ) NPN_ReleaseObject(inputObj);
if( logObj ) NPN_ReleaseObject(logObj);
if( playlistObj ) NPN_ReleaseObject(playlistObj);
if( videoObj ) NPN_ReleaseObject(videoObj);
}
}
const NPUTF8 * const LibvlcRootNPObject::propertyNames[] =
{
"audio",
"input",
"log",
"playlist",
"video",
"VersionInfo",
};
const int LibvlcRootNPObject::propertyCount = sizeof(LibvlcRootNPObject::propertyNames)/sizeof(NPUTF8 *);
enum LibvlcRootNPObjectPropertyIds
{
ID_root_audio = 0,
ID_root_input,
ID_root_log,
ID_root_playlist,
ID_root_video,
ID_root_VersionInfo,
};
RuntimeNPObject::InvokeResult LibvlcRootNPObject::getProperty(int index, NPVariant &result)
{
/* is plugin still running */
if( _instance->pdata )
{
switch( index )
{
case ID_root_audio:
// create child object in lazyman fashion to avoid ownership problem with firefox
if( ! audioObj )
audioObj = NPN_CreateObject(_instance, RuntimeNPClass<LibvlcAudioNPObject>::getClass());
OBJECT_TO_NPVARIANT(NPN_RetainObject(audioObj), result);
return INVOKERESULT_NO_ERROR;
case ID_root_input:
// create child object in lazyman fashion to avoid ownership problem with firefox
if( ! inputObj )
inputObj = NPN_CreateObject(_instance, RuntimeNPClass<LibvlcInputNPObject>::getClass());
OBJECT_TO_NPVARIANT(NPN_RetainObject(inputObj), result);
return INVOKERESULT_NO_ERROR;
case ID_root_log:
// create child object in lazyman fashion to avoid ownership problem with firefox
if( ! logObj )
logObj = NPN_CreateObject(_instance, RuntimeNPClass<LibvlcLogNPObject>::getClass());
OBJECT_TO_NPVARIANT(NPN_RetainObject(logObj), result);
return INVOKERESULT_NO_ERROR;
case ID_root_playlist:
// create child object in lazyman fashion to avoid ownership problem with firefox
if( ! playlistObj )
playlistObj = NPN_CreateObject(_instance, RuntimeNPClass<LibvlcPlaylistNPObject>::getClass());
OBJECT_TO_NPVARIANT(NPN_RetainObject(playlistObj), result);
return INVOKERESULT_NO_ERROR;
case ID_root_video:
// create child object in lazyman fashion to avoid ownership problem with firefox
if( ! videoObj )
videoObj = NPN_CreateObject(_instance,RuntimeNPClass<LibvlcVideoNPObject>::getClass());
OBJECT_TO_NPVARIANT(NPN_RetainObject(videoObj), result);
return INVOKERESULT_NO_ERROR;
case ID_root_VersionInfo:
{
int len = strlen(VLC_Version());
NPUTF8 *retval =(NPUTF8*)NPN_MemAlloc(len);
if( retval )
{
memcpy(retval, VLC_Version(), len);
STRINGN_TO_NPVARIANT(retval, len, result);
}
else
{
NULL_TO_NPVARIANT(result);
}
return INVOKERESULT_NO_ERROR;
}
default:
;
}
}
return INVOKERESULT_GENERIC_ERROR;
}
const NPUTF8 * const LibvlcRootNPObject::methodNames[] =
{
"versionInfo",
};
const int LibvlcRootNPObject::methodCount = sizeof(LibvlcRootNPObject::methodNames)/sizeof(NPUTF8 *);
enum LibvlcRootNPObjectMethodIds
{
ID_root_versionInfo,
};
RuntimeNPObject::InvokeResult LibvlcRootNPObject::invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result)
{
/* is plugin still running */
if( _instance->pdata )
{
libvlc_exception_t ex;
libvlc_exception_init(&ex);
switch( index )
{
case ID_root_versionInfo:
if( argCount == 0 )
{
int len = strlen(VLC_Version());
NPUTF8 *retval =(NPUTF8*)NPN_MemAlloc(len);
if( retval )
{
memcpy(retval, VLC_Version(), len);
STRINGN_TO_NPVARIANT(retval, len, result);
}
else
{
NULL_TO_NPVARIANT(result);
}
return INVOKERESULT_NO_ERROR;
}
return INVOKERESULT_NO_SUCH_METHOD;
default:
;
}
}
return INVOKERESULT_GENERIC_ERROR;
}
/*
** implementation of libvlc audio object
*/
const NPUTF8 * const LibvlcAudioNPObject::propertyNames[] =
{
"mute",
"volume",
};
const int LibvlcAudioNPObject::propertyCount = sizeof(LibvlcAudioNPObject::propertyNames)/sizeof(NPUTF8 *);
enum LibvlcAudioNPObjectPropertyIds
{
ID_audio_mute,
ID_audio_volume
};
RuntimeNPObject::InvokeResult LibvlcAudioNPObject::getProperty(int index, NPVariant &result)
{
/* is plugin still running */
if( _instance->pdata )
{
VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(_instance->pdata);
libvlc_exception_t ex;
libvlc_exception_init(&ex);
switch( index )
{
case ID_audio_mute:
{
vlc_bool_t muted = libvlc_audio_get_mute(p_plugin->getVLC(), &ex);
if( libvlc_exception_raised(&ex) )
{
NPN_SetException(this, libvlc_exception_get_message(&ex));
libvlc_exception_clear(&ex);
return INVOKERESULT_GENERIC_ERROR;
}
BOOLEAN_TO_NPVARIANT(muted, result);
return INVOKERESULT_NO_ERROR;
}
case ID_audio_volume:
{
int volume = libvlc_audio_get_volume(p_plugin->getVLC(), &ex);
if( libvlc_exception_raised(&ex) )
{
NPN_SetException(this, libvlc_exception_get_message(&ex));
libvlc_exception_clear(&ex);
return INVOKERESULT_GENERIC_ERROR;
}
INT32_TO_NPVARIANT(volume, result);
return INVOKERESULT_NO_ERROR;
}
default:
;
}
}
return INVOKERESULT_GENERIC_ERROR;
}
RuntimeNPObject::InvokeResult LibvlcAudioNPObject::setProperty(int index, const NPVariant &value)
{
/* is plugin still running */
if( _instance->pdata )
{
VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(_instance->pdata);
libvlc_exception_t ex;
libvlc_exception_init(&ex);
switch( index )
{
case ID_audio_mute:
if( NPVARIANT_IS_BOOLEAN(value) )
{
libvlc_audio_set_mute(p_plugin->getVLC(),
NPVARIANT_TO_BOOLEAN(value), &ex);
if( libvlc_exception_raised(&ex) )
{
NPN_SetException(this, libvlc_exception_get_message(&ex));
libvlc_exception_clear(&ex);
return INVOKERESULT_GENERIC_ERROR;
}
return INVOKERESULT_NO_ERROR;
}
return INVOKERESULT_INVALID_VALUE;
case ID_audio_volume:
if( isNumberValue(value) )
{
libvlc_audio_set_volume(p_plugin->getVLC(),
numberValue(value), &ex);
if( libvlc_exception_raised(&ex) )
{
NPN_SetException(this, libvlc_exception_get_message(&ex));
libvlc_exception_clear(&ex);
return INVOKERESULT_GENERIC_ERROR;
}
return INVOKERESULT_NO_ERROR;
}
return INVOKERESULT_INVALID_VALUE;
default:
;
}
}
return INVOKERESULT_GENERIC_ERROR;
}
const NPUTF8 * const LibvlcAudioNPObject::methodNames[] =
{
"toggleMute",
};
const int LibvlcAudioNPObject::methodCount = sizeof(LibvlcAudioNPObject::methodNames)/sizeof(NPUTF8 *);
enum LibvlcAudioNPObjectMethodIds
{
ID_audio_togglemute,
};
RuntimeNPObject::InvokeResult LibvlcAudioNPObject::invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result)
{
/* is plugin still running */
if( _instance->pdata )
{
VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(_instance->pdata);
libvlc_exception_t ex;
libvlc_exception_init(&ex);
switch( index )
{
case ID_audio_togglemute:
if( argCount == 0 )
{
libvlc_audio_toggle_mute(p_plugin->getVLC(), &ex);
if( libvlc_exception_raised(&ex) )
{
NPN_SetException(this, libvlc_exception_get_message(&ex));
libvlc_exception_clear(&ex);
return INVOKERESULT_GENERIC_ERROR;
}
else
{
VOID_TO_NPVARIANT(result);
return INVOKERESULT_NO_ERROR;
}
}
return INVOKERESULT_NO_SUCH_METHOD;
default:
;
}
}
return INVOKERESULT_GENERIC_ERROR;
}
/*
** implementation of libvlc input object
*/
const NPUTF8 * const LibvlcInputNPObject::propertyNames[] =
{
"length",
"position",
"time",
"state",
"rate",
"fps",
"hasVout",
};
const int LibvlcInputNPObject::propertyCount = sizeof(LibvlcInputNPObject::propertyNames)/sizeof(NPUTF8 *);
enum LibvlcInputNPObjectPropertyIds
{
ID_input_length,
ID_input_position,
ID_input_time,
ID_input_state,
ID_input_rate,
ID_input_fps,
ID_input_hasvout,
};
RuntimeNPObject::InvokeResult LibvlcInputNPObject::getProperty(int index, NPVariant &result)
{
/* is plugin still running */
if( _instance->pdata )
{
VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(_instance->pdata);
libvlc_exception_t ex;
libvlc_exception_init(&ex);
libvlc_input_t *p_input = libvlc_playlist_get_input(p_plugin->getVLC(), &ex);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -