npolibvlc.cpp 54.5 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 98
RuntimeNPObject::InvokeResult
LibvlcRootNPObject::getProperty(int index, NPVariant &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 );
Rafaël Carré's avatar
Rafaël Carré committed
107 108 109
                OBJECT_TO_NPVARIANT(NPN_RetainObject(audioObj), result);
                return INVOKERESULT_NO_ERROR;
            case ID_root_input:
110
                InstantObj<LibvlcInputNPObject>( inputObj );
Rafaël Carré's avatar
Rafaël Carré committed
111 112 113
                OBJECT_TO_NPVARIANT(NPN_RetainObject(inputObj), result);
                return INVOKERESULT_NO_ERROR;
            case ID_root_playlist:
114
                InstantObj<LibvlcPlaylistNPObject>( playlistObj );
Rafaël Carré's avatar
Rafaël Carré committed
115 116
                OBJECT_TO_NPVARIANT(NPN_RetainObject(playlistObj), result);
                return INVOKERESULT_NO_ERROR;
117
            case ID_root_subtitle:
118
                InstantObj<LibvlcSubtitleNPObject>( subtitleObj );
119 120
                OBJECT_TO_NPVARIANT(NPN_RetainObject(subtitleObj), result);
                return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
121
            case ID_root_video:
122
                InstantObj<LibvlcVideoNPObject>( videoObj );
Rafaël Carré's avatar
Rafaël Carré committed
123 124 125
                OBJECT_TO_NPVARIANT(NPN_RetainObject(videoObj), result);
                return INVOKERESULT_NO_ERROR;
            case ID_root_VersionInfo:
126
                return invokeResultString(libvlc_get_version(),result);
127 128 129 130 131 132
            case ID_root_MediaDescription:
            {
                InstantObj<LibvlcMediaDescriptionNPObject>( mediaDescriptionObj );
                OBJECT_TO_NPVARIANT(NPN_RetainObject(mediaDescriptionObj), result);
                return INVOKERESULT_NO_ERROR;
            }
Rafaël Carré's avatar
Rafaël Carré committed
133 134 135 136 137 138 139 140 141 142
            default:
                ;
        }
    }
    return INVOKERESULT_GENERIC_ERROR;
}

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

enum LibvlcRootNPObjectMethodIds
{
    ID_root_versionInfo,
151 152
    ID_root_addeventlistener,
    ID_root_removeeventlistener,
Rafaël Carré's avatar
Rafaël Carré committed
153 154
};

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

    switch( index )
Rafaël Carré's avatar
Rafaël Carré committed
163
    {
164 165 166 167 168 169 170
    case ID_root_versionInfo:
        if( 0 != argCount )
            return INVOKERESULT_NO_SUCH_METHOD;
        return invokeResultString(libvlc_get_version(),result);

    case ID_root_addeventlistener:
    case ID_root_removeeventlistener:
171 172 173 174 175 176 177 178
        if ( argCount < 2 )
            return INVOKERESULT_INVALID_ARGS;

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

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

181
        if( !VlcPluginBase::canUseEventListener() )
Rafaël Carré's avatar
Rafaël Carré committed
182
        {
183 184
            NPN_SetException(this, ERROR_API_VERSION);
            return INVOKERESULT_GENERIC_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
185
        }
186

187
        VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
188

Jean-Paul Saman's avatar
Jean-Paul Saman committed
189 190
        if( ID_root_addeventlistener == index )
        {
191
            p_plugin->subscribe( npapi::to_tmp_string( args[0] ), listener );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
192
        }
193
        else
Jean-Paul Saman's avatar
Jean-Paul Saman committed
194
        {
195
            p_plugin->unsubscribe( npapi::to_tmp_string( args[0] ), listener );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
196
        }
197 198
        VOID_TO_NPVARIANT(result);

199
        return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
200
    }
201
    return INVOKERESULT_NO_SUCH_METHOD;
Rafaël Carré's avatar
Rafaël Carré committed
202 203 204 205 206 207
}

/*
** implementation of libvlc audio object
*/

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

enum LibvlcAudioNPObjectPropertyIds
{
    ID_audio_mute,
    ID_audio_volume,
    ID_audio_track,
223
    ID_audio_count,
Rafaël Carré's avatar
Rafaël Carré committed
224 225 226
    ID_audio_channel,
};

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

235 236
        auto& mp = p_plugin->getMD();
        if( !mp )
237
            RETURN_ON_ERROR;
Rémi Duraffort's avatar
Rémi Duraffort committed
238

Rafaël Carré's avatar
Rafaël Carré committed
239 240 241 242
        switch( index )
        {
            case ID_audio_mute:
            {
243
                bool muted = mp.mute();
Rafaël Carré's avatar
Rafaël Carré committed
244 245 246 247 248
                BOOLEAN_TO_NPVARIANT(muted, result);
                return INVOKERESULT_NO_ERROR;
            }
            case ID_audio_volume:
            {
249
                int volume = mp.volume();
Rafaël Carré's avatar
Rafaël Carré committed
250 251 252 253 254
                INT32_TO_NPVARIANT(volume, result);
                return INVOKERESULT_NO_ERROR;
            }
            case ID_audio_track:
            {
255 256
                int track = p_plugin->player().currentAudioTrack();
                INT32_TO_NPVARIANT(track, result);
Rafaël Carré's avatar
Rafaël Carré committed
257 258
                return INVOKERESULT_NO_ERROR;
            }
259 260 261
            case ID_audio_count:
            {
                // get the number of audio track available
262
                int i_track = mp.audioTrackCount();
263 264 265 266
                // return it
                INT32_TO_NPVARIANT(i_track, result);
                return INVOKERESULT_NO_ERROR;
            }
Rafaël Carré's avatar
Rafaël Carré committed
267 268
            case ID_audio_channel:
            {
269
                int channel = mp.channel();
Rafaël Carré's avatar
Rafaël Carré committed
270 271 272 273 274 275 276 277 278 279
                INT32_TO_NPVARIANT(channel, result);
                return INVOKERESULT_NO_ERROR;
            }
            default:
                ;
        }
    }
    return INVOKERESULT_GENERIC_ERROR;
}

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

288 289
        auto& mp = p_plugin->getMD();
        if( !mp )
290
            RETURN_ON_ERROR;
Rémi Duraffort's avatar
Rémi Duraffort committed
291

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

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

enum LibvlcAudioNPObjectMethodIds
{
    ID_audio_togglemute,
343
    ID_audio_description,
Rafaël Carré's avatar
Rafaël Carré committed
344 345
};

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

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

/*
** implementation of libvlc input object
*/

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

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

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

        switch( index )
        {
            case ID_input_length:
            {
441
                double val = mp.length();
Rafaël Carré's avatar
Rafaël Carré committed
442 443 444 445 446
                DOUBLE_TO_NPVARIANT(val, result);
                return INVOKERESULT_NO_ERROR;
            }
            case ID_input_position:
            {
447
                double val = mp.position();
Rafaël Carré's avatar
Rafaël Carré committed
448 449 450 451 452
                DOUBLE_TO_NPVARIANT(val, result);
                return INVOKERESULT_NO_ERROR;
            }
            case ID_input_time:
            {
453
                double val = (double)mp.time();
Rafaël Carré's avatar
Rafaël Carré committed
454 455 456 457 458
                DOUBLE_TO_NPVARIANT(val, result);
                return INVOKERESULT_NO_ERROR;
            }
            case ID_input_state:
            {
459
                int val = mp.state();
Rafaël Carré's avatar
Rafaël Carré committed
460 461 462 463 464
                INT32_TO_NPVARIANT(val, result);
                return INVOKERESULT_NO_ERROR;
            }
            case ID_input_rate:
            {
465
                float val = mp.rate();
Rafaël Carré's avatar
Rafaël Carré committed
466 467 468 469 470
                DOUBLE_TO_NPVARIANT(val, result);
                return INVOKERESULT_NO_ERROR;
            }
            case ID_input_fps:
            {
471
                double val = mp.fps();
Rafaël Carré's avatar
Rafaël Carré committed
472 473 474 475 476
                DOUBLE_TO_NPVARIANT(val, result);
                return INVOKERESULT_NO_ERROR;
            }
            case ID_input_hasvout:
            {
477
                bool val = p_plugin->player().get_mp().hasVout() != 0;
Rafaël Carré's avatar
Rafaël Carré committed
478 479 480 481 482 483 484 485 486 487
                BOOLEAN_TO_NPVARIANT(val, result);
                return INVOKERESULT_NO_ERROR;
            }
            default:
                ;
        }
    }
    return INVOKERESULT_GENERIC_ERROR;
}

488 489
RuntimeNPObject::InvokeResult
LibvlcInputNPObject::setProperty(int index, const NPVariant &value)
Rafaël Carré's avatar
Rafaël Carré committed
490 491
{
    /* is plugin still running */
492
    if( isPluginRunning() )
Rafaël Carré's avatar
Rafaël Carré committed
493
    {
494
        VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
495 496
        auto& mp = p_plugin->getMD();
        if( !mp )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
497
            RETURN_ON_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
498

499
        auto v = npapi::Variant( value );
Rafaël Carré's avatar
Rafaël Carré committed
500 501 502 503
        switch( index )
        {
            case ID_input_position:
            {
504
                if( !v.is<double>() )
Rafaël Carré's avatar
Rafaël Carré committed
505 506 507 508
                {
                    return INVOKERESULT_INVALID_VALUE;
                }

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

519
                mp.setTime( v );
Rafaël Carré's avatar
Rafaël Carré committed
520 521 522 523
                return INVOKERESULT_NO_ERROR;
            }
            case ID_input_rate:
            {
524
                if( !v.is<double>() )
Rafaël Carré's avatar
Rafaël Carré committed
525 526 527 528
                {
                    return INVOKERESULT_INVALID_VALUE;
                }

529
                mp.setRate( v );
Rafaël Carré's avatar
Rafaël Carré committed
530 531 532 533 534 535 536 537 538 539 540 541
                return INVOKERESULT_NO_ERROR;
            }
            default:
                ;
        }
    }
    return INVOKERESULT_GENERIC_ERROR;
}

const NPUTF8 * const LibvlcInputNPObject::methodNames[] =
{
    /* no methods */
542
    "none",
Rafaël Carré's avatar
Rafaël Carré committed
543
};
544
COUNTNAMES(LibvlcInputNPObject,methodCount,methodNames);
Rafaël Carré's avatar
Rafaël Carré committed
545

546 547 548 549 550 551
enum LibvlcInputNPObjectMethodIds
{
    ID_none,
};

RuntimeNPObject::InvokeResult
552 553
LibvlcInputNPObject::invoke(int index, const NPVariant *,
                            uint32_t, NPVariant &)
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
{
    /* is plugin still running */
    if( isPluginRunning() )
    {
        switch( index )
        {
            case ID_none:
                return INVOKERESULT_NO_SUCH_METHOD;
            default:
                ;
        }
    }
    return INVOKERESULT_GENERIC_ERROR;
}

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 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
/*
** 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
LibvlcMediaDescriptionNPObject::getProperty(int index, NPVariant &result)
{
    /* is plugin still running */
    if( isPluginRunning() )
    {
        VlcPlugin* p_plugin = getPrivate<VlcPlugin>();
622 623
        auto& mp = p_plugin->getMD();
        if( !mp )
624
            RETURN_ON_ERROR;
625 626
        auto media = mp.media();
        if( !media )
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
            RETURN_ON_ERROR;
        const char *info;
        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:
648 649 650 651
            {
                auto m = media->meta( (libvlc_meta_t)index );
                return invokeResultString(m.c_str(), result);
            }
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
            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
671 672 673 674
/*
** implementation of libvlc playlist items object
*/

675
const NPUTF8 * const LibvlcPlaylistItemsNPObject::propertyNames[] =
Rafaël Carré's avatar
Rafaël Carré committed
676 677 678
{
    "count",
};
679
COUNTNAMES(LibvlcPlaylistItemsNPObject,propertyCount,propertyNames);
Rafaël Carré's avatar
Rafaël Carré committed
680 681 682 683 684 685

enum LibvlcPlaylistItemsNPObjectPropertyIds
{
    ID_playlistitems_count,
};

686 687
RuntimeNPObject::InvokeResult
LibvlcPlaylistItemsNPObject::getProperty(int index, NPVariant &result)
Rafaël Carré's avatar
Rafaël Carré committed
688 689
{
    /* is plugin still running */
690
    if( isPluginRunning() )
Rafaël Carré's avatar
Rafaël Carré committed
691
    {
692
        VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
Rafaël Carré's avatar
Rafaël Carré committed
693 694 695 696 697

        switch( index )
        {
            case ID_playlistitems_count:
            {
698
                int val = p_plugin->player().items_count();
Rafaël Carré's avatar
Rafaël Carré committed
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
                INT32_TO_NPVARIANT(val, result);
                return INVOKERESULT_NO_ERROR;
            }
            default:
                ;
        }
    }
    return INVOKERESULT_GENERIC_ERROR;
}

const NPUTF8 * const LibvlcPlaylistItemsNPObject::methodNames[] =
{
    "clear",
    "remove",
};
714
COUNTNAMES(LibvlcPlaylistItemsNPObject,methodCount,methodNames);
Rafaël Carré's avatar
Rafaël Carré committed
715 716 717 718 719 720 721

enum LibvlcPlaylistItemsNPObjectMethodIds
{
    ID_playlistitems_clear,
    ID_playlistitems_remove,
};

722 723 724
RuntimeNPObject::InvokeResult
LibvlcPlaylistItemsNPObject::invoke(int index, const NPVariant *args,
                                    uint32_t argCount, NPVariant &result)
Rafaël Carré's avatar
Rafaël Carré committed
725 726
{
    /* is plugin still running */
727
    if( isPluginRunning() )
Rafaël Carré's avatar
Rafaël Carré committed
728
    {
729
        VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
Rafaël Carré's avatar
Rafaël Carré committed
730 731 732 733 734 735

        switch( index )
        {
            case ID_playlistitems_clear:
                if( argCount == 0 )
                {
736
                    p_plugin->player().clear_items();
737 738
                    VOID_TO_NPVARIANT(result);
                    return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
739 740 741
                }
                return INVOKERESULT_NO_SUCH_METHOD;
            case ID_playlistitems_remove:
742 743 744 745 746
            {
                if ( argCount < 1 )
                    INVOKERESULT_INVALID_ARGS;
                auto v = npapi::Variant( args[0] );
                if( v.is<int>() )
Rafaël Carré's avatar
Rafaël Carré committed
747
                {
748
                    if( !p_plugin->player().delete_item( v ) )
749
                        return INVOKERESULT_GENERIC_ERROR;
750 751
                    VOID_TO_NPVARIANT(result);
                    return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
752 753
                }
                return INVOKERESULT_NO_SUCH_METHOD;
754
            }
Rafaël Carré's avatar
Rafaël Carré committed
755 756 757 758 759 760 761 762 763 764 765 766 767
            default:
                ;
        }
    }
    return INVOKERESULT_GENERIC_ERROR;
}

/*
** implementation of libvlc playlist object
*/

LibvlcPlaylistNPObject::~LibvlcPlaylistNPObject()
{
768 769 770
    // Why the isValid()?
    if( isValid() && playlistItemsObj )
        NPN_ReleaseObject(playlistItemsObj);
Rafaël Carré's avatar
Rafaël Carré committed
771 772
};

773
const NPUTF8 * const LibvlcPlaylistNPObject::propertyNames[] =
Rafaël Carré's avatar
Rafaël Carré committed
774 775 776
{
    "itemCount", /* deprecated */
    "isPlaying",
777
    "currentItem",
Rafaël Carré's avatar
Rafaël Carré committed
778 779
    "items",
};
780
COUNTNAMES(LibvlcPlaylistNPObject,propertyCount,propertyNames);
Rafaël Carré's avatar
Rafaël Carré committed
781 782 783 784 785

enum LibvlcPlaylistNPObjectPropertyIds
{
    ID_playlist_itemcount,
    ID_playlist_isplaying,
786
    ID_playlist_currentitem,
Rafaël Carré's avatar
Rafaël Carré committed
787 788 789
    ID_playlist_items,
};

790 791
RuntimeNPObject::InvokeResult
LibvlcPlaylistNPObject::getProperty(int index, NPVariant &result)
Rafaël Carré's avatar
Rafaël Carré committed
792 793
{
    /* is plugin still running */
794
    if( isPluginRunning() )
Rafaël Carré's avatar
Rafaël Carré committed
795
    {
796
        VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
Rafaël Carré's avatar
Rafaël Carré committed
797 798 799 800 801

        switch( index )
        {
            case ID_playlist_itemcount: /* deprecated */
            {
802
                int val = p_plugin->player().items_count();
Rafaël Carré's avatar
Rafaël Carré committed
803 804 805 806 807
                INT32_TO_NPVARIANT(val, result);
                return INVOKERESULT_NO_ERROR;
            }
            case ID_playlist_isplaying:
            {
808
                int val = p_plugin->player().mlp().isPlaying();
Rafaël Carré's avatar
Rafaël Carré committed
809 810 811
                BOOLEAN_TO_NPVARIANT(val, result);
                return INVOKERESULT_NO_ERROR;
            }
812 813
            case ID_playlist_currentitem:
            {
814
                int val = p_plugin->player().current_item();
815 816 817
                INT32_TO_NPVARIANT(val, result);
                return INVOKERESULT_NO_ERROR;
            }
Rafaël Carré's avatar
Rafaël Carré committed
818 819
            case ID_playlist_items:
            {
820
                InstantObj<LibvlcPlaylistItemsNPObject>( playlistItemsObj );
Rafaël Carré's avatar
Rafaël Carré committed
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
                OBJECT_TO_NPVARIANT(NPN_RetainObject(playlistItemsObj), result);
                return INVOKERESULT_NO_ERROR;
            }
            default:
                ;
        }
    }
    return INVOKERESULT_GENERIC_ERROR;
}

const NPUTF8 * const LibvlcPlaylistNPObject::methodNames[] =
{
    "add",
    "play",
    "playItem",
836
    "pause",
Rafaël Carré's avatar
Rafaël Carré committed
837 838 839 840 841 842 843
    "togglePause",
    "stop",
    "next",
    "prev",
    "clear", /* deprecated */
    "removeItem", /* deprecated */
};
844
COUNTNAMES(LibvlcPlaylistNPObject,methodCount,methodNames);
Rafaël Carré's avatar
Rafaël Carré committed
845 846 847 848 849 850

enum LibvlcPlaylistNPObjectMethodIds
{
    ID_playlist_add,
    ID_playlist_play,
    ID_playlist_playItem,
851
    ID_playlist_pause,
Rafaël Carré's avatar
Rafaël Carré committed
852 853 854 855 856 857 858 859
    ID_playlist_togglepause,
    ID_playlist_stop,
    ID_playlist_next,
    ID_playlist_prev,
    ID_playlist_clear,
    ID_playlist_removeitem
};

860 861 862
RuntimeNPObject::InvokeResult
LibvlcPlaylistNPObject::invoke(int index, const NPVariant *args,
                               uint32_t argCount, NPVariant &result)
Rafaël Carré's avatar
Rafaël Carré committed
863 864
{
    /* is plugin still running */
865
    if( isPluginRunning() )
Rafaël Carré's avatar
Rafaël Carré committed
866
    {
867
        VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
Rafaël Carré's avatar
Rafaël Carré committed
868 869 870

        switch( index )
        {
871
            // XXX FIXME this needs squashing into something much smaller
Rafaël Carré's avatar
Rafaël Carré committed
872 873 874 875
            case ID_playlist_add:
            {
                if( (argCount < 1) || (argCount > 3) )
                    return INVOKERESULT_NO_SUCH_METHOD;
876
                if( !npapi::is_string( args[0] ) )
JP Dinger's avatar
JP Dinger committed
877
                    return INVOKERESULT_NO_SUCH_METHOD;
Rafaël Carré's avatar
Rafaël Carré committed
878 879

                // grab URL
880 881 882
                if( NPVARIANT_IS_NULL(args[0]) )
                    return INVOKERESULT_NO_SUCH_METHOD;

883 884 885 886
                // 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
887

888
                //FIXME: This is never used
889
                auto name = npapi::NPStringPtr( nullptr, NPN_MemFree );
Rafaël Carré's avatar
Rafaël Carré committed
890 891 892 893 894 895 896 897 898
                // grab name if available
                if( argCount > 1 )
                {
                    if( NPVARIANT_IS_NULL(args[1]) )
                    {
                        // do nothing
                    }
                    else if( NPVARIANT_IS_STRING(args[1]) )
                    {
899
                        name = npapi::to_string( args[1] );
Rafaël Carré's avatar
Rafaël Carré committed
900 901 902 903 904 905 906 907 908 909 910 911 912
                    }
                    else
                    {
                        return INVOKERESULT_INVALID_VALUE;
                    }
                }

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

                // grab options if available
                if( argCount > 2 )
                {
913 914
                    npapi::Variant v = args[2];
                    if( v.is<std::nullptr_t>() )
Rafaël Carré's avatar
Rafaël Carré committed
915 916 917
                    {
                        // do nothing
                    }
918
                    else if( v.is<NPString>() )
Rafaël Carré's avatar
Rafaël Carré committed
919
                    {
920
                        parseOptions((NPString)v, &i_options, &ppsz_options);
Rafaël Carré's avatar
Rafaël Carré committed
921 922

                    }
923
                    else if( v.is<NPObject>() )
Rafaël Carré's avatar
Rafaël Carré committed
924
                    {
925
                        parseOptions((NPObject*)v, &i_options, &ppsz_options);
Rafaël Carré's avatar
Rafaël Carré committed
926 927 928 929 930 931 932
                    }
                    else
                    {
                        return INVOKERESULT_INVALID_VALUE;
                    }
                }

933
                int item = p_plugin->player().add_item( url ? url.get() : s.get(), i_options, const_cast<const char **>(ppsz_options));
934 935 936
                if( item == -1 )
                    RETURN_ON_ERROR;

Rafaël Carré's avatar
Rafaël Carré committed
937 938
                for( int i=0; i< i_options; ++i )
                {
Jean-Paul Saman's avatar
Jean-Paul Saman committed
939
                    free(ppsz_options[i]);
Rafaël Carré's avatar
Rafaël Carré committed
940
                }
941
                free(ppsz_options);
Rafaël Carré's avatar
Rafaël Carré committed
942

943 944
                INT32_TO_NPVARIANT(item, result);
                return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
945 946 947 948
            }
            case ID_playlist_play:
                if( argCount == 0 )
                {
949
                    p_plugin->player().play();
950 951
                    VOID_TO_NPVARIANT(result);
                    return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
952 953 954
                }
                return INVOKERESULT_NO_SUCH_METHOD;
            case ID_playlist_playItem:
955 956 957 958 959
            {
                if ( argCount < 1 )
                    return INVOKERESULT_INVALID_ARGS;
                npapi::Variant v = args[0];
                if ( v.is<int>() )
Rafaël Carré's avatar
Rafaël Carré committed
960
                {
961
                    p_plugin->player().mlp().playItemAtIndex( v );
962 963
                    VOID_TO_NPVARIANT(result);
                    return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
964 965
                }
                return INVOKERESULT_NO_SUCH_METHOD;
966
            }
967
            case ID_playlist_pause:
Rafaël Carré's avatar
Rafaël Carré committed
968 969
                if( argCount == 0 )
                {
970
                    p_plugin->player().get_mp().setPause( true );
971 972
                    VOID_TO_NPVARIANT(result);
                    return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
973 974
                }
                return INVOKERESULT_NO_SUCH_METHOD;
975 976 977
            case ID_playlist_togglepause:
                if( argCount == 0 )
                {
978
                    p_plugin->player().mlp().pause();
979 980 981 982
                    VOID_TO_NPVARIANT(result);
                    return INVOKERESULT_NO_ERROR;
                }
                return INVOKERESULT_NO_SUCH_METHOD;
Rafaël Carré's avatar
Rafaël Carré committed
983 984 985
            case ID_playlist_stop:
                if( argCount == 0 )
                {
986
                    p_plugin->player().mlp().stop();
987 988
                    VOID_TO_NPVARIANT(result);
                    return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
989 990 991 992 993
                }
                return INVOKERESULT_NO_SUCH_METHOD;
            case ID_playlist_next:
                if( argCount == 0 )
                {
994
                    p_plugin->player().mlp().next();
995 996
                    VOID_TO_NPVARIANT(result);
                    return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
997 998 999 1000 1001
                }
                return INVOKERESULT_NO_SUCH_METHOD;
            case ID_playlist_prev:
                if( argCount == 0 )
                {
1002
                    p_plugin->player().mlp().previous();
1003 1004
                    VOID_TO_NPVARIANT(result);
                    return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
1005 1006 1007 1008 1009
                }
                return INVOKERESULT_NO_SUCH_METHOD;
            case ID_playlist_clear: /* deprecated */
                if( argCount == 0 )
                {
1010
                    p_plugin->player().clear_items();
1011 1012
                    VOID_TO_NPVARIANT(result);
                    return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
1013 1014 1015
                }
                return INVOKERESULT_NO_SUCH_METHOD;
            case ID_playlist_removeitem: /* deprecated */
1016 1017 1018 1019 1020
            {
                if ( argCount < 1 )
                    return INVOKERESULT_INVALID_ARGS;
                npapi::Variant v = args[0];
                if ( v.is<int>() )
Rafaël Carré's avatar
Rafaël Carré committed
1021
                {
1022
                    if( !p_plugin->player().delete_item( v ) )
1023
                        return INVOKERESULT_GENERIC_ERROR;
1024 1025
                    VOID_TO_NPVARIANT(result);
                    return INVOKERESULT_NO_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
1026 1027
                }
                return INVOKERESULT_NO_SUCH_METHOD;
1028
            }
Rafaël Carré's avatar
Rafaël Carré committed
1029 1030 1031 1032 1033 1034 1035
            default:
                ;
        }
    }
    return INVOKERESULT_GENERIC_ERROR;
}

1036 1037 1038 1039 1040 1041
// 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.

1042
void LibvlcPlaylistNPObject::parseOptions(const NPString& nps,
1043
                                         int *i_options, char*** ppsz_options)
Rafaël Carré's avatar
Rafaël Carré committed
1044
{
1045
    if( nps.UTF8Length )
Rafaël Carré's avatar
Rafaël Carré committed
1046
    {
1047 1048
        auto s = CStr( strdup( nps.UTF8Characters ), free );
        char *val = s.get();
Rafaël Carré's avatar
Rafaël Carré committed
1049 1050 1051 1052 1053 1054 1055 1056
        if( val )
        {
            long capacity = 16;
            char **options = (char **)malloc(capacity*sizeof(char *));
            if( options )
            {
                int nOptions = 0;

1057
                char *end = val + nps.UTF8Length;
Rafaël Carré's avatar
Rafaël Carré committed
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
                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;
1084
                            char **moreOptions = (char **)realloc(options, capacity*sizeof(char*));
Rafaël Carré's avatar
Rafaël Carré committed
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
                            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;
            }
        }
    }
}

1109
// XXX FIXME See comment at the other parseOptions variant.
1110 1111
void LibvlcPlaylistNPObject::parseOptions(NPObject *obj, int *i_options,
                                          char*** ppsz_options)
Rafaël Carré's avatar
Rafaël Carré committed
1112 1113 1114
{
    /* WARNING: Safari does not implement NPN_HasProperty/NPN_HasMethod */

1115
    npapi::Variant value;
Rafaël Carré's avatar
Rafaël Carré committed
1116 1117 1118

    /* we are expecting to have a Javascript Array object */
    NPIdentifier propId = NPN_GetStringIdentifier("length");
1119
    if( NPN_GetProperty(_instance, obj, propId, value) )
Rafaël Carré's avatar
Rafaël Carré committed
1120
    {
1121
        int count = value;
Rafaël Carré's avatar
Rafaël Carré committed
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132

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

                while( nOptions < count )
                {
1133
                    npapi::Variant value;
Rafaël Carré's avatar
Rafaël Carré committed
1134
                    propId = NPN_GetIntIdentifier(nOptions);
1135
                    if( ! NPN_GetProperty(_instance, obj, propId, value) )
Rafaël Carré's avatar
Rafaël Carré committed
1136 1137 1138
                        /* return what we got so far */
                        break;

1139
                    if( !value.is<NPString>() )
Rafaël Carré's avatar
Rafaël Carré committed
1140 1141 1142 1143 1144 1145 1146 1147
                    {
                        /* return what we got so far */
                        break;
                    }

                    if( nOptions == capacity )
                    {
                        capacity += 16;
1148
                        char **moreOptions = (char **)realloc(options, capacity*sizeof(char*));
Rafaël Carré's avatar
Rafaël Carré committed
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
                        if( ! moreOptions )
                        {
                            /* failed to allocate more memory */
                            /* return what we got so far */
                            *i_options = nOptions;
                            *ppsz_options = options;
                            break;
                        }
                        options = moreOptions;
                    }

1160
                    options[nOptions++] = strdup(value);
Rafaël Carré's avatar
Rafaël Carré committed
1161 1162 1163 1164 1165 1166 1167 1168
                }
                *i_options = nOptions;
                *ppsz_options = options;
            }
        }
    }
}

1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
/*
** 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
LibvlcSubtitleNPObject::getProperty(int index, NPVariant &result)
{
    /* is plugin still running */
    if( isPluginRunning() )
    {
1192
        VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
1193 1194
        auto& mp = p_plugin->getMD();
        if( !mp )
1195
            RETURN_ON_ERROR;
1196 1197 1198 1199 1200

        switch( index )
        {
            case ID_subtitle_track:
            {
1201
                /* get the current internal subtitles track ID */
1202
                int actualTrack = p_plugin->player().currentSubtitleTrack();
1203
                INT32_TO_NPVARIANT(actualTrack, result);
1204 1205 1206 1207 1208
                return INVOKERESULT_NO_ERROR;
            }
            case ID_subtitle_count:
            {
                /* get the number of subtitles available */
1209
                int i_spu = mp.spuCount();
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
                /* return it */
                INT32_TO_NPVARIANT(i_spu, result);
                return INVOKERESULT_NO_ERROR;
            }
        }
    }
    return INVOKERESULT_GENERIC_ERROR;
}

RuntimeNPObject::InvokeResult
LibvlcSubtitleNPObject::setProperty(int index, const NPVariant &value)
{
    /* is plugin still running */
    if( isPluginRunning() )
    {
1225
        VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
1226 1227
        auto& mp = p_plugin->getMD();
        if( !mp )
1228
            RETURN_ON_ERROR;
1229 1230 1231 1232 1233

        switch( index )
        {
            case ID_subtitle_track:
            {
1234 1235
                auto v = npapi::Variant( value );
                if( v.is<int>() )
1236
                {
1237 1238 1239 1240
                    auto tracks = mp.spuDescription();
                    if ( v >= tracks.size() )
                        return INVOKERESULT_INVALID_ARGS;
                    if ( mp.setSpu( tracks[ v ].id() ) )
1241
                        return INVOKERESULT_NO_ERROR;
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
                }
                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,
                            uint32_t argCount, NPVariant &result)
{
    /* is plugin still running */
    if( isPluginRunning() )
    {
1268
        VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
1269 1270
        auto& mp = p_plugin->getMD();
        if( !mp )
1271
            RETURN_ON_ERROR;
1272 1273 1274 1275 1276

        switch( index )
        {
            case ID_subtitle_description:
            {
1277 1278 1279 1280
                if ( argCount < 1 )
                    return INVOKERESULT_INVALID_ARGS;
                auto v = npapi::Variant( args[0] );
                if ( v.is<int>() )
1281
                {
1282
                    auto tracks = mp.spuDescription();
1283
                    if ( v >= tracks.size() )
1284
                        return INVOKERESULT_INVALID_VALUE;
1285
                    /* display the name of the track chosen */
1286
                    return invokeResultString( tracks[v].name().c_str(), result );
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
                }
                return INVOKERESULT_NO_SUCH_METHOD;
            }
            default:
                return INVOKERESULT_NO_SUCH_METHOD;
        }
    }
    return INVOKERESULT_GENERIC_ERROR;
}

Rafaël Carré's avatar
Rafaël Carré committed
1297 1298 1299 1300
/*
** implementation of libvlc video object
*/

1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
LibvlcVideoNPObject::~LibvlcVideoNPObject()
{
    if( isValid() )
    {
        if( marqueeObj ) NPN_ReleaseObject(marqueeObj);
        if( logoObj    ) NPN_ReleaseObject(logoObj);
        if( deintObj   ) NPN_ReleaseObject(deintObj);
    }
}

1311
const NPUTF8 * const LibvlcVideoNPObject::propertyNames[] =
Rafaël Carré's avatar
Rafaël Carré committed
1312 1313 1314 1315 1316 1317 1318
{
    "fullscreen",
    "height",
    "width",
    "aspectRatio",
    "subtitle",
    "crop",
1319
    "teletext",
1320
    "marquee",
1321 1322
    "logo",
    "deinterlace",
Rafaël Carré's avatar
Rafaël Carré committed
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
};

enum LibvlcVideoNPObjectPropertyIds
{
    ID_video_fullscreen,
    ID_video_height,
    ID_video_width,
    ID_video_aspectratio,
    ID_video_subtitle,
    ID_video_crop,
1333
    ID_video_teletext,
1334
    ID_video_marquee,
1335 1336
    ID_video_logo,
    ID_video_deinterlace,
Rafaël Carré's avatar
Rafaël Carré committed
1337
};
1338
COUNTNAMES(LibvlcVideoNPObject,propertyCount,propertyNames);
Rafaël Carré's avatar
Rafaël Carré committed
1339

1340 1341
RuntimeNPObject::InvokeResult
LibvlcVideoNPObject::getProperty(int index, NPVariant &result)
Rafaël Carré's avatar
Rafaël Carré committed
1342 1343
{
    /* is plugin still running */
1344
    if( isPluginRunning() )
Rafaël Carré's avatar
Rafaël Carré committed
1345
    {
1346
        VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
1347 1348
        auto& mp = p_plugin->getMD();
        if( !mp )
1349
            RETURN_ON_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
1350 1351 1352 1353 1354

        switch( index )
        {
            case ID_video_fullscreen:
            {
1355
                int val = p_plugin->get_fullscreen();
Rafaël Carré's avatar
Rafaël Carré committed
1356 1357 1358 1359 1360
                BOOLEAN_TO_NPVARIANT(val, result);
                return INVOKERESULT_NO_ERROR;
            }
            case ID_video_height:
            {
1361 1362 1363
                unsigned width, height;
                mp.size( 0, &width, &height );
                INT32_TO_NPVARIANT(height, result);
Rafaël Carré's avatar
Rafaël Carré committed
1364 1365 1366 1367
                return INVOKERESULT_NO_ERROR;
            }
            case ID_video_width:
            {
1368 1369 1370
                unsigned width, height;
                mp.size( 0, &width, &height );
                INT32_TO_NPVARIANT(width, result);
Rafaël Carré's avatar
Rafaël Carré committed
1371 1372 1373 1374
                return INVOKERESULT_NO_ERROR;
            }
            case ID_video_aspectratio:
            {
1375 1376
                auto ar = mp.aspectRatio();
                if( ar.empty() )
Rafaël Carré's avatar
Rafaël Carré committed
1377 1378
                    return INVOKERESULT_GENERIC_ERROR;

1379
                STRINGZ_TO_NPVARIANT(ar.c_str(), result);
Rafaël Carré's avatar
Rafaël Carré committed
1380 1381 1382 1383
                return INVOKERESULT_NO_ERROR;
            }
            case ID_video_subtitle:
            {
1384
                int i_spu = mp.spu();
Rafaël Carré's avatar
Rafaël Carré committed
1385 1386 1387 1388 1389
                INT32_TO_NPVARIANT(i_spu, result);
                return INVOKERESULT_NO_ERROR;
            }
            case ID_video_crop:
            {
1390 1391
                auto geo = mp.cropGeometry();
                if( geo.empty() )
Rafaël Carré's avatar
Rafaël Carré committed
1392 1393
                    return INVOKERESULT_GENERIC_ERROR;

1394
                STRINGZ_TO_NPVARIANT(geo.c_str(), result);
Rafaël Carré's avatar
Rafaël Carré committed
1395 1396 1397 1398
                return INVOKERESULT_NO_ERROR;
            }
            case ID_video_teletext:
            {
1399
                int i_page = mp.teletext();
1400 1401
                if( i_page < 0 )
                    return INVOKERESULT_GENERIC_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
1402 1403 1404
                INT32_TO_NPVARIANT(i_page, result);
                return INVOKERESULT_NO_ERROR;
            }
1405 1406
            case ID_video_marquee:
            {
1407 1408
                InstantObj<LibvlcMarqueeNPObject>( marqueeObj );
                OBJECT_TO_NPVARIANT(NPN_RetainObject(marqueeObj), result);
1409 1410
                return INVOKERESULT_NO_ERROR;
            }
1411 1412 1413 1414 1415 1416
            case ID_video_logo:
            {
                InstantObj<LibvlcLogoNPObject>( logoObj );
                OBJECT_TO_NPVARIANT(NPN_RetainObject(logoObj), result);
                return INVOKERESULT_NO_ERROR;
            }
1417 1418 1419 1420 1421 1422
            case ID_video_deinterlace:
            {
                InstantObj<LibvlcDeinterlaceNPObject>( deintObj );
                OBJECT_TO_NPVARIANT(NPN_RetainObject(deintObj), result);
                return INVOKERESULT_NO_ERROR;
            }
Rafaël Carré's avatar
Rafaël Carré committed
1423 1424 1425 1426 1427
        }
    }
    return INVOKERESULT_GENERIC_ERROR;
}

1428 1429
RuntimeNPObject::InvokeResult
LibvlcVideoNPObject::setProperty(int index, const NPVariant &value)
Rafaël Carré's avatar
Rafaël Carré committed
1430 1431
{
    /* is plugin still running */
1432
    if( isPluginRunning() )
Rafaël Carré's avatar
Rafaël Carré committed
1433
    {
1434
        VlcPluginBase* p_plugin = getPrivate<VlcPluginBase>();
1435 1436
        auto& mp = p_plugin->getMD();
        if( !mp )
1437
            RETURN_ON_ERROR;
Rafaël Carré's avatar
Rafaël Carré committed
1438

1439 1440
        auto v = npapi::Variant( value );

Rafaël Carré's avatar
Rafaël Carré committed
1441 1442 1443 1444
        switch( index )
        {
            case ID_video_fullscreen:
            {
1445
                if( !v.is<bool>() )
Rafaël Carré's avatar
Rafaël Carré committed
1446 1447
                    return INVOKERESULT_INVALID_VALUE;

1448
                p_plugin->set_fullscreen( (bool)v );
Rafaël Carré's avatar
Rafaël Carré committed
1449 1450 1451 1452 1453
                return INVOKERESULT_NO_ERROR;
            }
            case ID_video_aspectratio:
            {

1454
                if( !v.is<NPString>() )
Rafaël Carré's avatar
Rafaël Carré committed
1455 1456
                    return INVOKERESULT_INVALID_VALUE;

1457 1458 1459 1460
                std::string ar = v;
                if ( ar == "default" )
                    ar = "";
                mp.setAspectRatio( ar );
Rafaël Carré's avatar
Rafaël Carré committed
1461 1462 1463 1464 1465

                return INVOKERESULT_NO_ERROR;
            }
            case ID_video_subtitle:
            {
1466
                if( v.is<int>() )
Rafaël Carré's avatar
Rafaël Carré committed
1467
                {
1468
                    mp.setSpu( v );
Rafaël Carré's avatar
Rafaël Carré committed
1469 1470 1471 1472 1473 1474
                    return INVOKERESULT_NO_ERROR;
                }
                return INVOKERESULT_INVALID_VALUE;
            }
            case ID_video_crop:
            {
1475
                if( !v.is<NPString>() )
Rafaël Carré's avatar