[vlc-devel] Support of events in Mozilla plugin - Beta

y.brehon at qiplay.com y.brehon at qiplay.com
Wed Nov 11 11:44:43 CET 2009


Please find attached a corrected version, following your recommendations.
Please feel free to comment.
Thanks for your help!
Yannick

On Mon, 9 Nov 2009 13:19:53 +0100, jpd at videolan.org wrote:
> With apologies for the delay.
> 
> On Tue, Nov 03, 2009 at 02:27:29PM +0100, Yannick Br??hon wrote:
>> >>>> First, I noticed you did use a bitmap, which is good, but I had
>> >>>> something slightly different in mind. I have that code around so
>> >>>> I'll
>> >>>> drop that in soonish. Same with the list of events, or you could
>> >>>> take
>> >>>> a look at the lookup function in core.
>> >>> We are not quite sure what you want here, can you please expand on
>> >>> this
>> >>> topic? Thanks.
> 
> Sure. Something like:
> 
> // Note that the accessor functions are unsafe, but this is handled in
> // the next layer up. 64bit uints can be substituted to taste (shift=6).
> template<size_t M> class bitmap {
> private:
>     typedef uint32_t bitu_t; enum { shift=5 };
>     enum { bmax=M, bpu=1<<shift, mask=bpu-1, units=(bmax+bpu-1)/bpu };
>     bitu_t bits[units];
> public:
>     bool get(size_t idx) const { return
bits[idx>>shift]&(1<<(idx&mask)); }
>     void set(size_t idx)       { bits[idx>>shift]|=  1<<(idx&mask);  }
>     void reset(size_t idx)     { bits[idx>>shift]&=~(1<<(idx&mask)); }
>     void toggle(size_t idx)    { bits[idx>>shift]^=  1<<(idx&mask);  }
>     size_t maxbit() const      { return bmax; }
>     void clear()               { memset(bits,0,sizeof(bits)); }
>     bitmap() { clear(); }
>     ~bitmap() { }
> };
> 
> class eventtypes_bitmap_t: private bitmap<libvlc_num_event_types> {
> private:
>     typedef libvlc_event_type_t event_t;
>     event_t find_event(const char *s)
>     {
>         event_t i;
>         for(i=0;i<maxbit();++i)
>             if(!strcmp(s,libvlc_event_type_name(i)))
>                 break;
>         return i;
>     }
> public:
>     bool add_event(const char *s) {
>         event_t event=find_event(s);
>         bool b=event<maxbit();
>         if(b) set(event);
>         return b;
>     }
>     bool del_event(const char *s) {
>         event_t event=find_event(s);
>         bool b=event<maxbit();
>         if(b) reset(event);
>         return b;
>     }
>     bool have_event(libvlc_event_type_t event)
>     {
>         return event<maxbit()?get(event):false;
>     }
> };
> 
> Use an instance of eventtypes_bitmap_t to handle storing and filtering
> events, one for each receiver. This implementation allows for all events
> to be reported back. If that is too many, well, that can be fixed.
> 
> You can also keep a plugin-global instance of this to lazily register
> callbacks with libvlc.
> 
> I think you'll find that with the above you can simplify if not outright
> remove a lot of code. Note that the above code hasn't been tested;
> please make sure you test it first.
> 
> 
>> >>>> Second, I'd really rather you didn't traverse string arguments
>> >>>> where you could be parsing an array-of-strings passed from
>> >>>> javascript instead.
>> >>> We have mimicked the mechanism used today to setup a new VLC
>> >>> plugin to get the options. We now support both a string of options
>> >>> or an array of strings (exactly the same as the options for a new
>> >>> plugin).
> 
> I would like you to drop support for multiple events in a single string.
> 
> Instead, support any number of arguments of type string, or one argument
> of type array of string. Feel free to return an error and do nothing as
> soon as you find any argument that does not match those profiles.
> 
> This because I'd like a new API to come into life clean, and there
> really is no need to spend any effort on picking apart strings for
> tokens. Just let the type above look at the entire string, and only
> handle walking through multiple string and array arguments.
> 
> 
>> >>> We have deliberately not called the
>> >>> LibvlcPlaylistNPObject::parseOptions method for the following two
>> >>> reasons:
>> >>> - it is apparently bound to evolve quite a bit according to the
>> >>> number
>> >>> of FIXMEs and we are not sure our code will be compatible with such
>> >>> evolutions and/or that our code won't prevent such evolutions
> 
> If the code is integrated then that ceases to be a problem: It will have
> to be maintained integrally with the rest. But I wasn't asking you to
> call parseOptions.
> 
> 
>> >>> Moreover, we noticed on *our* development platform that
>> >>> compilation fails because the HAS_NPFUNCTIONS_H variable is not
>> >>> #defined for us. We had to manually define it, otherwise compiler
>> >>> uses npupp.h instead of npfunctions.h, which doesn't define the
>> >>> API functions we need for our patch.
> 
> Might be something for autoconf to handle, but I can't really comment
> on that at this point.
> 
> 
>> +// Verifies the version of the NPAPI.
>> +// The eventListeners use a NPAPI function available
>> +// since Gecko 1.9.
>> +bool canUseEventListener()
> 
> Could use a static. In fact it should be a static member of the plugin
> wrapper object.
> 
> 
>>  const NPUTF8 * const LibvlcRootNPObject::methodNames[] =
>>  {
>>      "versionInfo",
>> +    "addEventListener",
>> +    "removeEventListeners"
>>  };
> 
> After rethinking this for a while, and considering how the rest of the
> plugin api is structured, this should change to an events property with
> addListener and removeListener methods. Naming suggestions welcome.
> 
> 
>> +#if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) >= 20)
>> +    return (*gNetscapeFuncs.pluginthreadasynccall)(plugin, func,
>> userData);
>> +#endif
>> +}
> [...]
>>          gNetscapeFuncs.reloadplugins = nsTable->reloadplugins;
>> +        gNetscapeFuncs.pluginthreadasynccall =
>> +            nsTable->pluginthreadasynccall;
> 
> Looks like missing #if guards?
-------------- next part --------------
From 8ca6ea2c5869427a582790344e7d1a5e60c60130 Mon Sep 17 00:00:00 2001
From: Yannick Brehon <y.brehon at qiplay.com>
Date: Tue, 10 Nov 2009 16:09:06 +0100
Subject: [PATCH] Mozilla plugin event listeners.

---
 THANKS                                 |    2 +
 projects/mozilla/control/npolibvlc.cpp |  200 +++++++++++++++++++++++++++++++-
 projects/mozilla/control/npolibvlc.h   |   25 ++++-
 projects/mozilla/support/npunix.c      |   14 +++
 projects/mozilla/vlcplugin.cpp         |  103 ++++++++++++++++
 projects/mozilla/vlcplugin.h           |  117 +++++++++++++++++++
 6 files changed, 458 insertions(+), 3 deletions(-)

diff --git a/THANKS b/THANKS
index 54fb75a..25cb3cb 100644
--- a/THANKS
+++ b/THANKS
@@ -22,6 +22,7 @@ Alexey Salmin <alexey dot salmin at gmail dot com> - Russian localisation
 Alexis Ballier <aballlier at gentoo dot org> - Additional options in configure
 Alexis de Lattre <alexis at videolan dot org> - Documentation, packaging, IGMPv3 support and various fixes
 Amanpreet Singh Alam <aalam at users dot sf dot net> - Punjabi translation
+Amir Gouini <a.gouini at qiplay dot com> - VLC mozilla plugin event listerners.
 Andrea Guzzo <xant at xant dot net> - dc1394 firewire support
 André de Barros Martins Ribeiro <andrerib at ajato.com.br> - Brazilian portuguese localization
 Andre Pang <adre.pang at csiro dot au> - Annodex support
@@ -316,6 +317,7 @@ William Hawkins - Speex RTP payload format
 Xavier Maillard <zedek at fxgsproject.org> - audio converters
 Xavier Marchesini <xav at alarue.net> - Win32 fixes
 Xènia Albà Cantero <xenia_alba at hotmail.com> - Catalan translation
+Yannick Bréhon <y.brehon at qiplay dot com> - VLC mozilla plugin event listerners.
 Ye zhang <yzhang90003 _at_ gmail dot com> - Fix for VLM RTSP concurent LEAVE make VLC crash
 Yuehua Zhao <zhao908 at hotmail.com> - real video codec
 Yuksel Yildirim <xleopar at yahoo d0t com> - Turkish localisation
diff --git a/projects/mozilla/control/npolibvlc.cpp b/projects/mozilla/control/npolibvlc.cpp
index 348880d..6a7bd67 100644
--- a/projects/mozilla/control/npolibvlc.cpp
+++ b/projects/mozilla/control/npolibvlc.cpp
@@ -26,6 +26,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
+#include <vector>
 
 /* Mozilla stuff */
 #ifdef HAVE_MOZILLA_CONFIG_H
@@ -47,6 +48,71 @@
         return INVOKERESULT_GENERIC_ERROR; \
     } } while(false)
 
+#define ERROR_EVENT_NOT_FOUND "ERROR: One or more events could not be found."
+#define ERROR_API_VERSION "ERROR: NPAPI version not high enough. (Gecko >= 1.9 needed)"
+
+// Make a copy of an NPVariant.
+NPVariant saveNPVariant(const NPVariant& original)
+{
+    NPVariant res;
+
+    if (NPVARIANT_IS_STRING(original))
+        STRINGZ_TO_NPVARIANT(strdup(NPVARIANT_TO_STRING(original).utf8characters), res);
+    else if (NPVARIANT_IS_INT32(original))
+        INT32_TO_NPVARIANT(NPVARIANT_TO_INT32(original), res);
+    else if (NPVARIANT_IS_DOUBLE(original))
+        DOUBLE_TO_NPVARIANT(NPVARIANT_TO_DOUBLE(original), res);
+    else if (NPVARIANT_IS_OBJECT(original))
+    {
+        NPObject *obj = NPVARIANT_TO_OBJECT(original);
+        NPN_RetainObject(obj);
+        OBJECT_TO_NPVARIANT(obj, res);
+    }
+    else if (NPVARIANT_IS_BOOLEAN(original))
+        BOOLEAN_TO_NPVARIANT(NPVARIANT_TO_BOOLEAN(original), res);
+
+    return res;
+}
+
+// Parse an event Array given as a NPObject by JS.
+// This function is similar to LibvlcPlaylistNPObject::parseOptions,
+// but we don't use it because it's not clearly accessible and the FIXME flags
+// implie that it might be modified.
+bool parseEventArray(NPObject *obj, eventtypes_bitmap_t &eventToGet, NPP instance)
+{
+    NPIdentifier propId = NPN_GetStringIdentifier("length");
+    NPVariant value;
+
+    if (!NPN_GetProperty(instance, obj, propId, &value))
+        return false;
+
+    int count = NPVARIANT_TO_INT32(value);
+    NPN_ReleaseVariantValue(&value);
+    if (count == 0)
+        return false;
+
+    int nOptions = 0;
+    while (nOptions < count)
+    {
+        propId = NPN_GetIntIdentifier(nOptions);
+        // if there is no other string in the array.
+        if( ! NPN_GetProperty(instance, obj, propId, &value) )
+            break;
+
+        // if the element is not a string.
+        if( ! NPVARIANT_IS_STRING(value) )
+        {
+            NPN_ReleaseVariantValue(&value);
+            break;
+        }
+
+        if (!eventToGet.add_event(NPVARIANT_TO_STRING(value).utf8characters))
+            return false;
+        nOptions++;
+    }
+    return true;
+}
+
 /*
 ** implementation of libvlc root object
 */
@@ -77,6 +143,7 @@ const NPUTF8 * const LibvlcRootNPObject::propertyNames[] =
     "playlist",
     "subtitle",
     "video",
+    "events",
     "VersionInfo",
 };
 COUNTNAMES(LibvlcRootNPObject,propertyCount,propertyNames);
@@ -88,6 +155,7 @@ enum LibvlcRootNPObjectPropertyIds
     ID_root_playlist,
     ID_root_subtitle,
     ID_root_video,
+    ID_root_events,
     ID_root_VersionInfo,
 };
 
@@ -139,6 +207,14 @@ LibvlcRootNPObject::getProperty(int index, NPVariant &result)
                              RuntimeNPClass<LibvlcVideoNPObject>::getClass());
                 OBJECT_TO_NPVARIANT(NPN_RetainObject(videoObj), result);
                 return INVOKERESULT_NO_ERROR;
+            case ID_root_events:
+                // create child object in lazyman fashion to avoid
+                // ownership problem with firefox
+                if( ! eventObj )
+                    eventObj = NPN_CreateObject(_instance,
+                             RuntimeNPClass<LibvlcEventNPObject>::getClass());
+                OBJECT_TO_NPVARIANT(NPN_RetainObject(eventObj), result);
+                return INVOKERESULT_NO_ERROR;
             case ID_root_VersionInfo:
                 return invokeResultString(libvlc_get_version(),result);
             default:
@@ -696,8 +772,8 @@ LibvlcPlaylistItemsNPObject::invoke(int index, const NPVariant *args,
                     return INVOKERESULT_NO_ERROR;
                 }
                 return INVOKERESULT_NO_SUCH_METHOD;
-            default:
-                ;
+          default:
+              ;
         }
     }
     return INVOKERESULT_GENERIC_ERROR;
@@ -1837,3 +1913,123 @@ LibvlcMarqueeNPObject::invoke(int index, const NPVariant *args,
     }
     return INVOKERESULT_GENERIC_ERROR;
 }
+
+/*
+** implementation of libvlc event object
+*/
+
+const NPUTF8 * const LibvlcEventNPObject::propertyNames[] =
+{
+};
+
+enum LibvlcEventNPObjectPropertyIds
+{
+};
+COUNTNAMES(LibvlcEventNPObject,propertyCount,propertyNames);
+
+const NPUTF8 * const LibvlcEventNPObject::methodNames[] =
+{
+    "addListener",
+    "removeListeners"
+};
+COUNTNAMES(LibvlcEventNPObject,methodCount,methodNames);
+
+enum LibvlcEventNPObjectMethodIds
+{
+    ID_root_addListener,
+    ID_root_removeListeners,
+};
+
+bool LibvlcEventNPObject::parseArgs(const NPVariant *args, uint32_t argCount,
+                                    eventtypes_bitmap_t &eventToGet)
+{
+    if (argCount > 2)
+        eventToGet.clear_event();
+
+    for (int argIndex = 2; argIndex < argCount; argIndex++)
+    {
+        if (NPVARIANT_IS_STRING(args[argIndex]))
+        {
+            if (!eventToGet.add_event(NPVARIANT_TO_STRING(args[argIndex]).utf8characters))
+                return false;
+        }
+        else if (NPVARIANT_IS_OBJECT(args[argIndex]))
+        {
+            if (!parseEventArray(NPVARIANT_TO_OBJECT(args[argIndex]), eventToGet, _instance))
+                return false;
+        }
+        else
+            return false;
+    }
+    return true;
+}
+
+RuntimeNPObject::InvokeResult
+LibvlcEventNPObject::invoke(int index, const NPVariant *args,
+                            uint32_t argCount, NPVariant &result)
+{
+    /* is plugin still running */
+    if( isPluginRunning() )
+    {
+        libvlc_exception_t ex;
+        libvlc_exception_init(&ex);
+
+        switch( index )
+        {
+            case ID_root_addListener:
+                if (argCount >= 2)
+                {
+                    // Checks if the first argument is a NPObject
+                    if (!NPVARIANT_IS_OBJECT(args[0]))
+                        return INVOKERESULT_NO_SUCH_METHOD;
+
+                    // Checks if the browser has the NPAPI version 0.19 at least.
+                    if (!VlcPlugin::canUseEventListener())
+                    {
+                      NPN_SetException(this, strdup(ERROR_API_VERSION));
+                      return INVOKERESULT_GENERIC_ERROR;
+                    }
+
+                    VlcPlugin* p_plugin = getPrivate<VlcPlugin>();
+
+                    // Gets the binary field corresponding to the events the
+                    // listener must listen to if specified.
+                    // Else, listen to all events.
+                    eventtypes_bitmap_t *eventToGet = new eventtypes_bitmap_t();
+                    eventToGet->set_all_events();
+
+                    if (!parseArgs(args, argCount, *eventToGet))
+                    {
+                        NPN_SetException(this, strdup(ERROR_EVENT_NOT_FOUND));
+                        return INVOKERESULT_GENERIC_ERROR;
+                    }
+
+                    NPObject *listener = NPVARIANT_TO_OBJECT(args[0]);
+                    NPN_RetainObject(listener);
+
+                    EventListener *eventListener = new EventListener();
+                    eventListener->listener = listener;
+                    eventListener->id = saveNPVariant(args[1]);
+                    eventListener->eventMap = eventToGet;
+                    p_plugin->eventToCatch.add_event(*eventToGet);
+
+                    p_plugin->eventListeners.push_back(eventListener);
+
+                    return INVOKERESULT_NO_ERROR;
+                }
+                return INVOKERESULT_NO_SUCH_METHOD;
+          case ID_root_removeListeners:
+              if (argCount == 0)
+              {
+                  VlcPlugin* p_plugin = getPrivate<VlcPlugin>();
+                  p_plugin->eventListeners.clear();
+                  p_plugin->eventToCatch.clear_event();
+                  return INVOKERESULT_NO_ERROR;
+              }
+              return INVOKERESULT_NO_SUCH_METHOD;
+          default:
+              ;
+        }
+    }
+    return INVOKERESULT_GENERIC_ERROR;
+}
diff --git a/projects/mozilla/control/npolibvlc.h b/projects/mozilla/control/npolibvlc.h
index 2fc3107..53d551d 100644
--- a/projects/mozilla/control/npolibvlc.h
+++ b/projects/mozilla/control/npolibvlc.h
@@ -38,7 +38,8 @@ protected:
     inputObj(NULL),
     playlistObj(NULL),
     subtitleObj(NULL),
-    videoObj(NULL) {};
+    videoObj(NULL),
+    eventObj(NULL) {};
 
     virtual ~LibvlcRootNPObject();
 
@@ -58,6 +59,7 @@ private:
     NPObject *playlistObj;
     NPObject *subtitleObj;
     NPObject *videoObj;
+    NPObject *eventObj;
 };
 
 class LibvlcAudioNPObject: public RuntimeNPObject
@@ -218,3 +220,24 @@ protected:
 
     InvokeResult invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result);
 };
+
+class LibvlcEventNPObject: public RuntimeNPObject
+{
+protected:
+    friend class RuntimeNPClass<LibvlcEventNPObject>;
+
+    LibvlcEventNPObject(NPP instance, const NPClass *aClass) :
+        RuntimeNPObject(instance, aClass) {};
+    virtual ~LibvlcEventNPObject() {};
+
+    static const int propertyCount;
+    static const NPUTF8 * const propertyNames[];
+
+    static const int methodCount;
+    static const NPUTF8 * const methodNames[];
+
+    InvokeResult invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result);
+
+    bool parseArgs(const NPVariant *args, uint32_t argCount,
+                   eventtypes_bitmap_t &eventToGet);
+};
diff --git a/projects/mozilla/support/npunix.c b/projects/mozilla/support/npunix.c
index 41ceff2..52096bc 100644
--- a/projects/mozilla/support/npunix.c
+++ b/projects/mozilla/support/npunix.c
@@ -100,6 +100,16 @@ NPN_Version(int* plugin_major, int* plugin_minor,
     *netscape_minor = gNetscapeFuncs.version & 0xFF;
 }
 
+void
+NPN_PluginThreadAsyncCall(NPP plugin,
+                          void (*func)(void *),
+                          void *userData)
+{
+#if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) >= 20)
+    return (*gNetscapeFuncs.pluginthreadasynccall)(plugin, func, userData);
+#endif
+}
+
 NPError
 NPN_GetValue(NPP instance, NPNVariable variable, void *r_value)
 {
@@ -846,6 +856,10 @@ NP_Initialize(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs)
         gNetscapeFuncs.memfree       = nsTable->memfree;
         gNetscapeFuncs.memflush      = nsTable->memflush;
         gNetscapeFuncs.reloadplugins = nsTable->reloadplugins;
+#if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) >= 20)
+        gNetscapeFuncs.pluginthreadasynccall =
+            nsTable->pluginthreadasynccall;
+#endif
 #ifdef OJI
         if( minor >= NPVERS_HAS_LIVECONNECT )
         {
diff --git a/projects/mozilla/vlcplugin.cpp b/projects/mozilla/vlcplugin.cpp
index 34526c0..3f953b2 100644
--- a/projects/mozilla/vlcplugin.cpp
+++ b/projects/mozilla/vlcplugin.cpp
@@ -86,6 +86,54 @@ static bool boolValue(const char *value) {
              !strcasecmp(value, "yes") );
 }
 
+void eventAsync(void *param)
+{
+    VlcPlugin *plugin = (VlcPlugin*)param;
+    NPVariant result;
+    NPVariant params[2];
+
+    // mutex lock
+    pthread_mutex_lock(&plugin->mutex);
+
+    for (int i = 0; i < plugin->eventList.size(); i++)
+    {
+        for (int j = 0; j < plugin->eventListeners.size(); j++)
+        {
+            const NPUTF8* eventName = plugin->eventList[i];
+
+            if (plugin->eventListeners[j]->eventMap->have_event(eventName))
+            {
+                STRINGZ_TO_NPVARIANT(strdup(eventName), params[0]);
+                params[1] = plugin->eventListeners[j]->id;
+                NPN_InvokeDefault(plugin->getBrowser(), plugin->eventListeners[j]->listener, params, 2, &result);
+                NPN_ReleaseVariantValue(&result);
+                NPN_ReleaseVariantValue(&params[0]);
+            }
+
+        }
+    }
+    plugin->eventList.clear();
+
+    // mutex unlock
+    pthread_mutex_unlock(&plugin->mutex);
+}
+
+void eventFct(const libvlc_event_t* event, void *param)
+{
+    VlcPlugin *plugin = (VlcPlugin*)param;
+
+    // mutex lock
+    pthread_mutex_lock(&plugin->mutex);
+
+    if (plugin->eventToCatch.have_event(event->type))
+        plugin->eventList.push_back(libvlc_event_type_name(event->type));
+
+    // mutex unlock
+    pthread_mutex_unlock(&plugin->mutex);
+
+    NPN_PluginThreadAsyncCall(plugin->getBrowser(), eventAsync, plugin);
+}
+
 NPError VlcPlugin::init(int argc, char* const argn[], char* const argv[])
 {
     /* prepare VLC command line */
@@ -264,6 +312,10 @@ NPError VlcPlugin::init(int argc, char* const argn[], char* const argv[])
     /* new APIs */
     p_scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
 
+    // mutex init
+    if (pthread_mutex_init(&mutex, NULL) != 0)
+        return NPERR_GENERIC_ERROR;
+
     return NPERR_NO_ERROR;
 }
 
@@ -279,6 +331,16 @@ VlcPlugin::~VlcPlugin()
         libvlc_media_list_release( libvlc_media_list );
     if( libvlc_instance )
         libvlc_release(libvlc_instance);
+
+    for (int i = 0; i < eventListeners.size(); i++)
+    {
+        NPN_ReleaseObject(eventListeners[i]->listener);
+        NPN_ReleaseVariantValue(&(eventListeners[i]->id));
+        delete eventListeners[i]->eventMap;
+        delete eventListeners[i];
+    }
+
+    pthread_mutex_destroy(&mutex);
 }
 
 /*****************************************************************************
@@ -343,6 +405,7 @@ int VlcPlugin::playlist_add_extended_untrusted( const char *mrl, const char *nam
 bool VlcPlugin::playlist_select( int idx, libvlc_exception_t *ex )
 {
     libvlc_media_t *p_m = NULL;
+    libvlc_event_manager_t *eventManager = NULL;
 
     libvlc_media_list_lock(libvlc_media_list);
 
@@ -368,10 +431,33 @@ bool VlcPlugin::playlist_select( int idx, libvlc_exception_t *ex )
     }
 
     libvlc_media_player = libvlc_media_player_new_from_media(p_m,ex);
+
+
     if( libvlc_media_player )
+      {
         set_player_window(ex);
 
+        // Registers the events we're interested in.
+        eventManager = libvlc_media_player_event_manager(libvlc_media_player, ex);
+
+        libvlc_event_attach(eventManager, libvlc_MediaPlayerOpening, eventFct, this, ex);
+        libvlc_event_attach(eventManager, libvlc_MediaPlayerBuffering, eventFct, this, ex);
+        libvlc_event_attach(eventManager, libvlc_MediaPlayerPlaying, eventFct, this, ex);
+        libvlc_event_attach(eventManager, libvlc_MediaPlayerStopped, eventFct, this, ex);
+        libvlc_event_attach(eventManager, libvlc_MediaPlayerPaused, eventFct, this, ex);
+        libvlc_event_attach(eventManager, libvlc_MediaPlayerEndReached, eventFct, this, ex);
+        libvlc_event_attach(eventManager, libvlc_MediaPlayerForward, eventFct, this, ex);
+        libvlc_event_attach(eventManager, libvlc_MediaPlayerBackward, eventFct, this, ex);
+        libvlc_event_attach(eventManager, libvlc_MediaPlayerEndReached, eventFct, this, ex);
+        libvlc_event_attach(eventManager, libvlc_MediaPlayerEncounteredError, eventFct, this, ex);
+        libvlc_event_attach(eventManager, libvlc_MediaPlayerTimeChanged, eventFct, this, ex);
+        libvlc_event_attach(eventManager, libvlc_MediaPlayerPositionChanged, eventFct, this, ex);
+        libvlc_event_attach(eventManager, libvlc_MediaPlayerTitleChanged, eventFct, this, ex);
+        libvlc_event_attach(eventManager, libvlc_MediaPlayerSnapshotTaken, eventFct, this, ex);
+      }
+
     libvlc_media_release( p_m );
+
     return !libvlc_exception_raised(ex);
 
 bad_unlock:
@@ -911,3 +997,20 @@ vlc_toolbar_clicked_t VlcPlugin::getToolbarButtonClicked( int i_xpos, int i_ypos
 }
 #undef BTN_SPACE
 #endif
+
+// Verifies the version of the NPAPI.
+// The eventListeners use a NPAPI function available
+// since Gecko 1.9.
+bool VlcPlugin::canUseEventListener()
+{
+    int plugin_major, plugin_minor;
+    int browser_major, browser_minor;
+
+    NPN_Version(&plugin_major, &plugin_minor,
+                &browser_major, &browser_minor);
+
+    if (browser_minor >= 0.19 || browser_major > 0)
+        return true;
+    return false;
+}
+
diff --git a/projects/mozilla/vlcplugin.h b/projects/mozilla/vlcplugin.h
index 517530e..df609b8 100644
--- a/projects/mozilla/vlcplugin.h
+++ b/projects/mozilla/vlcplugin.h
@@ -31,6 +31,9 @@
 
 #include <vlc/vlc.h>
 #include <npapi.h>
+#include <string>
+#include <vector>
+#include <map>
 #include "control/nporuntime.h"
 
 #if !defined(XP_MACOSX) && !defined(XP_UNIX) && !defined(XP_WIN)
@@ -79,6 +82,111 @@ typedef enum vlc_toolbar_clicked_e {
     clicked_Unmute
 } vlc_toolbar_clicked_t;
 
+
+// Note that the accessor functions are unsafe, but this is handled in
+// the next layer up. 64bit uints can be substituted to taste (shift=6).
+template<size_t M> class bitmap
+{
+private:
+    typedef uint32_t bitu_t; enum { shift=5 };
+    enum { bmax=M, bpu=1<<shift, mask=bpu-1, units=(bmax+bpu-1)/bpu };
+    bitu_t bits[units];
+public:
+    bool get(size_t idx) const
+    {
+        return bits[idx>>shift]&(1<<(idx&mask));
+    }
+
+    void set(size_t idx)       { bits[idx>>shift]|=  1<<(idx&mask);  }
+    void reset(size_t idx)     { bits[idx>>shift]&=~(1<<(idx&mask)); }
+    void toggle(size_t idx)    { bits[idx>>shift]^=  1<<(idx&mask);  }
+    size_t maxbit() const      { return bmax; }
+    void clear()               { memset(bits,0,sizeof(bits)); }
+    bitmap() { clear(); }
+    ~bitmap() { }
+};
+
+class eventtypes_bitmap_t: private bitmap<libvlc_num_event_types> {
+private:
+    typedef libvlc_event_type_t event_t;
+    event_t find_event(const char *s) const
+    {
+        event_t i;
+        for(i=0;i<maxbit();++i)
+            if(!strcmp(s,libvlc_event_type_name(i)))
+                break;
+        return i;
+    }
+public:
+    bool add_event(const eventtypes_bitmap_t &eventBitmap)
+    {
+        event_t i;
+        for(i=0;i<maxbit();++i)
+            if (eventBitmap.have_event(i))
+                set(i);
+    }
+    bool add_event(const char *s)
+    {
+        event_t event = find_event(s);
+        bool b = event<maxbit();
+        if(event)
+            set(event);
+
+        return b;
+    }
+    bool del_event(const char *s) {
+        event_t event=find_event(s);
+        bool b=event<maxbit();
+        if(b) reset(event);
+        return b;
+    }
+    bool have_event(libvlc_event_type_t event) const
+    {
+        return event<maxbit()?get(event):false;
+    }
+    bool have_event(const char *s) const
+    {
+        event_t event = find_event(s);
+        return event<maxbit()?get(event):false;
+    }
+    void clear_event()
+    {
+        clear();
+    }
+    void set_all_events()
+    {
+        event_t i;
+        for(i=0;i<maxbit();++i)
+            set(i);
+    }
+};
+
+
+// Structure used to represent an EventListener.
+// It contains the listener object that will be invoked,
+// An Id given by the addEventListener function and sent
+// when invoking the listener. Can be anything or nothing.
+// The profile associated with the listener used to invoke
+// the listener only to some events.
+typedef struct s_EventListener
+{
+    NPObject *listener;
+    NPVariant id;
+    eventtypes_bitmap_t *eventMap;
+
+} EventListener;
+
+// Comparator for the global map of event. With it, we can use
+// char* as key.
+struct StringCmp
+{
+    bool operator() (const char* s1, const char* s2) const
+    {
+        return strcmp(s1, s2) < 0;
+    }
+};
+
+
 class VlcPlugin
 {
 public:
@@ -197,6 +305,15 @@ public:
 
     bool  player_has_vout( libvlc_exception_t * );
 
+
+    // Events related members
+    std::vector<EventListener*> eventListeners; // List of registered listerners.
+    std::vector<const NPUTF8*> eventList; // List of event sent by VLC that must be returned to JS.
+    eventtypes_bitmap_t eventToCatch;
+    pthread_mutex_t mutex;
+
+    static bool canUseEventListener();
+
 private:
     bool playlist_select(int,libvlc_exception_t *);
     void set_player_window( libvlc_exception_t * );
-- 
1.5.6.3



More information about the vlc-devel mailing list