vlccontrol2.cpp 34.8 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 131 132
    *track = Instance()->get_player().get_track();

    return S_OK;
133 134 135 136
};

STDMETHODIMP VLCAudio::put_track(long track)
{
137 138 139
    Instance()->get_player().set_track(track);

    return S_OK;
140 141
};

142 143 144 145 146
STDMETHODIMP VLCAudio::get_count(long* trackNumber)
{
    if( NULL == trackNumber )
        return E_POINTER;

147
    *trackNumber = Instance()->get_player().track_count();
148

149 150
    return S_OK;
};
151 152 153 154 155 156 157 158

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

    libvlc_media_player_t* p_md;

159
    HRESULT hr = getMD(&p_md);
160 161 162 163 164 165 166
    if( SUCCEEDED(hr) )
    {
        int i, i_limit;
        const char *psz_name;
        libvlc_track_description_t *p_trackDesc;

        // get tracks description
167 168 169
        p_trackDesc = libvlc_audio_get_track_description(p_md);
        if( !p_trackDesc )
            return E_FAIL;
170 171

        //get the number of available track
172 173 174
        i_limit = libvlc_audio_get_track_count(p_md);
        if( i_limit < 0 )
            return E_FAIL;
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

        // check if the number given is a good one
        if ( ( trackID > ( i_limit -1 ) ) || ( trackID < 0 ) )
                return E_FAIL;

        // 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;

        // 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;
};

200
STDMETHODIMP VLCAudio::get_channel(long *channel)
201 202 203 204
{
    if( NULL == channel )
        return E_POINTER;

205 206 207
    *channel = Instance()->get_player().get_channel();

    return S_OK;
208 209
};

210
STDMETHODIMP VLCAudio::put_channel(long channel)
211
{
212 213 214
    Instance()->get_player().set_channel(channel);

    return S_OK;
215 216
};

217 218
STDMETHODIMP VLCAudio::toggleMute()
{
219 220 221
    Instance()->get_player().toggle_mute();

    return S_OK;
222 223
};

224
/****************************************************************************/
225

226 227 228 229 230 231
STDMETHODIMP VLCDeinterlace::disable()
{
    libvlc_media_player_t *p_md;
    HRESULT hr = getMD(&p_md);
    if( SUCCEEDED(hr) )
    {
232
        libvlc_video_set_deinterlace(p_md, "");
233 234 235 236 237 238 239 240 241 242 243
    }
    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);
244
        libvlc_video_set_deinterlace(p_md, psz_mode);
245 246 247 248 249 250 251
        CoTaskMemFree(psz_mode);
    }
    return hr;
}

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

252
STDMETHODIMP VLCInput::get_length(double* length)
253 254 255 256
{
    if( NULL == length )
        return E_POINTER;

257 258 259
    *length = static_cast<double>(Instance()->get_player().get_length());

    return S_OK;
260 261
};

262
STDMETHODIMP VLCInput::get_position(double* position)
263 264 265 266
{
    if( NULL == position )
        return E_POINTER;

267 268 269
    *position = Instance()->get_player().get_position();

    return S_OK;
270 271
};

272
STDMETHODIMP VLCInput::put_position(double position)
273
{
274 275 276
    Instance()->get_player().set_position( static_cast<float>(position) );

    return S_OK;
277 278
};

279
STDMETHODIMP VLCInput::get_time(double* time)
280 281 282 283
{
    if( NULL == time )
        return E_POINTER;

284 285 286
    *time = static_cast<double>(Instance()->get_player().get_time());

    return S_OK;
287 288
};

289
STDMETHODIMP VLCInput::put_time(double time)
290
{
291 292 293
    Instance()->get_player().set_time(static_cast<libvlc_time_t>(time));

    return S_OK;
294 295
};

296
STDMETHODIMP VLCInput::get_state(long* state)
297 298 299 300
{
    if( NULL == state )
        return E_POINTER;

301 302 303
    *state = Instance()->get_player().get_state();

    return S_OK;
304 305
};

306
STDMETHODIMP VLCInput::get_rate(double* rate)
307 308 309 310
{
    if( NULL == rate )
        return E_POINTER;

311 312 313
    *rate = Instance()->get_player().get_rate();

    return S_OK;
314 315
};

316
STDMETHODIMP VLCInput::put_rate(double rate)
317
{
318 319 320
    Instance()->get_player().set_rate(static_cast<float>(rate));

    return S_OK;
321 322
};

323
STDMETHODIMP VLCInput::get_fps(double* fps)
324 325 326 327
{
    if( NULL == fps )
        return E_POINTER;

328 329 330
    *fps = static_cast<double>(Instance()->get_player().get_fps());

    return S_OK;
331 332 333 334 335 336 337
};

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

338 339 340
    *hasVout = varbool( Instance()->get_player().has_vout() );

    return S_OK;
341 342
};

343
/****************************************************************************/
344

345
HRESULT VLCMarquee::do_put_int(unsigned idx, LONG val)
346 347
{
    libvlc_media_player_t *p_md;
348
    HRESULT hr = getMD(&p_md);
349 350
    if( SUCCEEDED(hr) )
    {
351
        libvlc_video_set_marquee_int(p_md, idx, val);
352 353
    }
    return hr;
354
}
355

356
HRESULT VLCMarquee::do_get_int(unsigned idx, LONG *val)
357
{
358 359
    if( NULL == val )
        return E_POINTER;
360 361

    libvlc_media_player_t *p_md;
362
    HRESULT hr = getMD(&p_md);
363 364
    if( SUCCEEDED(hr) )
    {
365
        *val = libvlc_video_get_marquee_int(p_md, idx);
366 367
    }
    return hr;
368
}
369

370
STDMETHODIMP VLCMarquee::get_position(BSTR* val)
371
{
372 373
    if( NULL == val )
        return E_POINTER;
374

375 376 377 378
    LONG i;
    HRESULT hr = do_get_int(libvlc_marquee_Position, &i);
    if(SUCCEEDED(hr))
        *val = BSTRFromCStr(CP_UTF8, position_bynumber(i));
379 380

    return hr;
381
}
382

383
STDMETHODIMP VLCMarquee::put_position(BSTR val)
384
{
385 386
    char *n = CStrFromBSTR(CP_UTF8, val);
    if( !n ) return E_OUTOFMEMORY;
387

388 389 390 391 392 393
    size_t i;
    HRESULT hr;
    if( position_byname( n, i ) )
        hr = do_put_int(libvlc_marquee_Position,i);
    else
        hr = E_INVALIDARG;
394

395
    CoTaskMemFree(n);
396
    return hr;
397
}
398

399
STDMETHODIMP VLCMarquee::get_text(BSTR *val)
400
{
401 402 403
    char *psz;
    if( NULL == val )
        return E_POINTER;
404 405

    libvlc_media_player_t *p_md;
406
    HRESULT hr = getMD(&p_md);
407 408
    if( SUCCEEDED(hr) )
    {
409 410
        psz = libvlc_video_get_marquee_string(p_md, libvlc_marquee_Text);
        if( psz )
411
            *val = BSTRFromCStr(CP_UTF8, psz);
412 413
    }
    return hr;
414
}
415

416
STDMETHODIMP VLCMarquee::put_text(BSTR val)
417 418
{
    libvlc_media_player_t *p_md;
419
    HRESULT hr = getMD(&p_md);
420 421
    if( SUCCEEDED(hr) )
    {
422
        char *psz_text = CStrFromBSTR(CP_UTF8, val);
423
        libvlc_video_set_marquee_string(p_md, libvlc_marquee_Text, psz_text);
424
        CoTaskMemFree(psz_text);
425 426
    }
    return hr;
427
}
428

429
/****************************************************************************/
430

431 432 433 434 435
STDMETHODIMP VLCPlaylistItems::get_count(long* count)
{
    if( NULL == count )
        return E_POINTER;

436
    *count = Instance()->get_player().items_count();
437
    return S_OK;
438 439 440 441
};

STDMETHODIMP VLCPlaylistItems::clear()
{
442
    Instance()->get_player().clear_items();
443
    return S_OK;
444 445 446 447
};

STDMETHODIMP VLCPlaylistItems::remove(long item)
{
448 449
    Instance()->get_player().delete_item(item);
    return S_OK;
450 451
};

452
/****************************************************************************/
453

454
STDMETHODIMP VLCPlaylist::get_itemCount(long* count)
455 456 457 458
{
    if( NULL == count )
        return E_POINTER;

459 460
    *count = Instance()->get_player().items_count();

461
    return S_OK;
462 463 464 465 466 467 468
};

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

469 470 471
    *isPlaying = varbool( Instance()->get_player().is_playing()!=0 );

    return S_OK;
472 473
};

474 475 476 477 478 479 480 481 482 483
STDMETHODIMP VLCPlaylist::get_currentItem(long* index)
{
    if( NULL == index )
        return E_POINTER;

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

    return S_OK;
};

484
STDMETHODIMP VLCPlaylist::add(BSTR uri, VARIANT name, VARIANT options, long* item)
485 486 487 488 489 490 491 492
{
    if( NULL == item )
        return E_POINTER;

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

    libvlc_instance_t* p_libvlc;
493
    HRESULT hr = getVLC(&p_libvlc);
494 495
    if( SUCCEEDED(hr) )
    {
496
        char *psz_uri = NULL;
497
        if( SysStringLen(Instance()->getBaseURL()) > 0 )
498 499 500 501
        {
            /*
            ** if the MRL a relative URL, we should end up with an absolute URL
            */
502
            LPWSTR abs_url = CombineURL(Instance()->getBaseURL(), uri);
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
            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;
        }

526 527 528 529 530 531
        int i_options;
        char **ppsz_options;

        hr = VLCControl::CreateTargetOptions(CP_UTF8, &options, &ppsz_options, &i_options);
        if( FAILED(hr) )
        {
532 533
            CoTaskMemFree(psz_uri);
            return hr;
534 535 536
        }

        char *psz_name = NULL;
537 538 539
        VARIANT v_name;
        VariantInit(&v_name);
        if( SUCCEEDED(VariantChangeType(&v_name, &name, 0, VT_BSTR)) )
540
        {
541 542 543 544
            if( SysStringLen(V_BSTR(&v_name)) > 0 )
                psz_name = CStrFromBSTR(CP_UTF8, V_BSTR(&v_name));

            VariantClear(&v_name);
545 546
        }

547
        *item = Instance()->playlist_add_extended_untrusted(psz_uri,
548
                    i_options, const_cast<const char **>(ppsz_options));
549 550 551

        VLCControl::FreeTargetOptions(ppsz_options, i_options);
        CoTaskMemFree(psz_uri);
552
        if( psz_name ) /* XXX Do we even need to check? */
553 554 555 556 557 558 559
            CoTaskMemFree(psz_name);
    }
    return hr;
};

STDMETHODIMP VLCPlaylist::play()
{
560
    Instance()->get_player().play();
561
    return S_OK;
562 563
};

564
STDMETHODIMP VLCPlaylist::playItem(long item)
565
{
566
    Instance()->get_player().play(item);
567
    return S_OK;
568 569
};

570
STDMETHODIMP VLCPlaylist::pause()
571
{
572 573
    Instance()->get_player().pause();
    return S_OK;
574 575
};

576 577 578 579 580 581
STDMETHODIMP VLCPlaylist::togglePause()
{
    Instance()->get_player().togglePause();
    return S_OK;
};

582 583
STDMETHODIMP VLCPlaylist::stop()
{
584 585
    Instance()->get_player().stop();
    return S_OK;
586 587 588 589
};

STDMETHODIMP VLCPlaylist::next()
{
590
    Instance()->get_player().next();
591
    return S_OK;
592 593 594 595
};

STDMETHODIMP VLCPlaylist::prev()
{
596
    Instance()->get_player().prev();
597
    return S_OK;
598 599 600 601
};

STDMETHODIMP VLCPlaylist::clear()
{
602
    Instance()->get_player().clear_items();
603
    return S_OK;
604 605
};

606
STDMETHODIMP VLCPlaylist::removeItem(long item)
607
{
608 609
    Instance()->get_player().delete_item(item);
    return S_OK;
610 611
};

612 613 614 615 616 617 618 619 620 621 622 623 624 625
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;
};

626
/****************************************************************************/
627

628 629 630 631 632 633
STDMETHODIMP VLCSubtitle::get_track(long* spu)
{
    if( NULL == spu )
        return E_POINTER;

    libvlc_media_player_t *p_md;
634
    HRESULT hr = getMD(&p_md);
635 636
    if( SUCCEEDED(hr) )
    {
637
        *spu = libvlc_video_get_spu(p_md);
638 639 640 641 642 643 644
    }
    return hr;
};

STDMETHODIMP VLCSubtitle::put_track(long spu)
{
    libvlc_media_player_t *p_md;
645
    HRESULT hr = getMD(&p_md);
646 647
    if( SUCCEEDED(hr) )
    {
648
        libvlc_video_set_spu(p_md, spu);
649 650 651 652 653 654 655 656 657 658
    }
    return hr;
};

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

    libvlc_media_player_t *p_md;
659
    HRESULT hr = getMD(&p_md);
660 661 662
    if( SUCCEEDED(hr) )
    {
        // get the number of video subtitle available and return it
663
        *spuNumber = libvlc_video_get_spu_count(p_md);
664 665 666 667 668 669 670 671 672 673 674 675
    }
    return hr;
};


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

    libvlc_media_player_t* p_md;

676
    HRESULT hr = getMD(&p_md);
677 678 679 680 681 682 683
    if( SUCCEEDED(hr) )
    {
        int i, i_limit;
        const char *psz_name;
        libvlc_track_description_t *p_spuDesc;

        // get subtitles description
684 685 686
        p_spuDesc = libvlc_video_get_spu_description(p_md);
        if( !p_spuDesc )
            return E_FAIL;
687 688

        // get the number of available subtitle
689 690 691
        i_limit = libvlc_video_get_spu_count(p_md);
        if( i_limit < 0 )
            return E_FAIL;
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716

        // check if the number given is a good one
        if ( ( nameID > ( i_limit -1 ) ) || ( nameID < 0 ) )
            return E_FAIL;

        // 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;

        // 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;
};

717
/****************************************************************************/
718

719 720 721 722 723
STDMETHODIMP VLCVideo::get_fullscreen(VARIANT_BOOL* fullscreen)
{
    if( NULL == fullscreen )
        return E_POINTER;

JP Dinger's avatar
JP Dinger committed
724
    libvlc_media_player_t *p_md;
725
    HRESULT hr = getMD(&p_md);
726 727
    if( SUCCEEDED(hr) )
    {
728
        *fullscreen = varbool( libvlc_get_fullscreen(p_md) );
729 730 731 732 733 734
    }
    return hr;
};

STDMETHODIMP VLCVideo::put_fullscreen(VARIANT_BOOL fullscreen)
{
JP Dinger's avatar
JP Dinger committed
735
    libvlc_media_player_t *p_md;
736
    HRESULT hr = getMD(&p_md);
737 738
    if( SUCCEEDED(hr) )
    {
739
        libvlc_set_fullscreen(p_md, VARIANT_FALSE != fullscreen);
740 741 742 743
    }
    return hr;
};

744
STDMETHODIMP VLCVideo::get_width(long* width)
745 746 747 748
{
    if( NULL == width )
        return E_POINTER;

JP Dinger's avatar
JP Dinger committed
749
    libvlc_media_player_t *p_md;
750
    HRESULT hr = getMD(&p_md);
751 752
    if( SUCCEEDED(hr) )
    {
753
        *width = libvlc_video_get_width(p_md);
754 755 756 757
    }
    return hr;
};

758
STDMETHODIMP VLCVideo::get_height(long* height)
759 760 761 762
{
    if( NULL == height )
        return E_POINTER;

JP Dinger's avatar
JP Dinger committed
763
    libvlc_media_player_t *p_md;
764
    HRESULT hr = getMD(&p_md);
765 766
    if( SUCCEEDED(hr) )
    {
767
        *height = libvlc_video_get_height(p_md);
768 769 770 771
    }
    return hr;
};

772
STDMETHODIMP VLCVideo::get_aspectRatio(BSTR* aspect)
773
{
774
    if( NULL == aspect )
775 776
        return E_POINTER;

JP Dinger's avatar
JP Dinger committed
777
    libvlc_media_player_t *p_md;
778
    HRESULT hr = getMD(&p_md);
779 780
    if( SUCCEEDED(hr) )
    {
781
        char *psz_aspect = libvlc_video_get_aspect_ratio(p_md);
782

783
        if( !psz_aspect )
784
        {
JP Dinger's avatar
JP Dinger committed
785
            *aspect = BSTRFromCStr(CP_UTF8, psz_aspect);
786 787
            if( NULL == *aspect )
                hr = E_OUTOFMEMORY;
788 789
        } else if( NULL == psz_aspect)
                hr = E_OUTOFMEMORY;
JP Dinger's avatar
JP Dinger committed
790
        free( psz_aspect );
791 792 793 794 795 796 797 798 799
    }
    return hr;
};

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

JP Dinger's avatar
JP Dinger committed
800
    libvlc_media_player_t *p_md;
801
    HRESULT hr = getMD(&p_md);
802 803
    if( SUCCEEDED(hr) )
    {
JP Dinger's avatar
JP Dinger committed
804
        char *psz_aspect = CStrFromBSTR(CP_UTF8, aspect);
805
        if( !psz_aspect )
806
        {
JP Dinger's avatar
JP Dinger committed
807 808
            return E_OUTOFMEMORY;
        }
809

810
        libvlc_video_set_aspect_ratio(p_md, psz_aspect);
811

JP Dinger's avatar
JP Dinger committed
812
        CoTaskMemFree(psz_aspect);
813 814 815 816
    }
    return hr;
};

817 818 819 820 821
STDMETHODIMP VLCVideo::get_subtitle(long* spu)
{
    if( NULL == spu )
        return E_POINTER;

JP Dinger's avatar
JP Dinger committed
822
    libvlc_media_player_t *p_md;
823
    HRESULT hr = getMD(&p_md);
824 825
    if( SUCCEEDED(hr) )
    {
826
        *spu = libvlc_video_get_spu(p_md);
827 828 829 830 831 832
    }
    return hr;
};

STDMETHODIMP VLCVideo::put_subtitle(long spu)
{
JP Dinger's avatar
JP Dinger committed
833
    libvlc_media_player_t *p_md;
834
    HRESULT hr = getMD(&p_md);
835 836
    if( SUCCEEDED(hr) )
    {
837
        libvlc_video_set_spu(p_md, spu);
838 839 840 841
    }
    return hr;
};

842 843 844 845 846
STDMETHODIMP VLCVideo::get_crop(BSTR* geometry)
{
    if( NULL == geometry )
        return E_POINTER;

JP Dinger's avatar
JP Dinger committed
847
    libvlc_media_player_t *p_md;
848
    HRESULT hr = getMD(&p_md);
849 850
    if( SUCCEEDED(hr) )
    {
851 852
        char *psz_geometry = libvlc_video_get_crop_geometry(p_md);
        if( !psz_geometry )
853
        {
JP Dinger's avatar
JP Dinger committed
854
            *geometry = BSTRFromCStr(CP_UTF8, psz_geometry);
855 856 857 858
            if( !geometry )
                hr = E_OUTOFMEMORY;
        } else if( !psz_geometry )
                hr = E_OUTOFMEMORY;
JP Dinger's avatar
JP Dinger committed
859
        free( psz_geometry );
860 861 862 863 864 865 866 867 868 869 870 871
    }
    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
872
    libvlc_media_player_t *p_md;
873
    HRESULT hr = getMD(&p_md);
874 875
    if( SUCCEEDED(hr) )
    {
JP Dinger's avatar
JP Dinger committed
876
        char *psz_geometry = CStrFromBSTR(CP_UTF8, geometry);
877
        if( !psz_geometry )
878
        {
JP Dinger's avatar
JP Dinger committed
879 880
            return E_OUTOFMEMORY;
        }
881

882
        libvlc_video_set_crop_geometry(p_md, psz_geometry);
883

JP Dinger's avatar
JP Dinger committed
884
        CoTaskMemFree(psz_geometry);
885 886 887 888
    }
    return hr;
};

889 890 891 892 893
STDMETHODIMP VLCVideo::get_teletext(long* page)
{
    if( NULL == page )
        return E_POINTER;

JP Dinger's avatar
JP Dinger committed
894
    libvlc_media_player_t *p_md;
895
    HRESULT hr = getMD(&p_md);
896 897
    if( SUCCEEDED(hr) )
    {
898
        *page = libvlc_video_get_teletext(p_md);
899
    }
900

901 902 903 904 905
    return hr;
};

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

909 910
    if( SUCCEEDED(hr) )
    {
911
        libvlc_video_set_teletext(p_md, page);
912 913 914 915
    }
    return hr;
};

916
STDMETHODIMP VLCVideo::takeSnapshot(LPPICTUREDISP* picture)
917
{
918
    if( NULL == picture )
919 920
        return E_POINTER;

JP Dinger's avatar
JP Dinger committed
921
    libvlc_media_player_t *p_md;
922
    HRESULT hr = getMD(&p_md);
923 924
    if( SUCCEEDED(hr) )
    {
JP Dinger's avatar
JP Dinger committed
925 926
        static int uniqueId = 0;
        TCHAR path[MAX_PATH+1];
927

JP Dinger's avatar
JP Dinger committed
928 929 930
        int pathlen = GetTempPath(MAX_PATH-24, path);
        if( (0 == pathlen) || (pathlen > (MAX_PATH-24)) )
            return E_FAIL;
931

JP Dinger's avatar
JP Dinger committed
932 933 934 935 936 937 938
        /* 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 )
            {
939
                Instance()->setErrorInfo(IID_IVLCVideo,
JP Dinger's avatar
JP Dinger committed
940 941 942 943
                        "Invalid temporary directory for snapshot images, check values of TMP, TEMP envars.");
                return E_FAIL;
            }
            else
944
            {
JP Dinger's avatar
JP Dinger committed
945 946 947 948
                BY_HANDLE_FILE_INFORMATION bhfi;
                BOOL res = GetFileInformationByHandle(dirHandle, &bhfi);
                CloseHandle(dirHandle);
                if( !res || !(bhfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
949
                {
950
                    Instance()->setErrorInfo(IID_IVLCVideo,
951 952 953
                            "Invalid temporary directory for snapshot images, check values of TMP, TEMP envars.");
                    return E_FAIL;
                }
954
            }
JP Dinger's avatar
JP Dinger committed
955
        }
956

JP Dinger's avatar
JP Dinger committed
957
        TCHAR filepath[MAX_PATH+1];
958

JP Dinger's avatar
JP Dinger committed
959 960
        _stprintf(filepath, TEXT("%sAXVLC%lXS%lX.bmp"),
                 path, GetCurrentProcessId(), ++uniqueId);
961 962

#ifdef _UNICODE
JP Dinger's avatar
JP Dinger committed
963 964 965
        /* reuse path storage for UTF8 string */
        char *psz_filepath = (char *)path;
        WCHAR* wpath = filepath;
966
#else
JP Dinger's avatar
JP Dinger committed
967 968 969
        char *psz_filepath = path;
        /* first convert to unicode using current code page */
        WCHAR wpath[MAX_PATH+1];
970 971
        if( 0 == MultiByteToWideChar(CP_ACP, 0, filepath, -1,
                                     wpath, sizeof(wpath)/sizeof(WCHAR)) )
JP Dinger's avatar
JP Dinger committed
972
            return E_FAIL;
973
#endif
JP Dinger's avatar
JP Dinger committed
974
        /* convert to UTF8 */
975 976 977 978 979
        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
980 981
        if( (0 == pathlen) || (sizeof(path) == pathlen) )
            return E_FAIL;
982

JP Dinger's avatar
JP Dinger committed
983
        /* take snapshot into file */
984
        if( libvlc_video_take_snapshot(p_md, 0, psz_filepath, 0, 0) == 0 )
JP Dinger's avatar
JP Dinger committed
985 986
        {
            /* open snapshot file */
987 988
            HANDLE snapPic = LoadImage(NULL, filepath, IMAGE_BITMAP, 0, 0,
                                       LR_CREATEDIBSECTION|LR_LOADFROMFILE);
JP Dinger's avatar
JP Dinger committed
989
            if( snapPic )
990
            {
JP Dinger's avatar
JP Dinger committed
991 992 993 994 995 996 997
                PICTDESC snapDesc;

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

998 999
                hr = OleCreatePictureIndirect(&snapDesc, IID_IPictureDisp,
                                              TRUE, (LPVOID*)picture);
JP Dinger's avatar
JP Dinger committed
1000
                if( FAILED(hr) )
1001
                {
JP Dinger's avatar
JP Dinger committed
1002 1003
                    *picture = NULL;
                    DeleteObject(snapPic);
1004
                }
1005
            }
JP Dinger's avatar
JP Dinger committed
1006
            DeleteFile(filepath);
1007 1008 1009 1010 1011
        }
    }
    return hr;
};

1012 1013
STDMETHODIMP VLCVideo::toggleFullscreen()
{
JP Dinger's avatar
JP Dinger committed
1014
    libvlc_media_player_t *p_md;
1015
    HRESULT hr = getMD(&p_md);
1016 1017
    if( SUCCEEDED(hr) )
    {
1018
        Instance()->toggleFullscreen();
1019 1020 1021 1022
    }
    return hr;
};

1023 1024
STDMETHODIMP VLCVideo::toggleTeletext()
{
JP Dinger's avatar
JP Dinger committed
1025
    libvlc_media_player_t *p_md;
1026
    HRESULT hr = getMD(&p_md);
1027 1028
    if( SUCCEEDED(hr) )
    {
1029
        libvlc_toggle_teletext(p_md);
1030 1031 1032 1033
    }
    return hr;
};

1034 1035
STDMETHODIMP VLCVideo::get_marquee(IVLCMarquee** obj)
{
1036 1037
    return object_get(obj,_p_vlcmarquee);
}
1038

1039 1040
STDMETHODIMP VLCVideo::get_logo(IVLCLogo** obj)
{
1041
    return object_get(obj,_p_vlclogo);
1042 1043
}

1044 1045 1046 1047 1048
STDMETHODIMP VLCVideo::get_deinterlace(IVLCDeinterlace** obj)
{
    return object_get(obj,_p_vlcdeint);
}

1049 1050 1051 1052 1053
/****************************************************************************/

HRESULT VLCLogo::do_put_int(unsigned idx, LONG val)
{
    libvlc_media_player_t *p_md;
1054
    HRESULT hr = getMD(&p_md);
1055 1056
    if( SUCCEEDED(hr) )
    {
1057
        libvlc_video_set_logo_int(p_md, idx, val);
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
    }
    return hr;
}

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

    libvlc_media_player_t *p_md;
1068
    HRESULT hr = getMD(&p_md);
1069 1070
    if( SUCCEEDED(hr) )
    {
1071
        *val = libvlc_video_get_logo_int(p_md, idx);
1072 1073 1074
    }
    return hr;
}
1075

1076 1077 1078
STDMETHODIMP VLCLogo::file(BSTR fname)
{
    libvlc_media_player_t *p_md;
1079
    HRESULT hr = getMD(&p_md);
1080 1081 1082 1083 1084 1085

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

    if( SUCCEEDED(hr) )
    {
1086
        libvlc_video_set_logo_string(p_md, libvlc_logo_file, n);
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
    }

    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))
1102
        *val = BSTRFromCStr(CP_UTF8, position_bynumber(i));
1103 1104 1105

    return hr;
}
1106

1107 1108 1109 1110 1111
STDMETHODIMP VLCLogo::put_position(BSTR val)
{
    char *n = CStrFromBSTR(CP_UTF8, val);
    if( !n ) return E_OUTOFMEMORY;

1112 1113 1114 1115 1116
    size_t i;
    HRESULT hr;
    if( position_byname( n, i ) )
        hr = do_put_int(libvlc_logo_position,i);
    else
1117 1118 1119 1120 1121
        hr = E_INVALIDARG;

    CoTaskMemFree(n);
    return hr;
}
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
/****************************************************************************/

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);
}
1147

1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
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);
}
1212
/****************************************************************************/
1213 1214 1215 1216

VLCControl2::VLCControl2(VLCPlugin *p_instance) :
    _p_instance(p_instance),
    _p_typeinfo(NULL),
1217 1218 1219 1220
    _p_vlcaudio(new VLCAudio(p_instance)),
    _p_vlcinput(new VLCInput(p_instance)),
    _p_vlcplaylist(new VLCPlaylist(p_instance)),
    _p_vlcsubtitle(new VLCSubtitle(p_instance)),
1221 1222
    _p_vlcvideo(new VLCVideo(p_instance)),
    _p_vlcmedia_desc(p_instance)
1223 1224
{
}
1225 1226 1227

VLCControl2::~VLCControl2()
{
1228 1229 1230 1231 1232
    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);
1233 1234 1235 1236
    if( _p_typeinfo )
        _p_typeinfo->Release();
};

1237
HRESULT VLCControl2::loadTypeInfo(void)
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
{
    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;

1263
    if( SUCCEEDED(loadTypeInfo()) )
1264 1265 1266 1267 1268 1269 1270
        *pctInfo = 1;
    else
        *pctInfo = 0;

    return NOERROR;
};

1271
STDMETHODIMP VLCControl2::GetTypeInfo(UINT, LCID, LPTYPEINFO* ppTInfo)
1272 1273 1274 1275
{
    if( NULL == ppTInfo )
        return E_INVALIDARG;

1276
    if( SUCCEEDED(loadTypeInfo()) )
1277 1278 1279 1280 1281 1282 1283 1284 1285
    {
        _p_typeinfo->AddRef();
        *ppTInfo = _p_typeinfo;
        return NOERROR;
    }
    *ppTInfo = NULL;
    return E_NOTIMPL;
};

1286 1287
STDMETHODIMP VLCControl2::GetIDsOfNames(REFIID, LPOLESTR* rgszNames,
        UINT cNames, LCID, DISPID* rgDispID)
1288
{
1289
    if( SUCCEEDED(loadTypeInfo()) )
1290 1291 1292 1293 1294 1295
    {
        return DispGetIDsOfNames(_p_typeinfo, rgszNames, cNames, rgDispID);
    }
    return E_NOTIMPL;
};

1296 1297
STDMETHODIMP VLCControl2::Invoke(DISPID dispIdMember, REFIID,
        LCID, WORD wFlags, DISPPARAMS* pDispParams,
1298 1299
        VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)
{
1300
    if( SUCCEEDED(loadTypeInfo()) )
1301 1302 1303 1304 1305 1306 1307
    {
        return DispInvoke(this, _p_typeinfo, dispIdMember, wFlags, pDispParams,
                pVarResult, pExcepInfo, puArgErr);
    }
    return E_NOTIMPL;
};

1308 1309 1310 1311 1312
STDMETHODIMP VLCControl2::get_AutoLoop(VARIANT_BOOL *autoloop)
{
    if( NULL == autoloop )
        return E_POINTER;

1313
    *autoloop = varbool( _p_instance->getAutoLoop() );
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
    return S_OK;
};

STDMETHODIMP VLCControl2::put_AutoLoop(VARIANT_BOOL autoloop)
{
    _p_instance->setAutoLoop((VARIANT_FALSE != autoloop) ? TRUE: FALSE);
    return S_OK;
};

STDMETHODIMP VLCControl2::get_AutoPlay(VARIANT_BOOL *autoplay)
{
    if( NULL == autoplay )
        return E_POINTER;

1328
    *autoplay = varbool( _p_instance->getAutoPlay() );
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
    return S_OK;
};

STDMETHODIMP VLCControl2::put_AutoPlay(VARIANT_BOOL autoplay)
{
    _p_instance->setAutoPlay((VARIANT_FALSE != autoplay) ? TRUE: FALSE);
    return S_OK;
};

STDMETHODIMP VLCControl2::get_BaseURL(BSTR *url)
{
    if( NULL == url )
        return E_POINTER;

    *url = SysAllocStringLen(_p_instance->getBaseURL(),
                SysStringLen(_p_instance->getBaseURL()));
    return NOERROR;
};

STDMETHODIMP VLCControl2::put_BaseURL(BSTR mrl)
{
    _p_instance->setBaseURL(mrl);

    return S_OK;
};

STDMETHODIMP VLCControl2::get_MRL(BSTR *mrl)
{
    if( NULL == mrl )
        return E_POINTER;

    *mrl = SysAllocStringLen(_p_instance->getMRL(),
                SysStringLen(_p_instance->getMRL()));
    return NOERROR;
};

STDMETHODIMP VLCControl2::put_MRL(BSTR mrl)
{
    _p_instance->setMRL(mrl);

    return S_OK;
};

1372 1373 1374 1375 1376 1377

STDMETHODIMP VLCControl2::get_Toolbar(VARIANT_BOOL *visible)
{
    if( NULL == visible )
        return E_POINTER;

1378
    *visible = varbool( _p_instance->getShowToolbar() != FALSE );
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388

    return S_OK;
};

STDMETHODIMP VLCControl2::put_Toolbar(VARIANT_BOOL visible)
{
    _p_instance->setShowToolbar((VARIANT_FALSE != visible) ? TRUE: FALSE);
    return S_OK;
};

1389 1390 1391 1392 1393
STDMETHODIMP VLCControl2::get_FullscreenEnabled(VARIANT_BOOL* enabled)
{
    if( NULL == enabled )
        return E_POINTER;

1394
    *enabled = varbool( _p_instance->get_options().get_enable_fs() );
1395 1396