]> git.sesse.net Git - vlc/blob - projects/mozilla/vlcplugin.cpp
libvlc: rename libvlc_media_descriptor to libvlc_media and libvlc_media_instance...
[vlc] / projects / mozilla / vlcplugin.cpp
1 /*****************************************************************************
2  * vlcplugin.cpp: a VLC plugin for Mozilla
3  *****************************************************************************
4  * Copyright (C) 2002-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Damien Fouilleul <damienf.fouilleul@laposte.net>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include "config.h"
29
30 #ifdef HAVE_MOZILLA_CONFIG_H
31 #   include <mozilla-config.h>
32 #endif
33
34 #include "vlcplugin.h"
35 #include "control/npovlc.h"
36 #include "control/npolibvlc.h"
37
38 #include <ctype.h>
39
40 /*****************************************************************************
41  * VlcPlugin constructor and destructor
42  *****************************************************************************/
43 VlcPlugin::VlcPlugin( NPP instance, uint16 mode ) :
44     i_npmode(mode),
45     b_stream(0),
46     b_autoplay(1),
47     b_toolbar(0),
48     psz_target(NULL),
49     libvlc_instance(NULL),
50     libvlc_log(NULL),
51     p_scriptClass(NULL),
52     p_browser(instance),
53     psz_baseURL(NULL)
54 #if XP_WIN
55     ,pf_wndproc(NULL)
56 #endif
57 #if XP_UNIX
58     ,i_width((unsigned)-1)
59     ,i_height((unsigned)-1)
60     ,i_tb_width(0)
61     ,i_tb_height(0)
62     ,i_last_position(0)
63 #endif
64 {
65     memset(&npwindow, 0, sizeof(NPWindow));
66 }
67
68 static bool boolValue(const char *value) {
69     return ( !strcmp(value, "1") ||
70              !strcasecmp(value, "true") ||
71              !strcasecmp(value, "yes") );
72 }
73
74 NPError VlcPlugin::init(int argc, char* const argn[], char* const argv[])
75 {
76     /* prepare VLC command line */
77     char *ppsz_argv[32];
78     int ppsz_argc = 0;
79
80     /* locate VLC module path */
81 #ifdef XP_MACOSX
82     ppsz_argv[ppsz_argc++] = "--plugin-path";
83     ppsz_argv[ppsz_argc++] = "/Library/Internet Plug-Ins/VLC Plugin.plugin/"
84                              "Contents/MacOS/modules";
85 #elif defined(XP_WIN)
86     HKEY h_key;
87     DWORD i_type, i_data = MAX_PATH + 1;
88     char p_data[MAX_PATH + 1];
89     if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\VideoLAN\\VLC",
90                       0, KEY_READ, &h_key ) == ERROR_SUCCESS )
91     {
92          if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
93                               (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
94          {
95              if( i_type == REG_SZ )
96              {
97                  strcat( p_data, "\\plugins" );
98                  ppsz_argv[ppsz_argc++] = "--plugin-path";
99                  ppsz_argv[ppsz_argc++] = p_data;
100              }
101          }
102          RegCloseKey( h_key );
103     }
104     ppsz_argv[ppsz_argc++] = "--no-one-instance";
105
106 #endif /* XP_MACOSX */
107
108     /* common settings */
109     ppsz_argv[ppsz_argc++] = "-vv";
110     ppsz_argv[ppsz_argc++] = "--no-stats";
111     ppsz_argv[ppsz_argc++] = "--no-media-library";
112     ppsz_argv[ppsz_argc++] = "--intf";
113     ppsz_argv[ppsz_argc++] = "dummy";
114
115     const char *progid = NULL;
116
117     /* parse plugin arguments */
118     for( int i = 0; i < argc ; i++ )
119     {
120         fprintf(stderr, "argn=%s, argv=%s\n", argn[i], argv[i]);
121
122         if( !strcmp( argn[i], "target" )
123          || !strcmp( argn[i], "mrl")
124          || !strcmp( argn[i], "filename")
125          || !strcmp( argn[i], "src") )
126         {
127             psz_target = argv[i];
128         }
129         else if( !strcmp( argn[i], "autoplay")
130               || !strcmp( argn[i], "autostart") )
131         {
132             b_autoplay = boolValue(argv[i]);
133         }
134         else if( !strcmp( argn[i], "fullscreen" ) )
135         {
136             if( boolValue(argv[i]) )
137             {
138                 ppsz_argv[ppsz_argc++] = "--fullscreen";
139             }
140             else
141             {
142                 ppsz_argv[ppsz_argc++] = "--no-fullscreen";
143             }
144         }
145         else if( !strcmp( argn[i], "mute" ) )
146         {
147             if( boolValue(argv[i]) )
148             {
149                 ppsz_argv[ppsz_argc++] = "--volume";
150                 ppsz_argv[ppsz_argc++] = "0";
151             }
152         }
153         else if( !strcmp( argn[i], "loop")
154               || !strcmp( argn[i], "autoloop") )
155         {
156             if( boolValue(argv[i]) )
157             {
158                 ppsz_argv[ppsz_argc++] = "--loop";
159             }
160             else {
161                 ppsz_argv[ppsz_argc++] = "--no-loop";
162             }
163         }
164         else if( !strcmp( argn[i], "version")
165               || !strcmp( argn[i], "progid") )
166         {
167             progid = argv[i];
168         }
169         else if( !strcmp( argn[i], "toolbar" ) )
170         {
171             b_toolbar = boolValue(argv[i]);
172         }
173     }
174
175
176
177     libvlc_instance = libvlc_new(ppsz_argc, ppsz_argv, NULL);
178     if( ! libvlc_instance )
179     {
180         return NPERR_GENERIC_ERROR;
181     }
182
183     /*
184     ** fetch plugin base URL, which is the URL of the page containing the plugin
185     ** this URL is used for making absolute URL from relative URL that may be
186     ** passed as an MRL argument
187     */
188     NPObject *plugin;
189
190     if( NPERR_NO_ERROR == NPN_GetValue(p_browser, NPNVWindowNPObject, &plugin) )
191     {
192         /*
193         ** is there a better way to get that info ?
194         */
195         static const char docLocHref[] = "document.location.href";
196         NPString script;
197         NPVariant result;
198
199         script.utf8characters = docLocHref;
200         script.utf8length = sizeof(docLocHref)-1;
201
202         if( NPN_Evaluate(p_browser, plugin, &script, &result) )
203         {
204             if( NPVARIANT_IS_STRING(result) )
205             {
206                 NPString &location = NPVARIANT_TO_STRING(result);
207
208                 psz_baseURL = new char[location.utf8length+1];
209                 if( psz_baseURL )
210                 {
211                     strncpy(psz_baseURL, location.utf8characters, location.utf8length);
212                     psz_baseURL[location.utf8length] = '\0';
213                 }
214             }
215             NPN_ReleaseVariantValue(&result);
216         }
217         NPN_ReleaseObject(plugin);
218     }
219
220     if( psz_target )
221     {
222         // get absolute URL from src
223         char *psz_absurl = getAbsoluteURL(psz_target);
224         psz_target = psz_absurl ? psz_absurl : strdup(psz_target);
225     }
226
227     /* assign plugin script root class */
228     if( (NULL != progid) && (!strcmp(progid, "VideoLAN.VLCPlugin.2")) )
229     {
230         /* new APIs */
231         p_scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
232     }
233     else
234     {
235         /* legacy APIs */
236         p_scriptClass = RuntimeNPClass<VlcNPObject>::getClass();
237     }
238
239     return NPERR_NO_ERROR;
240 }
241
242 #if 0
243 #ifdef XP_WIN
244 /* This is really ugly but there is a deadlock when stopping a stream
245  * (in VLC_CleanUp()) because the video output is a child of the drawable but
246  * is in a different thread. */
247 static void HackStopVout( VlcPlugin* p_plugin )
248 {
249     MSG msg;
250     HWND hwnd;
251     vlc_value_t value;
252
253     int i_vlc = libvlc_get_vlc_id(p_plugin->libvlc_instance);
254     VLC_VariableGet( i_vlc, "drawable", &value );
255
256     hwnd = FindWindowEx( (HWND)value.i_int, 0, 0, 0 );
257     if( !hwnd ) return;
258
259     PostMessage( hwnd, WM_CLOSE, 0, 0 );
260
261     do
262     {
263         while( PeekMessage( &msg, (HWND)value.i_int, 0, 0, PM_REMOVE ) )
264         {
265             TranslateMessage(&msg);
266             DispatchMessage(&msg);
267         }
268         if( FindWindowEx( (HWND)value.i_int, 0, 0, 0 ) ) Sleep( 10 );
269     }
270     while( (hwnd = FindWindowEx( (HWND)value.i_int, 0, 0, 0 )) );
271 }
272 #endif /* XP_WIN */
273 #endif
274
275 VlcPlugin::~VlcPlugin()
276 {
277     delete psz_baseURL;
278     delete psz_target;
279     if( libvlc_log )
280         libvlc_log_close(libvlc_log, NULL);
281     if( libvlc_instance )
282         libvlc_release(libvlc_instance);
283 }
284
285 /*****************************************************************************
286  * VlcPlugin methods
287  *****************************************************************************/
288
289 char *VlcPlugin::getAbsoluteURL(const char *url)
290 {
291     if( NULL != url )
292     {
293         // check whether URL is already absolute
294         const char *end=strchr(url, ':');
295         if( (NULL != end) && (end != url) )
296         {
297             // validate protocol header
298             const char *start = url;
299             char c = *start;
300             if( isalpha(c) )
301             {
302                 ++start;
303                 while( start != end )
304                 {
305                     c  = *start;
306                     if( ! (isalnum(c)
307                        || ('-' == c)
308                        || ('+' == c)
309                        || ('.' == c)
310                        || ('/' == c)) ) /* VLC uses / to allow user to specify a demuxer */
311                         // not valid protocol header, assume relative URL
312                         goto relativeurl;
313                     ++start;
314                 }
315                 /* we have a protocol header, therefore URL is absolute */
316                 return strdup(url);
317             }
318             // not a valid protocol header, assume relative URL
319         }
320
321 relativeurl:
322
323         if( psz_baseURL )
324         {
325             size_t baseLen = strlen(psz_baseURL);
326             char *href = new char[baseLen+strlen(url)+1];
327             if( href )
328             {
329                 /* prepend base URL */
330                 strcpy(href, psz_baseURL);
331
332                 /*
333                 ** relative url could be empty,
334                 ** in which case return base URL
335                 */
336                 if( '\0' == *url )
337                     return href;
338
339                 /*
340                 ** locate pathname part of base URL
341                 */
342
343                 /* skip over protocol part  */
344                 char *pathstart = strchr(href, ':');
345                 char *pathend;
346                 if( pathstart )
347                 {
348                     if( '/' == *(++pathstart) )
349                     {
350                         if( '/' == *(++pathstart) )
351                         {
352                             ++pathstart;
353                         }
354                     }
355                     /* skip over host part */
356                     pathstart = strchr(pathstart, '/');
357                     pathend = href+baseLen;
358                     if( ! pathstart )
359                     {
360                         // no path, add a / past end of url (over '\0')
361                         pathstart = pathend;
362                         *pathstart = '/';
363                     }
364                 }
365                 else
366                 {
367                     /* baseURL is just a UNIX path */
368                     if( '/' != *href )
369                     {
370                         /* baseURL is not an absolute path */
371                         return NULL;
372                     }
373                     pathstart = href;
374                     pathend = href+baseLen;
375                 }
376
377                 /* relative URL made of an absolute path ? */
378                 if( '/' == *url )
379                 {
380                     /* replace path completely */
381                     strcpy(pathstart, url);
382                     return href;
383                 }
384
385                 /* find last path component and replace it */
386                 while( '/' != *pathend)
387                     --pathend;
388
389                 /*
390                 ** if relative url path starts with one or more '../',
391                 ** factor them out of href so that we return a
392                 ** normalized URL
393                 */
394                 while( pathend != pathstart )
395                 {
396                     const char *p = url;
397                     if( '.' != *p )
398                         break;
399                     ++p;
400                     if( '\0' == *p  )
401                     {
402                         /* relative url is just '.' */
403                         url = p;
404                         break;
405                     }
406                     if( '/' == *p  )
407                     {
408                         /* relative url starts with './' */
409                         url = ++p;
410                         continue;
411                     }
412                     if( '.' != *p )
413                         break;
414                     ++p;
415                     if( '\0' == *p )
416                     {
417                         /* relative url is '..' */
418                     }
419                     else
420                     {
421                         if( '/' != *p )
422                             break;
423                         /* relative url starts with '../' */
424                         ++p;
425                     }
426                     url = p;
427                     do
428                     {
429                         --pathend;
430                     }
431                     while( '/' != *pathend );
432                 }
433                 /* skip over '/' separator */
434                 ++pathend;
435                 /* concatenate remaining base URL and relative URL */
436                 strcpy(pathend, url);
437             }
438             return href;
439         }
440     }
441     return NULL;
442 }
443
444 #if XP_UNIX
445 int  VlcPlugin::setSize(unsigned width, unsigned height)
446 {
447     int diff = (width != i_width) || (height != i_height);
448
449     i_width = width;
450     i_height = height;
451
452     /* return size */
453     return diff;
454 }
455
456 void VlcPlugin::showToolbar()
457 {
458     const NPWindow& window = getWindow();
459     Window control = getControlWindow();
460     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
461     unsigned int i_height = 0, i_width = 0;
462
463     /* load icons */
464     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/play.xpm",
465                         &p_btnPlay, NULL, NULL);
466     if( p_btnPlay )
467     {
468         i_height = __MAX( i_height, p_btnPlay->height );
469         i_width  = __MAX( i_width,  p_btnPlay->width );
470     }
471     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/pause.xpm",
472                         &p_btnPause, NULL, NULL);
473     if( p_btnPause )
474     {
475         i_height = __MAX( i_height, p_btnPause->height );
476         i_width  = __MAX( i_width,  p_btnPause->width );
477     }
478     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/stop.xpm",
479                         &p_btnStop, NULL, NULL );
480     if( p_btnStop )
481     {
482         i_height = __MAX( i_height, p_btnStop->height );
483         i_width  = __MAX( i_width,  p_btnStop->width );
484     }
485     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_line.xpm",
486                         &p_timeline, NULL, NULL);
487     if( p_timeline )
488     {
489         i_height = __MAX( i_height, p_timeline->height );
490         i_width  = __MAX( i_width,  p_timeline->width );
491     }
492     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_icon.xpm",
493                         &p_btnTime, NULL, NULL);
494     if( p_btnTime )
495     {
496         i_height = __MAX( i_height, p_btnTime->height );
497         i_width  = __MAX( i_width,  p_btnTime->width );
498     }
499     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/fullscreen.xpm",
500                         &p_btnFullscreen, NULL, NULL);
501     if( p_btnFullscreen )
502     {
503         i_height = __MAX( i_height, p_btnFullscreen->height );
504         i_width  = __MAX( i_width,  p_btnFullscreen->width );
505     }
506     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_max.xpm",
507                         &p_btnMute, NULL, NULL);
508     if( p_btnMute )
509     {
510         i_height = __MAX( i_height, p_btnMute->height );
511         i_width  = __MAX( i_width,  p_btnMute->width );
512     }
513     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_mute.xpm",
514                         &p_btnUnmute, NULL, NULL);
515     if( p_btnUnmute )
516     {
517         i_height = __MAX( i_height, p_btnUnmute->height );
518         i_width  = __MAX( i_width,  p_btnUnmute->width );
519     }
520     setToolbarSize( i_width, i_height );
521
522     if( !p_btnPlay || !p_btnPause || !p_btnStop || !p_timeline ||
523         !p_btnTime || !p_btnFullscreen || !p_btnMute || !p_btnUnmute )
524         fprintf(stderr, "Error: some button images not found in %s\n", DATA_PATH );
525 }
526
527 void VlcPlugin::hideToolbar()
528 {
529     i_tb_width = i_tb_height = 0;
530
531     if( p_btnPlay )  XDestroyImage( p_btnPlay );
532     if( p_btnPause ) XDestroyImage( p_btnPause );
533     if( p_btnStop )  XDestroyImage( p_btnStop );
534     if( p_timeline ) XDestroyImage( p_timeline );
535     if( p_btnTime )  XDestroyImage( p_btnTime );
536     if( p_btnFullscreen ) XDestroyImage( p_btnFullscreen );
537     if( p_btnMute )  XDestroyImage( p_btnMute );
538     if( p_btnUnmute ) XDestroyImage( p_btnUnmute );
539
540     p_btnPlay = NULL;
541     p_btnPause = NULL;
542     p_btnStop = NULL;
543     p_timeline = NULL;
544     p_btnTime = NULL;
545     p_btnFullscreen = NULL;
546     p_btnMute = NULL;
547     p_btnUnmute = NULL;
548 }
549
550 void VlcPlugin::redrawToolbar()
551 {
552     libvlc_media_player_t *p_md = NULL;
553     libvlc_exception_t ex;
554     float f_position = 0.0;
555     int i_playing = 0;
556     bool b_mute = false;
557     unsigned int dst_x, dst_y;
558     GC gc;
559     XGCValues gcv;
560 #define BTN_SPACE ((unsigned int)4)
561
562     const NPWindow& window = getWindow();
563     Window control = getControlWindow();
564     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
565
566     /* get media instance */
567     libvlc_exception_init( &ex );
568     p_md = libvlc_playlist_get_media_player( getVLC(), &ex );
569     libvlc_exception_clear( &ex );
570
571     /* get isplaying */
572     libvlc_exception_init( &ex );
573     i_playing = libvlc_playlist_isplaying( getVLC(), &ex );
574     libvlc_exception_clear( &ex );
575
576     /* get mute info */
577     libvlc_exception_init(&ex);
578     b_mute = libvlc_audio_get_mute( getVLC(), &ex );
579     libvlc_exception_clear( &ex );
580
581     /* get movie position in % */
582     if( i_playing == 1 )
583     {
584         libvlc_exception_init( &ex );
585         f_position = libvlc_media_player_get_position( p_md, &ex ) * 100;
586         libvlc_exception_clear( &ex );
587     }
588     libvlc_media_player_release( p_md );
589
590     gcv.foreground = BlackPixel( p_display, 0 );
591     gc = XCreateGC( p_display, control, GCForeground, &gcv );
592
593     XFillRectangle( p_display, control, gc,
594                     0, 0, window.width, i_tb_height );
595     gcv.foreground = WhitePixel( p_display, 0 );
596     XChangeGC( p_display, gc, GCForeground, &gcv );
597
598     /* position icons */
599     dst_x = 4; dst_y = 4;
600
601     fprintf( stderr, ">>>>>> is playing = %d\n", i_playing );
602     if( p_btnPause && (i_playing == 1) )
603     {
604         XPutImage( p_display, control, gc, p_btnPause, 0, 0, dst_x, dst_y,
605                    p_btnPause->width, p_btnPause->height );
606     }
607     else if( p_btnPlay )
608     {
609         XPutImage( p_display, control, gc, p_btnPlay, 0, 0, dst_x, dst_y,
610                    p_btnPlay->width, p_btnPlay->height );
611     }
612
613     dst_x += BTN_SPACE + ( p_btnPlay ? p_btnPlay->width : 0 );
614     dst_y = 4;
615
616     if( p_btnStop )
617         XPutImage( p_display, control, gc, p_btnStop, 0, 0, dst_x, dst_y,
618                    p_btnStop->width, p_btnStop->height );
619
620     dst_x += BTN_SPACE + ( p_btnStop ? p_btnStop->width : 0 );
621     dst_y = 4;
622
623     if( p_btnFullscreen )
624         XPutImage( p_display, control, gc, p_btnFullscreen, 0, 0, dst_x, dst_y,
625                    p_btnFullscreen->width, p_btnFullscreen->height );
626
627     dst_x += BTN_SPACE + ( p_btnFullscreen ? p_btnFullscreen->width : 0 );
628     dst_y = 4;
629
630     if( p_btnUnmute && b_mute )
631     {
632         XPutImage( p_display, control, gc, p_btnUnmute, 0, 0, dst_x, dst_y,
633                    p_btnUnmute->width, p_btnUnmute->height );
634
635         dst_x += BTN_SPACE + ( p_btnUnmute ? p_btnUnmute->width : 0 );
636         dst_y = 4;
637     }
638     else if( p_btnMute )
639     {
640         XPutImage( p_display, control, gc, p_btnMute, 0, 0, dst_x, dst_y,
641                    p_btnMute->width, p_btnMute->height );
642
643         dst_x += BTN_SPACE + ( p_btnMute ? p_btnMute->width : 0 );
644         dst_y = 4;
645     }
646
647     if( p_timeline )
648         XPutImage( p_display, control, gc, p_timeline, 0, 0, dst_x, dst_y,
649                    (window.width-(dst_x+BTN_SPACE)), p_timeline->height );
650
651     if( f_position > 0 )
652         i_last_position = (((float)window.width-8.0)/100.0)*f_position;
653
654     if( p_btnTime )
655         XPutImage( p_display, control, gc, p_btnTime,
656                    0, 0, (dst_x+i_last_position), dst_y,
657                    p_btnTime->width, p_btnTime->height );
658
659     XFreeGC( p_display, gc );
660 }
661 #endif