gstreamerplayer.cpp 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/***************************************************************************
                          gstreamerplayer.cpp  -  description
                             -------------------
    begin                : Sat Feb 9 2003
    copyright            : (C) 2003 by Tim Jansen
    email                : tim@tjansen.de
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

Scott Wheeler's avatar
Scott Wheeler committed
18
#include <config.h>
19 20

#if HAVE_GSTREAMER
21 22 23 24 25 26 27

#include <kdebug.h>

#include <qfile.h>

#include "gstreamerplayer.h"

28 29
using namespace KDE::GST;
using namespace KDE::GSTPlay;
30 31 32 33 34

////////////////////////////////////////////////////////////////////////////////
// public methods
////////////////////////////////////////////////////////////////////////////////

35
GStreamerPlayer::GStreamerPlayer() : QObject(0), Player(),
36
                                     m_positionNs(0), m_durationNs(0), m_currentVolume(1.0)
37 38 39 40 41 42
{
    setupPlayer();
}

GStreamerPlayer::~GStreamerPlayer()
{
43
    delete m_player;
44 45
}

46
void GStreamerPlayer::play(const FileHandle &file)
47
{
48
    m_currentFile = file.absFilePath();
49 50
    m_positionNs = 0;
    m_durationNs = 0;
51

52 53
    if(!file.isNull())
        m_player->setLocation(file.absFilePath());
54

55 56
    if(m_player->getState() != Element::STATE_PLAYING)
        m_player->setState(Element::STATE_PLAYING);
57 58 59 60
}

void GStreamerPlayer::pause()
{
61
    if(m_player->getState() != Element::STATE_PAUSED)
62
        m_player->setState(Element::STATE_PAUSED);
63 64 65 66
}

void GStreamerPlayer::stop()
{
67
    if(m_player->getState() != Element::STATE_READY)
68
        m_player->setState(Element::STATE_READY);
69 70 71 72 73
}

void GStreamerPlayer::setVolume(float volume)
{
    // 1.0 is full volume
74
    m_player->setVolume(volume);
75 76
}

77
float GStreamerPlayer::volume() const
78 79
{
    // 1.0 is full volume
80
    return m_player->getVolume();
81 82 83
}

/////////////////////////////////////////////////////////////////////////////////
84
// m_player status functions
85 86 87 88 89
/////////////////////////////////////////////////////////////////////////////////

bool GStreamerPlayer::playing() const
{
    // true if playing
90
    return m_player->getState() == Element::STATE_PLAYING;
91 92 93 94 95
}

bool GStreamerPlayer::paused() const
{
    // true if paused
96
    return m_player->getState() == Element::STATE_PAUSED;
97 98 99 100
}

long GStreamerPlayer::totalTime() const
{
101
    return m_durationNs / 1000000000L;
102 103 104 105
}

long GStreamerPlayer::currentTime() const
{
106
    return m_positionNs / 1000000000L;
107 108 109 110
}

int GStreamerPlayer::position() const
{
111
    if (m_durationNs > 0)
112
        return int((m_positionNs * 1000.0) / m_durationNs);
113
    else
114
        return 0;
115 116 117
}

/////////////////////////////////////////////////////////////////////////////////
118
// m_player seek functions
119 120 121 122 123
/////////////////////////////////////////////////////////////////////////////////

void GStreamerPlayer::seek(long seekTime)
{
    // seek time in seconds?
124
    m_player->seekToTime(seekTime*1000000000);
125 126 127 128 129
}

void GStreamerPlayer::seekPosition(int position)
{
    // position unit is 1/1000th
130
    if(m_durationNs > 0)
131
        m_player->seekToTime(position * m_durationNs / 1000L);
132
    else
133
        m_player->seekToTime(0);
134 135 136 137 138 139 140 141 142

}

/////////////////////////////////////////////////////////////////////////////////
// private
/////////////////////////////////////////////////////////////////////////////////

void GStreamerPlayer::setupPlayer()
{
143
    m_player = new Play(Play::PIPE_AUDIO_BUFFER_THREADED, this, "Play");
Zack Rusin's avatar
Zack Rusin committed
144
    connect(m_player, SIGNAL(timeTick(long long)),
145
            SLOT(slotSetPosition(long long)));
Zack Rusin's avatar
Zack Rusin committed
146
    connect(m_player, SIGNAL(streamLength(long long)),
147
            SLOT(slotSetDuration(long long)));
Zack Rusin's avatar
Zack Rusin committed
148
    connect(m_player, SIGNAL(streamEnd()), SLOT(slotStopIfNotPlaying()));
149 150
}

151 152 153
void GStreamerPlayer::slotStopIfNotPlaying()
{
    if(!playing())
154
        stop();
155 156 157 158 159
}

#include "gstreamerplayer.moc"

#endif