playermanager.cpp 13.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/***************************************************************************
    begin                : Sat Feb 14 2004
    copyright            : (C) 2004 by Scott Wheeler
    email                : wheeler@kde.org
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <kdebug.h>
17
#include <klocale.h>
18 19

#include <qslider.h>
20
#include <qtimer.h>
21

22 23
#include "artsplayer.h"
#include "gstreamerplayer.h"
24
#include "playermanager.h"
25
#include "playlistinterface.h"
26
#include "slideraction.h"
27
#include "statuslabel.h"
28
#include "actioncollection.h"
29
#include "collectionlist.h"
30
#include "tag.h"
31

32 33
#include "config.h"

34
using namespace ActionCollection;
35 36 37

PlayerManager *PlayerManager::m_instance = 0;

38 39
enum Status { StatusStopped = -1, StatusPaused = 1, StatusPlaying = 2 };

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
////////////////////////////////////////////////////////////////////////////////
// helper functions
////////////////////////////////////////////////////////////////////////////////

enum SoundSystem { ArtsBackend = 0, GStreamerBackend = 1 };

static Player *createPlayer(int system = ArtsBackend)
{

    Player *p = 0;
#if HAVE_GSTREAMER
    switch(system) {
    case ArtsBackend:
        p = new ArtsPlayer;
        break;
    case GStreamerBackend:
        p = new GStreamerPlayer;
        break;
    }
#else
    Q_UNUSED(system);
    p = new ArtsPlayer;
#endif

    return p;
}

67 68 69 70 71
////////////////////////////////////////////////////////////////////////////////
// protected members
////////////////////////////////////////////////////////////////////////////////

PlayerManager::PlayerManager() :
Scott Wheeler's avatar
Scott Wheeler committed
72
    Player(),
73
    m_sliderAction(0),
74
    m_playlistInterface(0),
75
    m_statusLabel(0),
76 77
    m_player(0),
    m_timer(0),
78 79
    m_noSeek(false),
    m_muted(false)
80
{
Michael Pyne's avatar
Michael Pyne committed
81 82 83 84 85
// This class is the first thing constructed during program startup, and
// therefore has no access to the widgets needed by the setup() method.
// Since the setup() method will be called indirectly by the player() method
// later, just disable it here. -- mpyne
//    setup();
86 87 88 89
}

PlayerManager::~PlayerManager()
{
90
    delete m_player;
91 92 93 94 95 96 97 98 99 100 101 102 103
}

////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////

PlayerManager *PlayerManager::instance() // static
{
    if(!m_instance)
        m_instance = new PlayerManager;
    return m_instance;
}

104 105
bool PlayerManager::playing() const
{
106
    if(!player())
107 108
        return false;

109
    return player()->playing();
110 111 112 113
}

bool PlayerManager::paused() const
{
114
    if(!player())
115 116
        return false;

117
    return player()->paused();
118 119 120 121
}

float PlayerManager::volume() const
{
122
    if(!player())
123 124
        return 0;

125
    return player()->volume();
126 127
}

128 129 130
int PlayerManager::status() const
{
    if(!player())
131
        return StatusStopped;
132 133

    if(player()->paused())
134
        return StatusPaused;
135 136

    if(player()->playing())
137
        return StatusPlaying;
138 139 140 141
    
    return 0;
}

Scott Wheeler's avatar
Scott Wheeler committed
142
int PlayerManager::totalTime() const
143
{
144
    if(!player())
145 146
        return 0;

147
    return player()->totalTime();
148 149
}

Scott Wheeler's avatar
Scott Wheeler committed
150
int PlayerManager::currentTime() const
151
{
152
    if(!player())
153 154
        return 0;

155
    return player()->currentTime();
156 157 158 159
}

int PlayerManager::position() const
{
160
    if(!player())
161 162
        return 0;

163
    return player()->position();
164 165
}

166 167 168 169 170 171 172 173 174 175 176 177 178
QStringList PlayerManager::trackProperties()
{
    return FileHandle::properties();
}

QString PlayerManager::trackProperty(const QString &property) const
{
    if(!playing())
        return QString::null;

    return m_file.property(property);
}

179 180 181 182 183
FileHandle PlayerManager::playingFile() const
{
    return m_file;
}

184 185 186 187 188 189 190 191
QString PlayerManager::playingString() const
{
    if(!playing())
        return QString::null;

    return m_file.tag()->artist() + " - " + m_file.tag()->title();
}

192
void PlayerManager::setPlaylistInterface(PlaylistInterface *interface)
193 194 195 196
{
    m_playlistInterface = interface;
}

197 198 199 200 201
void PlayerManager::setStatusLabel(StatusLabel *label)
{
    m_statusLabel = label;
}

202 203 204 205 206 207 208 209 210 211 212 213 214 215
KSelectAction *PlayerManager::playerSelectAction(QObject *parent) // static
{
    KSelectAction *action = 0;
#if HAVE_GSTREAMER
    action = new KSelectAction(i18n("&Output To"), 0, parent, "outputSelect");
    QStringList l;
    l << "aRts" << "GStreamer";
    action->setItems(l);
#else
    Q_UNUSED(parent);
#endif
    return action;
}

216 217 218 219
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

220
void PlayerManager::play(const FileHandle &file)
221
{
222
    if(!player() || !m_playlistInterface)
223 224
        return;

225 226
    if(file.isNull()) {
        if(player()->paused())
227
            player()->play();
228
        else if(player()->playing()) {
229
            m_sliderAction->trackPositionSlider()->setValue(0);
230
            player()->seekPosition(0);
231
        }
232
        else {
233
	    m_playlistInterface->playNext();
234
            m_file = m_playlistInterface->currentFile();
235

236
            if(!m_file.isNull())
237
                player()->play(m_file);
238
        }
239
    }
240
    else {
241
        m_file = file;
242
        player()->play(file);
243
    }
244

245
    // Make sure that the player() actually starts before doing anything.
246

247
    if(!player()->playing()) {
248 249 250
        stop();
        return;
    }
251

252 253 254 255
    action("pause")->setEnabled(true);
    action("stop")->setEnabled(true);
    action("forward")->setEnabled(true);
    action("back")->setEnabled(true);
256

257
    m_sliderAction->trackPositionSlider()->setEnabled(true);
258 259

    m_timer->start(m_pollInterval);
260 261

    emit signalPlay();
262 263
}

264 265 266 267 268 269 270 271 272
void PlayerManager::play(const QString &file)
{
    CollectionListItem *item = CollectionList::instance()->lookup(file);
    if(item) {
        Playlist::setPlaying(item);
        play(item->file());
    }
}

Scott Wheeler's avatar
Scott Wheeler committed
273 274 275 276 277
void PlayerManager::play()
{
    play(FileHandle::null());
}

278 279
void PlayerManager::pause()
{
280
    if(!player())
281 282
        return;

283 284 285 286 287 288
    if(player()->paused()) {
        play();
        return;
    }

    m_timer->stop();
289
    action("pause")->setEnabled(false);
290 291

    player()->pause();
292 293

    emit signalPause();
294 295 296 297
}

void PlayerManager::stop()
{
298
    if(!player() || !m_playlistInterface)
299 300
        return;

301 302
    m_timer->stop();

303 304 305 306
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
307 308 309 310 311

    m_sliderAction->trackPositionSlider()->setValue(0);
    m_sliderAction->trackPositionSlider()->setEnabled(false);

    player()->stop();
312
    m_playlistInterface->stop();
313

314 315
    m_file = FileHandle::null();

316
    emit signalStop();
317 318
}

Laurent Montel's avatar
Laurent Montel committed
319
void PlayerManager::setVolume(float volume)
320
{
321
    if(!player())
322 323
        return;

324
    player()->setVolume(volume);
325 326
}

Scott Wheeler's avatar
Scott Wheeler committed
327
void PlayerManager::seek(int seekTime)
328
{
329
    if(!player())
330
        return;
331

332
    player()->seek(seekTime);
333 334
}

335
void PlayerManager::seekPosition(int position)
336
{
337 338 339 340
    if(!player())
        return;

    if(!player()->playing() || m_noSeek)
341
        return;
342

343 344
    slotUpdateTime(position);
    player()->seekPosition(position);
345 346 347 348 349 350 351 352 353 354 355 356 357
    m_sliderAction->trackPositionSlider()->setValue(position);
}

void PlayerManager::seekForward()
{
    int position = m_sliderAction->trackPositionSlider()->value();
    seekPosition(kMin(m_sliderAction->trackPositionSlider()->maxValue(), position + 10));
}

void PlayerManager::seekBack()
{
    int position = m_sliderAction->trackPositionSlider()->value();
    seekPosition(kMax(m_sliderAction->trackPositionSlider()->minValue(), position - 10));
358 359
}

360 361
void PlayerManager::playPause()
{
362
    playing() ? action("pause")->activate() : action("play")->activate();
363 364
}

365 366
void PlayerManager::forward()
{
367 368
    m_playlistInterface->playNext();
    FileHandle file = m_playlistInterface->currentFile();
369

370 371 372 373 374 375 376 377
    if(!file.isNull())
        play(file);
    else
        stop();
}

void PlayerManager::back()
{
378 379
    m_playlistInterface->playPrevious();
    FileHandle file = m_playlistInterface->currentFile();
380

381 382 383 384 385 386
    if(!file.isNull())
        play(file);
    else
        stop();
}

387 388
void PlayerManager::volumeUp()
{
389
    if(!player() || !m_sliderAction)
390
        return;
391

392
    int volume = m_sliderAction->volumeSlider()->volume() +
393
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% up
394 395

    slotSetVolume(volume);
396
    m_sliderAction->volumeSlider()->setVolume(volume);
397 398 399 400
}

void PlayerManager::volumeDown()
{
401
    if(!player() || !m_sliderAction)
402
        return;
403 404

    int volume = m_sliderAction->volumeSlider()->value() -
405
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% down
406 407

    slotSetVolume(volume);
408
    m_sliderAction->volumeSlider()->setVolume(volume);
409 410 411 412
}

void PlayerManager::mute()
{
413
    if(!player() || !m_sliderAction)
414 415 416 417 418 419
        return;

    slotSetVolume(m_muted ? m_sliderAction->volumeSlider()->value() : 0);
    m_muted = !m_muted;
}

420 421 422
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
423

424
void PlayerManager::slotPollPlay()
425
{
426
    if(!player() || !m_playlistInterface)
427
        return;
428

429
    m_noSeek = true;
430

431
    if(!player()->playing()) {
432
        m_timer->stop();
433

434 435
        m_playlistInterface->playNext();
        FileHandle nextFile = m_playlistInterface->currentFile();
436
        if(!nextFile.isNull())
437
            play(nextFile);
438 439
        else
            stop();
440 441
    }
    else if(!m_sliderAction->dragging()) {
442
        m_sliderAction->trackPositionSlider()->setValue(player()->position());
443

444 445 446 447
        if(m_statusLabel) {
            m_statusLabel->setItemTotalTime(player()->totalTime());
            m_statusLabel->setItemCurrentTime(player()->currentTime());
        }
448
    }
449

450 451 452 453
    // Ok, this is weird stuff, but it works pretty well.  Ordinarily we don't
    // need to check up on our playing time very often, but in the span of the
    // last interval, we want to check a lot -- to figure out that we've hit the
    // end of the song as soon as possible.
454

455 456 457
    if(player()->playing() &&
       player()->totalTime() > 0 &&
       float(player()->totalTime() - player()->currentTime()) < m_pollInterval * 2)
458 459 460
    {
        m_timer->changeInterval(50);
    }
461

462
    m_noSeek = false;
463 464
}

465 466 467 468
void PlayerManager::slotSetOutput(int system)
{
    stop();
    delete m_player;
469
    m_player = createPlayer(system);
470
    setup();
471 472 473 474 475 476 477 478 479 480 481 482 483
}

void PlayerManager::slotSetVolume(int volume)
{
    setVolume(float(volume) / float(m_sliderAction->volumeSlider()->maxValue()));
}

void PlayerManager::slotUpdateTime(int position)
{
    if(!m_statusLabel)
        return;

    float positionFraction = float(position) / m_sliderAction->trackPositionSlider()->maxValue();
484
    float totalTime = float(player()->totalTime());
Scott Wheeler's avatar
Scott Wheeler committed
485
    int seekTime = int(positionFraction * totalTime + 0.5); // "+0.5" for rounding
486 487 488 489

    m_statusLabel->setItemCurrentTime(seekTime);
}

490 491 492 493
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

494 495 496 497 498 499 500 501
Player *PlayerManager::player() const
{
    if(!m_player)
        instance()->setup();

    return m_player;
}

502 503 504 505
void PlayerManager::setup()
{
    // All of the actions required by this class should be listed here.

506 507 508 509 510
    if(!action("pause") ||
       !action("stop") ||
       !action("back") ||
       !action("forward") ||
       !action("trackPositionAction"))
511 512 513 514 515
    {
        kdWarning(65432) << k_funcinfo << "Could not find all of the required actions." << endl;
        return;
    }

516 517
    // initialize action states

518 519 520 521
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
522

523 524
    // setup sliders

525
    m_sliderAction = action<SliderAction>("trackPositionAction");
526

527 528 529 530
    connect(m_sliderAction, SIGNAL(signalPositionChanged(int)),
            this, SLOT(seekPosition(int)));
    connect(m_sliderAction->trackPositionSlider(), SIGNAL(valueChanged(int)),
            this, SLOT(slotUpdateTime(int)));
531
    connect(m_sliderAction->volumeSlider(), SIGNAL(signalVolumeChanged(int)),
532 533
            this, SLOT(slotSetVolume(int)));

534 535 536
    // Call this method manually to avoid warnings.
    
    KAction *outputAction = actions()->action("outputSelect");
537 538 539

    if(outputAction) {
        int mediaSystem = static_cast<KSelectAction *>(outputAction)->currentItem();
540
        m_player = createPlayer(mediaSystem);
Scott Wheeler's avatar
Scott Wheeler committed
541
        connect(outputAction, SIGNAL(activated(int)), this, SLOT(slotSetOutput(int)));
542 543
    }
    else
544
        m_player = createPlayer();
545

546
    float volume =
547
        float(m_sliderAction->volumeSlider()->volume()) /
548
        float(m_sliderAction->volumeSlider()->maxValue());
549 550 551

    m_player->setVolume(volume);

552 553
    m_timer = new QTimer(this, "play timer");
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotPollPlay()));
554 555 556
}

#include "playermanager.moc"