vlcplugin_base.cpp 28.2 KB
Newer Older
1
/*****************************************************************************
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
 *          Cheng Sun <chengsun9@gmail.com>
11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * 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
24
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 26 27 28 29
 *****************************************************************************/

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

34
#include "vlcplugin.h"
Jean-Paul Saman's avatar
Jean-Paul Saman committed
35

36
#include "control/npolibvlc.h"
37

38
#include <cctype>
39

40
#if defined(XP_UNIX)
41 42 43 44
#   include <pthread.h>
#elif defined(XP_WIN)
    /* windows headers */
#   include <winbase.h>
45 46 47 48
#else
#warning "locking not implemented for this platform"
#endif

49 50 51 52
#include <cstdio>
#include <cassert>
#include <cstdlib>
#include <cstring>
53

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
/*****************************************************************************
 * utilitiy functions
 *****************************************************************************/
static void plugin_lock_init(plugin_lock_t *lock)
{
    assert(lock);

#if defined(XP_UNIX)
    pthread_mutex_init(&lock->mutex, NULL);
#elif defined(XP_WIN)
    InitializeCriticalSection(&lock->cs);
#else
#warning "locking not implemented in this platform"
#endif
}

static void plugin_lock_destroy(plugin_lock_t *lock)
{
    assert(lock);

#if defined(XP_UNIX)
    pthread_mutex_destroy(&lock->mutex);
#elif defined(XP_WIN)
    DeleteCriticalSection(&lock->cs);
#else
#warning "locking not implemented in this platform"
#endif
}

static void plugin_lock(plugin_lock_t *lock)
{
    assert(lock);

#if defined(XP_UNIX)
    pthread_mutex_lock(&lock->mutex);
#elif defined(XP_WIN)
    EnterCriticalSection(&lock->cs);
#else
#warning "locking not implemented in this platform"
#endif
}

static void plugin_unlock(plugin_lock_t *lock)
{
    assert(lock);

#if defined(XP_UNIX)
    pthread_mutex_unlock(&lock->mutex);
#elif defined(XP_WIN)
    LeaveCriticalSection(&lock->cs);
#else
#warning "locking not implemented in this platform"
#endif
}

109
/*****************************************************************************
Jean-Paul Saman's avatar
Jean-Paul Saman committed
110
 * Event Object
111
 *****************************************************************************/
Jean-Paul Saman's avatar
Jean-Paul Saman committed
112 113 114 115 116 117 118
static void handle_input_event(const libvlc_event_t* event, void *param);
static void handle_changed_event(const libvlc_event_t* event, void *param);

static vlcplugin_event_t vlcevents[] = {
    { "MediaPlayerMediaChanged", libvlc_MediaPlayerMediaChanged, handle_input_event },
    { "MediaPlayerNothingSpecial", libvlc_MediaPlayerNothingSpecial, handle_input_event },
    { "MediaPlayerOpening", libvlc_MediaPlayerOpening, handle_input_event },
119
    { "MediaPlayerBuffering", libvlc_MediaPlayerBuffering, handle_changed_event },
Jean-Paul Saman's avatar
Jean-Paul Saman committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    { "MediaPlayerPlaying", libvlc_MediaPlayerPlaying, handle_input_event },
    { "MediaPlayerPaused", libvlc_MediaPlayerPaused, handle_input_event },
    { "MediaPlayerStopped", libvlc_MediaPlayerStopped, handle_input_event },
    { "MediaPlayerForward", libvlc_MediaPlayerForward, handle_input_event },
    { "MediaPlayerBackward", libvlc_MediaPlayerBackward, handle_input_event },
    { "MediaPlayerEndReached", libvlc_MediaPlayerEndReached, handle_input_event },
    { "MediaPlayerEncounteredError", libvlc_MediaPlayerEncounteredError, handle_input_event },
    { "MediaPlayerTimeChanged", libvlc_MediaPlayerTimeChanged, handle_changed_event },
    { "MediaPlayerPositionChanged", libvlc_MediaPlayerPositionChanged, handle_changed_event },
    { "MediaPlayerSeekableChanged", libvlc_MediaPlayerSeekableChanged, handle_changed_event },
    { "MediaPlayerPausableChanged", libvlc_MediaPlayerPausableChanged, handle_changed_event },
    { "MediaPlayerTitleChanged", libvlc_MediaPlayerTitleChanged, handle_changed_event },
    { "MediaPlayerLengthChanged", libvlc_MediaPlayerLengthChanged, handle_changed_event },
};

static void handle_input_event(const libvlc_event_t* event, void *param)
136
{
137
    VlcPluginBase *plugin = (VlcPluginBase*)param;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
138 139 140 141 142 143 144 145 146 147 148
    switch( event->type )
    {
        case libvlc_MediaPlayerNothingSpecial:
        case libvlc_MediaPlayerOpening:
        case libvlc_MediaPlayerPlaying:
        case libvlc_MediaPlayerPaused:
        case libvlc_MediaPlayerStopped:
        case libvlc_MediaPlayerForward:
        case libvlc_MediaPlayerBackward:
        case libvlc_MediaPlayerEndReached:
        case libvlc_MediaPlayerEncounteredError:
149
            plugin->event_callback(event, NULL, 0);
Jean-Paul Saman's avatar
Jean-Paul Saman committed
150 151 152 153
            break;
        default: /* ignore all other libvlc_event_type_t */
            break;
    }
154 155
}

Jean-Paul Saman's avatar
Jean-Paul Saman committed
156 157 158 159 160
static void handle_changed_event(const libvlc_event_t* event, void *param)
{
    uint32_t   npcount = 1;
    NPVariant *npparam = (NPVariant *) NPN_MemAlloc( sizeof(NPVariant) * npcount );

161
    VlcPluginBase *plugin = (VlcPluginBase*)param;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
162 163
    switch( event->type )
    {
164
#ifdef LIBVLC120
165 166 167
        case libvlc_MediaPlayerBuffering:
            DOUBLE_TO_NPVARIANT(event->u.media_player_buffering.new_cache, npparam[0]);
            break;
168
#endif
Jean-Paul Saman's avatar
Jean-Paul Saman committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
        case libvlc_MediaPlayerTimeChanged:
            DOUBLE_TO_NPVARIANT(event->u.media_player_time_changed.new_time, npparam[0]);
            break;
        case libvlc_MediaPlayerPositionChanged:
            DOUBLE_TO_NPVARIANT(event->u.media_player_position_changed.new_position, npparam[0]);
            break;
        case libvlc_MediaPlayerSeekableChanged:
            BOOLEAN_TO_NPVARIANT(event->u.media_player_seekable_changed.new_seekable, npparam[0]);
            break;
        case libvlc_MediaPlayerPausableChanged:
            BOOLEAN_TO_NPVARIANT(event->u.media_player_pausable_changed.new_pausable, npparam[0]);
            break;
        case libvlc_MediaPlayerTitleChanged:
            BOOLEAN_TO_NPVARIANT(event->u.media_player_title_changed.new_title, npparam[0]);
            break;
        case libvlc_MediaPlayerLengthChanged:
            DOUBLE_TO_NPVARIANT(event->u.media_player_length_changed.new_length, npparam[0]);
            break;
        default: /* ignore all other libvlc_event_type_t */
            NPN_MemFree( npparam );
            return;
    }
191
    plugin->event_callback(event, npparam, npcount);
192
}
193

194 195
bool EventObj::init()
{
196 197
    plugin_lock_init(&lock);
    return true;
198 199 200 201
}

EventObj::~EventObj()
{
202
    plugin_lock_destroy(&lock);
203
}
204 205

void EventObj::deliver(NPP browser)
206
{
207 208 209
    if(_already_in_deliver)
        return;

210
    plugin_lock(&lock);
211
    _already_in_deliver = true;
212

Jean-Paul Saman's avatar
Jean-Paul Saman committed
213
    for( ev_l::iterator iter = _elist.begin(); iter != _elist.end(); ++iter )
214
    {
Jean-Paul Saman's avatar
Jean-Paul Saman committed
215
        for( lr_l::iterator j = _llist.begin(); j != _llist.end(); ++j )
216
        {
Jean-Paul Saman's avatar
Jean-Paul Saman committed
217
            if( j->event_type() == iter->event_type() )
218
            {
Jean-Paul Saman's avatar
Jean-Paul Saman committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
                NPVariant result;
                NPVariant *params = iter->params();
                uint32_t   count  = iter->count();

                NPObject *listener = j->listener();
                assert( listener );

                NPN_InvokeDefault( browser, listener, params, count, &result );
                NPN_ReleaseVariantValue( &result );

                for( uint32_t n = 0; n < count; n++ )
                {
                    if( NPVARIANT_IS_STRING(params[n]) )
                        NPN_MemFree( (void*) NPVARIANT_TO_STRING(params[n]).UTF8Characters );
                    else if( NPVARIANT_IS_OBJECT(params[n]) )
                    {
                        NPN_ReleaseObject( NPVARIANT_TO_OBJECT(params[n]) );
                        NPN_MemFree( (void*)NPVARIANT_TO_OBJECT(params[n]) );
                    }
                }
239
                if (params) NPN_MemFree( params );
240 241 242
            }
        }
    }
243
    _elist.clear();
244

245
    _already_in_deliver = false;
246
    plugin_unlock(&lock);
247 248
}

Jean-Paul Saman's avatar
Jean-Paul Saman committed
249 250
void EventObj::callback(const libvlc_event_t* event,
                        NPVariant *npparams, uint32_t count)
251
{
252
    plugin_lock(&lock);
Jean-Paul Saman's avatar
Jean-Paul Saman committed
253
    _elist.push_back(VLCEvent(event->type, npparams, count));
254
    plugin_unlock(&lock);
255
}
256

Jean-Paul Saman's avatar
Jean-Paul Saman committed
257
vlcplugin_event_t *EventObj::find_event(const char *s) const
258
{
Jean-Paul Saman's avatar
Jean-Paul Saman committed
259 260 261 262 263 264
    for( int i = 0; i < ARRAY_SIZE(vlcevents); i++ )
    {
        if( strncmp(vlcevents[i].name, s, strlen(vlcevents[i].name)) == 0 )
            return &vlcevents[i];
    }
    return NULL;
265 266
}

Jean-Paul Saman's avatar
Jean-Paul Saman committed
267
const char *EventObj::find_name(const libvlc_event_t *event)
268
{
Jean-Paul Saman's avatar
Jean-Paul Saman committed
269 270 271 272 273 274
    for( int i = 0; i < ARRAY_SIZE(vlcevents); i++ )
    {
        if( vlcevents[i].libvlc_type == event->type )
            return vlcevents[i].name;
    }
    return NULL;
275 276
}

Jean-Paul Saman's avatar
Jean-Paul Saman committed
277
bool EventObj::insert(const NPString &name, NPObject *listener, bool bubble)
278
{
Jean-Paul Saman's avatar
Jean-Paul Saman committed
279 280
    vlcplugin_event_t *event = find_event(name.UTF8Characters);
    if( !event )
281 282
        return false;

283 284 285 286 287 288 289
    for(lr_l::iterator iter = _llist.begin(); iter != _llist.end(); ++iter ){
        if(iter->listener() == listener &&
           event->libvlc_type == iter->event_type() &&
           iter->bubble() == bubble)
        {
            return false;
        }
290 291
    }

292 293
    _llist.push_back(Listener(event, listener, bubble));
    return true;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
294
}
295

Jean-Paul Saman's avatar
Jean-Paul Saman committed
296
bool EventObj::remove(const NPString &name, NPObject *listener, bool bubble)
297
{
Jean-Paul Saman's avatar
Jean-Paul Saman committed
298 299
    vlcplugin_event_t *event = find_event(name.UTF8Characters);
    if( !event )
300 301
        return false;

Jean-Paul Saman's avatar
Jean-Paul Saman committed
302
    for( lr_l::iterator iter = _llist.begin(); iter !=_llist.end(); iter++ )
303
    {
Jean-Paul Saman's avatar
Jean-Paul Saman committed
304 305 306
        if( iter->event_type() == event->libvlc_type &&
            iter->listener() == listener &&
            iter->bubble() == bubble )
307
        {
Jean-Paul Saman's avatar
Jean-Paul Saman committed
308 309
            iter = _llist.erase(iter);
            return true;
310 311
        }
    }
312

Jean-Paul Saman's avatar
Jean-Paul Saman committed
313
    return false;
314 315
}

Jean-Paul Saman's avatar
Jean-Paul Saman committed
316
void EventObj::hook_manager( libvlc_event_manager_t *em, void *userdata )
317
{
Jean-Paul Saman's avatar
Jean-Paul Saman committed
318 319 320 321 322 323 324 325 326 327 328 329
    _em = em;

    if( _em )
    {
        /* attach all libvlc events we care about */
        for( int i = 0; i < ARRAY_SIZE(vlcevents); i++ )
        {
            libvlc_event_attach( _em, vlcevents[i].libvlc_type,
                                      vlcevents[i].libvlc_callback,
                                      userdata );
        }
    }
330 331
}

Jean-Paul Saman's avatar
Jean-Paul Saman committed
332
void EventObj::unhook_manager( void *userdata )
333
{
Jean-Paul Saman's avatar
Jean-Paul Saman committed
334 335 336 337 338 339 340 341 342 343
    if( _em )
    {
		/* detach all libvlc events we cared about */
        for( int i = 0; i < ARRAY_SIZE(vlcevents); i++ )
        {
            libvlc_event_detach( _em, vlcevents[i].libvlc_type,
                                      vlcevents[i].libvlc_callback,
                                      userdata );
        }
    }
344 345
}

Jean-Paul Saman's avatar
Jean-Paul Saman committed
346
/*****************************************************************************
347
 * VlcPluginBase constructor and destructor
Jean-Paul Saman's avatar
Jean-Paul Saman committed
348
 *****************************************************************************/
349
VlcPluginBase::VlcPluginBase( NPP instance, NPuint16_t mode ) :
Jean-Paul Saman's avatar
Jean-Paul Saman committed
350 351 352 353 354 355 356 357 358 359 360 361 362
    i_npmode(mode),
    b_stream(0),
    b_autoplay(1),
    b_toolbar(0),
    psz_text(NULL),
    psz_target(NULL),
    playlist_index(-1),
    libvlc_instance(NULL),
    libvlc_media_list(NULL),
    libvlc_media_player(NULL),
    p_scriptClass(NULL),
    p_browser(instance),
    psz_baseURL(NULL)
363
{
Jean-Paul Saman's avatar
Jean-Paul Saman committed
364
    memset(&npwindow, 0, sizeof(NPWindow));
365 366
}

Jean-Paul Saman's avatar
Jean-Paul Saman committed
367 368 369 370 371
static bool boolValue(const char *value) {
    return ( !strcmp(value, "1") ||
             !strcasecmp(value, "true") ||
             !strcasecmp(value, "yes") );
}
372

373
void VlcPluginBase::eventAsync(void *param)
374
{
375
    VlcPluginBase *plugin = (VlcPluginBase*)param;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
376
    plugin->events.deliver(plugin->getBrowser());
377
    plugin->update_controls();
378 379
}

380 381
void VlcPluginBase::event_callback(const libvlc_event_t* event,
                NPVariant *npparams, uint32_t npcount)
Jean-Paul Saman's avatar
Jean-Paul Saman committed
382
{
383
#if defined(XP_UNIX) || defined(XP_WIN)
384 385
    events.callback(event, npparams, npcount);
    NPN_PluginThreadAsyncCall(getBrowser(), eventAsync, this);
Jean-Paul Saman's avatar
Jean-Paul Saman committed
386
#else
387 388 389
#ifdef _MSC_VER
#pragma message("NPN_PluginThreadAsyncCall not implemented yet.")
#else
Jean-Paul Saman's avatar
Jean-Paul Saman committed
390
#warning NPN_PluginThreadAsyncCall not implemented yet.
391
#endif //_MSC_VER
Jean-Paul Saman's avatar
Jean-Paul Saman committed
392 393 394
    printf("No NPN_PluginThreadAsyncCall(), doing nothing.\n");
#endif
}
395

396
NPError VlcPluginBase::init(int argc, char* const argn[], char* const argv[])
397
{
398
    /* prepare VLC command line */
399
    const char *ppsz_argv[32];
400
    int ppsz_argc = 0;
401

402 403 404 405
#ifndef NDEBUG
    ppsz_argv[ppsz_argc++] = "--no-plugins-cache";
#endif

406 407
    /* locate VLC module path */
#ifdef XP_MACOSX
408
    ppsz_argv[ppsz_argc++] = "--plugin-path=/Library/Internet\\ Plug-Ins/VLC\\ Plugin.plugin/Contents/MacOS/plugins";
409
    ppsz_argv[ppsz_argc++] = "--vout=minimal_macosx";
410 411 412 413 414 415
#elif defined(XP_WIN)
    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 )
416
    {
417 418 419 420 421
         if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
                              (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
         {
             if( i_type == REG_SZ )
             {
422
                 strcat( p_data, "\\plugins" );
423 424
                 ppsz_argv[ppsz_argc++] = "--plugin-path";
                 ppsz_argv[ppsz_argc++] = p_data;
425 426 427
             }
         }
         RegCloseKey( h_key );
428
    }
429
    ppsz_argv[ppsz_argc++] = "--no-one-instance";
430

431
#endif /* XP_MACOSX */
432

433 434 435 436
    /* common settings */
    ppsz_argv[ppsz_argc++] = "-vv";
    ppsz_argv[ppsz_argc++] = "--no-stats";
    ppsz_argv[ppsz_argc++] = "--no-media-library";
437
    ppsz_argv[ppsz_argc++] = "--intf=dummy";
438
    ppsz_argv[ppsz_argc++] = "--no-video-title-show";
Christophe Mutricy's avatar
Christophe Mutricy committed
439
    ppsz_argv[ppsz_argc++] = "--no-xlib";
440

441
    const char *progid = NULL;
442 443

    /* parse plugin arguments */
444
    for( int i = 0; (i < argc) && (ppsz_argc < 32); i++ )
445
    {
446
       /* fprintf(stderr, "argn=%s, argv=%s\n", argn[i], argv[i]); */
447 448 449 450 451 452 453 454

        if( !strcmp( argn[i], "target" )
         || !strcmp( argn[i], "mrl")
         || !strcmp( argn[i], "filename")
         || !strcmp( argn[i], "src") )
        {
            psz_target = argv[i];
        }
455 456
        else if( !strcmp( argn[i], "text" ) )
        {
457
            free( psz_text );
458 459
            psz_text = strdup( argv[i] );
        }
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
        else if( !strcmp( argn[i], "autoplay")
              || !strcmp( argn[i], "autostart") )
        {
            b_autoplay = boolValue(argv[i]);
        }
        else if( !strcmp( argn[i], "fullscreen" ) )
        {
            if( boolValue(argv[i]) )
            {
                ppsz_argv[ppsz_argc++] = "--fullscreen";
            }
            else
            {
                ppsz_argv[ppsz_argc++] = "--no-fullscreen";
            }
        }
        else if( !strcmp( argn[i], "mute" ) )
        {
            if( boolValue(argv[i]) )
            {
480
                ppsz_argv[ppsz_argc++] = "--volume=0";
481 482 483 484 485 486 487 488 489
            }
        }
        else if( !strcmp( argn[i], "loop")
              || !strcmp( argn[i], "autoloop") )
        {
            if( boolValue(argv[i]) )
            {
                ppsz_argv[ppsz_argc++] = "--loop";
            }
490 491
            else
            {
492 493 494
                ppsz_argv[ppsz_argc++] = "--no-loop";
            }
        }
495 496
        else if( !strcmp( argn[i], "version")
              || !strcmp( argn[i], "progid") )
497
        {
498
            progid = argv[i];
499
        }
500
        else if( !strcmp( argn[i], "toolbar" ) )
501
        {
502
            b_toolbar = boolValue(argv[i]);
503
        }
504 505
    }

506
    libvlc_instance = libvlc_new(ppsz_argc, ppsz_argv);
507 508
    if( !libvlc_instance )
        return NPERR_GENERIC_ERROR;
509
    libvlc_media_list = libvlc_media_list_new(libvlc_instance);
510 511 512 513 514 515

    /*
    ** 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
516
    NPObject *plugin = NULL;
517 518 519 520 521 522 523 524 525 526

    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;

527 528
        script.UTF8Characters = docLocHref;
        script.UTF8Length = sizeof(docLocHref)-1;
529 530 531 532 533 534 535

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

536
                psz_baseURL = (char *) malloc(location.UTF8Length+1);
537 538
                if( psz_baseURL )
                {
539 540
                    strncpy(psz_baseURL, location.UTF8Characters, location.UTF8Length);
                    psz_baseURL[location.UTF8Length] = '\0';
541 542 543 544 545 546 547 548 549 550
                }
            }
            NPN_ReleaseVariantValue(&result);
        }
        NPN_ReleaseObject(plugin);
    }

    if( psz_target )
    {
        // get absolute URL from src
551 552
        char *psz_absurl = getAbsoluteURL(psz_target);
        psz_target = psz_absurl ? psz_absurl : strdup(psz_target);
553 554 555
    }

    /* assign plugin script root class */
556 557
    /* new APIs */
    p_scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
558

559
    if( !events.init() )
560 561
        return NPERR_GENERIC_ERROR;

562
    return NPERR_NO_ERROR;
563 564
}

565
VlcPluginBase::~VlcPluginBase()
566
{
567 568
    free(psz_baseURL);
    free(psz_target);
569
    free(psz_text);
Jean-Paul Saman's avatar
Jean-Paul Saman committed
570

571
    if( libvlc_media_player )
572
    {
573 574
        if( playlist_isplaying() )
            playlist_stop();
Jean-Paul Saman's avatar
Jean-Paul Saman committed
575
        events.unhook_manager( this );
576
        libvlc_media_player_release( libvlc_media_player );
577
    }
578 579
    if( libvlc_media_list )
        libvlc_media_list_release( libvlc_media_list );
580
    if( libvlc_instance )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
581
        libvlc_release( libvlc_instance );
582 583
}

584
void VlcPluginBase::setWindow(const NPWindow &window)
585 586 587 588
{
    npwindow = window;
};

589
/*****************************************************************************
590
 * VlcPluginBase playlist replacement methods
591
 *****************************************************************************/
592
int VlcPluginBase::playlist_add( const char *mrl )
593 594
{
    int item = -1;
595
    libvlc_media_t *p_m = libvlc_media_new_location(libvlc_instance,mrl);
596
    if( !p_m )
597
        return -1;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
598
    assert( libvlc_media_list );
599
    libvlc_media_list_lock(libvlc_media_list);
600
    if( !libvlc_media_list_add_media(libvlc_media_list,p_m) )
601
        item = libvlc_media_list_count(libvlc_media_list)-1;
602 603 604 605 606 607 608
    libvlc_media_list_unlock(libvlc_media_list);

    libvlc_media_release(p_m);

    return item;
}

609
int VlcPluginBase::playlist_add_extended_untrusted( const char *mrl, const char *name,
610
                    int optc, const char **optv )
611
{
Jean-Paul Saman's avatar
Jean-Paul Saman committed
612
    libvlc_media_t *p_m;
613
    int item = -1;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
614 615 616

    assert( libvlc_media_list );

617
    p_m = libvlc_media_new_location(libvlc_instance, mrl);
618
    if( !p_m )
619 620 621
        return -1;

    for( int i = 0; i < optc; ++i )
622
        libvlc_media_add_option_flag(p_m, optv[i], libvlc_media_option_unique);
623 624

    libvlc_media_list_lock(libvlc_media_list);
625
    if( !libvlc_media_list_add_media(libvlc_media_list,p_m) )
626
        item = libvlc_media_list_count(libvlc_media_list)-1;
627 628 629 630 631 632
    libvlc_media_list_unlock(libvlc_media_list);
    libvlc_media_release(p_m);

    return item;
}

633
bool VlcPluginBase::playlist_select( int idx )
634 635 636
{
    libvlc_media_t *p_m = NULL;

Jean-Paul Saman's avatar
Jean-Paul Saman committed
637 638
    assert( libvlc_media_list );

639 640
    libvlc_media_list_lock(libvlc_media_list);

641
    int count = libvlc_media_list_count(libvlc_media_list);
642 643 644 645 646
    if( idx<0||idx>=count )
        goto bad_unlock;

    playlist_index = idx;

647
    p_m = libvlc_media_list_item_at_index(libvlc_media_list,playlist_index);
648 649
    libvlc_media_list_unlock(libvlc_media_list);

650
    if( !p_m )
651 652 653 654
        return false;

    if( libvlc_media_player )
    {
Jean-Paul Saman's avatar
Jean-Paul Saman committed
655 656
        if( playlist_isplaying() )
            playlist_stop();
Jean-Paul Saman's avatar
Jean-Paul Saman committed
657
        events.unhook_manager( this );
658
        on_media_player_release();
659 660 661 662
        libvlc_media_player_release( libvlc_media_player );
        libvlc_media_player = NULL;
    }

Jean-Paul Saman's avatar
Jean-Paul Saman committed
663
    libvlc_media_player = libvlc_media_player_new_from_media( p_m );
664
    if( libvlc_media_player )
665
    {
666
        on_media_player_new();
667
        set_player_window();
Jean-Paul Saman's avatar
Jean-Paul Saman committed
668 669 670 671

        libvlc_event_manager_t *p_em;
        p_em = libvlc_media_player_event_manager( libvlc_media_player );
        events.hook_manager( p_em, this );
672 673
    }

674
    libvlc_media_release( p_m );
675
    return true;
676 677

bad_unlock:
Jean-Paul Saman's avatar
Jean-Paul Saman committed
678
    libvlc_media_list_unlock( libvlc_media_list );
679 680 681
    return false;
}

682
int VlcPluginBase::playlist_delete_item( int idx )
683
{
Jean-Paul Saman's avatar
Jean-Paul Saman committed
684 685
    if( !libvlc_media_list )
        return -1;
686
    libvlc_media_list_lock(libvlc_media_list);
687
    int ret = libvlc_media_list_remove_index(libvlc_media_list,idx);
688
    libvlc_media_list_unlock(libvlc_media_list);
689
    return ret;
690 691
}

692
void VlcPluginBase::playlist_clear()
693 694 695
{
    if( libvlc_media_list )
        libvlc_media_list_release(libvlc_media_list);
696
    libvlc_media_list = libvlc_media_list_new(getVLC());
697 698
}

699
int VlcPluginBase::playlist_count()
700 701
{
    int items_count = 0;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
702 703
    if( !libvlc_media_list )
        return items_count;
704
    libvlc_media_list_lock(libvlc_media_list);
705
    items_count = libvlc_media_list_count(libvlc_media_list);
706 707 708 709 710
    libvlc_media_list_unlock(libvlc_media_list);
    return items_count;
}


711
bool  VlcPluginBase::player_has_vout()
712
{
Jean-Paul Saman's avatar
Jean-Paul Saman committed
713
    bool r = false;
714
    if( playlist_isplaying() )
715
        r = libvlc_media_player_has_vout(libvlc_media_player);
716 717 718
    return r;
}

719
/*****************************************************************************
720
 * VlcPluginBase methods
721
 *****************************************************************************/
722

723
char *VlcPluginBase::getAbsoluteURL(const char *url)
724
{
725
    if( NULL != url )
726
    {
727 728 729
        // check whether URL is already absolute
        const char *end=strchr(url, ':');
        if( (NULL != end) && (end != url) )
730
        {
731 732
            // validate protocol header
            const char *start = url;
733 734 735
            char c = *start;
            if( isalpha(c) )
            {
736
                ++start;
737 738 739 740 741 742 743 744 745 746 747 748 749 750
                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);
751
            }
752
            // not a valid protocol header, assume relative URL
753
        }
754

755 756
relativeurl:

757 758 759
        if( psz_baseURL )
        {
            size_t baseLen = strlen(psz_baseURL);
Jean-Paul Saman's avatar
Jean-Paul Saman committed
760
            char *href = (char *) malloc(baseLen+strlen(url)+1);
761 762 763
            if( href )
            {
                /* prepend base URL */
JP Dinger's avatar
JP Dinger committed
764
                memcpy(href, psz_baseURL, baseLen+1);
765 766 767 768 769 770 771 772 773 774 775 776 777 778

                /*
                ** 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
779
                char *pathend = href+baseLen;
780
                if( pathstart )
781 782 783
                {
                    if( '/' == *(++pathstart) )
                    {
784 785 786 787 788 789 790 791 792 793 794 795
                        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 = '/';
796 797
                    }
                }
798
                else
799
                {
800 801 802 803
                    /* baseURL is just a UNIX path */
                    if( '/' != *href )
                    {
                        /* baseURL is not an absolute path */
Jean-Paul Saman's avatar
Jean-Paul Saman committed
804
                        free(href);
805 806 807
                        return NULL;
                    }
                    pathstart = href;
808 809 810 811 812 813 814 815 816 817
                }

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

818
                /* find last path component and replace it */
819 820
                while( '/' != *pathend)
                    --pathend;
821 822 823 824 825 826 827 828 829 830 831 832

                /*
                ** 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;
833 834 835 836
                    if( '\0' == *p  )
                    {
                        /* relative url is just '.' */
                        url = p;
837
                        break;
838 839 840 841 842 843 844
                    }
                    if( '/' == *p  )
                    {
                        /* relative url starts with './' */
                        url = ++p;
                        continue;
                    }
845
                    if( '.' != *p )
846 847
                        break;
                    ++p;
848 849 850 851 852 853
                    if( '\0' == *p )
                    {
                        /* relative url is '..' */
                    }
                    else
                    {
854
                        if( '/' != *p )
855 856 857 858
                            break;
                        /* relative url starts with '../' */
                        ++p;
                    }
859
                    url = p;
860 861 862 863 864
                    do
                    {
                        --pathend;
                    }
                    while( '/' != *pathend );
865
                }
866 867
                /* skip over '/' separator */
                ++pathend;
868
                /* concatenate remaining base URL and relative URL */
869
                strcpy(pathend, url);
870 871 872
            }
            return href;
        }
873
    }
874 875
    return NULL;
}
876

877
void VlcPluginBase::control_handler(vlc_toolbar_clicked_t clicked)
878
{
879
    switch( clicked )
880
    {
881 882 883 884 885
        case clicked_Play:
        {
            playlist_play();
        }
        break;
886

887 888 889 890 891
        case clicked_Pause:
        {
            playlist_pause();
        }
        break;
892

893 894 895 896 897
        case clicked_Stop:
        {
            playlist_stop();
        }
        break;
898

899 900
        case clicked_Fullscreen:
        {
901
            toggle_fullscreen();
902 903
        }
        break;
904

905 906 907 908 909 910 911 912 913
        case clicked_Mute:
        case clicked_Unmute:
#if 0
        {
            if( p_md )
                libvlc_audio_toggle_mute( p_md );
        }
#endif
        break;
914

915 916 917 918 919 920 921 922
        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;
923

924 925
                f_length = (float)f_length *
                        ( ((float)i_xPos-4.0 ) / ( ((float)i_width-8.0)/100) );
926

927 928 929
                libvlc_media_player_set_time( p_md, f_length );
            }
        }
Jean-Paul Saman's avatar
Jean-Paul Saman committed
930
#endif
931
        break;
932

933 934 935 936 937
        case clicked_Time:
        {
            /* Not implemented yet*/
        }
        break;
938

939 940 941 942
        default: /* button_Unknown */
            fprintf(stderr, "button Unknown!\n");
        break;
    }
943
}
944 945 946 947

// Verifies the version of the NPAPI.
// The eventListeners use a NPAPI function available
// since Gecko 1.9.
948
bool VlcPluginBase::canUseEventListener()
949 950 951 952 953 954 955 956 957 958 959
{
    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;
}