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
FileHandle PlayerManager::playingFile() const
{
    return m_file;
}

155 156 157 158 159 160 161 162
QString PlayerManager::playingString() const
{
    if(!playing())
        return QString::null;

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

163
void PlayerManager::setPlaylistInterface(PlaylistInterface *interface)
164 165 166 167
{
    m_playlistInterface = interface;
}

168 169 170 171 172
void PlayerManager::setStatusLabel(StatusLabel *label)
{
    m_statusLabel = label;
}

173 174 175 176 177 178 179 180 181 182 183 184 185 186
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;
}

187 188 189 190
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

191
void PlayerManager::play(const FileHandle &file)
192
{
193
    if(!player() || !m_playlistInterface)
194 195
        return;

196 197
    if(file.isNull()) {
        if(player()->paused())
198
            player()->play();
199
        else if(player()->playing()) {
200
            m_sliderAction->trackPositionSlider()->setValue(0);
201
            player()->seekPosition(0);
202
        }
203
        else {
204
            m_file = m_playlistInterface->currentFile();
205

206 207 208
            if(!m_file.isNull()) {
                player()->play(m_file);
                m_statusLabel->setPlayingItemInfo(m_file, m_playlistInterface);
209
            }
210
        }
211
    }
212
    else {
213
        m_file = file;
214
        player()->play(file);
215 216
        m_statusLabel->setPlayingItemInfo(file, m_playlistInterface);
    }
217

218
    // Make sure that the player() actually starts before doing anything.
219

220
    if(!player()->playing()) {
221 222 223
        stop();
        return;
    }
224

225 226 227 228
    action("pause")->setEnabled(true);
    action("stop")->setEnabled(true);
    action("forward")->setEnabled(true);
    action("back")->setEnabled(true);
229

230
    m_sliderAction->trackPositionSlider()->setEnabled(true);
231 232

    m_timer->start(m_pollInterval);
233 234

    emit signalPlay();
235 236
}

Scott Wheeler's avatar
Scott Wheeler committed
237 238 239 240 241
void PlayerManager::play()
{
    play(FileHandle::null());
}

242 243
void PlayerManager::pause()
{
244
    if(!player())
245 246
        return;

247 248 249 250 251 252
    if(player()->paused()) {
        play();
        return;
    }

    m_timer->stop();
253
    action("pause")->setEnabled(false);
254 255

    player()->pause();
256 257

    emit signalPause();
258 259 260 261
}

void PlayerManager::stop()
{
262
    if(!player())
263 264
        return;

265 266
    m_timer->stop();

267 268 269 270
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
271 272 273 274 275 276 277

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

    m_statusLabel->clear();

    player()->stop();
278

279 280
    m_file = FileHandle::null();

281
    emit signalStop();
282 283
}

Laurent Montel's avatar
Laurent Montel committed
284
void PlayerManager::setVolume(float volume)
285
{
286
    if(!player())
287 288
        return;

289
    player()->setVolume(volume);
290 291
}

Scott Wheeler's avatar
Scott Wheeler committed
292
void PlayerManager::seek(int seekTime)
293
{
294
    if(!player())
295
        return;
296

297
    player()->seek(seekTime);
298 299
}

300
void PlayerManager::seekPosition(int position)
301
{
302 303 304 305
    if(!player())
        return;

    if(!player()->playing() || m_noSeek)
306
        return;
307

308 309
    slotUpdateTime(position);
    player()->seekPosition(position);
310 311 312 313 314 315 316 317 318 319 320 321 322
    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));
323 324
}

325 326
void PlayerManager::playPause()
{
327
    playing() ? action("pause")->activate() : action("play")->activate();
328 329
}

330 331
void PlayerManager::forward()
{
332
    FileHandle file = m_playlistInterface->nextFile();
333

334 335 336 337 338 339 340 341
    if(!file.isNull())
        play(file);
    else
        stop();
}

void PlayerManager::back()
{
342
    FileHandle file = m_playlistInterface->previousFile();
343

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

350 351
void PlayerManager::volumeUp()
{
352
    if(!player() || !m_sliderAction)
353
        return;
354

355
    int volume = m_sliderAction->volumeSlider()->volume() +
356
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% up
357 358

    slotSetVolume(volume);
359
    m_sliderAction->volumeSlider()->setVolume(volume);
360 361 362 363
}

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

    int volume = m_sliderAction->volumeSlider()->value() -
368
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% down
369 370

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

void PlayerManager::mute()
{
376
    if(!player() || !m_sliderAction)
377 378 379 380 381 382
        return;

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

383 384 385
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
386

387
void PlayerManager::slotPollPlay()
388
{
389
    if(!player() || !m_playlistInterface)
390
        return;
391

392
    m_noSeek = true;
393

394
    if(!player()->playing()) {
395
        m_timer->stop();
396

397
        FileHandle nextFile = m_playlistInterface->nextFile();
398
        if(!nextFile.isNull())
399
            play(nextFile);
400 401
        else
            stop();
402 403
    }
    else if(!m_sliderAction->dragging()) {
404
        m_sliderAction->trackPositionSlider()->setValue(player()->position());
405

406 407 408 409
        if(m_statusLabel) {
            m_statusLabel->setItemTotalTime(player()->totalTime());
            m_statusLabel->setItemCurrentTime(player()->currentTime());
        }
410
    }
411

412 413 414 415
    // 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.
416

417 418 419
    if(player()->playing() &&
       player()->totalTime() > 0 &&
       float(player()->totalTime() - player()->currentTime()) < m_pollInterval * 2)
420 421 422
    {
        m_timer->changeInterval(50);
    }
423

424
    m_noSeek = false;
425 426
}

427 428 429 430
void PlayerManager::slotSetOutput(int system)
{
    stop();
    delete m_player;
431
    m_player = createPlayer(system);
432 433 434 435 436 437 438 439 440 441 442 443 444
}

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();
445
    float totalTime = float(player()->totalTime());
Scott Wheeler's avatar
Scott Wheeler committed
446
    int seekTime = int(positionFraction * totalTime + 0.5); // "+0.5" for rounding
447 448 449 450

    m_statusLabel->setItemCurrentTime(seekTime);
}

451 452 453 454
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

455 456 457 458 459 460 461 462
Player *PlayerManager::player() const
{
    if(!m_player)
        instance()->setup();

    return m_player;
}

463 464 465 466
void PlayerManager::setup()
{
    // All of the actions required by this class should be listed here.

467 468 469 470 471
    if(!action("pause") ||
       !action("stop") ||
       !action("back") ||
       !action("forward") ||
       !action("trackPositionAction"))
472 473 474 475 476 477

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

478 479
    // initialize action states

480 481 482 483
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
484

485 486
    // setup sliders

487
    m_sliderAction = action<SliderAction>("trackPositionAction");
488

489 490 491 492
    connect(m_sliderAction, SIGNAL(signalPositionChanged(int)),
            this, SLOT(seekPosition(int)));
    connect(m_sliderAction->trackPositionSlider(), SIGNAL(valueChanged(int)),
            this, SLOT(slotUpdateTime(int)));
493
    connect(m_sliderAction->volumeSlider(), SIGNAL(signalVolumeChanged(int)),
494 495
            this, SLOT(slotSetVolume(int)));

496
    KAction *outputAction = action("outputSelect");
497 498 499

    if(outputAction) {
        int mediaSystem = static_cast<KSelectAction *>(outputAction)->currentItem();
500
        m_player = createPlayer(mediaSystem);
Scott Wheeler's avatar
Scott Wheeler committed
501
        connect(outputAction, SIGNAL(activated(int)), this, SLOT(slotSetOutput(int)));
502 503
    }
    else
504
        m_player = createPlayer();
505

506
    float volume =
507
        float(m_sliderAction->volumeSlider()->volume()) /
508
        float(m_sliderAction->volumeSlider()->maxValue());
509 510 511

    m_player->setVolume(volume);

512 513
    m_timer = new QTimer(this, "play timer");
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotPollPlay()));
514 515 516
}

#include "playermanager.moc"