~neon/juk/master

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/***************************************************************************
    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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <config.h>

#if HAVE_GSTREAMER

#include <kdebug.h>

#include <qfile.h>

#include "gstreamerplayer.h"

using namespace KDE::GST;
using namespace KDE::GSTPlay;

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

GStreamerPlayer::GStreamerPlayer() : Player(),
                                     m_positionNs(0), m_durationNs(0), m_currentVolume(1.0)
{
    setupPlayer();
}

GStreamerPlayer::~GStreamerPlayer()
{
    delete m_player;
}

void GStreamerPlayer::play(const FileHandle &file)
{
    m_currentFile = file.absFilePath();
    m_positionNs = 0;
    m_durationNs = 0;

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

    if(m_player->getState() != Element::STATE_PLAYING)
        m_player->setState(Element::STATE_PLAYING);
}

void GStreamerPlayer::pause()
{
    if(m_player->getState() != Element::STATE_PAUSED)
        m_player->setState(Element::STATE_PAUSED);
}

void GStreamerPlayer::stop()
{
    if(m_player->getState() != Element::STATE_NULL)
        m_player->setState(Element::STATE_NULL);
}

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

float GStreamerPlayer::volume() const
{
    // 1.0 is full volume
    return m_player->getVolume();
}

/////////////////////////////////////////////////////////////////////////////////
// m_player status functions
/////////////////////////////////////////////////////////////////////////////////

bool GStreamerPlayer::playing() const
{
    // true if playing
    return m_player->getState() == Element::STATE_PLAYING;
}

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

int GStreamerPlayer::totalTime() const
{
    return m_durationNs / 1000000000L;
}

int GStreamerPlayer::currentTime() const
{
    return m_positionNs / 1000000000L;
}

int GStreamerPlayer::position() const
{
    if (m_durationNs > 0)
        return int((m_positionNs * 1000.0) / m_durationNs);
    else
        return 0;
}

/////////////////////////////////////////////////////////////////////////////////
// m_player seek functions
/////////////////////////////////////////////////////////////////////////////////

void GStreamerPlayer::seek(int seekTime)
{
    // seek time in seconds?
    m_player->seekToTime(seekTime*1000000000);
}

void GStreamerPlayer::seekPosition(int position)
{
    // position unit is 1/1000th
    if(m_durationNs > 0)
        m_player->seekToTime(position * m_durationNs / 1000L);
    else
        m_player->seekToTime(0);

}

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

void GStreamerPlayer::setupPlayer()
{
    m_player = new Play(Play::PIPE_AUDIO_BUFFER_THREADED, this, "Play");
    connect(m_player, SIGNAL(timeTick(long long)),
            SLOT(slotSetPosition(long long)));
    connect(m_player, SIGNAL(streamLength(long long)),
            SLOT(slotSetDuration(long long)));
    connect(m_player, SIGNAL(streamEnd()), SLOT(slotStopIfNotPlaying()));
}

void GStreamerPlayer::slotStopIfNotPlaying()
{
    if(!playing())
        stop();
}

#include "gstreamerplayer.moc"

#endif