summaryrefslogtreecommitdiffstats
path: root/juk/gstreamerplayer.cpp
blob: 39f371a324f6f690222aa8ed07894c8ecdb09f6f (plain)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/***************************************************************************
    copyright            : (C) 2004 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 "gstreamerplayer.h"

#if HAVE_GSTREAMER

#include <kapplication.h>
#include <kconfig.h>
#include <kglobal.h>
#include <kdebug.h>

#include <tqfile.h>
#include <tqtimer.h>

// Defined because recent versions of glib add support for having gcc check
// whether the sentinel used on g_object_{set,get} is correct.  Although 0
// is a valid NULL pointer in C++, when used in a C function call g++ doesn't
// know to turn it into a pointer so it leaves it as an int instead (which is
// wrong for 64-bit arch).  So, use the handy define below instead.

#define JUK_GLIB_NULL static_cast<gpointer>(0)

#if GSTREAMER_VERSION == 8

/******************************************************************************/
/******************************************************************************/
/******************************  GSTREAMER 0.8  *******************************/
/******************************************************************************/
/******************************************************************************/


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

GStreamerPlayer::GStreamerPlayer() :
    Player(),
    m_pipeline(0),
    m_source(0),
    m_decoder(0),
    m_volume(0),
    m_sink(0)
{
    readConfig();
    setupPipeline();
}

GStreamerPlayer::~GStreamerPlayer()
{
    stop();
    gst_object_unref(GST_OBJECT(m_pipeline));
}

void GStreamerPlayer::play(const FileHandle &file)
{
    if(!file.isNull()) {
        stop();
        g_object_set(G_OBJECT(m_source), "location", file.absFilePath().local8Bit().data(), JUK_GLIB_NULL);
    }

    gst_element_set_state(m_pipeline, GST_STATE_PLAYING);
}

void GStreamerPlayer::pause()
{
    gst_element_set_state(m_pipeline, GST_STATE_PAUSED);
}

void GStreamerPlayer::stop()
{
    gst_element_set_state(m_pipeline, GST_STATE_NULL);
}

void GStreamerPlayer::setVolume(float volume)
{
    g_object_set(G_OBJECT(m_volume), "volume", volume, JUK_GLIB_NULL);
}

float GStreamerPlayer::volume() const
{
    gdouble value;
    g_object_get(G_OBJECT(m_volume), "volume", &value, JUK_GLIB_NULL);
    return (float) value;
}

bool GStreamerPlayer::playing() const
{
    return gst_element_get_state(m_pipeline) == GST_STATE_PLAYING;
}

bool GStreamerPlayer::paused() const
{
    return gst_element_get_state(m_pipeline) == GST_STATE_PAUSED;
}

int GStreamerPlayer::totalTime() const
{
    return time(GST_QUERY_TOTAL) / GST_SECOND;
}

int GStreamerPlayer::currentTime() const
{
    return time(GST_QUERY_POSITION) / GST_SECOND;
}

int GStreamerPlayer::position() const
{
    long long total   = time(GST_QUERY_TOTAL);
    long long current = time(GST_QUERY_POSITION);
    return total > 0 ? int((double(current) / double(total)) * double(1000) + 0.5) : 0;
}

void GStreamerPlayer::seek(int seekTime)
{
    int type = (GST_FORMAT_TIME | GST_SEEK_METHOD_SET | GST_SEEK_FLAG_FLUSH);
    gst_element_seek(m_sink, GstSeekType(type), seekTime * GST_SECOND);
}

void GStreamerPlayer::seekPosition(int position)
{
    long long total = time(GST_QUERY_TOTAL);
    if(total > 0)
        seek(int(double(position) / double(1000) * double(totalTime()) + 0.5));
}

////////////////////////////////////////////////////////////////////////////////
// private methods
////////////////////////////////////////////////////////////////////////////////

void GStreamerPlayer::readConfig()
{
    KConfigGroup config(KGlobal::config(), "GStreamerPlayer");
    m_sinkName = config.readEntry("SinkName", TQString::null);
}

void GStreamerPlayer::setupPipeline()
{
    static bool initialized = false;
    if(!initialized) {
        int argc = kapp->argc();
        char **argv = kapp->argv();
        gst_init(&argc, &argv);
        initialized = true;
    }

    m_pipeline = gst_thread_new("pipeline");
    m_source   = gst_element_factory_make("filesrc", "source");
    m_decoder  = gst_element_factory_make("spider", "decoder");
    m_volume   = gst_element_factory_make("volume", "volume");

    if(!m_sinkName.isNull())
        m_sink = gst_element_factory_make(m_sinkName.utf8().data(), "sink");
    else {
        m_sink = gst_element_factory_make("alsasink", "sink");
        if(!m_sink)
            m_sink = gst_element_factory_make("osssink", "sink");            
    }
    

    gst_bin_add_many(GST_BIN(m_pipeline), m_source, m_decoder, m_volume, m_sink, 0);
    gst_element_link_many(m_source, m_decoder, m_volume, m_sink, 0);
}

long long GStreamerPlayer::time(GstQueryType type) const
{
    gint64 ns = 0;
    GstFormat format = GST_FORMAT_TIME;
    gst_element_query(m_sink, type, &format, &ns);
    return ns;
}

#else

/******************************************************************************/
/******************************************************************************/
/******************************  GSTREAMER 0.10  ******************************/
/******************************************************************************/
/******************************************************************************/

static GstBusSyncReply messageHandler(GstBus *, GstMessage *message, gpointer data)
{
    if(GST_MESSAGE_TYPE(message) == GST_MESSAGE_EOS) {
        GStreamerPlayer *player = static_cast<GStreamerPlayer *>(data);
        TQTimer::singleShot(0, player, TQT_SLOT(stop()));
    }

    gst_message_unref(message);
    return GST_BUS_DROP;
}

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

GStreamerPlayer::GStreamerPlayer() :
    Player(),
    m_playbin(0)
{
    setupPipeline();
}

GStreamerPlayer::~GStreamerPlayer()
{
    stop();
    gst_object_unref(GST_OBJECT(m_playbin));
}

void GStreamerPlayer::play(const FileHandle &file)
{
    if(!file.isNull()) {
        stop();
        gchar *uri = g_filename_to_uri(file.absFilePath().local8Bit().data(), NULL, NULL);
        g_object_set(G_OBJECT(m_playbin), "uri", uri, JUK_GLIB_NULL);
    }

    gst_element_set_state(m_playbin, GST_STATE_PLAYING);
}

void GStreamerPlayer::pause()
{
    gst_element_set_state(m_playbin, GST_STATE_PAUSED);
}

void GStreamerPlayer::stop()
{
    gst_element_set_state(m_playbin, GST_STATE_NULL);
}

void GStreamerPlayer::setVolume(float volume)
{
    g_object_set(G_OBJECT(m_playbin), "volume", volume, JUK_GLIB_NULL);
}

float GStreamerPlayer::volume() const
{
    gdouble value;
    g_object_get(G_OBJECT(m_playbin), "volume", &value, JUK_GLIB_NULL);
    return (float) value;
}

bool GStreamerPlayer::playing() const
{
    return state() == GST_STATE_PLAYING;
}

bool GStreamerPlayer::paused() const
{
    return state() == GST_STATE_PAUSED;
}

int GStreamerPlayer::totalTime() const
{
    return time(TotalLength) / GST_SECOND;
}

int GStreamerPlayer::currentTime() const
{
    return time(CurrentPosition) / GST_SECOND;
}

int GStreamerPlayer::position() const
{
    long long total   = time(TotalLength);
    long long current = time(CurrentPosition);
    return total > 0 ? int((double(current) / double(total)) * double(1000) + 0.5) : 0;
}

void GStreamerPlayer::seek(int seekTime)
{
    gst_element_seek(m_playbin, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
                     GST_SEEK_TYPE_SET, seekTime * GST_SECOND, GST_SEEK_TYPE_END, 0);
}

void GStreamerPlayer::seekPosition(int position)
{
    gint64 time = gint64((double(position) / double(1000) * double(totalTime())
                          + 0.5) * double(GST_SECOND));
    gst_element_seek(m_playbin, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
                     GST_SEEK_TYPE_SET, time, GST_SEEK_TYPE_END, 0);
}

////////////////////////////////////////////////////////////////////////////////
// private methods
////////////////////////////////////////////////////////////////////////////////

void GStreamerPlayer::setupPipeline()
{
    static bool initialized = false;
    if(!initialized) {
        int argc = kapp->argc();
        char **argv = kapp->argv();
        gst_init(&argc, &argv);
        initialized = true;
    }

    m_playbin = gst_element_factory_make("playbin", "playbin");
    gst_bus_set_sync_handler(gst_pipeline_get_bus(GST_PIPELINE(m_playbin)), messageHandler, this);
}

long long GStreamerPlayer::time(TimeQuery type) const
{
    GstQuery *query = (type == CurrentPosition)
        ? gst_query_new_position(GST_FORMAT_TIME)
        : gst_query_new_duration(GST_FORMAT_TIME);

    gint64 ns = 0;
    GstFormat format;

    if(gst_element_query(m_playbin, query))
    {
        if(type == CurrentPosition)
            gst_query_parse_position(query, &format, &ns);
        else
            gst_query_parse_duration(query, &format, &ns);
    }

    gst_query_unref(query);

    return ns;
}

GstState GStreamerPlayer::state() const
{
    GstState state;
    gst_element_get_state(m_playbin, &state, NULL, GST_CLOCK_TIME_NONE);
    return state;
}

#endif

#include "gstreamerplayer.moc"
#endif

// vim: set et sw=4: