playermanager.cpp 11.8 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
}

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

225 226
void PlayerManager::pause()
{
227
    if(!player())
228 229
        return;

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

    m_timer->stop();
236
    action("pause")->setEnabled(false);
237 238

    player()->pause();
239 240 241 242
}

void PlayerManager::stop()
{
243
    if(!player())
244 245
        return;

246 247
    m_timer->stop();

248 249 250 251
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
252 253 254 255 256 257 258

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

    m_statusLabel->clear();

    player()->stop();
259 260
}

Laurent Montel's avatar
Laurent Montel committed
261
void PlayerManager::setVolume(float volume)
262
{
263
    if(!player())
264 265
        return;

266
    player()->setVolume(volume);
267 268
}

Scott Wheeler's avatar
Scott Wheeler committed
269
void PlayerManager::seek(int seekTime)
270
{
271
    if(!player())
272
        return;
273

274
    player()->seek(seekTime);
275 276
}

277
void PlayerManager::seekPosition(int position)
278
{
279 280 281 282
    if(!player())
        return;

    if(!player()->playing() || m_noSeek)
283
        return;
284

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

302 303
void PlayerManager::playPause()
{
304
    playing() ? action("pause")->activate() : action("play")->activate();
305 306
}

307 308
void PlayerManager::forward()
{
309
    FileHandle file = m_playlistInterface->nextFile();
310

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

void PlayerManager::back()
{
319
    FileHandle file = m_playlistInterface->previousFile();
320

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

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

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

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

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

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

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

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

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

360 361 362
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
363

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

369
    m_noSeek = true;
370

371
    if(!player()->playing()) {
372
        m_timer->stop();
373

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

383 384 385 386
        if(m_statusLabel) {
            m_statusLabel->setItemTotalTime(player()->totalTime());
            m_statusLabel->setItemCurrentTime(player()->currentTime());
        }
387
    }
388

389 390 391 392
    // 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.
393

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

401
    m_noSeek = false;
402 403
}

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

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();
422
    float totalTime = float(player()->totalTime());
Scott Wheeler's avatar
Scott Wheeler committed
423
    int seekTime = int(positionFraction * totalTime + 0.5); // "+0.5" for rounding
424 425 426 427

    m_statusLabel->setItemCurrentTime(seekTime);
}

428 429 430 431
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

432 433 434 435 436 437 438 439
Player *PlayerManager::player() const
{
    if(!m_player)
        instance()->setup();

    return m_player;
}

440 441 442 443
void PlayerManager::setup()
{
    // All of the actions required by this class should be listed here.

444 445 446 447 448
    if(!action("pause") ||
       !action("stop") ||
       !action("back") ||
       !action("forward") ||
       !action("trackPositionAction"))
449 450 451 452 453 454

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

455 456
    // initialize action states

457 458 459 460
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
461

462 463
    // setup sliders

464
    m_sliderAction = action<SliderAction>("trackPositionAction");
465

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

473
    KAction *outputAction = action("outputSelect");
474 475 476

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

483
    float volume =
484
        float(m_sliderAction->volumeSlider()->volume()) /
485
        float(m_sliderAction->volumeSlider()->maxValue());
486 487 488

    m_player->setVolume(volume);

489 490
    m_timer = new QTimer(this, "play timer");
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotPollPlay()));
491 492 493
}

#include "playermanager.moc"