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"
38 /*****************************************************************************
39 * VlcPlugin constructor and destructor
40 *****************************************************************************/
41 VlcPlugin::VlcPlugin( NPP instance, uint16 mode ) :
46 libvlc_instance(NULL),
54 ,i_width((unsigned)-1)
55 ,i_height((unsigned)-1)
58 memset(&npwindow, 0, sizeof(NPWindow));
61 static int boolValue(const char *value) {
62 return ( !strcmp(value, "1") ||
63 !strcasecmp(value, "true") ||
64 !strcasecmp(value, "yes") );
67 NPError VlcPlugin::init(int argc, char* const argn[], char* const argv[])
69 /* prepare VLC command line */
70 char *ppsz_argv[32] = { "vlc" };
73 /* locate VLC module path */
75 ppsz_argv[ppsz_argc++] = "--plugin-path";
76 ppsz_argv[ppsz_argc++] = "/Library/Internet Plug-Ins/VLC Plugin.plugin/"
77 "Contents/MacOS/modules";
80 DWORD i_type, i_data = MAX_PATH + 1;
81 char p_data[MAX_PATH + 1];
82 if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\VideoLAN\\VLC",
83 0, KEY_READ, &h_key ) == ERROR_SUCCESS )
85 if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
86 (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
88 if( i_type == REG_SZ )
90 strcat( p_data, "\\plugins" );
91 ppsz_argv[ppsz_argc++] = "--plugin-path";
92 ppsz_argv[ppsz_argc++] = p_data;
97 ppsz_argv[ppsz_argc++] = "--no-one-instance";
98 if( IsDebuggerPresent() )
101 ** VLC default threading mechanism is designed to be as compatible
102 ** with POSIX as possible. However when debugged on win32, threads
103 ** lose signals and eventually VLC get stuck during initialization.
104 ** threading support can be configured to be more debugging friendly
105 ** but it will be less compatible with POSIX.
106 ** This is done by initializing with the following options:
108 ppsz_argv[ppsz_argc++] = "--fast-mutex";
109 ppsz_argv[ppsz_argc++] = "--win9x-cv-method=1";
114 ppsz_argv[0] = "C:\\Cygwin\\home\\damienf\\vlc-trunk\\vlc";
117 #endif /* XP_MACOSX */
119 /* common settings */
120 ppsz_argv[ppsz_argc++] = "-vv";
121 ppsz_argv[ppsz_argc++] = "--no-stats";
122 ppsz_argv[ppsz_argc++] = "--no-media-library";
123 ppsz_argv[ppsz_argc++] = "--intf";
124 ppsz_argv[ppsz_argc++] = "dummy";
126 const char *version = NULL;
128 /* parse plugin arguments */
129 for( int i = 0; i < argc ; i++ )
131 fprintf(stderr, "argn=%s, argv=%s\n", argn[i], argv[i]);
133 if( !strcmp( argn[i], "target" )
134 || !strcmp( argn[i], "mrl")
135 || !strcmp( argn[i], "filename")
136 || !strcmp( argn[i], "src") )
138 psz_target = argv[i];
140 else if( !strcmp( argn[i], "autoplay")
141 || !strcmp( argn[i], "autostart") )
143 b_autoplay = boolValue(argv[i]);
145 else if( !strcmp( argn[i], "fullscreen" ) )
147 if( boolValue(argv[i]) )
149 ppsz_argv[ppsz_argc++] = "--fullscreen";
153 ppsz_argv[ppsz_argc++] = "--no-fullscreen";
156 else if( !strcmp( argn[i], "mute" ) )
158 if( boolValue(argv[i]) )
160 ppsz_argv[ppsz_argc++] = "--volume";
161 ppsz_argv[ppsz_argc++] = "0";
164 else if( !strcmp( argn[i], "loop")
165 || !strcmp( argn[i], "autoloop") )
167 if( boolValue(argv[i]) )
169 ppsz_argv[ppsz_argc++] = "--loop";
172 ppsz_argv[ppsz_argc++] = "--no-loop";
175 else if( !strcmp( argn[i], "version") )
181 libvlc_instance = libvlc_new(ppsz_argc, ppsz_argv, NULL);
182 if( ! libvlc_instance )
184 return NPERR_GENERIC_ERROR;
188 ** fetch plugin base URL, which is the URL of the page containing the plugin
189 ** this URL is used for making absolute URL from relative URL that may be
190 ** passed as an MRL argument
194 if( NPERR_NO_ERROR == NPN_GetValue(p_browser, NPNVWindowNPObject, &plugin) )
197 ** is there a better way to get that info ?
199 static const char docLocHref[] = "document.location.href";
203 script.utf8characters = docLocHref;
204 script.utf8length = sizeof(docLocHref)-1;
206 if( NPN_Evaluate(p_browser, plugin, &script, &result) )
208 if( NPVARIANT_IS_STRING(result) )
210 NPString &location = NPVARIANT_TO_STRING(result);
212 psz_baseURL = new char[location.utf8length+1];
215 strncpy(psz_baseURL, location.utf8characters, location.utf8length);
216 psz_baseURL[location.utf8length] = '\0';
219 NPN_ReleaseVariantValue(&result);
221 NPN_ReleaseObject(plugin);
226 // get absolute URL from src
227 psz_target = getAbsoluteURL(psz_target);
230 /* assign plugin script root class */
231 if( (NULL != version) && (!strcmp(version, "VideoLAN.VLCPlugin.2")) )
234 scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
239 scriptClass = RuntimeNPClass<VlcNPObject>::getClass();
242 return NPERR_NO_ERROR;
247 /* This is really ugly but there is a deadlock when stopping a stream
248 * (in VLC_CleanUp()) because the video output is a child of the drawable but
249 * is in a different thread. */
250 static void HackStopVout( VlcPlugin* p_plugin )
256 int i_vlc = libvlc_get_vlc_id(p_plugin->libvlc_instance);
257 VLC_VariableGet( i_vlc, "drawable", &value );
259 hwnd = FindWindowEx( (HWND)value.i_int, 0, 0, 0 );
262 PostMessage( hwnd, WM_CLOSE, 0, 0 );
266 while( PeekMessage( &msg, (HWND)value.i_int, 0, 0, PM_REMOVE ) )
268 TranslateMessage(&msg);
269 DispatchMessage(&msg);
271 if( FindWindowEx( (HWND)value.i_int, 0, 0, 0 ) ) Sleep( 10 );
273 while( (hwnd = FindWindowEx( (HWND)value.i_int, 0, 0, 0 )) );
278 VlcPlugin::~VlcPlugin()
282 if( libvlc_instance )
283 libvlc_destroy(libvlc_instance, NULL );
286 /*****************************************************************************
288 *****************************************************************************/
290 char *VlcPlugin::getAbsoluteURL(const char *url)
294 // check whether URL is already absolute
295 const char *end=strchr(url, ':');
296 if( (NULL != end) && (end != url) )
298 // validate protocol header
299 const char *start = url;
300 while( start != end ) {
301 char c = *start | 0x20;
302 if( (c < 'a') || (c > 'z') )
303 // not valid protocol header, assume relative URL
307 /* we have a protocol header, therefore URL is absolute */
313 size_t baseLen = strlen(psz_baseURL);
314 char *href = new char[baseLen+strlen(url)];
317 /* prepend base URL */
318 strcpy(href, psz_baseURL);
321 ** relative url could be empty,
322 ** in which case return base URL
328 ** locate pathname part of base URL
331 /* skip over protocol part */
332 char *pathstart = strchr(href, ':');
334 if( '/' == *(++pathstart) )
336 if( '/' == *(++pathstart) )
341 /* skip over host part */
342 pathstart = strchr(pathstart, '/');
343 pathend = href+baseLen;
346 // no path, add a / past end of url (over '\0')
351 /* relative URL made of an absolute path ? */
354 /* replace path completely */
355 strcpy(pathstart, url);
359 /* find last path component and replace it */
360 while( '/' != *pathend) --pathend;
363 ** if relative url path starts with one or more '../',
364 ** factor them out of href so that we return a
367 while( pathend != pathstart )
380 while( '/' != *pathend ) --pathend;
382 /* concatenate remaining base URL and relative URL */
383 strcpy(pathend+1, url);
392 int VlcPlugin::setSize(unsigned width, unsigned height)
394 int diff = (width != i_width) || (height != i_height);