playermanager.cpp 12.3 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 64 65
////////////////////////////////////////////////////////////////////////////////
// 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);
#ifdef USE_ARTS
    p = new ArtsPlayer;
#endif
#endif

    return p;
}

66 67 68 69 70
////////////////////////////////////////////////////////////////////////////////
// protected members
////////////////////////////////////////////////////////////////////////////////

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

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

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

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

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

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

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

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

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

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

Scott Wheeler's avatar
Scott Wheeler committed
123
int PlayerManager::totalTime() const
124
{
125
    if(!player())
126 127
        return 0;

128
    return player()->totalTime();
129 130
}

Scott Wheeler's avatar
Scott Wheeler committed
131
int PlayerManager::currentTime() const
132
{
133
    if(!player())
134 135
        return 0;

136
    return player()->currentTime();
137 138 139 140
}

int PlayerManager::position() const
{
141
    if(!player())
142 143
        return 0;

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

147 148 149 150 151 152 153 154 155 156 157 158 159
QStringList PlayerManager::trackProperties()
{
    return FileHandle::properties();
}

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

    return m_file.property(property);
}

160 161 162 163 164
FileHandle PlayerManager::playingFile() const
{
    return m_file;
}

165 166 167 168 169 170 171 172
QString PlayerManager::playingString() const
{
    if(!playing())
        return QString::null;

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

173
void PlayerManager::setPlaylistInterface(PlaylistInterface *interface)
174 175 176 177
{
    m_playlistInterface = interface;
}

178 179 180 181 182
void PlayerManager::setStatusLabel(StatusLabel *label)
{
    m_statusLabel = label;
}

183 184 185 186 187 188 189 190 191 192 193 194 195 196
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;
}

197 198 199 200
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

201
void PlayerManager::play(const FileHandle &file)
202
{
203
    if(!player() || !m_playlistInterface)
204 205
        return;

206 207
    if(file.isNull()) {
        if(player()->paused())
208
            player()->play();
209
        else if(player()->playing()) {
210
            m_sliderAction->trackPositionSlider()->setValue(0);
211
            player()->seekPosition(0);
212
        }
213
        else {
214
	    m_playlistInterface->playNext();
215
            m_file = m_playlistInterface->currentFile();
216

217
            if(!m_file.isNull())
218
                player()->play(m_file);
219
        }
220
    }
221
    else {
222
        m_file = file;
223
        player()->play(file);
224
    }
225

226
    // Make sure that the player() actually starts before doing anything.
227

228
    if(!player()->playing()) {
229 230 231
        stop();
        return;
    }
232

233 234 235 236
    action("pause")->setEnabled(true);
    action("stop")->setEnabled(true);
    action("forward")->setEnabled(true);
    action("back")->setEnabled(true);
237

238
    m_sliderAction->trackPositionSlider()->setEnabled(true);
239 240

    m_timer->start(m_pollInterval);
241 242

    emit signalPlay();
243 244
}

Scott Wheeler's avatar
Scott Wheeler committed
245 246 247 248 249
void PlayerManager::play()
{
    play(FileHandle::null());
}

250 251
void PlayerManager::pause()
{
252
    if(!player())
253 254
        return;

255 256 257 258 259 260
    if(player()->paused()) {
        play();
        return;
    }

    m_timer->stop();
261
    action("pause")->setEnabled(false);
262 263

    player()->pause();
264 265

    emit signalPause();
266 267 268 269
}

void PlayerManager::stop()
{
270
    if(!player())
271 272
        return;

273 274
    m_timer->stop();

275 276 277 278
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
279 280 281 282 283

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

    player()->stop();
284
    m_playlistInterface->stop();
285

286 287
    m_file = FileHandle::null();

288
    emit signalStop();
289 290
}

Laurent Montel's avatar
Laurent Montel committed
291
void PlayerManager::setVolume(float volume)
292
{
293
    if(!player())
294 295
        return;

296
    player()->setVolume(volume);
297 298
}

Scott Wheeler's avatar
Scott Wheeler committed
299
void PlayerManager::seek(int seekTime)
300
{
301
    if(!player())
302
        return;
303

304
    player()->seek(seekTime);
305 306
}

307
void PlayerManager::seekPosition(int position)
308
{
309 310 311 312
    if(!player())
        return;

    if(!player()->playing() || m_noSeek)
313
        return;
314

315 316
    slotUpdateTime(position);
    player()->seekPosition(position);
317 318 319 320 321 322 323 324 325 326 327 328 329
    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));
330 331
}

332 333
void PlayerManager::playPause()
{
334
    playing() ? action("pause")->activate() : action("play")->activate();
335 336
}

337 338
void PlayerManager::forward()
{
339 340
    m_playlistInterface->playNext();
    FileHandle file = m_playlistInterface->currentFile();
341

342 343 344 345 346 347 348 349
    if(!file.isNull())
        play(file);
    else
        stop();
}

void PlayerManager::back()
{
350 351
    m_playlistInterface->playPrevious();
    FileHandle file = m_playlistInterface->currentFile();
352

353 354 355 356 357 358
    if(!file.isNull())
        play(file);
    else
        stop();
}

359 360
void PlayerManager::volumeUp()
{
361
    if(!player() || !m_sliderAction)
362
        return;
363

364
    int volume = m_sliderAction->volumeSlider()->volume() +
365
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% up
366 367

    slotSetVolume(volume);
368
    m_sliderAction->volumeSlider()->setVolume(volume);
369 370 371 372
}

void PlayerManager::volumeDown()
{
373
    if(!player() || !m_sliderAction)
374
        return;
375 376

    int volume = m_sliderAction->volumeSlider()->value() -
377
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% down
378 379

    slotSetVolume(volume);
380
    m_sliderAction->volumeSlider()->setVolume(volume);
381 382 383 384
}

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

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

392 393 394
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
395

396
void PlayerManager::slotPollPlay()
397
{
398
    if(!player() || !m_playlistInterface)
399
        return;
400

401
    m_noSeek = true;
402

403
    if(!player()->playing()) {
404
        m_timer->stop();
405

406 407
        m_playlistInterface->playNext();
        FileHandle nextFile = m_playlistInterface->currentFile();
408
        if(!nextFile.isNull())
409
            play(nextFile);
410 411
        else
            stop();
412 413
    }
    else if(!m_sliderAction->dragging()) {
414
        m_sliderAction->trackPositionSlider()->setValue(player()->position());
415

416 417 418 419
        if(m_statusLabel) {
            m_statusLabel->setItemTotalTime(player()->totalTime());
            m_statusLabel->setItemCurrentTime(player()->currentTime());
        }
420
    }
421

422 423 424 425
    // 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.
426

427 428 429
    if(player()->playing() &&
       player()->totalTime() > 0 &&
       float(player()->totalTime() - player()->currentTime()) < m_pollInterval * 2)
430 431 432
    {
        m_timer->changeInterval(50);
    }
433

434
    m_noSeek = false;
435 436
}

437 438 439 440
void PlayerManager::slotSetOutput(int system)
{
    stop();
    delete m_player;
441
    m_player = createPlayer(system);
442 443 444 445 446 447 448 449 450 451 452 453 454
}

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();
455
    float totalTime = float(player()->totalTime());
Scott Wheeler's avatar
Scott Wheeler committed
456
    int seekTime = int(positionFraction * totalTime + 0.5); // "+0.5" for rounding
457 458 459 460

    m_statusLabel->setItemCurrentTime(seekTime);
}

461 462 463 464
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

465 466 467 468 469 470 471 472
Player *PlayerManager::player() const
{
    if(!m_player)
        instance()->setup();

    return m_player;
}

473 474 475 476
void PlayerManager::setup()
{
    // All of the actions required by this class should be listed here.

477 478 479 480 481
    if(!action("pause") ||
       !action("stop") ||
       !action("back") ||
       !action("forward") ||
       !action("trackPositionAction"))
482 483 484 485 486
    {
        kdWarning(65432) << k_funcinfo << "Could not find all of the required actions." << endl;
        return;
    }

487 488
    // initialize action states

489 490 491 492
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
493

494 495
    // setup sliders

496
    m_sliderAction = action<SliderAction>("trackPositionAction");
497

498 499 500 501
    connect(m_sliderAction, SIGNAL(signalPositionChanged(int)),
            this, SLOT(seekPosition(int)));
    connect(m_sliderAction->trackPositionSlider(), SIGNAL(valueChanged(int)),
            this, SLOT(slotUpdateTime(int)));
502
    connect(m_sliderAction->volumeSlider(), SIGNAL(signalVolumeChanged(int)),
503 504
            this, SLOT(slotSetVolume(int)));

505
    KAction *outputAction = action("outputSelect");
506 507 508

    if(outputAction) {
        int mediaSystem = static_cast<KSelectAction *>(outputAction)->currentItem();
509
        m_player = createPlayer(mediaSystem);
Scott Wheeler's avatar
Scott Wheeler committed
510
        connect(outputAction, SIGNAL(activated(int)), this, SLOT(slotSetOutput(int)));
511 512
    }
    else
513
        m_player = createPlayer();
514

515
    float volume =
516
        float(m_sliderAction->volumeSlider()->volume()) /
517
        float(m_sliderAction->volumeSlider()->maxValue());
518 519 520

    m_player->setVolume(volume);

521 522
    m_timer = new QTimer(this, "play timer");
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotPollPlay()));
523 524 525
}

#include "playermanager.moc"