playermanager.cpp 11.7 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
            FileHandle currentFile = m_playlistInterface->currentFile();
190

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

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

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

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

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

    m_timer->start(m_pollInterval);
217 218 219 220
}

void PlayerManager::pause()
{
221
    if(!player())
222 223
        return;

224 225 226 227 228 229
    if(player()->paused()) {
        play();
        return;
    }

    m_timer->stop();
230
    action("pause")->setEnabled(false);
231 232

    player()->pause();
233 234 235 236
}

void PlayerManager::stop()
{
237
    if(!player())
238 239
        return;

240 241
    m_timer->stop();

242 243 244 245
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
246 247 248 249 250 251 252

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

    m_statusLabel->clear();

    player()->stop();
253 254
}

Laurent Montel's avatar
Laurent Montel committed
255
void PlayerManager::setVolume(float volume)
256
{
257
    if(!player())
258 259
        return;

260
    player()->setVolume(volume);
261 262
}

263
void PlayerManager::seek(long seekTime)
264
{
265
    if(!player())
266
        return;
267

268
    player()->seek(seekTime);
269 270
}

271
void PlayerManager::seekPosition(int position)
272
{
273 274 275 276
    if(!player())
        return;

    if(!player()->playing() || m_noSeek)
277
        return;
278

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

296 297
void PlayerManager::playPause()
{
298
    playing() ? action("pause")->activate() : action("play")->activate();
299 300
}

301 302
void PlayerManager::forward()
{
303
    FileHandle file = m_playlistInterface->nextFile();
304

305 306 307 308 309 310 311 312
    if(!file.isNull())
        play(file);
    else
        stop();
}

void PlayerManager::back()
{
313
    FileHandle file = m_playlistInterface->previousFile();
314

315 316 317 318 319 320
    if(!file.isNull())
        play(file);
    else
        stop();
}

321 322
void PlayerManager::volumeUp()
{
323
    if(!player() || !m_sliderAction)
324
        return;
325

326
    int volume = m_sliderAction->volumeSlider()->volume() +
327
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% up
328 329

    slotSetVolume(volume);
330
    m_sliderAction->volumeSlider()->setVolume(volume);
331 332 333 334
}

void PlayerManager::volumeDown()
{
335
    if(!player() || !m_sliderAction)
336
        return;
337 338

    int volume = m_sliderAction->volumeSlider()->value() -
339
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% down
340 341

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

void PlayerManager::mute()
{
347
    if(!player() || !m_sliderAction)
348 349 350 351 352 353
        return;

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

354 355 356
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
357

358
void PlayerManager::slotPollPlay()
359
{
360
    if(!player() || !m_playlistInterface)
361
        return;
362

363
    m_noSeek = true;
364

365
    if(!player()->playing()) {
366
        m_timer->stop();
367

368
        FileHandle nextFile = m_playlistInterface->nextFile();
369
        if(!nextFile.isNull())
370
            play(nextFile);
371 372
        else
            stop();
373 374
    }
    else if(!m_sliderAction->dragging()) {
375
        m_sliderAction->trackPositionSlider()->setValue(player()->position());
376

377 378 379 380
        if(m_statusLabel) {
            m_statusLabel->setItemTotalTime(player()->totalTime());
            m_statusLabel->setItemCurrentTime(player()->currentTime());
        }
381
    }
382

383 384 385 386
    // 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.
387

388 389 390
    if(player()->playing() &&
       player()->totalTime() > 0 &&
       float(player()->totalTime() - player()->currentTime()) < m_pollInterval * 2)
391 392 393
    {
        m_timer->changeInterval(50);
    }
394

395
    m_noSeek = false;
396 397
}

398 399 400 401
void PlayerManager::slotSetOutput(int system)
{
    stop();
    delete m_player;
402
    m_player = createPlayer(system);
403 404 405 406 407 408 409 410 411 412 413 414 415
}

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();
416
    float totalTime = float(player()->totalTime());
417 418 419 420 421
    long seekTime = long(positionFraction * totalTime + 0.5); // "+0.5" for rounding

    m_statusLabel->setItemCurrentTime(seekTime);
}

422 423 424 425
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

426 427 428 429 430 431 432 433
Player *PlayerManager::player() const
{
    if(!m_player)
        instance()->setup();

    return m_player;
}

434 435 436 437
void PlayerManager::setup()
{
    // All of the actions required by this class should be listed here.

438 439 440 441 442
    if(!action("pause") ||
       !action("stop") ||
       !action("back") ||
       !action("forward") ||
       !action("trackPositionAction"))
443 444 445 446 447 448

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

449 450
    // initialize action states

451 452 453 454
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
455

456 457
    // setup sliders

458
    m_sliderAction = action<SliderAction>("trackPositionAction");
459

460 461 462 463
    connect(m_sliderAction, SIGNAL(signalPositionChanged(int)),
            this, SLOT(seekPosition(int)));
    connect(m_sliderAction->trackPositionSlider(), SIGNAL(valueChanged(int)),
            this, SLOT(slotUpdateTime(int)));
464
    connect(m_sliderAction->volumeSlider(), SIGNAL(signalVolumeChanged(int)),
465 466
            this, SLOT(slotSetVolume(int)));

467
    KAction *outputAction = action("outputSelect");
468 469 470

    if(outputAction) {
        int mediaSystem = static_cast<KSelectAction *>(outputAction)->currentItem();
471
        m_player = createPlayer(mediaSystem);
Scott Wheeler's avatar
Scott Wheeler committed
472
        connect(outputAction, SIGNAL(activated(int)), this, SLOT(slotSetOutput(int)));
473 474
    }
    else
475
        m_player = createPlayer();
476

477
    float volume =
478
        float(m_sliderAction->volumeSlider()->volume()) /
479
        float(m_sliderAction->volumeSlider()->maxValue());
480 481 482

    m_player->setVolume(volume);

483 484
    m_timer = new QTimer(this, "play timer");
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotPollPlay()));
485 486 487
}

#include "playermanager.moc"