📄 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 "vlcplugin.h"#include "npolibvlc.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(libvlc_get_version()); NPUTF8 *retval =(NPUTF8*)NPN_MemAlloc(len); if( retval ) { memcpy(retval, libvlc_get_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(libvlc_get_version()); NPUTF8 *retval =(NPUTF8*)NPN_MemAlloc(len); if( retval ) { memcpy(retval, libvlc_get_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", "track", "channel",};const int LibvlcAudioNPObject::propertyCount = sizeof(LibvlcAudioNPObject::propertyNames)/sizeof(NPUTF8 *);enum LibvlcAudioNPObjectPropertyIds{ ID_audio_mute, ID_audio_volume, ID_audio_track, ID_audio_channel,};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: { bool 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; } case ID_audio_track: { libvlc_media_player_t *p_md = libvlc_playlist_get_media_player(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; } int track = libvlc_audio_get_track(p_md, &ex); libvlc_media_player_release(p_md); if( libvlc_exception_raised(&ex) ) { NPN_SetException(this, libvlc_exception_get_message(&ex)); libvlc_exception_clear(&ex); return INVOKERESULT_GENERIC_ERROR; } INT32_TO_NPVARIANT(track, result); return INVOKERESULT_NO_ERROR; } case ID_audio_channel: { int channel = libvlc_audio_get_channel(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(channel, 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; case ID_audio_track: if( isNumberValue(value) ) { libvlc_media_player_t *p_md = libvlc_playlist_get_media_player(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; } libvlc_audio_set_track(p_md, numberValue(value), &ex); libvlc_media_player_release(p_md); 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_channel: if( isNumberValue(value) ) { libvlc_audio_set_channel(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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -