vlcplugin_base.cpp 16.2 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 "npruntime/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
        else if( !strcmp( argn[i], "fullscreen" )
170 171
              || !strcmp( argn[i], "allowfullscreen" )
              || !strcmp( argn[i], "fullscreenenabled" ) )
172
        {
173
            set_enable_fs( boolValue(argv[i]) );
174 175 176 177 178
        }
        else if( !strcmp( argn[i], "mute" ) )
        {
            if( boolValue(argv[i]) )
            {
179
                ppsz_argv[ppsz_argc++] = "--volume=0";
180 181 182 183 184
            }
        }
        else if( !strcmp( argn[i], "loop")
              || !strcmp( argn[i], "autoloop") )
        {
185
            b_autoloop = boolValue(argv[i]);
186
        }
187 188
        else if( !strcmp( argn[i], "toolbar" )
              || !strcmp( argn[i], "controls") )
189
        {
190
            set_show_toolbar( boolValue(argv[i]) );
191
        }
192 193
        else if( !strcmp( argn[i], "bgcolor" ) )
        {
194
            set_bg_color( argv[i] );
195
        }
196 197 198 199
        else if( !strcmp( argn[i], "branding" ) )
        {
            set_enable_branding( boolValue(argv[i]) );
        }
200 201
    }

202
    libvlc_instance = libvlc_new(ppsz_argc, ppsz_argv);
203 204
    if( !libvlc_instance )
        return NPERR_GENERIC_ERROR;
205 206

    vlc_player::open(libvlc_instance);
207

208 209 210
    vlc_player::set_mode(b_autoloop ? libvlc_playback_mode_loop :
                                      libvlc_playback_mode_default);

211 212 213 214 215
    /*
    ** 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
216
    NPObject *plugin = NULL;
217 218 219 220 221 222 223 224 225 226

    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;

227 228
        script.UTF8Characters = docLocHref;
        script.UTF8Length = sizeof(docLocHref)-1;
229 230 231 232 233 234 235

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

236
                psz_baseURL = (char *) malloc(location.UTF8Length+1);
237 238
                if( psz_baseURL )
                {
239 240
                    strncpy(psz_baseURL, location.UTF8Characters, location.UTF8Length);
                    psz_baseURL[location.UTF8Length] = '\0';
241 242 243 244 245 246 247 248 249 250
                }
            }
            NPN_ReleaseVariantValue(&result);
        }
        NPN_ReleaseObject(plugin);
    }

    if( psz_target )
    {
        // get absolute URL from src
251 252
        char *psz_absurl = getAbsoluteURL(psz_target);
        psz_target = psz_absurl ? psz_absurl : strdup(psz_target);
253 254 255
    }

    /* assign plugin script root class */
256 257
    /* new APIs */
    p_scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
258

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

266
    return NPERR_NO_ERROR;
267 268
}

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

274
    if( vlc_player::is_open() )
275
    {
276

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

    _instances.erase(this);
286 287
}

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

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

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

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

316
/*****************************************************************************
317
 * VlcPluginBase methods
318
 *****************************************************************************/
319

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

352 353
relativeurl:

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

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

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

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

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

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

484 485 486 487 488
        case clicked_Pause:
        {
            playlist_pause();
        }
        break;
489

490 491 492 493 494
        case clicked_Stop:
        {
            playlist_stop();
        }
        break;
495

496 497
        case clicked_Fullscreen:
        {
498
            toggle_fullscreen();
499 500
        }
        break;
501

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

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

521 522
                f_length = (float)f_length *
                        ( ((float)i_xPos-4.0 ) / ( ((float)i_width-8.0)/100) );
523

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

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

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

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