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 31
#include "actioncollection.h"

32 33
#include "config.h"

34
using namespace ActionCollection;
35 36 37

PlayerManager *PlayerManager::m_instance = 0;

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

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

PlayerManager::PlayerManager() :
    QObject(0, "PlayerManager"),
    m_sliderAction(0),
74
    m_playlistInterface(0),
75
    m_statusLabel(0),
76 77
    m_player(0),
    m_timer(0),
78 79
    m_noSeek(false),
    m_muted(false)
80 81 82 83 84 85
{
    setup();
}

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

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

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

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

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

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

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

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

121
    return player()->volume();
122 123 124 125
}

long PlayerManager::totalTime() const
{
126
    if(!player())
127 128
        return 0;

129
    return player()->totalTime();
130 131 132
}

long PlayerManager::currentTime() const
133
{
134
    if(!player())
135 136
        return 0;

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

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

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

148
void PlayerManager::setPlaylistInterface(PlaylistInterface *interface)
149 150 151 152
{
    m_playlistInterface = interface;
}

153 154 155 156 157
void PlayerManager::setStatusLabel(StatusLabel *label)
{
    m_statusLabel = label;
}

158 159 160 161 162 163 164 165 166 167 168 169 170 171
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;
}

172 173 174 175
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

176
void PlayerManager::play(const FileHandle &file)
177
{
178
    if(!player() || !m_playlistInterface)
179 180
        return;

181 182
    if(file.isNull()) {
        if(player()->paused())
183
            player()->play();
184
        else if(player()->playing()) {
185
            m_sliderAction->trackPositionSlider()->setValue(0);
186
            player()->seekPosition(0);
187
        }
188
        else {
189 190 191 192 193
            // TODO: currentFile() really should return a FileHanlde as such
            // making the cast below not needed.

            FileHandle currentFile = FileHandle(m_playlistInterface->currentFile());

194
            if(!currentFile.isNull()) {
195
                player()->play(currentFile);
196 197
                m_statusLabel->setPlayingItemInfo(currentFile, m_playlistInterface);
            }
198
        }
199
    }
200
    else {
201
        player()->play(file);
202 203
        m_statusLabel->setPlayingItemInfo(file, m_playlistInterface);
    }
204

205
    // Make sure that the player() actually starts before doing anything.
206

207
    if(!player()->playing()) {
208 209 210
        stop();
        return;
    }
211

212 213 214 215
    action("pause")->setEnabled(true);
    action("stop")->setEnabled(true);
    action("forward")->setEnabled(true);
    action("back")->setEnabled(true);
216

217
    m_sliderAction->trackPositionSlider()->setEnabled(true);
218 219

    m_timer->start(m_pollInterval);
220 221 222 223
}

void PlayerManager::pause()
{
224
    if(!player())
225 226
        return;

227 228 229 230 231 232
    if(player()->paused()) {
        play();
        return;
    }

    m_timer->stop();
233
    action("pause")->setEnabled(false);
234 235

    player()->pause();
236 237 238 239
}

void PlayerManager::stop()
{
240
    if(!player())
241 242
        return;

243 244
    m_timer->stop();

245 246 247 248
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
249 250 251 252 253 254 255

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

    m_statusLabel->clear();

    player()->stop();
256 257
}

Laurent Montel's avatar
Laurent Montel committed
258
void PlayerManager::setVolume(float volume)
259
{
260
    if(!player())
261 262
        return;

263
    player()->setVolume(volume);
264 265
}

266
void PlayerManager::seek(long seekTime)
267
{
268
    if(!player())
269
        return;
270

271
    player()->seek(seekTime);
272 273
}

274
void PlayerManager::seekPosition(int position)
275
{
276 277 278 279
    if(!player())
        return;

    if(!player()->playing() || m_noSeek)
280
        return;
281

282 283
    slotUpdateTime(position);
    player()->seekPosition(position);
284 285 286 287 288 289 290 291 292 293 294 295 296
    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));
297 298
}

299 300
void PlayerManager::playPause()
{
301
    playing() ? action("pause")->activate() : action("play")->activate();
302 303
}

304 305
void PlayerManager::forward()
{
306 307 308 309
    // TODO: nextFile() should return a FileHandle

    FileHandle file = FileHandle(m_playlistInterface->nextFile());

310 311 312 313 314 315 316 317
    if(!file.isNull())
        play(file);
    else
        stop();
}

void PlayerManager::back()
{
318 319 320 321
    // TODO: previousFile() should return a FileHandle

    FileHandle file = FileHandle(m_playlistInterface->previousFile());

322 323 324 325 326 327
    if(!file.isNull())
        play(file);
    else
        stop();
}

328 329
void PlayerManager::volumeUp()
{
330
    if(!player() || !m_sliderAction)
331
        return;
332

333
    int volume = m_sliderAction->volumeSlider()->volume() +
334
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% up
335 336

    slotSetVolume(volume);
337
    m_sliderAction->volumeSlider()->setVolume(volume);
338 339 340 341
}

void PlayerManager::volumeDown()
{
342
    if(!player() || !m_sliderAction)
343
        return;
344 345

    int volume = m_sliderAction->volumeSlider()->value() -
346
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% down
347 348

    slotSetVolume(volume);
349
    m_sliderAction->volumeSlider()->setVolume(volume);
350 351 352 353
}

void PlayerManager::mute()
{
354
    if(!player() || !m_sliderAction)
355 356 357 358 359 360
        return;

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

361 362 363
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
364

365
void PlayerManager::slotPollPlay()
366
{
367
    if(!player() || !m_playlistInterface)
368
        return;
369

370
    m_noSeek = true;
371

372
    if(!player()->playing()) {
373
        m_timer->stop();
374 375 376 377

        // TODO: nextFile() should use FileHandle

        FileHandle nextFile = FileHandle(m_playlistInterface->nextFile());
378
        if(!nextFile.isNull())
379
            play(nextFile);
380 381
        else
            stop();
382 383
    }
    else if(!m_sliderAction->dragging()) {
384
        m_sliderAction->trackPositionSlider()->setValue(player()->position());
385

386 387 388 389
        if(m_statusLabel) {
            m_statusLabel->setItemTotalTime(player()->totalTime());
            m_statusLabel->setItemCurrentTime(player()->currentTime());
        }
390
    }
391

392 393 394 395
    // 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.
396

397 398 399
    if(player()->playing() &&
       player()->totalTime() > 0 &&
       float(player()->totalTime() - player()->currentTime()) < m_pollInterval * 2)
400 401 402
    {
        m_timer->changeInterval(50);
    }
403

404
    m_noSeek = false;
405 406
}

407 408 409 410
void PlayerManager::slotSetOutput(int system)
{
    stop();
    delete m_player;
411
    m_player = createPlayer(system);
412 413 414 415 416 417 418 419 420 421 422 423 424
}

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();
425
    float totalTime = float(player()->totalTime());
426 427 428 429 430
    long seekTime = long(positionFraction * totalTime + 0.5); // "+0.5" for rounding

    m_statusLabel->setItemCurrentTime(seekTime);
}

431 432 433 434
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

435 436 437 438 439 440 441 442
Player *PlayerManager::player() const
{
    if(!m_player)
        instance()->setup();

    return m_player;
}

443 444 445 446
void PlayerManager::setup()
{
    // All of the actions required by this class should be listed here.

447 448 449 450 451
    if(!action("pause") ||
       !action("stop") ||
       !action("back") ||
       !action("forward") ||
       !action("trackPositionAction"))
452 453 454 455 456 457

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

458 459
    // initialize action states

460 461 462 463
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
464

465 466
    // setup sliders

467
    m_sliderAction = action<SliderAction>("trackPositionAction");
468

469 470 471 472
    connect(m_sliderAction, SIGNAL(signalPositionChanged(int)),
            this, SLOT(seekPosition(int)));
    connect(m_sliderAction->trackPositionSlider(), SIGNAL(valueChanged(int)),
            this, SLOT(slotUpdateTime(int)));
473
    connect(m_sliderAction->volumeSlider(), SIGNAL(signalVolumeChanged(int)),
474 475
            this, SLOT(slotSetVolume(int)));

476
    KAction *outputAction = action("outputSelect");
477 478 479

    if(outputAction) {
        int mediaSystem = static_cast<KSelectAction *>(outputAction)->currentItem();
480
        m_player = createPlayer(mediaSystem);
Scott Wheeler's avatar
Scott Wheeler committed
481
        connect(outputAction, SIGNAL(activated(int)), this, SLOT(slotSetOutput(int)));
482 483
    }
    else
484
        m_player = createPlayer();
485

486
    float volume =
487
        float(m_sliderAction->volumeSlider()->volume()) /
488
        float(m_sliderAction->volumeSlider()->maxValue());
489 490 491

    m_player->setVolume(volume);

492 493
    m_timer = new QTimer(this, "play timer");
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotPollPlay()));
494 495 496
}

#include "playermanager.moc"