vlccontrol2.cpp 38.7 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * vlccontrol2.cpp: ActiveX control for VLC
 *****************************************************************************
 * Copyright (C) 2006 the VideoLAN team
5
 * Copyright (C) 2010 M2X BV
6 7
 *
 * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
8
 *          Jean-Paul Saman <jpsaman _at_ m2x _dot_ nl>
9 10 11 12 13 14 15 16 17 18 19
 *
 * 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.
 *
20 21 22
 * 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.
23
 *****************************************************************************/
24
#include "config.h"
25

26
#include <stdio.h>
27 28
#include <shlwapi.h>
#include <wininet.h>
29
#include <tchar.h>
30

31 32 33 34 35
#include "utils.h"
#include "plugin.h"
#include "vlccontrol2.h"
#include "vlccontrol.h"

36
#include "../common/position.h"
37

38 39 40
// ---------

HRESULT VLCInterfaceBase::loadTypeInfo(REFIID riid)
41
{
42
    // if( _ti ) return NOERROR; // unnecessairy
43

44 45 46
    ITypeLib *p_typelib;
    HRESULT hr = _plug->getTypeLib(LOCALE_USER_DEFAULT, &p_typelib);
    if( SUCCEEDED(hr) )
47
    {
48 49 50
        hr = p_typelib->GetTypeInfoOfGuid(riid, &_ti);
        if( FAILED(hr) ) _ti = NULL;
        p_typelib->Release();
51 52
    }
    return hr;
53
}
54

55 56
#define BIND_INTERFACE( class ) \
    template<> REFIID VLCInterface<class, I##class>::_riid = IID_I##class;
57

58 59 60 61
BIND_INTERFACE( VLCAudio )
BIND_INTERFACE( VLCInput )
BIND_INTERFACE( VLCMarquee )
BIND_INTERFACE( VLCLogo )
62
BIND_INTERFACE( VLCDeinterlace )
63 64 65 66
BIND_INTERFACE( VLCPlaylistItems )
BIND_INTERFACE( VLCPlaylist )
BIND_INTERFACE( VLCVideo )
BIND_INTERFACE( VLCSubtitle )
67
BIND_INTERFACE( VLCMediaDescription )
68

69
#undef  BIND_INTERFACE
70

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
template<class I> static inline
HRESULT object_get(I **dst, I *src)
{
    if( NULL == dst )
        return E_POINTER;

    *dst = src;
    if( NULL != src )
    {
        src->AddRef();
        return NOERROR;
    }
    return E_OUTOFMEMORY;
}

86 87
static inline
VARIANT_BOOL varbool(bool b) { return b ? VARIANT_TRUE : VARIANT_FALSE; }
88

89
// ---------
90 91 92 93 94 95

STDMETHODIMP VLCAudio::get_mute(VARIANT_BOOL* mute)
{
    if( NULL == mute )
        return E_POINTER;

96 97 98
    *mute = varbool( Instance()->get_player().is_muted() );

    return S_OK;
99 100 101 102
};

STDMETHODIMP VLCAudio::put_mute(VARIANT_BOOL mute)
{
103 104 105
    Instance()->get_player().set_mute( VARIANT_FALSE != mute );

    return S_OK;
106 107
};

108
STDMETHODIMP VLCAudio::get_volume(long* volume)
109 110 111 112
{
    if( NULL == volume )
        return E_POINTER;

113 114 115
    *volume = Instance()->get_player().get_volume();

    return S_OK;
116 117
};

118
STDMETHODIMP VLCAudio::put_volume(long volume)
119
{
120 121 122
    Instance()->get_player().set_volume( volume );

    return S_OK;
123 124
};

125 126 127 128 129
STDMETHODIMP VLCAudio::get_track(long* track)
{
    if( NULL == track )
        return E_POINTER;

130
    libvlc_media_player_t* p_md;
131

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    HRESULT hr = getMD(&p_md);
    if( SUCCEEDED(hr) )
    {
        // get the current internal audio track ID
        int i_actualTrack = libvlc_audio_get_track(p_md);

        //get the number of available track
        int i_audioTrackCount = libvlc_audio_get_track_count(p_md);
        if( i_audioTrackCount < 0 )
        {
            *track = i_actualTrack;
            return S_OK;
        }

        // get tracks description
        libvlc_track_description_t *p_currentTrack = libvlc_audio_get_track_description(p_md);
        if( !p_currentTrack )
            return E_FAIL;

        int i_fakeTrackIndex = 0;
        while( p_currentTrack )
        {
            if (i_actualTrack == p_currentTrack->i_id)
                break;

            p_currentTrack = p_currentTrack->p_next;
            i_fakeTrackIndex++;
        }
        libvlc_track_description_list_release(p_currentTrack);

        *track = i_fakeTrackIndex;
    }
    return hr;
165 166 167 168
};

STDMETHODIMP VLCAudio::put_track(long track)
{
169
    libvlc_media_player_t* p_md;
170

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    HRESULT hr = getMD(&p_md);
    if( SUCCEEDED(hr) )
    {
        // bounds checking
        int i_count = libvlc_audio_get_track_count(p_md);
        if (track >= i_count || i_count == 0)
            return E_INVALIDARG;

        libvlc_track_description_t *p_currentTrack = libvlc_audio_get_track_description(p_md);

        for ( int x = 0; x < track+1; x++ )
        {
            if (x == track)
                break;

            if (p_currentTrack->p_next)
                p_currentTrack = p_currentTrack->p_next;
            else
            {
                libvlc_track_description_list_release(p_currentTrack);
                return E_INVALIDARG;
            }
        }
        int actualTrack = p_currentTrack->i_id;
        libvlc_track_description_list_release(p_currentTrack);

        libvlc_audio_set_track(p_md, actualTrack);
    }
    return hr;
200 201
};

202 203 204 205 206
STDMETHODIMP VLCAudio::get_count(long* trackNumber)
{
    if( NULL == trackNumber )
        return E_POINTER;

207
    *trackNumber = Instance()->get_player().track_count();
208

209 210
    return S_OK;
};
211 212 213 214 215 216 217 218

STDMETHODIMP VLCAudio::description(long trackID, BSTR* name)
{
    if( NULL == name )
        return E_POINTER;

    libvlc_media_player_t* p_md;

219
    HRESULT hr = getMD(&p_md);
220 221 222 223 224 225 226
    if( SUCCEEDED(hr) )
    {
        int i, i_limit;
        const char *psz_name;
        libvlc_track_description_t *p_trackDesc;

        // get tracks description
227 228
        p_trackDesc = libvlc_audio_get_track_description(p_md);
        if( !p_trackDesc )
229 230
        {
            libvlc_track_description_list_release(p_trackDesc);
231
            return E_FAIL;
232
        }
233 234

        //get the number of available track
235 236
        i_limit = libvlc_audio_get_track_count(p_md);
        if( i_limit < 0 )
237 238
        {
            libvlc_track_description_list_release(p_trackDesc);
239
            return E_FAIL;
240
        }
241 242 243

        // check if the number given is a good one
        if ( ( trackID > ( i_limit -1 ) ) || ( trackID < 0 ) )
244 245 246 247
        {
            libvlc_track_description_list_release(p_trackDesc);
            return E_INVALIDARG;
        }
248 249 250 251 252 253 254 255

        // get the good trackDesc
        for( i = 0 ; i < trackID ; i++ )
        {
            p_trackDesc = p_trackDesc->p_next;
        }
        // get the track name
        psz_name = p_trackDesc->psz_name;
256
        libvlc_track_description_list_release(p_trackDesc);
257 258 259 260 261 262 263 264 265 266 267 268 269

        // return it
        if( psz_name != NULL )
        {
            *name = BSTRFromCStr(CP_UTF8, psz_name);
            return (NULL == *name) ? E_OUTOFMEMORY : NOERROR;
        }
        *name = NULL;
        return E_FAIL;
    }
    return hr;
};

270
STDMETHODIMP VLCAudio::get_channel(long *channel)
271 272 273 274
{
    if( NULL == channel )
        return E_POINTER;

275 276 277
    *channel = Instance()->get_player().get_channel();

    return S_OK;
278 279
};

280
STDMETHODIMP VLCAudio::put_channel(long channel)
281
{
282 283 284
    Instance()->get_player().set_channel(channel);

    return S_OK;
285 286
};

287 288
STDMETHODIMP VLCAudio::toggleMute()
{
289 290 291
    Instance()->get_player().toggle_mute();

    return S_OK;
292 293
};

294
/****************************************************************************/
295

296 297 298 299 300 301
STDMETHODIMP VLCDeinterlace::disable()
{
    libvlc_media_player_t *p_md;
    HRESULT hr = getMD(&p_md);
    if( SUCCEEDED(hr) )
    {
302
        libvlc_video_set_deinterlace(p_md, "");
303 304 305 306 307 308 309 310 311 312 313
    }
    return hr;
}

STDMETHODIMP VLCDeinterlace::enable(BSTR mode)
{
    libvlc_media_player_t *p_md;
    HRESULT hr = getMD(&p_md);
    if( SUCCEEDED(hr) )
    {
        char *psz_mode = CStrFromBSTR(CP_UTF8, mode);
314
        libvlc_video_set_deinterlace(p_md, psz_mode);
315 316 317 318 319 320 321
        CoTaskMemFree(psz_mode);
    }
    return hr;
}

/****************************************************************************/

322
STDMETHODIMP VLCInput::get_length(double* length)
323 324 325 326
{
    if( NULL == length )
        return E_POINTER;

327 328 329
    *length = static_cast<double>(Instance()->get_player().get_length());

    return S_OK;
330 331
};

332
STDMETHODIMP VLCInput::get_position(double* position)
333 334 335 336
{
    if( NULL == position )
        return E_POINTER;

337 338 339
    *position = Instance()->get_player().get_position();

    return S_OK;
340 341
};

342
STDMETHODIMP VLCInput::put_position(double position)
343
{
344 345 346
    Instance()->get_player().set_position( static_cast<float>(position) );

    return S_OK;
347 348
};

349
STDMETHODIMP VLCInput::get_time(double* time)
350 351 352 353
{
    if( NULL == time )
        return E_POINTER;

354 355 356
    *time = static_cast<double>(Instance()->get_player().get_time());

    return S_OK;
357 358
};

359
STDMETHODIMP VLCInput::put_time(double time)
360
{
361 362 363
    Instance()->get_player().set_time(static_cast<libvlc_time_t>(time));

    return S_OK;
364 365
};

366
STDMETHODIMP VLCInput::get_state(long* state)
367 368 369 370
{
    if( NULL == state )
        return E_POINTER;

371 372 373
    *state = Instance()->get_player().get_state();

    return S_OK;
374 375
};

376
STDMETHODIMP VLCInput::get_rate(double* rate)
377 378 379 380
{
    if( NULL == rate )
        return E_POINTER;

381 382 383
    *rate = Instance()->get_player().get_rate();

    return S_OK;
384 385
};

386
STDMETHODIMP VLCInput::put_rate(double rate)
387
{
388 389 390
    Instance()->get_player().set_rate(static_cast<float>(rate));

    return S_OK;
391 392
};

393
STDMETHODIMP VLCInput::get_fps(double* fps)
394 395 396 397
{
    if( NULL == fps )
        return E_POINTER;

398 399 400
    *fps = static_cast<double>(Instance()->get_player().get_fps());

    return S_OK;
401 402 403 404 405 406 407
};

STDMETHODIMP VLCInput::get_hasVout(VARIANT_BOOL* hasVout)
{
    if( NULL == hasVout )
        return E_POINTER;

408 409 410
    *hasVout = varbool( Instance()->get_player().has_vout() );

    return S_OK;
411 412
};

413
/****************************************************************************/
414

415
HRESULT VLCMarquee::do_put_int(unsigned idx, LONG val)
416 417
{
    libvlc_media_player_t *p_md;
418
    HRESULT hr = getMD(&p_md);
419 420
    if( SUCCEEDED(hr) )
    {
421
        libvlc_video_set_marquee_int(p_md, idx, val);
422 423
    }
    return hr;
424
}
425

426
HRESULT VLCMarquee::do_get_int(unsigned idx, LONG *val)
427
{
428 429
    if( NULL == val )
        return E_POINTER;
430 431

    libvlc_media_player_t *p_md;
432
    HRESULT hr = getMD(&p_md);
433 434
    if( SUCCEEDED(hr) )
    {
435
        *val = libvlc_video_get_marquee_int(p_md, idx);
436 437
    }
    return hr;
438
}
439

440
STDMETHODIMP VLCMarquee::get_position(BSTR* val)
441
{
442 443
    if( NULL == val )
        return E_POINTER;
444

445 446 447 448
    LONG i;
    HRESULT hr = do_get_int(libvlc_marquee_Position, &i);
    if(SUCCEEDED(hr))
        *val = BSTRFromCStr(CP_UTF8, position_bynumber(i));
449 450

    return hr;
451
}
452

453
STDMETHODIMP VLCMarquee::put_position(BSTR val)
454
{
455 456
    char *n = CStrFromBSTR(CP_UTF8, val);
    if( !n ) return E_OUTOFMEMORY;
457

458 459 460 461 462 463
    size_t i;
    HRESULT hr;
    if( position_byname( n, i ) )
        hr = do_put_int(libvlc_marquee_Position,i);
    else
        hr = E_INVALIDARG;
464

465
    CoTaskMemFree(n);
466
    return hr;
467
}
468

469
STDMETHODIMP VLCMarquee::get_text(BSTR *val)
470
{
471 472 473
    char *psz;
    if( NULL == val )
        return E_POINTER;
474 475

    libvlc_media_player_t *p_md;
476
    HRESULT hr = getMD(&p_md);
477 478
    if( SUCCEEDED(hr) )
    {
479 480
        psz = libvlc_video_get_marquee_string(p_md, libvlc_marquee_Text);
        if( psz )
481
            *val = BSTRFromCStr(CP_UTF8, psz);
482 483
    }
    return hr;
484
}
485

486
STDMETHODIMP VLCMarquee::put_text(BSTR val)
487 488
{
    libvlc_media_player_t *p_md;
489
    HRESULT hr = getMD(&p_md);
490 491
    if( SUCCEEDED(hr) )
    {
492
        char *psz_text = CStrFromBSTR(CP_UTF8, val);
493
        libvlc_video_set_marquee_string(p_md, libvlc_marquee_Text, psz_text);
494
        CoTaskMemFree(psz_text);
495 496
    }
    return hr;
497
}
498

499
/****************************************************************************/
500

501 502 503 504 505
STDMETHODIMP VLCPlaylistItems::get_count(long* count)
{
    if( NULL == count )
        return E_POINTER;

506
    *count = Instance()->get_player().items_count();
507
    return S_OK;
508 509 510 511
};

STDMETHODIMP VLCPlaylistItems::clear()
{
512
    Instance()->get_player().clear_items();
513
    return S_OK;
514 515 516 517
};

STDMETHODIMP VLCPlaylistItems::remove(long item)
{
518 519
    Instance()->get_player().delete_item(item);
    return S_OK;
520 521
};

522
/****************************************************************************/
523

524
STDMETHODIMP VLCPlaylist::get_itemCount(long* count)
525 526 527 528
{
    if( NULL == count )
        return E_POINTER;

529 530
    *count = Instance()->get_player().items_count();

531
    return S_OK;
532 533 534 535 536 537 538
};

STDMETHODIMP VLCPlaylist::get_isPlaying(VARIANT_BOOL* isPlaying)
{
    if( NULL == isPlaying )
        return E_POINTER;

539 540 541
    *isPlaying = varbool( Instance()->get_player().is_playing()!=0 );

    return S_OK;
542 543
};

544 545 546 547 548 549 550 551 552 553
STDMETHODIMP VLCPlaylist::get_currentItem(long* index)
{
    if( NULL == index )
        return E_POINTER;

    *index = Instance()->get_player().current_item();

    return S_OK;
};

554
STDMETHODIMP VLCPlaylist::add(BSTR uri, VARIANT name, VARIANT options, long* item)
555 556 557 558 559 560 561 562
{
    if( NULL == item )
        return E_POINTER;

    if( 0 == SysStringLen(uri) )
        return E_INVALIDARG;

    libvlc_instance_t* p_libvlc;
563
    HRESULT hr = getVLC(&p_libvlc);
564 565
    if( SUCCEEDED(hr) )
    {
566
        char *psz_uri = NULL;
567
        if( SysStringLen(Instance()->getBaseURL()) > 0 )
568 569 570 571
        {
            /*
            ** if the MRL a relative URL, we should end up with an absolute URL
            */
572
            LPWSTR abs_url = CombineURL(Instance()->getBaseURL(), uri);
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
            if( NULL != abs_url )
            {
                psz_uri = CStrFromWSTR(CP_UTF8, abs_url, wcslen(abs_url));
                CoTaskMemFree(abs_url);
            }
            else
            {
                psz_uri = CStrFromBSTR(CP_UTF8, uri);
            }
        }
        else
        {
            /*
            ** baseURL is empty, assume MRL is absolute
            */
            psz_uri = CStrFromBSTR(CP_UTF8, uri);
        }

        if( NULL == psz_uri )
        {
            return E_OUTOFMEMORY;
        }

596 597 598 599 600 601
        int i_options;
        char **ppsz_options;

        hr = VLCControl::CreateTargetOptions(CP_UTF8, &options, &ppsz_options, &i_options);
        if( FAILED(hr) )
        {
602 603
            CoTaskMemFree(psz_uri);
            return hr;
604 605 606
        }

        char *psz_name = NULL;
607 608 609
        VARIANT v_name;
        VariantInit(&v_name);
        if( SUCCEEDED(VariantChangeType(&v_name, &name, 0, VT_BSTR)) )
610
        {
611 612 613 614
            if( SysStringLen(V_BSTR(&v_name)) > 0 )
                psz_name = CStrFromBSTR(CP_UTF8, V_BSTR(&v_name));

            VariantClear(&v_name);
615 616
        }

617
        *item = Instance()->playlist_add_extended_untrusted(psz_uri,
618
                    i_options, const_cast<const char **>(ppsz_options));
619 620 621

        VLCControl::FreeTargetOptions(ppsz_options, i_options);
        CoTaskMemFree(psz_uri);
622
        if( psz_name ) /* XXX Do we even need to check? */
623 624 625 626 627 628 629
            CoTaskMemFree(psz_name);
    }
    return hr;
};

STDMETHODIMP VLCPlaylist::play()
{
630
    Instance()->get_player().play();
631
    return S_OK;
632 633
};

634
STDMETHODIMP VLCPlaylist::playItem(long item)
635
{
636
    Instance()->get_player().play(item);
637
    return S_OK;
638 639
};

640
STDMETHODIMP VLCPlaylist::pause()
641
{
642 643
    Instance()->get_player().pause();
    return S_OK;
644 645
};

646 647 648 649 650 651
STDMETHODIMP VLCPlaylist::togglePause()
{
    Instance()->get_player().togglePause();
    return S_OK;
};

652 653
STDMETHODIMP VLCPlaylist::stop()
{
654 655
    Instance()->get_player().stop();
    return S_OK;
656 657 658 659
};

STDMETHODIMP VLCPlaylist::next()
{
660
    Instance()->get_player().next();
661
    return S_OK;
662 663 664 665
};

STDMETHODIMP VLCPlaylist::prev()
{
666
    Instance()->get_player().prev();
667
    return S_OK;
668 669 670 671
};

STDMETHODIMP VLCPlaylist::clear()
{
672
    Instance()->get_player().clear_items();
673
    return S_OK;
674 675
};

676
STDMETHODIMP VLCPlaylist::removeItem(long item)
677
{
678 679
    Instance()->get_player().delete_item(item);
    return S_OK;
680 681
};

682 683 684 685 686 687 688 689 690 691 692 693 694 695
STDMETHODIMP VLCPlaylist::get_items(IVLCPlaylistItems** obj)
{
    if( NULL == obj )
        return E_POINTER;

    *obj = _p_vlcplaylistitems;
    if( NULL != _p_vlcplaylistitems )
    {
        _p_vlcplaylistitems->AddRef();
        return NOERROR;
    }
    return E_OUTOFMEMORY;
};

696
/****************************************************************************/
697

698 699 700 701 702
STDMETHODIMP VLCSubtitle::get_track(long* spu)
{
    if( NULL == spu )
        return E_POINTER;

703 704
    libvlc_media_player_t* p_md;

705
    HRESULT hr = getMD(&p_md);
706 707
    if( SUCCEEDED(hr) )
    {
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
        // get the current internal subtitles track ID
        int i_actualTrack = libvlc_video_get_spu(p_md);

        //get the number of available track
        int i_spuTrackCount = libvlc_video_get_spu_count(p_md);
        if( i_spuTrackCount < 0 )
        {
            *spu = i_actualTrack;
            return S_OK;
        }

        // get tracks description
        libvlc_track_description_t *p_currentTrack = libvlc_video_get_spu_description(p_md);
        if( !p_currentTrack )
            return E_FAIL;

        int i_fakeTrackIndex = 0;
        while( p_currentTrack )
        {
            if (i_actualTrack == p_currentTrack->i_id)
                break;

            p_currentTrack = p_currentTrack->p_next;
            i_fakeTrackIndex++;
        }
        libvlc_track_description_list_release(p_currentTrack);

        *spu = i_fakeTrackIndex;
736 737 738 739 740 741
    }
    return hr;
};

STDMETHODIMP VLCSubtitle::put_track(long spu)
{
742 743
    libvlc_media_player_t* p_md;

744
    HRESULT hr = getMD(&p_md);
745 746
    if( SUCCEEDED(hr) )
    {
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
        // bounds checking
        int i_count = libvlc_video_get_spu_count(p_md);
        if (spu >= i_count || i_count == 0)
            return E_INVALIDARG;

        libvlc_track_description_t *p_currentTrack = libvlc_video_get_spu_description(p_md);

        for ( int x = 0; x < spu+1; x++ )
        {
            if (x == spu)
                break;

            if (p_currentTrack->p_next)
                p_currentTrack = p_currentTrack->p_next;
            else
            {
                libvlc_track_description_list_release(p_currentTrack);
                return E_INVALIDARG;
            }
        }
        int i_actualTrack = p_currentTrack->i_id;
        libvlc_track_description_list_release(p_currentTrack);

        libvlc_video_set_spu(p_md, i_actualTrack);
771 772 773 774 775 776 777 778 779 780
    }
    return hr;
};

STDMETHODIMP VLCSubtitle::get_count(long* spuNumber)
{
    if( NULL == spuNumber )
        return E_POINTER;

    libvlc_media_player_t *p_md;
781
    HRESULT hr = getMD(&p_md);
782 783 784
    if( SUCCEEDED(hr) )
    {
        // get the number of video subtitle available and return it
785
        *spuNumber = libvlc_video_get_spu_count(p_md);
786 787 788 789 790 791 792 793 794 795 796 797
    }
    return hr;
};


STDMETHODIMP VLCSubtitle::description(long nameID, BSTR* name)
{
    if( NULL == name )
       return E_POINTER;

    libvlc_media_player_t* p_md;

798
    HRESULT hr = getMD(&p_md);
799 800 801 802 803 804 805
    if( SUCCEEDED(hr) )
    {
        int i, i_limit;
        const char *psz_name;
        libvlc_track_description_t *p_spuDesc;

        // get subtitles description
806 807
        p_spuDesc = libvlc_video_get_spu_description(p_md);
        if( !p_spuDesc )
808 809
        {
            libvlc_track_description_list_release(p_spuDesc);
810
            return E_FAIL;
811
        }
812 813

        // get the number of available subtitle
814 815
        i_limit = libvlc_video_get_spu_count(p_md);
        if( i_limit < 0 )
816 817
        {
            libvlc_track_description_list_release(p_spuDesc);
818
            return E_FAIL;
819
        }
820 821 822

        // check if the number given is a good one
        if ( ( nameID > ( i_limit -1 ) ) || ( nameID < 0 ) )
823 824 825 826
        {
            libvlc_track_description_list_release(p_spuDesc);
            return E_INVALIDARG;
        }
827 828 829 830 831 832 833 834

        // get the good spuDesc
        for( i = 0 ; i < nameID ; i++ )
        {
            p_spuDesc = p_spuDesc->p_next;
        }
        // get the subtitle name
        psz_name = p_spuDesc->psz_name;
835
        libvlc_track_description_list_release(p_spuDesc);
836 837 838 839 840 841 842 843 844 845 846 847 848

        // return it
        if( psz_name != NULL )
        {
            *name = BSTRFromCStr(CP_UTF8, psz_name);
            return (NULL == *name) ? E_OUTOFMEMORY : NOERROR;
        }
        *name = NULL;
        return E_FAIL;
    }
    return hr;
};

849
/****************************************************************************/
850

851 852 853 854 855
STDMETHODIMP VLCVideo::get_fullscreen(VARIANT_BOOL* fullscreen)
{
    if( NULL == fullscreen )
        return E_POINTER;

JP Dinger's avatar
JP Dinger committed
856
    libvlc_media_player_t *p_md;
857
    HRESULT hr = getMD(&p_md);
858 859
    if( SUCCEEDED(hr) )
    {
860
        *fullscreen = varbool( libvlc_get_fullscreen(p_md) );
861 862 863 864 865 866
    }
    return hr;
};

STDMETHODIMP VLCVideo::put_fullscreen(VARIANT_BOOL fullscreen)
{
JP Dinger's avatar
JP Dinger committed
867
    libvlc_media_player_t *p_md;
868
    HRESULT hr = getMD(&p_md);
869 870
    if( SUCCEEDED(hr) )
    {
871
        libvlc_set_fullscreen(p_md, VARIANT_FALSE != fullscreen);
872 873 874 875
    }
    return hr;
};

876
STDMETHODIMP VLCVideo::get_width(long* width)
877 878 879 880
{
    if( NULL == width )
        return E_POINTER;

JP Dinger's avatar
JP Dinger committed
881
    libvlc_media_player_t *p_md;
882
    HRESULT hr = getMD(&p_md);
883 884
    if( SUCCEEDED(hr) )
    {
885
        *width = libvlc_video_get_width(p_md);
886 887 888 889
    }
    return hr;
};

890
STDMETHODIMP VLCVideo::get_height(long* height)
891 892 893 894
{
    if( NULL == height )
        return E_POINTER;

JP Dinger's avatar
JP Dinger committed
895
    libvlc_media_player_t *p_md;
896
    HRESULT hr = getMD(&p_md);
897 898
    if( SUCCEEDED(hr) )
    {
899
        *height = libvlc_video_get_height(p_md);
900 901 902 903
    }
    return hr;
};

904
STDMETHODIMP VLCVideo::get_aspectRatio(BSTR* aspect)
905
{
906
    if( NULL == aspect )
907 908
        return E_POINTER;

JP Dinger's avatar
JP Dinger committed
909
    libvlc_media_player_t *p_md;
910
    HRESULT hr = getMD(&p_md);
911 912
    if( SUCCEEDED(hr) )
    {
913
        char *psz_aspect = libvlc_video_get_aspect_ratio(p_md);
914

915
        if( !psz_aspect )
916
        {
JP Dinger's avatar
JP Dinger committed
917
            *aspect = BSTRFromCStr(CP_UTF8, psz_aspect);
918 919
            if( NULL == *aspect )
                hr = E_OUTOFMEMORY;
920 921
        } else if( NULL == psz_aspect)
                hr = E_OUTOFMEMORY;
JP Dinger's avatar
JP Dinger committed
922
        free( psz_aspect );
923 924 925 926 927 928 929 930 931
    }
    return hr;
};

STDMETHODIMP VLCVideo::put_aspectRatio(BSTR aspect)
{
    if( NULL == aspect )
        return E_POINTER;

JP Dinger's avatar
JP Dinger committed
932
    libvlc_media_player_t *p_md;
933
    HRESULT hr = getMD(&p_md);
934 935
    if( SUCCEEDED(hr) )
    {
JP Dinger's avatar
JP Dinger committed
936
        char *psz_aspect = CStrFromBSTR(CP_UTF8, aspect);
937
        if( !psz_aspect )
938
        {
JP Dinger's avatar
JP Dinger committed
939 940
            return E_OUTOFMEMORY;
        }
941

942
        libvlc_video_set_aspect_ratio(p_md, psz_aspect);
943

JP Dinger's avatar
JP Dinger committed
944
        CoTaskMemFree(psz_aspect);
945 946 947 948
    }
    return hr;
};

949 950 951 952 953
STDMETHODIMP VLCVideo::get_subtitle(long* spu)
{
    if( NULL == spu )
        return E_POINTER;

JP Dinger's avatar
JP Dinger committed
954
    libvlc_media_player_t *p_md;
955
    HRESULT hr = getMD(&p_md);
956 957
    if( SUCCEEDED(hr) )
    {
958
        *spu = libvlc_video_get_spu(p_md);
959 960 961 962 963 964
    }
    return hr;
};

STDMETHODIMP VLCVideo::put_subtitle(long spu)
{
JP Dinger's avatar
JP Dinger committed
965
    libvlc_media_player_t *p_md;
966
    HRESULT hr = getMD(&p_md);
967 968
    if( SUCCEEDED(hr) )
    {
969
        libvlc_video_set_spu(p_md, spu);
970 971 972 973
    }
    return hr;
};

974 975 976 977 978
STDMETHODIMP VLCVideo::get_crop(BSTR* geometry)
{
    if( NULL == geometry )
        return E_POINTER;

JP Dinger's avatar
JP Dinger committed
979
    libvlc_media_player_t *p_md;
980
    HRESULT hr = getMD(&p_md);
981 982
    if( SUCCEEDED(hr) )
    {
983 984
        char *psz_geometry = libvlc_video_get_crop_geometry(p_md);
        if( !psz_geometry )
985
        {
JP Dinger's avatar
JP Dinger committed
986
            *geometry = BSTRFromCStr(CP_UTF8, psz_geometry);
987 988 989 990
            if( !geometry )
                hr = E_OUTOFMEMORY;
        } else if( !psz_geometry )
                hr = E_OUTOFMEMORY;
JP Dinger's avatar
JP Dinger committed
991
        free( psz_geometry );
992 993 994 995 996 997 998 999 1000 1001 1002 1003
    }
    return hr;
};

STDMETHODIMP VLCVideo::put_crop(BSTR geometry)
{
    if( NULL == geometry )
        return E_POINTER;

    if( 0 == SysStringLen(geometry) )
        return E_INVALIDARG;

JP Dinger's avatar
JP Dinger committed
1004
    libvlc_media_player_t *p_md;
1005
    HRESULT hr = getMD(&p_md);
1006 1007
    if( SUCCEEDED(hr) )
    {
JP Dinger's avatar
JP Dinger committed
1008
        char *psz_geometry = CStrFromBSTR(CP_UTF8, geometry);
1009
        if( !psz_geometry )
1010
        {
JP Dinger's avatar
JP Dinger committed
1011 1012
            return E_OUTOFMEMORY;
        }
1013

1014
        libvlc_video_set_crop_geometry(p_md, psz_geometry);
1015

JP Dinger's avatar
JP Dinger committed
1016
        CoTaskMemFree(psz_geometry);
1017 1018 1019 1020
    }
    return hr;
};

1021 1022 1023 1024 1025
STDMETHODIMP VLCVideo::get_teletext(long* page)
{
    if( NULL == page )
        return E_POINTER;

JP Dinger's avatar
JP Dinger committed
1026
    libvlc_media_player_t *p_md;
1027
    HRESULT hr = getMD(&p_md);
1028 1029
    if( SUCCEEDED(hr) )
    {
1030
        *page = libvlc_video_get_teletext(p_md);
1031
    }
1032

1033 1034 1035 1036 1037
    return hr;
};

STDMETHODIMP VLCVideo::put_teletext(long page)
{
JP Dinger's avatar
JP Dinger committed
1038
    libvlc_media_player_t *p_md;
1039
    HRESULT hr = getMD(&p_md);
1040

1041 1042
    if( SUCCEEDED(hr) )
    {
1043
        libvlc_video_set_teletext(p_md, page);
1044 1045 1046 1047
    }
    return hr;
};

1048
STDMETHODIMP VLCVideo::takeSnapshot(LPPICTUREDISP* picture)
1049
{
1050
    if( NULL == picture )
1051 1052
        return E_POINTER;

JP Dinger's avatar
JP Dinger committed
1053
    libvlc_media_player_t *p_md;
1054
    HRESULT hr = getMD(&p_md);
1055 1056
    if( SUCCEEDED(hr) )
    {
JP Dinger's avatar
JP Dinger committed
1057 1058
        static int uniqueId = 0;
        TCHAR path[MAX_PATH+1];
1059

JP Dinger's avatar
JP Dinger committed
1060 1061 1062
        int pathlen = GetTempPath(MAX_PATH-24, path);
        if( (0 == pathlen) || (pathlen > (MAX_PATH-24)) )
            return E_FAIL;
1063

JP Dinger's avatar
JP Dinger committed
1064 1065 1066 1067 1068 1069 1070
        /* check temp directory path by openning it */
        {
            HANDLE dirHandle = CreateFile(path, GENERIC_READ,
                       FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
                       NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
            if( INVALID_HANDLE_VALUE == dirHandle )
            {
1071
                Instance()->setErrorInfo(IID_IVLCVideo,
JP Dinger's avatar
JP Dinger committed
1072 1073 1074 1075
                        "Invalid temporary directory for snapshot images, check values of TMP, TEMP envars.");
                return E_FAIL;
            }
            else
1076
            {
JP Dinger's avatar
JP Dinger committed
1077 1078 1079 1080
                BY_HANDLE_FILE_INFORMATION bhfi;
                BOOL res = GetFileInformationByHandle(dirHandle, &bhfi);
                CloseHandle(dirHandle);
                if( !res || !(bhfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
1081
                {
1082
                    Instance()->setErrorInfo(IID_IVLCVideo,
1083 1084 1085
                            "Invalid temporary directory for snapshot images, check values of TMP, TEMP envars.");
                    return E_FAIL;
                }
1086
            }
JP Dinger's avatar
JP Dinger committed
1087
        }
1088

JP Dinger's avatar
JP Dinger committed
1089
        TCHAR filepath[MAX_PATH+1];
1090

JP Dinger's avatar
JP Dinger committed
1091 1092
        _stprintf(filepath, TEXT("%sAXVLC%lXS%lX.bmp"),
                 path, GetCurrentProcessId(), ++uniqueId);
1093 1094

#ifdef _UNICODE
JP Dinger's avatar
JP Dinger committed
1095 1096 1097
        /* reuse path storage for UTF8 string */
        char *psz_filepath = (char *)path;
        WCHAR* wpath = filepath;
1098
#else
JP Dinger's avatar
JP Dinger committed
1099 1100 1101
        char *psz_filepath = path;
        /* first convert to unicode using current code page */
        WCHAR wpath[MAX_PATH+1];
1102 1103
        if( 0 == MultiByteToWideChar(CP_ACP, 0, filepath, -1,
                                     wpath, sizeof(wpath)/sizeof(WCHAR)) )
JP Dinger's avatar
JP Dinger committed
1104
            return E_FAIL;
1105
#endif
JP Dinger's avatar
JP Dinger committed
1106
        /* convert to UTF8 */
1107 1108 1109 1110 1111
        pathlen = WideCharToMultiByte(CP_UTF8, 0, wpath, -1,
                                      psz_filepath, sizeof(path), NULL, NULL);
        // fail if path is 0 or too short (i.e pathlen is the same as
        // storage size)

JP Dinger's avatar
JP Dinger committed
1112 1113
        if( (0 == pathlen) || (sizeof(path) == pathlen) )
            return E_FAIL;
1114

JP Dinger's avatar
JP Dinger committed
1115
        /* take snapshot into file */
1116
        if( libvlc_video_take_snapshot(p_md, 0, psz_filepath, 0, 0) == 0 )
JP Dinger's avatar
JP Dinger committed
1117 1118
        {
            /* open snapshot file */
1119 1120
            HANDLE snapPic = LoadImage(NULL, filepath, IMAGE_BITMAP, 0, 0,
                                       LR_CREATEDIBSECTION|LR_LOADFROMFILE);
JP Dinger's avatar
JP Dinger committed
1121
            if( snapPic )
1122
            {
JP Dinger's avatar
JP Dinger committed
1123 1124 1125 1126 1127 1128 1129
                PICTDESC snapDesc;

                snapDesc.cbSizeofstruct = sizeof(PICTDESC);
                snapDesc.picType        = PICTYPE_BITMAP;
                snapDesc.bmp.hbitmap    = (HBITMAP)snapPic;
                snapDesc.bmp.hpal       = NULL;

1130 1131
                hr = OleCreatePictureIndirect(&snapDesc, IID_IPictureDisp,
                                              TRUE, (LPVOID*)picture);
JP Dinger's avatar
JP Dinger committed
1132
                if( FAILED(hr) )
1133
                {
JP Dinger's avatar
JP Dinger committed
1134 1135
                    *picture = NULL;
                    DeleteObject(snapPic);
1136
                }
1137
            }
JP Dinger's avatar
JP Dinger committed
1138
            DeleteFile(filepath);
1139 1140 1141 1142 1143
        }
    }
    return hr;
};

1144 1145
STDMETHODIMP VLCVideo::toggleFullscreen()
{
JP Dinger's avatar
JP Dinger committed
1146
    libvlc_media_player_t *p_md;
1147
    HRESULT hr = getMD(&p_md);
1148 1149
    if( SUCCEEDED(hr) )
    {
1150
        Instance()->toggleFullscreen();
1151 1152 1153 1154
    }
    return hr;
};

1155 1156
STDMETHODIMP VLCVideo::toggleTeletext()
{
JP Dinger's avatar
JP Dinger committed
1157
    libvlc_media_player_t *p_md;
1158
    HRESULT hr = getMD(&p_md);
1159 1160
    if( SUCCEEDED(hr) )
    {
1161
        libvlc_toggle_teletext(p_md);
1162 1163 1164 1165
    }
    return hr;
};

1166 1167
STDMETHODIMP VLCVideo::get_marquee(IVLCMarquee** obj)
{
1168 1169
    return object_get(obj,_p_vlcmarquee);
}
1170

1171 1172
STDMETHODIMP VLCVideo::get_logo(IVLCLogo** obj)
{
1173
    return object_get(obj,_p_vlclogo);
1174 1175
}

1176 1177 1178 1179 1180
STDMETHODIMP VLCVideo::get_deinterlace(IVLCDeinterlace** obj)
{
    return object_get(obj,_p_vlcdeint);
}

1181 1182 1183 1184 1185
/****************************************************************************/

HRESULT VLCLogo::do_put_int(unsigned idx, LONG val)
{
    libvlc_media_player_t *p_md;
1186
    HRESULT hr = getMD(&p_md);
1187 1188
    if( SUCCEEDED(hr) )
    {
1189
        libvlc_video_set_logo_int(p_md, idx, val);
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
    }
    return hr;
}

HRESULT VLCLogo::do_get_int(unsigned idx, LONG *val)
{
    if( NULL == val )
        return E_POINTER;

    libvlc_media_player_t *p_md;
1200
    HRESULT hr = getMD(&p_md);
1201 1202
    if( SUCCEEDED(hr) )
    {
1203
        *val = libvlc_video_get_logo_int(p_md, idx);
1204 1205 1206
    }
    return hr;
}
1207

1208 1209 1210
STDMETHODIMP VLCLogo::file(BSTR fname)
{
    libvlc_media_player_t *p_md;
1211
    HRESULT hr = getMD(&p_md);
1212 1213 1214 1215 1216 1217

    char *n = CStrFromBSTR(CP_UTF8, fname);
    if( !n ) hr = E_OUTOFMEMORY;

    if( SUCCEEDED(hr) )
    {
1218
        libvlc_video_set_logo_string(p_md, libvlc_logo_file, n);
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
    }

    CoTaskMemFree(n);
    return hr;
}

STDMETHODIMP VLCLogo::get_position(BSTR* val)
{
    if( NULL == val )
        return E_POINTER;

    LONG i;
    HRESULT hr = do_get_int(libvlc_logo_position, &i);

    if(SUCCEEDED(hr))
1234
        *val = BSTRFromCStr(CP_UTF8, position_bynumber(i));
1235 1236 1237

    return hr;
}
1238

1239 1240 1241 1242 1243
STDMETHODIMP VLCLogo::put_position(BSTR val)
{
    char *n = CStrFromBSTR(CP_UTF8, val);
    if( !n ) return E_OUTOFMEMORY;

1244 1245 1246 1247 1248
    size_t i;
    HRESULT hr;
    if( position_byname( n, i ) )
        hr = do_put_int(libvlc_logo_position,i);
    else
1249 1250 1251 1252 1253
        hr = E_INVALIDARG;

    CoTaskMemFree(n);
    return hr;
}
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
/****************************************************************************/

STDMETHODIMP VLCMediaDescription::get_meta(BSTR* val, libvlc_meta_t e_meta)
{
    if( NULL == val )
        return E_POINTER;

    *val = 0;

    libvlc_media_player_t *p_md;
    HRESULT hr = getMD(&p_md);
    if( SUCCEEDED(hr) )
    {
        libvlc_media_t * p_media = libvlc_media_player_get_media(p_md);
        const char* info = p_media ? libvlc_media_get_meta(p_media, e_meta) : 0;
        *val = info ? BSTRFromCStr(CP_UTF8, info) : 0;
        hr = *val ? S_OK : E_FAIL;
    }
    return hr;
}

STDMETHODIMP VLCMediaDescription::get_title(BSTR *val)
{
    return get_meta(val, libvlc_meta_Title);
}
1279

1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
STDMETHODIMP VLCMediaDescription::get_artist(BSTR *val)
{
    return get_meta(val, libvlc_meta_Artist);
}
STDMETHODIMP VLCMediaDescription::get_genre(BSTR *val)
{
    return get_meta(val, libvlc_meta_Genre);
}
STDMETHODIMP VLCMediaDescription::get_copyright(BSTR *val)
{
    return get_meta(val, libvlc_meta_Copyright);
}
STDMETHODIMP VLCMediaDescription::get_album(BSTR *val)
{
    return get_meta(val, libvlc_meta_Album);
}
STDMETHODIMP VLCMediaDescription::get_trackNumber(BSTR *val)
{
    return get_meta(val, libvlc_meta_TrackNumber);
}
STDMETHODIMP VLCMediaDescription::get_description(BSTR *val)
{
    return get_meta(val, libvlc_meta_Description);
}
STDMETHODIMP VLCMediaDescription::get_rating(BSTR *val)
{
    return get_meta(val, libvlc_meta_Rating);
}
STDMETHODIMP VLCMediaDescription::get_date(BSTR *val)
{
    return get_meta(val, libvlc_meta_Date);
}
STDMETHODIMP VLCMediaDescription::get_setting(BSTR *val)
{
    return get_meta(val, libvlc_meta_Setting);
}
STDMETHODIMP VLCMediaDescription::get_URL(BSTR *val)
{
    return get_meta(val, libvlc_meta_URL);
}
STDMETHODIMP VLCMediaDescription::get_language(BSTR *val)
{
    return get_meta(val, libvlc_meta_Language);
}
STDMETHODIMP VLCMediaDescription::get_nowPlaying(BSTR *val)
{
    return get_meta(val, libvlc_meta_NowPlaying);
}
STDMETHODIMP VLCMediaDescription::get_publisher(BSTR *val)
{
    return get_meta(val, libvlc_meta_Publisher);
}
STDMETHODIMP VLCMediaDescription::get_encodedBy(BSTR *val)
{
    return get_meta(val, libvlc_meta_EncodedBy);
}
STDMETHODIMP VLCMediaDescription::get_artworkURL(BSTR *val)
{
    return get_meta(val, libvlc_meta_ArtworkURL);
}
STDMETHODIMP VLCMediaDescription::get_trackID(BSTR *val)
{
    return get_meta(val, libvlc_meta_TrackID);
}
1344
/****************************************************************************/
1345 1346 1347 1348

VLCControl2::VLCControl2(VLCPlugin *p_instance) :
    _p_instance(p_instance),
    _p_typeinfo(NULL),
1349 1350 1351 1352
    _p_vlcaudio(new VLCAudio(p_instance)),
    _p_vlcinput(new VLCInput(p_instance)),
    _p_vlcplaylist(new VLCPlaylist(p_instance)),
    _p_vlcsubtitle(new VLCSubtitle(p_instance)),
1353 1354
    _p_vlcvideo(new VLCVideo(p_instance)),
    _p_vlcmedia_desc(p_instance)
1355 1356
{
}
1357 1358 1359

VLCControl2::~VLCControl2()
{
1360 1361 1362 1363 1364
    delete static_cast<VLCVideo*>(_p_vlcvideo);
    delete static_cast<VLCSubtitle*>(_p_vlcsubtitle);
    delete static_cast<VLCPlaylist*>(_p_vlcplaylist);
    delete static_cast<VLCInput*>(_p_vlcinput);
    delete static_cast<VLCAudio*>(_p_vlcaudio);
1365 1366 1367 1368
    if( _p_typeinfo )
        _p_typeinfo->Release();
};

1369
HRESULT VLCControl2::loadTypeInfo(void)
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
{
    HRESULT hr = NOERROR;
    if( NULL == _p_typeinfo )
    {
        ITypeLib *p_typelib;

        hr = _p_instance->getTypeLib(LOCALE_USER_DEFAULT, &p_typelib);
        if( SUCCEEDED(hr) )
        {
            hr = p_typelib->GetTypeInfoOfGuid(IID_IVLCControl2, &_p_typeinfo);
            if( FAILED(hr) )
            {
                _p_typeinfo = NULL;
            }
            p_typelib->Release();
        }
    }
    return hr;
};

STDMETHODIMP VLCControl2::GetTypeInfoCount(UINT* pctInfo)
{
    if( NULL == pctInfo )
        return E_INVALIDARG;

1395
    if( SUCCEEDED(loadTypeInfo()) )
1396 1397 1398 1399 1400 1401 1402
        *pctInfo = 1;
    else
        *pctInfo = 0;

    return NOERROR;
};

Jean-Baptiste Kempf's avatar