vlcplugin_base.cpp 15.7 KB
Newer Older
1
/*****************************************************************************
Cheng Sun's avatar
Cheng Sun committed
2
 * vlcplugin_base.cpp: a VLC plugin for Mozilla
3
 *****************************************************************************
Jean-Paul Saman's avatar
Jean-Paul Saman committed
4
 * Copyright (C) 2002-2010 the VideoLAN team
5
 * $Id$
6 7
 *
 * Authors: Samuel Hocevar <sam@zoy.org>
8
 *          Damien Fouilleul <damienf.fouilleul@laposte.net>
9
 *          Jean-Paul Saman <jpsaman@videolan.org>
10
 *          Sergey Radionov <rsatom@gmail.com>
Cheng Sun's avatar
Cheng Sun committed
11
 *          Cheng Sun <chengsun9@gmail.com>
12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 27 28 29 30
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
31 32 33 34
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

Ludovic Fauvet's avatar
Ludovic Fauvet committed
35
#include "vlcplugin_base.h"
36
#include "vlcplugin.h"
Jean-Paul Saman's avatar
Jean-Paul Saman committed
37

38
#include "control/npolibvlc.h"
39

40
#include <cctype>
41

42 43 44
#include <cstdio>
#include <cstdlib>
#include <cstring>
45

Jean-Paul Saman's avatar
Jean-Paul Saman committed
46
/*****************************************************************************
47
 * VlcPluginBase constructor and destructor
Jean-Paul Saman's avatar
Jean-Paul Saman committed
48
 *****************************************************************************/
49
VlcPluginBase::VlcPluginBase( NPP instance, NPuint16_t mode ) :
Jean-Paul Saman's avatar
Jean-Paul Saman committed
50 51 52 53 54 55 56
    i_npmode(mode),
    b_stream(0),
    psz_target(NULL),
    libvlc_instance(NULL),
    p_scriptClass(NULL),
    p_browser(instance),
    psz_baseURL(NULL)
57
{
Jean-Paul Saman's avatar
Jean-Paul Saman committed
58
    memset(&npwindow, 0, sizeof(NPWindow));
59
    _instances.insert(this);
60 61
}

Jean-Paul Saman's avatar
Jean-Paul Saman committed
62
static bool boolValue(const char *value) {
Ludovic Fauvet's avatar
Ludovic Fauvet committed
63
    return ( *value == '\0' ||
64
             !strcmp(value, "1") ||
Jean-Paul Saman's avatar
Jean-Paul Saman committed
65 66 67
             !strcasecmp(value, "true") ||
             !strcasecmp(value, "yes") );
}
68

69 70
std::set<VlcPluginBase*> VlcPluginBase::_instances;

71
void VlcPluginBase::eventAsync(void *param)
72
{
73
    VlcPluginBase *plugin = (VlcPluginBase*)param;
74
    if( _instances.find(plugin) == _instances.end() )
75 76
        return;

Jean-Paul Saman's avatar
Jean-Paul Saman committed
77
    plugin->events.deliver(plugin->getBrowser());
78
    plugin->update_controls();
79 80
}

81 82
void VlcPluginBase::event_callback(const libvlc_event_t* event,
                NPVariant *npparams, uint32_t npcount)
Jean-Paul Saman's avatar
Jean-Paul Saman committed
83
{
84
#if defined(XP_UNIX) || defined(XP_WIN) || defined (XP_MACOSX)
85 86
    events.callback(event, npparams, npcount);
    NPN_PluginThreadAsyncCall(getBrowser(), eventAsync, this);
Jean-Paul Saman's avatar
Jean-Paul Saman committed
87
#else
Cheng Sun's avatar
Cheng Sun committed
88
#   warning NPN_PluginThreadAsyncCall not implemented yet.
Jean-Paul Saman's avatar
Jean-Paul Saman committed
89 90 91
    printf("No NPN_PluginThreadAsyncCall(), doing nothing.\n");
#endif
}
92

93
NPError VlcPluginBase::init(int argc, char* const argn[], char* const argv[])
94
{
95
    /* prepare VLC command line */
96
    const char *ppsz_argv[MAX_PARAMS];
97
    int ppsz_argc = 0;
98

99 100 101 102
#ifndef NDEBUG
    ppsz_argv[ppsz_argc++] = "--no-plugins-cache";
#endif

103
    /* locate VLC module path */
Felix Paul Kühne's avatar
Felix Paul Kühne committed
104
#ifdef XP_WIN
105 106 107 108 109
    HKEY h_key;
    DWORD i_type, i_data = MAX_PATH + 1;
    char p_data[MAX_PATH + 1];
    if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\VideoLAN\\VLC",
                      0, KEY_READ, &h_key ) == ERROR_SUCCESS )
110
    {
111 112 113 114 115
         if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
                              (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
         {
             if( i_type == REG_SZ )
             {
116
                 strcat( p_data, "\\plugins" );
117 118
                 ppsz_argv[ppsz_argc++] = "--plugin-path";
                 ppsz_argv[ppsz_argc++] = p_data;
119 120 121
             }
         }
         RegCloseKey( h_key );
122
    }
123
    ppsz_argv[ppsz_argc++] = "--no-one-instance";
124

125 126
#endif
#ifdef XP_MACOSX
127
    ppsz_argv[ppsz_argc++] = "--vout=vout_coregraphicslayer";
Felix Paul Kühne's avatar
Felix Paul Kühne committed
128
#endif
129

130 131 132 133
    /* common settings */
    ppsz_argv[ppsz_argc++] = "-vv";
    ppsz_argv[ppsz_argc++] = "--no-stats";
    ppsz_argv[ppsz_argc++] = "--no-media-library";
134
    ppsz_argv[ppsz_argc++] = "--intf=dummy";
135
    ppsz_argv[ppsz_argc++] = "--no-video-title-show";
Christophe Mutricy's avatar
Typo  
Christophe Mutricy committed
136
    ppsz_argv[ppsz_argc++] = "--no-xlib";
137

138 139
    bool b_autoloop = false;

140
    /* parse plugin arguments */
141
    for( int i = 0; (i < argc) && (ppsz_argc < MAX_PARAMS); i++ )
142
    {
143
       /* fprintf(stderr, "argn=%s, argv=%s\n", argn[i], argv[i]); */
144 145 146 147 148 149 150 151

        if( !strcmp( argn[i], "target" )
         || !strcmp( argn[i], "mrl")
         || !strcmp( argn[i], "filename")
         || !strcmp( argn[i], "src") )
        {
            psz_target = argv[i];
        }
152 153
        else if( !strcmp( argn[i], "text" ) )
        {
154
            set_bg_text( argv[i] );
155
        }
156 157 158
        else if( !strcmp( argn[i], "autoplay")
              || !strcmp( argn[i], "autostart") )
        {
159
            set_autoplay(boolValue(argv[i]));
160
        }
161 162
        else if( !strcmp( argn[i], "fullscreen" )
              || !strcmp( argn[i], "allowfullscreen" ) )
163
        {
164
            set_enable_fs( boolValue(argv[i]) );
165 166 167 168 169
        }
        else if( !strcmp( argn[i], "mute" ) )
        {
            if( boolValue(argv[i]) )
            {
170
                ppsz_argv[ppsz_argc++] = "--volume=0";
171 172 173 174 175
            }
        }
        else if( !strcmp( argn[i], "loop")
              || !strcmp( argn[i], "autoloop") )
        {
176
            b_autoloop = boolValue(argv[i]);
177
        }
178 179
        else if( !strcmp( argn[i], "toolbar" )
              || !strcmp( argn[i], "controls") )
180
        {
181
            set_show_toolbar( boolValue(argv[i]) );
182
        }
183 184
        else if( !strcmp( argn[i], "bgcolor" ) )
        {
185
            set_bg_color( argv[i] );
186
        }
187 188
    }

Rémi Duraffort's avatar
Rémi Duraffort committed
189
    libvlc_instance = libvlc_new(ppsz_argc, ppsz_argv);
190 191
    if( !libvlc_instance )
        return NPERR_GENERIC_ERROR;
192 193

    vlc_player::open(libvlc_instance);
194

195 196 197
    vlc_player::set_mode(b_autoloop ? libvlc_playback_mode_loop :
                                      libvlc_playback_mode_default);

198 199 200 201 202
    /*
    ** fetch plugin base URL, which is the URL of the page containing the plugin
    ** this URL is used for making absolute URL from relative URL that may be
    ** passed as an MRL argument
    */
Jean-Paul Saman's avatar
Jean-Paul Saman committed
203
    NPObject *plugin = NULL;
204 205 206 207 208 209 210 211 212 213

    if( NPERR_NO_ERROR == NPN_GetValue(p_browser, NPNVWindowNPObject, &plugin) )
    {
        /*
        ** is there a better way to get that info ?
        */
        static const char docLocHref[] = "document.location.href";
        NPString script;
        NPVariant result;

214 215
        script.UTF8Characters = docLocHref;
        script.UTF8Length = sizeof(docLocHref)-1;
216 217 218 219 220 221 222

        if( NPN_Evaluate(p_browser, plugin, &script, &result) )
        {
            if( NPVARIANT_IS_STRING(result) )
            {
                NPString &location = NPVARIANT_TO_STRING(result);

223
                psz_baseURL = (char *) malloc(location.UTF8Length+1);
224 225
                if( psz_baseURL )
                {
226 227
                    strncpy(psz_baseURL, location.UTF8Characters, location.UTF8Length);
                    psz_baseURL[location.UTF8Length] = '\0';
228 229 230 231 232 233 234 235 236 237
                }
            }
            NPN_ReleaseVariantValue(&result);
        }
        NPN_ReleaseObject(plugin);
    }

    if( psz_target )
    {
        // get absolute URL from src
238 239
        char *psz_absurl = getAbsoluteURL(psz_target);
        psz_target = psz_absurl ? psz_absurl : strdup(psz_target);
240 241 242
    }

    /* assign plugin script root class */
243 244
    /* new APIs */
    p_scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
245

246
    if( !events.init() )
247 248
        return NPERR_GENERIC_ERROR;

249 250 251 252 253 254
    libvlc_media_player_t *p_md = getMD();
    if( p_md ) {
      libvlc_event_manager_t *p_em;
      p_em = libvlc_media_player_event_manager( getMD() );
      events.hook_manager( p_em, this );
    }
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
255

256
    return NPERR_NO_ERROR;
257 258
}

259
VlcPluginBase::~VlcPluginBase()
260
{
261 262
    free(psz_baseURL);
    free(psz_target);
Jean-Paul Saman's avatar
Jean-Paul Saman committed
263

264
    if( vlc_player::is_open() )
265
    {
266

267 268
        if( playlist_isplaying() )
            playlist_stop();
Jean-Paul Saman's avatar
Jean-Paul Saman committed
269
        events.unhook_manager( this );
270
        vlc_player::close();
271
    }
272
    if( libvlc_instance )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
273
        libvlc_release( libvlc_instance );
274 275

    _instances.erase(this);
276 277
}

278
void VlcPluginBase::setWindow(const NPWindow &window)
279 280
{
    npwindow = window;
281
}
282

283 284 285 286 287 288 289
#if defined(XP_MACOSX)
NPError VlcPluginBase::get_root_layer(void *value)
{
    return NPERR_GENERIC_ERROR;
}
#endif

290 291 292 293 294
bool VlcPluginBase::handle_event(void *event)
{
    return false;
}

295
/*****************************************************************************
296
 * VlcPluginBase playlist replacement methods
297
 *****************************************************************************/
298
bool  VlcPluginBase::player_has_vout()
299
{
Jean-Paul Saman's avatar
Jean-Paul Saman committed
300
    bool r = false;
301
    if( playlist_isplaying() )
302
        r = libvlc_media_player_has_vout(get_player().get_mp())!=0;
303 304 305
    return r;
}

306
/*****************************************************************************
307
 * VlcPluginBase methods
308
 *****************************************************************************/
309

310
char *VlcPluginBase::getAbsoluteURL(const char *url)
311
{
312
    if( NULL != url )
313
    {
314 315 316
        // check whether URL is already absolute
        const char *end=strchr(url, ':');
        if( (NULL != end) && (end != url) )
317
        {
318 319
            // validate protocol header
            const char *start = url;
320 321 322
            char c = *start;
            if( isalpha(c) )
            {
323
                ++start;
324 325 326 327 328 329 330 331 332 333 334 335 336 337
                while( start != end )
                {
                    c  = *start;
                    if( ! (isalnum(c)
                       || ('-' == c)
                       || ('+' == c)
                       || ('.' == c)
                       || ('/' == c)) ) /* VLC uses / to allow user to specify a demuxer */
                        // not valid protocol header, assume relative URL
                        goto relativeurl;
                    ++start;
                }
                /* we have a protocol header, therefore URL is absolute */
                return strdup(url);
338
            }
339
            // not a valid protocol header, assume relative URL
340
        }
341

342 343
relativeurl:

344 345 346
        if( psz_baseURL )
        {
            size_t baseLen = strlen(psz_baseURL);
Jean-Paul Saman's avatar
Jean-Paul Saman committed
347
            char *href = (char *) malloc(baseLen+strlen(url)+1);
348 349 350
            if( href )
            {
                /* prepend base URL */
JP Dinger's avatar
JP Dinger committed
351
                memcpy(href, psz_baseURL, baseLen+1);
352 353 354 355 356 357 358 359 360 361 362 363 364 365

                /*
                ** relative url could be empty,
                ** in which case return base URL
                */
                if( '\0' == *url )
                    return href;

                /*
                ** locate pathname part of base URL
                */

                /* skip over protocol part  */
                char *pathstart = strchr(href, ':');
JP Dinger's avatar
JP Dinger committed
366
                char *pathend = href+baseLen;
367
                if( pathstart )
368 369 370
                {
                    if( '/' == *(++pathstart) )
                    {
371 372 373 374 375 376 377 378 379 380 381 382
                        if( '/' == *(++pathstart) )
                        {
                            ++pathstart;
                        }
                    }
                    /* skip over host part */
                    pathstart = strchr(pathstart, '/');
                    if( ! pathstart )
                    {
                        // no path, add a / past end of url (over '\0')
                        pathstart = pathend;
                        *pathstart = '/';
383 384
                    }
                }
385
                else
386
                {
387 388 389 390
                    /* baseURL is just a UNIX path */
                    if( '/' != *href )
                    {
                        /* baseURL is not an absolute path */
Jean-Paul Saman's avatar
Jean-Paul Saman committed
391
                        free(href);
392 393 394
                        return NULL;
                    }
                    pathstart = href;
395 396 397 398 399 400 401 402 403 404
                }

                /* relative URL made of an absolute path ? */
                if( '/' == *url )
                {
                    /* replace path completely */
                    strcpy(pathstart, url);
                    return href;
                }

405
                /* find last path component and replace it */
406 407
                while( '/' != *pathend)
                    --pathend;
408 409 410 411 412 413 414 415 416 417 418 419

                /*
                ** if relative url path starts with one or more '../',
                ** factor them out of href so that we return a
                ** normalized URL
                */
                while( pathend != pathstart )
                {
                    const char *p = url;
                    if( '.' != *p )
                        break;
                    ++p;
420 421 422 423
                    if( '\0' == *p  )
                    {
                        /* relative url is just '.' */
                        url = p;
424
                        break;
425 426 427 428 429 430 431
                    }
                    if( '/' == *p  )
                    {
                        /* relative url starts with './' */
                        url = ++p;
                        continue;
                    }
432
                    if( '.' != *p )
433 434
                        break;
                    ++p;
435 436 437 438 439 440
                    if( '\0' == *p )
                    {
                        /* relative url is '..' */
                    }
                    else
                    {
441
                        if( '/' != *p )
442 443 444 445
                            break;
                        /* relative url starts with '../' */
                        ++p;
                    }
446
                    url = p;
447 448 449 450 451
                    do
                    {
                        --pathend;
                    }
                    while( '/' != *pathend );
452
                }
453 454
                /* skip over '/' separator */
                ++pathend;
455
                /* concatenate remaining base URL and relative URL */
456
                strcpy(pathend, url);
457 458 459
            }
            return href;
        }
460
    }
461 462
    return NULL;
}
463

464
void VlcPluginBase::control_handler(vlc_toolbar_clicked_t clicked)
465
{
466
    switch( clicked )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
467
    {
468 469 470 471 472
        case clicked_Play:
        {
            playlist_play();
        }
        break;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
473

474 475 476 477 478
        case clicked_Pause:
        {
            playlist_pause();
        }
        break;
479

480 481 482 483 484
        case clicked_Stop:
        {
            playlist_stop();
        }
        break;
485

486 487
        case clicked_Fullscreen:
        {
488
            toggle_fullscreen();
489 490
        }
        break;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
491

492 493 494 495 496 497 498 499 500
        case clicked_Mute:
        case clicked_Unmute:
#if 0
        {
            if( p_md )
                libvlc_audio_toggle_mute( p_md );
        }
#endif
        break;
501

502 503 504 505 506 507 508 509
        case clicked_timeline:
#if 0
        {
            /* if a movie is loaded */
            if( p_md )
            {
                int64_t f_length;
                f_length = libvlc_media_player_get_length( p_md ) / 100;
510

511 512
                f_length = (float)f_length *
                        ( ((float)i_xPos-4.0 ) / ( ((float)i_width-8.0)/100) );
513

514 515 516
                libvlc_media_player_set_time( p_md, f_length );
            }
        }
Jean-Paul Saman's avatar
Jean-Paul Saman committed
517
#endif
518
        break;
519

520 521 522 523 524
        case clicked_Time:
        {
            /* Not implemented yet*/
        }
        break;
525

526 527 528 529
        default: /* button_Unknown */
            fprintf(stderr, "button Unknown!\n");
        break;
    }
530
}
531 532 533 534

// Verifies the version of the NPAPI.
// The eventListeners use a NPAPI function available
// since Gecko 1.9.
535
bool VlcPluginBase::canUseEventListener()
536 537 538 539 540 541 542 543 544 545 546
{
    int plugin_major, plugin_minor;
    int browser_major, browser_minor;

    NPN_Version(&plugin_major, &plugin_minor,
                &browser_major, &browser_minor);

    if (browser_minor >= 19 || browser_major > 0)
        return true;
    return false;
}
547