Newer
Older
/*
* Copyright (C) 2019 Kevin Whitaker <eyecreate@eyecreate.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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mprisinterface.h"
#include <qcryptographichash.h>
#include <QtDBus/QDBusMessage>
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
MPRISInterface::MPRISInterface(QObject* parent) : QDBusAbstractAdaptor(parent)
{
}
bool MPRISInterface::CanQuit()
{
return false;
}
bool MPRISInterface::CanRaise()
{
return false;
}
bool MPRISInterface::HasTrackList()
{
return false; //TODO:this might have value implementing.
}
QString MPRISInterface::Identity()
{
return "Simple Cast";
}
QStringList MPRISInterface::SupportedMimeTypes()
{
return {"audio/ogg","audio/mpeg","audio/webm"};
}
QStringList MPRISInterface::SupportedUriSchemes()
{
return {"https","http"};
}
MPRISPlayerInterface::MPRISPlayerInterface(MiniMediaPlayer *player,QObject* parent) : QDBusAbstractAdaptor(parent),player(player)
{
//Connected Changed values from media player.
connect(player,&MiniMediaPlayer::playStateChanged,this,&MPRISPlayerInterface::mediaStateChanged);
connect(player,&MiniMediaPlayer::playlistChanged,this,&MPRISPlayerInterface::mediaPlaylistChanged);
connect(player,&MiniMediaPlayer::mediaVolumeChanged,this,&MPRISPlayerInterface::mediaVolumeChanged);
connect(player,&MiniMediaPlayer::trackPositionChanged,this,&MPRISPlayerInterface::mediaPositionChanged);
}
void MPRISPlayerInterface::dbusUpdate(QVariantMap changed)
{
//Workaround for qt dbus not sending this signal
QDBusMessage msg = QDBusMessage::createSignal("/org/mpris/MediaPlayer2","org.freedesktop.DBus.Properties","PropertiesChanged");
msg << "org.mpris.MediaPlayer2.Player";
msg << changed;
msg << QStringList();
QDBusConnection::sessionBus().send(msg);
}
void MPRISPlayerInterface::mediaPositionChanged(qint64 position)
{
emit positionChanged(Position());
QVariantMap map;
map.insert("Position",Position());
dbusUpdate(map);
void MPRISPlayerInterface::mediaStateChanged(bool isPlaying)
{
emit playbackStatusChanged(PlaybackStatus());
QVariantMap map;
map.insert("PlaybackStatus",PlaybackStatus());
dbusUpdate(map);
}
void MPRISPlayerInterface::mediaVolumeChanged(int volume)
{
emit volumeChanged(Volume());
QVariantMap map;
map.insert("Volume",Volume());
dbusUpdate(map);
}
void MPRISPlayerInterface::mediaPlaylistChanged(QStringList playlist)
{
emit metadataChanged(Metadata());
QVariantMap map;
map.insert("Metadata",Metadata());
dbusUpdate(map);
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
}
bool MPRISPlayerInterface::CanControl()
{
return true;
}
bool MPRISPlayerInterface::CanGoNext()
{
return true;
}
bool MPRISPlayerInterface::CanGoPrevious()
{
return false;
}
bool MPRISPlayerInterface::CanSeek()
{
return false;
}
bool MPRISPlayerInterface::CanPause()
{
return true;
}
bool MPRISPlayerInterface::CanPlay()
{
return true;
}
double MPRISPlayerInterface::MaximumRate()
{
return 1.0;
}
double MPRISPlayerInterface::MinimumRate()
{
return 1.0;
}
void MPRISPlayerInterface::OpenUri(QString uri)
{
//TODO:this might be another way to add tracks
}
void MPRISPlayerInterface::Next()
{
player->nextTrack();
}
void MPRISPlayerInterface::Pause()
{
player->playPauseTrack(false);
}
void MPRISPlayerInterface::Play()
{
player->playPauseTrack(true);
}
void MPRISPlayerInterface::PlayPause()
{
player->playPauseTrack(!player->isPlaying());
}
void MPRISPlayerInterface::Stop()
{
//TODO: think if worth implementing.
}
QString MPRISPlayerInterface::PlaybackStatus()
{
if(player->getTrackNames().size() == 0)
{
return "Stopped";
}
else if(player->isPlaying()) {
return "Playing";
}
else
{
return "Paused";
}
}
qlonglong MPRISPlayerInterface::Position()
{
return player->getCurrentPosition() * 1000; //milliseconds to microseconds
}
double MPRISPlayerInterface::Rate()
{
return 1.0;
}
double MPRISPlayerInterface::Volume()
{
return player->getVolume();
}
void MPRISPlayerInterface::setVolume(double volume)
{
if(volume < 0) player->setVolume(0);
player->setVolume(volume*100); //QMediaPlayer uses 0 to 100
}
QVariantMap MPRISPlayerInterface::Metadata()
QVariantMap map;
if(player->getTrackNames().size() > 0)
{
map.insert("mpris:trackid",QVariant("org.mpris.MediaPlayer2.simplecast.Player."+QString(QCryptographicHash::hash(player->getTrackNames()[0].toUtf8(),QCryptographicHash::Algorithm::Md5))));
map.insert("mpris:length",QVariant(player->getTrackDuration()*1000));
map.insert("xesam:title",QVariant(player->getTrackNames()[0]));
}