]> git.sesse.net Git - vlc/blob - projects/mozilla/vlcplugin.cpp
Cleanup and fix crash when plugin wasn't playing.
[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     i_control_height(45),
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 #endif
61 {
62     memset(&npwindow, 0, sizeof(NPWindow));
63 }
64
65 static bool boolValue(const char *value) {
66     return ( !strcmp(value, "1") ||
67              !strcasecmp(value, "true") ||
68              !strcasecmp(value, "yes") );
69 }
70
71 NPError VlcPlugin::init(int argc, char* const argn[], char* const argv[])
72 {
73     /* prepare VLC command line */
74     char *ppsz_argv[32];
75     int ppsz_argc = 0;
76
77     /* locate VLC module path */
78 #ifdef XP_MACOSX
79     ppsz_argv[ppsz_argc++] = "--plugin-path";
80     ppsz_argv[ppsz_argc++] = "/Library/Internet Plug-Ins/VLC Plugin.plugin/"
81                              "Contents/MacOS/modules";
82 #elif defined(XP_WIN)
83     HKEY h_key;
84     DWORD i_type, i_data = MAX_PATH + 1;
85     char p_data[MAX_PATH + 1];
86     if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\VideoLAN\\VLC",
87                       0, KEY_READ, &h_key ) == ERROR_SUCCESS )
88     {
89          if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
90                               (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
91          {
92              if( i_type == REG_SZ )
93              {
94                  strcat( p_data, "\\plugins" );
95                  ppsz_argv[ppsz_argc++] = "--plugin-path";
96                  ppsz_argv[ppsz_argc++] = p_data;
97              }
98          }
99          RegCloseKey( h_key );
100     }
101     ppsz_argv[ppsz_argc++] = "--no-one-instance";
102
103 #endif /* XP_MACOSX */
104
105     /* common settings */
106     ppsz_argv[ppsz_argc++] = "-vv";
107     ppsz_argv[ppsz_argc++] = "--no-stats";
108     ppsz_argv[ppsz_argc++] = "--no-media-library";
109     ppsz_argv[ppsz_argc++] = "--intf";
110     ppsz_argv[ppsz_argc++] = "dummy";
111
112     const char *progid = NULL;
113
114     /* parse plugin arguments */
115     for( int i = 0; i < argc ; i++ )
116     {
117         fprintf(stderr, "argn=%s, argv=%s\n", argn[i], argv[i]);
118
119         if( !strcmp( argn[i], "target" )
120          || !strcmp( argn[i], "mrl")
121          || !strcmp( argn[i], "filename")
122          || !strcmp( argn[i], "src") )
123         {
124             psz_target = argv[i];
125         }
126         else if( !strcmp( argn[i], "autoplay")
127               || !strcmp( argn[i], "autostart") )
128         {
129             b_autoplay = boolValue(argv[i]);
130         }
131         else if( !strcmp( argn[i], "fullscreen" ) )
132         {
133             if( boolValue(argv[i]) )
134             {
135                 ppsz_argv[ppsz_argc++] = "--fullscreen";
136             }
137             else
138             {
139                 ppsz_argv[ppsz_argc++] = "--no-fullscreen";
140             }
141         }
142         else if( !strcmp( argn[i], "mute" ) )
143         {
144             if( boolValue(argv[i]) )
145             {
146                 ppsz_argv[ppsz_argc++] = "--volume";
147                 ppsz_argv[ppsz_argc++] = "0";
148             }
149         }
150         else if( !strcmp( argn[i], "loop")
151               || !strcmp( argn[i], "autoloop") )
152         {
153             if( boolValue(argv[i]) )
154             {
155                 ppsz_argv[ppsz_argc++] = "--loop";
156             }
157             else {
158                 ppsz_argv[ppsz_argc++] = "--no-loop";
159             }
160         }
161         else if( !strcmp( argn[i], "version")
162               || !strcmp( argn[i], "progid") )
163         {
164             progid = argv[i];
165         }
166     }
167
168     libvlc_instance = libvlc_new(ppsz_argc, ppsz_argv, NULL);
169     if( ! libvlc_instance )
170     {
171         return NPERR_GENERIC_ERROR;
172     }
173
174     /*
175     ** fetch plugin base URL, which is the URL of the page containing the plugin
176     ** this URL is used for making absolute URL from relative URL that may be
177     ** passed as an MRL argument
178     */
179     NPObject *plugin;
180
181     if( NPERR_NO_ERROR == NPN_GetValue(p_browser, NPNVWindowNPObject, &plugin) )
182     {
183         /*
184         ** is there a better way to get that info ?
185         */
186         static const char docLocHref[] = "document.location.href";
187         NPString script;
188         NPVariant result;
189
190         script.utf8characters = docLocHref;
191         script.utf8length = sizeof(docLocHref)-1;
192
193         if( NPN_Evaluate(p_browser, plugin, &script, &result) )
194         {
195             if( NPVARIANT_IS_STRING(result) )
196             {
197                 NPString &location = NPVARIANT_TO_STRING(result);
198
199                 psz_baseURL = new char[location.utf8length+1];
200                 if( psz_baseURL )
201                 {
202                     strncpy(psz_baseURL, location.utf8characters, location.utf8length);
203                     psz_baseURL[location.utf8length] = '\0';
204                 }
205             }
206             NPN_ReleaseVariantValue(&result);
207         }
208         NPN_ReleaseObject(plugin);
209     }
210
211     if( psz_target )
212     {
213         // get absolute URL from src
214         char *psz_absurl = getAbsoluteURL(psz_target);
215         psz_target = psz_absurl ? psz_absurl : strdup(psz_target);
216     }
217
218     /* assign plugin script root class */
219     if( (NULL != progid) && (!strcmp(progid, "VideoLAN.VLCPlugin.2")) )
220     {
221         /* new APIs */
222         p_scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
223     }
224     else
225     {
226         /* legacy APIs */
227         p_scriptClass = RuntimeNPClass<VlcNPObject>::getClass();
228     }
229
230     return NPERR_NO_ERROR;
231 }
232
233 #if 0
234 #ifdef XP_WIN
235 /* This is really ugly but there is a deadlock when stopping a stream
236  * (in VLC_CleanUp()) because the video output is a child of the drawable but
237  * is in a different thread. */
238 static void HackStopVout( VlcPlugin* p_plugin )
239 {
240     MSG msg;
241     HWND hwnd;
242     vlc_value_t value;
243
244     int i_vlc = libvlc_get_vlc_id(p_plugin->libvlc_instance);
245     VLC_VariableGet( i_vlc, "drawable", &value );
246
247     hwnd = FindWindowEx( (HWND)value.i_int, 0, 0, 0 );
248     if( !hwnd ) return;
249
250     PostMessage( hwnd, WM_CLOSE, 0, 0 );
251
252     do
253     {
254         while( PeekMessage( &msg, (HWND)value.i_int, 0, 0, PM_REMOVE ) )
255         {
256             TranslateMessage(&msg);
257             DispatchMessage(&msg);
258         }
259         if( FindWindowEx( (HWND)value.i_int, 0, 0, 0 ) ) Sleep( 10 );
260     }
261     while( (hwnd = FindWindowEx( (HWND)value.i_int, 0, 0, 0 )) );
262 }
263 #endif /* XP_WIN */
264 #endif
265
266 VlcPlugin::~VlcPlugin()
267 {
268     delete psz_baseURL;
269     delete psz_target;
270     if( libvlc_log )
271         libvlc_log_close(libvlc_log, NULL);
272     if( libvlc_instance )
273         libvlc_release(libvlc_instance);
274 }
275
276 /*****************************************************************************
277  * VlcPlugin methods
278  *****************************************************************************/
279
280 char *VlcPlugin::getAbsoluteURL(const char *url)
281 {
282     if( NULL != url )
283     {
284         // check whether URL is already absolute
285         const char *end=strchr(url, ':');
286         if( (NULL != end) && (end != url) )
287         {
288             // validate protocol header
289             const char *start = url;
290             char c = *start;
291             if( isalpha(c) )
292             {
293                 ++start;
294                 while( start != end )
295                 {
296                     c  = *start;
297                     if( ! (isalnum(c)
298                        || ('-' == c)
299                        || ('+' == c)
300                        || ('.' == c)
301                        || ('/' == c)) ) /* VLC uses / to allow user to specify a demuxer */
302                         // not valid protocol header, assume relative URL
303                         goto relativeurl;
304                     ++start;
305                 }
306                 /* we have a protocol header, therefore URL is absolute */
307                 return strdup(url);
308             }
309             // not a valid protocol header, assume relative URL
310         }
311
312 relativeurl:
313
314         if( psz_baseURL )
315         {
316             size_t baseLen = strlen(psz_baseURL);
317             char *href = new char[baseLen+strlen(url)+1];
318             if( href )
319             {
320                 /* prepend base URL */
321                 strcpy(href, psz_baseURL);
322
323                 /*
324                 ** relative url could be empty,
325                 ** in which case return base URL
326                 */
327                 if( '\0' == *url )
328                     return href;
329
330                 /*
331                 ** locate pathname part of base URL
332                 */
333
334                 /* skip over protocol part  */
335                 char *pathstart = strchr(href, ':');
336                 char *pathend;
337                 if( pathstart )
338                 {
339                     if( '/' == *(++pathstart) )
340                     {
341                         if( '/' == *(++pathstart) )
342                         {
343                             ++pathstart;
344                         }
345                     }
346                     /* skip over host part */
347                     pathstart = strchr(pathstart, '/');
348                     pathend = href+baseLen;
349                     if( ! pathstart )
350                     {
351                         // no path, add a / past end of url (over '\0')
352                         pathstart = pathend;
353                         *pathstart = '/';
354                     }
355                 }
356                 else
357                 {
358                     /* baseURL is just a UNIX path */
359                     if( '/' != *href )
360                     {
361                         /* baseURL is not an absolute path */
362                         return NULL;
363                     }
364                     pathstart = href;
365                     pathend = href+baseLen;
366                 }
367
368                 /* relative URL made of an absolute path ? */
369                 if( '/' == *url )
370                 {
371                     /* replace path completely */
372                     strcpy(pathstart, url);
373                     return href;
374                 }
375
376                 /* find last path component and replace it */
377                 while( '/' != *pathend)
378                     --pathend;
379
380                 /*
381                 ** if relative url path starts with one or more '../',
382                 ** factor them out of href so that we return a
383                 ** normalized URL
384                 */
385                 while( pathend != pathstart )
386                 {
387                     const char *p = url;
388                     if( '.' != *p )
389                         break;
390                     ++p;
391                     if( '\0' == *p  )
392                     {
393                         /* relative url is just '.' */
394                         url = p;
395                         break;
396                     }
397                     if( '/' == *p  )
398                     {
399                         /* relative url starts with './' */
400                         url = ++p;
401                         continue;
402                     }
403                     if( '.' != *p )
404                         break;
405                     ++p;
406                     if( '\0' == *p )
407                     {
408                         /* relative url is '..' */
409                     }
410                     else
411                     {
412                         if( '/' != *p )
413                             break;
414                         /* relative url starts with '../' */
415                         ++p;
416                     }
417                     url = p;
418                     do
419                     {
420                         --pathend;
421                     }
422                     while( '/' != *pathend );
423                 }
424                 /* skip over '/' separator */
425                 ++pathend;
426                 /* concatenate remaining base URL and relative URL */
427                 strcpy(pathend, url);
428             }
429             return href;
430         }
431     }
432     return NULL;
433 }
434
435 #if XP_UNIX
436 int  VlcPlugin::setSize(unsigned width, unsigned height)
437 {
438     int diff = (width != i_width) || (height != i_height);
439
440     i_width = width;
441     i_height = height;
442
443     /* return size */
444     return diff;
445 }
446 #endif
447