playermanager.cpp 13.2 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
{
Michael Pyne's avatar
Michael Pyne committed
79 80 81 82 83
// 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();
84 85 86 87
}

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

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

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

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

107
    return player()->playing();
108 109 110 111
}

bool PlayerManager::paused() const
{
112
    if(!player())
113 114
        return false;

115
    return player()->paused();
116 117 118 119
}

float PlayerManager::volume() const
{
120
    if(!player())
121 122
        return 0;

123
    return player()->volume();
124 125
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
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
144
int PlayerManager::totalTime() const
145
{
146
    if(!player())
147 148
        return 0;

149
    return player()->totalTime();
150 151
}

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

157
    return player()->currentTime();
158 159 160 161
}

int PlayerManager::position() const
{
162
    if(!player())
163 164
        return 0;

165
    return player()->position();
166 167
}

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

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

    return m_file.property(property);
}

181 182 183 184 185
FileHandle PlayerManager::playingFile() const
{
    return m_file;
}

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

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

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

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

204 205 206 207 208 209 210 211 212 213 214 215 216 217
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;
}

218 219 220 221
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

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

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

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

247
    // Make sure that the player() actually starts before doing anything.
248

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

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

259
    m_sliderAction->trackPositionSlider()->setEnabled(true);
260 261

    m_timer->start(m_pollInterval);
262 263

    emit signalPlay();
264 265
}

266 267 268 269 270 271 272 273 274
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
275 276 277 278 279
void PlayerManager::play()
{
    play(FileHandle::null());
}

280 281
void PlayerManager::pause()
{
282
    if(!player())
283 284
        return;

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

    m_timer->stop();
291
    action("pause")->setEnabled(false);
292 293

    player()->pause();
294 295

    emit signalPause();
296 297 298 299
}

void PlayerManager::stop()
{
300
    if(!player() || !m_playlistInterface)
301 302
        return;

303 304
    m_timer->stop();

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

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

    player()->stop();
314
    m_playlistInterface->stop();
315

316 317
    m_file = FileHandle::null();

318
    emit signalStop();
319 320
}

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

326
    player()->setVolume(volume);
327 328
}

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

334
    player()->seek(seekTime);
335 336
}

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

    if(!player()->playing() || m_noSeek)
343
        return;
344

345 346
    slotUpdateTime(position);
    player()->seekPosition(position);
347 348 349 350 351 352 353 354 355 356 357 358 359
    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));
360 361
}

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

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

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

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

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

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

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

    slotSetVolume(volume);
398
    m_sliderAction->volumeSlider()->setVolume(volume);
399 400 401 402
}

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

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

    slotSetVolume(volume);
410
    m_sliderAction->volumeSlider()->setVolume(volume);
411 412 413 414
}

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

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

422 423 424
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
425

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

431
    m_noSeek = true;
432

433
    if(!player()->playing()) {
434
        m_timer->stop();
435

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

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

452 453 454 455
    // 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.
456

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

464
    m_noSeek = false;
465 466
}

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

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();
486
    float totalTime = float(player()->totalTime());
Scott Wheeler's avatar
Scott Wheeler committed
487
    int seekTime = int(positionFraction * totalTime + 0.5); // "+0.5" for rounding
488 489 490 491

    m_statusLabel->setItemCurrentTime(seekTime);
}

492 493 494 495
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

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

    return m_player;
}

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

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

518 519
    // initialize action states

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

525 526
    // setup sliders

527
    m_sliderAction = action<SliderAction>("trackPositionAction");
528

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

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

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

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

    m_player->setVolume(volume);

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

#include "playermanager.moc"