vlc_player.cpp 3.22 KB
Newer Older
1
/*****************************************************************************
2
 * Copyright � 2002-2011 VideoLAN and VLC authors
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * $Id$
 *
 * Authors: Sergey Radionov <rsatom_gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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.
 *****************************************************************************/

#include "vlc_player.h"

24
bool vlc_player::open(VLC::Instance& inst)
25 26 27 28 29 30
{
    if( !inst )
        return false;

    _libvlc_instance = inst;

31 32 33 34
    try {
        _mp   = VLC::MediaPlayer(inst);
        _ml   = VLC::MediaList(inst);
        _ml_p = VLC::MediaListPlayer(inst);
35

36 37
        _ml_p.setMediaList( _ml );
        _ml_p.setMediaPlayer( _mp );
38
    }
39
    catch (std::runtime_error&) {
40 41 42 43 44 45 46 47
        return false;
    }

    return true;
}

int vlc_player::add_item(const char * mrl, unsigned int optc, const char **optv)
{
48 49 50 51 52
    VLC::Media media;
    try {
        media = VLC::Media( _libvlc_instance, mrl, VLC::Media::FromLocation );
    }
    catch ( std::runtime_error& ) {
53
        return -1;
54
    }
55 56

    for( unsigned int i = 0; i < optc; ++i )
57
        media.addOptionFlag( optv[i], libvlc_media_option_unique );
58

59 60 61 62
    VLC::MediaList::Lock lock( _ml );
    if( _ml.addMedia( media ) )
         return _ml.count() - 1;
    return -1;
63 64 65 66
}

int vlc_player::current_item()
{
67
    auto media = _mp.media();
68 69 70

    if( !media )
        return -1;
71
    return _ml.indexOfItem( *media );
72 73 74 75
}

int vlc_player::items_count()
{
76 77
    VLC::MediaList::Lock lock( _ml );
    return _ml.count();
78 79 80 81
}

bool vlc_player::delete_item(unsigned int idx)
{
82 83
    VLC::MediaList::Lock lock( _ml );
    return _ml.removeIndex( idx );
84 85 86 87
}

void vlc_player::clear_items()
{
88 89 90
    VLC::MediaList::Lock lock( _ml );
    for( int i = _ml.count(); i > 0; --i) {
        _ml.removeIndex( i - 1 );
91 92 93 94 95
    }
}

void vlc_player::play()
{
96 97 98
    if( 0 == items_count() )
        return;
    else if( -1 == current_item() ) {
99
        _ml_p.playItemAtIndex( 0 );
100
    }
101
    else
102
        _ml_p.play();
103
}
104 105 106

int vlc_player::currentAudioTrack()
{
107 108
    auto current = _mp.audioTrack();
    auto tracks = _mp.audioTrackDescription();
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
    return getTrack( current, tracks );
}

int vlc_player::currentSubtitleTrack()
{
    auto current = _mp.spu();
    auto tracks = _mp.spuDescription();
    return getTrack( current, tracks );
}

int vlc_player::getTrack( int currentId, const std::vector<VLC::TrackDescription>& tracks )
{
    if ( tracks.empty() )
        return -1;

    int trackId = 0;
    for ( const auto& t : tracks )
    {
        if ( t.id() == currentId )
        {
            return trackId;
        }
        ++trackId;
    }
    return -1;
}