npolibvlc.cpp 52.1 KB
Newer Older
Rafaël Carré's avatar
Rafaël Carré committed
1 2 3
/*****************************************************************************
 * npolibvlc.cpp: official Javascript APIs
 *****************************************************************************
4
 * Copyright (C) 2002-2014 VLC authors and VideoLAN
5
 * Copyright (C) 2010 M2X BV
Rafaël Carré's avatar
Rafaël Carré committed
6 7
 *
 * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
8
 *          JP Dinger <jpd@videolan.org>
9
 *          Felix Paul Kühne <fkuehne # videolan.org>
Rafaël Carré's avatar
Rafaël Carré committed
10 11 12 13 14 15 16 17 18 19 20
 *
 * 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.
 *
21 22 23
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Rafaël Carré's avatar
Rafaël Carré committed
24 25 26 27 28 29 30 31 32
 *****************************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "vlcplugin.h"
#include "npolibvlc.h"

33
#include "../../common/position.h"
34

35 36 37
/*
** Local helper macros and function
*/
38
#define COUNTNAMES(a,b,c) const int a::b = sizeof(a::c)/sizeof(NPUTF8 *)
39 40 41 42 43
#define RETURN_ON_ERROR                             \
    do {                                            \
        NPN_SetException(this, libvlc_errmsg());    \
        return INVOKERESULT_GENERIC_ERROR;          \
    }while(0)
44

45 46 47 48
#define ERROR_EVENT_NOT_FOUND "ERROR: One or more events could not be found."
#define ERROR_API_VERSION "ERROR: NPAPI version not high enough. (Gecko >= 1.9 needed)"


Rafaël Carré's avatar
Rafaël Carré committed
49 50 51 52 53 54 55
/*
** implementation of libvlc root object
*/

LibvlcRootNPObject::~LibvlcRootNPObject()
{
    /*
56 57 58 59 60 61
    ** When the plugin is destroyed, firefox takes it upon itself to
    ** destroy all 'live' script objects and ignores refcounting.
    ** Therefore we cannot safely assume that refcounting will control
    ** lifespan of objects. Hence they are only lazily created on
    ** request, so that firefox can take ownership, and are not released
    ** when the plugin is destroyed.
Rafaël Carré's avatar
Rafaël Carré committed
62 63 64 65 66 67
    */
    if( isValid() )
    {
        if( audioObj    ) NPN_ReleaseObject(audioObj);
        if( inputObj    ) NPN_ReleaseObject(inputObj);
        if( playlistObj ) NPN_ReleaseObject(playlistObj);
68
        if( subtitleObj ) NPN_ReleaseObject(subtitleObj);
Rafaël Carré's avatar
Rafaël Carré committed
69
        if( videoObj    ) NPN_ReleaseObject(videoObj);
70
        if( mediaDescriptionObj ) NPN_ReleaseObject(mediaDescriptionObj);
Rafaël Carré's avatar
Rafaël Carré committed
71 72 73
    }
}

74
const NPUTF8 * const LibvlcRootNPObject::propertyNames[] =
Rafaël Carré's avatar
Rafaël Carré committed
75 76 77 78
{
    "audio",
    "input",
    "playlist",
79
    "subtitle",
Rafaël Carré's avatar
Rafaël Carré committed
80 81
    "video",
    "VersionInfo",
82
    "mediaDescription"
Rafaël Carré's avatar
Rafaël Carré committed
83
};
84
COUNTNAMES(LibvlcRootNPObject,propertyCount,propertyNames);
Rafaël Carré's avatar
Rafaël Carré committed
85 86 87 88 89 90

enum LibvlcRootNPObjectPropertyIds
{
    ID_root_audio = 0,
    ID_root_input,
    ID_root_playlist,
91
    ID_root_subtitle,
Rafaël Carré's avatar
Rafaël Carré committed
92 93
    ID_root_video,
    ID_root_VersionInfo,
94
    ID_root_MediaDescription,
Rafaël Carré's avatar
Rafaël Carré committed
95 96
};

97
RuntimeNPObject::InvokeResult
98
LibvlcRootNPObject::getProperty(int index, npapi::OutVariant &result)
Rafaël Carré's avatar
Rafaël Carré committed
99 100
{
    /* is plugin still running */
101
    if( isPluginRunning() )
Rafaël Carré's avatar
Rafaël Carré committed
102 103 104 105
    {
        switch( index )
        {
            case ID_root_audio:
106
                InstantObj<LibvlcAudioNPObject>( audioObj );
107
                result = audioObj;
Rafaël Carré's avatar
Rafaël Carré committed
108 109
                return INVOKERESULT_NO_ERROR;
            case ID_root_input:
110
                InstantObj<LibvlcInputNPObject>( inputObj );
111
                result = inputObj;
Rafaël Carré's avatar
Rafaël Carré committed
112 113
                return INVOKERESULT_NO_ERROR;
            case ID_root_playlist:
114
                InstantObj<LibvlcPlaylistNPObject>( playlistObj );
115
                result = playlistObj;
Rafaël Carré's avatar
Rafaël Carré committed
116
                return INVOKERESULT_NO_ERROR;
117
            case ID_root_subtitle:
118
                InstantObj<LibvlcSubtitleNPObject>( subtitleObj );
119
                result = subtitleObj;
120
                return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
121
            case ID_root_video:
122
                InstantObj<LibvlcVideoNPObject>( videoObj );
123
                result = videoObj;
Rafaël Carré's avatar
Rafaël Carré committed
124 125
                return INVOKERESULT_NO_ERROR;
            case ID_root_VersionInfo:
126 127
                result = libvlc_get_version();
                return INVOKERESULT_NO_ERROR;
128 129 130
            case ID_root_MediaDescription:
            {
                InstantObj<LibvlcMediaDescriptionNPObject>( mediaDescriptionObj );
131
                result = mediaDescriptionObj;
132 133
                return INVOKERESULT_NO_ERROR;
            }
Rafaël Carré's avatar
Rafaël Carré committed
134 135 136 137 138 139 140 141 142 143
            default:
                ;
        }
    }
    return INVOKERESULT_GENERIC_ERROR;
}

const NPUTF8 * const LibvlcRootNPObject::methodNames[] =
{
    "versionInfo",
144
    "getVersionInfo",
145 146
    "addEventListener",
    "removeEventListener",
Rafaël Carré's avatar
Rafaël Carré committed
147
};
148
COUNTNAMES(LibvlcRootNPObject,methodCount,methodNames);
Rafaël Carré's avatar
Rafaël Carré committed
149 150 151 152

enum LibvlcRootNPObjectMethodIds
{
    ID_root_versionInfo,
153
    ID_root_getVersionInfo,
154 155
    ID_root_addeventlistener,
    ID_root_removeeventlistener,
Rafaël Carré's avatar
Rafaël Carré committed
156 157
};

158
RuntimeNPObject::InvokeResult LibvlcRootNPObject::invoke(int index,
159
                  const NPVariant *args, uint32_t argCount, npapi::OutVariant& result)
Rafaël Carré's avatar
Rafaël Carré committed
160 161
{
    /* is plugin still running */
162 163 164 165
    if( !isPluginRunning() )
        return INVOKERESULT_GENERIC_ERROR;

    switch( index )
Rafaël Carré's avatar
Rafaël Carré committed
166
    {
167
    case ID_root_versionInfo:
168
    case ID_root_getVersionInfo:
169 170
        if( 0 != argCount )
            return INVOKERESULT_NO_SUCH_METHOD;
171 172
        result = libvlc_get_version();
        return INVOKERESULT_NO_ERROR;
173 174 175

    case ID_root_addeventlistener:
    case ID_root_removeeventlistener:
176 177 178 179
        if ( argCount < 2 )
            return INVOKERESULT_INVALID_ARGS;

        // Don't wrap eventName as it would copy the string even though it's not required.
180
        const npapi::Variant listener( args[1] );
181 182 183

        if ( !npapi::is_string( args[0] ) ||
            !listener.is<NPObject>() )
184
            return INVOKERESULT_INVALID_ARGS;
Rafaël Carré's avatar
Rafaël Carré committed
185

186
        if( !VlcPluginBase::canUseEventListener() )
Rafaël Carré's avatar
Rafaël Carré committed
187
        {
188 189
            NPN_SetException(this, ERROR_API_VERSION);
            return INVOKERESULT_GENERIC_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
190
        }
191

192
        VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
193

Jean-Paul Saman's avatar
Jean-Paul Saman committed
194 195
        if( ID_root_addeventlistener == index )
        {
196
            p_plugin->subscribe( npapi::to_tmp_string( args[0] ), listener );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
197
        }
198
        else
Jean-Paul Saman's avatar
Jean-Paul Saman committed
199
        {
200
            p_plugin->unsubscribe( npapi::to_tmp_string( args[0] ), listener );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
201
        }
202
        return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
203
    }
204
    return INVOKERESULT_NO_SUCH_METHOD;
Rafaël Carré's avatar
Rafaël Carré committed
205 206 207 208 209 210
}

/*
** implementation of libvlc audio object
*/

211
const NPUTF8 * const LibvlcAudioNPObject::propertyNames[] =
Rafaël Carré's avatar
Rafaël Carré committed
212 213 214 215
{
    "mute",
    "volume",
    "track",
216
    "count",
Rafaël Carré's avatar
Rafaël Carré committed
217 218
    "channel",
};
219
COUNTNAMES(LibvlcAudioNPObject,propertyCount,propertyNames);
Rafaël Carré's avatar
Rafaël Carré committed
220 221 222 223 224 225

enum LibvlcAudioNPObjectPropertyIds
{
    ID_audio_mute,
    ID_audio_volume,
    ID_audio_track,
226
    ID_audio_count,
Rafaël Carré's avatar
Rafaël Carré committed
227 228 229
    ID_audio_channel,
};

230
RuntimeNPObject::InvokeResult
231
LibvlcAudioNPObject::getProperty(int index, npapi::OutVariant& result)
Rafaël Carré's avatar
Rafaël Carré committed
232 233
{
    /* is plugin still running */
234
    if( isPluginRunning() )
Rafaël Carré's avatar
Rafaël Carré committed
235
    {
236
        VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
Rafaël Carré's avatar
Rafaël Carré committed
237

238 239
        auto& mp = p_plugin->getMD();
        if( !mp )
240
            RETURN_ON_ERROR;
Rémi Duraffort's avatar
Rémi Duraffort committed
241

Rafaël Carré's avatar
Rafaël Carré committed
242 243 244 245
        switch( index )
        {
            case ID_audio_mute:
            {
246
                result = mp.mute();
Rafaël Carré's avatar
Rafaël Carré committed
247 248 249 250
                return INVOKERESULT_NO_ERROR;
            }
            case ID_audio_volume:
            {
251
                result = mp.volume();
Rafaël Carré's avatar
Rafaël Carré committed
252 253 254 255
                return INVOKERESULT_NO_ERROR;
            }
            case ID_audio_track:
            {
256
                result = p_plugin->player().currentAudioTrack();
Rafaël Carré's avatar
Rafaël Carré committed
257 258
                return INVOKERESULT_NO_ERROR;
            }
259 260
            case ID_audio_count:
            {
261
                result = mp.audioTrackCount();
262 263
                return INVOKERESULT_NO_ERROR;
            }
Rafaël Carré's avatar
Rafaël Carré committed
264 265
            case ID_audio_channel:
            {
266
                result = mp.channel();
Rafaël Carré's avatar
Rafaël Carré committed
267 268 269 270 271 272 273 274 275
                return INVOKERESULT_NO_ERROR;
            }
            default:
                ;
        }
    }
    return INVOKERESULT_GENERIC_ERROR;
}

276 277
RuntimeNPObject::InvokeResult
LibvlcAudioNPObject::setProperty(int index, const NPVariant &value)
Rafaël Carré's avatar
Rafaël Carré committed
278 279
{
    /* is plugin still running */
280
    if( isPluginRunning() )
Rafaël Carré's avatar
Rafaël Carré committed
281
    {
282
        VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
Rafaël Carré's avatar
Rafaël Carré committed
283

284 285
        auto& mp = p_plugin->getMD();
        if( !mp )
286
            RETURN_ON_ERROR;
Rémi Duraffort's avatar
Rémi Duraffort committed
287

288
        const npapi::Variant v( value );
Rafaël Carré's avatar
Rafaël Carré committed
289 290 291
        switch( index )
        {
            case ID_audio_mute:
292
                if( v.is<bool>() )
Rafaël Carré's avatar
Rafaël Carré committed
293
                {
294
                    mp.setMute( v );
Rafaël Carré's avatar
Rafaël Carré committed
295 296 297 298
                    return INVOKERESULT_NO_ERROR;
                }
                return INVOKERESULT_INVALID_VALUE;
            case ID_audio_volume:
299
                if( v.is<int>() )
Rafaël Carré's avatar
Rafaël Carré committed
300
                {
301
                    mp.setVolume( v );
Rafaël Carré's avatar
Rafaël Carré committed
302 303 304 305
                    return INVOKERESULT_NO_ERROR;
                }
                return INVOKERESULT_INVALID_VALUE;
            case ID_audio_track:
306
                if( v.is<int>() )
Rafaël Carré's avatar
Rafaël Carré committed
307
                {
308 309 310 311
                    auto tracks = mp.audioTrackDescription();
                    if ( v >= tracks.size() )
                        return INVOKERESULT_INVALID_VALUE;
                    if ( mp.setAudioTrack( tracks[v].id() ) )
312
                        return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
313 314 315
                }
                return INVOKERESULT_INVALID_VALUE;
            case ID_audio_channel:
316
                if( v.is<int>() )
Rafaël Carré's avatar
Rafaël Carré committed
317
                {
318
                    if ( mp.setChannel( v ) )
319
                        return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
320 321 322 323 324 325 326 327 328 329 330 331
                }
                return INVOKERESULT_INVALID_VALUE;
            default:
                ;
        }
    }
    return INVOKERESULT_GENERIC_ERROR;
}

const NPUTF8 * const LibvlcAudioNPObject::methodNames[] =
{
    "toggleMute",
332
    "description",
Rafaël Carré's avatar
Rafaël Carré committed
333
};
334
COUNTNAMES(LibvlcAudioNPObject,methodCount,methodNames);
Rafaël Carré's avatar
Rafaël Carré committed
335 336 337 338

enum LibvlcAudioNPObjectMethodIds
{
    ID_audio_togglemute,
339
    ID_audio_description,
Rafaël Carré's avatar
Rafaël Carré committed
340 341
};

342 343
RuntimeNPObject::InvokeResult
LibvlcAudioNPObject::invoke(int index, const NPVariant *args,
344
                            uint32_t argCount, npapi::OutVariant& result)
Rafaël Carré's avatar
Rafaël Carré committed
345 346
{
    /* is plugin still running */
347
    if( isPluginRunning() )
Rafaël Carré's avatar
Rafaël Carré committed
348
    {
349
        VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
350 351
        auto& mp = p_plugin->getMD();
        if( !mp )
352
            RETURN_ON_ERROR;
Rémi Duraffort's avatar
Rémi Duraffort committed
353

Rafaël Carré's avatar
Rafaël Carré committed
354 355 356 357 358
        switch( index )
        {
            case ID_audio_togglemute:
                if( argCount == 0 )
                {
359
                    mp.toggleMute();
360
                    return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
361 362
                }
                return INVOKERESULT_NO_SUCH_METHOD;
363 364
            case ID_audio_description:
            {
365 366
                if ( argCount < 1 )
                    return INVOKERESULT_INVALID_ARGS;
367
                const npapi::Variant v( args[0] );
368
                if( v.is<int>() )
369
                {
370
                    auto tracks = mp.audioTrackDescription();
371
                    if ( v >= tracks.size() )
372 373
                        return INVOKERESULT_INVALID_VALUE;
                    /* display the name of the track chosen */
374 375
                    result = tracks[v].name();
                    return INVOKERESULT_NO_ERROR;
376 377 378
                }
                return INVOKERESULT_NO_SUCH_METHOD;
            }
Rafaël Carré's avatar
Rafaël Carré committed
379 380 381 382 383 384 385 386 387 388 389
            default:
                ;
        }
    }
    return INVOKERESULT_GENERIC_ERROR;
}

/*
** implementation of libvlc input object
*/

390
const NPUTF8 * const LibvlcInputNPObject::propertyNames[] =
Rafaël Carré's avatar
Rafaël Carré committed
391 392 393 394 395 396 397 398 399
{
    "length",
    "position",
    "time",
    "state",
    "rate",
    "fps",
    "hasVout",
};
400
COUNTNAMES(LibvlcInputNPObject,propertyCount,propertyNames);
Rafaël Carré's avatar
Rafaël Carré committed
401 402 403 404 405 406 407 408 409 410 411 412

enum LibvlcInputNPObjectPropertyIds
{
    ID_input_length,
    ID_input_position,
    ID_input_time,
    ID_input_state,
    ID_input_rate,
    ID_input_fps,
    ID_input_hasvout,
};

413
RuntimeNPObject::InvokeResult
414
LibvlcInputNPObject::getProperty(int index, npapi::OutVariant& result)
Rafaël Carré's avatar
Rafaël Carré committed
415 416
{
    /* is plugin still running */
417
    if( isPluginRunning() )
Rafaël Carré's avatar
Rafaël Carré committed
418
    {
419
        VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
420 421
        auto& mp = p_plugin->getMD();
        if( !mp )
Rafaël Carré's avatar
Rafaël Carré committed
422 423
        {
            if( index != ID_input_state )
424
                RETURN_ON_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
425 426 427
            else
            {
                /* for input state, return CLOSED rather than an exception */
428
                result = 0;
Rafaël Carré's avatar
Rafaël Carré committed
429 430 431 432 433 434 435 436
                return INVOKERESULT_NO_ERROR;
            }
        }

        switch( index )
        {
            case ID_input_length:
            {
437
                result = mp.length();
Rafaël Carré's avatar
Rafaël Carré committed
438 439 440 441
                return INVOKERESULT_NO_ERROR;
            }
            case ID_input_position:
            {
442
                result = mp.position();
Rafaël Carré's avatar
Rafaël Carré committed
443 444 445 446
                return INVOKERESULT_NO_ERROR;
            }
            case ID_input_time:
            {
447
                result = mp.time();
Rafaël Carré's avatar
Rafaël Carré committed
448 449 450 451
                return INVOKERESULT_NO_ERROR;
            }
            case ID_input_state:
            {
452
                result = (int)mp.state();
Rafaël Carré's avatar
Rafaël Carré committed
453 454 455 456
                return INVOKERESULT_NO_ERROR;
            }
            case ID_input_rate:
            {
457
                result = mp.rate();
Rafaël Carré's avatar
Rafaël Carré committed
458 459 460 461
                return INVOKERESULT_NO_ERROR;
            }
            case ID_input_fps:
            {
462
                result = mp.fps();
Rafaël Carré's avatar
Rafaël Carré committed
463 464 465 466
                return INVOKERESULT_NO_ERROR;
            }
            case ID_input_hasvout:
            {
467
                result = p_plugin->player().get_mp().hasVout() != 0;
Rafaël Carré's avatar
Rafaël Carré committed
468 469 470 471 472 473 474 475 476
                return INVOKERESULT_NO_ERROR;
            }
            default:
                ;
        }
    }
    return INVOKERESULT_GENERIC_ERROR;
}

477 478
RuntimeNPObject::InvokeResult
LibvlcInputNPObject::setProperty(int index, const NPVariant &value)
Rafaël Carré's avatar
Rafaël Carré committed
479 480
{
    /* is plugin still running */
481
    if( isPluginRunning() )
Rafaël Carré's avatar
Rafaël Carré committed
482
    {
483
        VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
484 485
        auto& mp = p_plugin->getMD();
        if( !mp )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
486
            RETURN_ON_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
487

488
        const npapi::Variant v( value );
Rafaël Carré's avatar
Rafaël Carré committed
489 490 491 492
        switch( index )
        {
            case ID_input_position:
            {
493
                if( !v.is<double>() )
Rafaël Carré's avatar
Rafaël Carré committed
494 495 496 497
                {
                    return INVOKERESULT_INVALID_VALUE;
                }

498
                mp.setPosition( v );
Rafaël Carré's avatar
Rafaël Carré committed
499 500 501 502
                return INVOKERESULT_NO_ERROR;
            }
            case ID_input_time:
            {
503
                if( !v.is<int>() )
Rafaël Carré's avatar
Rafaël Carré committed
504 505 506 507
                {
                    return INVOKERESULT_INVALID_VALUE;
                }

508
                mp.setTime( v );
Rafaël Carré's avatar
Rafaël Carré committed
509 510 511 512
                return INVOKERESULT_NO_ERROR;
            }
            case ID_input_rate:
            {
513
                if( !v.is<double>() )
Rafaël Carré's avatar
Rafaël Carré committed
514 515 516 517
                {
                    return INVOKERESULT_INVALID_VALUE;
                }

518
                mp.setRate( v );
Rafaël Carré's avatar
Rafaël Carré committed
519 520 521 522 523 524 525 526 527 528 529 530
                return INVOKERESULT_NO_ERROR;
            }
            default:
                ;
        }
    }
    return INVOKERESULT_GENERIC_ERROR;
}

const NPUTF8 * const LibvlcInputNPObject::methodNames[] =
{
    /* no methods */
531
    "none",
Rafaël Carré's avatar
Rafaël Carré committed
532
};
533
COUNTNAMES(LibvlcInputNPObject,methodCount,methodNames);
Rafaël Carré's avatar
Rafaël Carré committed
534

535 536 537 538 539 540
enum LibvlcInputNPObjectMethodIds
{
    ID_none,
};

RuntimeNPObject::InvokeResult
541 542
LibvlcInputNPObject::invoke(int index, const NPVariant *,
                            uint32_t, NPVariant &)
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
{
    /* is plugin still running */
    if( isPluginRunning() )
    {
        switch( index )
        {
            case ID_none:
                return INVOKERESULT_NO_SUCH_METHOD;
            default:
                ;
        }
    }
    return INVOKERESULT_GENERIC_ERROR;
}

558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
/*
** implementation of libvlc MediaDescription object
*/

const NPUTF8 * const LibvlcMediaDescriptionNPObject::propertyNames[] =
{
    "title",
    "artist",
    "genre",
    "copyright",
    "album",
    "trackNumber",
    "description",
    "rating",
    "date",
    "setting",
    "URL",
    "language",
    "nowPlaying",
    "publisher",
    "encodedBy",
    "artworkURL",
    "trackID",
};
COUNTNAMES(LibvlcMediaDescriptionNPObject,propertyCount,propertyNames);

enum LibvlcMediaDescriptionNPObjectPropertyIds
{
    ID_meta_title,
    ID_meta_artist,
    ID_meta_genre,
    ID_meta_copyright,
    ID_meta_album,
    ID_meta_trackNumber,
    ID_meta_description,
    ID_meta_rating,
    ID_meta_date,
    ID_meta_setting,
    ID_meta_URL,
    ID_meta_language,
    ID_meta_nowPlaying,
    ID_meta_publisher,
    ID_meta_encodedBy,
    ID_meta_artworkURL,
    ID_meta_trackID,
};
RuntimeNPObject::InvokeResult
605
LibvlcMediaDescriptionNPObject::getProperty(int index, npapi::OutVariant &result)
606 607 608 609 610
{
    /* is plugin still running */
    if( isPluginRunning() )
    {
        VlcPlugin* p_plugin = getPrivate<VlcPlugin>();
611 612
        auto& mp = p_plugin->getMD();
        if( !mp )
613
            RETURN_ON_ERROR;
614 615
        auto media = mp.media();
        if( !media )
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
            RETURN_ON_ERROR;
        switch( index )
        {
            case ID_meta_title:
            case ID_meta_artist:
            case ID_meta_genre:
            case ID_meta_copyright:
            case ID_meta_album:
            case ID_meta_trackNumber:
            case ID_meta_description:
            case ID_meta_rating:
            case ID_meta_date:
            case ID_meta_setting:
            case ID_meta_URL:
            case ID_meta_language:
            case ID_meta_nowPlaying:
            case ID_meta_publisher:
            case ID_meta_encodedBy:
            case ID_meta_artworkURL:
            case ID_meta_trackID:
636 637
            {
                auto m = media->meta( (libvlc_meta_t)index );
638 639
                result = m;
                return INVOKERESULT_NO_ERROR;
640
            }
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
            default:
            ;
        }
     }
     return INVOKERESULT_GENERIC_ERROR;
}

const NPUTF8 * const LibvlcMediaDescriptionNPObject::methodNames[] =
{
    "None"
};
COUNTNAMES(LibvlcMediaDescriptionNPObject,methodCount,methodNames);

enum LibvlcMediaDescriptionNPObjectMethodIds
{
    ID_mediadescription_method_none,
};


Rafaël Carré's avatar
Rafaël Carré committed
660 661 662 663
/*
** implementation of libvlc playlist items object
*/

664
const NPUTF8 * const LibvlcPlaylistItemsNPObject::propertyNames[] =
Rafaël Carré's avatar
Rafaël Carré committed
665 666 667
{
    "count",
};
668
COUNTNAMES(LibvlcPlaylistItemsNPObject,propertyCount,propertyNames);
Rafaël Carré's avatar
Rafaël Carré committed
669 670 671 672 673 674

enum LibvlcPlaylistItemsNPObjectPropertyIds
{
    ID_playlistitems_count,
};

675
RuntimeNPObject::InvokeResult
676
LibvlcPlaylistItemsNPObject::getProperty(int index, npapi::OutVariant& result)
Rafaël Carré's avatar
Rafaël Carré committed
677 678
{
    /* is plugin still running */
679
    if( isPluginRunning() )
Rafaël Carré's avatar
Rafaël Carré committed
680
    {
681
        VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
Rafaël Carré's avatar
Rafaël Carré committed
682 683 684 685 686

        switch( index )
        {
            case ID_playlistitems_count:
            {
687
                result = p_plugin->player().items_count();
Rafaël Carré's avatar
Rafaël Carré committed
688 689 690 691 692 693 694 695 696 697 698 699 700 701
                return INVOKERESULT_NO_ERROR;
            }
            default:
                ;
        }
    }
    return INVOKERESULT_GENERIC_ERROR;
}

const NPUTF8 * const LibvlcPlaylistItemsNPObject::methodNames[] =
{
    "clear",
    "remove",
};
702
COUNTNAMES(LibvlcPlaylistItemsNPObject,methodCount,methodNames);
Rafaël Carré's avatar
Rafaël Carré committed
703 704 705 706 707 708 709

enum LibvlcPlaylistItemsNPObjectMethodIds
{
    ID_playlistitems_clear,
    ID_playlistitems_remove,
};

710 711
RuntimeNPObject::InvokeResult
LibvlcPlaylistItemsNPObject::invoke(int index, const NPVariant *args,
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
712
                                    uint32_t argCount, npapi::OutVariant& )
Rafaël Carré's avatar
Rafaël Carré committed
713 714
{
    /* is plugin still running */
715
    if( isPluginRunning() )
Rafaël Carré's avatar
Rafaël Carré committed
716
    {
717
        VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
Rafaël Carré's avatar
Rafaël Carré committed
718 719 720 721 722 723

        switch( index )
        {
            case ID_playlistitems_clear:
                if( argCount == 0 )
                {
724
                    p_plugin->player().clear_items();
725
                    return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
726 727 728
                }
                return INVOKERESULT_NO_SUCH_METHOD;
            case ID_playlistitems_remove:
729 730
            {
                if ( argCount < 1 )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
731
                    return INVOKERESULT_INVALID_ARGS;
732
                const npapi::Variant v( args[0] );
733
                if( v.is<int>() )
Rafaël Carré's avatar
Rafaël Carré committed
734
                {
735
                    if( !p_plugin->player().delete_item( v ) )
736
                        return INVOKERESULT_GENERIC_ERROR;
737
                    return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
738 739
                }
                return INVOKERESULT_NO_SUCH_METHOD;
740
            }
Rafaël Carré's avatar
Rafaël Carré committed
741 742 743 744 745 746 747 748 749 750 751 752 753
            default:
                ;
        }
    }
    return INVOKERESULT_GENERIC_ERROR;
}

/*
** implementation of libvlc playlist object
*/

LibvlcPlaylistNPObject::~LibvlcPlaylistNPObject()
{
754 755 756
    // Why the isValid()?
    if( isValid() && playlistItemsObj )
        NPN_ReleaseObject(playlistItemsObj);
Rafaël Carré's avatar
Rafaël Carré committed
757 758
};

759
const NPUTF8 * const LibvlcPlaylistNPObject::propertyNames[] =
Rafaël Carré's avatar
Rafaël Carré committed
760 761 762
{
    "itemCount", /* deprecated */
    "isPlaying",
763
    "currentItem",
Rafaël Carré's avatar
Rafaël Carré committed
764 765
    "items",
};
766
COUNTNAMES(LibvlcPlaylistNPObject,propertyCount,propertyNames);
Rafaël Carré's avatar
Rafaël Carré committed
767 768 769 770 771

enum LibvlcPlaylistNPObjectPropertyIds
{
    ID_playlist_itemcount,
    ID_playlist_isplaying,
772
    ID_playlist_currentitem,
Rafaël Carré's avatar
Rafaël Carré committed
773 774 775
    ID_playlist_items,
};

776
RuntimeNPObject::InvokeResult
777
LibvlcPlaylistNPObject::getProperty(int index, npapi::OutVariant& result)
Rafaël Carré's avatar
Rafaël Carré committed
778 779
{
    /* is plugin still running */
780
    if( isPluginRunning() )
Rafaël Carré's avatar
Rafaël Carré committed
781
    {
782
        VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
Rafaël Carré's avatar
Rafaël Carré committed
783 784 785 786 787

        switch( index )
        {
            case ID_playlist_itemcount: /* deprecated */
            {
788
                result = p_plugin->player().items_count();
Rafaël Carré's avatar
Rafaël Carré committed
789 790 791 792
                return INVOKERESULT_NO_ERROR;
            }
            case ID_playlist_isplaying:
            {
793
                result = p_plugin->player().mlp().isPlaying();
Rafaël Carré's avatar
Rafaël Carré committed
794 795
                return INVOKERESULT_NO_ERROR;
            }
796 797
            case ID_playlist_currentitem:
            {
798
                result = p_plugin->player().current_item();
799 800
                return INVOKERESULT_NO_ERROR;
            }
Rafaël Carré's avatar
Rafaël Carré committed
801 802
            case ID_playlist_items:
            {
803
                InstantObj<LibvlcPlaylistItemsNPObject>( playlistItemsObj );
804
                result = playlistItemsObj;
Rafaël Carré's avatar
Rafaël Carré committed
805 806 807 808 809 810 811 812 813 814 815 816 817 818
                return INVOKERESULT_NO_ERROR;
            }
            default:
                ;
        }
    }
    return INVOKERESULT_GENERIC_ERROR;
}

const NPUTF8 * const LibvlcPlaylistNPObject::methodNames[] =
{
    "add",
    "play",
    "playItem",
819
    "pause",
Rafaël Carré's avatar
Rafaël Carré committed
820 821 822 823 824 825 826
    "togglePause",
    "stop",
    "next",
    "prev",
    "clear", /* deprecated */
    "removeItem", /* deprecated */
};
827
COUNTNAMES(LibvlcPlaylistNPObject,methodCount,methodNames);
Rafaël Carré's avatar
Rafaël Carré committed
828 829 830 831 832 833

enum LibvlcPlaylistNPObjectMethodIds
{
    ID_playlist_add,
    ID_playlist_play,
    ID_playlist_playItem,
834
    ID_playlist_pause,
Rafaël Carré's avatar
Rafaël Carré committed
835 836 837 838 839 840 841 842
    ID_playlist_togglepause,
    ID_playlist_stop,
    ID_playlist_next,
    ID_playlist_prev,
    ID_playlist_clear,
    ID_playlist_removeitem
};

843 844
RuntimeNPObject::InvokeResult
LibvlcPlaylistNPObject::invoke(int index, const NPVariant *args,
845
                               uint32_t argCount, npapi::OutVariant& result)
Rafaël Carré's avatar
Rafaël Carré committed
846 847
{
    /* is plugin still running */
848
    if( isPluginRunning() )
Rafaël Carré's avatar
Rafaël Carré committed
849
    {
850
        VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
Rafaël Carré's avatar
Rafaël Carré committed
851 852 853

        switch( index )
        {
854
            // XXX FIXME this needs squashing into something much smaller
Rafaël Carré's avatar
Rafaël Carré committed
855 856 857 858
            case ID_playlist_add:
            {
                if( (argCount < 1) || (argCount > 3) )
                    return INVOKERESULT_NO_SUCH_METHOD;
859
                if( !npapi::is_string( args[0] ) )
JP Dinger's avatar
JP Dinger committed
860
                    return INVOKERESULT_NO_SUCH_METHOD;
Rafaël Carré's avatar
Rafaël Carré committed
861 862

                // grab URL
863 864 865
                if( NPVARIANT_IS_NULL(args[0]) )
                    return INVOKERESULT_NO_SUCH_METHOD;

866 867 868 869
                // Don't assign url pointer to s, they don't have the same
                // deleter function.
                auto s = npapi::to_string( args[0] );
                auto url = CStr( p_plugin->getAbsoluteURL( s.get() ), free );
Rafaël Carré's avatar
Rafaël Carré committed
870

871
                //FIXME: This is never used
872
                auto name = npapi::NPStringPtr( nullptr, NPN_MemFree );
Rafaël Carré's avatar
Rafaël Carré committed
873 874 875 876 877 878 879 880 881
                // grab name if available
                if( argCount > 1 )
                {
                    if( NPVARIANT_IS_NULL(args[1]) )
                    {
                        // do nothing
                    }
                    else if( NPVARIANT_IS_STRING(args[1]) )
                    {
882
                        name = npapi::to_string( args[1] );
Rafaël Carré's avatar
Rafaël Carré committed
883 884 885 886 887 888 889 890 891 892 893 894 895
                    }
                    else
                    {
                        return INVOKERESULT_INVALID_VALUE;
                    }
                }

                int i_options = 0;
                char** ppsz_options = NULL;

                // grab options if available
                if( argCount > 2 )
                {
896
                    const npapi::Variant v( args[2] );
897
                    if( v.is<std::nullptr_t>() )
Rafaël Carré's avatar
Rafaël Carré committed
898 899 900
                    {
                        // do nothing
                    }
901
                    else if( v.is<NPString>() )
Rafaël Carré's avatar
Rafaël Carré committed
902
                    {
903
                        parseOptions((NPString)v, &i_options, &ppsz_options);
Rafaël Carré's avatar
Rafaël Carré committed
904 905

                    }
906
                    else if( v.is<NPObject>() )
Rafaël Carré's avatar
Rafaël Carré committed
907
                    {
908
                        parseOptions((NPObject*)v, &i_options, &ppsz_options);
Rafaël Carré's avatar
Rafaël Carré committed
909 910 911 912 913 914 915
                    }
                    else
                    {
                        return INVOKERESULT_INVALID_VALUE;
                    }
                }

916
                int item = p_plugin->player().add_item( url ? url.get() : s.get(), i_options, const_cast<const char **>(ppsz_options));
917 918 919
                if( item == -1 )
                    RETURN_ON_ERROR;

Rafaël Carré's avatar
Rafaël Carré committed
920 921
                for( int i=0; i< i_options; ++i )
                {
Jean-Paul Saman's avatar
Jean-Paul Saman committed
922
                    free(ppsz_options[i]);
Rafaël Carré's avatar
Rafaël Carré committed
923
                }
924
                free(ppsz_options);
Rafaël Carré's avatar
Rafaël Carré committed
925

926
                result = item;
927
                return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
928 929 930 931
            }
            case ID_playlist_play:
                if( argCount == 0 )
                {
932
                    p_plugin->player().play();
933
                    return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
934 935 936
                }
                return INVOKERESULT_NO_SUCH_METHOD;
            case ID_playlist_playItem:
937 938 939
            {
                if ( argCount < 1 )
                    return INVOKERESULT_INVALID_ARGS;
940
                const npapi::Variant v( args[0] );
941
                if ( v.is<int>() )
Rafaël Carré's avatar
Rafaël Carré committed
942
                {
943
                    p_plugin->player().mlp().playItemAtIndex( v );
944
                    return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
945 946
                }
                return INVOKERESULT_NO_SUCH_METHOD;
947
            }
948
            case ID_playlist_pause:
Rafaël Carré's avatar
Rafaël Carré committed
949 950
                if( argCount == 0 )
                {
951
                    p_plugin->player().get_mp().setPause( true );
952
                    return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
953 954
                }
                return INVOKERESULT_NO_SUCH_METHOD;
955 956 957
            case ID_playlist_togglepause:
                if( argCount == 0 )
                {
958
                    p_plugin->player().mlp().pause();
959 960 961
                    return INVOKERESULT_NO_ERROR;
                }
                return INVOKERESULT_NO_SUCH_METHOD;
Rafaël Carré's avatar
Rafaël Carré committed
962 963 964
            case ID_playlist_stop:
                if( argCount == 0 )
                {
965
                    p_plugin->player().mlp().stop();
966
                    return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
967 968 969 970 971
                }
                return INVOKERESULT_NO_SUCH_METHOD;
            case ID_playlist_next:
                if( argCount == 0 )
                {
972
                    p_plugin->player().mlp().next();
973
                    return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
974 975 976 977 978
                }
                return INVOKERESULT_NO_SUCH_METHOD;
            case ID_playlist_prev:
                if( argCount == 0 )
                {
979
                    p_plugin->player().mlp().previous();
980
                    return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
981 982 983 984 985
                }
                return INVOKERESULT_NO_SUCH_METHOD;
            case ID_playlist_clear: /* deprecated */
                if( argCount == 0 )
                {
986
                    p_plugin->player().clear_items();
987
                    return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
988 989 990
                }
                return INVOKERESULT_NO_SUCH_METHOD;
            case ID_playlist_removeitem: /* deprecated */
991 992 993
            {
                if ( argCount < 1 )
                    return INVOKERESULT_INVALID_ARGS;
994
                const npapi::Variant v( args[0] );
995
                if ( v.is<int>() )
Rafaël Carré's avatar
Rafaël Carré committed
996
                {
997
                    if( !p_plugin->player().delete_item( v ) )
998
                        return INVOKERESULT_GENERIC_ERROR;
999
                    return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
1000 1001
                }
                return INVOKERESULT_NO_SUCH_METHOD;
1002
            }
Rafaël Carré's avatar
Rafaël Carré committed
1003 1004 1005 1006 1007 1008 1009
            default:
                ;
        }
    }
    return INVOKERESULT_GENERIC_ERROR;
}

1010 1011 1012 1013 1014 1015
// XXX FIXME The new playlist_add creates a media instance and feeds it
// XXX FIXME these options one at a time, so this hunk of code does lots
// XXX FIXME of unnecessairy work. Break out something that can do one
// XXX FIXME option at a time and doesn't need to realloc().
// XXX FIXME Same for the other version of parseOptions.

1016
void LibvlcPlaylistNPObject::parseOptions(const NPString& nps,
1017
                                         int *i_options, char*** ppsz_options)
Rafaël Carré's avatar
Rafaël Carré committed
1018
{
1019
    if( nps.UTF8Length )
Rafaël Carré's avatar
Rafaël Carré committed
1020
    {
1021 1022
        auto s = CStr( strdup( nps.UTF8Characters ), free );
        char *val = s.get();
Rafaël Carré's avatar
Rafaël Carré committed
1023 1024 1025 1026 1027 1028 1029 1030
        if( val )
        {
            long capacity = 16;
            char **options = (char **)malloc(capacity*sizeof(char *));
            if( options )
            {
                int nOptions = 0;

1031
                char *end = val + nps.UTF8Length;
Rafaël Carré's avatar
Rafaël Carré committed
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
                while( val < end )
                {
                    // skip leading blanks
                    while( (val < end)
                        && ((*val == ' ' ) || (*val == '\t')) )
                        ++val;

                    char *start = val;
                    // skip till we get a blank character
                    while( (val < end)
                        && (*val != ' ' )
                        && (*val != '\t') )
                    {
                        char c = *(val++);
                        if( ('\'' == c) || ('"' == c) )
                        {
                            // skip till end of string
                            while( (val < end) && (*(val++) != c ) );
                        }
                    }

                    if( val > start )
                    {
                        if( nOptions == capacity )
                        {
                            capacity += 16;
1058
                            char **moreOptions = (char **)realloc(options, capacity*sizeof(char*));
Rafaël Carré's avatar
Rafaël Carré committed
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
                            if( ! moreOptions )
                            {
                                /* failed to allocate more memory */
                                /* return what we got so far */
                                *i_options = nOptions;
                                *ppsz_options = options;
                                return;
                            }
                            options = moreOptions;
                        }
                        *(val++) = '\0';
                        options[nOptions++] = strdup(start);
                    }
                    else
                        // must be end of string
                        break;
                }
                *i_options = nOptions;
                *ppsz_options = options;
            }
        }
    }
}

1083
// XXX FIXME See comment at the other parseOptions variant.
1084 1085
void LibvlcPlaylistNPObject::parseOptions(NPObject *obj, int *i_options,
                                          char*** ppsz_options)
Rafaël Carré's avatar
Rafaël Carré committed
1086 1087 1088
{
    /* WARNING: Safari does not implement NPN_HasProperty/NPN_HasMethod */

1089
    npapi::Variant value;
Rafaël Carré's avatar
Rafaël Carré committed
1090 1091 1092

    /* we are expecting to have a Javascript Array object */
    NPIdentifier propId = NPN_GetStringIdentifier("length");
1093
    if( NPN_GetProperty(_instance, obj, propId, value) )
Rafaël Carré's avatar
Rafaël Carré committed
1094
    {
1095
        int count = value;
Rafaël Carré's avatar
Rafaël Carré committed
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106

        if( count )
        {
            long capacity = 16;
            char **options = (char **)malloc(capacity*sizeof(char *));
            if( options )
            {
                int nOptions = 0;

                while( nOptions < count )
                {
1107
                    npapi::Variant value;
Rafaël Carré's avatar
Rafaël Carré committed
1108
                    propId = NPN_GetIntIdentifier(nOptions);
1109
                    if( ! NPN_GetProperty(_instance, obj, propId, value) )
Rafaël Carré's avatar
Rafaël Carré committed
1110 1111 1112
                        /* return what we got so far */
                        break;

1113
                    if( !value.is<NPString>() )
Rafaël Carré's avatar
Rafaël Carré committed
1114 1115 1116 1117 1118 1119 1120 1121
                    {
                        /* return what we got so far */
                        break;
                    }

                    if( nOptions == capacity )
                    {
                        capacity += 16;
1122
                        char **moreOptions = (char **)realloc(options, capacity*sizeof(char*));
Rafaël Carré's avatar
Rafaël Carré committed
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
                        if( ! moreOptions )
                        {
                            /* failed to allocate more memory */
                            /* return what we got so far */
                            *i_options = nOptions;
                            *ppsz_options = options;
                            break;
                        }
                        options = moreOptions;
                    }

1134
                    options[nOptions++] = strdup(value);
Rafaël Carré's avatar
Rafaël Carré committed
1135 1136 1137 1138 1139 1140 1141 1142
                }
                *i_options = nOptions;
                *ppsz_options = options;
            }
        }
    }
}

1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
/*
** implementation of libvlc subtitle object
*/

const NPUTF8 * const LibvlcSubtitleNPObject::propertyNames[] =
{
    "track",
    "count",
};

enum LibvlcSubtitleNPObjectPropertyIds
{
    ID_subtitle_track,
    ID_subtitle_count,
};
COUNTNAMES(LibvlcSubtitleNPObject,propertyCount,propertyNames);

RuntimeNPObject::InvokeResult
1161
LibvlcSubtitleNPObject::getProperty(int index, npapi::OutVariant& result)
1162 1163 1164 1165
{
    /* is plugin still running */
    if( isPluginRunning() )
    {
1166
        VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
1167 1168
        auto& mp = p_plugin->getMD();
        if( !mp )
1169
            RETURN_ON_ERROR;
1170 1171 1172 1173 1174

        switch( index )
        {
            case ID_subtitle_track:
            {
1175
                /* get the current internal subtitles track ID */
1176
                result = p_plugin->player().currentSubtitleTrack();
1177 1178 1179 1180 1181
                return INVOKERESULT_NO_ERROR;
            }
            case ID_subtitle_count:
            {
                /* get the number of subtitles available */
1182
                result = mp.spuCount();
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
                /* return it */
                return INVOKERESULT_NO_ERROR;
            }
        }
    }
    return INVOKERESULT_GENERIC_ERROR;
}

RuntimeNPObject::InvokeResult
LibvlcSubtitleNPObject::setProperty(int index, const NPVariant &value)
{
    /* is plugin still running */
    if( isPluginRunning() )
    {
1197
        VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
1198 1199
        auto& mp = p_plugin->getMD();
        if( !mp )
1200
            RETURN_ON_ERROR;
1201 1202 1203 1204 1205

        switch( index )
        {
            case ID_subtitle_track:
            {
1206
                const npapi::Variant v( value );
1207
                if( v.is<int>() )
1208
                {
1209 1210 1211 1212
                    auto tracks = mp.spuDescription();
                    if ( v >= tracks.size() )
                        return INVOKERESULT_INVALID_ARGS;
                    if ( mp.setSpu( tracks[ v ].id() ) )
1213
                        return INVOKERESULT_NO_ERROR;
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
                }
                return INVOKERESULT_INVALID_VALUE;
            }
        }
    }
    return INVOKERESULT_GENERIC_ERROR;
}

const NPUTF8 * const LibvlcSubtitleNPObject::methodNames[] =
{
    "description"
};
COUNTNAMES(LibvlcSubtitleNPObject,methodCount,methodNames);

enum LibvlcSubtitleNPObjectMethodIds
{
    ID_subtitle_description
};

RuntimeNPObject::InvokeResult
LibvlcSubtitleNPObject::invoke(int index, const NPVariant *args,
1235
                            uint32_t argCount, npapi::OutVariant& result)
1236 1237 1238 1239
{
    /* is plugin still running */
    if( isPluginRunning() )
    {
1240
        VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
1241 1242
        auto& mp = p_plugin->getMD();
        if( !mp )
1243
            RETURN_ON_ERROR;
1244 1245 1246 1247 1248

        switch( index )
        {
            case ID_subtitle_description:
            {
1249 1250
                if ( argCount < 1 )
                    return INVOKERESULT_INVALID_ARGS;
1251
                const npapi::Variant v( args[0] );
1252
                if ( v.is<int>() )
1253
                {
1254
                    auto tracks = mp.spuDescription();
1255
                    if ( v >= tracks.size() )
1256
                        return INVOKERESULT_INVALID_VALUE;
1257
                    /* display the name of the track chosen */
1258 1259
                    result = tracks[v].name();
                    return INVOKERESULT_NO_ERROR;
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
                }
                return INVOKERESULT_NO_SUCH_METHOD;
            }
            default:
                return INVOKERESULT_NO_SUCH_METHOD;
        }
    }
    return INVOKERESULT_GENERIC_ERROR;
}

Rafaël Carré's avatar
Rafaël Carré committed
1270 1271 1272 1273
/*
** implementation of libvlc video object
*/

1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
LibvlcVideoNPObject::~LibvlcVideoNPObject()
{
    if( isValid() )
    {
        if( marqueeObj ) NPN_ReleaseObject(marqueeObj);
        if( logoObj    ) NPN_ReleaseObject(logoObj);
        if( deintObj   ) NPN_ReleaseObject(deintObj);
    }
}

1284
const NPUTF8 * const LibvlcVideoNPObject::propertyNames[] =
Rafaël Carré's avatar
Rafaël Carré committed
1285 1286 1287 1288 1289 1290 1291
{
    "fullscreen",
    "height",
    "width",
    "aspectRatio",
    "subtitle",
    "crop",
1292
    "teletext",
1293
    "marquee",
1294 1295
    "logo",
    "deinterlace",
Rafaël Carré's avatar
Rafaël Carré committed
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
};

enum LibvlcVideoNPObjectPropertyIds
{
    ID_video_fullscreen,
    ID_video_height,
    ID_video_width,
    ID_video_aspectratio,
    ID_video_subtitle,
    ID_video_crop,
1306
    ID_video_teletext,
1307
    ID_video_marquee,
1308 1309
    ID_video_logo,
    ID_video_deinterlace,
Rafaël Carré's avatar
Rafaël Carré committed
1310
};
1311
COUNTNAMES(LibvlcVideoNPObject,propertyCount,propertyNames);
Rafaël Carré's avatar
Rafaël Carré committed
1312

1313
RuntimeNPObject::InvokeResult
1314
LibvlcVideoNPObject::getProperty(int index, npapi::OutVariant& result)
Rafaël Carré's avatar
Rafaël Carré committed
1315 1316
{
    /* is plugin still running */
1317
    if( isPluginRunning() )
Rafaël Carré's avatar
Rafaël Carré committed
1318
    {
1319
        VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
1320 1321
        auto& mp = p_plugin->getMD();
        if( !mp )
1322
            RETURN_ON_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
1323 1324 1325 1326 1327

        switch( index )
        {
            case ID_video_fullscreen:
            {
1328
                result = p_plugin->get_fullscreen();
Rafaël Carré's avatar
Rafaël Carré committed
1329 1330 1331 1332
                return INVOKERESULT_NO_ERROR;
            }
            case ID_video_height:
            {
1333 1334
                unsigned width, height;
                mp.size( 0, &width, &height );
1335
                result = height;
Rafaël Carré's avatar
Rafaël Carré committed
1336 1337 1338 1339
                return INVOKERESULT_NO_ERROR;
            }
            case ID_video_width:
            {
1340 1341
                unsigned width, height;
                mp.size( 0, &width, &height );
1342
                result = width;
Rafaël Carré's avatar
Rafaël Carré committed
1343 1344 1345 1346
                return INVOKERESULT_NO_ERROR;
            }
            case ID_video_aspectratio:
            {
1347 1348
                auto ar = mp.aspectRatio();
                if( ar.empty() )
Rafaël Carré's avatar
Rafaël Carré committed
1349 1350
                    return INVOKERESULT_GENERIC_ERROR;

1351
                result = ar;
Rafaël Carré's avatar
Rafaël Carré committed
1352 1353 1354 1355
                return INVOKERESULT_NO_ERROR;
            }
            case ID_video_subtitle:
            {
1356
                result = mp.spu();
Rafaël Carré's avatar
Rafaël Carré committed
1357 1358 1359 1360
                return INVOKERESULT_NO_ERROR;
            }
            case ID_video_crop:
            {
1361 1362
                auto geo = mp.cropGeometry();
                if( geo.empty() )
Rafaël Carré's avatar
Rafaël Carré committed
1363 1364
                    return INVOKERESULT_GENERIC_ERROR;

1365
                result = geo;
Rafaël Carré's avatar
Rafaël Carré committed
1366 1367 1368 1369
                return INVOKERESULT_NO_ERROR;
            }
            case ID_video_teletext:
            {
1370
                int i_page = mp.teletext();
1371 1372
                if( i_page < 0 )
                    return INVOKERESULT_GENERIC_ERROR;
1373
                result = i_page;
Rafaël Carré's avatar
Rafaël Carré committed
1374 1375
                return INVOKERESULT_NO_ERROR;
            }
1376 1377
            case ID_video_marquee:
            {
1378
                InstantObj<LibvlcMarqueeNPObject>( marqueeObj );
1379
                result = marqueeObj;
1380 1381
                return INVOKERESULT_NO_ERROR;
            }
1382 1383 1384
            case ID_video_logo:
            {