]> git.sesse.net Git - vlc/blob - projects/mozilla/vlcplugin.h
Mozilla: Add the relevant webm mimetypes to the webplguin
[vlc] / projects / mozilla / vlcplugin.h
1 /*****************************************************************************
2  * vlcplugin.h: a VLC plugin for Mozilla
3  *****************************************************************************
4  * Copyright (C) 2002-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Damien Fouilleul <damienf@videolan.org>
9  *          Jean-Paul Saman <jpsaman@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*******************************************************************************
27  * Instance state information about the plugin.
28  ******************************************************************************/
29 #ifndef __VLCPLUGIN_H__
30 #define __VLCPLUGIN_H__
31
32 #include <vlc/vlc.h>
33
34 // Setup XP_MACOSX, XP_UNIX, XP_WIN
35 #if defined(_WIN32)
36 #define XP_WIN 1
37 #elif defined(__APPLE__)
38 #define XP_MACOSX 1
39 #else
40 #define XP_UNIX 1
41 #define MOZ_X11 1
42 #endif
43
44 #if !defined(XP_MACOSX) && !defined(XP_UNIX) && !defined(XP_WIN)
45 #define XP_UNIX 1
46 #elif defined(XP_MACOSX)
47 #undef XP_UNIX
48 #endif
49
50 #ifdef XP_WIN
51     /* Windows stuff */
52 #   include <windows.h>
53 #   include <winbase.h>
54 #endif
55
56 #ifdef XP_MACOSX
57     /* Mac OS X stuff */
58 #   include <Quickdraw.h>
59 #endif
60
61 #ifdef XP_UNIX
62 #   include <pthread.h>
63     /* X11 stuff */
64 #   include <X11/Xlib.h>
65 #   include <X11/Intrinsic.h>
66 #   include <X11/StringDefs.h>
67 #   include <X11/X.h>
68
69 #   ifndef __APPLE__
70 #       include <X11/xpm.h>
71 #   endif
72 #endif
73
74 #ifndef __MAX
75 #   define __MAX(a, b)   ( ((a) > (b)) ? (a) : (b) )
76 #endif
77 #ifndef __MIN
78 #   define __MIN(a, b)   ( ((a) < (b)) ? (a) : (b) )
79 #endif
80
81 #include <npapi.h>
82 #include <vector>
83
84 #include "control/nporuntime.h"
85
86
87 typedef struct {
88 #if defined(XP_UNIX)
89     pthread_mutex_t mutex;
90 #elif defined(XP_WIN)
91     CRITICAL_SECTION cs;
92 #else
93 #warning "locking not implemented in this platform"
94 #endif
95 } plugin_lock_t;
96
97
98 typedef enum vlc_toolbar_clicked_e {
99     clicked_Unknown = 0,
100     clicked_Play,
101     clicked_Pause,
102     clicked_Stop,
103     clicked_timeline,
104     clicked_Time,
105     clicked_Fullscreen,
106     clicked_Mute,
107     clicked_Unmute
108 } vlc_toolbar_clicked_t;
109
110
111 // Note that the accessor functions are unsafe, but this is handled in
112 // the next layer up. 64bit uints can be substituted to taste (shift=6).
113 template<size_t M> class bitmap
114 {
115 private:
116     typedef uint32_t bitu_t; enum { shift=5 };
117     enum { bmax=M, bpu=1<<shift, mask=bpu-1, units=(bmax+bpu-1)/bpu };
118     bitu_t bits[units];
119 public:
120     bool get(size_t idx) const { return bits[idx>>shift]&(1<<(idx&mask)); }
121     void set(size_t idx)       { bits[idx>>shift]|=  1<<(idx&mask);  }
122     void reset(size_t idx)     { bits[idx>>shift]&=~(1<<(idx&mask)); }
123     void toggle(size_t idx)    { bits[idx>>shift]^=  1<<(idx&mask);  }
124     size_t maxbit() const      { return bmax; }
125     void clear()               { memset(bits,0,sizeof(bits)); }
126     bitmap() { clear(); }
127     ~bitmap() { }
128     bool empty() const { // naive invert() will break this
129         for(size_t i=0;i<units;++i)
130             if(bits[i]) return false;
131         return true;
132     }
133 };
134
135 typedef bitmap<libvlc_VlmMediaInstanceStatusError+1> eventtypes_bitmap_t;
136
137
138 class EventObj: private eventtypes_bitmap_t
139 {
140 private:
141     typedef libvlc_event_type_t event_t;
142     bool have_event(event_t e) const { return e<maxbit()?get(e):false; }
143
144     class Listener: public eventtypes_bitmap_t
145     {
146     public:
147         Listener(event_t e,NPObject *o,bool b): _l(o), _b(b)
148             { NPN_RetainObject(o); set(e); }
149         Listener(): _l(NULL), _b(false) { }
150         ~Listener() { if(_l) NPN_ReleaseObject(_l); }
151         NPObject *listener() const { return _l; }
152         bool bubble() const { return _b; }
153     private:
154         NPObject *_l;
155         bool _b;
156     };
157
158     libvlc_event_manager_t *_em;
159     libvlc_callback_t _cb;
160     void *_ud;
161 public:
162     EventObj(): _em(NULL)  { /* deferred to init() */ }
163     bool init();
164     ~EventObj();
165
166     void deliver(NPP browser);
167     void callback(const libvlc_event_t*);
168     bool insert(const NPString &, NPObject *, bool);
169     bool remove(const NPString &, NPObject *, bool);
170     void unhook_manager();
171     void hook_manager(libvlc_event_manager_t *,libvlc_callback_t, void *);
172 private:
173     event_t find_event(const char *s) const;
174     typedef std::vector<Listener> lr_l;
175     typedef std::vector<libvlc_event_type_t> ev_l;
176     lr_l _llist;
177     ev_l _elist;
178
179     plugin_lock_t lock;
180
181     bool ask_for_event(event_t e);
182     void unask_for_event(event_t e);
183 };
184
185
186 class VlcPlugin
187 {
188 public:
189 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
190              VlcPlugin( NPP, uint16 );
191 #else
192              VlcPlugin( NPP, uint16_t );
193 #endif
194     virtual ~VlcPlugin();
195
196     NPError             init(int argc, char* const argn[], char* const argv[]);
197     libvlc_instance_t*  getVLC()
198                             { return libvlc_instance; };
199     libvlc_media_player_t* getMD()
200     {
201         if( !libvlc_media_player )
202         {
203              libvlc_printerr("no mediaplayer");
204         }
205         return libvlc_media_player;
206     }
207     NPP                 getBrowser()
208                             { return p_browser; };
209     char*               getAbsoluteURL(const char *url);
210     NPWindow&           getWindow()
211                             { return npwindow; };
212     void                setWindow(const NPWindow &window)
213                             { npwindow = window; };
214
215     NPClass*            getScriptClass()
216                             { return p_scriptClass; };
217
218 #if defined(XP_WIN)
219     WNDPROC             getWindowProc()
220                             { return pf_wndproc; };
221     void                setWindowProc(WNDPROC wndproc)
222                             { pf_wndproc = wndproc; };
223 #endif
224
225 #if defined(XP_UNIX)
226     int                 setSize(unsigned width, unsigned height);
227     Window              getVideoWindow()
228                             { return npvideo; };
229     void                setVideoWindow(Window window)
230                             { npvideo = window; };
231     Window              getControlWindow()
232                             { return npcontrol; };
233     void                setControlWindow(Window window)
234                             { npcontrol = window; };
235
236     void                showToolbar();
237     void                hideToolbar();
238     void                redrawToolbar();
239     void                getToolbarSize(unsigned int *width, unsigned int *height)
240                             { *width = i_tb_width; *height = i_tb_height; };
241     int                 setToolbarSize(unsigned int width, unsigned int height)
242                             { i_tb_width = width; i_tb_height = height; return 1; };
243     vlc_toolbar_clicked_t getToolbarButtonClicked( int i_xpos, int i_ypos );
244 #endif
245
246 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
247     uint16    i_npmode; /* either NP_EMBED or NP_FULL */
248 #else
249     uint16_t  i_npmode; /* either NP_EMBED or NP_FULL */
250 #endif
251
252     /* plugin properties */
253     int      b_stream;
254     int      b_autoplay;
255     int      b_toolbar;
256     char *   psz_text;
257     char *   psz_target;
258
259     void playlist_play()
260     {
261         if( playlist_isplaying() )
262             playlist_stop();
263         if( libvlc_media_player||playlist_select(0) )
264             libvlc_media_player_play(libvlc_media_player);
265     }
266     void playlist_play_item(int idx)
267     {
268         if( playlist_select(idx) )
269             libvlc_media_player_play(libvlc_media_player);
270     }
271     void playlist_stop()
272     {
273         if( libvlc_media_player )
274             libvlc_media_player_stop(libvlc_media_player);
275     }
276     void playlist_next()
277     {
278         if( playlist_select(playlist_index+1) )
279             libvlc_media_player_play(libvlc_media_player);
280     }
281     void playlist_prev()
282     {
283         if( playlist_select(playlist_index-1) )
284             libvlc_media_player_play(libvlc_media_player);
285     }
286     void playlist_pause()
287     {
288         if( libvlc_media_player )
289             libvlc_media_player_pause(libvlc_media_player);
290     }
291     int playlist_isplaying()
292     {
293         int is_playing = 0;
294         if( libvlc_media_player )
295             is_playing = libvlc_media_player_is_playing(
296                                 libvlc_media_player );
297         return is_playing;
298     }
299
300     int playlist_add( const char * );
301     int playlist_add_extended_untrusted( const char *, const char *, int,
302                                 const char ** );
303     int playlist_delete_item( int );
304     void playlist_clear();
305     int  playlist_count();
306
307     void toggle_fullscreen();
308     void set_fullscreen( int );
309     int  get_fullscreen();
310
311     bool  player_has_vout();
312
313
314     static bool canUseEventListener();
315
316     EventObj events;
317 private:
318     bool playlist_select(int);
319     void set_player_window();
320
321     /* VLC reference */
322     int                 playlist_index;
323     libvlc_instance_t   *libvlc_instance;
324     libvlc_media_list_t *libvlc_media_list;
325     libvlc_media_player_t *libvlc_media_player;
326     NPClass             *p_scriptClass;
327
328     /* browser reference */
329     NPP     p_browser;
330     char*   psz_baseURL;
331
332     /* display settings */
333     NPWindow  npwindow;
334 #if defined(XP_WIN)
335     WNDPROC   pf_wndproc;
336 #endif
337 #if defined(XP_UNIX)
338     unsigned int     i_width, i_height;
339     unsigned int     i_tb_width, i_tb_height;
340     Window           npvideo, npcontrol;
341
342     XImage *p_btnPlay;
343     XImage *p_btnPause;
344     XImage *p_btnStop;
345     XImage *p_timeline;
346     XImage *p_btnTime;
347     XImage *p_btnFullscreen;
348     XImage *p_btnMute;
349     XImage *p_btnUnmute;
350
351     int i_last_position;
352 #endif
353
354     static void eventAsync(void *);
355     static void event_callback(const libvlc_event_t *, void *);
356 };
357
358 /*******************************************************************************
359  * Plugin properties.
360  ******************************************************************************/
361 #define PLUGIN_NAME         "VLC Web Plugin"
362 #define PLUGIN_DESCRIPTION \
363     "Version %s, copyright 1996-2010 VideoLAN and Authors" \
364     "<br><a href=\"http://www.videolan.org/\">http://www.videolan.org/</a>"
365
366 #define PLUGIN_MIMETYPES \
367     /* MPEG-1 and MPEG-2 */ \
368     "audio/mpeg:mp2,mp3,mpga,mpega:MPEG audio;" \
369     "audio/x-mpeg:mp2,mp3,mpga,mpega:MPEG audio;" \
370     "video/mpeg:mpg,mpeg,mpe:MPEG video;" \
371     "video/x-mpeg:mpg,mpeg,mpe:MPEG video;" \
372     "video/mpeg-system:mpg,mpeg,mpe,vob:MPEG video;" \
373     "video/x-mpeg-system:mpg,mpeg,mpe,vob:MPEG video;" \
374     /* M3U */ \
375     "audio/x-mpegurl:m3u:MPEG audio;" \
376     /* MPEG-4 */ \
377     "video/mp4:mp4,mpg4:MPEG-4 video;" \
378     "audio/mp4:mp4,mpg4:MPEG-4 audio;" \
379     "audio/x-m4a:m4a:MPEG-4 audio;" \
380     "application/mpeg4-iod:mp4,mpg4:MPEG-4 video;" \
381     "application/mpeg4-muxcodetable:mp4,mpg4:MPEG-4 video;" \
382     /* AVI */ \
383     "video/x-msvideo:avi:AVI video;" \
384     /* QuickTime */ \
385     "video/quicktime:mov,qt:QuickTime video;" \
386     /* OGG */ \
387     "application/x-ogg:ogg:Ogg stream;" \
388     "application/ogg:ogg:Ogg stream;" \
389     /* VLC */ \
390     "application/x-vlc-plugin:vlc:VLC plug-in;" \
391     /* Windows Media */ \
392     "video/x-ms-asf-plugin:asf,asx:Windows Media Video;" \
393     "video/x-ms-asf:asf,asx:Windows Media Video;" \
394     "application/x-mplayer2::Windows Media;" \
395     "video/x-ms-wmv:wmv:Windows Media;" \
396     "video/x-ms-wvx:wvx:Windows Media Video;" \
397     "audio/x-ms-wma:wma:Windows Media Audio;" \
398     /* Google VLC */ \
399     "application/x-google-vlc-plugin::Google VLC plug-in;" \
400     /* WAV audio */ \
401     "audio/wav:wav:WAV audio;" \
402     "audio/x-wav:wav:WAV audio;" \
403     /* 3GPP */ \
404     "audio/3gpp:3gp,3gpp:3GPP audio;" \
405     "video/3gpp:3gp,3gpp:3GPP video;" \
406     /* 3GPP2 */ \
407     "audio/3gpp2:3g2,3gpp2:3GPP2 audio;" \
408     "video/3gpp2:3g2,3gpp2:3GPP2 video;" \
409     /* DIVX */ \
410     "video/divx:divx:DivX video;" \
411     /* FLV */ \
412     "video/flv:flv:FLV video;" \
413     "video/x-flv:flv:FLV video;" \
414     /* Matroska */ \
415     "video/x-matroska:mkv:Matroska video;" \
416     "audio/x-matroska:mka:Matroska audio;" \
417     /* XSPF */ \
418     "application/xspf+xml:xspf:Playlist xspf;" \
419     /* Webm */ \
420     "video/webm:webm:WebM video;" \
421     "audio/webm:webm:WebM audio;"
422
423 #endif