playermanager.cpp 12.4 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_file = m_playlistInterface->currentFile();
218

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

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

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

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

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

    m_timer->start(m_pollInterval);
246 247

    emit signalPlay();
248 249
}

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

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

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

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

    player()->pause();
269 270

    emit signalPause();
271 272 273 274
}

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

278 279
    m_timer->stop();

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

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

    m_statusLabel->clear();

    player()->stop();
291

292 293
    m_file = FileHandle::null();

294
    emit signalStop();
295 296
}

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

302
    player()->setVolume(volume);
303 304
}

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

310
    player()->seek(seekTime);
311 312
}

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

    if(!player()->playing() || m_noSeek)
319
        return;
320

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

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

343 344
void PlayerManager::forward()
{
345
    FileHandle file = m_playlistInterface->nextFile();
346

347 348 349 350 351 352 353 354
    if(!file.isNull())
        play(file);
    else
        stop();
}

void PlayerManager::back()
{
355
    FileHandle file = m_playlistInterface->previousFile();
356

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

363 364
void PlayerManager::volumeUp()
{
365
    if(!player() || !m_sliderAction)
366
        return;
367

368
    int volume = m_sliderAction->volumeSlider()->volume() +
369
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% up
370 371

    slotSetVolume(volume);
372
    m_sliderAction->volumeSlider()->setVolume(volume);
373 374 375 376
}

void PlayerManager::volumeDown()
{
377
    if(!player() || !m_sliderAction)
378
        return;
379 380

    int volume = m_sliderAction->volumeSlider()->value() -
381
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% down
382 383

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

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

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

396 397 398
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
399

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

405
    m_noSeek = true;
406

407
    if(!player()->playing()) {
408
        m_timer->stop();
409

410
        FileHandle nextFile = m_playlistInterface->nextFile();
411
        if(!nextFile.isNull())
412
            play(nextFile);
413 414
        else
            stop();
415 416
    }
    else if(!m_sliderAction->dragging()) {
417
        m_sliderAction->trackPositionSlider()->setValue(player()->position());
418

419 420 421 422
        if(m_statusLabel) {
            m_statusLabel->setItemTotalTime(player()->totalTime());
            m_statusLabel->setItemCurrentTime(player()->currentTime());
        }
423
    }
424

425 426 427 428
    // 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.
429

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

437
    m_noSeek = false;
438 439
}

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

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

    m_statusLabel->setItemCurrentTime(seekTime);
}

464 465 466 467
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

468 469 470 471 472 473 474 475
Player *PlayerManager::player() const
{
    if(!m_player)
        instance()->setup();

    return m_player;
}

476 477 478 479
void PlayerManager::setup()
{
    // All of the actions required by this class should be listed here.

480 481 482 483 484
    if(!action("pause") ||
       !action("stop") ||
       !action("back") ||
       !action("forward") ||
       !action("trackPositionAction"))
485 486 487 488 489 490

    {
        kdWarning(65432) << k_funcinfo << "Could not find all of the required actions." << endl;
        return;
    }

491 492
    // initialize action states

493 494 495 496
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
497

498 499
    // setup sliders

500
    m_sliderAction = action<SliderAction>("trackPositionAction");
501

502 503 504 505
    connect(m_sliderAction, SIGNAL(signalPositionChanged(int)),
            this, SLOT(seekPosition(int)));
    connect(m_sliderAction->trackPositionSlider(), SIGNAL(valueChanged(int)),
            this, SLOT(slotUpdateTime(int)));
506
    connect(m_sliderAction->volumeSlider(), SIGNAL(signalVolumeChanged(int)),
507 508
            this, SLOT(slotSetVolume(int)));

509
    KAction *outputAction = action("outputSelect");
510 511 512

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

519
    float volume =
520
        float(m_sliderAction->volumeSlider()->volume()) /
521
        float(m_sliderAction->volumeSlider()->maxValue());
522 523 524

    m_player->setVolume(volume);

525 526
    m_timer = new QTimer(this, "play timer");
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotPollPlay()));
527 528 529
}

#include "playermanager.moc"