playermanager.cpp 12.1 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 74
    Player(),
    DCOPObject("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
QString PlayerManager::playingString() const
{
    if(!playing())
        return QString::null;

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

158
void PlayerManager::setPlaylistInterface(PlaylistInterface *interface)
159 160 161 162
{
    m_playlistInterface = interface;
}

163 164 165 166 167
void PlayerManager::setStatusLabel(StatusLabel *label)
{
    m_statusLabel = label;
}

168 169 170 171 172 173 174 175 176 177 178 179 180 181
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;
}

182 183 184 185
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

186
void PlayerManager::play(const FileHandle &file)
187
{
188
    if(!player() || !m_playlistInterface)
189 190
        return;

191 192
    if(file.isNull()) {
        if(player()->paused())
193
            player()->play();
194
        else if(player()->playing()) {
195
            m_sliderAction->trackPositionSlider()->setValue(0);
196
            player()->seekPosition(0);
197
        }
198
        else {
199
            m_file = m_playlistInterface->currentFile();
200

201 202 203
            if(!m_file.isNull()) {
                player()->play(m_file);
                m_statusLabel->setPlayingItemInfo(m_file, m_playlistInterface);
204
            }
205
        }
206
    }
207
    else {
208
        m_file = file;
209
        player()->play(file);
210 211
        m_statusLabel->setPlayingItemInfo(file, m_playlistInterface);
    }
212

213
    // Make sure that the player() actually starts before doing anything.
214

215
    if(!player()->playing()) {
216 217 218
        stop();
        return;
    }
219

220 221 222 223
    action("pause")->setEnabled(true);
    action("stop")->setEnabled(true);
    action("forward")->setEnabled(true);
    action("back")->setEnabled(true);
224

225
    m_sliderAction->trackPositionSlider()->setEnabled(true);
226 227

    m_timer->start(m_pollInterval);
228 229

    emit signalPlay();
230 231
}

Scott Wheeler's avatar
Scott Wheeler committed
232 233 234 235 236
void PlayerManager::play()
{
    play(FileHandle::null());
}

237 238
void PlayerManager::pause()
{
239
    if(!player())
240 241
        return;

242 243 244 245 246 247
    if(player()->paused()) {
        play();
        return;
    }

    m_timer->stop();
248
    action("pause")->setEnabled(false);
249 250

    player()->pause();
251 252

    emit signalPause();
253 254 255 256
}

void PlayerManager::stop()
{
257
    if(!player())
258 259
        return;

260 261
    m_timer->stop();

262 263 264 265
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
266 267 268 269 270 271 272

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

    m_statusLabel->clear();

    player()->stop();
273

274 275
    m_file = FileHandle::null();

276
    emit signalStop();
277 278
}

Laurent Montel's avatar
Laurent Montel committed
279
void PlayerManager::setVolume(float volume)
280
{
281
    if(!player())
282 283
        return;

284
    player()->setVolume(volume);
285 286
}

Scott Wheeler's avatar
Scott Wheeler committed
287
void PlayerManager::seek(int seekTime)
288
{
289
    if(!player())
290
        return;
291

292
    player()->seek(seekTime);
293 294
}

295
void PlayerManager::seekPosition(int position)
296
{
297 298 299 300
    if(!player())
        return;

    if(!player()->playing() || m_noSeek)
301
        return;
302

303 304
    slotUpdateTime(position);
    player()->seekPosition(position);
305 306 307 308 309 310 311 312 313 314 315 316 317
    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));
318 319
}

320 321
void PlayerManager::playPause()
{
322
    playing() ? action("pause")->activate() : action("play")->activate();
323 324
}

325 326
void PlayerManager::forward()
{
327
    FileHandle file = m_playlistInterface->nextFile();
328

329 330 331 332 333 334 335 336
    if(!file.isNull())
        play(file);
    else
        stop();
}

void PlayerManager::back()
{
337
    FileHandle file = m_playlistInterface->previousFile();
338

339 340 341 342 343 344
    if(!file.isNull())
        play(file);
    else
        stop();
}

345 346
void PlayerManager::volumeUp()
{
347
    if(!player() || !m_sliderAction)
348
        return;
349

350
    int volume = m_sliderAction->volumeSlider()->volume() +
351
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% up
352 353

    slotSetVolume(volume);
354
    m_sliderAction->volumeSlider()->setVolume(volume);
355 356 357 358
}

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

    int volume = m_sliderAction->volumeSlider()->value() -
363
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% down
364 365

    slotSetVolume(volume);
366
    m_sliderAction->volumeSlider()->setVolume(volume);
367 368 369 370
}

void PlayerManager::mute()
{
371
    if(!player() || !m_sliderAction)
372 373 374 375 376 377
        return;

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

378 379 380
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
381

382
void PlayerManager::slotPollPlay()
383
{
384
    if(!player() || !m_playlistInterface)
385
        return;
386

387
    m_noSeek = true;
388

389
    if(!player()->playing()) {
390
        m_timer->stop();
391

392
        FileHandle nextFile = m_playlistInterface->nextFile();
393
        if(!nextFile.isNull())
394
            play(nextFile);
395 396
        else
            stop();
397 398
    }
    else if(!m_sliderAction->dragging()) {
399
        m_sliderAction->trackPositionSlider()->setValue(player()->position());
400

401 402 403 404
        if(m_statusLabel) {
            m_statusLabel->setItemTotalTime(player()->totalTime());
            m_statusLabel->setItemCurrentTime(player()->currentTime());
        }
405
    }
406

407 408 409 410
    // 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.
411

412 413 414
    if(player()->playing() &&
       player()->totalTime() > 0 &&
       float(player()->totalTime() - player()->currentTime()) < m_pollInterval * 2)
415 416 417
    {
        m_timer->changeInterval(50);
    }
418

419
    m_noSeek = false;
420 421
}

422 423 424 425
void PlayerManager::slotSetOutput(int system)
{
    stop();
    delete m_player;
426
    m_player = createPlayer(system);
427 428 429 430 431 432 433 434 435 436 437 438 439
}

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();
440
    float totalTime = float(player()->totalTime());
Scott Wheeler's avatar
Scott Wheeler committed
441
    int seekTime = int(positionFraction * totalTime + 0.5); // "+0.5" for rounding
442 443 444 445

    m_statusLabel->setItemCurrentTime(seekTime);
}

446 447 448 449
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

450 451 452 453 454 455 456 457
Player *PlayerManager::player() const
{
    if(!m_player)
        instance()->setup();

    return m_player;
}

458 459 460 461
void PlayerManager::setup()
{
    // All of the actions required by this class should be listed here.

462 463 464 465 466
    if(!action("pause") ||
       !action("stop") ||
       !action("back") ||
       !action("forward") ||
       !action("trackPositionAction"))
467 468 469 470 471 472

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

473 474
    // initialize action states

475 476 477 478
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
479

480 481
    // setup sliders

482
    m_sliderAction = action<SliderAction>("trackPositionAction");
483

484 485 486 487
    connect(m_sliderAction, SIGNAL(signalPositionChanged(int)),
            this, SLOT(seekPosition(int)));
    connect(m_sliderAction->trackPositionSlider(), SIGNAL(valueChanged(int)),
            this, SLOT(slotUpdateTime(int)));
488
    connect(m_sliderAction->volumeSlider(), SIGNAL(signalVolumeChanged(int)),
489 490
            this, SLOT(slotSetVolume(int)));

491
    KAction *outputAction = action("outputSelect");
492 493 494

    if(outputAction) {
        int mediaSystem = static_cast<KSelectAction *>(outputAction)->currentItem();
495
        m_player = createPlayer(mediaSystem);
Scott Wheeler's avatar
Scott Wheeler committed
496
        connect(outputAction, SIGNAL(activated(int)), this, SLOT(slotSetOutput(int)));
497 498
    }
    else
499
        m_player = createPlayer();
500

501
    float volume =
502
        float(m_sliderAction->volumeSlider()->volume()) /
503
        float(m_sliderAction->volumeSlider()->maxValue());
504 505 506

    m_player->setVolume(volume);

507 508
    m_timer = new QTimer(this, "play timer");
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotPollPlay()));
509 510 511
}

#include "playermanager.moc"