Fixed crash when no gstreamer0.10-plugins-good-extra package installed
[someplayer] / src / player / player.cpp
1 /*
2  * SomePlayer - An alternate music player for Maemo 5
3  * Copyright (C) 2010 Nikolay (somebody) Tischenko <niktischenko@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19
20 #include "player.h"
21 #include <phonon/MediaSource>
22 #include <phonon/Effect>
23 #include <phonon/BackendCapabilities>
24 #include <phonon/EffectParameter>
25 #include "../config.h"
26 #include <QTime>
27
28 using namespace SomePlayer::Playback;
29 using namespace SomePlayer::DataObjects;
30 using namespace SomePlayer::Storage;
31
32 Player::Player(QObject *parent) :
33     QObject(parent)
34 {
35         _player = new Phonon::MediaObject(this);
36         _output = new Phonon::AudioOutput(Phonon::MusicCategory, this);
37         _player->setTickInterval(1000);
38         _equalizer = NULL;
39         _equalizer_enabled = false;
40         connect(_player, SIGNAL(stateChanged(Phonon::State,Phonon::State)), this, SLOT(_stateChanged(Phonon::State,Phonon::State)));
41         connect(_player, SIGNAL(tick(qint64)), this, SLOT(_tick(qint64)));
42         _path = Phonon::createPath(_player, _output);
43         QList<Phonon::EffectDescription> effects = Phonon::BackendCapabilities::availableAudioEffects();
44         foreach (Phonon::EffectDescription desc, effects) {
45                 if (desc.name() == "equalizer-10bands") {
46                         _equalizer = new Phonon::Effect(desc, this);
47                         Config config;
48                         if (config.getValue("equalizer/equalizer").toString() == "enabled") {
49                                 for (int i = 0; i < 10; i++) {
50                                         QVariant var = config.getValue(QString("equalizer/band%1").arg(i));
51                                         setEqualizerValue(i, var.toDouble());
52                                 }
53                                 enableEqualizer();
54                         } else if (config.getValue("equalizer/equalizer") == "") {
55                                 for (int i = 0; i < 10; i++) {
56                                         config.setValue(QString("equalizer/band%1").arg(i), 0);
57                                 }
58                         }
59                 }
60         }
61         int seed = QTime::currentTime().msec();
62         qsrand(seed);
63         _random = _config.getValue("playback/random").toBool();
64         _repeat = _config.getValue("playback/repeat").toBool();
65         _current = -1;
66 }
67
68 void Player::setTrackId(int id) {
69         _current = id;
70         if (!_history.isEmpty() && _history.top() != _current || _history.isEmpty()) {
71                 _history.push(_current);
72         }
73         _track = _playlist.tracks().at(_current);
74         _set_source();
75         _state = PLAYER_LOADING;
76         emit stateChanged(_state);
77 }
78
79 void Player::toggle() {
80         if (_state == PLAYER_PLAYING) { // pause
81                 _player->pause();
82                 _state = PLAYER_PAUSED;
83                 emit stateChanged(_state);
84         } else { //play
85                 play();
86         }
87 }
88
89 void Player::stop() {
90         _player->stop();
91         _state = PLAYER_STOPPED;
92         emit stateChanged(_state);
93 }
94
95 void Player::next() {
96         int count = _playlist.tracks().count();
97         if (count == 0) {
98                 stop(); // empty playlist
99                 return;
100         }
101         _history.push(_current % count);
102         if (!_queue.isEmpty()) {
103                 _current = _queue.dequeue();
104         } else if (!_prev_history.isEmpty()) {
105                 _current = _prev_history.pop();
106         } else {
107                 if (_random) {
108                         _current = (count + (qrand()  + qrand() + qrand()) % count) % count;
109                 } else {
110                         _current = _current + 1;
111                 }
112         }
113         if (_random && _history.count() >= count && !_repeat ||
114                 !_repeat && _current >= count) {
115                 _history.clear();
116                 stop();
117         } else {
118                 _current %= count;
119                 _track = _playlist.tracks().at(_current);
120                 _set_source();
121                 play();
122         }
123 }
124
125 void Player::_set_source() {
126         _player->setCurrentSource(Phonon::MediaSource(_track.source()));
127         emit trackChanged(_track);
128 }
129
130 void Player::prev() {
131         if (_history.count() > 0) {
132                 _prev_history.push(_current);
133                 _current = _history.pop();
134                 _track = _playlist.tracks().at(_current);
135         }
136         _set_source();
137         play();
138 }
139
140 void Player::_stateChanged(Phonon::State newState, Phonon::State /*oldState*/) {
141         switch (newState) {
142         case Phonon::PlayingState:
143                 if (_state == PLAYER_LOADING) {
144                         _state = PLAYER_PLAYING;
145                         emit stateChanged(_state);
146                 }
147                 break;
148         case Phonon::StoppedState:
149                 break;
150         case Phonon::LoadingState:
151                 break;
152         case Phonon::PausedState:
153                 if (_state == PLAYER_PLAYING) {
154                         next();
155                 } else if (_state == PLAYER_ERROR) {
156                         play();
157                 }
158                 break;
159         case Phonon::BufferingState:
160                 break;
161         case Phonon::ErrorState:
162                 play(); // force
163 //              _state = PLAYER_ERROR;
164                 break;
165         }
166 }
167
168 void Player::_tick(qint64 ticks) {
169         int done = ticks/1000;
170         int all = _track.metadata().length();
171         emit tick(done, all);
172         if (done+2 == all) {
173                 _track.setCount(_track.count()+1);
174                 emit trackDone(_track);
175         }
176 }
177
178 void Player::setPlaylist(Playlist playlist) {
179         _playlist = playlist;
180         _history.clear();
181         _prev_history.clear();
182         _queue.clear();
183 }
184
185 void Player::seek(int s) {
186         _player->seek(s*1000);
187 }
188
189 void Player::play() {
190         if (_playlist.tracks().isEmpty())
191                 return;
192         _state = PLAYER_PLAYING;
193         emit stateChanged(_state);
194         if (_current == -1) {
195                 _current = 0;
196                 _track = _playlist.tracks().at(0);
197                 _set_source();
198         }
199         _player->play();
200 }
201
202 void Player::enqueue(int id) {
203         _queue.enqueue(id);
204 }
205
206 void Player::toggleRandom() {
207         _random = !_random;
208         _config.setValue("playback/random", _random);
209 }
210
211 void Player::toggleRepeat() {
212         _repeat = !_repeat;
213         _config.setValue("playback/repeat", _repeat);
214 }
215
216 void Player::setVolume(int v) {
217         _output->setVolume(v*0.01);
218 }
219
220 void Player::equalizerValue(int band, double *val) {
221         if (_equalizer == NULL) {
222                 *val = 0;
223                 return;
224         }
225         if (band < 0 || band > 9) {
226                 *val = -24;
227                 return;
228         }
229         if (_equalizer_enabled) {
230                 QList<Phonon::EffectParameter> plist = _equalizer->parameters();
231                 QVariant var = _equalizer->parameterValue(plist[band]);
232                 *val = var.toDouble();
233         }
234 }
235
236 void Player::enableEqualizer() {
237         if (_equalizer == NULL)
238                 return;
239         _equalizer_enabled = true;
240         _path.insertEffect(_equalizer);
241         Config config;
242         config.setValue("equalizer/equalizer", "enabled");
243 }
244
245 void Player::disableEqualizer() {
246         if (_equalizer == NULL)
247                 return;
248         _equalizer_enabled = false;
249         _path.removeEffect(_equalizer);
250         Config config;
251         config.setValue("equalizer/equalizer", "disabled");
252 }
253
254 void Player::setEqualizerValue(int band, double value) {
255         if (_equalizer == NULL)
256                 return;
257         if (band < 0 || band > 9 || value < -24 || value > 12) {
258                 return;
259         }
260         QList<Phonon::EffectParameter> plist = _equalizer->parameters();
261         _equalizer->setParameterValue(plist[band], QVariant::fromValue(value));
262         Config config;
263         config.setValue(QString("equalizer/band%1").arg(band), value);
264 }