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