playermanager.cpp 12.8 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 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
////////////////////////////////////////////////////////////////////////////////
// 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;
}

65 66 67 68 69
////////////////////////////////////////////////////////////////////////////////
// protected members
////////////////////////////////////////////////////////////////////////////////

PlayerManager::PlayerManager() :
Scott Wheeler's avatar
Scott Wheeler committed
70
    Player(),
71
    m_sliderAction(0),
72
    m_playlistInterface(0),
73
    m_statusLabel(0),
74 75
    m_player(0),
    m_timer(0),
76 77
    m_noSeek(false),
    m_muted(false)
78 79 80 81 82 83
{
    setup();
}

PlayerManager::~PlayerManager()
{
84
    delete m_player;
85 86 87 88 89 90 91 92 93 94 95 96 97
}

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

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

98 99
bool PlayerManager::playing() const
{
100
    if(!player())
101 102
        return false;

103
    return player()->playing();
104 105 106 107
}

bool PlayerManager::paused() const
{
108
    if(!player())
109 110
        return false;

111
    return player()->paused();
112 113 114 115
}

float PlayerManager::volume() const
{
116
    if(!player())
117 118
        return 0;

119
    return player()->volume();
120 121
}

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
int PlayerManager::status() const
{
    // DCOP call to interface better with the Media Control applet
    // Constant values provided by mETz.
    // mETz: You can thank me later. :-)

    if(!player())
        return -1;

    if(player()->paused())
        return 1;

    if(player()->playing())
        return 2;
    
    return 0;
}

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

145
    return player()->totalTime();
146 147
}

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

153
    return player()->currentTime();
154 155 156 157
}

int PlayerManager::position() const
{
158
    if(!player())
159 160
        return 0;

161
    return player()->position();
162 163
}

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

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

    return m_file.property(property);
}

177 178 179 180 181
FileHandle PlayerManager::playingFile() const
{
    return m_file;
}

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

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

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

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

200 201 202 203 204 205 206 207 208 209 210 211 212 213
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;
}

214 215 216 217
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

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

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

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

243
    // Make sure that the player() actually starts before doing anything.
244

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

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

255
    m_sliderAction->trackPositionSlider()->setEnabled(true);
256 257

    m_timer->start(m_pollInterval);
258 259

    emit signalPlay();
260 261
}

262 263 264 265 266 267 268 269 270
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
271 272 273 274 275
void PlayerManager::play()
{
    play(FileHandle::null());
}

276 277
void PlayerManager::pause()
{
278
    if(!player())
279 280
        return;

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

    m_timer->stop();
287
    action("pause")->setEnabled(false);
288 289

    player()->pause();
290 291

    emit signalPause();
292 293 294 295
}

void PlayerManager::stop()
{
296
    if(!player())
297 298
        return;

299 300
    m_timer->stop();

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

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

    player()->stop();
310
    m_playlistInterface->stop();
311

312 313
    m_file = FileHandle::null();

314
    emit signalStop();
315 316
}

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

322
    player()->setVolume(volume);
323 324
}

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

330
    player()->seek(seekTime);
331 332
}

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

    if(!player()->playing() || m_noSeek)
339
        return;
340

341 342
    slotUpdateTime(position);
    player()->seekPosition(position);
343 344 345 346 347 348 349 350 351 352 353 354 355
    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));
356 357
}

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

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

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

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

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

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

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

    slotSetVolume(volume);
394
    m_sliderAction->volumeSlider()->setVolume(volume);
395 396 397 398
}

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

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

    slotSetVolume(volume);
406
    m_sliderAction->volumeSlider()->setVolume(volume);
407 408 409 410
}

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

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

418 419 420
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
421

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

427
    m_noSeek = true;
428

429
    if(!player()->playing()) {
430
        m_timer->stop();
431

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

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

448 449 450 451
    // 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.
452

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

460
    m_noSeek = false;
461 462
}

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

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();
481
    float totalTime = float(player()->totalTime());
Scott Wheeler's avatar
Scott Wheeler committed
482
    int seekTime = int(positionFraction * totalTime + 0.5); // "+0.5" for rounding
483 484 485 486

    m_statusLabel->setItemCurrentTime(seekTime);
}

487 488 489 490
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

491 492 493 494 495 496 497 498
Player *PlayerManager::player() const
{
    if(!m_player)
        instance()->setup();

    return m_player;
}

499 500 501 502
void PlayerManager::setup()
{
    // All of the actions required by this class should be listed here.

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

513 514
    // initialize action states

515 516 517 518
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
519

520 521
    // setup sliders

522
    m_sliderAction = action<SliderAction>("trackPositionAction");
523

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

531
    KAction *outputAction = action("outputSelect");
532 533 534

    if(outputAction) {
        int mediaSystem = static_cast<KSelectAction *>(outputAction)->currentItem();
535
        m_player = createPlayer(mediaSystem);
Scott Wheeler's avatar
Scott Wheeler committed
536
        connect(outputAction, SIGNAL(activated(int)), this, SLOT(slotSetOutput(int)));
537 538
    }
    else
539
        m_player = createPlayer();
540

541
    float volume =
542
        float(m_sliderAction->volumeSlider()->volume()) /
543
        float(m_sliderAction->volumeSlider()->maxValue());
544 545 546

    m_player->setVolume(volume);

547 548
    m_timer = new QTimer(this, "play timer");
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotPollPlay()));
549 550 551
}

#include "playermanager.moc"