vlc_player.cpp 6.25 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 24 25 26 27
 * $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"

vlc_player::vlc_player()
{
}

28
bool vlc_player::open(VLC::Instance& inst)
29 30 31 32 33 34
{
    if( !inst )
        return false;

    _libvlc_instance = inst;

35 36 37 38
    try {
        _mp   = VLC::MediaPlayer(inst);
        _ml   = VLC::MediaList(inst);
        _ml_p = VLC::MediaListPlayer(inst);
39

40 41
        _ml_p.setMediaList( _ml );
        _ml_p.setMediaPlayer( _mp );
42
    }
43
    catch (std::runtime_error&) {
44 45 46 47 48 49 50 51
        return false;
    }

    return true;
}

bool vlc_player::is_playing()
{
52
    return _ml_p && _ml_p.isPlaying() != 0;
53 54 55 56 57 58 59
}

libvlc_state_t vlc_player::get_state()
{
    if( !is_open() )
        return libvlc_NothingSpecial;

60
    return _ml_p.state();
61 62 63 64 65 66 67
}

int vlc_player::add_item(const char * mrl, unsigned int optc, const char **optv)
{
    if( !is_open() )
        return -1;

68 69 70 71 72
    VLC::Media media;
    try {
        media = VLC::Media( _libvlc_instance, mrl, VLC::Media::FromLocation );
    }
    catch ( std::runtime_error& ) {
73
        return -1;
74
    }
75 76

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

79 80 81 82
    VLC::MediaList::Lock lock( _ml );
    if( _ml.addMedia( media ) )
         return _ml.count() - 1;
    return -1;
83 84 85 86 87 88 89
}

int vlc_player::current_item()
{
    if( !is_open() )
        return -1;

90
    auto media = _mp.media();
91 92 93

    if( !media )
        return -1;
94
    return _ml.indexOfItem( *media );
95 96 97 98 99 100 101
}

int vlc_player::items_count()
{
    if( !is_open() )
        return 0;

102 103
    VLC::MediaList::Lock lock( _ml );
    return _ml.count();
104 105 106 107 108 109 110
}

bool vlc_player::delete_item(unsigned int idx)
{
    if( !is_open() )
        return false;

111 112
    VLC::MediaList::Lock lock( _ml );
    return _ml.removeIndex( idx );
113 114 115 116 117 118 119
}

void vlc_player::clear_items()
{
    if( !is_open() )
        return;

120 121 122
    VLC::MediaList::Lock lock( _ml );
    for( int i = _ml.count(); i > 0; --i) {
        _ml.removeIndex( i - 1 );
123 124 125 126 127 128 129 130
    }
}

void vlc_player::play()
{
    if( !is_open() )
        return;

131 132 133 134 135 136
    if( 0 == items_count() )
        return;
    else if( -1 == current_item() ) {
        play(0);
    }
    else {
137
        _ml_p.play();
138 139
        on_player_action(pa_play);
    }
140 141 142 143 144 145 146
}

bool vlc_player::play(unsigned int idx)
{
    if( !is_open() )
        return false;

147
    if( _ml_p.playItemAtIndex( idx ) ) {
148 149 150 151 152 153 154
        on_player_action(pa_play);
        return true;
    }
    return false;
}

void vlc_player::pause()
155 156
{
    if( is_open() ) {
157
        _mp.setPause( true );
158 159 160 161 162
        on_player_action(pa_pause);
    }
}

void vlc_player::togglePause()
163 164
{
    if( is_open() ) {
165
        _ml_p.pause();
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
        on_player_action(pa_pause);
    }
}

void vlc_player::stop()
{
    if( is_open() ){
        libvlc_media_list_player_stop(_ml_p);
        on_player_action(pa_stop);
    }
}

bool vlc_player::next()
{
    if( !is_open() )
        return false;

183
    if( _ml_p.next() ) {
184 185 186 187 188 189 190 191 192 193 194
        on_player_action(pa_next);
        return true;
    }
    return false;
}

bool vlc_player::prev()
{
    if( !is_open() )
        return false;

195 196

    if( _ml_p.previous() ) {
197 198 199 200 201 202 203 204 205 206 207
        on_player_action(pa_prev);
        return true;
    }
    return false;
}

float vlc_player::get_rate()
{
    if( !is_open() )
        return 1.f;

208
    return _mp.rate();
209 210 211 212 213 214 215
}

void vlc_player::set_rate(float rate)
{
    if( !is_open() )
        return;

216
    _mp.setRate( rate );
217 218 219 220 221 222 223
}

float vlc_player::get_fps()
{
    if( !is_open() )
        return 0;

224
    return _mp.fps();
225 226 227 228 229 230 231
}

bool vlc_player::has_vout()
{
    if( !is_open() )
        return false;

232
    return _mp.hasVout() > 0;
233 234 235 236 237 238 239
}

float vlc_player::get_position()
{
    if( !is_open() )
        return 0.f;

240 241
    float p = _mp.position();
    return p < 0 ? 0.f : p;
242 243 244 245 246 247 248
}

void vlc_player::set_position(float p)
{
    if( !is_open() )
        return;

249
    _mp.setPosition( p );
250 251 252 253 254 255 256
}

libvlc_time_t vlc_player::get_time()
{
    if( !is_open() )
        return 0;

257
    auto t = _mp.time();
258

259
    return t < 0 ? 0 : t;
260 261 262 263 264 265 266
}

void vlc_player::set_time(libvlc_time_t t)
{
    if( !is_open() )
        return;

267
    _mp.setTime( t );
268 269 270 271 272 273 274
}

libvlc_time_t vlc_player::get_length()
{
    if( !is_open() )
        return 0;

275 276
    auto t = _mp.length();
    return t < 0 ? 0 : t;
277 278 279 280 281
}

void vlc_player::set_mode(libvlc_playback_mode_t mode)
{
    if( is_open() )
282
        _ml_p.setPlaybackMode( mode );
283 284 285 286 287 288 289
}

bool vlc_player::is_muted()
{
    if( !is_open() )
        return false;

290
    return _mp.mute();
291 292 293 294 295
}

void vlc_player::toggle_mute()
{
    if( is_open() )
296
        _mp.toggleMute();
297 298 299 300 301
}

void vlc_player::set_mute(bool mute)
{
    if( is_open() )
302
        _mp.setMute( mute );
303 304 305 306 307 308 309
}

unsigned int vlc_player::get_volume()
{
    if( !is_open() )
        return 0;

310 311
    int v = _mp.volume();
    return v < 0 ? 0 : v;
312 313 314 315 316
}

void vlc_player::set_volume(unsigned int volume)
{
    if( is_open() )
317
        _mp.setVolume( volume );
318 319 320 321 322 323 324
}

unsigned int vlc_player::track_count()
{
    if( !is_open() )
        return 0;

325
    int tc = _mp.audioTrackCount();
326 327 328 329 330 331 332 333 334 335 336

    return tc<0 ? 0 : tc ;
}

unsigned int vlc_player::get_track()
{
    if( !is_open() )
        return 0;

    int t = libvlc_audio_get_track(_mp);

337
    return t < 0 ? 0 : t;
338 339 340 341 342
}

void vlc_player::set_track(unsigned int track)
{
    if( is_open() )
343
        _mp.setAudioTrack( track );
344 345 346 347 348 349 350
}

unsigned int vlc_player::get_channel()
{
    if( !is_open() )
        return 0;

351
    return _mp.channel();
352 353 354 355 356
}

void vlc_player::set_channel(unsigned int channel)
{
    if( is_open() )
357
        _mp.setChannel( channel );
358
}