1 /*****************************************************************************
2 * vlcplugin.cpp: a VLC plugin for Mozilla
3 *****************************************************************************
4 * Copyright (C) 2002-2005 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Damien Fouilleul <damienf.fouilleul@laposte.net>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
30 #ifdef HAVE_MOZILLA_CONFIG_H
31 # include <mozilla-config.h>
34 #include "vlcplugin.h"
35 #include "control/npovlc.h"
36 #include "control/npolibvlc.h"
40 /*****************************************************************************
41 * VlcPlugin constructor and destructor
42 *****************************************************************************/
43 VlcPlugin::VlcPlugin( NPP instance, uint16 mode ) :
48 libvlc_instance(NULL),
58 ,i_width((unsigned)-1)
59 ,i_height((unsigned)-1)
62 memset(&npwindow, 0, sizeof(NPWindow));
65 static bool boolValue(const char *value) {
66 return ( !strcmp(value, "1") ||
67 !strcasecmp(value, "true") ||
68 !strcasecmp(value, "yes") );
71 NPError VlcPlugin::init(int argc, char* const argn[], char* const argv[])
73 /* prepare VLC command line */
74 char *ppsz_argv[32] = { "vlc" };
77 /* locate VLC module path */
79 ppsz_argv[ppsz_argc++] = "--plugin-path";
80 ppsz_argv[ppsz_argc++] = "/Library/Internet Plug-Ins/VLC Plugin.plugin/"
81 "Contents/MacOS/modules";
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 )
89 if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
90 (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
92 if( i_type == REG_SZ )
94 strcat( p_data, "\\plugins" );
95 ppsz_argv[ppsz_argc++] = "--plugin-path";
96 ppsz_argv[ppsz_argc++] = p_data;
101 ppsz_argv[ppsz_argc++] = "--no-one-instance";
102 if( IsDebuggerPresent() )
105 ** VLC default threading mechanism is designed to be as compatible
106 ** with POSIX as possible. However when debugged on win32, threads
107 ** lose signals and eventually VLC get stuck during initialization.
108 ** threading support can be configured to be more debugging friendly
109 ** but it will be less compatible with POSIX.
110 ** This is done by initializing with the following options:
112 ppsz_argv[ppsz_argc++] = "--fast-mutex";
113 ppsz_argv[ppsz_argc++] = "--win9x-cv-method=1";
118 ppsz_argv[0] = "C:\\Cygwin\\home\\damienf\\vlc-trunk\\vlc";
121 #endif /* XP_MACOSX */
123 /* common settings */
124 ppsz_argv[ppsz_argc++] = "-vv";
125 ppsz_argv[ppsz_argc++] = "--no-stats";
126 ppsz_argv[ppsz_argc++] = "--no-media-library";
127 ppsz_argv[ppsz_argc++] = "--intf";
128 ppsz_argv[ppsz_argc++] = "dummy";
130 const char *progid = NULL;
132 /* parse plugin arguments */
133 for( int i = 0; i < argc ; i++ )
135 fprintf(stderr, "argn=%s, argv=%s\n", argn[i], argv[i]);
137 if( !strcmp( argn[i], "target" )
138 || !strcmp( argn[i], "mrl")
139 || !strcmp( argn[i], "filename")
140 || !strcmp( argn[i], "src") )
142 psz_target = argv[i];
144 else if( !strcmp( argn[i], "autoplay")
145 || !strcmp( argn[i], "autostart") )
147 b_autoplay = boolValue(argv[i]);
149 else if( !strcmp( argn[i], "fullscreen" ) )
151 if( boolValue(argv[i]) )
153 ppsz_argv[ppsz_argc++] = "--fullscreen";
157 ppsz_argv[ppsz_argc++] = "--no-fullscreen";
160 else if( !strcmp( argn[i], "mute" ) )
162 if( boolValue(argv[i]) )
164 ppsz_argv[ppsz_argc++] = "--volume";
165 ppsz_argv[ppsz_argc++] = "0";
168 else if( !strcmp( argn[i], "loop")
169 || !strcmp( argn[i], "autoloop") )
171 if( boolValue(argv[i]) )
173 ppsz_argv[ppsz_argc++] = "--loop";
176 ppsz_argv[ppsz_argc++] = "--no-loop";
179 else if( !strcmp( argn[i], "version")
180 || !strcmp( argn[i], "progid") )
186 libvlc_instance = libvlc_new(ppsz_argc, ppsz_argv, NULL);
187 if( ! libvlc_instance )
189 return NPERR_GENERIC_ERROR;
193 ** fetch plugin base URL, which is the URL of the page containing the plugin
194 ** this URL is used for making absolute URL from relative URL that may be
195 ** passed as an MRL argument
199 if( NPERR_NO_ERROR == NPN_GetValue(p_browser, NPNVWindowNPObject, &plugin) )
202 ** is there a better way to get that info ?
204 static const char docLocHref[] = "document.location.href";
208 script.utf8characters = docLocHref;
209 script.utf8length = sizeof(docLocHref)-1;
211 if( NPN_Evaluate(p_browser, plugin, &script, &result) )
213 if( NPVARIANT_IS_STRING(result) )
215 NPString &location = NPVARIANT_TO_STRING(result);
217 psz_baseURL = new char[location.utf8length+1];
220 strncpy(psz_baseURL, location.utf8characters, location.utf8length);
221 psz_baseURL[location.utf8length] = '\0';
224 NPN_ReleaseVariantValue(&result);
226 NPN_ReleaseObject(plugin);
231 // get absolute URL from src
232 char *psz_absurl = getAbsoluteURL(psz_target);
233 psz_target = psz_absurl ? psz_absurl : strdup(psz_target);
236 /* assign plugin script root class */
237 if( (NULL != progid) && (!strcmp(progid, "VideoLAN.VLCPlugin.2")) )
240 p_scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
245 p_scriptClass = RuntimeNPClass<VlcNPObject>::getClass();
248 return NPERR_NO_ERROR;
253 /* This is really ugly but there is a deadlock when stopping a stream
254 * (in VLC_CleanUp()) because the video output is a child of the drawable but
255 * is in a different thread. */
256 static void HackStopVout( VlcPlugin* p_plugin )
262 int i_vlc = libvlc_get_vlc_id(p_plugin->libvlc_instance);
263 VLC_VariableGet( i_vlc, "drawable", &value );
265 hwnd = FindWindowEx( (HWND)value.i_int, 0, 0, 0 );
268 PostMessage( hwnd, WM_CLOSE, 0, 0 );
272 while( PeekMessage( &msg, (HWND)value.i_int, 0, 0, PM_REMOVE ) )
274 TranslateMessage(&msg);
275 DispatchMessage(&msg);
277 if( FindWindowEx( (HWND)value.i_int, 0, 0, 0 ) ) Sleep( 10 );
279 while( (hwnd = FindWindowEx( (HWND)value.i_int, 0, 0, 0 )) );
284 VlcPlugin::~VlcPlugin()
289 NPN_ReleaseObject(p_scriptObject);
291 libvlc_log_close(libvlc_log, NULL);
292 if( libvlc_instance )
293 libvlc_destroy(libvlc_instance, NULL );
296 /*****************************************************************************
298 *****************************************************************************/
300 char *VlcPlugin::getAbsoluteURL(const char *url)
304 // check whether URL is already absolute
305 const char *end=strchr(url, ':');
306 if( (NULL != end) && (end != url) )
308 // validate protocol header
309 const char *start = url;
310 while( start != end ) {
311 char c = tolower(*start);
312 if( (c < 'a') || (c > 'z') )
313 // not valid protocol header, assume relative URL
317 /* we have a protocol header, therefore URL is absolute */
325 size_t baseLen = strlen(psz_baseURL);
326 char *href = new char[baseLen+strlen(url)+1];
329 /* prepend base URL */
330 strcpy(href, psz_baseURL);
333 ** relative url could be empty,
334 ** in which case return base URL
340 ** locate pathname part of base URL
343 /* skip over protocol part */
344 char *pathstart = strchr(href, ':');
348 if( '/' == *(++pathstart) )
350 if( '/' == *(++pathstart) )
355 /* skip over host part */
356 pathstart = strchr(pathstart, '/');
357 pathend = href+baseLen;
360 // no path, add a / past end of url (over '\0')
367 /* baseURL is just a UNIX path */
370 /* baseURL is not an absolute path */
374 pathend = href+baseLen;
377 /* relative URL made of an absolute path ? */
380 /* replace path completely */
381 strcpy(pathstart, url);
385 /* find last path component and replace it */
386 while( '/' != *pathend)
390 ** if relative url path starts with one or more '../',
391 ** factor them out of href so that we return a
394 while( pathend != pathstart )
402 /* relative url is just '.' */
408 /* relative url starts with './' */
417 /* relative url is '..' */
423 /* relative url starts with '../' */
431 while( '/' != *pathend );
433 /* skip over '/' separator */
435 /* concatenate remaining base URL and relative URL */
436 strcpy(pathend, url);
444 NPObject* VlcPlugin::getScriptObject()
446 if( NULL == p_scriptObject )
448 p_scriptObject = NPN_CreateObject(p_browser, p_scriptClass);
450 return NPN_RetainObject(p_scriptObject);
454 int VlcPlugin::setSize(unsigned width, unsigned height)
456 int diff = (width != i_width) || (height != i_height);