vlccontrol2.cpp 44.5 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 25

#ifdef HAVE_CONFIG_H
26
#include "config.h"
27
#endif
28

29
#include <stdio.h>
30 31
#include <shlwapi.h>
#include <wininet.h>
32
#include <tchar.h>
33

34 35 36 37
#include "utils.h"
#include "plugin.h"
#include "vlccontrol2.h"

38
#include "../common/position.h"
39

40 41 42
// ---------

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

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

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

60
BIND_INTERFACE( VLCAudio )
61 62
BIND_INTERFACE( VLCTitle )
BIND_INTERFACE( VLCChapter )
63 64 65
BIND_INTERFACE( VLCInput )
BIND_INTERFACE( VLCMarquee )
BIND_INTERFACE( VLCLogo )
66
BIND_INTERFACE( VLCDeinterlace )
67 68 69 70
BIND_INTERFACE( VLCPlaylistItems )
BIND_INTERFACE( VLCPlaylist )
BIND_INTERFACE( VLCVideo )
BIND_INTERFACE( VLCSubtitle )
71
BIND_INTERFACE( VLCMediaDescription )
72

73
#undef  BIND_INTERFACE
74

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
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;
}

90 91
static inline
VARIANT_BOOL varbool(bool b) { return b ? VARIANT_TRUE : VARIANT_FALSE; }
92

93 94
static inline INT negativeToZero(int i) { return i < 0 ? 0 : i; }

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 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 165 166 167 168 169 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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
static HRESULT parseStringOptions(int codePage, BSTR bstr, char*** cOptions, int *cOptionCount)
{
    HRESULT hr = E_INVALIDARG;
    if (SysStringLen(bstr) > 0)
    {
        hr = E_OUTOFMEMORY;
        char *s = CStrFromBSTR(codePage, bstr);
        char *val = s;
        if (val)
        {
            long capacity = 16;
            char **options = (char **) CoTaskMemAlloc(capacity*sizeof(char *));
            if (options)
            {
                int nOptions = 0;

                char *end = val + strlen(val);
                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;
                            char **moreOptions = (char **) CoTaskMemRealloc(options, capacity*sizeof(char*));
                            if (!moreOptions)
                            {
                                /* failed to allocate more memory */
                                CoTaskMemFree(s);
                                /* return what we got so far */
                                *cOptionCount = nOptions;
                                *cOptions = options;
                                return NOERROR;
                            }
                            options = moreOptions;
                        }
                        *(val++) = '\0';
                        options[nOptions] = (char *) CoTaskMemAlloc(val - start);
                        if (options[nOptions])
                        {
                            memcpy(options[nOptions], start, val - start);
                            ++nOptions;
                        }
                        else
                        {
                            /* failed to allocate memory */
                            CoTaskMemFree(s);
                            /* return what we got so far */
                            *cOptionCount = nOptions;
                            *cOptions = options;
                            return NOERROR;
                        }
                    }
                    else
                        // must be end of string
                        break;
                }
                *cOptionCount = nOptions;
                *cOptions = options;
                hr = NOERROR;
            }
            CoTaskMemFree(s);
        }
    }
    return hr;
}

static void FreeTargetOptions(char **cOptions, int cOptionCount)
{
    // clean up
    if (NULL != cOptions)
    {
        for (int pos = 0; pos<cOptionCount; ++pos)
        {
            char *cOption = cOptions[pos];
            if (NULL != cOption)
                CoTaskMemFree(cOption);
            else
                break;
        }
        CoTaskMemFree(cOptions);
    }
};

static HRESULT CreateTargetOptions(int codePage, VARIANT *options, char ***cOptions, int *cOptionCount)
{
    HRESULT hr = E_INVALIDARG;
    if (VT_ERROR == V_VT(options))
    {
        if (DISP_E_PARAMNOTFOUND == V_ERROR(options))
        {
            // optional parameter not set
            *cOptions = NULL;
            *cOptionCount = 0;
            return NOERROR;
        }
    }
    else if ((VT_EMPTY == V_VT(options)) || (VT_NULL == V_VT(options)))
    {
        // null parameter
        *cOptions = NULL;
        *cOptionCount = 0;
        return NOERROR;
    }
    else if (VT_DISPATCH == V_VT(options))
    {
        // if object is a collection, retrieve enumerator
        VARIANT colEnum;
        V_VT(&colEnum) = VT_UNKNOWN;
        hr = GetObjectProperty(V_DISPATCH(options), DISPID_NEWENUM, colEnum);
        if (SUCCEEDED(hr))
        {
            IEnumVARIANT *enumVar;
            hr = V_UNKNOWN(&colEnum)->QueryInterface(IID_IEnumVARIANT, (LPVOID *) &enumVar);
            if (SUCCEEDED(hr))
            {
                long pos = 0;
                long capacity = 16;
                VARIANT option;

                *cOptions = (char **) CoTaskMemAlloc(capacity*sizeof(char *));
                if (NULL != *cOptions)
                {
                    ZeroMemory(*cOptions, sizeof(char *)*capacity);
                    while (SUCCEEDED(hr) && (S_OK == enumVar->Next(1, &option, NULL)))
                    {
                        if (VT_BSTR == V_VT(&option))
                        {
                            char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
                            (*cOptions)[pos] = cOption;
                            if (NULL != cOption)
                            {
                                ++pos;
                                if (pos == capacity)
                                {
                                    char **moreOptions = (char **) CoTaskMemRealloc(*cOptions, (capacity + 16)*sizeof(char *));
                                    if (NULL != moreOptions)
                                    {
                                        ZeroMemory(moreOptions + capacity, sizeof(char *) * 16);
                                        capacity += 16;
                                        *cOptions = moreOptions;
                                    }
                                    else
                                        hr = E_OUTOFMEMORY;
                                }
                            }
                            else
                                hr = (SysStringLen(V_BSTR(&option)) > 0) ?
                            E_OUTOFMEMORY : E_INVALIDARG;
                        }
                        else
                            hr = E_INVALIDARG;

                        VariantClear(&option);
                    }
                    *cOptionCount = pos;
                    if (FAILED(hr))
                    {
                        // free already processed elements
                        FreeTargetOptions(*cOptions, *cOptionCount);
                    }
                }
                else
                    hr = E_OUTOFMEMORY;

                enumVar->Release();
            }
        }
        else
        {
            // coerce object into a string and parse it
            VARIANT v_name;
            VariantInit(&v_name);
            hr = VariantChangeType(&v_name, options, 0, VT_BSTR);
            if (SUCCEEDED(hr))
            {
                hr = parseStringOptions(codePage, V_BSTR(&v_name), cOptions, cOptionCount);
                VariantClear(&v_name);
            }
        }
    }
    else if (V_ISARRAY(options))
    {
        // array parameter
        SAFEARRAY *array = V_ISBYREF(options) ? *V_ARRAYREF(options) : V_ARRAY(options);

        if (SafeArrayGetDim(array) != 1)
            return E_INVALIDARG;

        long lBound = 0;
        long uBound = 0;
        SafeArrayGetLBound(array, 1, &lBound);
        SafeArrayGetUBound(array, 1, &uBound);

        // have we got any options
        if (uBound >= lBound)
        {
            VARTYPE vType;
            hr = SafeArrayGetVartype(array, &vType);
            if (FAILED(hr))
                return hr;

            long pos;

            // marshall options into an array of C strings
            if (VT_VARIANT == vType)
            {
                *cOptions = (char **) CoTaskMemAlloc(sizeof(char *)*(uBound - lBound + 1));
                if (NULL == *cOptions)
                    return E_OUTOFMEMORY;

                ZeroMemory(*cOptions, sizeof(char *)*(uBound - lBound + 1));
                for (pos = lBound; (pos <= uBound) && SUCCEEDED(hr); ++pos)
                {
                    VARIANT option;
                    hr = SafeArrayGetElement(array, &pos, &option);
                    if (SUCCEEDED(hr))
                    {
                        if (VT_BSTR == V_VT(&option))
                        {
                            char *cOption = CStrFromBSTR(codePage, V_BSTR(&option));
                            (*cOptions)[pos - lBound] = cOption;
                            if (NULL == cOption)
                                hr = (SysStringLen(V_BSTR(&option)) > 0) ?
                            E_OUTOFMEMORY : E_INVALIDARG;
                        }
                        else
                            hr = E_INVALIDARG;
                        VariantClear(&option);
                    }
                }
            }
            else if (VT_BSTR == vType)
            {
                *cOptions = (char **) CoTaskMemAlloc(sizeof(char *)*(uBound - lBound + 1));
                if (NULL == *cOptions)
                    return E_OUTOFMEMORY;

                ZeroMemory(*cOptions, sizeof(char *)*(uBound - lBound + 1));
                for (pos = lBound; (pos <= uBound) && SUCCEEDED(hr); ++pos)
                {
                    BSTR option;
                    hr = SafeArrayGetElement(array, &pos, &option);
                    if (SUCCEEDED(hr))
                    {
                        char *cOption = CStrFromBSTR(codePage, option);

                        (*cOptions)[pos - lBound] = cOption;
                        if (NULL == cOption)
                            hr = (SysStringLen(option) > 0) ?
                        E_OUTOFMEMORY : E_INVALIDARG;
                        SysFreeString(option);
                    }
                }
            }
            else
            {
                // unsupported type
                return E_INVALIDARG;
            }

            *cOptionCount = pos - lBound;
            if (FAILED(hr))
            {
                // free already processed elements
                FreeTargetOptions(*cOptions, *cOptionCount);
            }
        }
        else
        {
            // empty array
            *cOptions = NULL;
            *cOptionCount = 0;
            return NOERROR;
        }
    }
    else if (VT_UNKNOWN == V_VT(options))
    {
        // coerce object into a string and parse it
        VARIANT v_name;
        VariantInit(&v_name);
        hr = VariantChangeType(&v_name, options, 0, VT_BSTR);
        if (SUCCEEDED(hr))
        {
            hr = parseStringOptions(codePage, V_BSTR(&v_name), cOptions, cOptionCount);
            VariantClear(&v_name);
        }
    }
    else if (VT_BSTR == V_VT(options))
    {
        hr = parseStringOptions(codePage, V_BSTR(options), cOptions, cOptionCount);
    }
    return hr;
}


410
// ---------
411 412 413 414 415 416

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

417
    *mute = varbool( _plug->get_player().get_mp().mute() );
418 419

    return S_OK;
420
}
421 422 423

STDMETHODIMP VLCAudio::put_mute(VARIANT_BOOL mute)
{
424
    _plug->get_player().get_mp().setMute( VARIANT_FALSE != mute );
425 426

    return S_OK;
427
}
428

429
STDMETHODIMP VLCAudio::get_volume(long* volume)
430 431 432 433
{
    if( NULL == volume )
        return E_POINTER;

434
    *volume = _plug->get_player().get_mp().volume();
435 436

    return S_OK;
437
}
438

439
STDMETHODIMP VLCAudio::put_volume(long volume)
440
{
441
    _plug->get_player().get_mp().setVolume( volume );
442 443

    return S_OK;
444
}
445

446 447 448 449 450
STDMETHODIMP VLCAudio::get_track(long* track)
{
    if( NULL == track )
        return E_POINTER;

451 452 453
    *track = _plug->get_player().currentAudioTrack();
    return S_OK;
}
454 455 456

STDMETHODIMP VLCAudio::put_track(long track)
{
457 458
    auto tracks = _plug->get_player().get_mp().audioTrackDescription();
    if ( track >= tracks.size() )
459
        return E_INVALIDARG;
460 461 462
    _plug->get_player().get_mp().setAudioTrack( tracks[track].id() );
    return S_OK;
}
463

464 465 466 467 468
STDMETHODIMP VLCAudio::get_count(long* trackNumber)
{
    if( NULL == trackNumber )
        return E_POINTER;

469
    *trackNumber = negativeToZero( _plug->get_player().get_mp().audioTrackCount() );
470

471
    return S_OK;
472
}
473

474
STDMETHODIMP VLCAudio::description(long trackId, BSTR* name)
475 476 477 478
{
    if( NULL == name )
        return E_POINTER;

479 480 481 482 483 484
    auto tracks = _plug->get_player().get_mp().audioTrackDescription();
    if ( trackId >= tracks.size() )
        return E_INVALIDARG;
    *name = BSTRFromCStr( CP_UTF8, tracks[trackId].name().c_str() );
    return (NULL == *name) ? E_OUTOFMEMORY : S_OK;
}
485

486
STDMETHODIMP VLCAudio::get_channel(long *channel)
487 488 489 490
{
    if( NULL == channel )
        return E_POINTER;

491
    *channel = _plug->get_player().get_mp().channel();
492 493

    return S_OK;
494
}
495

496
STDMETHODIMP VLCAudio::put_channel(long channel)
497
{
498
    _plug->get_player().get_mp().setChannel( channel );
499 500

    return S_OK;
501
}
502

503 504
STDMETHODIMP VLCAudio::toggleMute()
{
505
    _plug->get_player().get_mp().toggleMute();
506 507

    return S_OK;
508
}
509

510
/****************************************************************************/
511

512 513
STDMETHODIMP VLCDeinterlace::disable()
{
514 515
    _plug->get_player().get_mp().setDeinterlace( "" );
    return S_OK;
516 517 518 519
}

STDMETHODIMP VLCDeinterlace::enable(BSTR mode)
{
520
    char *psz_mode = CStrFromBSTR(CP_UTF8, mode);
521 522
    if ( psz_mode == nullptr )
        return E_OUTOFMEMORY;
523 524
    _plug->get_player().get_mp().setDeinterlace( psz_mode );
    CoTaskMemFree(psz_mode);
525
    return S_OK;
526 527 528 529
}

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

530 531 532 533 534
STDMETHODIMP VLCTitle::get_count(long* countTracks)
{
    if( NULL == countTracks )
        return E_POINTER;

535
    *countTracks = negativeToZero( _plug->get_player().get_mp().titleCount() );
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
    return S_OK;
}

STDMETHODIMP VLCTitle::get_track(long* track)
{
    if( NULL == track )
        return E_POINTER;

    *track = _plug->get_player().get_mp().title();
    return S_OK;
}

STDMETHODIMP VLCTitle::put_track(long track)
{
    _plug->get_player().get_mp().setTitle(track);
    return S_OK;
}

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

    auto tracks = _plug->get_player().get_mp().titleDescription();
    if ( track >= tracks.size() )
        return E_INVALIDARG;
    *name = BSTRFromCStr( CP_UTF8, tracks[track].name().c_str() );
    return (NULL == *name) ? E_OUTOFMEMORY : S_OK;
}

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

STDMETHODIMP VLCChapter::get_count(long* countTracks)
{
    if( NULL == countTracks )
        return E_POINTER;

573
    *countTracks = negativeToZero( _plug->get_player().get_mp().chapterCount() );
574 575 576 577 578 579 580 581
    return S_OK;
}

STDMETHODIMP VLCChapter::countForTitle(long track, long* countTracks)
{
    if( NULL == countTracks )
        return E_POINTER;

582
    *countTracks = negativeToZero( _plug->get_player().get_mp().chapterCountForTitle(track) );
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 622 623 624 625 626 627 628 629 630
    return S_OK;
}

STDMETHODIMP VLCChapter::get_track(long* track)
{
    if( NULL == track )
        return E_POINTER;

    *track = _plug->get_player().get_mp().chapter();
    return S_OK;
}

STDMETHODIMP VLCChapter::put_track(long track)
{
    _plug->get_player().get_mp().setChapter(track);
    return S_OK;
}

STDMETHODIMP VLCChapter::description(long title, long chapter, BSTR* name)
{
    if( NULL == name )
        return E_POINTER;

    auto titleTracks = _plug->get_player().get_mp().titleDescription();
    if ( title >= titleTracks.size() )
        return E_INVALIDARG;

    auto tracks = _plug->get_player().get_mp().chapterDescription(title);
    if ( chapter >= tracks.size() )
        return E_INVALIDARG;
    *name = BSTRFromCStr( CP_UTF8, tracks[chapter].name().c_str() );
    return (NULL == *name) ? E_OUTOFMEMORY : S_OK;
}

STDMETHODIMP VLCChapter::next()
{
    _plug->get_player().get_mp().nextChapter();
    return S_OK;
}

STDMETHODIMP VLCChapter::prev()
{
    _plug->get_player().get_mp().previousChapter();
    return S_OK;
}

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

631
STDMETHODIMP VLCInput::get_length(double* length)
632 633 634 635
{
    if( NULL == length )
        return E_POINTER;

636
    *length = static_cast<double>(_plug->get_player().get_mp().length() );
637 638

    return S_OK;
639
}
640

641
STDMETHODIMP VLCInput::get_position(double* position)
642 643 644 645
{
    if( NULL == position )
        return E_POINTER;

646
    *position = _plug->get_player().get_mp().position();
647 648

    return S_OK;
649
}
650

651
STDMETHODIMP VLCInput::put_position(double position)
652
{
653
    _plug->get_player().get_mp().setPosition( static_cast<float>(position) );
654 655

    return S_OK;
656
}
657

658
STDMETHODIMP VLCInput::get_time(double* time)
659 660 661 662
{
    if( NULL == time )
        return E_POINTER;

663
    *time = static_cast<double>(_plug->get_player().get_mp().time() );
664 665

    return S_OK;
666
}
667

668
STDMETHODIMP VLCInput::put_time(double time)
669
{
670
    _plug->get_player().get_mp().setTime(static_cast<libvlc_time_t>(time));
671 672

    return S_OK;
673
}
674

675
STDMETHODIMP VLCInput::get_state(long* state)
676 677 678 679
{
    if( NULL == state )
        return E_POINTER;

680
    *state = _plug->get_player().get_mp().state();
681 682

    return S_OK;
683
}
684

685
STDMETHODIMP VLCInput::get_rate(double* rate)
686 687 688 689
{
    if( NULL == rate )
        return E_POINTER;

690
    *rate = _plug->get_player().get_mp().rate();
691 692

    return S_OK;
693
}
694

695
STDMETHODIMP VLCInput::put_rate(double rate)
696
{
697
    _plug->get_player().get_mp().setRate( static_cast<float>(rate) );
698 699

    return S_OK;
700
}
701

702
STDMETHODIMP VLCInput::get_fps(double* fps)
703 704 705 706
{
    if( NULL == fps )
        return E_POINTER;

707 708
    auto media = _plug->get_player().get_mp().media();
    if ( media == nullptr )
709
        return E_FAIL;
710 711 712 713 714 715 716 717 718 719
    auto tracks = media->tracks();
    for ( const auto& t : tracks )
    {
        if ( t.type() == VLC::MediaTrack::Type::Video )
        {
            *fps = (float)( (float)t.fpsNum() / (float)t.fpsDen() );
            return S_OK;
        }
    }
    *fps = 0.0;
720 721

    return S_OK;
722
}
723 724 725 726 727 728

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

729
    *hasVout = varbool( _plug->get_player().get_mp().hasVout() > 0 );
730 731

    return S_OK;
732
}
733

734 735 736 737 738 739 740 741 742 743
STDMETHODIMP VLCInput::get_title(IVLCTitle** obj)
{
    return object_get(obj,_p_vlctitle);
}

STDMETHODIMP VLCInput::get_chapter(IVLCChapter** obj)
{
    return object_get(obj,_p_vlcchapter);
}

744
/****************************************************************************/
745

746
HRESULT VLCMarquee::do_put_int(unsigned idx, LONG val)
747
{
748 749
    _plug->get_player().get_mp().setMarqueeInt( idx, val );
    return S_OK;
750
}
751

752
HRESULT VLCMarquee::do_get_int(unsigned idx, LONG *val)
753
{
754 755
    if( NULL == val )
        return E_POINTER;
756

757 758
    *val = _plug->get_player().get_mp().marqueeInt( idx );
    return S_OK;
759
}
760

761
STDMETHODIMP VLCMarquee::get_position(BSTR* val)
762
{
763 764
    if( NULL == val )
        return E_POINTER;
765

766 767 768 769
    LONG i;
    HRESULT hr = do_get_int(libvlc_marquee_Position, &i);
    if(SUCCEEDED(hr))
        *val = BSTRFromCStr(CP_UTF8, position_bynumber(i));
770 771

    return hr;
772
}
773

774
STDMETHODIMP VLCMarquee::put_position(BSTR val)
775
{
776 777
    char *n = CStrFromBSTR(CP_UTF8, val);
    if( !n ) return E_OUTOFMEMORY;
778

779 780 781 782 783 784
    size_t i;
    HRESULT hr;
    if( position_byname( n, i ) )
        hr = do_put_int(libvlc_marquee_Position,i);
    else
        hr = E_INVALIDARG;
785

786
    CoTaskMemFree(n);
787
    return hr;
788
}
789

790
STDMETHODIMP VLCMarquee::get_text(BSTR *val)
791
{
792 793
    if( NULL == val )
        return E_POINTER;
794

795 796 797 798
    auto str = _plug->get_player().get_mp().marqueeString( libvlc_marquee_Text );
    if( !str.empty() )
        *val = BSTRFromCStr( CP_UTF8, str.c_str() );
    return S_OK;
799
}
800

801
STDMETHODIMP VLCMarquee::put_text(BSTR val)
802
{
803
    char *psz_text = CStrFromBSTR(CP_UTF8, val);
804 805
    if ( psz_text == nullptr )
        return E_OUTOFMEMORY;
806 807 808
    _plug->get_player().get_mp().setMarqueeString( libvlc_marquee_Text, psz_text );
    CoTaskMemFree(psz_text);
    return S_OK;
809
}
810

811
/****************************************************************************/
812

813 814 815 816 817
STDMETHODIMP VLCPlaylistItems::get_count(long* count)
{
    if( NULL == count )
        return E_POINTER;

818
    *count = _plug->get_player().items_count();
819
    return S_OK;
820
}
821 822 823

STDMETHODIMP VLCPlaylistItems::clear()
{
824
    _plug->get_player().clear_items();
825
    return S_OK;
826
}
827 828 829

STDMETHODIMP VLCPlaylistItems::remove(long item)
{
830
    _plug->get_player().delete_item(item);
831
    return S_OK;
832
}
833

834
/****************************************************************************/
835

836
STDMETHODIMP VLCPlaylist::get_itemCount(long* count)
837 838 839 840
{
    if( NULL == count )
        return E_POINTER;

841
    *count = _plug->get_player().items_count();
842

843
    return S_OK;
844
}
845 846 847 848 849 850

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

851
    *isPlaying = varbool( _plug->get_player().mlp().isPlaying() );
852 853

    return S_OK;
854
}
855

856 857 858 859 860
STDMETHODIMP VLCPlaylist::get_currentItem(long* index)
{
    if( NULL == index )
        return E_POINTER;

861
    *index = _plug->get_player().current_item();
862 863

    return S_OK;
864
}
865

866
STDMETHODIMP VLCPlaylist::add(BSTR uri, VARIANT name, VARIANT options, long* item)
867 868 869 870 871 872 873
{
    if( NULL == item )
        return E_POINTER;

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

874 875 876 877 878 879 880 881 882
    HRESULT hr;
    char *psz_uri = NULL;
    if( SysStringLen(_plug->getBaseURL()) > 0 )
    {
        /*
        ** if the MRL a relative URL, we should end up with an absolute URL
        */
        LPWSTR abs_url = CombineURL(_plug->getBaseURL(), uri);
        if( NULL != abs_url )
883
        {
884 885
            psz_uri = CStrFromWSTR(CP_UTF8, abs_url, wcslen(abs_url));
            CoTaskMemFree(abs_url);
886 887 888 889 890
        }
        else
        {
            psz_uri = CStrFromBSTR(CP_UTF8, uri);
        }
891 892 893 894 895 896 897 898
    }
    else
    {
        /*
        ** baseURL is empty, assume MRL is absolute
        */
        psz_uri = CStrFromBSTR(CP_UTF8, uri);
    }
899

900 901 902 903
    if( NULL == psz_uri )
    {
        return E_OUTOFMEMORY;
    }
904

905 906
    int i_options;
    char **ppsz_options;
907

908
    hr = CreateTargetOptions(CP_UTF8, &options, &ppsz_options, &i_options);
909 910 911 912 913
    if( FAILED(hr) )
    {
        CoTaskMemFree(psz_uri);
        return hr;
    }
914

915 916 917 918 919 920 921
    char *psz_name = NULL;
    VARIANT v_name;
    VariantInit(&v_name);
    if( SUCCEEDED(VariantChangeType(&v_name, &name, 0, VT_BSTR)) )
    {
        if( SysStringLen(V_BSTR(&v_name)) > 0 )
            psz_name = CStrFromBSTR(CP_UTF8, V_BSTR(&v_name));
922

923 924
        VariantClear(&v_name);
    }
925

926 927
    *item = _plug->get_player().add_item( psz_uri, i_options,
                                               const_cast<const char **>(ppsz_options));
928

929
    FreeTargetOptions(ppsz_options, i_options);
930 931 932
    CoTaskMemFree(psz_uri);
    if( psz_name ) /* XXX Do we even need to check? */
        CoTaskMemFree(psz_name);
933
    return hr;
934
}
935 936 937

STDMETHODIMP VLCPlaylist::play()
{
938
    _plug->get_player().mlp().play();
939
    return S_OK;
940 941
};

942
STDMETHODIMP VLCPlaylist::playItem(long item)
943
{
944
    _plug->get_player().mlp().playItemAtIndex( item );
945
    return S_OK;
946
}
947

948
STDMETHODIMP VLCPlaylist::pause()
949
{
Daniel Amm's avatar
Daniel Amm committed
950
    _plug->get_player().get_mp().setPause( true );
951
    return S_OK;
952
}
953

954 955
STDMETHODIMP VLCPlaylist::togglePause()
{
956
    _plug->get_player().mlp().pause();
957
    return S_OK;
958
}
959

960 961
STDMETHODIMP VLCPlaylist::stop()
{
962
    _plug->get_player().mlp().stop();
963
    return S_OK;
964
}
965 966 967

STDMETHODIMP VLCPlaylist::next()
{
968
    _plug->get_player().mlp().next();
969
    return S_OK;
970
}
971 972 973

STDMETHODIMP VLCPlaylist::prev()
{
974
    _plug->get_player().mlp().previous();
975
    return S_OK;
976
}
977 978 979

STDMETHODIMP VLCPlaylist::clear()
{
980
    _plug->get_player().clear_items();
981
    return S_OK;
982
}
983

984
STDMETHODIMP VLCPlaylist::removeItem(long item)
985
{
986
    _plug->get_player().delete_item(item);
987
    return S_OK;
988
}
989

990 991 992 993 994 995 996 997 998 999 1000 1001
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;
1002
}
1003

1004
/****************************************************************************/
1005

1006 1007 1008 1009 1010
STDMETHODIMP VLCSubtitle::get_track(long* spu)
{
    if( NULL == spu )
        return E_POINTER;

1011 1012 1013
    *spu = _plug->get_player().currentSubtitleTrack();
    return S_OK;
}
1014

1015
//FIXME: this should be unsigned
1016 1017
STDMETHODIMP VLCSubtitle::put_track(long spu)
{
1018 1019
    auto tracks = _plug->get_player().get_mp().spuDescription();
    if ( spu >= tracks.size() )
1020
        return E_INVALIDARG;
1021 1022 1023
    _plug->get_player().get_mp().setSpu( tracks[spu].id() );
    return S_OK;
}
1024 1025 1026 1027 1028 1029

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

1030 1031 1032
    *spuNumber = _plug->get_player().get_mp().spuCount();
    return S_OK;
}
1033 1034 1035 1036 1037 1038

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

1039 1040
    auto tracks = _plug->get_player().get_mp().spuDescription();
    if ( nameID >= tracks.size() )
1041
        return E_INVALIDARG;
1042 1043 1044
    *name = BSTRFromCStr( CP_UTF8, tracks[nameID].name().c_str() );
    return (NULL == *name) ? E_OUTOFMEMORY : S_OK;
}
1045

1046
/****************************************************************************/
1047

1048 1049 1050 1051 1052
STDMETHODIMP VLCVideo::get_fullscreen(VARIANT_BOOL* fullscreen)
{
    if( NULL == fullscreen )
        return E_POINTER;

1053
    *fullscreen = _plug->getFullscreen();
1054 1055
    return S_OK;
}
1056 1057 1058

STDMETHODIMP VLCVideo::put_fullscreen(VARIANT_BOOL fullscreen)
{
1059
    _plug->setFullscreen( VARIANT_FALSE != fullscreen );
1060 1061
    return S_OK;
}
1062

1063
//FIXME: This should be unsigned int
1064
STDMETHODIMP VLCVideo::get_width(long* width)
1065 1066 1067 1068
{
    if( NULL == width )
        return E_POINTER;

1069 1070 1071 1072
    unsigned int height;
    _plug->get_player().get_mp().size( 0, (unsigned int*)width, &height );
    return S_OK;
}
1073

1074
STDMETHODIMP VLCVideo::get_height(long* height)
1075 1076 1077 1078
{
    if( NULL == height )
        return E_POINTER;

1079 1080 1081 1082
    unsigned int width;
    _plug->get_player().get_mp().size( 0, &width, (unsigned int*)height );
    return S_OK;
}
1083

1084
STDMETHODIMP VLCVideo::get_aspectRatio(BSTR* aspect)
1085
{
1086
    if( NULL == aspect )
1087 1088
        return E_POINTER;

1089 1090 1091 1092
    auto ar = _plug->get_player().get_mp().aspectRatio();
    *aspect = BSTRFromCStr( CP_UTF8, ar.c_str() );
    return *aspect == nullptr ? E_OUTOFMEMORY : S_OK;
}
1093 1094 1095

STDMETHODIMP VLCVideo::put_aspectRatio(BSTR aspect)
{
1096 1097 1098 1099 1100
    if( 0 == SysStringLen(aspect) )
    {
        _plug->get_player().get_mp().setAspectRatio( "" );
        return S_OK;
    }
1101

1102 1103
    char *psz_aspect = CStrFromBSTR(CP_UTF8, aspect);
    if( !psz_aspect )
1104
    {
1105
        return E_OUTOFMEMORY;
1106
    }
1107 1108 1109 1110
    _plug->get_player().get_mp().setAspectRatio( psz_aspect );
    CoTaskMemFree(psz_aspect);
    return S_OK;
}
1111

1112 1113 1114 1115 1116
STDMETHODIMP VLCVideo::get_subtitle(long* spu)
{
    if( NULL == spu )
        return E_POINTER;

1117
    *spu = _plug->get_player().currentSubtitleTrack();
1118 1119
    return S_OK;
}
1120 1121 1122

STDMETHODIMP VLCVideo::put_subtitle(long spu)
{
1123 1124 1125 1126
    auto tracks = _plug->get_player().get_mp().spuDescription();
    if ( spu >= tracks.size() )
        return E_INVALIDARG;
    _plug->get_player().get_mp().setSpu( tracks[spu].id() );
1127 1128
    return S_OK;
}
1129

1130 1131 1132 1133 1134
STDMETHODIMP VLCVideo::get_crop(BSTR* geometry)
{
    if( NULL == geometry )
        return E_POINTER;

1135 1136 1137 1138
    auto g = _plug->get_player().get_mp().cropGeometry();
    *geometry = BSTRFromCStr( CP_UTF8, g.c_str() );
    return *geometry == nullptr ? E_OUTOFMEMORY : S_OK;
}
1139 1140 1141 1142

STDMETHODIMP VLCVideo::put_crop(BSTR geometry)
{
    if( 0 == SysStringLen(geometry) )
1143 1144 1145 1146
    {
        _plug->get_player().get_mp().setCropGeometry( "" );
        return S_OK;
    }
1147

1148 1149
    char *psz_geometry = CStrFromBSTR(CP_UTF8, geometry);
    if( !psz_geometry )
1150
    {
1151
        return E_OUTOFMEMORY;
1152
    }
1153 1154 1155 1156 1157
    _plug->get_player().get_mp().setCropGeometry( psz_geometry );
    CoTaskMemFree(psz_geometry);

    return S_OK;
}
1158

1159 1160 1161 1162 1163
STDMETHODIMP VLCVideo::get_teletext(long* page)
{
    if( NULL == page )
        return E_POINTER;

1164 1165 1166
    *page = _plug->get_player().get_mp().teletext();
    return S_OK;
}
1167 1168 1169

STDMETHODIMP VLCVideo::put_teletext(long page)
{
1170 1171 1172
    _plug->get_player().get_mp().setTeletext( page );
    return S_OK;
}
1173

1174
STDMETHODIMP VLCVideo::takeSnapshot(LPPICTUREDISP* picture)
1175
{
1176
    if( NULL == picture )
1177 1178
        return E_POINTER;

1179 1180
    static int uniqueId = 0;
    TCHAR path[MAX_PATH+1];
1181

1182 1183 1184 1185 1186
    int pathlen = GetTempPath(MAX_PATH-24, path);
    if( (0 == pathlen) || (pathlen > (MAX_PATH-24)) )
        return E_FAIL;

    HRESULT hr;
1187

1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
    /* 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 )
        {
            _plug->setErrorInfo(IID_IVLCVideo,
                    "Invalid temporary directory for snapshot images, check values of TMP, TEMP envars.");
            return E_FAIL;
        }
        else
JP Dinger's avatar
JP Dinger committed
1200
        {
1201 1202 1203 1204
            BY_HANDLE_FILE_INFORMATION bhfi;
            BOOL res = GetFileInformationByHandle(dirHandle, &bhfi);
            CloseHandle(dirHandle);
            if( !res || !(bhfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
JP Dinger's avatar
JP Dinger committed
1205
            {
1206
                _plug->setErrorInfo(IID_IVLCVideo,
JP Dinger's avatar
JP Dinger committed
1207 1208 1209 1210
                        "Invalid temporary directory for snapshot images, check values of TMP, TEMP envars.");
                return E_FAIL;
            }
        }
1211
    }
1212

1213
    TCHAR filepath[MAX_PATH+1];
1214

1215 1216
    _stprintf(filepath, TEXT("%sAXVLC%lXS%lX.bmp"),
             path, GetCurrentProcessId(), ++uniqueId);
1217 1218

#ifdef _UNICODE
1219 1220 1221
    /* reuse path storage for UTF8 string */
    char *psz_filepath = (char *)path;
    WCHAR* wpath = filepath;
1222
#else
1223 1224 1225 1226 1227 1228
    char *psz_filepath = path;
    /* first convert to unicode using current code page */
    WCHAR wpath[MAX_PATH+1];
    if( 0 == MultiByteToWideChar(CP_ACP, 0, filepath, -1,
                                 wpath, sizeof(wpath)/sizeof(WCHAR)) )
        return E_FAIL;
1229
#endif
1230 1231 1232 1233 1234
    /* convert to UTF8 */
    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)
1235

1236 1237
    if( (0 == pathlen) || (sizeof(path) == pathlen) )
        return E_FAIL;
1238

1239 1240 1241 1242 1243 1244 1245
    /* take snapshot into file */
    if ( !_plug->get_player().get_mp().takeSnapshot( 0, psz_filepath, 0, 0 ) )
    {
        /* open snapshot file */
        HANDLE snapPic = LoadImage(NULL, filepath, IMAGE_BITMAP, 0, 0,
                                   LR_CREATEDIBSECTION|LR_LOADFROMFILE);
        if( snapPic )
JP Dinger's avatar
JP Dinger committed
1246
        {
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
            PICTDESC snapDesc;

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

            hr = OleCreatePictureIndirect(&snapDesc, IID_IPictureDisp,
                                          TRUE, (LPVOID*)picture);
            if( FAILED(hr) )
1257
            {
1258 1259
                *picture = NULL;
                DeleteObject(snapPic);
1260 1261
            }
        }
1262
        DeleteFile(filepath);
1263 1264
    }
    return hr;
1265
}
1266

1267 1268
STDMETHODIMP VLCVideo::toggleFullscreen()
{
1269
    _plug->toggleFullscreen();
1270 1271
    return S_OK;
}
1272

1273 1274
STDMETHODIMP VLCVideo::toggleTeletext()
{
1275
#if LIBVLC_VERSION_INT >= LIBVLC_VERSION(3, 0, 0, 0)
1276
    if( _plug->get_player().get_mp().teletext() == 0 )
1277 1278
        _plug->get_player().get_mp().setTeletext( 100 );
    else
1279
        _plug->get_player().get_mp().setTeletext( 0 );
1280
    return S_OK;
1281 1282 1283
#else
    _plug->get_player().get_mp().toggleTeletext();
#endif
1284
}
1285

1286 1287
STDMETHODIMP VLCVideo::get_marquee(IVLCMarquee** obj)
{
1288 1289
    return object_get(obj,_p_vlcmarquee);
}
1290

1291 1292
STDMETHODIMP VLCVideo::get_logo(IVLCLogo** obj)
{
1293
    return object_get(obj,_p_vlclogo);
1294 1295
}

1296 1297 1298 1299 1300
STDMETHODIMP VLCVideo::get_deinterlace(IVLCDeinterlace** obj)
{
    return object_get(obj,_p_vlcdeint);
}

1301 1302 1303 1304
/****************************************************************************/

HRESULT VLCLogo::do_put_int(unsigned idx, LONG val)
{
1305 1306
    _plug->get_player().get_mp().setLogoInt( idx, val );
    return S_OK;
1307 1308 1309 1310 1311 1312 1313
}

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

1314 1315
    *val = _plug->get_player().get_mp().logoInt( idx );
    return S_OK;
1316
}
1317

1318 1319 1320
STDMETHODIMP VLCLogo::file(BSTR fname)
{
    char *n = CStrFromBSTR(CP_UTF8, fname);
1321 1322
    if ( !n )
        return E_OUTOFMEMORY;
1323

1324
    _plug->get_player().get_mp().setLogoString( libvlc_logo_file, n );
1325
    CoTaskMemFree(n);
1326
    return S_OK;
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
}

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))
1338
        *val = BSTRFromCStr(CP_UTF8, position_bynumber(i));
1339 1340 1341

    return hr;
}
1342

1343 1344 1345 1346 1347
STDMETHODIMP VLCLogo::put_position(BSTR val)
{
    char *n = CStrFromBSTR(CP_UTF8, val);
    if( !n ) return E_OUTOFMEMORY;

1348 1349 1350 1351 1352
    size_t i;
    HRESULT hr;
    if( position_byname( n, i ) )
        hr = do_put_int(libvlc_logo_position,i);
    else
1353 1354 1355 1356 1357
        hr = E_INVALIDARG;

    CoTaskMemFree(n);
    return hr;
}
1358 1359 1360 1361 1362 1363 1364 1365 1366
/****************************************************************************/

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

    *val = 0;

1367 1368 1369 1370 1371 1372
    auto media = _plug->get_player().get_mp().media();
    if ( media == nullptr )
        return E_FAIL;
    auto info = media->meta( e_meta );
    *val = BSTRFromCStr( CP_UTF8, info.c_str() );
    return *val ? S_OK : E_FAIL;
1373 1374 1375 1376 1377 1378
}

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

1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
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::