-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPlayer.cpp
155 lines (128 loc) · 4.03 KB
/
Player.cpp
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
/*
Player.cpp - Slimmer
Copyright (C) 2016-2017 Terényi, Balázs ([email protected])
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 "Player.h"
#include "Config.h"
#include <iostream>
Player::Player(string aId, Server& aServer) : mId(aId), mServer(aServer)
{
}
void Player::update(const bool fullPlaylist)
{
Json::Value response = mServer.playerStatus(mId, fullPlaylist);
if (Config::verbose())
cout << "Status: " << response.toStyledString() << endl;
try
{
string s = response["mode"].asString();
if (s == "play")
mMode = Play;
else if (s == "stop")
mMode = Stop;
else if (s == "pause")
mMode = Pause;
else
mMode = Unknown;
mPowered = response["power"].asInt() == 1;
mPlaylistSize = response["playlist_tracks"].asInt();
s = response["playlist_cur_index"].asString();
if (s.length() > 0)
mPlaylistIndex = stoi(s);
else
mPlaylistIndex = 0;
int loopIndex = fullPlaylist ? mPlaylistIndex : 0;
mRemote = response["playlist_loop"][loopIndex]["remote"].asString() == "1";
mRemoteTitle = response["playlist_loop"][loopIndex]["remote_title"].asString();
mArtist = response["playlist_loop"][loopIndex]["artist"].asString();
mAlbum = response["playlist_loop"][loopIndex]["album"].asString();
mTitle = response["playlist_loop"][loopIndex]["title"].asString();
mDuration = response["duration"].asFloat();
mTime = response["time"].asFloat();
mVolume = response["mixer volume"].asInt();
mRepeat = response["playlist repeat"].asInt();
mShuffle = response["playlist shuffle"].asInt();
if (fullPlaylist)
mPlaylist = mPlaylistSize == 0 ? Json::arrayValue : response["playlist_loop"];
}
catch (const Json::LogicError& e)
{
cerr << "[ERROR] Status: " << response.toStyledString() << endl;
cerr << "[ERROR] Invalid JSON response from server: " << e.what() << endl;
}
}
void Player::setVolume(const int volume)
{
mServer.setPlayerVolume(mId, std::to_string(volume));
}
int Player::increaseVolume()
{
mServer.setPlayerVolume(mId, "+" + std::to_string(Config::cVolumeStep));
mVolume += Config::cVolumeStep;
if (mVolume > 100) mVolume = 100;
return mVolume;
}
int Player::decreaseVolume()
{
mServer.setPlayerVolume(mId, "-" + std::to_string(Config::cVolumeStep));
mVolume -= Config::cVolumeStep;
if (mVolume < 0) mVolume = 0;
return mVolume;
}
void Player::next()
{
mServer.playPlaylistItem(mId, "+1");
}
void Player::previous()
{
if (!mRemote && mTime > Config::cTrackRestartLimit)
{
// We restart the current track if it is played for more than cTrackRestartLimit
// seconds instead of jumping to the previous track.
this->playPlaylistItem(mPlaylistIndex);
}
else
mServer.playPlaylistItem(mId, "-1");
}
void Player::playPlaylistItem(const int index)
{
mServer.playPlaylistItem(mId, std::to_string(index));
}
void Player::playFavorite(const string& id)
{
mServer.playFavorite(mId, id);
}
void Player::playlistControl(const Server::PlaylistControlCommand cmd, const Server::PlaylistControlType type, const string& id)
{
mServer.playlistControl(mId, cmd, type, id);
}
void Player::pause()
{
mServer.pause(mId);
}
void Player::removePlaylistItem(const int index)
{
mServer.removePlaylistItem(mId, index);
}
void Player::clearPlaylist()
{
mServer.clearPlaylist(mId);
}
Json::Value Player::radios(const string& plugin, const string& id)
{
return mServer.radios(mId, plugin, id);
}
void Player::playRadio(const Server::PlaylistControlCommand cmd, const string& plugin, const string& id)
{
mServer.playRadio(mId, cmd, plugin, id);
}