Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

playermanager.cpp 13.4 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
////////////////////////////////////////////////////////////////////////////////
// helper functions
////////////////////////////////////////////////////////////////////////////////

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

static Player *createPlayer(int system = ArtsBackend)
{

    Player *p = 0;
50 51

#if defined(HAVE_ARTS) && defined(HAVE_GSTREAMER)
52 53 54 55 56 57 58
    switch(system) {
    case ArtsBackend:
        p = new ArtsPlayer;
        break;
    case GStreamerBackend:
        p = new GStreamerPlayer;
        break;
59 60 61
    default:
        p = new ArtsPlayer;
        break;
62 63
    }
#else
64 65
    Q_UNUSED(system)
#ifdef HAVE_ARTS
66 67 68
    p = new ArtsPlayer;
#endif

69 70 71 72 73 74 75
#ifdef HAVE_GSTREAMER
    p = new GStreamerPlayer;
#else
#warning No Player Backend Available
#endif
#endif

76 77 78
    return p;
}

79 80 81 82 83
////////////////////////////////////////////////////////////////////////////////
// protected members
////////////////////////////////////////////////////////////////////////////////

PlayerManager::PlayerManager() :
Scott Wheeler's avatar
Scott Wheeler committed
84
    Player(),
85
    m_sliderAction(0),
86
    m_playlistInterface(0),
87
    m_statusLabel(0),
88 89
    m_player(0),
    m_timer(0),
90 91
    m_noSeek(false),
    m_muted(false)
92
{
Michael Pyne's avatar
Michael Pyne committed
93 94 95 96 97
// 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();
98 99 100 101
}

PlayerManager::~PlayerManager()
{
102
    delete m_player;
103 104 105 106 107 108 109 110 111 112 113 114 115
}

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

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

116 117
bool PlayerManager::playing() const
{
118
    if(!player())
119 120
        return false;

121
    return player()->playing();
122 123 124 125
}

bool PlayerManager::paused() const
{
126
    if(!player())
127 128
        return false;

129
    return player()->paused();
130 131 132 133
}

float PlayerManager::volume() const
{
134
    if(!player())
135 136
        return 0;

137
    return player()->volume();
138 139
}

140 141 142
int PlayerManager::status() const
{
    if(!player())
143
        return StatusStopped;
144 145

    if(player()->paused())
146
        return StatusPaused;
147 148

    if(player()->playing())
149
        return StatusPlaying;
150 151 152 153
    
    return 0;
}

Scott Wheeler's avatar
Scott Wheeler committed
154
int PlayerManager::totalTime() const
155
{
156
    if(!player())
157 158
        return 0;

159
    return player()->totalTime();
160 161
}

Scott Wheeler's avatar
Scott Wheeler committed
162
int PlayerManager::currentTime() const
163
{
164
    if(!player())
165 166
        return 0;

167
    return player()->currentTime();
168 169 170 171
}

int PlayerManager::position() const
{
172
    if(!player())
173 174
        return 0;

175
    return player()->position();
176 177
}

178 179 180 181 182 183 184 185 186 187 188 189 190
QStringList PlayerManager::trackProperties()
{
    return FileHandle::properties();
}

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

    return m_file.property(property);
}

191 192 193 194 195
FileHandle PlayerManager::playingFile() const
{
    return m_file;
}

196 197 198 199 200 201 202 203
QString PlayerManager::playingString() const
{
    if(!playing())
        return QString::null;

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

204
void PlayerManager::setPlaylistInterface(PlaylistInterface *interface)
205 206 207 208
{
    m_playlistInterface = interface;
}

209 210 211 212 213
void PlayerManager::setStatusLabel(StatusLabel *label)
{
    m_statusLabel = label;
}

214 215 216 217 218
KSelectAction *PlayerManager::playerSelectAction(QObject *parent) // static
{
    KSelectAction *action = 0;
    action = new KSelectAction(i18n("&Output To"), 0, parent, "outputSelect");
    QStringList l;
219 220 221

#if defined(HAVE_ARTS) && defined(HAVE_GSTREAMER)
    l << i18n("aRts") << i18n("GStreamer");
222 223
    action->setItems(l);
#else
224
    Q_UNUSED(parent)
225 226 227 228
#endif
    return action;
}

229 230 231 232
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

233
void PlayerManager::play(const FileHandle &file)
234
{
235
    if(!player() || !m_playlistInterface)
236 237
        return;

238 239
    if(file.isNull()) {
        if(player()->paused())
240
            player()->play();
241
        else if(player()->playing()) {
242
            m_sliderAction->trackPositionSlider()->setValue(0);
243
            player()->seekPosition(0);
244
        }
245
        else {
246
	    m_playlistInterface->playNext();
247
            m_file = m_playlistInterface->currentFile();
248

249
            if(!m_file.isNull())
250
                player()->play(m_file);
251
        }
252
    }
253
    else {
254
        m_file = file;
255
        player()->play(file);
256
    }
257

258
    // Make sure that the player() actually starts before doing anything.
259

260
    if(!player()->playing()) {
261 262 263
        stop();
        return;
    }
264

265 266 267 268
    action("pause")->setEnabled(true);
    action("stop")->setEnabled(true);
    action("forward")->setEnabled(true);
    action("back")->setEnabled(true);
269

270
    m_sliderAction->trackPositionSlider()->setEnabled(true);
271 272

    m_timer->start(m_pollInterval);
273 274

    emit signalPlay();
275 276
}

277 278 279 280 281 282 283 284 285
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
286 287 288 289 290
void PlayerManager::play()
{
    play(FileHandle::null());
}

291 292
void PlayerManager::pause()
{
293
    if(!player())
294 295
        return;

296 297 298 299 300 301
    if(player()->paused()) {
        play();
        return;
    }

    m_timer->stop();
302
    action("pause")->setEnabled(false);
303 304

    player()->pause();
305 306

    emit signalPause();
307 308 309 310
}

void PlayerManager::stop()
{
311
    if(!player() || !m_playlistInterface)
312 313
        return;

314 315
    m_timer->stop();

316 317 318 319
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
320 321 322 323 324

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

    player()->stop();
325
    m_playlistInterface->stop();
326

327 328
    m_file = FileHandle::null();

329
    emit signalStop();
330 331
}

Laurent Montel's avatar
Laurent Montel committed
332
void PlayerManager::setVolume(float volume)
333
{
334
    if(!player())
335 336
        return;

337
    player()->setVolume(volume);
338 339
}

Scott Wheeler's avatar
Scott Wheeler committed
340
void PlayerManager::seek(int seekTime)
341
{
342
    if(!player())
343
        return;
344

345
    player()->seek(seekTime);
346 347
}

348
void PlayerManager::seekPosition(int position)
349
{
350 351 352 353
    if(!player())
        return;

    if(!player()->playing() || m_noSeek)
354
        return;
355

356 357
    slotUpdateTime(position);
    player()->seekPosition(position);
358 359 360 361 362 363 364 365 366 367 368 369 370
    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));
371 372
}

373 374
void PlayerManager::playPause()
{
375
    playing() ? action("pause")->activate() : action("play")->activate();
376 377
}

378 379
void PlayerManager::forward()
{
380 381
    m_playlistInterface->playNext();
    FileHandle file = m_playlistInterface->currentFile();
382

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

void PlayerManager::back()
{
391 392
    m_playlistInterface->playPrevious();
    FileHandle file = m_playlistInterface->currentFile();
393

394 395 396 397 398 399
    if(!file.isNull())
        play(file);
    else
        stop();
}

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

405
    int volume = m_sliderAction->volumeSlider()->volume() +
406
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% up
407 408

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

void PlayerManager::volumeDown()
{
414
    if(!player() || !m_sliderAction)
415
        return;
416 417

    int volume = m_sliderAction->volumeSlider()->value() -
418
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% down
419 420

    slotSetVolume(volume);
421
    m_sliderAction->volumeSlider()->setVolume(volume);
422 423 424 425
}

void PlayerManager::mute()
{
426
    if(!player() || !m_sliderAction)
427 428 429 430 431 432
        return;

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

433 434 435
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
436

437
void PlayerManager::slotPollPlay()
438
{
439
    if(!player() || !m_playlistInterface)
440
        return;
441

442
    m_noSeek = true;
443

444
    if(!player()->playing()) {
445
        m_timer->stop();
446

447 448
        m_playlistInterface->playNext();
        FileHandle nextFile = m_playlistInterface->currentFile();
449
        if(!nextFile.isNull())
450
            play(nextFile);
451 452
        else
            stop();
453 454
    }
    else if(!m_sliderAction->dragging()) {
455
        m_sliderAction->trackPositionSlider()->setValue(player()->position());
456

457 458 459 460
        if(m_statusLabel) {
            m_statusLabel->setItemTotalTime(player()->totalTime());
            m_statusLabel->setItemCurrentTime(player()->currentTime());
        }
461
    }
462

463 464 465 466
    // 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.
467

468 469 470
    if(player()->playing() &&
       player()->totalTime() > 0 &&
       float(player()->totalTime() - player()->currentTime()) < m_pollInterval * 2)
471 472 473
    {
        m_timer->changeInterval(50);
    }
474

475
    m_noSeek = false;
476 477
}

478 479 480 481
void PlayerManager::slotSetOutput(int system)
{
    stop();
    delete m_player;
482
    m_player = createPlayer(system);
483
    setup();
484 485 486 487 488 489 490 491 492 493 494 495 496
}

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();
497
    float totalTime = float(player()->totalTime());
Scott Wheeler's avatar
Scott Wheeler committed
498
    int seekTime = int(positionFraction * totalTime + 0.5); // "+0.5" for rounding
499 500 501 502

    m_statusLabel->setItemCurrentTime(seekTime);
}

503 504 505 506
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

507 508 509 510 511 512 513 514
Player *PlayerManager::player() const
{
    if(!m_player)
        instance()->setup();

    return m_player;
}

515 516 517 518
void PlayerManager::setup()
{
    // All of the actions required by this class should be listed here.

519 520 521 522 523
    if(!action("pause") ||
       !action("stop") ||
       !action("back") ||
       !action("forward") ||
       !action("trackPositionAction"))
524 525 526 527 528
    {
        kdWarning(65432) << k_funcinfo << "Could not find all of the required actions." << endl;
        return;
    }

529 530
    // initialize action states

531 532 533 534
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
535

536 537
    // setup sliders

538
    m_sliderAction = action<SliderAction>("trackPositionAction");
539

540 541 542 543
    connect(m_sliderAction, SIGNAL(signalPositionChanged(int)),
            this, SLOT(seekPosition(int)));
    connect(m_sliderAction->trackPositionSlider(), SIGNAL(valueChanged(int)),
            this, SLOT(slotUpdateTime(int)));
544
    connect(m_sliderAction->volumeSlider(), SIGNAL(signalVolumeChanged(int)),
545 546
            this, SLOT(slotSetVolume(int)));

547 548 549
    // Call this method manually to avoid warnings.
    
    KAction *outputAction = actions()->action("outputSelect");
550 551 552

    if(outputAction) {
        int mediaSystem = static_cast<KSelectAction *>(outputAction)->currentItem();
553
        m_player = createPlayer(mediaSystem);
Scott Wheeler's avatar
Scott Wheeler committed
554
        connect(outputAction, SIGNAL(activated(int)), this, SLOT(slotSetOutput(int)));
555 556
    }
    else
557
        m_player = createPlayer();
558

559
    float volume =
560
        float(m_sliderAction->volumeSlider()->volume()) /
561
        float(m_sliderAction->volumeSlider()->maxValue());
562 563 564

    m_player->setVolume(volume);

565 566
    m_timer = new QTimer(this, "play timer");
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotPollPlay()));
567 568 569
}

#include "playermanager.moc"