playermanager.cpp 11.9 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
////////////////////////////////////////////////////////////////////////////////
// protected members
////////////////////////////////////////////////////////////////////////////////

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

173 174 175 176
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

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

182 183
    if(file.isNull()) {
        if(player()->paused())
184
            player()->play();
185
        else if(player()->playing()) {
186
            m_sliderAction->trackPositionSlider()->setValue(0);
187
            player()->seekPosition(0);
188
        }
189
        else {
190
            FileHandle currentFile = m_playlistInterface->currentFile();
191

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

203
    // Make sure that the player() actually starts before doing anything.
204

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

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

215
    m_sliderAction->trackPositionSlider()->setEnabled(true);
216 217

    m_timer->start(m_pollInterval);
218 219

    emit signalPlay();
220 221
}

Scott Wheeler's avatar
Scott Wheeler committed
222 223 224 225 226
void PlayerManager::play()
{
    play(FileHandle::null());
}

227 228
void PlayerManager::pause()
{
229
    if(!player())
230 231
        return;

232 233 234 235 236 237
    if(player()->paused()) {
        play();
        return;
    }

    m_timer->stop();
238
    action("pause")->setEnabled(false);
239 240

    player()->pause();
241 242

    emit signalPause();
243 244 245 246
}

void PlayerManager::stop()
{
247
    if(!player())
248 249
        return;

250 251
    m_timer->stop();

252 253 254 255
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
256 257 258 259 260 261 262

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

    m_statusLabel->clear();

    player()->stop();
263 264

    emit signalStop();
265 266
}

Laurent Montel's avatar
Laurent Montel committed
267
void PlayerManager::setVolume(float volume)
268
{
269
    if(!player())
270 271
        return;

272
    player()->setVolume(volume);
273 274
}

Scott Wheeler's avatar
Scott Wheeler committed
275
void PlayerManager::seek(int seekTime)
276
{
277
    if(!player())
278
        return;
279

280
    player()->seek(seekTime);
281 282
}

283
void PlayerManager::seekPosition(int position)
284
{
285 286 287 288
    if(!player())
        return;

    if(!player()->playing() || m_noSeek)
289
        return;
290

291 292
    slotUpdateTime(position);
    player()->seekPosition(position);
293 294 295 296 297 298 299 300 301 302 303 304 305
    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));
306 307
}

308 309
void PlayerManager::playPause()
{
310
    playing() ? action("pause")->activate() : action("play")->activate();
311 312
}

313 314
void PlayerManager::forward()
{
315
    FileHandle file = m_playlistInterface->nextFile();
316

317 318 319 320 321 322 323 324
    if(!file.isNull())
        play(file);
    else
        stop();
}

void PlayerManager::back()
{
325
    FileHandle file = m_playlistInterface->previousFile();
326

327 328 329 330 331 332
    if(!file.isNull())
        play(file);
    else
        stop();
}

333 334
void PlayerManager::volumeUp()
{
335
    if(!player() || !m_sliderAction)
336
        return;
337

338
    int volume = m_sliderAction->volumeSlider()->volume() +
339
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% up
340 341

    slotSetVolume(volume);
342
    m_sliderAction->volumeSlider()->setVolume(volume);
343 344 345 346
}

void PlayerManager::volumeDown()
{
347
    if(!player() || !m_sliderAction)
348
        return;
349 350

    int volume = m_sliderAction->volumeSlider()->value() -
351
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% down
352 353

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

void PlayerManager::mute()
{
359
    if(!player() || !m_sliderAction)
360 361 362 363 364 365
        return;

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

366 367 368
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
369

370
void PlayerManager::slotPollPlay()
371
{
372
    if(!player() || !m_playlistInterface)
373
        return;
374

375
    m_noSeek = true;
376

377
    if(!player()->playing()) {
378
        m_timer->stop();
379

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

389 390 391 392
        if(m_statusLabel) {
            m_statusLabel->setItemTotalTime(player()->totalTime());
            m_statusLabel->setItemCurrentTime(player()->currentTime());
        }
393
    }
394

395 396 397 398
    // 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.
399

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

407
    m_noSeek = false;
408 409
}

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

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();
428
    float totalTime = float(player()->totalTime());
Scott Wheeler's avatar
Scott Wheeler committed
429
    int seekTime = int(positionFraction * totalTime + 0.5); // "+0.5" for rounding
430 431 432 433

    m_statusLabel->setItemCurrentTime(seekTime);
}

434 435 436 437
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

438 439 440 441 442 443 444 445
Player *PlayerManager::player() const
{
    if(!m_player)
        instance()->setup();

    return m_player;
}

446 447 448 449
void PlayerManager::setup()
{
    // All of the actions required by this class should be listed here.

450 451 452 453 454
    if(!action("pause") ||
       !action("stop") ||
       !action("back") ||
       !action("forward") ||
       !action("trackPositionAction"))
455 456 457 458 459 460

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

461 462
    // initialize action states

463 464 465 466
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
467

468 469
    // setup sliders

470
    m_sliderAction = action<SliderAction>("trackPositionAction");
471

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

479
    KAction *outputAction = action("outputSelect");
480 481 482

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

489
    float volume =
490
        float(m_sliderAction->volumeSlider()->volume()) /
491
        float(m_sliderAction->volumeSlider()->maxValue());
492 493 494

    m_player->setVolume(volume);

495 496
    m_timer = new QTimer(this, "play timer");
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotPollPlay()));
497 498 499
}

#include "playermanager.moc"