Parent: [824a6b] (diff)

Child: [10eeb5] (diff)

Download this file

mpd.cpp    160 lines (134 with data), 4.1 kB

  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
#include "mpdas.h"
CMPD* MPD = 0;
void CMPD::SetSong(const Song *song)
{
_cached = false;
if(song && !song->getArtist().empty() && !song->getTitle().empty()) {
_song = *song;
_gotsong = true;
iprintf("New song: %s - %s", _song.getArtist().c_str(), _song.getTitle().c_str());
AudioScrobbler->SendNowPlaying(*song);
}
else {
_gotsong = false;
}
_starttime = time(NULL);
}
void CMPD::CheckSubmit(int curplaytime)
{
if(!_gotsong || _cached || (_song.getArtist().empty() || _song.getTitle().empty())) return;
if(curplaytime - _start >= 240 || curplaytime - _start >= _song.getDuration()/2) {
Cache->AddToCache(_song, _starttime);
_cached = true;
}
}
CMPD::CMPD()
{
_conn = NULL;
_gotsong = false;
_connected = false;
_cached = false;
_songid = -1;
_songpos = -1;
if(Connect())
iprintf("%s", "Connected to MPD.");
else
eprintf("%s", "Could not connect to MPD.");
}
CMPD::~CMPD()
{
if(_conn)
mpd_connection_free(_conn);
}
bool CMPD::Connect()
{
if(_conn)
mpd_connection_free(_conn);
_conn = mpd_connection_new(Config->Get("host").c_str(), Config->GetInt("port"), 0);
_connected = _conn && mpd_connection_get_error(_conn) == MPD_ERROR_SUCCESS;
if(_connected && Config->Get("mpdpassword").size() > 0) {
_connected &= mpd_run_password(_conn, Config->Get("mpdpassword").c_str());
}
else if(!_connected) {
eprintf("MPD connection error: %s", mpd_connection_get_error_message(_conn));
}
if(_connected)
mpd_run_subscribe(_conn, "mpdas");
return _connected;
}
void CMPD::GotNewSong(struct mpd_song *song)
{
Song *s = new Song(song);
SetSong(s);
delete s;
}
void CMPD::Update()
{
if(!_connected) {
iprintf("Reconnecting in 10 seconds.");
sleep(10);
if(Connect())
iprintf("%s", "Reconnected!");
else {
eprintf("%s", "Could not reconnect.");
return;
}
}
mpd_status *status = mpd_run_status(_conn);
mpd_stats *stats = mpd_run_stats(_conn);
if(status && stats) {
int newsongid = mpd_status_get_song_id(status);
int newsongpos = mpd_status_get_elapsed_time(status);
int curplaytime = mpd_stats_get_play_time(stats);
// new song
if(newsongid != _songid) {
_songid = newsongid;
_songpos = newsongpos;
_start = curplaytime;
mpd_song *song = mpd_run_current_song(_conn);
if(song) {
GotNewSong(song);
mpd_song_free(song);
}
}
// song playing
if(newsongpos != _songpos) {
_songpos = newsongpos;
CheckSubmit(curplaytime);
}
// check for client-to-client messages
if(mpd_send_read_messages(_conn)) {
mpd_message *msg;
while((msg = mpd_recv_message(_conn)) != NULL) {
const char *text = mpd_message_get_text(msg);
if(_gotsong && text) {
if(!strncmp(text, "love", 4)) {
AudioScrobbler->LoveTrack(_song);
}
else if(!strncmp(text, "unlove", 6)) {
AudioScrobbler->LoveTrack(_song, true);
}
}
mpd_message_free(msg);
}
mpd_response_finish(_conn);
}
mpd_status_free(status);
mpd_stats_free(stats);
}
else { // we have most likely lost our connection
eprintf("Could not query MPD server: %s", mpd_connection_get_error_message(_conn));
_connected = false;
}
}
Song::Song(struct mpd_song *song)
{
const char* temp;
temp = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
artist = temp ? temp : "";
temp = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
title = temp ? temp : "";
temp = mpd_song_get_tag(song, MPD_TAG_ALBUM, 0);
album = temp ? temp : "";
duration = mpd_song_get_duration(song);
}