vlcplugin.h 11 KB
Newer Older
1
/*****************************************************************************
2
 * vlcplugin.h: a VLC plugin for Mozilla
3
 *****************************************************************************
Jean-Paul Saman's avatar
Jean-Paul Saman committed
4
 * Copyright (C) 2002-2009 the VideoLAN team
5
 * $Id$
6 7
 *
 * Authors: Samuel Hocevar <sam@zoy.org>
8
 *          Damien Fouilleul <damienf@videolan.org>
Jean-Paul Saman's avatar
Jean-Paul Saman committed
9
 *          Jean-Paul Saman <jpsaman@videolan.org>
10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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
Antoine Cellerier's avatar
Antoine Cellerier committed
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 25 26 27 28
 *****************************************************************************/

/*******************************************************************************
 * Instance state information about the plugin.
 ******************************************************************************/
29 30 31
#ifndef __VLCPLUGIN_H__
#define __VLCPLUGIN_H__

32 33 34 35
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

36
#include <vlc/vlc.h>
37 38 39 40 41 42 43 44 45 46 47

// Setup XP_MACOSX, XP_UNIX, XP_WIN
#if defined(_WIN32)
#define XP_WIN 1
#elif defined(__APPLE__)
#define XP_MACOSX 1
#else
#define XP_UNIX 1
#define MOZ_X11 1
#endif

48 49 50 51 52 53 54 55
#if !defined(XP_MACOSX) && !defined(XP_UNIX) && !defined(XP_WIN)
#define XP_UNIX 1
#elif defined(XP_MACOSX)
#undef XP_UNIX
#endif

#ifdef XP_WIN
    /* Windows stuff */
56
#   include <windows.h>
57
#   include <winbase.h>
58 59 60 61 62 63 64 65
#endif

#ifdef XP_MACOSX
    /* Mac OS X stuff */
#   include <Quickdraw.h>
#endif

#ifdef XP_UNIX
66
#   include <pthread.h>
67 68 69 70
    /* X11 stuff */
#   include <X11/Xlib.h>
#   include <X11/Intrinsic.h>
#   include <X11/StringDefs.h>
71
#   include <X11/X.h>
72 73 74 75 76 77 78 79 80 81 82

#   ifndef __APPLE__
#       include <X11/xpm.h>
#   endif
#endif

#ifndef __MAX
#   define __MAX(a, b)   ( ((a) > (b)) ? (a) : (b) )
#endif
#ifndef __MIN
#   define __MIN(a, b)   ( ((a) < (b)) ? (a) : (b) )
83
#endif
84

Jean-Paul Saman's avatar
Jean-Paul Saman committed
85 86
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

87 88 89 90 91
//on windows, to avoid including <npapi.h> 
//from Microsoft SDK (rather then from Mozilla SDK),
//#include it indirectly via <npfunctions.h>
#include <npfunctions.h>

92
#include <vector>
Jean-Paul Saman's avatar
Jean-Paul Saman committed
93
#include <assert.h>
94 95 96

#include "control/nporuntime.h"

97 98 99 100 101 102 103 104 105 106
typedef struct {
#if defined(XP_UNIX)
    pthread_mutex_t mutex;
#elif defined(XP_WIN)
    CRITICAL_SECTION cs;
#else
#warning "locking not implemented in this platform"
#endif
} plugin_lock_t;

107 108 109 110 111 112 113 114 115 116 117 118
typedef enum vlc_toolbar_clicked_e {
    clicked_Unknown = 0,
    clicked_Play,
    clicked_Pause,
    clicked_Stop,
    clicked_timeline,
    clicked_Time,
    clicked_Fullscreen,
    clicked_Mute,
    clicked_Unmute
} vlc_toolbar_clicked_t;

Jean-Paul Saman's avatar
Jean-Paul Saman committed
119 120 121 122 123
typedef struct {
    const char *name;                      /* event name */
    const libvlc_event_type_t libvlc_type; /* libvlc event type */
    libvlc_callback_t libvlc_callback;     /* libvlc callback function */
} vlcplugin_event_t;
124

Jean-Paul Saman's avatar
Jean-Paul Saman committed
125
class EventObj
126
{
127 128
private:

Jean-Paul Saman's avatar
Jean-Paul Saman committed
129
    class Listener
130
    {
131
    public:
Jean-Paul Saman's avatar
Jean-Paul Saman committed
132 133 134 135 136 137 138 139 140 141 142 143 144
        Listener(vlcplugin_event_t *event, NPObject *p_object, bool b_bubble):
            _event(event), _listener(p_object), _bubble(b_bubble)
            {
                assert(event);
                assert(p_object);
            }
        Listener(): _event(NULL), _listener(NULL), _bubble(false) { }
        ~Listener()
            {
            }
        const libvlc_event_type_t event_type() const { return _event->libvlc_type; }
        NPObject *listener() const { return _listener; }
        bool bubble() const { return _bubble; }
145
    private:
Jean-Paul Saman's avatar
Jean-Paul Saman committed
146 147 148
        vlcplugin_event_t *_event;
        NPObject *_listener;
        bool _bubble;
149 150
    };

Jean-Paul Saman's avatar
Jean-Paul Saman committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    class VLCEvent
    {
    public:
        VLCEvent(libvlc_event_type_t libvlc_event_type, NPVariant *npparams, uint32_t npcount):
            _libvlc_event_type(libvlc_event_type), _npparams(npparams), _npcount(npcount)
            {
            }
        VLCEvent(): _libvlc_event_type(0), _npparams(NULL), _npcount(0) { }
        ~VLCEvent()
            {
            }
        const libvlc_event_type_t event_type() { return _libvlc_event_type; }
        NPVariant *params() const { return _npparams; }
        const uint32_t count() { return _npcount; }
    private:
        libvlc_event_type_t _libvlc_event_type;
        NPVariant *_npparams;
        uint32_t _npcount;
    };
    libvlc_event_manager_t *_em; /* libvlc media_player event manager */
171 172
public:
    EventObj(): _em(NULL)  { /* deferred to init() */ }
173 174
    bool init();
    ~EventObj();
175 176

    void deliver(NPP browser);
Jean-Paul Saman's avatar
Jean-Paul Saman committed
177 178 179 180 181
    void callback(const libvlc_event_t *event, NPVariant *npparams, uint32_t count);
    bool insert(const NPString &name, NPObject *listener, bool bubble);
    bool remove(const NPString &name, NPObject *listener, bool bubble);
    void unhook_manager(void *);
    void hook_manager(libvlc_event_manager_t *, void *);
182
private:
Jean-Paul Saman's avatar
Jean-Paul Saman committed
183 184
    vlcplugin_event_t *find_event(const char *s) const;
    const char *find_name(const libvlc_event_t *event);
185
    typedef std::vector<Listener> lr_l;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
186 187 188
    typedef std::vector<VLCEvent> ev_l;
    lr_l _llist; /* list of registered listeners with 'addEventListener' method */
    ev_l _elist; /* scheduled events list for delivery to browser */
189

190
    plugin_lock_t lock;
191
};
192

193 194
#ifdef XP_WIN
HMODULE DllGetModule();
195
#include "../common/win32_fullscreen.h"
196 197
#endif

198
class VlcPlugin
199
{
200
public:
201
#if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
202
             VlcPlugin( NPP, uint16 );
203 204 205
#else
             VlcPlugin( NPP, uint16_t );
#endif
206 207
    virtual ~VlcPlugin();

208
    NPError             init(int argc, char* const argn[], char* const argv[]);
209
    libvlc_instance_t*  getVLC()
210
                            { return libvlc_instance; };
211
    libvlc_media_player_t* getMD()
212 213 214
    {
        if( !libvlc_media_player )
        {
Christophe Mutricy's avatar
Christophe Mutricy committed
215
             libvlc_printerr("no mediaplayer");
216 217 218
        }
        return libvlc_media_player;
    }
219 220 221
    NPP                 getBrowser()
                            { return p_browser; };
    char*               getAbsoluteURL(const char *url);
222 223
    NPWindow&           getWindow()
                            { return npwindow; };
224
    void                setWindow(const NPWindow &window);
225

226 227
    NPClass*            getScriptClass()
                            { return p_scriptClass; };
228

229
#if defined(XP_WIN)
230 231 232 233
    WNDPROC             getWindowProc()
                            { return pf_wndproc; };
    void                setWindowProc(WNDPROC wndproc)
                            { pf_wndproc = wndproc; };
234
#endif
235

236
#if defined(XP_UNIX)
237
    int                 setSize(unsigned width, unsigned height);
238 239 240 241 242 243 244 245
    Window              getVideoWindow()
                            { return npvideo; };
    void                setVideoWindow(Window window)
                            { npvideo = window; };
    Window              getControlWindow()
                            { return npcontrol; };
    void                setControlWindow(Window window)
                            { npcontrol = window; };
246 247 248 249

    void                showToolbar();
    void                hideToolbar();
    void                redrawToolbar();
250 251 252 253
    void                getToolbarSize(unsigned int *width, unsigned int *height)
                            { *width = i_tb_width; *height = i_tb_height; };
    int                 setToolbarSize(unsigned int width, unsigned int height)
                            { i_tb_width = width; i_tb_height = height; return 1; };
254
    vlc_toolbar_clicked_t getToolbarButtonClicked( int i_xpos, int i_ypos );
255 256
#endif

257
#if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
258
    uint16    i_npmode; /* either NP_EMBED or NP_FULL */
259 260 261
#else
    uint16_t  i_npmode; /* either NP_EMBED or NP_FULL */
#endif
262

263
    /* plugin properties */
264
    int      b_stream;
265
    int      b_autoplay;
266
    int      b_toolbar;
267
    char *   psz_text;
268
    char *   psz_target;
269

270
    void playlist_play()
271
    {
272 273
        if( playlist_isplaying() )
            playlist_stop();
274
        if( libvlc_media_player||playlist_select(0) )
275
            libvlc_media_player_play(libvlc_media_player);
276
    }
277
    void playlist_play_item(int idx)
278
    {
279
        if( playlist_select(idx) )
280
            libvlc_media_player_play(libvlc_media_player);
281
    }
282
    void playlist_stop()
283 284
    {
        if( libvlc_media_player )
285
            libvlc_media_player_stop(libvlc_media_player);
286
    }
287
    void playlist_next()
288
    {
289
        if( playlist_select(playlist_index+1) )
290
            libvlc_media_player_play(libvlc_media_player);
291
    }
292
    void playlist_prev()
293
    {
294
        if( playlist_select(playlist_index-1) )
295
            libvlc_media_player_play(libvlc_media_player);
296
    }
Rémi Duraffort's avatar
Rémi Duraffort committed
297
    void playlist_pause()
298 299
    {
        if( libvlc_media_player )
300
            libvlc_media_player_pause(libvlc_media_player);
301
    }
302
    int playlist_isplaying()
303 304 305 306
    {
        int is_playing = 0;
        if( libvlc_media_player )
            is_playing = libvlc_media_player_is_playing(
307
                                libvlc_media_player );
308 309
        return is_playing;
    }
310

311
    int playlist_add( const char * );
312
    int playlist_add_extended_untrusted( const char *, const char *, int,
313
                                const char ** );
314
    int playlist_delete_item( int );
315
    void playlist_clear();
316
    int  playlist_count();
317

318 319 320
    void toggle_fullscreen();
    void set_fullscreen( int );
    int  get_fullscreen();
321

322
    bool  player_has_vout();
323

324 325 326

    static bool canUseEventListener();

327
    EventObj events;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
328
    static void event_callback(const libvlc_event_t *, NPVariant *, uint32_t, void *);
329
private:
330
    bool playlist_select(int);
331
    void set_player_window();
332

333
    /* VLC reference */
334
    int                 playlist_index;
335
    libvlc_instance_t   *libvlc_instance;
336 337
    libvlc_media_list_t *libvlc_media_list;
    libvlc_media_player_t *libvlc_media_player;
338
    NPClass             *p_scriptClass;
339 340 341 342 343 344 345

    /* browser reference */
    NPP     p_browser;
    char*   psz_baseURL;

    /* display settings */
    NPWindow  npwindow;
346
#if defined(XP_WIN)
347
    WNDPROC   pf_wndproc;
348
    VLCWindowsManager _WindowsManager;
349
#endif
350
#if defined(XP_UNIX)
351
    unsigned int     i_width, i_height;
352
    unsigned int     i_tb_width, i_tb_height;
353
    Window           npvideo, npcontrol;
354

355 356 357 358 359 360 361 362 363 364
    XImage *p_btnPlay;
    XImage *p_btnPause;
    XImage *p_btnStop;
    XImage *p_timeline;
    XImage *p_btnTime;
    XImage *p_btnFullscreen;
    XImage *p_btnMute;
    XImage *p_btnUnmute;

    int i_last_position;
365
#endif
366 367

    static void eventAsync(void *);
368
};
369 370 371 372

/*******************************************************************************
 * Plugin properties.
 ******************************************************************************/
373
#define PLUGIN_NAME         "VLC Web Plugin"
374
#define PLUGIN_DESCRIPTION \
375 376
    "Version %s, copyright 1996-2011 VideoLAN and Authors" \
    "<br /><a href=\"http://www.videolan.org/vlc/\">http://www.videolan.org/vlc/</a>"
377

378
#endif