vlcshell.cpp 16.2 KB
Newer Older
1
/*****************************************************************************
2
 * vlcshell.cpp: a VLC plugin for Mozilla
3
 *****************************************************************************
Jean-Paul Saman's avatar
Jean-Paul Saman committed
4
 * Copyright (C) 2002-2009 the VideoLAN team
5
 * $Id$
6 7
 *
 * Authors: Samuel Hocevar <sam@zoy.org>
8
 *          Jean-Paul Saman <jpsaman@videolan.org>
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
28 29 30 31 32

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33 34
#include <stdio.h>
#include <string.h>
35
#include <stdlib.h>
36

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
37
#include "common.h"
38
#include "vlcshell.h"
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
39
#include "vlcplugin.h"
40

41 42 43 44 45 46 47 48 49 50 51
static char mimetype[] =
    /* MPEG-1 and MPEG-2 */
    "audio/mpeg:mp2,mp3,mpga,mpega:MPEG audio;"
    "audio/x-mpeg:mp2,mp3,mpga,mpega:MPEG audio;"
    "video/mpeg:mpg,mpeg,mpe:MPEG video;"
    "video/x-mpeg:mpg,mpeg,mpe:MPEG video;"
    "video/mpeg-system:mpg,mpeg,mpe,vob:MPEG video;"
    "video/x-mpeg-system:mpg,mpeg,mpe,vob:MPEG video;"
    /* MPEG-4 */
    "audio/mp4:aac,mp4,mpg4:MPEG-4 audio;"
    "audio/x-m4a:m4a:MPEG-4 audio;"
52
    "audio/m4a:m4a:MPEG-4 audio;"
53 54 55 56 57 58 59 60
    /* MPEG-4 ASP */
    "video/mp4:mp4,mpg4:MPEG-4 video;"
    "application/mpeg4-iod:mp4,mpg4:MPEG-4 video;"
    "application/mpeg4-muxcodetable:mp4,mpg4:MPEG-4 video;"
    "video/x-m4v:m4v:MPEG-4 video;"
    /* AVI */
    "video/x-msvideo:avi:AVI video;"
    /* QuickTime */
61
    "video/quicktime:mov,qt:QuickTime video;"
62 63 64
    /* OGG */
    "application/ogg:ogg:Ogg stream;"
    "video/ogg:ogv:Ogg video;"
65
    "audio/ogg:oga:Ogg audio;"
66
    "application/x-ogg:ogg:Ogg stream;"
67 68
    /* Opus */
    "audio/ogg;codecs=opus:opus:Opus audio;"
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 109 110 111
    /* VLC */
    "application/x-vlc-plugin::VLC plug-in;"
    /* Windows Media */
    "video/x-ms-asf-plugin:asf,asx:Windows Media Video;"
    "video/x-ms-asf:asf,asx:Windows Media Video;"
    "application/x-mplayer2::Windows Media;"
    "video/x-ms-wmv:wmv:Windows Media;"
    "video/x-ms-wvx:wvx:Windows Media Video;"
    "audio/x-ms-wma:wma:Windows Media Audio;"
    /* Google VLC */
    "application/x-google-vlc-plugin::Google VLC plug-in;"
    /* WAV audio */
    "audio/wav:wav:WAV audio;"
    "audio/x-wav:wav:WAV audio;"
    /* 3GPP */
    "audio/3gpp:3gp,3gpp:3GPP audio;"
    "video/3gpp:3gp,3gpp:3GPP video;"
    /* 3GPP2 */
    "audio/3gpp2:3g2,3gpp2:3GPP2 audio;"
    "video/3gpp2:3g2,3gpp2:3GPP2 video;"
    /* DIVX */
    "video/divx:divx:DivX video;"
    /* FLV */
    "video/flv:flv:FLV video;"
    "video/x-flv:flv:FLV video;"
    /* Matroska */
    "application/x-matroska:mkv:Matroska video;"
    "video/x-matroska:mkv:Matroska video;"
    "audio/x-matroska:mka:Matroska audio;"
    /* XSPF */
    "application/xspf+xml:xspf:Playlist xspf;"
    /* M3U */
    "audio/x-mpegurl:m3u:MPEG audio;"
    /* Webm */
    "video/webm:webm:WebM video;"
    "audio/webm:webm:WebM audio;"
    /* Real Media */
    "application/vnd.rn-realmedia:rm:Real Media File;"
    "audio/x-realaudio:ra:Real Media Audio;"
    /* AMR */
    "audio/amr:amr:AMR audio;"
    /* FLAC */
    "audio/x-flac:flac:FLAC audio;"
112
    "audio/flac:flac:FLAC audio;"
113
    ;
114 115 116 117

/******************************************************************************
 * UNIX-only API calls
 *****************************************************************************/
118
NPP_GET_MIME_CONST char * NPP_GetMIMEDescription( void )
119
{
120
    return mimetype;
121 122 123 124
}

NPError NPP_GetValue( NPP instance, NPPVariable variable, void *value )
{
125
    static char psz_name[] = PLUGIN_NAME;
Sam Hocevar's avatar
Sam Hocevar committed
126
    static char psz_desc[1000];
127

128
    /* plugin class variables */
129 130 131
    switch( variable )
    {
        case NPPVpluginNameString:
132
            *((char **)value) = psz_name;
133 134 135
            return NPERR_NO_ERROR;

        case NPPVpluginDescriptionString:
136
            snprintf( psz_desc, sizeof(psz_desc), PLUGIN_DESCRIPTION,
137
                      libvlc_get_version() );
Sam Hocevar's avatar
Sam Hocevar committed
138
            *((char **)value) = psz_desc;
139
            return NPERR_NO_ERROR;
Sam Hocevar's avatar
Sam Hocevar committed
140

141 142 143 144 145 146
#if defined(XP_UNIX)
        case NPPVpluginNeedsXEmbed:
            *((bool *)value) = true;
            return NPERR_NO_ERROR;
#endif

Sam Hocevar's avatar
Sam Hocevar committed
147
        default:
148
            /* move on to instance variables ... */
149
            ;
150 151 152 153 154 155 156
    }

    if( instance == NULL )
    {
        return NPERR_INVALID_INSTANCE_ERROR;
    }

157
    /* plugin instance variables */
158

159
    VlcPluginBase *p_plugin = reinterpret_cast<VlcPluginBase *>(instance->pdata);
160
    if( NULL == p_plugin )
161
    {
162
        /* plugin has not been initialized yet ! */
163 164
        return NPERR_INVALID_INSTANCE_ERROR;
    }
165

166 167
    switch( variable )
    {
168
        case NPPVpluginScriptableNPObject:
169
        {
170 171 172
            /* retrieve plugin root class */
            NPClass *scriptClass = p_plugin->getScriptClass();
            if( scriptClass )
173
            {
174 175 176
                /* create an instance and return it */
                *(NPObject**)value = NPN_CreateObject(instance, scriptClass);
                return NPERR_NO_ERROR;
177 178
            }
            break;
179
        }
180 181 182 183
#if defined(XP_MACOSX)
        case NPPVpluginCoreAnimationLayer:
        {
            if( instance )
184
                return p_plugin->get_root_layer(value);
185 186 187 188

            break;
        }
#endif
189

190
        default:
191
            ;
192
    }
193
    return NPERR_GENERIC_ERROR;
194 195
}

196
/*
197
 * there is some confusion in NPAPI headers regarding definition of this API
198 199 200
 * NPPVariable is wrongly defined as NPNVariable, which sounds incorrect.
 */

201
NPError NPP_SetValue( NPP, NPNVariable, void * )
202 203 204 205
{
    return NPERR_GENERIC_ERROR;
}

206 207 208 209 210
/******************************************************************************
 * General Plug-in Calls
 *****************************************************************************/
NPError NPP_Initialize( void )
{
211 212 213 214 215 216 217 218 219 220 221
#ifdef XP_UNIX
    NPError err = NPERR_NO_ERROR;
    bool supportsXEmbed = false;

    err = NPN_GetValue( NULL, NPNVSupportsXEmbedBool,
                        (void *)&supportsXEmbed );

    if ( err != NPERR_NO_ERROR || supportsXEmbed != true )
        return NPERR_INCOMPATIBLE_VERSION_ERROR;
#endif

222 223 224 225 226 227 228 229
    return NPERR_NO_ERROR;
}

void NPP_Shutdown( void )
{
    ;
}

230 231 232 233 234 235
static bool boolValue(const char *value) {
    return ( !strcmp(value, "1") ||
             !strcasecmp(value, "true") ||
             !strcasecmp(value, "yes") );
}

236
NPError NPP_New( NPMIMEType, NPP instance,
237
                 NPuint16_t mode, NPint16_t argc,
238
                 char* argn[], char* argv[], NPSavedData* )
239
{
240
    NPError status;
241
    VlcPluginBase *p_plugin;
242 243 244 245 246 247

    if( instance == NULL )
    {
        return NPERR_INVALID_INSTANCE_ERROR;
    }

248
    bool windowless = false;
249 250 251 252 253 254 255 256 257 258 259 260 261
    /* we need to tell whether the plugin will be windowless
     * before it is instantiated */
    for( int i = 0; i < argc; i++ )
    {
        if( !strcmp( argn[i], "windowless" ) )
        {
            windowless = boolValue(argv[i]);
            break;
        }
    }

    if( windowless )
    {
262
        printf( "Using Windowless mode\n" );
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
        /* set windowless flag */
        status = NPN_SetValue( instance, NPPVpluginWindowBool,
                                       (void *)false);
        if( NPERR_NO_ERROR != status )
        {
            return status;
        }
        status = NPN_SetValue( instance, NPPVpluginTransparentBool,
                                       (void *)false);
        if( NPERR_NO_ERROR != status )
        {
            return status;
        }

        p_plugin = new VlcWindowless( instance, mode );
    }
    else
    {
        p_plugin = new VlcPlugin( instance, mode );
    }
283
    if( NULL == p_plugin )
284 285 286 287
    {
        return NPERR_OUT_OF_MEMORY_ERROR;
    }

288
    status = p_plugin->init(argc, argn, argv);
289 290
    if( NPERR_NO_ERROR == status )
    {
291
        instance->pdata = reinterpret_cast<void*>(p_plugin);
292
    }
293 294
    else
    {
295 296
        delete p_plugin;
    }
297
    return status;
298 299
}

300
NPError NPP_Destroy( NPP instance, NPSavedData** )
301
{
302
    if( NULL == instance )
303 304
        return NPERR_INVALID_INSTANCE_ERROR;

305
    VlcPluginBase *p_plugin = reinterpret_cast<VlcPluginBase *>(instance->pdata);
306 307 308 309
    if( NULL == p_plugin )
        return NPERR_NO_ERROR;

    instance->pdata = NULL;
310

311 312
    if( p_plugin->playlist_isplaying() )
        p_plugin->playlist_stop();
313

314
    delete p_plugin;
315 316 317 318 319 320

    return NPERR_NO_ERROR;
}

NPError NPP_SetWindow( NPP instance, NPWindow* window )
{
321
    if( ! instance )
322 323 324 325
    {
        return NPERR_INVALID_INSTANCE_ERROR;
    }

326
    /* NPP_SetWindow may be called before NPP_New (Opera) */
327
    VlcPluginBase *p_plugin = reinterpret_cast<VlcPluginBase *>(instance->pdata);
328
    if( NULL == p_plugin )
329 330 331 332
    {
        /* we should probably show a splash screen here */
        return NPERR_NO_ERROR;
    }
Jean-Paul Saman's avatar
Jean-Paul Saman committed
333

334
    p_plugin->getVLC();
335

336 337 338 339 340 341 342 343
    /*
     * PLUGIN DEVELOPERS:
     *  Before setting window to point to the
     *  new window, you may wish to compare the new window
     *  info to the previous window (if any) to note window
     *  size changes, etc.
     */

344
    /* retrieve current window */
345
    NPWindow& curr_window = p_plugin->getWindow();
346

347
    if (window/* && window->window */) {
348 349
        if (!curr_window.window) {
            /* we've just been created */
350
            p_plugin->setWindow(*window);
351 352
            p_plugin->create_windows();
            p_plugin->resize_windows();
353
            p_plugin->set_player_window();
354 355

            /* now set plugin state to that requested in parameters */
356 357
            bool show_toolbar = p_plugin->get_options().get_show_toolbar();
            p_plugin->set_toolbar_visible( show_toolbar );
358 359 360 361 362 363 364 365

            /* handle streams properly */
            if( !p_plugin->b_stream )
            {
                if( p_plugin->psz_target )
                {
                    if( p_plugin->playlist_add( p_plugin->psz_target ) != -1 )
                    {
366
                        if( p_plugin->get_options().get_autoplay() )
367 368 369 370 371 372 373
                        {
                            p_plugin->playlist_play();
                        }
                    }
                    p_plugin->b_stream = true;
                }
            }
374 375

            p_plugin->update_controls();
376 377 378
        } else {
            if (window->window == curr_window.window) {
                /* resize / move notification */
379
                p_plugin->setWindow(*window);
380 381
                p_plugin->resize_windows();
            } else {
382
                /* plugin parent window was changed, notify plugin about it */
383 384 385 386
                p_plugin->destroy_windows();
                p_plugin->setWindow(*window);
                p_plugin->create_windows();
                p_plugin->resize_windows();
387
            }
388
        }
389
    } else {
390 391
        /* NOTE: on Windows, Opera does not call NPP_SetWindow
         * on window destruction. */
392 393 394 395
        if (curr_window.window) {
            /* we've been destroyed */
            p_plugin->destroy_windows();
        }
396
    }
397 398 399
    return NPERR_NO_ERROR;
}

400 401
NPError NPP_NewStream( NPP instance, NPMIMEType, NPStream *stream,
                       NPBool, NPuint16_t *stype )
402
{
403
    if( NULL == instance  )
404 405 406 407
    {
        return NPERR_INVALID_INSTANCE_ERROR;
    }

408
    VlcPluginBase *p_plugin = reinterpret_cast<VlcPluginBase *>(instance->pdata);
409 410 411 412
    if( NULL == p_plugin )
    {
        return NPERR_INVALID_INSTANCE_ERROR;
    }
413

414 415 416 417 418 419 420 421
   /*
   ** Firefox/Mozilla may decide to open a stream from the URL specified
   ** in the SRC parameter of the EMBED tag and pass it to us
   **
   ** since VLC will open the SRC URL as well, we're not interested in
   ** that stream. Otherwise, we'll take it and queue it up in the playlist
   */
    if( !p_plugin->psz_target || strcmp(stream->url, p_plugin->psz_target) )
422
    {
423 424 425
        /* TODO: use pipes !!!! */
        *stype = NP_ASFILEONLY;
        return NPERR_NO_ERROR;
426
    }
427
    return NPERR_GENERIC_ERROR;
428 429
}

430
NPint32_t NPP_WriteReady( NPP, NPStream *)
431
{
432 433
    /* TODO */
    return 8*1024;
434 435
}

436 437
NPint32_t NPP_Write( NPP, NPStream *, NPint32_t,
                 NPint32_t len, void * )
438
{
439 440
    /* TODO */
    return len;
441 442
}

443
NPError NPP_DestroyStream( NPP instance, NPStream *, NPError )
444 445 446 447 448 449 450 451
{
    if( instance == NULL )
    {
        return NPERR_INVALID_INSTANCE_ERROR;
    }
    return NPERR_NO_ERROR;
}

452
void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* )
453 454 455 456 457 458
{
    if( instance == NULL )
    {
        return;
    }

459
    VlcPluginBase *p_plugin = reinterpret_cast<VlcPluginBase *>(instance->pdata);
460 461 462 463
    if( NULL == p_plugin )
    {
        return;
    }
464

465
    if( p_plugin->playlist_add( stream->url ) != -1 )
466
    {
467
        if( p_plugin->get_options().get_autoplay() )
468
        {
469
            p_plugin->playlist_play();
470 471
        }
    }
472
}
473

474 475
void NPP_URLNotify( NPP instance, const char* ,
                    NPReason, void* )
476 477 478 479 480 481 482 483 484 485
{
    /***** Insert NPP_URLNotify code here *****\
    PluginInstance* p_plugin;
    if (instance != NULL)
        p_plugin = (PluginInstance*) instance->pdata;
    \*********************************************/
}

void NPP_Print( NPP instance, NPPrint* printInfo )
{
486 487
    if( printInfo == NULL )
    {
488
        return;
489
    }
490

491 492 493
    if( instance != NULL )
    {
        /***** Insert NPP_Print code here *****\
494
        PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
495
        \**************************************/
496

497 498
        if( printInfo->mode == NP_FULL )
        {
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
            /*
             * PLUGIN DEVELOPERS:
             *  If your plugin would like to take over
             *  printing completely when it is in full-screen mode,
             *  set printInfo->pluginPrinted to TRUE and print your
             *  plugin as you see fit.  If your plugin wants Netscape
             *  to handle printing in this case, set
             *  printInfo->pluginPrinted to FALSE (the default) and
             *  do nothing.  If you do want to handle printing
             *  yourself, printOne is true if the print button
             *  (as opposed to the print menu) was clicked.
             *  On the Macintosh, platformPrint is a THPrint; on
             *  Windows, platformPrint is a structure
             *  (defined in npapi.h) containing the printer name, port,
             *  etc.
             */

516
            /***** Insert NPP_Print code here *****\
517 518 519 520
            void* platformPrint =
                printInfo->print.fullPrint.platformPrint;
            NPBool printOne =
                printInfo->print.fullPrint.printOne;
521
            \**************************************/
522 523

            /* Do the default*/
524
            printInfo->print.fullPrint.pluginPrinted = false;
525
        }
526 527 528
        else
        {
            /* If not fullscreen, we must be embedded */
529 530 531 532 533 534 535 536 537 538 539 540
            /*
             * PLUGIN DEVELOPERS:
             *  If your plugin is embedded, or is full-screen
             *  but you returned false in pluginPrinted above, NPP_Print
             *  will be called with mode == NP_EMBED.  The NPWindow
             *  in the printInfo gives the location and dimensions of
             *  the embedded plugin on the printed page.  On the
             *  Macintosh, platformPrint is the printer port; on
             *  Windows, platformPrint is the handle to the printing
             *  device context.
             */

541
            /***** Insert NPP_Print code here *****\
542 543 544 545
            NPWindow* printWindow =
                &(printInfo->print.embedPrint.window);
            void* platformPrint =
                printInfo->print.embedPrint.platformPrint;
546
            \**************************************/
547 548 549
        }
    }
}
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566

int16_t NPP_HandleEvent( NPP instance, void * event )
{
    if( instance == NULL )
    {
        return false;
    }

    VlcPluginBase *p_plugin = reinterpret_cast<VlcPluginBase *>(instance->pdata);
    if( p_plugin == NULL )
    {
        return false;
    }

    return p_plugin->handle_event(event);

}