playermanager.cpp 12.6 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 "tag.h"
30

31 32
#include "config.h"

33
using namespace ActionCollection;
34 35 36

PlayerManager *PlayerManager::m_instance = 0;

37 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
////////////////////////////////////////////////////////////////////////////////
// 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;
}

64 65 66 67 68
////////////////////////////////////////////////////////////////////////////////
// protected members
////////////////////////////////////////////////////////////////////////////////

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    return m_file.property(property);
}

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

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

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

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

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

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

213 214 215 216
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

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

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

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

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

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

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

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

    m_timer->start(m_pollInterval);
257 258

    emit signalPlay();
259 260
}

Scott Wheeler's avatar
Scott Wheeler committed
261 262 263 264 265
void PlayerManager::play()
{
    play(FileHandle::null());
}

266 267
void PlayerManager::pause()
{
268
    if(!player())
269 270
        return;

271 272 273 274 275 276
    if(player()->paused()) {
        play();
        return;
    }

    m_timer->stop();
277
    action("pause")->setEnabled(false);
278 279

    player()->pause();
280 281

    emit signalPause();
282 283 284 285
}

void PlayerManager::stop()
{
286
    if(!player())
287 288
        return;

289 290
    m_timer->stop();

291 292 293 294
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
295 296 297 298 299

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

    player()->stop();
300
    m_playlistInterface->stop();
301

302 303
    m_file = FileHandle::null();

304
    emit signalStop();
305 306
}

Laurent Montel's avatar
Laurent Montel committed
307
void PlayerManager::setVolume(float volume)
308
{
309
    if(!player())
310 311
        return;

312
    player()->setVolume(volume);
313 314
}

Scott Wheeler's avatar
Scott Wheeler committed
315
void PlayerManager::seek(int seekTime)
316
{
317
    if(!player())
318
        return;
319

320
    player()->seek(seekTime);
321 322
}

323
void PlayerManager::seekPosition(int position)
324
{
325 326 327 328
    if(!player())
        return;

    if(!player()->playing() || m_noSeek)
329
        return;
330

331 332
    slotUpdateTime(position);
    player()->seekPosition(position);
333 334 335 336 337 338 339 340 341 342 343 344 345
    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));
346 347
}

348 349
void PlayerManager::playPause()
{
350
    playing() ? action("pause")->activate() : action("play")->activate();
351 352
}

353 354
void PlayerManager::forward()
{
355 356
    m_playlistInterface->playNext();
    FileHandle file = m_playlistInterface->currentFile();
357

358 359 360 361 362 363 364 365
    if(!file.isNull())
        play(file);
    else
        stop();
}

void PlayerManager::back()
{
366 367
    m_playlistInterface->playPrevious();
    FileHandle file = m_playlistInterface->currentFile();
368

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

375 376
void PlayerManager::volumeUp()
{
377
    if(!player() || !m_sliderAction)
378
        return;
379

380
    int volume = m_sliderAction->volumeSlider()->volume() +
381
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% up
382 383

    slotSetVolume(volume);
384
    m_sliderAction->volumeSlider()->setVolume(volume);
385 386 387 388
}

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

    int volume = m_sliderAction->volumeSlider()->value() -
393
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% down
394 395

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

void PlayerManager::mute()
{
401
    if(!player() || !m_sliderAction)
402 403 404 405 406 407
        return;

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

408 409 410
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
411

412
void PlayerManager::slotPollPlay()
413
{
414
    if(!player() || !m_playlistInterface)
415
        return;
416

417
    m_noSeek = true;
418

419
    if(!player()->playing()) {
420
        m_timer->stop();
421

422 423
        m_playlistInterface->playNext();
        FileHandle nextFile = m_playlistInterface->currentFile();
424
        if(!nextFile.isNull())
425
            play(nextFile);
426 427
        else
            stop();
428 429
    }
    else if(!m_sliderAction->dragging()) {
430
        m_sliderAction->trackPositionSlider()->setValue(player()->position());
431

432 433 434 435
        if(m_statusLabel) {
            m_statusLabel->setItemTotalTime(player()->totalTime());
            m_statusLabel->setItemCurrentTime(player()->currentTime());
        }
436
    }
437

438 439 440 441
    // 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.
442

443 444 445
    if(player()->playing() &&
       player()->totalTime() > 0 &&
       float(player()->totalTime() - player()->currentTime()) < m_pollInterval * 2)
446 447 448
    {
        m_timer->changeInterval(50);
    }
449

450
    m_noSeek = false;
451 452
}

453 454 455 456
void PlayerManager::slotSetOutput(int system)
{
    stop();
    delete m_player;
457
    m_player = createPlayer(system);
458 459 460 461 462 463 464 465 466 467 468 469 470
}

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();
471
    float totalTime = float(player()->totalTime());
Scott Wheeler's avatar
Scott Wheeler committed
472
    int seekTime = int(positionFraction * totalTime + 0.5); // "+0.5" for rounding
473 474 475 476

    m_statusLabel->setItemCurrentTime(seekTime);
}

477 478 479 480
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

481 482 483 484 485 486 487 488
Player *PlayerManager::player() const
{
    if(!m_player)
        instance()->setup();

    return m_player;
}

489 490 491 492
void PlayerManager::setup()
{
    // All of the actions required by this class should be listed here.

493 494 495 496 497
    if(!action("pause") ||
       !action("stop") ||
       !action("back") ||
       !action("forward") ||
       !action("trackPositionAction"))
498 499 500 501 502
    {
        kdWarning(65432) << k_funcinfo << "Could not find all of the required actions." << endl;
        return;
    }

503 504
    // initialize action states

505 506 507 508
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
509

510 511
    // setup sliders

512
    m_sliderAction = action<SliderAction>("trackPositionAction");
513

514 515 516 517
    connect(m_sliderAction, SIGNAL(signalPositionChanged(int)),
            this, SLOT(seekPosition(int)));
    connect(m_sliderAction->trackPositionSlider(), SIGNAL(valueChanged(int)),
            this, SLOT(slotUpdateTime(int)));
518
    connect(m_sliderAction->volumeSlider(), SIGNAL(signalVolumeChanged(int)),
519 520
            this, SLOT(slotSetVolume(int)));

521
    KAction *outputAction = action("outputSelect");
522 523 524

    if(outputAction) {
        int mediaSystem = static_cast<KSelectAction *>(outputAction)->currentItem();
525
        m_player = createPlayer(mediaSystem);
Scott Wheeler's avatar
Scott Wheeler committed
526
        connect(outputAction, SIGNAL(activated(int)), this, SLOT(slotSetOutput(int)));
527 528
    }
    else
529
        m_player = createPlayer();
530

531
    float volume =
532
        float(m_sliderAction->volumeSlider()->volume()) /
533
        float(m_sliderAction->volumeSlider()->maxValue());
534 535 536

    m_player->setVolume(volume);

537 538
    m_timer = new QTimer(this, "play timer");
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotPollPlay()));
539 540 541
}

#include "playermanager.moc"