playermanager.cpp 12.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/***************************************************************************
                          playermanager.cpp
                             -------------------
    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>
19
#include <klocale.h>
20 21

#include <qslider.h>
22
#include <qtimer.h>
23

24 25
#include "artsplayer.h"
#include "gstreamerplayer.h"
26
#include "playermanager.h"
27
#include "playlistinterface.h"
28
#include "slideraction.h"
29
#include "statuslabel.h"
30
#include "actioncollection.h"
31
#include "tag.h"
32

33 34
#include "config.h"

35
using namespace ActionCollection;
36 37 38

PlayerManager *PlayerManager::m_instance = 0;

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 66 67
////////////////////////////////////////////////////////////////////////////////
// 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;
}

68 69 70 71 72
////////////////////////////////////////////////////////////////////////////////
// protected members
////////////////////////////////////////////////////////////////////////////////

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

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
}

Scott Wheeler's avatar
Scott Wheeler committed
126
int PlayerManager::totalTime() const
127
{
128
    if(!player())
129 130
        return 0;

131
    return player()->totalTime();
132 133
}

Scott Wheeler's avatar
Scott Wheeler committed
134
int PlayerManager::currentTime() const
135
{
136
    if(!player())
137 138
        return 0;

139
    return player()->currentTime();
140 141 142 143
}

int PlayerManager::position() const
{
144
    if(!player())
145 146
        return 0;

147
    return player()->position();
148 149
}

150 151 152 153 154 155 156 157 158 159 160 161 162
QStringList PlayerManager::trackProperties()
{
    return FileHandle::properties();
}

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

    return m_file.property(property);
}

163 164 165 166 167
FileHandle PlayerManager::playingFile() const
{
    return m_file;
}

168 169 170 171 172 173 174 175
QString PlayerManager::playingString() const
{
    if(!playing())
        return QString::null;

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

176
void PlayerManager::setPlaylistInterface(PlaylistInterface *interface)
177 178 179 180
{
    m_playlistInterface = interface;
}

181 182 183 184 185
void PlayerManager::setStatusLabel(StatusLabel *label)
{
    m_statusLabel = label;
}

186 187 188 189 190 191 192 193 194 195 196 197 198 199
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;
}

200 201 202 203
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

204
void PlayerManager::play(const FileHandle &file)
205
{
206
    if(!player() || !m_playlistInterface)
207 208
        return;

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

220 221 222
            if(!m_file.isNull()) {
                player()->play(m_file);
                m_statusLabel->setPlayingItemInfo(m_file, m_playlistInterface);
223
            }
224
        }
225
    }
226
    else {
227
        m_file = file;
228
        player()->play(file);
229 230
        m_statusLabel->setPlayingItemInfo(file, m_playlistInterface);
    }
231

232
    // Make sure that the player() actually starts before doing anything.
233

234
    if(!player()->playing()) {
235 236 237
        stop();
        return;
    }
238

239 240 241 242
    action("pause")->setEnabled(true);
    action("stop")->setEnabled(true);
    action("forward")->setEnabled(true);
    action("back")->setEnabled(true);
243

244
    m_sliderAction->trackPositionSlider()->setEnabled(true);
245 246

    m_timer->start(m_pollInterval);
247 248

    emit signalPlay();
249 250
}

Scott Wheeler's avatar
Scott Wheeler committed
251 252 253 254 255
void PlayerManager::play()
{
    play(FileHandle::null());
}

256 257
void PlayerManager::pause()
{
258
    if(!player())
259 260
        return;

261 262 263 264 265 266
    if(player()->paused()) {
        play();
        return;
    }

    m_timer->stop();
267
    action("pause")->setEnabled(false);
268 269

    player()->pause();
270 271

    emit signalPause();
272 273 274 275
}

void PlayerManager::stop()
{
276
    if(!player())
277 278
        return;

279 280
    m_timer->stop();

281 282 283 284
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
285 286 287 288 289 290 291

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

    m_statusLabel->clear();

    player()->stop();
292
    m_playlistInterface->stop();
293

294 295
    m_file = FileHandle::null();

296
    emit signalStop();
297 298
}

Laurent Montel's avatar
Laurent Montel committed
299
void PlayerManager::setVolume(float volume)
300
{
301
    if(!player())
302 303
        return;

304
    player()->setVolume(volume);
305 306
}

Scott Wheeler's avatar
Scott Wheeler committed
307
void PlayerManager::seek(int seekTime)
308
{
309
    if(!player())
310
        return;
311

312
    player()->seek(seekTime);
313 314
}

315
void PlayerManager::seekPosition(int position)
316
{
317 318 319 320
    if(!player())
        return;

    if(!player()->playing() || m_noSeek)
321
        return;
322

323 324
    slotUpdateTime(position);
    player()->seekPosition(position);
325 326 327 328 329 330 331 332 333 334 335 336 337
    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));
338 339
}

340 341
void PlayerManager::playPause()
{
342
    playing() ? action("pause")->activate() : action("play")->activate();
343 344
}

345 346
void PlayerManager::forward()
{
347 348
    m_playlistInterface->playNext();
    FileHandle file = m_playlistInterface->currentFile();
349

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

void PlayerManager::back()
{
358 359
    m_playlistInterface->playPrevious();
    FileHandle file = m_playlistInterface->currentFile();
360

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

367 368
void PlayerManager::volumeUp()
{
369
    if(!player() || !m_sliderAction)
370
        return;
371

372
    int volume = m_sliderAction->volumeSlider()->volume() +
373
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% up
374 375

    slotSetVolume(volume);
376
    m_sliderAction->volumeSlider()->setVolume(volume);
377 378 379 380
}

void PlayerManager::volumeDown()
{
381
    if(!player() || !m_sliderAction)
382
        return;
383 384

    int volume = m_sliderAction->volumeSlider()->value() -
385
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% down
386 387

    slotSetVolume(volume);
388
    m_sliderAction->volumeSlider()->setVolume(volume);
389 390 391 392
}

void PlayerManager::mute()
{
393
    if(!player() || !m_sliderAction)
394 395 396 397 398 399
        return;

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

400 401 402
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
403

404
void PlayerManager::slotPollPlay()
405
{
406
    if(!player() || !m_playlistInterface)
407
        return;
408

409
    m_noSeek = true;
410

411
    if(!player()->playing()) {
412
        m_timer->stop();
413

414 415
        m_playlistInterface->playNext();
        FileHandle nextFile = m_playlistInterface->currentFile();
416
        if(!nextFile.isNull())
417
            play(nextFile);
418 419
        else
            stop();
420 421
    }
    else if(!m_sliderAction->dragging()) {
422
        m_sliderAction->trackPositionSlider()->setValue(player()->position());
423

424 425 426 427
        if(m_statusLabel) {
            m_statusLabel->setItemTotalTime(player()->totalTime());
            m_statusLabel->setItemCurrentTime(player()->currentTime());
        }
428
    }
429

430 431 432 433
    // 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.
434

435 436 437
    if(player()->playing() &&
       player()->totalTime() > 0 &&
       float(player()->totalTime() - player()->currentTime()) < m_pollInterval * 2)
438 439 440
    {
        m_timer->changeInterval(50);
    }
441

442
    m_noSeek = false;
443 444
}

445 446 447 448
void PlayerManager::slotSetOutput(int system)
{
    stop();
    delete m_player;
449
    m_player = createPlayer(system);
450 451 452 453 454 455 456 457 458 459 460 461 462
}

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();
463
    float totalTime = float(player()->totalTime());
Scott Wheeler's avatar
Scott Wheeler committed
464
    int seekTime = int(positionFraction * totalTime + 0.5); // "+0.5" for rounding
465 466 467 468

    m_statusLabel->setItemCurrentTime(seekTime);
}

469 470 471 472
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

473 474 475 476 477 478 479 480
Player *PlayerManager::player() const
{
    if(!m_player)
        instance()->setup();

    return m_player;
}

481 482 483 484
void PlayerManager::setup()
{
    // All of the actions required by this class should be listed here.

485 486 487 488 489
    if(!action("pause") ||
       !action("stop") ||
       !action("back") ||
       !action("forward") ||
       !action("trackPositionAction"))
490 491 492 493 494
    {
        kdWarning(65432) << k_funcinfo << "Could not find all of the required actions." << endl;
        return;
    }

495 496
    // initialize action states

497 498 499 500
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
501

502 503
    // setup sliders

504
    m_sliderAction = action<SliderAction>("trackPositionAction");
505

506 507 508 509
    connect(m_sliderAction, SIGNAL(signalPositionChanged(int)),
            this, SLOT(seekPosition(int)));
    connect(m_sliderAction->trackPositionSlider(), SIGNAL(valueChanged(int)),
            this, SLOT(slotUpdateTime(int)));
510
    connect(m_sliderAction->volumeSlider(), SIGNAL(signalVolumeChanged(int)),
511 512
            this, SLOT(slotSetVolume(int)));

513
    KAction *outputAction = action("outputSelect");
514 515 516

    if(outputAction) {
        int mediaSystem = static_cast<KSelectAction *>(outputAction)->currentItem();
517
        m_player = createPlayer(mediaSystem);
Scott Wheeler's avatar
Scott Wheeler committed
518
        connect(outputAction, SIGNAL(activated(int)), this, SLOT(slotSetOutput(int)));
519 520
    }
    else
521
        m_player = createPlayer();
522

523
    float volume =
524
        float(m_sliderAction->volumeSlider()->volume()) /
525
        float(m_sliderAction->volumeSlider()->maxValue());
526 527 528

    m_player->setVolume(volume);

529 530
    m_timer = new QTimer(this, "play timer");
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotPollPlay()));
531 532 533
}

#include "playermanager.moc"