~ubuntu-branches/ubuntu/breezy/kdemultimedia/breezy

« back to all changes in this revision

Viewing changes to juk/gstreamerplayer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2005-03-24 04:48:58 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050324044858-8ff88o9jxej6ii3d
Tags: 4:3.4.0-0ubuntu3
Add kubuntu_02_hide_arts_menu_entries.diff to hide artsbuilder and artscontrol k-menu entries

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
    copyright            : (C) 2004 Scott Wheeler
 
3
    email                : wheeler@kde.org
 
4
***************************************************************************/
 
5
 
 
6
/***************************************************************************
 
7
 *                                                                         *
 
8
 *   This program is free software; you can redistribute it and/or modify  *
 
9
 *   it under the terms of the GNU General Public License as published by  *
 
10
 *   the Free Software Foundation; either version 2 of the License, or     *
 
11
 *   (at your option) any later version.                                   *
 
12
 *                                                                         *
 
13
 ***************************************************************************/
 
14
 
 
15
#include "gstreamerplayer.h"
 
16
 
 
17
#if HAVE_GSTREAMER
 
18
 
 
19
#include <kapplication.h>
 
20
#include <kconfig.h>
 
21
#include <kglobal.h>
 
22
#include <kdebug.h>
 
23
 
 
24
#include <qfile.h>
 
25
 
 
26
////////////////////////////////////////////////////////////////////////////////
 
27
// public methods
 
28
////////////////////////////////////////////////////////////////////////////////
 
29
 
 
30
GStreamerPlayer::GStreamerPlayer() :
 
31
    Player(),
 
32
    m_pipeline(0),
 
33
    m_source(0),
 
34
    m_decoder(0),
 
35
    m_volume(0),
 
36
    m_sink(0)
 
37
{
 
38
    readConfig();
 
39
    setupPipeline();
 
40
}
 
41
 
 
42
GStreamerPlayer::~GStreamerPlayer()
 
43
{
 
44
    stop();
 
45
    gst_object_unref(GST_OBJECT(m_pipeline));
 
46
}
 
47
 
 
48
void GStreamerPlayer::play(const FileHandle &file)
 
49
{
 
50
    if(!file.isNull()) {
 
51
        stop();
 
52
        g_object_set(G_OBJECT(m_source), "location", file.absFilePath().local8Bit().data(), 0);
 
53
    }
 
54
 
 
55
    gst_element_set_state(m_pipeline, GST_STATE_PLAYING);
 
56
}
 
57
 
 
58
void GStreamerPlayer::pause()
 
59
{
 
60
    gst_element_set_state(m_pipeline, GST_STATE_PAUSED);
 
61
}
 
62
 
 
63
void GStreamerPlayer::stop()
 
64
{
 
65
    gst_element_set_state(m_pipeline, GST_STATE_NULL);
 
66
}
 
67
 
 
68
void GStreamerPlayer::setVolume(float volume)
 
69
{
 
70
    g_object_set(G_OBJECT(m_volume), "volume", volume, 0);
 
71
}
 
72
 
 
73
float GStreamerPlayer::volume() const
 
74
{
 
75
    gdouble value;
 
76
    g_object_get(G_OBJECT(m_volume), "volume", &value, 0);
 
77
    return (float) value;
 
78
}
 
79
 
 
80
bool GStreamerPlayer::playing() const
 
81
{
 
82
    return gst_element_get_state(m_pipeline) == GST_STATE_PLAYING;
 
83
}
 
84
 
 
85
bool GStreamerPlayer::paused() const
 
86
{
 
87
    return gst_element_get_state(m_pipeline) == GST_STATE_PAUSED;
 
88
}
 
89
 
 
90
int GStreamerPlayer::totalTime() const
 
91
{
 
92
    return time(GST_QUERY_TOTAL) / GST_SECOND;
 
93
}
 
94
 
 
95
int GStreamerPlayer::currentTime() const
 
96
{
 
97
    return time(GST_QUERY_POSITION) / GST_SECOND;
 
98
}
 
99
 
 
100
int GStreamerPlayer::position() const
 
101
{
 
102
    long long total   = time(GST_QUERY_TOTAL);
 
103
    long long current = time(GST_QUERY_POSITION);
 
104
    return total > 0 ? int((double(current) / double(total)) * double(1000) + 0.5) : 0;
 
105
}
 
106
 
 
107
void GStreamerPlayer::seek(int seekTime)
 
108
{
 
109
    int type = (GST_FORMAT_TIME | GST_SEEK_METHOD_SET | GST_SEEK_FLAG_FLUSH);
 
110
    gst_element_seek(m_sink, GstSeekType(type), seekTime * GST_SECOND);
 
111
}
 
112
 
 
113
void GStreamerPlayer::seekPosition(int position)
 
114
{
 
115
    long long total = time(GST_QUERY_TOTAL);
 
116
    if(total > 0)
 
117
        seek(int(double(position) / double(1000) * double(totalTime()) + 0.5));
 
118
}
 
119
 
 
120
////////////////////////////////////////////////////////////////////////////////
 
121
// private methods
 
122
////////////////////////////////////////////////////////////////////////////////
 
123
 
 
124
void GStreamerPlayer::readConfig()
 
125
{
 
126
    KConfigGroup config(KGlobal::config(), "GStreamerPlayer");
 
127
    m_sinkName = config.readEntry("SinkName", QString::null);
 
128
}
 
129
 
 
130
void GStreamerPlayer::setupPipeline()
 
131
{
 
132
    static bool initialized = false;
 
133
    if(!initialized) {
 
134
        int argc = kapp->argc();
 
135
        char **argv = kapp->argv();
 
136
        gst_init(&argc, &argv);
 
137
        initialized = true;
 
138
    }
 
139
 
 
140
    m_pipeline = gst_thread_new("pipeline");
 
141
    m_source   = gst_element_factory_make("filesrc", "source");
 
142
    m_decoder  = gst_element_factory_make("spider", "decoder");
 
143
    m_volume   = gst_element_factory_make("volume", "volume");
 
144
 
 
145
    if(!m_sinkName.isNull())
 
146
        m_sink = gst_element_factory_make(m_sinkName.utf8().data(), "sink");
 
147
    else {
 
148
        m_sink = gst_element_factory_make("alsasink", "sink");
 
149
        if(!m_sink)
 
150
            m_sink = gst_element_factory_make("osssink", "sink");            
 
151
    }
 
152
    
 
153
 
 
154
    gst_bin_add_many(GST_BIN(m_pipeline), m_source, m_decoder, m_volume, m_sink, 0);
 
155
    gst_element_link_many(m_source, m_decoder, m_volume, m_sink, 0);
 
156
}
 
157
 
 
158
long long GStreamerPlayer::time(GstQueryType type) const
 
159
{
 
160
    gint64 ns = 0;
 
161
    GstFormat format = GST_FORMAT_TIME;
 
162
    gst_element_query(m_sink, type, &format, &ns);
 
163
    return ns;
 
164
}
 
165
 
 
166
#include "gstreamerplayer.moc"
 
167
#endif
 
168
 
 
169
// vim: set et sw=4: