vlcplugin_base.cpp 16.1 KB
Newer Older
1
/*****************************************************************************
2
 * vlcplugin_base.cpp: a VLC plugin for Mozilla
3
 *****************************************************************************
4
 * Copyright (C) 2002-2012 VLC authors and VideoLAN
5
 * $Id$
6
 *
7
 * Authors: Damien Fouilleul <damienf.fouilleul@laposte.net>
8
 *          Jean-Paul Saman <jpsaman@videolan.org>
9
 *          Sergey Radionov <rsatom@gmail.com>
10
 *          Cheng Sun <chengsun9@gmail.com>
11 12 13
 *          Yannick Brehon <y.brehon@qiplay.com>
 *          Felix Paul Kühne <fkuehne # videolan.org>
 *          Ludovic Fauvet <etix@videolan.org>
14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * 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
27
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
28 29 30 31 32
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
33 34 35 36
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

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

40
#include "control/npolibvlc.h"
41

42
#include <cctype>
43

44 45 46
#include <cstdio>
#include <cstdlib>
#include <cstring>
47

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

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

71 72
std::set<VlcPluginBase*> VlcPluginBase::_instances;

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

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

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

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

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

105
    /* locate VLC module path */
Felix Paul Kühne's avatar
Felix Paul Kühne committed
106
#ifdef XP_WIN
107 108 109 110 111
    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 )
112
    {
113 114 115 116 117
         if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
                              (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
         {
             if( i_type == REG_SZ )
             {
118
                 strcat( p_data, "\\plugins" );
119 120
                 ppsz_argv[ppsz_argc++] = "--plugin-path";
                 ppsz_argv[ppsz_argc++] = p_data;
121 122 123
             }
         }
         RegCloseKey( h_key );
124
    }
125
    ppsz_argv[ppsz_argc++] = "--no-one-instance";
126

127 128
#endif
#ifdef XP_MACOSX
129
    ppsz_argv[ppsz_argc++] = "--vout=caopengllayer";
130 131 132
    ppsz_argv[ppsz_argc++] = "--scaletempo-stride=30";
    ppsz_argv[ppsz_argc++] = "--scaletempo-overlap=0,2";
    ppsz_argv[ppsz_argc++] = "--scaletempo-search=14";
133
    ppsz_argv[ppsz_argc++] = "--auhal-volume=256";
134 135
    ppsz_argv[ppsz_argc++] = "--auhal-audio-device=0";
    ppsz_argv[ppsz_argc++] = "--no-volume-save";
Felix Paul Kühne's avatar
Felix Paul Kühne committed
136
#endif
137

138 139 140 141
    /* common settings */
    ppsz_argv[ppsz_argc++] = "-vv";
    ppsz_argv[ppsz_argc++] = "--no-stats";
    ppsz_argv[ppsz_argc++] = "--no-media-library";
142
    ppsz_argv[ppsz_argc++] = "--intf=dummy";
143
    ppsz_argv[ppsz_argc++] = "--no-video-title-show";
Christophe Mutricy's avatar
Christophe Mutricy committed
144
    ppsz_argv[ppsz_argc++] = "--no-xlib";
145

146 147
    bool b_autoloop = false;

148
    /* parse plugin arguments */
149
    for( int i = 0; (i < argc) && (ppsz_argc < MAX_PARAMS); i++ )
150
    {
151
       /* fprintf(stderr, "argn=%s, argv=%s\n", argn[i], argv[i]); */
152 153 154 155 156 157 158 159

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

197
    libvlc_instance = libvlc_new(ppsz_argc, ppsz_argv);
198 199
    if( !libvlc_instance )
        return NPERR_GENERIC_ERROR;
200 201

    vlc_player::open(libvlc_instance);
202

203 204 205
    vlc_player::set_mode(b_autoloop ? libvlc_playback_mode_loop :
                                      libvlc_playback_mode_default);

206 207 208 209 210
    /*
    ** 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
211
    NPObject *plugin = NULL;
212 213 214 215 216 217 218 219 220 221

    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;

222 223
        script.UTF8Characters = docLocHref;
        script.UTF8Length = sizeof(docLocHref)-1;
224 225 226 227 228 229 230

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

231
                psz_baseURL = (char *) malloc(location.UTF8Length+1);
232 233
                if( psz_baseURL )
                {
234 235
                    strncpy(psz_baseURL, location.UTF8Characters, location.UTF8Length);
                    psz_baseURL[location.UTF8Length] = '\0';
236 237 238 239 240 241 242 243 244 245
                }
            }
            NPN_ReleaseVariantValue(&result);
        }
        NPN_ReleaseObject(plugin);
    }

    if( psz_target )
    {
        // get absolute URL from src
246 247
        char *psz_absurl = getAbsoluteURL(psz_target);
        psz_target = psz_absurl ? psz_absurl : strdup(psz_target);
248 249 250
    }

    /* assign plugin script root class */
251 252
    /* new APIs */
    p_scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
253

254
    if( !events.init() )
255 256
        return NPERR_GENERIC_ERROR;

257 258 259 260 261 262
    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 );
    }
263

264
    return NPERR_NO_ERROR;
265 266
}

267
VlcPluginBase::~VlcPluginBase()
268
{
269 270
    free(psz_baseURL);
    free(psz_target);
Jean-Paul Saman's avatar
Jean-Paul Saman committed
271

272
    if( vlc_player::is_open() )
273
    {
274

275 276
        if( playlist_isplaying() )
            playlist_stop();
Jean-Paul Saman's avatar
Jean-Paul Saman committed
277
        events.unhook_manager( this );
278
        vlc_player::close();
279
    }
280
    if( libvlc_instance )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
281
        libvlc_release( libvlc_instance );
282 283

    _instances.erase(this);
284 285
}

286
void VlcPluginBase::setWindow(const NPWindow &window)
287 288
{
    npwindow = window;
289
}
290

291 292 293 294 295 296 297
#if defined(XP_MACOSX)
NPError VlcPluginBase::get_root_layer(void *value)
{
    return NPERR_GENERIC_ERROR;
}
#endif

298 299 300 301 302
bool VlcPluginBase::handle_event(void *event)
{
    return false;
}

303
/*****************************************************************************
304
 * VlcPluginBase playlist replacement methods
305
 *****************************************************************************/
306
bool  VlcPluginBase::player_has_vout()
307
{
Jean-Paul Saman's avatar
Jean-Paul Saman committed
308
    bool r = false;
309
    if( playlist_isplaying() )
310
        r = libvlc_media_player_has_vout(get_player().get_mp())!=0;
311 312 313
    return r;
}

314
/*****************************************************************************
315
 * VlcPluginBase methods
316
 *****************************************************************************/
317

318
char *VlcPluginBase::getAbsoluteURL(const char *url)
319
{
320
    if( NULL != url )
321
    {
322 323 324
        // check whether URL is already absolute
        const char *end=strchr(url, ':');
        if( (NULL != end) && (end != url) )
325
        {
326 327
            // validate protocol header
            const char *start = url;
328 329 330
            char c = *start;
            if( isalpha(c) )
            {
331
                ++start;
332 333 334 335 336 337 338 339 340 341 342 343 344 345
                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);
346
            }
347
            // not a valid protocol header, assume relative URL
348
        }
349

350 351
relativeurl:

352 353 354
        if( psz_baseURL )
        {
            size_t baseLen = strlen(psz_baseURL);
Jean-Paul Saman's avatar
Jean-Paul Saman committed
355
            char *href = (char *) malloc(baseLen+strlen(url)+1);
356 357 358
            if( href )
            {
                /* prepend base URL */
JP Dinger's avatar
JP Dinger committed
359
                memcpy(href, psz_baseURL, baseLen+1);
360 361 362 363 364 365 366 367 368 369 370 371 372 373

                /*
                ** 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
374
                char *pathend = href+baseLen;
375
                if( pathstart )
376 377 378
                {
                    if( '/' == *(++pathstart) )
                    {
379 380 381 382 383 384 385 386 387 388 389 390
                        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 = '/';
391 392
                    }
                }
393
                else
394
                {
395 396 397 398
                    /* baseURL is just a UNIX path */
                    if( '/' != *href )
                    {
                        /* baseURL is not an absolute path */
Jean-Paul Saman's avatar
Jean-Paul Saman committed
399
                        free(href);
400 401 402
                        return NULL;
                    }
                    pathstart = href;
403 404 405 406 407 408 409 410 411 412
                }

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

413
                /* find last path component and replace it */
414 415
                while( '/' != *pathend)
                    --pathend;
416 417 418 419 420 421 422 423 424 425 426 427

                /*
                ** 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;
428 429 430 431
                    if( '\0' == *p  )
                    {
                        /* relative url is just '.' */
                        url = p;
432
                        break;
433 434 435 436 437 438 439
                    }
                    if( '/' == *p  )
                    {
                        /* relative url starts with './' */
                        url = ++p;
                        continue;
                    }
440
                    if( '.' != *p )
441 442
                        break;
                    ++p;
443 444 445 446 447 448
                    if( '\0' == *p )
                    {
                        /* relative url is '..' */
                    }
                    else
                    {
449
                        if( '/' != *p )
450 451 452 453
                            break;
                        /* relative url starts with '../' */
                        ++p;
                    }
454
                    url = p;
455 456 457 458 459
                    do
                    {
                        --pathend;
                    }
                    while( '/' != *pathend );
460
                }
461 462
                /* skip over '/' separator */
                ++pathend;
463
                /* concatenate remaining base URL and relative URL */
464
                strcpy(pathend, url);
465 466 467
            }
            return href;
        }
468
    }
469 470
    return NULL;
}
471

472
void VlcPluginBase::control_handler(vlc_toolbar_clicked_t clicked)
473
{
474
    switch( clicked )
475
    {
476 477 478 479 480
        case clicked_Play:
        {
            playlist_play();
        }
        break;
481

482 483 484 485 486
        case clicked_Pause:
        {
            playlist_pause();
        }
        break;
487

488 489 490 491 492
        case clicked_Stop:
        {
            playlist_stop();
        }
        break;
493

494 495
        case clicked_Fullscreen:
        {
496
            toggle_fullscreen();
497 498
        }
        break;
499

500 501 502 503 504 505 506 507 508
        case clicked_Mute:
        case clicked_Unmute:
#if 0
        {
            if( p_md )
                libvlc_audio_toggle_mute( p_md );
        }
#endif
        break;
509

510 511 512 513 514 515 516 517
        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;
518

519 520
                f_length = (float)f_length *
                        ( ((float)i_xPos-4.0 ) / ( ((float)i_width-8.0)/100) );
521

522 523 524
                libvlc_media_player_set_time( p_md, f_length );
            }
        }
Jean-Paul Saman's avatar
Jean-Paul Saman committed
525
#endif
526
        break;
527

528 529 530 531 532
        case clicked_Time:
        {
            /* Not implemented yet*/
        }
        break;
533

534 535 536 537
        default: /* button_Unknown */
            fprintf(stderr, "button Unknown!\n");
        break;
    }
538
}
539 540 541 542

// Verifies the version of the NPAPI.
// The eventListeners use a NPAPI function available
// since Gecko 1.9.
543
bool VlcPluginBase::canUseEventListener()
544 545 546 547 548 549 550 551 552 553 554
{
    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;
}
555