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